MDL-53645 tool_lp: Address FIXME comments
[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();
3c230247
FM
26
27use tool_lp\plan;
6cc8480a
FM
28use tool_lp\user_competency;
29
30global $CFG;
31
32/**
33 * Lib testcase.
34 *
35 * @package tool_lp
36 * @copyright 2015 Frédéric Massart - FMCorz.net
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 */
39class tool_lp_lib_testcase extends advanced_testcase {
40
41 public function test_comment_add_user_competency() {
3b167714 42 global $DB;
6cc8480a
FM
43 $this->resetAfterTest();
44 $dg = $this->getDataGenerator();
45 $lpg = $dg->get_plugin_generator('tool_lp');
46
47 $u1 = $dg->create_user();
48 $u2 = $dg->create_user();
49 $u3 = $dg->create_user();
50 $reviewerroleid = $dg->create_role();
51 assign_capability('tool/lp:planview', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
943989c2 52 assign_capability('tool/lp:usercompetencycomment', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
6cc8480a
FM
53 $dg->role_assign($reviewerroleid, $u2->id, context_user::instance($u1->id));
54 $dg->role_assign($reviewerroleid, $u3->id, context_user::instance($u1->id));
55 accesslib_clear_all_caches_for_unit_testing();
56
57 $f1 = $lpg->create_framework();
58 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 1 plan.
59 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // In 2 plans.
60 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get_id())); // Orphan.
61
62 $p1 = $lpg->create_plan(array('userid' => $u1->id));
63 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c1->get_id()));
64 $lpg->create_plan_competency(array('planid' => $p1->get_id(), 'competencyid' => $c2->get_id()));
65 $p2 = $lpg->create_plan(array('userid' => $u1->id));
66 $lpg->create_plan_competency(array('planid' => $p2->get_id(), 'competencyid' => $c2->get_id()));
67
3b167714
FM
68 $DB->set_field(plan::TABLE, 'timemodified', 1, array('id' => $p1->get_id())); // Make plan 1 appear as old.
69 $p1->read();
70
6cc8480a
FM
71 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c1->get_id(),
72 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
73 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c2->get_id(),
74 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
75 $uc3 = $lpg->create_user_competency(array('userid' => $u1->id, 'competencyid' => $c3->get_id(),
76 'status' => user_competency::STATUS_IN_REVIEW, 'reviewerid' => $u2->id));
77
78 // Post a comment for the user competency being in one plan. The reviewer is messaged.
79 $this->setUser($u1);
80 $comment = $uc1->get_comment_object();
81 $sink = $this->redirectMessages();
82 $comment->add('Hello world!');
83 $messages = $sink->get_messages();
84 $sink->close();
85 $this->assertCount(1, $messages);
86 $message = array_pop($messages);
87
88 $expectedurlname = $c1->get_shortname();
89 $expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
90 'userid' => $u1->id,
91 'competencyid' => $c1->get_id(),
92 'planid' => $p1->get_id()
93 ));
94 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
95 $this->assertEquals($u2->id, $message->useridto);
96 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
97 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
98 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
99 $this->assertEquals($expectedurl->out(false), $message->contexturl);
100 $this->assertEquals($expectedurlname, $message->contexturlname);
101
102 // Reviewer posts a comment for the user competency being in two plans. Owner is messaged.
103 $this->setUser($u2);
104 $comment = $uc2->get_comment_object();
105 $sink = $this->redirectMessages();
106 $comment->add('Hello world!');
107 $messages = $sink->get_messages();
108 $sink->close();
109 $this->assertCount(1, $messages);
110 $message = array_pop($messages);
111
112 $expectedurlname = $c2->get_shortname();
113 $expectedurl = new moodle_url('/admin/tool/lp/user_competency_in_plan.php', array(
114 'userid' => $u1->id,
115 'competencyid' => $c2->get_id(),
3b167714 116 'planid' => $p2->get_id()
6cc8480a
FM
117 ));
118 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
119 $this->assertEquals($u1->id, $message->useridto);
120 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
121 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
122 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
123 $this->assertEquals($expectedurl->out(false), $message->contexturl);
124 $this->assertEquals($expectedurlname, $message->contexturlname);
125
126 // Reviewer posts a comment for the user competency being in no plans. User is messaged.
127 $this->setUser($u2);
128 $comment = $uc3->get_comment_object();
129 $sink = $this->redirectMessages();
130 $comment->add('Hello world!');
131 $messages = $sink->get_messages();
132 $sink->close();
133 $this->assertCount(1, $messages);
134 $message = array_pop($messages);
135
136 $expectedurlname = get_string('userplans', 'tool_lp');
137 $expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
138 'userid' => $u1->id,
139 ));
140 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
141 $this->assertEquals($u1->id, $message->useridto);
142 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
143 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
144 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
145 $this->assertEquals($expectedurl->out(false), $message->contexturl);
146 $this->assertEquals($expectedurlname, $message->contexturlname);
147
148 // A comment is posted by another user, reviewer and owner are messaged.
149 $this->setUser($u3);
150 $comment = $uc3->get_comment_object();
151 $sink = $this->redirectMessages();
152 $comment->add('Hello world!');
153 $messages = $sink->get_messages();
154 $sink->close();
155 $this->assertCount(2, $messages);
156 $message1 = array_shift($messages);
157 $message2 = array_shift($messages);
158 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
159 $this->assertEquals($u1->id, $message1->useridto);
160 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
161 $this->assertEquals($u2->id, $message2->useridto);
162
163 // A comment is posted in HTML.
164 $this->setUser($u2);
165 $comment = $uc3->get_comment_object();
166 $sink = $this->redirectMessages();
167 $comment->add('<em>Hello world!</em>', FORMAT_HTML);
168 $messages = $sink->get_messages();
169 $sink->close();
170 $this->assertCount(1, $messages);
171 $message = array_pop($messages);
172
173 $expectedurlname = get_string('userplans', 'tool_lp');
174 $expectedurl = new moodle_url('/admin/tool/lp/plans.php', array(
175 'userid' => $u1->id,
176 ));
177 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
178 $this->assertEquals($u1->id, $message->useridto);
179 $this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
180 $this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
181 $this->assertEquals(FORMAT_HTML, $message->fullmessageformat);
182 $this->assertEquals($expectedurl->out(false), $message->contexturl);
183 $this->assertEquals($expectedurlname, $message->contexturlname);
184 }
185
3c230247
FM
186 /**
187 * Commenting on a plan.
188 */
189 public function test_comment_add_plan() {
190 $this->resetAfterTest();
191 $dg = $this->getDataGenerator();
192 $lpg = $dg->get_plugin_generator('tool_lp');
193
194 $u1 = $dg->create_user();
195 $u2 = $dg->create_user();
196 $u3 = $dg->create_user();
197 $userroleid = $dg->create_role();
198 $reviewerroleid = $dg->create_role();
199 assign_capability('tool/lp:planviewowndraft', CAP_ALLOW, $userroleid, context_system::instance()->id, true);
200 assign_capability('tool/lp:planviewown', CAP_ALLOW, $userroleid, context_system::instance()->id, true);
201 assign_capability('tool/lp:planviewdraft', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
202 assign_capability('tool/lp:planmanage', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
203 assign_capability('tool/lp:plancomment', CAP_ALLOW, $reviewerroleid, context_system::instance()->id, true);
204 $dg->role_assign($userroleid, $u1->id, context_user::instance($u1->id));
205 $dg->role_assign($reviewerroleid, $u2->id, context_user::instance($u1->id));
206 $dg->role_assign($reviewerroleid, $u3->id, context_system::instance());
207 accesslib_clear_all_caches_for_unit_testing();
208
209 $p1 = $lpg->create_plan(array('userid' => $u1->id));
210
211 // Post a comment in own plan, no reviewer. Nobody is messaged.
212 $this->setUser($u1);
213 $comment = $p1->get_comment_object();
214 $sink = $this->redirectMessages();
215 $comment->add('Hello world!');
216 $messages = $sink->get_messages();
217 $sink->close();
218 $this->assertCount(0, $messages);
219
220 // Post a comment in plan as someone else, no reviewer. The owner is messages.
221 $this->setUser($u3);
222 $comment = $p1->get_comment_object();
223 $sink = $this->redirectMessages();
224 $comment->add('Hello world!');
225 $messages = $sink->get_messages();
226 $sink->close();
227 $this->assertCount(1, $messages);
228 $message = array_pop($messages);
229 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
230 $this->assertEquals($u1->id, $message->useridto);
231
232 // Post a comment in a plan with reviewer. The reviewer is messaged.
233 $p1->set_reviewerid($u2->id);
234 $p1->update();
235 $this->setUser($u1);
236 $comment = $p1->get_comment_object();
237 $sink = $this->redirectMessages();
238 $comment->add('Hello world!');
239 $messages = $sink->get_messages();
240 $sink->close();
241 $this->assertCount(1, $messages);
242 $message = array_pop($messages);
243 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
244 $this->assertEquals($u2->id, $message->useridto);
245
246 // Post a comment as reviewer in a plan being reviewed. The owner is messaged.
247 $p1->set_reviewerid($u2->id);
248 $p1->update();
249 $this->setUser($u2);
250 $comment = $p1->get_comment_object();
251 $sink = $this->redirectMessages();
252 $comment->add('Hello world!');
253 $messages = $sink->get_messages();
254 $sink->close();
255 $this->assertCount(1, $messages);
256 $message = array_pop($messages);
257 $this->assertEquals(core_user::get_noreply_user()->id, $message->useridfrom);
258 $this->assertEquals($u1->id, $message->useridto);
259
260 // Post a comment as someone else in a plan being reviewed. The owner and reviewer are messaged.
261 $p1->set_reviewerid($u2->id);
262 $p1->update();
263 $this->setUser($u3);
264 $comment = $p1->get_comment_object();
265 $sink = $this->redirectMessages();
266 $comment->add('Hello world!');
267 $messages = $sink->get_messages();
268 $sink->close();
269 $this->assertCount(2, $messages);
270 $message1 = array_shift($messages);
271 $message2 = array_shift($messages);
272 $this->assertEquals(core_user::get_noreply_user()->id, $message1->useridfrom);
273 $this->assertEquals($u1->id, $message1->useridto);
274 $this->assertEquals(core_user::get_noreply_user()->id, $message2->useridfrom);
275 $this->assertEquals($u2->id, $message2->useridto);
276
277 $p1->set_reviewerid(null);
278 $p1->update();
279
280 // Test message content.
281 $this->setUser($u3);
282 $comment = $p1->get_comment_object();
283 $sink = $this->redirectMessages();
284 $comment->add('Hello world!');
285 $messages = $sink->get_messages();
286 $sink->close();
287 $this->assertCount(1, $messages);
288 $message = array_pop($messages);
289
290 $expectedurlname = $p1->get_name();
291 $expectedurl = new moodle_url('/admin/tool/lp/plan.php', array(
292 'id' => $p1->get_id()
293 ));
294 $this->assertTrue(strpos($message->fullmessage, 'Hello world!') !== false);
295 $this->assertTrue(strpos($message->fullmessagehtml, 'Hello world!') !== false);
296 $this->assertEquals(FORMAT_MOODLE, $message->fullmessageformat);
297 $this->assertEquals($expectedurl->out(false), $message->contexturl);
298 $this->assertEquals($expectedurlname, $message->contexturlname);
299
300 // Test message content as HTML.
301 $this->setUser($u3);
302 $comment = $p1->get_comment_object();
303 $sink = $this->redirectMessages();
304 $comment->add('<em>Hello world!</em>', FORMAT_HTML);
305 $messages = $sink->get_messages();
306 $sink->close();
307 $this->assertCount(1, $messages);
308 $message = array_pop($messages);
309
310 $expectedurlname = $p1->get_name();
311 $expectedurl = new moodle_url('/admin/tool/lp/plan.php', array(
312 'id' => $p1->get_id()
313 ));
314 $this->assertTrue(strpos($message->fullmessage, '<em>Hello world!</em>') !== false);
315 $this->assertTrue(strpos($message->fullmessagehtml, '<em>Hello world!</em>') !== false);
316 $this->assertEquals(FORMAT_HTML, $message->fullmessageformat);
317 $this->assertEquals($expectedurl->out(false), $message->contexturl);
318 $this->assertEquals($expectedurlname, $message->contexturlname);
319 }
320
6cc8480a 321}