2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
20 * @package core_competency
21 * @copyright 2016 Frédéric Massart - FMCorz.net
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 use core_competency\course_competency
;
28 use core_competency\course_module_competency
;
29 use core_competency\user_competency_course
;
34 * @package core_competency
35 * @copyright 2016 Frédéric Massart - FMCorz.net
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class core_competency_hooks_testcase
extends advanced_testcase
{
40 public function test_hook_course_deleted() {
41 $this->resetAfterTest();
42 $dg = $this->getDataGenerator();
43 $ccg = $dg->get_plugin_generator('core_competency');
45 $u1 = $dg->create_user();
47 $framework = $ccg->create_framework();
48 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
49 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
51 $c1 = $dg->create_course();
52 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
]);
53 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
]);
54 $assign1a = $dg->create_module('assign', ['course' => $c1]);
55 $assign1b = $dg->create_module('assign', ['course' => $c1]);
56 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid
]);
57 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid
]);
58 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
,
59 'userid' => $u1->id
]);
60 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
,
61 'userid' => $u1->id
]);
63 $c2 = $dg->create_course();
64 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
]);
65 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
]);
66 $assign2a = $dg->create_module('assign', ['course' => $c2]);
67 $assign2b = $dg->create_module('assign', ['course' => $c2]);
68 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid
]);
69 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid
]);
70 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
,
71 'userid' => $u1->id
]);
72 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
,
73 'userid' => $u1->id
]);
75 delete_course($c1, false);
77 $this->assertEquals(0, course_competency
::count_records(['courseid' => $c1->id
]));
78 $this->assertEquals(2, course_competency
::count_records(['courseid' => $c2->id
]));
79 $this->assertEquals(0, course_module_competency
::count_records(['cmid' => $assign1a->cmid
]));
80 $this->assertEquals(0, course_module_competency
::count_records(['cmid' => $assign1b->cmid
]));
81 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign2a->cmid
]));
82 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign2b->cmid
]));
83 $this->assertEquals(0, user_competency_course
::count_records(['courseid' => $c1->id
, 'userid' => $u1->id
]));
84 $this->assertEquals(2, user_competency_course
::count_records(['courseid' => $c2->id
, 'userid' => $u1->id
]));
87 public function test_hook_course_module_deleted() {
88 $this->resetAfterTest();
89 $dg = $this->getDataGenerator();
90 $ccg = $dg->get_plugin_generator('core_competency');
92 $u1 = $dg->create_user();
94 $framework = $ccg->create_framework();
95 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
96 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
98 $c1 = $dg->create_course();
99 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
]);
100 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
]);
101 $assign1a = $dg->create_module('assign', ['course' => $c1]);
102 $assign1b = $dg->create_module('assign', ['course' => $c1]);
103 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid
]);
104 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid
]);
105 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
,
106 'userid' => $u1->id
]);
107 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
,
108 'userid' => $u1->id
]);
110 $c2 = $dg->create_course();
111 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
]);
112 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
]);
113 $assign2a = $dg->create_module('assign', ['course' => $c2]);
114 $assign2b = $dg->create_module('assign', ['course' => $c2]);
115 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid
]);
116 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid
]);
117 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
,
118 'userid' => $u1->id
]);
119 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
,
120 'userid' => $u1->id
]);
122 course_delete_module($assign1b->cmid
);
124 $this->assertEquals(2, course_competency
::count_records(['courseid' => $c1->id
]));
125 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign1a->cmid
]));
126 $this->assertEquals(0, course_module_competency
::count_records(['cmid' => $assign1b->cmid
]));
127 $this->assertEquals(2, user_competency_course
::count_records(['courseid' => $c1->id
]));
129 $this->assertEquals(2, course_competency
::count_records(['courseid' => $c2->id
]));
130 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign2a->cmid
]));
131 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign2b->cmid
]));
132 $this->assertEquals(2, user_competency_course
::count_records(['courseid' => $c2->id
, 'userid' => $u1->id
]));
135 public function test_hook_course_reset_competency_ratings() {
136 $this->resetAfterTest();
137 $dg = $this->getDataGenerator();
138 $ccg = $dg->get_plugin_generator('core_competency');
140 $u1 = $dg->create_user();
142 $framework = $ccg->create_framework();
143 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
144 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
146 $c1 = $dg->create_course();
147 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
]);
148 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
]);
149 $assign1a = $dg->create_module('assign', ['course' => $c1]);
150 $assign1b = $dg->create_module('assign', ['course' => $c1]);
151 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid
]);
152 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid
]);
153 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
,
154 'userid' => $u1->id
]);
155 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
,
156 'userid' => $u1->id
]);
158 $c2 = $dg->create_course();
159 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
]);
160 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
]);
161 $assign2a = $dg->create_module('assign', ['course' => $c2]);
162 $assign2b = $dg->create_module('assign', ['course' => $c2]);
163 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid
]);
164 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid
]);
165 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
,
166 'userid' => $u1->id
]);
167 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
,
168 'userid' => $u1->id
]);
170 reset_course_userdata((object) ['id' => $c1->id
, 'reset_competency_ratings' => true]);
172 $this->assertEquals(2, course_competency
::count_records(['courseid' => $c1->id
]));
173 $this->assertEquals(2, course_competency
::count_records(['courseid' => $c2->id
]));
174 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign1a->cmid
]));
175 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign1b->cmid
]));
176 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign2a->cmid
]));
177 $this->assertEquals(1, course_module_competency
::count_records(['cmid' => $assign2b->cmid
]));
178 $this->assertEquals(0, user_competency_course
::count_records(['courseid' => $c1->id
, 'userid' => $u1->id
]));
179 $this->assertEquals(2, user_competency_course
::count_records(['courseid' => $c2->id
, 'userid' => $u1->id
]));
182 public function test_hook_cohort_deleted() {
183 $this->resetAfterTest();
184 $this->setAdminUser();
186 $datagen = $this->getDataGenerator();
187 $corecompgen = $datagen->get_plugin_generator('core_competency');
189 $c1 = $datagen->create_cohort();
190 $c2 = $datagen->create_cohort();
191 $t1 = $corecompgen->create_template();
192 $t2 = $corecompgen->create_template();
194 // Create the template cohorts.
195 core_competency\api
::create_template_cohort($t1->get('id'), $c1->id
);
196 core_competency\api
::create_template_cohort($t1->get('id'), $c2->id
);
197 core_competency\api
::create_template_cohort($t2->get('id'), $c1->id
);
199 // Check that the association was made.
200 $this->assertEquals(2, \core_competency\template_cohort
::count_records(array('templateid' => $t1->get('id'))));
201 $this->assertEquals(1, \core_competency\template_cohort
::count_records(array('templateid' => $t2->get('id'))));
203 // Delete the first cohort.
204 cohort_delete_cohort($c1);
206 // Check that the association was removed.
207 $this->assertEquals(1, \core_competency\template_cohort
::count_records(array('templateid' => $t1->get('id'))));
208 $this->assertEquals(0, \core_competency\template_cohort
::count_records(array('templateid' => $t2->get('id'))));
211 public function test_hook_user_deleted() {
212 $this->resetAfterTest();
213 $dg = $this->getDataGenerator();
214 $ccg = $dg->get_plugin_generator('core_competency');
216 $u1 = $dg->create_user();
218 $framework = $ccg->create_framework();
219 $comp1 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
220 $comp2 = $ccg->create_competency(['competencyframeworkid' => $framework->get('id')]);
222 $c1 = $dg->create_course();
223 $cc1a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
]);
224 $cc1b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
]);
225 $assign1a = $dg->create_module('assign', ['course' => $c1]);
226 $assign1b = $dg->create_module('assign', ['course' => $c1]);
227 $cmc1a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1a->cmid
]);
228 $cmc1b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign1b->cmid
]);
229 $ucc1a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c1->id
,
230 'userid' => $u1->id
]);
231 $ucc1b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c1->id
,
232 'userid' => $u1->id
]);
234 $c2 = $dg->create_course();
235 $cc2a = $ccg->create_course_competency(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
]);
236 $cc2b = $ccg->create_course_competency(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
]);
237 $assign2a = $dg->create_module('assign', ['course' => $c2]);
238 $assign2b = $dg->create_module('assign', ['course' => $c2]);
239 $cmc2a = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2a->cmid
]);
240 $cmc2b = $ccg->create_course_module_competency(['competencyid' => $comp1->get('id'), 'cmid' => $assign2b->cmid
]);
241 $ucc2a = $ccg->create_user_competency_course(['competencyid' => $comp1->get('id'), 'courseid' => $c2->id
,
242 'userid' => $u1->id
]);
243 $ucc2b = $ccg->create_user_competency_course(['competencyid' => $comp2->get('id'), 'courseid' => $c2->id
,
244 'userid' => $u1->id
]);
246 reset_course_userdata((object) ['id' => $c1->id
, 'reset_competency_ratings' => true]);
250 // Assert the records don't exist anymore.
251 $this->assertEquals(0, user_competency_course
::count_records(['courseid' => $c1->id
, 'userid' => $u1->id
]));