MDL-52528 tool_lp: Weird behaviour if deleting competency in user plan
[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())));
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
f4428607
FM
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
ed549dbf
IT
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}