weekly release 4.4dev
[moodle.git] / competency / tests / hooks_test.php
blob595d31c17c3b91d233f7fdc9911bec604b92da2c
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.
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/>.
17 namespace core_competency;
19 /**
20 * Hook tests.
22 * @package core_competency
23 * @copyright 2016 Frédéric Massart - FMCorz.net
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 class hooks_test extends \advanced_testcase {
28 public function test_hook_course_deleted() {
29 $this->resetAfterTest();
30 $dg = $this->getDataGenerator();
31 $ccg = $dg->get_plugin_generator('core_competency');
33 $u1 = $dg->create_user();
35 $framework = $ccg->create_framework();
36 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
37 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
39 $c1 = $dg->create_course();
40 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
41 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
42 $assign1a = $dg->create_module('assign', ['course' => $c1]);
43 $assign1b = $dg->create_module('assign', ['course' => $c1]);
44 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
45 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
46 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
47 'userid' => $u1->id]);
48 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
49 'userid' => $u1->id]);
51 $c2 = $dg->create_course();
52 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
53 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
54 $assign2a = $dg->create_module('assign', ['course' => $c2]);
55 $assign2b = $dg->create_module('assign', ['course' => $c2]);
56 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
57 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
58 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
59 'userid' => $u1->id]);
60 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
61 'userid' => $u1->id]);
63 delete_course($c1, false);
65 $this->assertEquals(0, course_competency::count_records(['courseid' => $c1->id]));
66 $this->assertEquals(2, course_competency::count_records(['courseid' => $c2->id]));
67 $this->assertEquals(0, course_module_competency::count_records(['cmid' => $assign1a->cmid]));
68 $this->assertEquals(0, course_module_competency::count_records(['cmid' => $assign1b->cmid]));
69 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2a->cmid]));
70 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2b->cmid]));
71 $this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));
72 $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id]));
75 public function test_hook_course_module_deleted() {
76 $this->resetAfterTest();
77 $dg = $this->getDataGenerator();
78 $ccg = $dg->get_plugin_generator('core_competency');
80 $u1 = $dg->create_user();
82 $framework = $ccg->create_framework();
83 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
84 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
86 $c1 = $dg->create_course();
87 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
88 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
89 $assign1a = $dg->create_module('assign', ['course' => $c1]);
90 $assign1b = $dg->create_module('assign', ['course' => $c1]);
91 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
92 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
93 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
94 'userid' => $u1->id]);
95 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
96 'userid' => $u1->id]);
98 $c2 = $dg->create_course();
99 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
100 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
101 $assign2a = $dg->create_module('assign', ['course' => $c2]);
102 $assign2b = $dg->create_module('assign', ['course' => $c2]);
103 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
104 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
105 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
106 'userid' => $u1->id]);
107 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
108 'userid' => $u1->id]);
110 course_delete_module($assign1b->cmid);
112 $this->assertEquals(2, course_competency::count_records(['courseid' => $c1->id]));
113 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign1a->cmid]));
114 $this->assertEquals(0, course_module_competency::count_records(['cmid' => $assign1b->cmid]));
115 $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c1->id]));
117 $this->assertEquals(2, course_competency::count_records(['courseid' => $c2->id]));
118 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2a->cmid]));
119 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2b->cmid]));
120 $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id]));
123 public function test_hook_course_reset_competency_ratings() {
124 $this->resetAfterTest();
125 $dg = $this->getDataGenerator();
126 $ccg = $dg->get_plugin_generator('core_competency');
128 $u1 = $dg->create_user();
130 $framework = $ccg->create_framework();
131 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
132 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
134 $c1 = $dg->create_course();
135 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
136 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
137 $assign1a = $dg->create_module('assign', ['course' => $c1]);
138 $assign1b = $dg->create_module('assign', ['course' => $c1]);
139 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
140 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
141 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
142 'userid' => $u1->id]);
143 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
144 'userid' => $u1->id]);
146 $c2 = $dg->create_course();
147 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
148 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
149 $assign2a = $dg->create_module('assign', ['course' => $c2]);
150 $assign2b = $dg->create_module('assign', ['course' => $c2]);
151 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
152 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
153 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
154 'userid' => $u1->id]);
155 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
156 'userid' => $u1->id]);
158 reset_course_userdata((object) ['id' => $c1->id, 'reset_competency_ratings' => true]);
160 $this->assertEquals(2, course_competency::count_records(['courseid' => $c1->id]));
161 $this->assertEquals(2, course_competency::count_records(['courseid' => $c2->id]));
162 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign1a->cmid]));
163 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign1b->cmid]));
164 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2a->cmid]));
165 $this->assertEquals(1, course_module_competency::count_records(['cmid' => $assign2b->cmid]));
166 $this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));
167 $this->assertEquals(2, user_competency_course::count_records(['courseid' => $c2->id, 'userid' => $u1->id]));
170 public function test_hook_cohort_deleted() {
171 $this->resetAfterTest();
172 $this->setAdminUser();
174 $datagen = $this->getDataGenerator();
175 $corecompgen = $datagen->get_plugin_generator('core_competency');
177 $c1 = $datagen->create_cohort();
178 $c2 = $datagen->create_cohort();
179 $t1 = $corecompgen->create_template();
180 $t2 = $corecompgen->create_template();
182 // Create the template cohorts.
183 api::create_template_cohort($t1->get('id'), $c1->id);
184 api::create_template_cohort($t1->get('id'), $c2->id);
185 api::create_template_cohort($t2->get('id'), $c1->id);
187 // Check that the association was made.
188 $this->assertEquals(2, \core_competency\template_cohort::count_records(array('templateid' => $t1->get('id'))));
189 $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t2->get('id'))));
191 // Delete the first cohort.
192 cohort_delete_cohort($c1);
194 // Check that the association was removed.
195 $this->assertEquals(1, \core_competency\template_cohort::count_records(array('templateid' => $t1->get('id'))));
196 $this->assertEquals(0, \core_competency\template_cohort::count_records(array('templateid' => $t2->get('id'))));
199 public function test_hook_user_deleted() {
200 $this->resetAfterTest();
201 $dg = $this->getDataGenerator();
202 $ccg = $dg->get_plugin_generator('core_competency');
204 $u1 = $dg->create_user();
206 $framework = $ccg->create_framework();
207 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
208 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
210 $c1 = $dg->create_course();
211 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id]);
212 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id]);
213 $assign1a = $dg->create_module('assign', ['course' => $c1]);
214 $assign1b = $dg->create_module('assign', ['course' => $c1]);
215 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid]);
216 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid]);
217 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id,
218 'userid' => $u1->id]);
219 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id,
220 'userid' => $u1->id]);
222 $c2 = $dg->create_course();
223 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id]);
224 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id]);
225 $assign2a = $dg->create_module('assign', ['course' => $c2]);
226 $assign2b = $dg->create_module('assign', ['course' => $c2]);
227 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid]);
228 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid]);
229 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id,
230 'userid' => $u1->id]);
231 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id,
232 'userid' => $u1->id]);
234 reset_course_userdata((object) ['id' => $c1->id, 'reset_competency_ratings' => true]);
236 delete_user($u1);
238 // Assert the records don't exist anymore.
239 $this->assertEquals(0, user_competency_course::count_records(['courseid' => $c1->id, 'userid' => $u1->id]));