forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_test.php
314 lines (256 loc) · 14.3 KB
/
task_test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Task tests.
*
* @package core_competency
* @copyright 2015 Issam Taboubi <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
use core_competency\api;
use core_competency\plan;
/**
* Task tests.
*
* @package core_competency
* @copyright 2015 Issam Taboubi <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_competency_task_testcase extends advanced_testcase {
public function test_sync_plans_from_cohorts_task() {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
// Sql to simulate the execution in time.
$cmsql = "UPDATE {cohort_members} SET timeadded = :currenttime WHERE cohortid = :cohortid AND userid = :userid";
$tplsql = "UPDATE {" . \core_competency\template::TABLE . "} SET timemodified = :currenttime WHERE id = :templateid";
$plansql = "UPDATE {" . \core_competency\plan::TABLE . "} SET timemodified = :currenttime WHERE id = :planid";
$currenttime = time();
$user1 = $dg->create_user();
$user2 = $dg->create_user();
$user3 = $dg->create_user();
$user4 = $dg->create_user();
$user5 = $dg->create_user();
$cohort = $dg->create_cohort();
$tpl = $lpg->create_template();
// Add 2 users to the cohort.
cohort_add_member($cohort->id, $user1->id);
cohort_add_member($cohort->id, $user2->id);
// Creating plans from template cohort.
$templatecohort = api::create_template_cohort($tpl->get('id'), $cohort->id);
$created = api::create_plans_from_template_cohort($tpl->get('id'), $cohort->id);
$this->assertEquals(2, $created);
$task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
$this->assertInstanceOf('\core\task\sync_plans_from_template_cohorts_task', $task);
// Add two more users to the cohort.
cohort_add_member($cohort->id, $user3->id);
cohort_add_member($cohort->id, $user4->id);
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Test if remove user from cohort will affect plans.
cohort_remove_member($cohort->id, $user3->id);
cohort_remove_member($cohort->id, $user4->id);
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
$currenttime = $currenttime + 1;
$tpl->set('visible', false);
$tpl->update();
$DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get('id')));
$currenttime = $currenttime + 1;
cohort_add_member($cohort->id, $user5->id);
$DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user5->id));
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Now I set the template as visible again, the plan is created.
$currenttime = $currenttime + 1;
$tpl->set('visible', true);
$tpl->update();
$DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get('id')));
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get('id'))));
// Let's unlink the plan and run the task again, it should not be recreated.
$currenttime = $currenttime + 1;
$plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get('id')));
\core_competency\api::unlink_plan_from_template($plan);
$DB->execute($plansql, array('currenttime' => $currenttime, 'planid' => $plan->get('id')));
$this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Adding users to cohort that already exist in plans.
$currenttime = $currenttime + 1;
cohort_add_member($cohort->id, $user3->id);
cohort_add_member($cohort->id, $user4->id);
$DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id));
$DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id));
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Test a user plan deleted will not be recreated.
$currenttime = $currenttime + 1;
$plan = plan::get_record(array('userid' => $user4->id, 'templateid' => $tpl->get('id')));
\core_competency\api::delete_plan($plan->get('id'));
$currenttime = $currenttime + 1;
$task->execute();
$task->set_last_run_time($currenttime);
$this->assertEquals(3, plan::count_records(array('templateid' => $tpl->get('id'))));
}
public function test_sync_plans_from_cohorts_with_templateduedate_task() {
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$user1 = $dg->create_user();
$user2 = $dg->create_user();
$user3 = $dg->create_user();
$user4 = $dg->create_user();
$user5 = $dg->create_user();
$cohort = $dg->create_cohort();
$tpl = $lpg->create_template(array('duedate' => time() + 400));
// Add 2 users to the cohort.
cohort_add_member($cohort->id, $user1->id);
cohort_add_member($cohort->id, $user2->id);
// Creating plans from template cohort.
$templatecohort = api::create_template_cohort($tpl->get('id'), $cohort->id);
$created = api::create_plans_from_template_cohort($tpl->get('id'), $cohort->id);
$this->assertEquals(2, $created);
$task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
$this->assertInstanceOf('\core\task\sync_plans_from_template_cohorts_task', $task);
// Add two more users to the cohort.
cohort_add_member($cohort->id, $user3->id);
cohort_add_member($cohort->id, $user4->id);
$task->execute();
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Test if remove user from cohort will affect plans.
cohort_remove_member($cohort->id, $user3->id);
cohort_remove_member($cohort->id, $user4->id);
$task->execute();
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
$tpl->set('visible', false);
$tpl->update();
cohort_add_member($cohort->id, $user5->id);
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
$task->execute();
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Now I set the template as visible again, the plan is created.
$tpl->set('visible', true);
$tpl->update();
$task->execute();
$this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get('id'))));
// Let's unlink the plan and run the task again, it should not be recreated.
$plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get('id')));
\core_competency\api::unlink_plan_from_template($plan);
$this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
$task->execute();
$this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
$this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
// Adding users to cohort that already exist in plans.
cohort_add_member($cohort->id, $user3->id);
cohort_add_member($cohort->id, $user4->id);
$task->execute();
$this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
}
public function test_sync_plans_from_cohorts_with_passed_duedate() {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$user1 = $dg->create_user();
$user2 = $dg->create_user();
$cohort = $dg->create_cohort();
$tpl = $lpg->create_template(array('duedate' => time() + 1000));
$templatecohort = api::create_template_cohort($tpl->get('id'), $cohort->id);
$task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
// Add 1 user to the cohort.
cohort_add_member($cohort->id, $user1->id);
// Creating plans from template cohort.
$task->execute();
$this->assertEquals(1, \core_competency\plan::count_records());
// Now add another user, but this time the template will be expired.
cohort_add_member($cohort->id, $user2->id);
$record = $tpl->to_record();
$record->duedate = time() - 10000;
$DB->update_record(\core_competency\template::TABLE, $record);
$tpl->read();
$task->execute();
$this->assertEquals(1, \core_competency\plan::count_records()); // Still only one plan.
// Pretend it wasn't expired.
$tpl->set('duedate', time() + 100);
$tpl->update();
$task->execute();
$this->assertEquals(2, \core_competency\plan::count_records()); // Now there is two.
}
public function test_complete_plans_task() {
global $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
$dg = $this->getDataGenerator();
$lpg = $dg->get_plugin_generator('core_competency');
$user = $dg->create_user();
$up1 = $lpg->create_plan(array('userid' => $user->id,
'status' => \core_competency\plan::STATUS_DRAFT));
$up2 = $lpg->create_plan(array('userid' => $user->id,
'status' => \core_competency\plan::STATUS_ACTIVE));
// Set duedate in the past.
$date = new \DateTime('yesterday');
$record1 = $up1->to_record();
$record2 = $up2->to_record();
$record1->duedate = $date->getTimestamp();
$record2->duedate = $date->getTimestamp();
$DB->update_record(plan::TABLE, $record1);
$DB->update_record(plan::TABLE, $record2);
$task = \core\task\manager::get_scheduled_task('\\core\\task\\complete_plans_task');
$this->assertInstanceOf('\\core\\task\\complete_plans_task', $task);
// Test that draft plan can not be completed on running task.
$task->execute();
$plandraft = api::read_plan($up1->get('id'));
$this->assertEquals(\core_competency\plan::STATUS_DRAFT, $plandraft->get('status'));
// Test that active plan can be completed on running task.
$task->execute();
$planactive = api::read_plan($up2->get('id'));
$this->assertEquals(\core_competency\plan::STATUS_COMPLETE, $planactive->get('status'));
}
}