MDL-52960 tool_lp: Created event for competency viewed
[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() {
40 $this->resetAfterTest(true);
41 $this->setAdminUser();
42 $dg = $this->getDataGenerator();
43 $lpg = $dg->get_plugin_generator('tool_lp');
44
45 $user1 = $dg->create_user();
46 $user2 = $dg->create_user();
47 $user3 = $dg->create_user();
48 $user4 = $dg->create_user();
f4428607 49 $user5 = $dg->create_user();
ed549dbf
IT
50
51 $cohort = $dg->create_cohort();
52 $tpl = $lpg->create_template();
53
54 // Add 2 users to the cohort.
55 cohort_add_member($cohort->id, $user1->id);
56 cohort_add_member($cohort->id, $user2->id);
57
58 // Creating plans from template cohort.
59 $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
60 $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id);
61
62 $this->assertEquals(2, $created);
63
64 $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\sync_plans_from_template_cohorts_task');
65 $this->assertInstanceOf('\tool_lp\task\sync_plans_from_template_cohorts_task', $task);
66
67 // Add two more users to the cohort.
68 cohort_add_member($cohort->id, $user3->id);
69 cohort_add_member($cohort->id, $user4->id);
70
71 $task->execute();
72
73 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
3c581164
IT
74
75 // Test if remove user from cohort will affect plans.
76 cohort_remove_member($cohort->id, $user3->id);
77 cohort_remove_member($cohort->id, $user4->id);
78
79 $task->execute();
80 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
81
82 // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
83 $tpl->set_visible(false);
84 $tpl->update();
85 cohort_add_member($cohort->id, $user5->id);
86 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
87 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
88 $task->execute();
89 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
90 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
91
92 // Now I set the template as visible again, the plan is created.
93 $tpl->set_visible(true);
94 $tpl->update();
95 $task->execute();
96 $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
97 $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get_id())));
98
99 // Let's unlink the plan and run the task again, it should not be recreated.
100 $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id()));
101 \tool_lp\api::unlink_plan_from_template($plan);
102 $this->assertTrue(plan::record_exists_select('userid = ?', array($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 $task->execute();
106 $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
107 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
108 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
109
110 // Adding users to cohort that already exist in plans.
111 cohort_add_member($cohort->id, $user3->id);
112 cohort_add_member($cohort->id, $user4->id);
113
114 $task->execute();
115 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
116 }
117
118 public function test_sync_plans_from_cohorts_with_templateduedate_task() {
119 $this->resetAfterTest(true);
120 $this->setAdminUser();
121 $dg = $this->getDataGenerator();
122 $lpg = $dg->get_plugin_generator('tool_lp');
123
124 $user1 = $dg->create_user();
125 $user2 = $dg->create_user();
126 $user3 = $dg->create_user();
127 $user4 = $dg->create_user();
128 $user5 = $dg->create_user();
129
130 $cohort = $dg->create_cohort();
a967d2aa 131 $tpl = $lpg->create_template(array('duedate' => time() + 400));
3c581164
IT
132
133 // Add 2 users to the cohort.
134 cohort_add_member($cohort->id, $user1->id);
135 cohort_add_member($cohort->id, $user2->id);
136
137 // Creating plans from template cohort.
138 $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
139 $created = api::create_plans_from_template_cohort($tpl->get_id(), $cohort->id);
140
141 $this->assertEquals(2, $created);
142
143 $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\sync_plans_from_template_cohorts_task');
144 $this->assertInstanceOf('\tool_lp\task\sync_plans_from_template_cohorts_task', $task);
145
146 // Add two more users to the cohort.
147 cohort_add_member($cohort->id, $user3->id);
148 cohort_add_member($cohort->id, $user4->id);
149
150 $task->execute();
151
152 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
ed549dbf
IT
153
154 // Test if remove user from cohort will affect plans.
155 cohort_remove_member($cohort->id, $user3->id);
156 cohort_remove_member($cohort->id, $user4->id);
157
158 $task->execute();
159 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
160
f4428607
FM
161 // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
162 $tpl->set_visible(false);
163 $tpl->update();
164 cohort_add_member($cohort->id, $user5->id);
165 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
166 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
167 $task->execute();
168 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
169 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
170
171 // Now I set the template as visible again, the plan is created.
172 $tpl->set_visible(true);
173 $tpl->update();
174 $task->execute();
175 $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
176 $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get_id())));
177
178 // Let's unlink the plan and run the task again, it should not be recreated.
179 $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get_id()));
180 \tool_lp\api::unlink_plan_from_template($plan);
181 $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
182 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
183 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
184 $task->execute();
185 $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
186 $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get_id())));
187 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
188
ed549dbf
IT
189 // Adding users to cohort that already exist in plans.
190 cohort_add_member($cohort->id, $user3->id);
191 cohort_add_member($cohort->id, $user4->id);
192
193 $task->execute();
194 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get_id())));
195 }
7a9f14c3 196
9031be44
FM
197 public function test_sync_plans_from_cohorts_with_passed_duedate() {
198 global $DB;
199
200 $this->resetAfterTest(true);
201 $this->setAdminUser();
202 $dg = $this->getDataGenerator();
203 $lpg = $dg->get_plugin_generator('tool_lp');
204
205 $user1 = $dg->create_user();
206 $user2 = $dg->create_user();
207 $cohort = $dg->create_cohort();
208 $tpl = $lpg->create_template(array('duedate' => time() + 1000));
209 $templatecohort = api::create_template_cohort($tpl->get_id(), $cohort->id);
210 $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\sync_plans_from_template_cohorts_task');
211
212 // Add 1 user to the cohort.
213 cohort_add_member($cohort->id, $user1->id);
214
215 // Creating plans from template cohort.
216 $task->execute();
217 $this->assertEquals(1, \tool_lp\plan::count_records());
218
219 // Now add another user, but this time the template will be expired.
220 cohort_add_member($cohort->id, $user2->id);
221 $record = $tpl->to_record();
222 $record->duedate = time() - 10000;
223 $DB->update_record(\tool_lp\template::TABLE, $record);
224 $tpl->read();
225 $task->execute();
226 $this->assertEquals(1, \tool_lp\plan::count_records()); // Still only one plan.
227
228 // Pretend it wasn't expired.
229 $tpl->set_duedate(time() + 100);
230 $tpl->update();
231 $task->execute();
232 $this->assertEquals(2, \tool_lp\plan::count_records()); // Now there is two.
233 }
234
7a9f14c3
IT
235 public function test_complete_plans_task() {
236 global $DB;
237 $this->resetAfterTest(true);
238 $this->setAdminUser();
239 $dg = $this->getDataGenerator();
240 $lpg = $dg->get_plugin_generator('tool_lp');
241
242 $user = $dg->create_user();
243
244 $up1 = $lpg->create_plan(array('userid' => $user->id,
245 'status' => \tool_lp\plan::STATUS_DRAFT));
246 $up2 = $lpg->create_plan(array('userid' => $user->id,
247 'status' => \tool_lp\plan::STATUS_ACTIVE));
248 // Set duedate in the past.
249 $date = new \DateTime('yesterday');
250 $record1 = $up1->to_record();
251 $record2 = $up2->to_record();
252
253 $record1->duedate = $date->getTimestamp();
254 $record2->duedate = $date->getTimestamp();
255 $DB->update_record(plan::TABLE, $record1);
256 $DB->update_record(plan::TABLE, $record2);
257
258 $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\complete_plans_task');
259 $this->assertInstanceOf('\\tool_lp\\task\\complete_plans_task', $task);
260
261 // Test that draft plan can not be completed on running task.
262 $task->execute();
263
264 $plandraft = api::read_plan($up1->get_id());
265 $this->assertEquals(\tool_lp\plan::STATUS_DRAFT, $plandraft->get_status());
266
267 // Test that active plan can be completed on running task.
268 $task->execute();
269
270 $planactive = api::read_plan($up2->get_id());
271 $this->assertEquals(\tool_lp\plan::STATUS_COMPLETE, $planactive->get_status());
272 }
ed549dbf 273}