MDL-53700 competency: Migrating data generator to core
[moodle.git] / admin / tool / lp / tests / plan_test.php
CommitLineData
5159d679
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 * Plan persistent class 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();
26global $CFG;
27
67bc0eaf 28use core_competency\plan;
c78ebeac 29
5159d679
FM
30/**
31 * Plan persistent 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_plan_testcase extends advanced_testcase {
38
39 public function test_can_manage_user() {
40 $this->resetAfterTest(true);
41
42 $manage = create_role('Manage', 'manage', 'Plan manager');
43 $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
44
45 $u1 = $this->getDataGenerator()->create_user();
46 $u2 = $this->getDataGenerator()->create_user();
47 $u3 = $this->getDataGenerator()->create_user();
48
49 $syscontext = context_system::instance();
50 $u1context = context_user::instance($u1->id);
51 $u2context = context_user::instance($u2->id);
52 $u3context = context_user::instance($u3->id);
53
b90e2205
FM
54 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $manage, $syscontext->id);
55 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageown, $u2context->id);
5159d679
FM
56
57 role_assign($manage, $u1->id, $syscontext->id);
58 role_assign($manageown, $u2->id, $syscontext->id);
59 role_assign($manage, $u3->id, $u2context->id);
60 accesslib_clear_all_caches_for_unit_testing();
61
62 $this->setUser($u1);
67bc0eaf
FM
63 $this->assertTrue(\core_competency\plan::can_manage_user($u1->id));
64 $this->assertTrue(\core_competency\plan::can_manage_user($u2->id));
65 $this->assertTrue(\core_competency\plan::can_manage_user($u3->id));
5159d679
FM
66
67 $this->setUser($u2);
67bc0eaf
FM
68 $this->assertFalse(\core_competency\plan::can_manage_user($u1->id));
69 $this->assertTrue(\core_competency\plan::can_manage_user($u2->id));
70 $this->assertFalse(\core_competency\plan::can_manage_user($u3->id));
5159d679
FM
71
72 $this->setUser($u3);
67bc0eaf
FM
73 $this->assertFalse(\core_competency\plan::can_manage_user($u1->id));
74 $this->assertTrue(\core_competency\plan::can_manage_user($u2->id));
75 $this->assertFalse(\core_competency\plan::can_manage_user($u3->id));
5159d679
FM
76 }
77
78 public function test_can_manage_user_draft() {
79 $this->resetAfterTest(true);
80
81 $manage = create_role('Manage', 'manage', 'Plan manager');
82 $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
83 $managedraft = create_role('Managedraft', 'managedraft', 'Draft plan manager');
84 $manageowndraft = create_role('Manageowndraft', 'manageowndraft', 'Own draft plan manager');
85
86 $u1 = $this->getDataGenerator()->create_user();
87 $u2 = $this->getDataGenerator()->create_user();
88 $u3 = $this->getDataGenerator()->create_user();
89 $u4 = $this->getDataGenerator()->create_user();
90 $u5 = $this->getDataGenerator()->create_user();
91
92 $syscontext = context_system::instance();
93 $u1context = context_user::instance($u1->id);
94 $u2context = context_user::instance($u2->id);
95 $u3context = context_user::instance($u3->id);
96 $u4context = context_user::instance($u4->id);
97 $u5context = context_user::instance($u5->id);
98
b90e2205
FM
99 assign_capability('moodle/competency:planmanage', CAP_ALLOW, $manage, $syscontext->id);
100 assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageown, $syscontext->id);
101 assign_capability('moodle/competency:planmanagedraft', CAP_ALLOW, $managedraft, $syscontext->id);
102 assign_capability('moodle/competency:planmanageowndraft', CAP_ALLOW, $manageowndraft, $syscontext->id);
5159d679
FM
103
104 role_assign($manage, $u1->id, $syscontext->id);
105 role_assign($manageown, $u2->id, $syscontext->id);
106 role_assign($managedraft, $u3->id, $syscontext->id);
107 role_assign($managedraft, $u4->id, $u2context->id);
108 role_assign($manageowndraft, $u5->id, $syscontext->id);
109 accesslib_clear_all_caches_for_unit_testing();
110
111 $this->setUser($u1);
67bc0eaf
FM
112 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u1->id));
113 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u2->id));
114 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u3->id));
115 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u4->id));
116 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u5->id));
5159d679
FM
117
118 $this->setUser($u2);
67bc0eaf
FM
119 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u1->id));
120 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u2->id));
121 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u3->id));
122 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u4->id));
123 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u5->id));
5159d679
FM
124
125 $this->setUser($u3);
67bc0eaf
FM
126 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u1->id));
127 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u2->id));
128 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u3->id));
129 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u4->id));
130 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u5->id));
5159d679
FM
131
132 $this->setUser($u4);
67bc0eaf
FM
133 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u1->id));
134 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u2->id));
135 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u3->id));
136 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u4->id));
137 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u5->id));
5159d679
FM
138
139 $this->setUser($u5);
67bc0eaf
FM
140 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u1->id));
141 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u2->id));
142 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u3->id));
143 $this->assertFalse(\core_competency\plan::can_manage_user_draft($u4->id));
144 $this->assertTrue(\core_competency\plan::can_manage_user_draft($u5->id));
5159d679
FM
145 }
146
147 public function test_can_read_user() {
148 $this->resetAfterTest(true);
149
150 $read = create_role('Read', 'read', 'Plan reader');
151 $readown = create_role('Readown', 'readown', 'Own plan reader');
152
153 $u1 = $this->getDataGenerator()->create_user();
154 $u2 = $this->getDataGenerator()->create_user();
155 $u3 = $this->getDataGenerator()->create_user();
156
157 $syscontext = context_system::instance();
158 $u1context = context_user::instance($u1->id);
159 $u2context = context_user::instance($u2->id);
160 $u3context = context_user::instance($u3->id);
161
b90e2205
FM
162 assign_capability('moodle/competency:planview', CAP_ALLOW, $read, $syscontext->id);
163 assign_capability('moodle/competency:planviewown', CAP_ALLOW, $readown, $u2context->id);
5159d679
FM
164
165 role_assign($read, $u1->id, $syscontext->id);
166 role_assign($readown, $u2->id, $syscontext->id);
167 role_assign($read, $u3->id, $u2context->id);
168 accesslib_clear_all_caches_for_unit_testing();
169
170 $this->setUser($u1);
67bc0eaf
FM
171 $this->assertTrue(\core_competency\plan::can_read_user($u1->id));
172 $this->assertTrue(\core_competency\plan::can_read_user($u2->id));
173 $this->assertTrue(\core_competency\plan::can_read_user($u3->id));
5159d679
FM
174
175 $this->setUser($u2);
67bc0eaf
FM
176 $this->assertFalse(\core_competency\plan::can_read_user($u1->id));
177 $this->assertTrue(\core_competency\plan::can_read_user($u2->id));
178 $this->assertFalse(\core_competency\plan::can_read_user($u3->id));
5159d679
FM
179
180 $this->setUser($u3);
67bc0eaf
FM
181 $this->assertFalse(\core_competency\plan::can_read_user($u1->id));
182 $this->assertTrue(\core_competency\plan::can_read_user($u2->id));
183 $this->assertTrue(\core_competency\plan::can_read_user($u3->id)); // Due to the default capability.
5159d679
FM
184 }
185
186 public function test_can_read_user_draft() {
187 $this->resetAfterTest(true);
188
189 $read = create_role('Read', 'read', 'Plan readr');
190 $readown = create_role('Readown', 'readown', 'Own plan readr');
191 $readdraft = create_role('Readdraft', 'readdraft', 'Draft plan readr');
192 $readowndraft = create_role('Readowndraft', 'readowndraft', 'Own draft plan readr');
193
194 $u1 = $this->getDataGenerator()->create_user();
195 $u2 = $this->getDataGenerator()->create_user();
196 $u3 = $this->getDataGenerator()->create_user();
197 $u4 = $this->getDataGenerator()->create_user();
198 $u5 = $this->getDataGenerator()->create_user();
199
200 $syscontext = context_system::instance();
201 $u1context = context_user::instance($u1->id);
202 $u2context = context_user::instance($u2->id);
203 $u3context = context_user::instance($u3->id);
204 $u4context = context_user::instance($u4->id);
205 $u5context = context_user::instance($u5->id);
206
b90e2205
FM
207 assign_capability('moodle/competency:planview', CAP_ALLOW, $read, $syscontext->id);
208 assign_capability('moodle/competency:planviewown', CAP_ALLOW, $readown, $syscontext->id);
209 assign_capability('moodle/competency:planviewdraft', CAP_ALLOW, $readdraft, $syscontext->id);
210 assign_capability('moodle/competency:planviewowndraft', CAP_ALLOW, $readowndraft, $syscontext->id);
211 assign_capability('moodle/competency:planviewown', CAP_PROHIBIT, $readowndraft, $syscontext->id);
5159d679
FM
212
213 role_assign($read, $u1->id, $syscontext->id);
214 role_assign($readown, $u2->id, $syscontext->id);
215 role_assign($readdraft, $u3->id, $syscontext->id);
216 role_assign($readdraft, $u4->id, $u2context->id);
217 role_assign($readowndraft, $u5->id, $syscontext->id);
218 accesslib_clear_all_caches_for_unit_testing();
219
220 $this->setUser($u1);
67bc0eaf
FM
221 $this->assertFalse(\core_competency\plan::can_read_user_draft($u1->id));
222 $this->assertFalse(\core_competency\plan::can_read_user_draft($u2->id));
223 $this->assertFalse(\core_competency\plan::can_read_user_draft($u3->id));
224 $this->assertFalse(\core_competency\plan::can_read_user_draft($u4->id));
225 $this->assertFalse(\core_competency\plan::can_read_user_draft($u5->id));
5159d679
FM
226
227 $this->setUser($u2);
67bc0eaf
FM
228 $this->assertFalse(\core_competency\plan::can_read_user_draft($u1->id));
229 $this->assertFalse(\core_competency\plan::can_read_user_draft($u2->id));
230 $this->assertFalse(\core_competency\plan::can_read_user_draft($u3->id));
231 $this->assertFalse(\core_competency\plan::can_read_user_draft($u4->id));
232 $this->assertFalse(\core_competency\plan::can_read_user_draft($u5->id));
5159d679
FM
233
234 $this->setUser($u3);
67bc0eaf
FM
235 $this->assertTrue(\core_competency\plan::can_read_user_draft($u1->id));
236 $this->assertTrue(\core_competency\plan::can_read_user_draft($u2->id));
237 $this->assertTrue(\core_competency\plan::can_read_user_draft($u3->id));
238 $this->assertTrue(\core_competency\plan::can_read_user_draft($u4->id));
239 $this->assertTrue(\core_competency\plan::can_read_user_draft($u5->id));
5159d679
FM
240
241 $this->setUser($u4);
67bc0eaf
FM
242 $this->assertFalse(\core_competency\plan::can_read_user_draft($u1->id));
243 $this->assertTrue(\core_competency\plan::can_read_user_draft($u2->id));
244 $this->assertFalse(\core_competency\plan::can_read_user_draft($u3->id));
245 $this->assertFalse(\core_competency\plan::can_read_user_draft($u4->id));
246 $this->assertFalse(\core_competency\plan::can_read_user_draft($u5->id));
5159d679
FM
247
248 $this->setUser($u5);
67bc0eaf
FM
249 $this->assertFalse(\core_competency\plan::can_read_user_draft($u1->id));
250 $this->assertFalse(\core_competency\plan::can_read_user_draft($u2->id));
251 $this->assertFalse(\core_competency\plan::can_read_user_draft($u3->id));
252 $this->assertFalse(\core_competency\plan::can_read_user_draft($u4->id));
253 $this->assertTrue(\core_competency\plan::can_read_user_draft($u5->id));
5159d679 254 }
3c581164
IT
255
256 public function test_validate_duedate() {
257 global $DB;
258 $this->resetAfterTest(true);
259 $this->setAdminUser();
260 $dg = $this->getDataGenerator();
922634d3 261 $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
3c581164
IT
262 $user = $dg->create_user();
263
264 $record = array('userid' => $user->id,
67bc0eaf 265 'status' => core_competency\plan::STATUS_DRAFT,
3c581164
IT
266 'duedate' => time() - 8000);
267
268 // Ignore duedate validation on create/update draft plan.
269 $plan = $lpg->create_plan($record);
67bc0eaf 270 $this->assertInstanceOf('\core_competency\plan', $plan);
3c581164
IT
271
272 // Passing from draft to active.
67bc0eaf 273 $plan->set_status(core_competency\plan::STATUS_ACTIVE);
3c581164
IT
274
275 // Draft to active with duedate in the past.
276 $expected = array(
277 'duedate' => new lang_string('errorcannotsetduedateinthepast', 'tool_lp'),
278 );
279 $this->assertEquals($expected, $plan->validate());
280
281 // Draft to active: past date => past date(fail).
282 $plan->set_duedate(time() - 100);
283 $expected = array(
284 'duedate' => new lang_string('errorcannotsetduedateinthepast', 'tool_lp'),
285 );
286 $this->assertEquals($expected, $plan->validate());
287
288 // Draft to active: past date => too soon (fail).
289 $plan->set_duedate(time() + 100);
290 $expected = array(
291 'duedate' => new lang_string('errorcannotsetduedatetoosoon', 'tool_lp'),
292 );
293 $this->assertEquals($expected, $plan->validate());
294
295 // Draft to active: past date => future date (pass).
67bc0eaf 296 $plan->set_duedate(time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
3c581164
IT
297 $this->assertEquals(true, $plan->validate());
298
299 // Draft to active: past date => unset date (pass).
300 $plan->set_duedate(0);
301 $this->assertEquals(true, $plan->validate());
302
303 // Updating active plan.
304 $plan->update();
305
c78ebeac
FM
306 // Active to active: past => same past (pass).
307 $record = $plan->to_record();
308 $record->duedate = 1;
309 $DB->update_record(plan::TABLE, $record);
310 $plan->read();
311 $plan->set_description(uniqid()); // Force revalidation.
312 $this->assertTrue($plan->is_valid());
313
314 // Active to active: past => unset (pass).
315 $plan->set_duedate(0);
316 $this->assertTrue($plan->is_valid());
317 $plan->update();
318
319 // Active to active: unset => unset (pass).
320 $plan->set_description(uniqid()); // Force revalidation.
321 $this->assertTrue($plan->is_valid());
322
3c581164
IT
323 // Active to active: unset date => past date(fail).
324 $plan->set_duedate(time() - 100);
325 $expected = array(
326 'duedate' => new lang_string('errorcannotsetduedateinthepast', 'tool_lp'),
327 );
328 $this->assertEquals($expected, $plan->validate());
329
330 // Active to active: unset date => too soon (fail).
331 $plan->set_duedate(time() + 100);
332 $expected = array(
333 'duedate' => new lang_string('errorcannotsetduedatetoosoon', 'tool_lp'),
334 );
335 $this->assertEquals($expected, $plan->validate());
336
337 // Active to active: unset date => future date (pass).
67bc0eaf 338 $plan->set_duedate(time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
3c581164
IT
339 $this->assertEquals(true, $plan->validate());
340
341 // Updating active plan with future date.
342 $plan->update();
343
c78ebeac
FM
344 // Active to active: future => same future (pass).
345 $plan->set_description(uniqid()); // Force revalidation.
346 $this->assertTrue($plan->is_valid());
347
3c581164
IT
348 // Active to active: future date => unset date (pass).
349 $plan->set_duedate(0);
350 $this->assertEquals(true, $plan->validate());
351
352 // Active to active: future date => past date(fail).
353 $plan->set_duedate(time() - 100);
354 $expected = array(
355 'duedate' => new lang_string('errorcannotsetduedateinthepast', 'tool_lp'),
356 );
357 $this->assertEquals($expected, $plan->validate());
358
359 // Active to active: future date => too soon (fail).
360 $plan->set_duedate(time() + 100);
361 $expected = array(
362 'duedate' => new lang_string('errorcannotsetduedatetoosoon', 'tool_lp'),
363 );
364 $this->assertEquals($expected, $plan->validate());
365
366 // Active to active: future date => future date (pass).
67bc0eaf 367 $plan->set_duedate(time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
3c581164
IT
368 $this->assertEquals(true, $plan->validate());
369
370 // Completing plan: with due date in the past.
371 $record = $plan->to_record();
67bc0eaf 372 $record->status = core_competency\plan::STATUS_ACTIVE;
3c581164 373 $record->duedate = time() - 200;
67bc0eaf 374 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 375
767f66c0 376 $success = core_competency\api::complete_plan($plan->get_id());
3c581164
IT
377 $this->assertTrue($success);
378
379 // Completing plan: with due date too soon (pass).
380 $record = $plan->to_record();
67bc0eaf 381 $record->status = core_competency\plan::STATUS_ACTIVE;
3c581164 382 $record->duedate = time() + 200;
67bc0eaf 383 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 384
767f66c0 385 $success = core_competency\api::complete_plan($plan->get_id());
3c581164
IT
386 $this->assertTrue($success);
387
388 // Completing plan: with due date in the future (pass).
389 $record = $plan->to_record();
67bc0eaf
FM
390 $record->status = core_competency\plan::STATUS_ACTIVE;
391 $record->duedate = time() + core_competency\plan::DUEDATE_THRESHOLD + 10;
392 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 393
767f66c0 394 $success = core_competency\api::complete_plan($plan->get_id());
3c581164
IT
395 $this->assertTrue($success);
396
397 // Completing plan: with due date unset (pass).
398 $record = $plan->to_record();
67bc0eaf 399 $record->status = core_competency\plan::STATUS_ACTIVE;
3c581164 400 $record->duedate = 0;
67bc0eaf 401 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 402
767f66c0 403 $success = core_competency\api::complete_plan($plan->get_id());
3c581164
IT
404 $this->assertTrue($success);
405
406 // Reopening plan: with due date in the past => duedate unset.
407 $record = $plan->to_record();
67bc0eaf 408 $record->status = core_competency\plan::STATUS_COMPLETE;
3c581164 409 $record->duedate = time() - 200;
67bc0eaf 410 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 411
767f66c0 412 $success = core_competency\api::reopen_plan($plan->get_id());
3c581164
IT
413 $this->assertTrue($success);
414 $plan->read();
415 $this->assertEquals(0, $plan->get_duedate());
416
417 // Reopening plan: with due date too soon => duedate unset.
418 $record = $plan->to_record();
67bc0eaf 419 $record->status = core_competency\plan::STATUS_COMPLETE;
3c581164 420 $record->duedate = time() + 100;
67bc0eaf 421 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 422
767f66c0 423 $success = core_competency\api::reopen_plan($plan->get_id());
3c581164
IT
424 $this->assertTrue($success);
425 $plan->read();
426 $this->assertEquals(0, $plan->get_duedate());
427
428 // Reopening plan: with due date in the future => duedate unchanged.
429 $record = $plan->to_record();
67bc0eaf
FM
430 $record->status = core_competency\plan::STATUS_COMPLETE;
431 $record->duedate = time() + core_competency\plan::DUEDATE_THRESHOLD + 10;
432 $DB->update_record(core_competency\plan::TABLE, $record);
3c581164 433
767f66c0 434 $success = core_competency\api::reopen_plan($plan->get_id());
3c581164
IT
435 $this->assertTrue($success);
436 $plan->read();
c78ebeac
FM
437
438 // Check that the due date has not changed, but allow for PHP Unit latency.
67bc0eaf
FM
439 $this->assertTrue($plan->get_duedate() >= time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
440 $this->assertTrue($plan->get_duedate() <= time() + core_competency\plan::DUEDATE_THRESHOLD + 15);
3c581164
IT
441
442 }
5159d679 443}