MDL-53700 competency: Migrating message definitions to core
[moodle.git] / admin / tool / lp / tests / task_test.php
CommitLineData
ed549dbf
IT
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 * Task tests.
19 *
20 * @package tool_lp
21 * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27use tool_lp\api;
28use tool_lp\plan;
29
30/**
31 * Task tests.
32 *
33 * @package tool_lp
34 * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37class tool_lp_task_testcase extends advanced_testcase {
38
39 public function test_sync_plans_from_cohorts_task() {
9331352c
SG
40 global $DB;
41
ed549dbf
IT
42 $this->resetAfterTest(true);
43 $this->setAdminUser();
44 $dg = $this->getDataGenerator();
45 $lpg = $dg->get_plugin_generator('tool_lp');
46
9331352c
SG
47 // Sql to simulate the execution in time.
48 $cmsql = "UPDATE {cohort_members} SET timeadded = :currenttime WHERE cohortid = :cohortid AND userid = :userid";
a4e659c7
FM
49 $tplsql = "UPDATE {" . \tool_lp\template::TABLE . "} SET timemodified = :currenttime WHERE id = :templateid";
50 $plansql = "UPDATE {" . \tool_lp\plan::TABLE . "} SET timemodified = :currenttime WHERE id = :planid";
9331352c
SG
51
52 $currenttime = time();
53
ed549dbf
IT
54 $user1 = $dg->create_user();
55 $user2 = $dg->create_user();
56 $user3 = $dg->create_user();
57 $user4 = $dg->create_user();
f4428607 58 $user5 = $dg->create_user();
ed549dbf
IT
59
60 $cohort = $dg->create_cohort();
61 $tpl = $lpg->create_template();
62
63 // Add 2 users to the cohort.
64 cohort_add_member($cohort->id, $user1->id);
65 cohort_add_member($cohort->id, $user2->id);
66
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);
70
71 $this->assertEquals(2, $created);
72
42a7899f
FM
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);
ed549dbf
IT
75
76 // Add two more users to the cohort.
77 cohort_add_member($cohort->id, $user3->id);
78 cohort_add_member($cohort->id, $user4->id);
79
9331352c 80 $currenttime = $currenttime + 1;
ed549dbf 81 $task->execute();
9331352c 82 $task->set_last_run_time($currenttime);
ed549dbf
IT
83
84 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
3c581164
IT
85
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);
89
9331352c 90 $currenttime = $currenttime + 1;
3c581164 91 $task->execute();
9331352c 92 $task->set_last_run_time($currenttime);
3c581164
IT
93 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
94
95 // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
9331352c 96 $currenttime = $currenttime + 1;
3c581164
IT
97 $tpl->set_visible(false);
98 $tpl->update();
9331352c
SG
99 $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get_id()));
100 $currenttime = $currenttime + 1;
3c581164 101 cohort_add_member($cohort->id, $user5->id);
9331352c 102 $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user5->id));
3c581164
IT
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())));
9331352c 105 $currenttime = $currenttime + 1;
3c581164 106 $task->execute();
9331352c 107 $task->set_last_run_time($currenttime);
3c581164
IT
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())));
110
111 // Now I set the template as visible again, the plan is created.
9331352c 112 $currenttime = $currenttime + 1;
3c581164
IT
113 $tpl->set_visible(true);
114 $tpl->update();
9331352c
SG
115 $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get_id()));
116 $currenttime = $currenttime + 1;
3c581164 117 $task->execute();
9331352c 118 $task->set_last_run_time($currenttime);
3c581164
IT
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())));
121
122 // Let's unlink the plan and run the task again, it should not be recreated.
9331352c 123 $currenttime = $currenttime + 1;
3c581164
IT
124 $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id()));
125 \tool_lp\api::unlink_plan_from_template($plan);
9331352c 126 $DB->execute($plansql, array('currenttime' => $currenttime, 'planid' => $plan->get_id()));
3c581164
IT
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())));
9331352c 130 $currenttime = $currenttime + 1;
3c581164 131 $task->execute();
9331352c 132 $task->set_last_run_time($currenttime);
3c581164
IT
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())));
136
137 // Adding users to cohort that already exist in plans.
9331352c 138 $currenttime = $currenttime + 1;
3c581164
IT
139 cohort_add_member($cohort->id, $user3->id);
140 cohort_add_member($cohort->id, $user4->id);
9331352c
SG
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));
3c581164 143
9331352c 144 $currenttime = $currenttime + 1;
3c581164 145 $task->execute();
9331352c 146 $task->set_last_run_time($currenttime);
3c581164 147 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
9331352c 148
96c2b847 149 // Test a user plan deleted will not be recreated.
9331352c
SG
150 $currenttime = $currenttime + 1;
151 $plan = plan::get_record(array('userid' => $user4->id, 'templateid' => $tpl->get_id()));
152 \tool_lp\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())));
3c581164
IT
157 }
158
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('tool_lp');
164
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();
170
171 $cohort = $dg->create_cohort();
a967d2aa 172 $tpl = $lpg->create_template(array('duedate' => time() + 400));
3c581164
IT
173
174 // Add 2 users to the cohort.
175 cohort_add_member($cohort->id, $user1->id);
176 cohort_add_member($cohort->id, $user2->id);
177
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);
181
182 $this->assertEquals(2, $created);
183
42a7899f
FM
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);
3c581164
IT
186
187 // Add two more users to the cohort.
188 cohort_add_member($cohort->id, $user3->id);
189 cohort_add_member($cohort->id, $user4->id);
190
191 $task->execute();
192
193 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
ed549dbf
IT
194
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);
198
199 $task->execute();
200 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
201
f4428607
FM
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())));
211
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())));
218
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 \tool_lp\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())));
229
ed549dbf
IT
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);
233
234 $task->execute();
235 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
236 }
7a9f14c3 237
9031be44
FM
238 public function test_sync_plans_from_cohorts_with_passed_duedate() {
239 global $DB;
240
241 $this->resetAfterTest(true);
242 $this->setAdminUser();
243 $dg = $this->getDataGenerator();
244 $lpg = $dg->get_plugin_generator('tool_lp');
245
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);
42a7899f 251 $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
9031be44
FM
252
253 // Add 1 user to the cohort.
254 cohort_add_member($cohort->id, $user1->id);
255
256 // Creating plans from template cohort.
257 $task->execute();
258 $this->assertEquals(1, \tool_lp\plan::count_records());
259
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(\tool_lp\template::TABLE, $record);
265 $tpl->read();
266 $task->execute();
267 $this->assertEquals(1, \tool_lp\plan::count_records()); // Still only one plan.
268
269 // Pretend it wasn't expired.
270 $tpl->set_duedate(time() + 100);
271 $tpl->update();
272 $task->execute();
273 $this->assertEquals(2, \tool_lp\plan::count_records()); // Now there is two.
274 }
275
7a9f14c3
IT
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('tool_lp');
282
283 $user = $dg->create_user();
284
285 $up1 = $lpg->create_plan(array('userid' => $user->id,
286 'status' => \tool_lp\plan::STATUS_DRAFT));
287 $up2 = $lpg->create_plan(array('userid' => $user->id,
288 'status' => \tool_lp\plan::STATUS_ACTIVE));
289 // Set duedate in the past.
290 $date = new \DateTime('yesterday');
291 $record1 = $up1->to_record();
292 $record2 = $up2->to_record();
293
294 $record1->duedate = $date->getTimestamp();
295 $record2->duedate = $date->getTimestamp();
296 $DB->update_record(plan::TABLE, $record1);
297 $DB->update_record(plan::TABLE, $record2);
298
42a7899f
FM
299 $task = \core\task\manager::get_scheduled_task('\\core\\task\\complete_plans_task');
300 $this->assertInstanceOf('\\core\\task\\complete_plans_task', $task);
7a9f14c3
IT
301
302 // Test that draft plan can not be completed on running task.
303 $task->execute();
304
305 $plandraft = api::read_plan($up1->get_id());
306 $this->assertEquals(\tool_lp\plan::STATUS_DRAFT, $plandraft->get_status());
307
308 // Test that active plan can be completed on running task.
309 $task->execute();
310
311 $planactive = api::read_plan($up2->get_id());
312 $this->assertEquals(\tool_lp\plan::STATUS_COMPLETE, $planactive->get_status());
313 }
ed549dbf 314}