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