MDL-17929 enrol_meta: synchronise with groups
[moodle.git] / enrol / meta / tests / plugin_test.php
CommitLineData
1344bc78
PS
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 * Meta enrolment sync functional test.
19 *
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
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29
30class enrol_meta_plugin_testcase extends advanced_testcase {
31
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));
37 }
38
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));
44 }
45
46 protected function is_meta_enrolled($user, $enrol, $role = null) {
47 global $DB;
48
49 if (!$DB->record_exists('user_enrolments', array('enrolid'=>$enrol->id, 'userid'=>$user->id))) {
50 return false;
51 }
52
53 if ($role === null) {
54 return true;
55 }
56
57 return $this->has_role($user, $enrol, $role);
58 }
59
60 protected function has_role($user, $enrol, $role) {
61 global $DB;
62
63 $context = context_course::instance($enrol->courseid);
64
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;
68 }
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;
71 }
72
73 return true;
74 }
9459ccc9 75
1344bc78
PS
76 public function test_sync() {
77 global $CFG, $DB;
78
79 $this->resetAfterTest(true);
80
81 $metalplugin = enrol_get_plugin('meta');
82 $manplugin = enrol_get_plugin('manual');
1344bc78
PS
83
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();
89
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);
98
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'));
102
103 $this->disable_plugin();
104
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);
110
111 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id);
112 $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $teacher->id);
113
114 $this->assertEquals(7, $DB->count_records('user_enrolments'));
115 $this->assertEquals(6, $DB->count_records('role_assignments'));
116
117 set_config('syncall', 0, 'enrol_meta');
118 set_config('nosyncroleids', $manager->id, 'enrol_meta');
119
120 require_once($CFG->dirroot.'/enrol/meta/locallib.php');
121
122 enrol_meta_sync(null, false);
123 $this->assertEquals(7, $DB->count_records('user_enrolments'));
124 $this->assertEquals(6, $DB->count_records('role_assignments'));
125
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'));
130
9e37d365
MG
131 $e1 = $metalplugin->add_instance($course3, array('customint1'=>$course1->id));
132 $e2 = $metalplugin->add_instance($course3, array('customint1'=>$course2->id));
1344bc78
PS
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));
137
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));
143
144 enrol_meta_sync(null, false);
145 $this->assertEquals(14, $DB->count_records('user_enrolments'));
146 $this->assertEquals(13, $DB->count_records('role_assignments'));
147
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));
153
154 $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student));
155 $this->assertTrue($this->is_meta_enrolled($user2, $enrol2, $teacher));
156
157 $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student));
158 $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher));
159
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'));
164
165 $this->assertTrue($this->is_meta_enrolled($user3, $enrol1, false));
166 $this->assertTrue($this->is_meta_enrolled($user5, $enrol1, false));
167
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);
172
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)));
176
177 $this->enable_plugin();
178
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)));
186
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)));
195
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)));
203
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)));
212
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));
219
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));
226
227
228 // Now try sync triggered by events.
229
230 set_config('syncall', 1, 'enrol_meta');
231
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));
242
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));
253
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));
264
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));
275
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));
282
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));
293
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));
304
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));
315
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));
321
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)));
328
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));
339
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));
350
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));
361
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));
372
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)));
379
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));
390
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));
401
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));
412
413
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)));
419
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)));
428
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)));
437
438 delete_course($course3, false);
439 delete_course($course4, false);
440
441 }
bb78e249 442
9e37d365
MG
443 public function test_add_to_group() {
444 global $CFG, $DB;
445
446 require_once($CFG->dirroot.'/group/lib.php');
447
448 $this->resetAfterTest(true);
449
450 $metalplugin = enrol_get_plugin('meta');
451
452 $user1 = $this->getDataGenerator()->create_user();
453 $user4 = $this->getDataGenerator()->create_user();
454
455 $course1 = $this->getDataGenerator()->create_course();
456 $course2 = $this->getDataGenerator()->create_course();
457 $course3 = $this->getDataGenerator()->create_course();
458 $manualenrol1 = $DB->get_record('enrol', array('courseid' => $course1->id, 'enrol' => 'manual'), '*', MUST_EXIST);
459 $manualenrol2 = $DB->get_record('enrol', array('courseid' => $course2->id, 'enrol' => 'manual'), '*', MUST_EXIST);
460
461 $student = $DB->get_record('role', array('shortname' => 'student'));
462 $teacher = $DB->get_record('role', array('shortname' => 'teacher'));
463
464 $id = groups_create_group((object)array('name' => 'Group 1 in course 3', 'courseid' => $course3->id));
465 $group31 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
466 $id = groups_create_group((object)array('name' => 'Group 2 in course 4', 'courseid' => $course3->id));
467 $group32 = $DB->get_record('groups', array('id' => $id), '*', MUST_EXIST);
468
469 $this->enable_plugin();
470
471 $e1 = $metalplugin->add_instance($course3, array('customint1' => $course1->id, 'customint2' => $group31->id));
472 $e2 = $metalplugin->add_instance($course3, array('customint1' => $course2->id, 'customint2' => $group32->id));
473
474 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id);
475 $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id);
476
477 $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id);
478
479 // Now make sure users are in the correct groups.
480 $this->assertTrue(groups_is_member($group31->id, $user1->id));
481 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user1->id,
482 'component' => 'enrol_meta', 'itemid' => $e1)));
483 $this->assertTrue(groups_is_member($group32->id, $user1->id));
484 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group32->id, 'userid' => $user1->id,
485 'component' => 'enrol_meta', 'itemid' => $e2)));
486
487 $this->assertTrue(groups_is_member($group31->id, $user4->id));
488 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user4->id,
489 'component' => 'enrol_meta', 'itemid' => $e1)));
490
491 // Make sure everything is the same after sync.
492 enrol_meta_sync(null, false);
493 $this->assertTrue(groups_is_member($group31->id, $user1->id));
494 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user1->id,
495 'component' => 'enrol_meta', 'itemid' => $e1)));
496 $this->assertTrue(groups_is_member($group32->id, $user1->id));
497 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group32->id, 'userid' => $user1->id,
498 'component' => 'enrol_meta', 'itemid' => $e2)));
499
500 $this->assertTrue(groups_is_member($group31->id, $user4->id));
501 $this->assertTrue($DB->record_exists('groups_members', array('groupid' => $group31->id, 'userid' => $user4->id,
502 'component' => 'enrol_meta', 'itemid' => $e1)));
503
504 set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta');
505
506 // When user 1 is unenrolled from course1, he is removed from group31 but still present in group32.
507 enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user1->id);
508 $this->assertFalse(groups_is_member($group31->id, $user1->id));
509 $this->assertTrue(groups_is_member($group32->id, $user1->id));
510 $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1, '', true)); // He still has active enrolment.
511 // And the same after sync.
512 enrol_meta_sync(null, false);
513 $this->assertFalse(groups_is_member($group31->id, $user1->id));
514 $this->assertTrue(groups_is_member($group32->id, $user1->id));
515 $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1, '', true));
516
517 // Unenroll user1 from course2 and make sure he is completely unenrolled from course3.
518 enrol_get_plugin('manual')->unenrol_user($manualenrol2, $user1->id);
519 $this->assertFalse(groups_is_member($group32->id, $user1->id));
520 $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user1));
521
522 set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta');
523
524 // When user is unenrolled in this case, he is still a member of a group (but enrolment is suspended).
525 enrol_get_plugin('manual')->unenrol_user($manualenrol1, $user4->id);
526 $this->assertTrue(groups_is_member($group31->id, $user4->id));
527 $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user4));
528 $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user4, '', true));
529 enrol_meta_sync(null, false);
530 $this->assertTrue(groups_is_member($group31->id, $user4->id));
531 $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user4));
532 $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user4, '', true));
533 }
534
bb78e249
RT
535 /**
536 * Test user_enrolment_created event.
537 */
bfce1e4a 538 public function test_user_enrolment_created_event() {
bb78e249
RT
539 global $DB;
540
541 $this->resetAfterTest();
542
543 $metaplugin = enrol_get_plugin('meta');
544 $user1 = $this->getDataGenerator()->create_user();
545 $course1 = $this->getDataGenerator()->create_course();
546 $course2 = $this->getDataGenerator()->create_course();
547 $student = $DB->get_record('role', array('shortname' => 'student'));
548
549 $e1 = $metaplugin->add_instance($course2, array('customint1' => $course1->id));
550 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
551
552 // Enrol user and capture event.
553 $sink = $this->redirectEvents();
554
555 $metaplugin->enrol_user($enrol1, $user1->id, $student->id);
556 $events = $sink->get_events();
557 $sink->close();
558 $event = array_shift($events);
559
560 // Test Event.
561 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
562 $this->assertInstanceOf('\core\event\user_enrolment_created', $event);
563 $this->assertEquals($dbuserenrolled->id, $event->objectid);
564 $this->assertEquals('user_enrolled', $event->get_legacy_eventname());
565 $expectedlegacyeventdata = $dbuserenrolled;
566 $expectedlegacyeventdata->enrol = 'meta';
567 $expectedlegacyeventdata->courseid = $course2->id;
568 $this->assertEventLegacyData($expectedlegacyeventdata, $event);
623a32e5 569 $this->assertEventContextNotUsed($event);
bb78e249
RT
570 }
571
572 /**
bfce1e4a 573 * Test user_enrolment_deleted event.
bb78e249 574 */
bfce1e4a 575 public function test_user_enrolment_deleted_event() {
bb78e249
RT
576 global $DB;
577
578 $this->resetAfterTest(true);
579
580 $metalplugin = enrol_get_plugin('meta');
581 $user1 = $this->getDataGenerator()->create_user();
582 $course1 = $this->getDataGenerator()->create_course();
583 $course2 = $this->getDataGenerator()->create_course();
584 $student = $DB->get_record('role', array('shortname'=>'student'));
585
586 $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
587 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
588
589 // Enrol user.
590 $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
591 $this->assertEquals(1, $DB->count_records('user_enrolments'));
592
593 // Unenrol user and capture event.
594 $sink = $this->redirectEvents();
595 $metalplugin->unenrol_user($enrol1, $user1->id);
596 $events = $sink->get_events();
597 $sink->close();
598 $event = array_pop($events);
599
600 $this->assertEquals(0, $DB->count_records('user_enrolments'));
601 $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event);
602 $this->assertEquals('user_unenrolled', $event->get_legacy_eventname());
623a32e5 603 $this->assertEventContextNotUsed($event);
bb78e249
RT
604 }
605
606 /**
607 * Test user_enrolment_updated event.
608 */
bfce1e4a 609 public function test_user_enrolment_updated_event() {
bb78e249
RT
610 global $DB;
611
612 $this->resetAfterTest(true);
613
614 $metalplugin = enrol_get_plugin('meta');
615 $user1 = $this->getDataGenerator()->create_user();
616 $course1 = $this->getDataGenerator()->create_course();
617 $course2 = $this->getDataGenerator()->create_course();
618 $student = $DB->get_record('role', array('shortname'=>'student'));
619
620 $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id));
621 $enrol1 = $DB->get_record('enrol', array('id' => $e1));
622
623 // Enrol user.
624 $metalplugin->enrol_user($enrol1, $user1->id, $student->id);
625 $this->assertEquals(1, $DB->count_records('user_enrolments'));
626
627 // Updated enrolment for user and capture event.
628 $sink = $this->redirectEvents();
629 $metalplugin->update_user_enrol($enrol1, $user1->id, ENROL_USER_SUSPENDED, null, time());
630 $events = $sink->get_events();
631 $sink->close();
632 $event = array_shift($events);
633
634 // Test Event.
635 $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id));
636 $this->assertInstanceOf('\core\event\user_enrolment_updated', $event);
637 $this->assertEquals($dbuserenrolled->id, $event->objectid);
638 $this->assertEquals('user_enrol_modified', $event->get_legacy_eventname());
639 $expectedlegacyeventdata = $dbuserenrolled;
640 $expectedlegacyeventdata->enrol = 'meta';
641 $expectedlegacyeventdata->courseid = $course2->id;
b63f7732
AA
642 $url = new \moodle_url('/enrol/editenrolment.php', array('ue' => $event->objectid));
643 $this->assertEquals($url, $event->get_url());
bb78e249 644 $this->assertEventLegacyData($expectedlegacyeventdata, $event);
623a32e5 645 $this->assertEventContextNotUsed($event);
bb78e249 646 }
1344bc78 647}