Merge branch 'MDL-42392-master' of git://github.com/andrewnicols/moodle
[moodle.git] / enrol / meta / tests / plugin_test.php
blob05cb522847b6f1d09eb45aca2d5879dc921f3452
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 /**
18 * Meta enrolment sync functional test.
20 * @package enrol_meta
21 * @category phpunit
22 * @copyright 2013 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
30 class enrol_meta_plugin_testcase extends advanced_testcase {
32 protected function enable_plugin() {
33 $enabled = enrol_get_plugins(true);
34 $enabled['meta'] = true;
35 $enabled = array_keys($enabled);
36 set_config('enrol_plugins_enabled', implode(',', $enabled));
39 protected function disable_plugin() {
40 $enabled = enrol_get_plugins(true);
41 unset($enabled['meta']);
42 $enabled = array_keys($enabled);
43 set_config('enrol_plugins_enabled', implode(',', $enabled));
46 protected function is_meta_enrolled($user, $enrol, $role = null) {
47 global $DB;
49 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$enrol->id, 'userid'=>$user->id))) {
50 return false;
53 if ($role === null) {
54 return true;
57 return $this->has_role($user, $enrol, $role);
60 protected function has_role($user, $enrol, $role) {
61 global $DB;
63 $context = context_course::instance($enrol->courseid);
65 if ($role === false) {
66 if ($DB->record_exists('role_assignments', array('contextid'=>$context->id, 'userid'=>$user->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id))) {
67 return false;
69 } else if (!$DB->record_exists('role_assignments', array('contextid'=>$context->id, 'userid'=>$user->id, 'roleid'=>$role->id, 'component'=>'enrol_meta', 'itemid'=>$enrol->id))) {
70 return false;
73 return true;
76 public function test_sync() {
77 global $CFG, $DB;
79 $this->resetAfterTest(true);
81 $metalplugin = enrol_get_plugin('meta');
82 $manplugin = enrol_get_plugin('manual');
84 $user1 = $this->getDataGenerator()->create_user();
85 $user2 = $this->getDataGenerator()->create_user();
86 $user3 = $this->getDataGenerator()->create_user();
87 $user4 = $this->getDataGenerator()->create_user();
88 $user5 = $this->getDataGenerator()->create_user();
90 $course1 = $this->getDataGenerator()->create_course();
91 $course2 = $this->getDataGenerator()->create_course();
92 $course3 = $this->getDataGenerator()->create_course();
93 $course4 = $this->getDataGenerator()->create_course();
94 $manual1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
95 $manual2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
96 $manual3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
97 $manual4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
99 $student = $DB->get_record('role', array('shortname'=>'student'));
100 $teacher = $DB->get_record('role', array('shortname'=>'teacher'));
101 $manager = $DB->get_record('role', array('shortname'=>'manager'));
103 $this->disable_plugin();
105 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
106 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $student->id);
107 $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 0);
108 $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id);
109 $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $manager->id);
111 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id);
112 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $teacher->id);
114 $this->assertEquals(7, $DB->count_records('user_enrolments'));
115 $this->assertEquals(6, $DB->count_records('role_assignments'));
117 set_config('syncall', 0, 'enrol_meta');
118 set_config('nosyncroleids', $manager->id, 'enrol_meta');
120 require_once($CFG->dirroot.'/enrol/meta/locallib.php');
122 enrol_meta_sync(null, false);
123 $this->assertEquals(7, $DB->count_records('user_enrolments'));
124 $this->assertEquals(6, $DB->count_records('role_assignments'));
126 $this->enable_plugin();
127 enrol_meta_sync(null, false);
128 $this->assertEquals(7, $DB->count_records('user_enrolments'));
129 $this->assertEquals(6, $DB->count_records('role_assignments'));
131 $e1 = $metalplugin->add_instance($course3, array('customint1'=>$course1->id));
132 $e2 = $metalplugin->add_instance($course3, array('customint1'=>$course2->id));
133 $e3 = $metalplugin->add_instance($course4, array('customint1'=>$course2->id));
134 $enrol1 = $DB->get_record('enrol', array('id'=>$e1));
135 $enrol2 = $DB->get_record('enrol', array('id'=>$e2));
136 $enrol3 = $DB->get_record('enrol', array('id'=>$e3));
138 enrol_meta_sync($course4->id, false);
139 $this->assertEquals(9, $DB->count_records('user_enrolments'));
140 $this->assertEquals(8, $DB->count_records('role_assignments'));
141 $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
142 $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher));
144 enrol_meta_sync(null, false);
145 $this->assertEquals(14, $DB->count_records('user_enrolments'));
146 $this->assertEquals(13, $DB->count_records('role_assignments'));
148 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
149 $this->assertTrue($this->is_meta_enrolled($user2, $enrol1, $student));
150 $this->assertFalse($this->is_meta_enrolled($user3, $enrol1));
151 $this->assertTrue($this->is_meta_enrolled($user4, $enrol1, $teacher));
152 $this->assertFalse($this->is_meta_enrolled($user5, $enrol1));
154 $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student));
155 $this->assertTrue($this->is_meta_enrolled($user2, $enrol2, $teacher));
157 $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
158 $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher));
160 set_config('syncall', 1, 'enrol_meta');
161 enrol_meta_sync(null, false);
162 $this->assertEquals(16, $DB->count_records('user_enrolments'));
163 $this->assertEquals(13, $DB->count_records('role_assignments'));
165 $this->assertTrue($this->is_meta_enrolled($user3, $enrol1, false));
166 $this->assertTrue($this->is_meta_enrolled($user5, $enrol1, false));
168 $this->assertEquals(16, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
169 $this->disable_plugin();
170 $manplugin->unenrol_user($manual1, $user1->id);
171 $manplugin->unenrol_user($manual2, $user1->id);
173 $this->assertEquals(14, $DB->count_records('user_enrolments'));
174 $this->assertEquals(11, $DB->count_records('role_assignments'));
175 $this->assertEquals(14, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
177 $this->enable_plugin();
179 set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta');
180 enrol_meta_sync($course4->id, false);
181 $this->assertEquals(14, $DB->count_records('user_enrolments'));
182 $this->assertEquals(11, $DB->count_records('role_assignments'));
183 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
184 $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
185 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol3->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
187 enrol_meta_sync(null, false);
188 $this->assertEquals(14, $DB->count_records('user_enrolments'));
189 $this->assertEquals(11, $DB->count_records('role_assignments'));
190 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
191 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
192 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol1->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
193 $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student));
194 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol2->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
196 set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
197 enrol_meta_sync($course4->id, false);
198 $this->assertEquals(14, $DB->count_records('user_enrolments'));
199 $this->assertEquals(10, $DB->count_records('role_assignments'));
200 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
201 $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, false));
202 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol3->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
204 enrol_meta_sync(null, false);
205 $this->assertEquals(14, $DB->count_records('user_enrolments'));
206 $this->assertEquals(8, $DB->count_records('role_assignments'));
207 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
208 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
209 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol1->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
210 $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, false));
211 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol2->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id)));
213 set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
214 enrol_meta_sync($course4->id, false);
215 $this->assertEquals(13, $DB->count_records('user_enrolments'));
216 $this->assertEquals(8, $DB->count_records('role_assignments'));
217 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
218 $this->assertFalse($this->is_meta_enrolled($user1, $enrol3));
220 enrol_meta_sync(null, false);
221 $this->assertEquals(11, $DB->count_records('user_enrolments'));
222 $this->assertEquals(8, $DB->count_records('role_assignments'));
223 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
224 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
225 $this->assertFalse($this->is_meta_enrolled($user1, $enrol2));
228 // Now try sync triggered by events.
230 set_config('syncall', 1, 'enrol_meta');
232 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
233 $this->assertEquals(13, $DB->count_records('user_enrolments'));
234 $this->assertEquals(10, $DB->count_records('role_assignments'));
235 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
236 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
237 enrol_meta_sync(null, false);
238 $this->assertEquals(13, $DB->count_records('user_enrolments'));
239 $this->assertEquals(10, $DB->count_records('role_assignments'));
240 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
241 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
243 $manplugin->unenrol_user($manual1, $user1->id);
244 $this->assertEquals(11, $DB->count_records('user_enrolments'));
245 $this->assertEquals(8, $DB->count_records('role_assignments'));
246 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
247 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
248 enrol_meta_sync(null, false);
249 $this->assertEquals(11, $DB->count_records('user_enrolments'));
250 $this->assertEquals(8, $DB->count_records('role_assignments'));
251 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
252 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
254 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
255 $this->assertEquals(13, $DB->count_records('user_enrolments'));
256 $this->assertEquals(8, $DB->count_records('role_assignments'));
257 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
258 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
259 enrol_meta_sync(null, false);
260 $this->assertEquals(13, $DB->count_records('user_enrolments'));
261 $this->assertEquals(8, $DB->count_records('role_assignments'));
262 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
263 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
265 $manplugin->unenrol_user($manual1, $user1->id);
266 $this->assertEquals(11, $DB->count_records('user_enrolments'));
267 $this->assertEquals(8, $DB->count_records('role_assignments'));
268 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
269 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
270 enrol_meta_sync(null, false);
271 $this->assertEquals(11, $DB->count_records('user_enrolments'));
272 $this->assertEquals(8, $DB->count_records('role_assignments'));
273 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
274 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
276 set_config('syncall', 0, 'enrol_meta');
277 enrol_meta_sync(null, false);
278 $this->assertEquals(9, $DB->count_records('user_enrolments'));
279 $this->assertEquals(8, $DB->count_records('role_assignments'));
280 $this->assertEquals(9, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
281 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
283 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0);
284 $this->assertEquals(10, $DB->count_records('user_enrolments'));
285 $this->assertEquals(8, $DB->count_records('role_assignments'));
286 $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
287 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
288 enrol_meta_sync(null, false);
289 $this->assertEquals(10, $DB->count_records('user_enrolments'));
290 $this->assertEquals(8, $DB->count_records('role_assignments'));
291 $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
292 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
294 role_assign($teacher->id, $user1->id, context_course::instance($course1->id)->id);
295 $this->assertEquals(11, $DB->count_records('user_enrolments'));
296 $this->assertEquals(10, $DB->count_records('role_assignments'));
297 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
298 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher));
299 enrol_meta_sync(null, false);
300 $this->assertEquals(11, $DB->count_records('user_enrolments'));
301 $this->assertEquals(10, $DB->count_records('role_assignments'));
302 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
303 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher));
305 role_unassign($teacher->id, $user1->id, context_course::instance($course1->id)->id);
306 $this->assertEquals(10, $DB->count_records('user_enrolments'));
307 $this->assertEquals(8, $DB->count_records('role_assignments'));
308 $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
309 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
310 enrol_meta_sync(null, false);
311 $this->assertEquals(10, $DB->count_records('user_enrolments'));
312 $this->assertEquals(8, $DB->count_records('role_assignments'));
313 $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
314 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student));
316 $manplugin->unenrol_user($manual1, $user1->id);
317 $this->assertEquals(9, $DB->count_records('user_enrolments'));
318 $this->assertEquals(8, $DB->count_records('role_assignments'));
319 $this->assertEquals(9, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
320 $this->assertFalse($this->is_meta_enrolled($user1, $enrol1));
322 set_config('syncall', 1, 'enrol_meta');
323 set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta');
324 enrol_meta_sync(null, false);
325 $this->assertEquals(11, $DB->count_records('user_enrolments'));
326 $this->assertEquals(8, $DB->count_records('role_assignments'));
327 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
329 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
330 $this->assertEquals(13, $DB->count_records('user_enrolments'));
331 $this->assertEquals(10, $DB->count_records('role_assignments'));
332 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
333 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
334 enrol_meta_sync(null, false);
335 $this->assertEquals(13, $DB->count_records('user_enrolments'));
336 $this->assertEquals(10, $DB->count_records('role_assignments'));
337 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
338 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
340 $manplugin->update_user_enrol($manual1, $user1->id, ENROL_USER_SUSPENDED);
341 $this->assertEquals(13, $DB->count_records('user_enrolments'));
342 $this->assertEquals(10, $DB->count_records('role_assignments'));
343 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
344 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
345 enrol_meta_sync(null, false);
346 $this->assertEquals(13, $DB->count_records('user_enrolments'));
347 $this->assertEquals(10, $DB->count_records('role_assignments'));
348 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
349 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
351 $manplugin->unenrol_user($manual1, $user1->id);
352 $this->assertEquals(12, $DB->count_records('user_enrolments'));
353 $this->assertEquals(9, $DB->count_records('role_assignments'));
354 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
355 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
356 enrol_meta_sync(null, false);
357 $this->assertEquals(12, $DB->count_records('user_enrolments'));
358 $this->assertEquals(9, $DB->count_records('role_assignments'));
359 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
360 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
362 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
363 $this->assertEquals(13, $DB->count_records('user_enrolments'));
364 $this->assertEquals(10, $DB->count_records('role_assignments'));
365 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
366 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
367 enrol_meta_sync(null, false);
368 $this->assertEquals(13, $DB->count_records('user_enrolments'));
369 $this->assertEquals(10, $DB->count_records('role_assignments'));
370 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
371 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
373 set_config('syncall', 1, 'enrol_meta');
374 set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
375 enrol_meta_sync(null, false);
376 $this->assertEquals(13, $DB->count_records('user_enrolments'));
377 $this->assertEquals(10, $DB->count_records('role_assignments'));
378 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
380 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
381 $this->assertEquals(13, $DB->count_records('user_enrolments'));
382 $this->assertEquals(10, $DB->count_records('role_assignments'));
383 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
384 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
385 enrol_meta_sync(null, false);
386 $this->assertEquals(13, $DB->count_records('user_enrolments'));
387 $this->assertEquals(10, $DB->count_records('role_assignments'));
388 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
389 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
391 $manplugin->unenrol_user($manual1, $user1->id);
392 $this->assertEquals(12, $DB->count_records('user_enrolments'));
393 $this->assertEquals(8, $DB->count_records('role_assignments'));
394 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
395 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
396 enrol_meta_sync(null, false);
397 $this->assertEquals(12, $DB->count_records('user_enrolments'));
398 $this->assertEquals(8, $DB->count_records('role_assignments'));
399 $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
400 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false));
402 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
403 $this->assertEquals(13, $DB->count_records('user_enrolments'));
404 $this->assertEquals(10, $DB->count_records('role_assignments'));
405 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
406 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
407 enrol_meta_sync(null, false);
408 $this->assertEquals(13, $DB->count_records('user_enrolments'));
409 $this->assertEquals(10, $DB->count_records('role_assignments'));
410 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
411 $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student));
414 set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
415 enrol_meta_sync(null, false);
416 $this->assertEquals(13, $DB->count_records('user_enrolments'));
417 $this->assertEquals(10, $DB->count_records('role_assignments'));
418 $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
420 delete_course($course1, false);
421 $this->assertEquals(3, $DB->count_records('user_enrolments'));
422 $this->assertEquals(3, $DB->count_records('role_assignments'));
423 $this->assertEquals(3, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
424 enrol_meta_sync(null, false);
425 $this->assertEquals(3, $DB->count_records('user_enrolments'));
426 $this->assertEquals(3, $DB->count_records('role_assignments'));
427 $this->assertEquals(3, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
429 delete_course($course2, false);
430 $this->assertEquals(0, $DB->count_records('user_enrolments'));
431 $this->assertEquals(0, $DB->count_records('role_assignments'));
432 $this->assertEquals(0, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
433 enrol_meta_sync(null, false);
434 $this->assertEquals(0, $DB->count_records('user_enrolments'));
435 $this->assertEquals(0, $DB->count_records('role_assignments'));
436 $this->assertEquals(0, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE)));
438 delete_course($course3, false);
439 delete_course($course4, false);
444 * Test user_enrolment_created event.
446 public function test_user_enrolment_created_observer() {
447 global $DB;
449 $this->resetAfterTest();
451 $metaplugin = enrol_get_plugin('meta');
452 $user1 = $this->getDataGenerator()->create_user();
453 $course1 = $this->getDataGenerator()->create_course();
454 $course2 = $this->getDataGenerator()->create_course();
455 $student = $DB->get_record('role', array('shortname' => 'student'));
457 $e1 = $metaplugin->add_instance($course2, array('customint1' => $course1->id));
458 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
460 // Enrol user and capture event.
461 $sink = $this->redirectEvents();
463 $metaplugin->enrol_user($enrol1, $user1->id, $student->id);
464 $events = $sink->get_events();
465 $sink->close();
466 $event = array_shift($events);
468 // Test Event.
469 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
470 $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
471 $this->assertEquals($dbuserenrolled->id, $event->objectid);
472 $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
473 $expectedlegacyeventdata = $dbuserenrolled;
474 $expectedlegacyeventdata->enrol = 'meta';
475 $expectedlegacyeventdata->courseid = $course2->id;
476 $this->assertEventLegacyData($expectedlegacyeventdata, $event);
480 * Test user_enrolment_deleted observer.
482 public function test_user_enrolment_deleted_observer() {
483 global $DB;
485 $this->resetAfterTest(true);
487 $metalplugin = enrol_get_plugin('meta');
488 $user1 = $this->getDataGenerator()->create_user();
489 $course1 = $this->getDataGenerator()->create_course();
490 $course2 = $this->getDataGenerator()->create_course();
491 $student = $DB->get_record('role', array('shortname'=>'student'));
493 $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
494 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
496 // Enrol user.
497 $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
498 $this->assertEquals(1, $DB->count_records('user_enrolments'));
500 // Unenrol user and capture event.
501 $sink = $this->redirectEvents();
502 $metalplugin->unenrol_user($enrol1, $user1->id);
503 $events = $sink->get_events();
504 $sink->close();
505 $event = array_pop($events);
507 $this->assertEquals(0, $DB->count_records('user_enrolments'));
508 $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
509 $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
513 * Test user_enrolment_updated event.
515 public function test_user_enrolment_updated_observer() {
516 global $DB;
518 $this->resetAfterTest(true);
520 $metalplugin = enrol_get_plugin('meta');
521 $user1 = $this->getDataGenerator()->create_user();
522 $course1 = $this->getDataGenerator()->create_course();
523 $course2 = $this->getDataGenerator()->create_course();
524 $student = $DB->get_record('role', array('shortname'=>'student'));
526 $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
527 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
529 // Enrol user.
530 $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
531 $this->assertEquals(1, $DB->count_records('user_enrolments'));
533 // Updated enrolment for user and capture event.
534 $sink = $this->redirectEvents();
535 $metalplugin->update_user_enrol($enrol1, $user1->id, ENROL_USER_SUSPENDED, null, time());
536 $events = $sink->get_events();
537 $sink->close();
538 $event = array_shift($events);
540 // Test Event.
541 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
542 $this->assertInstanceOf('\core\event\user_enrolment_updated', $event);
543 $this->assertEquals($dbuserenrolled->id, $event->objectid);
544 $this->assertEquals('user_enrol_modified', $event->get_legacy_eventname());
545 $expectedlegacyeventdata = $dbuserenrolled;
546 $expectedlegacyeventdata->enrol = 'meta';
547 $expectedlegacyeventdata->courseid = $course2->id;
548 $this->assertEventLegacyData($expectedlegacyeventdata, $event);