MDL-51036 tool_lp: Sensible messages sent after commenting on competency
[moodle.git] / admin / tool / lp / tests / lib_test.php
CommitLineData
6cc8480a
FM
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Lib tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26use tool_lp\user_competency;
27
28global $CFG;
29
30/**
31 * Lib testcase.
32 *
33 * @package tool_lp
34 * @copyright 2015 Frédéric Massart - FMCorz.net
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_lib_testcase extends advanced_testcase {
38
39 public function test_comment_add_user_competency() {
40 $this->resetAfterTest();
41 $dg = $this->getDataGenerator();
42 $lpg = $dg->get_plugin_generator('tool_lp');
43
44 $u1 = $dg->create_user();
45 $u2 = $dg->create_user();
46 $u3 = $dg->create_user();
47 $reviewerroleid = $dg->create_role();
48 assign_capability('tool/lp:planview', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
49 $dg->role_assign($reviewerroleid, $u2->id, context_user::instance($u1->id));
50 $dg->role_assign($reviewerroleid, $u3->id, context_user::instance($u1->id));
51 accesslib_clear_all_caches_for_unit_testing();
52
53 $f1 = $lpg->create_framework();
54 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 1 plan.
55 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 2 plans.
56 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // Orphan.
57
58 $p1 = $lpg->create_plan(array('userid' => $u1->id));
59 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c1->get_id()));
60 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c2->get_id()));
61 $p2 = $lpg->create_plan(array('userid' => $u1->id));
62 $lpg->create_plan_competency(array('planid' => $p2->get_id(), 'competencyid' => $c2->get_id()));
63
64 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c1->get_id(),
65 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
66 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c2->get_id(),
67 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
68 $uc3 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get_id(),
69 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
70
71 // Post a comment for the user competency being in one plan. The reviewer is messaged.
72 $this->setUser($u1);
73 $comment = $uc1->get_comment_object();
74 $sink = $this->redirectMessages();
75 $comment->add('Hello world!');
76 $messages = $sink->get_messages();
77 $sink->close();
78 $this->assertCount(1, $messages);
79 $message = array_pop($messages);
80
81 $expectedurlname = $c1->get_shortname();
82 $expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
83 'userid' => $u1->id,
84 'competencyid' => $c1->get_id(),
85 'planid' => $p1->get_id()
86 ));
87 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
88 $this->assertEquals($u2->id, $message->useridto);
89 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
90 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
91 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
92 $this->assertEquals($expectedurl->out(false), $message->contexturl);
93 $this->assertEquals($expectedurlname, $message->contexturlname);
94
95 // Reviewer posts a comment for the user competency being in two plans. Owner is messaged.
96 $this->setUser($u2);
97 $comment = $uc2->get_comment_object();
98 $sink = $this->redirectMessages();
99 $comment->add('Hello world!');
100 $messages = $sink->get_messages();
101 $sink->close();
102 $this->assertCount(1, $messages);
103 $message = array_pop($messages);
104
105 $expectedurlname = $c2->get_shortname();
106 $expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
107 'userid' => $u1->id,
108 'competencyid' => $c2->get_id(),
109 'planid' => $p1->get_id()
110 ));
111 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
112 $this->assertEquals($u1->id, $message->useridto);
113 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
114 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
115 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
116 $this->assertEquals($expectedurl->out(false), $message->contexturl);
117 $this->assertEquals($expectedurlname, $message->contexturlname);
118
119 // Reviewer posts a comment for the user competency being in no plans. User is messaged.
120 $this->setUser($u2);
121 $comment = $uc3->get_comment_object();
122 $sink = $this->redirectMessages();
123 $comment->add('Hello world!');
124 $messages = $sink->get_messages();
125 $sink->close();
126 $this->assertCount(1, $messages);
127 $message = array_pop($messages);
128
129 $expectedurlname = get_string('userplans', 'tool_lp');
130 $expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
131 'userid' => $u1->id,
132 ));
133 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
134 $this->assertEquals($u1->id, $message->useridto);
135 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
136 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
137 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
138 $this->assertEquals($expectedurl->out(false), $message->contexturl);
139 $this->assertEquals($expectedurlname, $message->contexturlname);
140
141 // A comment is posted by another user, reviewer and owner are messaged.
142 $this->setUser($u3);
143 $comment = $uc3->get_comment_object();
144 $sink = $this->redirectMessages();
145 $comment->add('Hello world!');
146 $messages = $sink->get_messages();
147 $sink->close();
148 $this->assertCount(2, $messages);
149 $message1 = array_shift($messages);
150 $message2 = array_shift($messages);
151 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
152 $this->assertEquals($u1->id, $message1->useridto);
153 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
154 $this->assertEquals($u2->id, $message2->useridto);
155
156 // A comment is posted in HTML.
157 $this->setUser($u2);
158 $comment = $uc3->get_comment_object();
159 $sink = $this->redirectMessages();
160 $comment->add('<em>Hello world!</em>', FORMAT_HTML);
161 $messages = $sink->get_messages();
162 $sink->close();
163 $this->assertCount(1, $messages);
164 $message = array_pop($messages);
165
166 $expectedurlname = get_string('userplans', 'tool_lp');
167 $expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
168 'userid' => $u1->id,
169 ));
170 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
171 $this->assertEquals($u1->id, $message->useridto);
172 $this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
173 $this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
174 $this->assertEquals(FORMAT_HTML, $message->fullmessageformat);
175 $this->assertEquals($expectedurl->out(false), $message->contexturl);
176 $this->assertEquals($expectedurlname, $message->contexturlname);
177 }
178
179}