PageRenderTime 44ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/competency/tests/task/task_test.php

https://bitbucket.org/moodle/moodle
PHP | 307 lines | 204 code | 57 blank | 46 comment | 0 complexity | aaaaf28cab81edde31eca9af2608e472 MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, BSD-3-Clause, MIT, GPL-3.0
  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. namespace core_competency\task;
  17. use core_competency\api;
  18. use core_competency\plan;
  19. use core_competency\template;
  20. /**
  21. * Task tests.
  22. *
  23. * @package core_competency
  24. * @copyright 2015 Issam Taboubi <issam.taboubi@umontreal.ca>
  25. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26. */
  27. class task_test extends \advanced_testcase {
  28. public function test_sync_plans_from_cohorts_task() {
  29. global $DB;
  30. $this->resetAfterTest(true);
  31. $this->setAdminUser();
  32. $dg = $this->getDataGenerator();
  33. $lpg = $dg->get_plugin_generator('core_competency');
  34. // Sql to simulate the execution in time.
  35. $cmsql = "UPDATE {cohort_members} SET timeadded = :currenttime WHERE cohortid = :cohortid AND userid = :userid";
  36. $tplsql = "UPDATE {" . template::TABLE . "} SET timemodified = :currenttime WHERE id = :templateid";
  37. $plansql = "UPDATE {" . plan::TABLE . "} SET timemodified = :currenttime WHERE id = :planid";
  38. $currenttime = time();
  39. $user1 = $dg->create_user();
  40. $user2 = $dg->create_user();
  41. $user3 = $dg->create_user();
  42. $user4 = $dg->create_user();
  43. $user5 = $dg->create_user();
  44. $cohort = $dg->create_cohort();
  45. $tpl = $lpg->create_template();
  46. // Add 2 users to the cohort.
  47. cohort_add_member($cohort->id, $user1->id);
  48. cohort_add_member($cohort->id, $user2->id);
  49. // Creating plans from template cohort.
  50. $templatecohort = api::create_template_cohort($tpl->get('id'), $cohort->id);
  51. $created = api::create_plans_from_template_cohort($tpl->get('id'), $cohort->id);
  52. $this->assertEquals(2, $created);
  53. $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
  54. $this->assertInstanceOf('\core\task\sync_plans_from_template_cohorts_task', $task);
  55. // Add two more users to the cohort.
  56. cohort_add_member($cohort->id, $user3->id);
  57. cohort_add_member($cohort->id, $user4->id);
  58. $currenttime = $currenttime + 1;
  59. $task->execute();
  60. $task->set_last_run_time($currenttime);
  61. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  62. // Test if remove user from cohort will affect plans.
  63. cohort_remove_member($cohort->id, $user3->id);
  64. cohort_remove_member($cohort->id, $user4->id);
  65. $currenttime = $currenttime + 1;
  66. $task->execute();
  67. $task->set_last_run_time($currenttime);
  68. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  69. // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
  70. $currenttime = $currenttime + 1;
  71. $tpl->set('visible', false);
  72. $tpl->update();
  73. $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get('id')));
  74. $currenttime = $currenttime + 1;
  75. cohort_add_member($cohort->id, $user5->id);
  76. $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user5->id));
  77. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  78. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  79. $currenttime = $currenttime + 1;
  80. $task->execute();
  81. $task->set_last_run_time($currenttime);
  82. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  83. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  84. // Now I set the template as visible again, the plan is created.
  85. $currenttime = $currenttime + 1;
  86. $tpl->set('visible', true);
  87. $tpl->update();
  88. $DB->execute($tplsql, array('currenttime' => $currenttime, 'templateid' => $tpl->get('id')));
  89. $currenttime = $currenttime + 1;
  90. $task->execute();
  91. $task->set_last_run_time($currenttime);
  92. $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  93. $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get('id'))));
  94. // Let's unlink the plan and run the task again, it should not be recreated.
  95. $currenttime = $currenttime + 1;
  96. $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get('id')));
  97. api::unlink_plan_from_template($plan);
  98. $DB->execute($plansql, array('currenttime' => $currenttime, 'planid' => $plan->get('id')));
  99. $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
  100. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  101. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  102. $currenttime = $currenttime + 1;
  103. $task->execute();
  104. $task->set_last_run_time($currenttime);
  105. $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
  106. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  107. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  108. // Adding users to cohort that already exist in plans.
  109. $currenttime = $currenttime + 1;
  110. cohort_add_member($cohort->id, $user3->id);
  111. cohort_add_member($cohort->id, $user4->id);
  112. $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id));
  113. $DB->execute($cmsql, array('currenttime' => $currenttime, 'cohortid' => $cohort->id, 'userid' => $user3->id));
  114. $currenttime = $currenttime + 1;
  115. $task->execute();
  116. $task->set_last_run_time($currenttime);
  117. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  118. // Test a user plan deleted will not be recreated.
  119. $currenttime = $currenttime + 1;
  120. $plan = plan::get_record(array('userid' => $user4->id, 'templateid' => $tpl->get('id')));
  121. api::delete_plan($plan->get('id'));
  122. $currenttime = $currenttime + 1;
  123. $task->execute();
  124. $task->set_last_run_time($currenttime);
  125. $this->assertEquals(3, plan::count_records(array('templateid' => $tpl->get('id'))));
  126. }
  127. public function test_sync_plans_from_cohorts_with_templateduedate_task() {
  128. $this->resetAfterTest(true);
  129. $this->setAdminUser();
  130. $dg = $this->getDataGenerator();
  131. $lpg = $dg->get_plugin_generator('core_competency');
  132. $user1 = $dg->create_user();
  133. $user2 = $dg->create_user();
  134. $user3 = $dg->create_user();
  135. $user4 = $dg->create_user();
  136. $user5 = $dg->create_user();
  137. $cohort = $dg->create_cohort();
  138. $tpl = $lpg->create_template(array('duedate' => time() + 400));
  139. // Add 2 users to the cohort.
  140. cohort_add_member($cohort->id, $user1->id);
  141. cohort_add_member($cohort->id, $user2->id);
  142. // Creating plans from template cohort.
  143. $templatecohort = api::create_template_cohort($tpl->get('id'), $cohort->id);
  144. $created = api::create_plans_from_template_cohort($tpl->get('id'), $cohort->id);
  145. $this->assertEquals(2, $created);
  146. $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
  147. $this->assertInstanceOf('\core\task\sync_plans_from_template_cohorts_task', $task);
  148. // Add two more users to the cohort.
  149. cohort_add_member($cohort->id, $user3->id);
  150. cohort_add_member($cohort->id, $user4->id);
  151. $task->execute();
  152. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  153. // Test if remove user from cohort will affect plans.
  154. cohort_remove_member($cohort->id, $user3->id);
  155. cohort_remove_member($cohort->id, $user4->id);
  156. $task->execute();
  157. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  158. // The template is now hidden, and I've added a user with a missing plan. Nothing should happen.
  159. $tpl->set('visible', false);
  160. $tpl->update();
  161. cohort_add_member($cohort->id, $user5->id);
  162. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  163. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  164. $task->execute();
  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. // Now I set the template as visible again, the plan is created.
  168. $tpl->set('visible', true);
  169. $tpl->update();
  170. $task->execute();
  171. $this->assertTrue(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  172. $this->assertEquals(5, plan::count_records(array('templateid' => $tpl->get('id'))));
  173. // Let's unlink the plan and run the task again, it should not be recreated.
  174. $plan = plan::get_record(array('userid' => $user5->id, 'templateid' => $tpl->get('id')));
  175. api::unlink_plan_from_template($plan);
  176. $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
  177. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  178. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  179. $task->execute();
  180. $this->assertTrue(plan::record_exists_select('userid = ?', array($user5->id)));
  181. $this->assertFalse(plan::record_exists_select('userid = ? AND templateid = ?', array($user5->id, $tpl->get('id'))));
  182. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  183. // Adding users to cohort that already exist in plans.
  184. cohort_add_member($cohort->id, $user3->id);
  185. cohort_add_member($cohort->id, $user4->id);
  186. $task->execute();
  187. $this->assertEquals(4, plan::count_records(array('templateid' => $tpl->get('id'))));
  188. }
  189. public function test_sync_plans_from_cohorts_with_passed_duedate() {
  190. global $DB;
  191. $this->resetAfterTest(true);
  192. $this->setAdminUser();
  193. $dg = $this->getDataGenerator();
  194. $lpg = $dg->get_plugin_generator('core_competency');
  195. $user1 = $dg->create_user();
  196. $user2 = $dg->create_user();
  197. $cohort = $dg->create_cohort();
  198. $tpl = $lpg->create_template(array('duedate' => time() + 1000));
  199. $templatecohort = api::create_template_cohort($tpl->get('id'), $cohort->id);
  200. $task = \core\task\manager::get_scheduled_task('\\core\\task\\sync_plans_from_template_cohorts_task');
  201. // Add 1 user to the cohort.
  202. cohort_add_member($cohort->id, $user1->id);
  203. // Creating plans from template cohort.
  204. $task->execute();
  205. $this->assertEquals(1, plan::count_records());
  206. // Now add another user, but this time the template will be expired.
  207. cohort_add_member($cohort->id, $user2->id);
  208. $record = $tpl->to_record();
  209. $record->duedate = time() - 10000;
  210. $DB->update_record(template::TABLE, $record);
  211. $tpl->read();
  212. $task->execute();
  213. $this->assertEquals(1, plan::count_records()); // Still only one plan.
  214. // Pretend it wasn't expired.
  215. $tpl->set('duedate', time() + 100);
  216. $tpl->update();
  217. $task->execute();
  218. $this->assertEquals(2, plan::count_records()); // Now there is two.
  219. }
  220. public function test_complete_plans_task() {
  221. global $DB;
  222. $this->resetAfterTest(true);
  223. $this->setAdminUser();
  224. $dg = $this->getDataGenerator();
  225. $lpg = $dg->get_plugin_generator('core_competency');
  226. $user = $dg->create_user();
  227. $up1 = $lpg->create_plan(array('userid' => $user->id,
  228. 'status' => plan::STATUS_DRAFT));
  229. $up2 = $lpg->create_plan(array('userid' => $user->id,
  230. 'status' => plan::STATUS_ACTIVE));
  231. // Set duedate in the past.
  232. $date = new \DateTime('yesterday');
  233. $record1 = $up1->to_record();
  234. $record2 = $up2->to_record();
  235. $record1->duedate = $date->getTimestamp();
  236. $record2->duedate = $date->getTimestamp();
  237. $DB->update_record(plan::TABLE, $record1);
  238. $DB->update_record(plan::TABLE, $record2);
  239. $task = \core\task\manager::get_scheduled_task('\\core\\task\\complete_plans_task');
  240. $this->assertInstanceOf('\\core\\task\\complete_plans_task', $task);
  241. // Test that draft plan can not be completed on running task.
  242. $task->execute();
  243. $plandraft = api::read_plan($up1->get('id'));
  244. $this->assertEquals(plan::STATUS_DRAFT, $plandraft->get('status'));
  245. // Test that active plan can be completed on running task.
  246. $task->execute();
  247. $planactive = api::read_plan($up2->get('id'));
  248. $this->assertEquals(plan::STATUS_COMPLETE, $planactive->get('status'));
  249. }
  250. }