54ad1ed56e3ea4a600bdd8367b6179c878859861
[moodle.git] / admin / tool / lp / tests / plan_test.php
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/>.
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  */
25 defined('MOODLE_INTERNAL') || die();
26 global $CFG;
28 use core_competency\plan;
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  */
37 class tool_lp_plan_testcase extends advanced_testcase {
39     public function test_can_manage_user() {
40         $this->resetAfterTest(true);
42         $manage = create_role('Manage', 'manage', 'Plan manager');
43         $manageown = create_role('Manageown', 'manageown', 'Own plan manager');
45         $u1 = $this->getDataGenerator()->create_user();
46         $u2 = $this->getDataGenerator()->create_user();
47         $u3 = $this->getDataGenerator()->create_user();
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);
54         assign_capability('moodle/competency:planmanage', CAP_ALLOW, $manage, $syscontext->id);
55         assign_capability('moodle/competency:planmanageown', CAP_ALLOW, $manageown, $u2context->id);
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();
62         $this->setUser($u1);
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));
67         $this->setUser($u2);
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));
72         $this->setUser($u3);
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));
76     }
78     public function test_can_manage_user_draft() {
79         $this->resetAfterTest(true);
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');
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();
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);
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);
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();
111         $this->setUser($u1);
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));
118         $this->setUser($u2);
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));
125         $this->setUser($u3);
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));
132         $this->setUser($u4);
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));
139         $this->setUser($u5);
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));
145     }
147     public function test_can_read_user() {
148         $this->resetAfterTest(true);
150         $read = create_role('Read', 'read', 'Plan reader');
151         $readown = create_role('Readown', 'readown', 'Own plan reader');
153         $u1 = $this->getDataGenerator()->create_user();
154         $u2 = $this->getDataGenerator()->create_user();
155         $u3 = $this->getDataGenerator()->create_user();
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);
162         assign_capability('moodle/competency:planview', CAP_ALLOW, $read, $syscontext->id);
163         assign_capability('moodle/competency:planviewown', CAP_ALLOW, $readown, $u2context->id);
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();
170         $this->setUser($u1);
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));
175         $this->setUser($u2);
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));
180         $this->setUser($u3);
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.
184     }
186     public function test_can_read_user_draft() {
187         $this->resetAfterTest(true);
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');
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();
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);
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);
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();
220         $this->setUser($u1);
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));
227         $this->setUser($u2);
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));
234         $this->setUser($u3);
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));
241         $this->setUser($u4);
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));
248         $this->setUser($u5);
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));
254     }
256     public function test_validate_duedate() {
257         global $DB;
258         $this->resetAfterTest(true);
259         $this->setAdminUser();
260         $dg = $this->getDataGenerator();
261         $lpg = $this->getDataGenerator()->get_plugin_generator('core_competency');
262         $user = $dg->create_user();
264         $record = array('userid' => $user->id,
265                         'status' => core_competency\plan::STATUS_DRAFT,
266                         'duedate' => time() - 8000);
268         // Ignore duedate validation on create/update draft plan.
269         $plan = $lpg->create_plan($record);
270         $this->assertInstanceOf('\core_competency\plan', $plan);
272         // Passing from draft to active.
273         $plan->set_status(core_competency\plan::STATUS_ACTIVE);
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());
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());
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());
295         // Draft to active: past date => future date (pass).
296         $plan->set_duedate(time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
297         $this->assertEquals(true, $plan->validate());
299         // Draft to active: past date => unset date (pass).
300         $plan->set_duedate(0);
301         $this->assertEquals(true, $plan->validate());
303         // Updating active plan.
304         $plan->update();
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());
314         // Active to active: past => unset (pass).
315         $plan->set_duedate(0);
316         $this->assertTrue($plan->is_valid());
317         $plan->update();
319         // Active to active: unset => unset (pass).
320         $plan->set_description(uniqid()); // Force revalidation.
321         $this->assertTrue($plan->is_valid());
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());
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());
337         // Active to active: unset date => future date (pass).
338         $plan->set_duedate(time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
339         $this->assertEquals(true, $plan->validate());
341         // Updating active plan with future date.
342         $plan->update();
344         // Active to active: future => same future (pass).
345         $plan->set_description(uniqid()); // Force revalidation.
346         $this->assertTrue($plan->is_valid());
348         // Active to active: future date => unset date (pass).
349         $plan->set_duedate(0);
350         $this->assertEquals(true, $plan->validate());
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());
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());
366         // Active to active: future date => future date (pass).
367         $plan->set_duedate(time() + core_competency\plan::DUEDATE_THRESHOLD + 10);
368         $this->assertEquals(true, $plan->validate());
370         // Completing plan: with due date in the past.
371         $record = $plan->to_record();
372         $record->status = core_competency\plan::STATUS_ACTIVE;
373         $record->duedate = time() - 200;
374         $DB->update_record(core_competency\plan::TABLE, $record);
376         $success = core_competency\api::complete_plan($plan->get_id());
377         $this->assertTrue($success);
379         // Completing plan: with due date too soon (pass).
380         $record = $plan->to_record();
381         $record->status = core_competency\plan::STATUS_ACTIVE;
382         $record->duedate = time() + 200;
383         $DB->update_record(core_competency\plan::TABLE, $record);
385         $success = core_competency\api::complete_plan($plan->get_id());
386         $this->assertTrue($success);
388         // Completing plan: with due date in the future (pass).
389         $record = $plan->to_record();
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);
394         $success = core_competency\api::complete_plan($plan->get_id());
395         $this->assertTrue($success);
397         // Completing plan: with due date unset (pass).
398         $record = $plan->to_record();
399         $record->status = core_competency\plan::STATUS_ACTIVE;
400         $record->duedate = 0;
401         $DB->update_record(core_competency\plan::TABLE, $record);
403         $success = core_competency\api::complete_plan($plan->get_id());
404         $this->assertTrue($success);
406         // Reopening plan: with due date in the past => duedate unset.
407         $record = $plan->to_record();
408         $record->status = core_competency\plan::STATUS_COMPLETE;
409         $record->duedate = time() - 200;
410         $DB->update_record(core_competency\plan::TABLE, $record);
412         $success = core_competency\api::reopen_plan($plan->get_id());
413         $this->assertTrue($success);
414         $plan->read();
415         $this->assertEquals(0, $plan->get_duedate());
417         // Reopening plan: with due date too soon => duedate unset.
418         $record = $plan->to_record();
419         $record->status = core_competency\plan::STATUS_COMPLETE;
420         $record->duedate = time() + 100;
421         $DB->update_record(core_competency\plan::TABLE, $record);
423         $success = core_competency\api::reopen_plan($plan->get_id());
424         $this->assertTrue($success);
425         $plan->read();
426         $this->assertEquals(0, $plan->get_duedate());
428         // Reopening plan: with due date in the future => duedate unchanged.
429         $record = $plan->to_record();
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);
434         $success = core_competency\api::reopen_plan($plan->get_id());
435         $this->assertTrue($success);
436         $plan->read();
438         // Check that the due date has not changed, but allow for PHP Unit latency.
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);
442     }