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