Merge branch 'wip-MDL-41231-master' of git://github.com/abgreeve/moodle
[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
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));
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 }
442}