Commit | Line | Data |
---|---|---|
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 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
28 | global $CFG; | |
29 | ||
30 | class 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 | ||
98177a45 DNA |
103 | $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id)); |
104 | $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST); | |
105 | $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id)); | |
106 | $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST); | |
107 | $id = groups_create_group((object)array('name'=>'Group 3', 'courseid'=>$course2->id)); | |
108 | $group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST); | |
109 | ||
1344bc78 PS |
110 | $this->disable_plugin(); |
111 | ||
112 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); | |
113 | $this->getDataGenerator()->enrol_user($user2->id, $course1->id, $student->id); | |
114 | $this->getDataGenerator()->enrol_user($user3->id, $course1->id, 0); | |
115 | $this->getDataGenerator()->enrol_user($user4->id, $course1->id, $teacher->id); | |
116 | $this->getDataGenerator()->enrol_user($user5->id, $course1->id, $manager->id); | |
117 | ||
118 | $this->getDataGenerator()->enrol_user($user1->id, $course2->id, $student->id); | |
119 | $this->getDataGenerator()->enrol_user($user2->id, $course2->id, $teacher->id); | |
120 | ||
121 | $this->assertEquals(7, $DB->count_records('user_enrolments')); | |
122 | $this->assertEquals(6, $DB->count_records('role_assignments')); | |
123 | ||
124 | set_config('syncall', 0, 'enrol_meta'); | |
125 | set_config('nosyncroleids', $manager->id, 'enrol_meta'); | |
126 | ||
127 | require_once($CFG->dirroot.'/enrol/meta/locallib.php'); | |
128 | ||
129 | enrol_meta_sync(null, false); | |
130 | $this->assertEquals(7, $DB->count_records('user_enrolments')); | |
131 | $this->assertEquals(6, $DB->count_records('role_assignments')); | |
132 | ||
133 | $this->enable_plugin(); | |
134 | enrol_meta_sync(null, false); | |
135 | $this->assertEquals(7, $DB->count_records('user_enrolments')); | |
136 | $this->assertEquals(6, $DB->count_records('role_assignments')); | |
137 | ||
98177a45 DNA |
138 | $e1 = $metalplugin->add_instance($course3, array('customint1'=>$course1->id, 'customint2'=>$group1->id)); |
139 | $e2 = $metalplugin->add_instance($course3, array('customint1'=>$course2->id, 'customint2'=>$group2->id)); | |
1344bc78 | 140 | $e3 = $metalplugin->add_instance($course4, array('customint1'=>$course2->id)); |
98177a45 | 141 | $e4 = $metalplugin->add_instance($course4, array('customint1'=>$course2->id, 'customint2'=>$group3->id)); |
1344bc78 PS |
142 | $enrol1 = $DB->get_record('enrol', array('id'=>$e1)); |
143 | $enrol2 = $DB->get_record('enrol', array('id'=>$e2)); | |
144 | $enrol3 = $DB->get_record('enrol', array('id'=>$e3)); | |
98177a45 | 145 | $enrol4 = $DB->get_record('enrol', array('id'=>$e3)); |
1344bc78 PS |
146 | |
147 | enrol_meta_sync($course4->id, false); | |
148 | $this->assertEquals(9, $DB->count_records('user_enrolments')); | |
149 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
150 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student)); | |
151 | $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher)); | |
152 | ||
153 | enrol_meta_sync(null, false); | |
154 | $this->assertEquals(14, $DB->count_records('user_enrolments')); | |
155 | $this->assertEquals(13, $DB->count_records('role_assignments')); | |
156 | ||
157 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
158 | $this->assertTrue($this->is_meta_enrolled($user2, $enrol1, $student)); | |
159 | $this->assertFalse($this->is_meta_enrolled($user3, $enrol1)); | |
160 | $this->assertTrue($this->is_meta_enrolled($user4, $enrol1, $teacher)); | |
161 | $this->assertFalse($this->is_meta_enrolled($user5, $enrol1)); | |
162 | ||
163 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student)); | |
164 | $this->assertTrue($this->is_meta_enrolled($user2, $enrol2, $teacher)); | |
165 | ||
166 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student)); | |
167 | $this->assertTrue($this->is_meta_enrolled($user2, $enrol3, $teacher)); | |
168 | ||
169 | set_config('syncall', 1, 'enrol_meta'); | |
170 | enrol_meta_sync(null, false); | |
171 | $this->assertEquals(16, $DB->count_records('user_enrolments')); | |
172 | $this->assertEquals(13, $DB->count_records('role_assignments')); | |
173 | ||
174 | $this->assertTrue($this->is_meta_enrolled($user3, $enrol1, false)); | |
175 | $this->assertTrue($this->is_meta_enrolled($user5, $enrol1, false)); | |
176 | ||
177 | $this->assertEquals(16, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
178 | $this->disable_plugin(); | |
179 | $manplugin->unenrol_user($manual1, $user1->id); | |
180 | $manplugin->unenrol_user($manual2, $user1->id); | |
181 | ||
182 | $this->assertEquals(14, $DB->count_records('user_enrolments')); | |
183 | $this->assertEquals(11, $DB->count_records('role_assignments')); | |
184 | $this->assertEquals(14, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
185 | ||
186 | $this->enable_plugin(); | |
187 | ||
98177a45 DNA |
188 | $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4)); |
189 | $this->assertTrue(groups_add_member($group1, $user4)); | |
190 | $this->assertTrue(groups_add_member($group2, $user4)); | |
191 | ||
192 | $this->assertFalse(groups_is_member($group1->id, $user1->id)); | |
193 | groups_add_member($group1->id, $user1->id); | |
194 | $this->assertTrue(groups_is_member($group1->id, $user1->id)); | |
195 | $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); | |
196 | ||
197 | groups_add_member($group1->id, $user4->id); | |
198 | $this->assertTrue(groups_is_member($group1->id, $user4->id)); | |
199 | $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id))); | |
200 | ||
201 | set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta'); | |
202 | ||
203 | groups_remove_member($group1->id, $user1->id); | |
204 | $this->assertFalse(groups_is_member($group1->id, $user1->id)); | |
205 | ||
206 | groups_remove_member($group1->id, $user4->id); | |
207 | $this->assertTrue(groups_is_member($group1->id, $user4->id)); | |
208 | $this->assertTrue(groups_is_member($group2->id, $user4->id)); | |
209 | ||
210 | set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta'); | |
211 | groups_add_member($group1->id, $user1->id); | |
212 | ||
213 | groups_remove_member($group1->id, $user1->id); | |
214 | $this->assertTrue(groups_is_member($group1->id, $user1->id)); | |
215 | ||
1344bc78 PS |
216 | set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta'); |
217 | enrol_meta_sync($course4->id, false); | |
218 | $this->assertEquals(14, $DB->count_records('user_enrolments')); | |
219 | $this->assertEquals(11, $DB->count_records('role_assignments')); | |
220 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
221 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, $student)); | |
222 | $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol3->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id))); | |
223 | ||
224 | enrol_meta_sync(null, false); | |
225 | $this->assertEquals(14, $DB->count_records('user_enrolments')); | |
226 | $this->assertEquals(11, $DB->count_records('role_assignments')); | |
227 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
228 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
229 | $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol1->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id))); | |
230 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, $student)); | |
231 | $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol2->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id))); | |
232 | ||
233 | set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta'); | |
234 | enrol_meta_sync($course4->id, false); | |
235 | $this->assertEquals(14, $DB->count_records('user_enrolments')); | |
236 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
237 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
238 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol3, false)); | |
239 | $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol3->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id))); | |
240 | ||
241 | enrol_meta_sync(null, false); | |
242 | $this->assertEquals(14, $DB->count_records('user_enrolments')); | |
243 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
244 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
245 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); | |
246 | $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol1->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id))); | |
247 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol2, false)); | |
248 | $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$enrol2->id, 'status'=>ENROL_USER_SUSPENDED, 'userid'=>$user1->id))); | |
249 | ||
250 | set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta'); | |
251 | enrol_meta_sync($course4->id, false); | |
252 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
253 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
254 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
255 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol3)); | |
256 | ||
257 | enrol_meta_sync(null, false); | |
258 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
259 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
260 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
261 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
262 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol2)); | |
263 | ||
264 | ||
265 | // Now try sync triggered by events. | |
266 | ||
267 | set_config('syncall', 1, 'enrol_meta'); | |
268 | ||
269 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); | |
270 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
271 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
272 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
273 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
274 | enrol_meta_sync(null, false); | |
275 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
276 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
277 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
278 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
279 | ||
280 | $manplugin->unenrol_user($manual1, $user1->id); | |
281 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
282 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
283 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
284 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
285 | enrol_meta_sync(null, false); | |
286 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
287 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
288 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
289 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
290 | ||
291 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0); | |
292 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
293 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
294 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
295 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); | |
296 | enrol_meta_sync(null, false); | |
297 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
298 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
299 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
300 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); | |
301 | ||
302 | $manplugin->unenrol_user($manual1, $user1->id); | |
303 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
304 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
305 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
306 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
307 | enrol_meta_sync(null, false); | |
308 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
309 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
310 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
311 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
312 | ||
313 | set_config('syncall', 0, 'enrol_meta'); | |
314 | enrol_meta_sync(null, false); | |
315 | $this->assertEquals(9, $DB->count_records('user_enrolments')); | |
316 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
317 | $this->assertEquals(9, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
318 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
319 | ||
320 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 0); | |
321 | $this->assertEquals(10, $DB->count_records('user_enrolments')); | |
322 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
323 | $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
324 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); | |
325 | enrol_meta_sync(null, false); | |
326 | $this->assertEquals(10, $DB->count_records('user_enrolments')); | |
327 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
328 | $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
329 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); | |
330 | ||
331 | role_assign($teacher->id, $user1->id, context_course::instance($course1->id)->id); | |
332 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
333 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
334 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
335 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher)); | |
336 | enrol_meta_sync(null, false); | |
337 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
338 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
339 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
340 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $teacher)); | |
341 | ||
342 | role_unassign($teacher->id, $user1->id, context_course::instance($course1->id)->id); | |
343 | $this->assertEquals(10, $DB->count_records('user_enrolments')); | |
344 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
345 | $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
346 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); | |
347 | enrol_meta_sync(null, false); | |
348 | $this->assertEquals(10, $DB->count_records('user_enrolments')); | |
349 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
350 | $this->assertEquals(10, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
351 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1, $student)); | |
352 | ||
353 | $manplugin->unenrol_user($manual1, $user1->id); | |
354 | $this->assertEquals(9, $DB->count_records('user_enrolments')); | |
355 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
356 | $this->assertEquals(9, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
357 | $this->assertFalse($this->is_meta_enrolled($user1, $enrol1)); | |
358 | ||
359 | set_config('syncall', 1, 'enrol_meta'); | |
360 | set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND, 'enrol_meta'); | |
361 | enrol_meta_sync(null, false); | |
362 | $this->assertEquals(11, $DB->count_records('user_enrolments')); | |
363 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
364 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
365 | ||
366 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); | |
367 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
368 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
369 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
370 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
371 | enrol_meta_sync(null, false); | |
372 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
373 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
374 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
375 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
376 | ||
377 | $manplugin->update_user_enrol($manual1, $user1->id, ENROL_USER_SUSPENDED); | |
378 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
379 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
380 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
381 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
382 | enrol_meta_sync(null, false); | |
383 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
384 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
385 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
386 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
387 | ||
388 | $manplugin->unenrol_user($manual1, $user1->id); | |
389 | $this->assertEquals(12, $DB->count_records('user_enrolments')); | |
390 | $this->assertEquals(9, $DB->count_records('role_assignments')); | |
391 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
392 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
393 | enrol_meta_sync(null, false); | |
394 | $this->assertEquals(12, $DB->count_records('user_enrolments')); | |
395 | $this->assertEquals(9, $DB->count_records('role_assignments')); | |
396 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
397 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
398 | ||
399 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); | |
400 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
401 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
402 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
403 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
404 | enrol_meta_sync(null, false); | |
405 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
406 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
407 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
408 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
409 | ||
410 | set_config('syncall', 1, 'enrol_meta'); | |
411 | set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES, 'enrol_meta'); | |
412 | enrol_meta_sync(null, false); | |
413 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
414 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
415 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
416 | ||
417 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); | |
418 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
419 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
420 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
421 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
422 | enrol_meta_sync(null, false); | |
423 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
424 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
425 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
426 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
427 | ||
428 | $manplugin->unenrol_user($manual1, $user1->id); | |
429 | $this->assertEquals(12, $DB->count_records('user_enrolments')); | |
430 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
431 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
432 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); | |
433 | enrol_meta_sync(null, false); | |
434 | $this->assertEquals(12, $DB->count_records('user_enrolments')); | |
435 | $this->assertEquals(8, $DB->count_records('role_assignments')); | |
436 | $this->assertEquals(11, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
437 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, false)); | |
438 | ||
439 | $this->getDataGenerator()->enrol_user($user1->id, $course1->id, $student->id); | |
440 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
441 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
442 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
443 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
444 | enrol_meta_sync(null, false); | |
445 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
446 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
447 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
448 | $this->assertTrue($this->is_meta_enrolled($user1, $enrol1, $student)); | |
449 | ||
450 | ||
451 | set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL, 'enrol_meta'); | |
452 | enrol_meta_sync(null, false); | |
453 | $this->assertEquals(13, $DB->count_records('user_enrolments')); | |
454 | $this->assertEquals(10, $DB->count_records('role_assignments')); | |
455 | $this->assertEquals(13, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
456 | ||
457 | delete_course($course1, false); | |
458 | $this->assertEquals(3, $DB->count_records('user_enrolments')); | |
459 | $this->assertEquals(3, $DB->count_records('role_assignments')); | |
460 | $this->assertEquals(3, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
461 | enrol_meta_sync(null, false); | |
462 | $this->assertEquals(3, $DB->count_records('user_enrolments')); | |
463 | $this->assertEquals(3, $DB->count_records('role_assignments')); | |
464 | $this->assertEquals(3, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
465 | ||
466 | delete_course($course2, false); | |
467 | $this->assertEquals(0, $DB->count_records('user_enrolments')); | |
468 | $this->assertEquals(0, $DB->count_records('role_assignments')); | |
469 | $this->assertEquals(0, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
470 | enrol_meta_sync(null, false); | |
471 | $this->assertEquals(0, $DB->count_records('user_enrolments')); | |
472 | $this->assertEquals(0, $DB->count_records('role_assignments')); | |
473 | $this->assertEquals(0, $DB->count_records('user_enrolments', array('status'=>ENROL_USER_ACTIVE))); | |
474 | ||
475 | delete_course($course3, false); | |
476 | delete_course($course4, false); | |
477 | ||
478 | } | |
bb78e249 RT |
479 | |
480 | /** | |
481 | * Test user_enrolment_created event. | |
482 | */ | |
bfce1e4a | 483 | public function test_user_enrolment_created_event() { |
bb78e249 RT |
484 | global $DB; |
485 | ||
486 | $this->resetAfterTest(); | |
487 | ||
488 | $metaplugin = enrol_get_plugin('meta'); | |
489 | $user1 = $this->getDataGenerator()->create_user(); | |
490 | $course1 = $this->getDataGenerator()->create_course(); | |
491 | $course2 = $this->getDataGenerator()->create_course(); | |
492 | $student = $DB->get_record('role', array('shortname' => 'student')); | |
493 | ||
494 | $e1 = $metaplugin->add_instance($course2, array('customint1' => $course1->id)); | |
495 | $enrol1 = $DB->get_record('enrol', array('id' => $e1)); | |
496 | ||
497 | // Enrol user and capture event. | |
498 | $sink = $this->redirectEvents(); | |
499 | ||
500 | $metaplugin->enrol_user($enrol1, $user1->id, $student->id); | |
501 | $events = $sink->get_events(); | |
502 | $sink->close(); | |
503 | $event = array_shift($events); | |
504 | ||
505 | // Test Event. | |
506 | $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id)); | |
507 | $this->assertInstanceOf('\core\event\user_enrolment_created', $event); | |
508 | $this->assertEquals($dbuserenrolled->id, $event->objectid); | |
509 | $this->assertEquals('user_enrolled', $event->get_legacy_eventname()); | |
510 | $expectedlegacyeventdata = $dbuserenrolled; | |
511 | $expectedlegacyeventdata->enrol = 'meta'; | |
512 | $expectedlegacyeventdata->courseid = $course2->id; | |
513 | $this->assertEventLegacyData($expectedlegacyeventdata, $event); | |
623a32e5 | 514 | $this->assertEventContextNotUsed($event); |
bb78e249 RT |
515 | } |
516 | ||
517 | /** | |
bfce1e4a | 518 | * Test user_enrolment_deleted event. |
bb78e249 | 519 | */ |
bfce1e4a | 520 | public function test_user_enrolment_deleted_event() { |
bb78e249 RT |
521 | global $DB; |
522 | ||
523 | $this->resetAfterTest(true); | |
524 | ||
525 | $metalplugin = enrol_get_plugin('meta'); | |
526 | $user1 = $this->getDataGenerator()->create_user(); | |
527 | $course1 = $this->getDataGenerator()->create_course(); | |
528 | $course2 = $this->getDataGenerator()->create_course(); | |
529 | $student = $DB->get_record('role', array('shortname'=>'student')); | |
530 | ||
531 | $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id)); | |
532 | $enrol1 = $DB->get_record('enrol', array('id' => $e1)); | |
533 | ||
534 | // Enrol user. | |
535 | $metalplugin->enrol_user($enrol1, $user1->id, $student->id); | |
536 | $this->assertEquals(1, $DB->count_records('user_enrolments')); | |
537 | ||
538 | // Unenrol user and capture event. | |
539 | $sink = $this->redirectEvents(); | |
540 | $metalplugin->unenrol_user($enrol1, $user1->id); | |
541 | $events = $sink->get_events(); | |
542 | $sink->close(); | |
543 | $event = array_pop($events); | |
544 | ||
545 | $this->assertEquals(0, $DB->count_records('user_enrolments')); | |
546 | $this->assertInstanceOf('\core\event\user_enrolment_deleted', $event); | |
547 | $this->assertEquals('user_unenrolled', $event->get_legacy_eventname()); | |
623a32e5 | 548 | $this->assertEventContextNotUsed($event); |
bb78e249 RT |
549 | } |
550 | ||
551 | /** | |
552 | * Test user_enrolment_updated event. | |
553 | */ | |
bfce1e4a | 554 | public function test_user_enrolment_updated_event() { |
bb78e249 RT |
555 | global $DB; |
556 | ||
557 | $this->resetAfterTest(true); | |
558 | ||
559 | $metalplugin = enrol_get_plugin('meta'); | |
560 | $user1 = $this->getDataGenerator()->create_user(); | |
561 | $course1 = $this->getDataGenerator()->create_course(); | |
562 | $course2 = $this->getDataGenerator()->create_course(); | |
563 | $student = $DB->get_record('role', array('shortname'=>'student')); | |
564 | ||
565 | $e1 = $metalplugin->add_instance($course2, array('customint1' => $course1->id)); | |
566 | $enrol1 = $DB->get_record('enrol', array('id' => $e1)); | |
567 | ||
568 | // Enrol user. | |
569 | $metalplugin->enrol_user($enrol1, $user1->id, $student->id); | |
570 | $this->assertEquals(1, $DB->count_records('user_enrolments')); | |
571 | ||
572 | // Updated enrolment for user and capture event. | |
573 | $sink = $this->redirectEvents(); | |
574 | $metalplugin->update_user_enrol($enrol1, $user1->id, ENROL_USER_SUSPENDED, null, time()); | |
575 | $events = $sink->get_events(); | |
576 | $sink->close(); | |
577 | $event = array_shift($events); | |
578 | ||
579 | // Test Event. | |
580 | $dbuserenrolled = $DB->get_record('user_enrolments', array('userid' => $user1->id)); | |
581 | $this->assertInstanceOf('\core\event\user_enrolment_updated', $event); | |
582 | $this->assertEquals($dbuserenrolled->id, $event->objectid); | |
583 | $this->assertEquals('user_enrol_modified', $event->get_legacy_eventname()); | |
584 | $expectedlegacyeventdata = $dbuserenrolled; | |
585 | $expectedlegacyeventdata->enrol = 'meta'; | |
586 | $expectedlegacyeventdata->courseid = $course2->id; | |
b63f7732 AA |
587 | $url = new \moodle_url('/enrol/editenrolment.php', array('ue' => $event->objectid)); |
588 | $this->assertEquals($url, $event->get_url()); | |
bb78e249 | 589 | $this->assertEventLegacyData($expectedlegacyeventdata, $event); |
623a32e5 | 590 | $this->assertEventContextNotUsed($event); |
bb78e249 | 591 | } |
1344bc78 | 592 | } |