See Release Notes
Long Term Support Release
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 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 */ 24 25 defined('MOODLE_INTERNAL') || die(); 26 27 use core_competency\api; 28 use core_competency\plan; 29 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 { 38 39 public function test_sync_plans_from_cohorts_task() { 40 global $DB; 41 42 $this->resetAfterTest(true); 43 $this->setAdminUser(); 44 $dg = $this->getDataGenerator(); 45 $lpg = $dg->get_plugin_generator('core_competency'); 46 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"; 51 52 $currenttime = time(); 53 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(); 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 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); 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 80 $currenttime = $currenttime + 1; 81 $task->execute(); 82 $task->set_last_run_time($currenttime); 83 84 $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id')))); 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 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')))); 94 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')))); 110 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')))); 121 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')))); 136 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)); 143 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')))); 148 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 } 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('core_competency'); 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(); 172 $tpl = $lpg->create_template(array('duedate' => time() + 400)); 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 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); 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')))); 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 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 \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')))); 229 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 } 237 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('core_competency'); 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); 251 $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task'); 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, \core_competency\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(\core_competency\template::TABLE, $record); 265 $tpl->read(); 266 $task->execute(); 267 $this->assertEquals(1, \core_competency\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, \core_competency\plan::count_records()); // Now there is two. 274 } 275 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'); 282 283 $user = $dg->create_user(); 284 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(); 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 299 $task = \core\task\manager::get_scheduled_task('\\core\\task\\complete_plans_task'); 300 $this->assertInstanceOf('\\core\\task\\complete_plans_task', $task); 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(\core_competency\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(\core_competency\plan::STATUS_COMPLETE, $planactive->get('status')); 313 } 314 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body