2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
20 * @package core_competency
21 * @copyright 2015 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 use core_competency\plan
;
28 use core_competency\url
;
29 use core_competency\user_competency
;
36 * @package core_competency
37 * @copyright 2015 Frédéric Massart - FMCorz.net
38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
40 class core_competency_lib_testcase
extends advanced_testcase
{
42 public function test_comment_add_user_competency() {
44 $this->resetAfterTest();
45 $dg = $this->getDataGenerator();
46 $lpg = $dg->get_plugin_generator('core_competency');
48 $u1 = $dg->create_user(['picture' => 1]);
49 $u2 = $dg->create_user();
50 $u3 = $dg->create_user();
51 $reviewerroleid = $dg->create_role();
52 assign_capability('moodle/competency:planview', CAP_ALLOW
, $reviewerroleid, context_system
::instance()->id
, true);
53 assign_capability('moodle/competency:usercompetencycomment', CAP_ALLOW
, $reviewerroleid,
54 context_system
::instance()->id
, true);
55 $dg->role_assign($reviewerroleid, $u2->id
, context_user
::instance($u1->id
));
56 $dg->role_assign($reviewerroleid, $u3->id
, context_user
::instance($u1->id
));
57 accesslib_clear_all_caches_for_unit_testing();
59 $f1 = $lpg->create_framework();
60 $c1 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); // In 1 plan.
61 $c2 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); // In 2 plans.
62 $c3 = $lpg->create_competency(array('competencyframeworkid' => $f1->get('id'))); // Orphan.
64 $p1 = $lpg->create_plan(array('userid' => $u1->id
));
65 $lpg->create_plan_competency(array('planid' => $p1->get('id'), 'competencyid' => $c1->get('id')));
66 $lpg->create_plan_competency(array('planid' => $p1->get('id'), 'competencyid' => $c2->get('id')));
67 $p2 = $lpg->create_plan(array('userid' => $u1->id
));
68 $lpg->create_plan_competency(array('planid' => $p2->get('id'), 'competencyid' => $c2->get('id')));
70 $DB->set_field(plan
::TABLE
, 'timemodified', 1, array('id' => $p1->get('id'))); // Make plan 1 appear as old.
73 $uc1 = $lpg->create_user_competency(array('userid' => $u1->id
, 'competencyid' => $c1->get('id'),
74 'status' => user_competency
::STATUS_IN_REVIEW
, 'reviewerid' => $u2->id
));
75 $uc2 = $lpg->create_user_competency(array('userid' => $u1->id
, 'competencyid' => $c2->get('id'),
76 'status' => user_competency
::STATUS_IN_REVIEW
, 'reviewerid' => $u2->id
));
77 $uc3 = $lpg->create_user_competency(array('userid' => $u1->id
, 'competencyid' => $c3->get('id'),
78 'status' => user_competency
::STATUS_IN_REVIEW
, 'reviewerid' => $u2->id
));
80 // Post a comment for the user competency being in one plan. The reviewer is messaged.
82 $comment = $uc1->get_comment_object();
83 $sink = $this->redirectMessages();
84 $comment->add('Hello world!');
85 $messages = $sink->get_messages();
87 $this->assertCount(1, $messages);
88 $message = array_pop($messages);
90 $expectedurlname = $c1->get('shortname');
91 $expectedurl = url
::user_competency_in_plan($u1->id
, $c1->get('id'), $p1->get('id'));
92 $this->assertEquals(core_user
::get_noreply_user()->id
, $message->useridfrom
);
93 $this->assertEquals($u2->id
, $message->useridto
);
94 $this->assertTrue(strpos($message->fullmessage
, 'Hello world!') !== false);
95 $this->assertTrue(strpos($message->fullmessagehtml
, 'Hello world!') !== false);
96 $this->assertEquals(FORMAT_MOODLE
, $message->fullmessageformat
);
97 $this->assertEquals($expectedurl->out(false), $message->contexturl
);
98 $this->assertEquals($expectedurlname, $message->contexturlname
);
100 $customdata = json_decode($message->customdata
);
101 $this->assertObjectHasAttribute('notificationiconurl', $customdata);
102 $this->assertStringContainsString('tokenpluginfile.php', $customdata->notificationiconurl
);
103 $userpicture = new \
user_picture($u1);
104 $userpicture->size
= 1; // Use f1 size.
105 $userpicture->includetoken
= $u2->id
;
106 $this->assertEquals($userpicture->get_url($PAGE)->out(false), $customdata->notificationiconurl
);
108 // Reviewer posts a comment for the user competency being in two plans. Owner is messaged.
110 $comment = $uc2->get_comment_object();
111 $sink = $this->redirectMessages();
112 $comment->add('Hello world!');
113 $messages = $sink->get_messages();
115 $this->assertCount(1, $messages);
116 $message = array_pop($messages);
118 $expectedurlname = $c2->get('shortname');
119 $expectedurl = url
::user_competency_in_plan($u1->id
, $c2->get('id'), $p2->get('id'));
120 $this->assertEquals(core_user
::get_noreply_user()->id
, $message->useridfrom
);
121 $this->assertEquals($u1->id
, $message->useridto
);
122 $this->assertTrue(strpos($message->fullmessage
, 'Hello world!') !== false);
123 $this->assertTrue(strpos($message->fullmessagehtml
, 'Hello world!') !== false);
124 $this->assertEquals(FORMAT_MOODLE
, $message->fullmessageformat
);
125 $this->assertEquals($expectedurl->out(false), $message->contexturl
);
126 $this->assertEquals($expectedurlname, $message->contexturlname
);
128 // Reviewer posts a comment for the user competency being in no plans. User is messaged.
130 $comment = $uc3->get_comment_object();
131 $sink = $this->redirectMessages();
132 $comment->add('Hello world!');
133 $messages = $sink->get_messages();
135 $this->assertCount(1, $messages);
136 $message = array_pop($messages);
138 $expectedurlname = get_string('userplans', 'core_competency');
139 $expectedurl = url
::plans($u1->id
);
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
);
148 // A comment is posted by another user, reviewer and owner are messaged.
150 $comment = $uc3->get_comment_object();
151 $sink = $this->redirectMessages();
152 $comment->add('Hello world!');
153 $messages = $sink->get_messages();
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
);
163 // A comment is posted in HTML.
165 $comment = $uc3->get_comment_object();
166 $sink = $this->redirectMessages();
167 $comment->add('<em>Hello world!</em>', FORMAT_HTML
);
168 $messages = $sink->get_messages();
170 $this->assertCount(1, $messages);
171 $message = array_pop($messages);
173 $expectedurlname = get_string('userplans', 'core_competency');
174 $expectedurl = url
::plans($u1->id
);
175 $this->assertEquals(core_user
::get_noreply_user()->id
, $message->useridfrom
);
176 $this->assertEquals($u1->id
, $message->useridto
);
177 $this->assertTrue(strpos($message->fullmessage
, '<em>Hello world!</em>') !== false);
178 $this->assertTrue(strpos($message->fullmessagehtml
, '<em>Hello world!</em>') !== false);
179 $this->assertEquals(FORMAT_HTML
, $message->fullmessageformat
);
180 $this->assertEquals($expectedurl->out(false), $message->contexturl
);
181 $this->assertEquals($expectedurlname, $message->contexturlname
);
185 * Commenting on a plan.
187 public function test_comment_add_plan() {
188 $this->resetAfterTest();
189 $dg = $this->getDataGenerator();
190 $lpg = $dg->get_plugin_generator('core_competency');
192 $u1 = $dg->create_user();
193 $u2 = $dg->create_user();
194 $u3 = $dg->create_user();
195 $userroleid = $dg->create_role();
196 $reviewerroleid = $dg->create_role();
197 assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW
, $userroleid, context_system
::instance()->id
, true);
198 assign_capability('moodle/competency:planviewown', CAP_ALLOW
, $userroleid, context_system
::instance()->id
, true);
199 assign_capability('moodle/competency:planviewdraft', CAP_ALLOW
, $reviewerroleid, context_system
::instance()->id
, true);
200 assign_capability('moodle/competency:planmanage', CAP_ALLOW
, $reviewerroleid, context_system
::instance()->id
, true);
201 assign_capability('moodle/competency:plancomment', CAP_ALLOW
, $reviewerroleid, context_system
::instance()->id
, true);
202 $dg->role_assign($userroleid, $u1->id
, context_user
::instance($u1->id
));
203 $dg->role_assign($reviewerroleid, $u2->id
, context_user
::instance($u1->id
));
204 $dg->role_assign($reviewerroleid, $u3->id
, context_system
::instance());
205 accesslib_clear_all_caches_for_unit_testing();
207 $p1 = $lpg->create_plan(array('userid' => $u1->id
));
209 // Post a comment in own plan, no reviewer. Nobody is messaged.
211 $comment = $p1->get_comment_object();
212 $sink = $this->redirectMessages();
213 $comment->add('Hello world!');
214 $messages = $sink->get_messages();
216 $this->assertCount(0, $messages);
218 // Post a comment in plan as someone else, no reviewer. The owner is messages.
220 $comment = $p1->get_comment_object();
221 $sink = $this->redirectMessages();
222 $comment->add('Hello world!');
223 $messages = $sink->get_messages();
225 $this->assertCount(1, $messages);
226 $message = array_pop($messages);
227 $this->assertEquals(core_user
::get_noreply_user()->id
, $message->useridfrom
);
228 $this->assertEquals($u1->id
, $message->useridto
);
230 $customdata = json_decode($message->customdata
);
231 $this->assertObjectHasAttribute('notificationiconurl', $customdata);
233 // Post a comment in a plan with reviewer. The reviewer is messaged.
234 $p1->set('reviewerid', $u2->id
);
237 $comment = $p1->get_comment_object();
238 $sink = $this->redirectMessages();
239 $comment->add('Hello world!');
240 $messages = $sink->get_messages();
242 $this->assertCount(1, $messages);
243 $message = array_pop($messages);
244 $this->assertEquals(core_user
::get_noreply_user()->id
, $message->useridfrom
);
245 $this->assertEquals($u2->id
, $message->useridto
);
247 // Post a comment as reviewer in a plan being reviewed. The owner is messaged.
248 $p1->set('reviewerid', $u2->id
);
251 $comment = $p1->get_comment_object();
252 $sink = $this->redirectMessages();
253 $comment->add('Hello world!');
254 $messages = $sink->get_messages();
256 $this->assertCount(1, $messages);
257 $message = array_pop($messages);
258 $this->assertEquals(core_user
::get_noreply_user()->id
, $message->useridfrom
);
259 $this->assertEquals($u1->id
, $message->useridto
);
261 // Post a comment as someone else in a plan being reviewed. The owner and reviewer are messaged.
262 $p1->set('reviewerid', $u2->id
);
265 $comment = $p1->get_comment_object();
266 $sink = $this->redirectMessages();
267 $comment->add('Hello world!');
268 $messages = $sink->get_messages();
270 $this->assertCount(2, $messages);
271 $message1 = array_shift($messages);
272 $message2 = array_shift($messages);
273 $this->assertEquals(core_user
::get_noreply_user()->id
, $message1->useridfrom
);
274 $this->assertEquals($u1->id
, $message1->useridto
);
275 $this->assertEquals(core_user
::get_noreply_user()->id
, $message2->useridfrom
);
276 $this->assertEquals($u2->id
, $message2->useridto
);
278 $p1->set('reviewerid', null);
281 // Test message content.
283 $comment = $p1->get_comment_object();
284 $sink = $this->redirectMessages();
285 $comment->add('Hello world!');
286 $messages = $sink->get_messages();
288 $this->assertCount(1, $messages);
289 $message = array_pop($messages);
291 $expectedurlname = $p1->get('name');
292 $expectedurl = url
::plan($p1->get('id'));
293 $this->assertTrue(strpos($message->fullmessage
, 'Hello world!') !== false);
294 $this->assertTrue(strpos($message->fullmessagehtml
, 'Hello world!') !== false);
295 $this->assertEquals(FORMAT_MOODLE
, $message->fullmessageformat
);
296 $this->assertEquals($expectedurl->out(false), $message->contexturl
);
297 $this->assertEquals($expectedurlname, $message->contexturlname
);
299 // Test message content as HTML.
301 $comment = $p1->get_comment_object();
302 $sink = $this->redirectMessages();
303 $comment->add('<em>Hello world!</em>', FORMAT_HTML
);
304 $messages = $sink->get_messages();
306 $this->assertCount(1, $messages);
307 $message = array_pop($messages);
309 $expectedurlname = $p1->get('name');
310 $expectedurl = url
::plan($p1->get('id'));
311 $this->assertTrue(strpos($message->fullmessage
, '<em>Hello world!</em>') !== false);
312 $this->assertTrue(strpos($message->fullmessagehtml
, '<em>Hello world!</em>') !== false);
313 $this->assertEquals(FORMAT_HTML
, $message->fullmessageformat
);
314 $this->assertEquals($expectedurl->out(false), $message->contexturl
);
315 $this->assertEquals($expectedurlname, $message->contexturlname
);