MDL-52454 tool_lp: Missing PARAM_RAW in exporter tests
[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 }
7a9f14c3
IT
117
118 public function test_complete_plans_task() {
119 global $DB;
120 $this->resetAfterTest(true);
121 $this->setAdminUser();
122 $dg = $this->getDataGenerator();
123 $lpg = $dg->get_plugin_generator('tool_lp');
124
125 $user = $dg->create_user();
126
127 $up1 = $lpg->create_plan(array('userid' => $user->id,
128 'status' => \tool_lp\plan::STATUS_DRAFT));
129 $up2 = $lpg->create_plan(array('userid' => $user->id,
130 'status' => \tool_lp\plan::STATUS_ACTIVE));
131 // Set duedate in the past.
132 $date = new \DateTime('yesterday');
133 $record1 = $up1->to_record();
134 $record2 = $up2->to_record();
135
136 $record1->duedate = $date->getTimestamp();
137 $record2->duedate = $date->getTimestamp();
138 $DB->update_record(plan::TABLE, $record1);
139 $DB->update_record(plan::TABLE, $record2);
140
141 $task = \core\task\manager::get_scheduled_task('\\tool_lp\\task\\complete_plans_task');
142 $this->assertInstanceOf('\\tool_lp\\task\\complete_plans_task', $task);
143
144 // Test that draft plan can not be completed on running task.
145 $task->execute();
146
147 $plandraft = api::read_plan($up1->get_id());
148 $this->assertEquals(\tool_lp\plan::STATUS_DRAFT, $plandraft->get_status());
149
150 // Test that active plan can be completed on running task.
151 $task->execute();
152
153 $planactive = api::read_plan($up2->get_id());
154 $this->assertEquals(\tool_lp\plan::STATUS_COMPLETE, $planactive->get_status());
155 }
ed549dbf 156}