MDL-53700 competency: Migrating tests to core
[moodle.git] / competency / tests / task_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  * Task tests.
19  *
20  * @package    core_competency
21  * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
22  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 defined('MOODLE_INTERNAL') || die();
27 use core_competency\api;
28 use core_competency\plan;
30 /**
31  * Task tests.
32  *
33  * @package    core_competency
34  * @copyright  2015 Issam Taboubi <issam.taboubi@umontreal.ca>
35  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36  */
37 class core_competency_task_testcase extends advanced_testcase {
39     public function test_sync_plans_from_cohorts_task() {
40         global $DB;
42         $this->resetAfterTest(true);
43         $this->setAdminUser();
44         $dg = $this->getDataGenerator();
45         $lpg = $dg->get_plugin_generator('core_competency');
47         // Sql to simulate the execution in time.
48         $cmsql = "UPDATE {cohort_members} SET timeadded = :currenttime WHERE cohortid = :cohortid AND userid = :userid";
49         $tplsql = "UPDATE {" . \core_competency\template::TABLE . "} SET timemodified = :currenttime WHERE id = :templateid";
50         $plansql = "UPDATE {" . \core_competency\plan::TABLE . "} SET timemodified = :currenttime WHERE id = :planid";
52         $currenttime = time();
54         $user1 = $dg->create_user();
55         $user2 = $dg->create_user();
56         $user3 = $dg->create_user();
57         $user4 = $dg->create_user();
58         $user5 = $dg->create_user();
60         $cohort = $dg->create_cohort();
61         $tpl = $lpg->create_template();
63         // Add 2 users to the cohort.
64         cohort_add_member($cohort->id, $user1->id);
65         cohort_add_member($cohort->id, $user2->id);
67         // Creating plans from template cohort.
68         $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
69         $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id);
71         $this->assertEquals(2, $created);
73         $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
74         $this->assertInstanceOf('\core\task\sync_plans_from_template_cohorts_task', $task);
76         // Add two more users to the cohort.
77         cohort_add_member($cohort->id, $user3->id);
78         cohort_add_member($cohort->id, $user4->id);
80         $currenttime = $currenttime + 1;
81         $task->execute();
82         $task->set_last_run_time($currenttime);
84         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
86         // Test if remove user from cohort will affect plans.
87         cohort_remove_member($cohort->id, $user3->id);
88         cohort_remove_member($cohort->id, $user4->id);
90         $currenttime = $currenttime + 1;
91         $task->execute();
92         $task->set_last_run_time($currenttime);
93         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
95         // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
96         $currenttime = $currenttime + 1;
97         $tpl->set_visible(false);
98         $tpl->update();
99         $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get_id()));
100         $currenttime = $currenttime + 1;
101         cohort_add_member($cohort->id, $user5->id);
102         $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user5->id));
103         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
104         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
105         $currenttime = $currenttime + 1;
106         $task->execute();
107         $task->set_last_run_time($currenttime);
108         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
109         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
111         // Now I set the template as visible again, the plan is created.
112         $currenttime = $currenttime + 1;
113         $tpl->set_visible(true);
114         $tpl->update();
115         $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get_id()));
116         $currenttime = $currenttime + 1;
117         $task->execute();
118         $task->set_last_run_time($currenttime);
119         $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
120         $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get_id())));
122         // Let's unlink the plan and run the task again, it should not be recreated.
123         $currenttime = $currenttime + 1;
124         $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id()));
125         \core_competency\api::unlink_plan_from_template($plan);
126         $DB->execute($plansql, array('currenttime' => $currenttime, 'planid' => $plan->get_id()));
127         $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
128         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
129         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
130         $currenttime = $currenttime + 1;
131         $task->execute();
132         $task->set_last_run_time($currenttime);
133         $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
134         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
135         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
137         // Adding users to cohort that already exist in plans.
138         $currenttime = $currenttime + 1;
139         cohort_add_member($cohort->id, $user3->id);
140         cohort_add_member($cohort->id, $user4->id);
141         $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id));
142         $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id));
144         $currenttime = $currenttime + 1;
145         $task->execute();
146         $task->set_last_run_time($currenttime);
147         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
149         // Test a user plan deleted will not be recreated.
150         $currenttime = $currenttime + 1;
151         $plan = plan::get_record(array('userid' => $user4->id, 'templateid' => $tpl->get_id()));
152         \core_competency\api::delete_plan($plan->get_id());
153         $currenttime = $currenttime + 1;
154         $task->execute();
155         $task->set_last_run_time($currenttime);
156         $this->assertEquals(3, plan::count_records(array('templateid' => $tpl->get_id())));
157     }
159     public function test_sync_plans_from_cohorts_with_templateduedate_task() {
160         $this->resetAfterTest(true);
161         $this->setAdminUser();
162         $dg = $this->getDataGenerator();
163         $lpg = $dg->get_plugin_generator('core_competency');
165         $user1 = $dg->create_user();
166         $user2 = $dg->create_user();
167         $user3 = $dg->create_user();
168         $user4 = $dg->create_user();
169         $user5 = $dg->create_user();
171         $cohort = $dg->create_cohort();
172         $tpl = $lpg->create_template(array('duedate' => time() + 400));
174         // Add 2 users to the cohort.
175         cohort_add_member($cohort->id, $user1->id);
176         cohort_add_member($cohort->id, $user2->id);
178         // Creating plans from template cohort.
179         $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
180         $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id);
182         $this->assertEquals(2, $created);
184         $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
185         $this->assertInstanceOf('\core\task\sync_plans_from_template_cohorts_task', $task);
187         // Add two more users to the cohort.
188         cohort_add_member($cohort->id, $user3->id);
189         cohort_add_member($cohort->id, $user4->id);
191         $task->execute();
193         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
195         // Test if remove user from cohort will affect plans.
196         cohort_remove_member($cohort->id, $user3->id);
197         cohort_remove_member($cohort->id, $user4->id);
199         $task->execute();
200         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
202         // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
203         $tpl->set_visible(false);
204         $tpl->update();
205         cohort_add_member($cohort->id, $user5->id);
206         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
207         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
208         $task->execute();
209         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
210         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
212         // Now I set the template as visible again, the plan is created.
213         $tpl->set_visible(true);
214         $tpl->update();
215         $task->execute();
216         $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
217         $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get_id())));
219         // Let's unlink the plan and run the task again, it should not be recreated.
220         $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id()));
221         \core_competency\api::unlink_plan_from_template($plan);
222         $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
223         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
224         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
225         $task->execute();
226         $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
227         $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
228         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
230         // Adding users to cohort that already exist in plans.
231         cohort_add_member($cohort->id, $user3->id);
232         cohort_add_member($cohort->id, $user4->id);
234         $task->execute();
235         $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
236     }
238     public function test_sync_plans_from_cohorts_with_passed_duedate() {
239         global $DB;
241         $this->resetAfterTest(true);
242         $this->setAdminUser();
243         $dg = $this->getDataGenerator();
244         $lpg = $dg->get_plugin_generator('core_competency');
246         $user1 = $dg->create_user();
247         $user2 = $dg->create_user();
248         $cohort = $dg->create_cohort();
249         $tpl = $lpg->create_template(array('duedate' => time() + 1000));
250         $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
251         $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
253         // Add 1 user to the cohort.
254         cohort_add_member($cohort->id, $user1->id);
256         // Creating plans from template cohort.
257         $task->execute();
258         $this->assertEquals(1, \core_competency\plan::count_records());
260         // Now add another user, but this time the template will be expired.
261         cohort_add_member($cohort->id, $user2->id);
262         $record = $tpl->to_record();
263         $record->duedate = time() - 10000;
264         $DB->update_record(\core_competency\template::TABLE, $record);
265         $tpl->read();
266         $task->execute();
267         $this->assertEquals(1, \core_competency\plan::count_records()); // Still only one plan.
269         // Pretend it wasn't expired.
270         $tpl->set_duedate(time() + 100);
271         $tpl->update();
272         $task->execute();
273         $this->assertEquals(2, \core_competency\plan::count_records()); // Now there is two.
274     }
276     public function test_complete_plans_task() {
277         global $DB;
278         $this->resetAfterTest(true);
279         $this->setAdminUser();
280         $dg = $this->getDataGenerator();
281         $lpg = $dg->get_plugin_generator('core_competency');
283         $user = $dg->create_user();
285         $up1 = $lpg->create_plan(array('userid' => $user->id,
286                                         'status' => \core_competency\plan::STATUS_DRAFT));
287         $up2 = $lpg->create_plan(array('userid' => $user->id,
288                                         'status' => \core_competency\plan::STATUS_ACTIVE));
289         // Set duedate in the past.
290         $date = new \DateTime('yesterday');
291         $record1 = $up1->to_record();
292         $record2 = $up2->to_record();
294         $record1->duedate = $date->getTimestamp();
295         $record2->duedate = $date->getTimestamp();
296         $DB->update_record(plan::TABLE, $record1);
297         $DB->update_record(plan::TABLE, $record2);
299         $task = \core\task\manager::get_scheduled_task('\\core\\task\\complete_plans_task');
300         $this->assertInstanceOf('\\core\\task\\complete_plans_task', $task);
302         // Test that draft plan can not be completed on running task.
303         $task->execute();
305         $plandraft = api::read_plan($up1->get_id());
306         $this->assertEquals(\core_competency\plan::STATUS_DRAFT, $plandraft->get_status());
308         // Test that active plan can be completed on running task.
309         $task->execute();
311         $planactive = api::read_plan($up2->get_id());
312         $this->assertEquals(\core_competency\plan::STATUS_COMPLETE, $planactive->get_status());
313     }