weekly on-sync release 2.5dev
[moodle.git] / enrol / cohort / tests / sync_test.php
CommitLineData
08403f9e
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 * Cohort enrolment sync functional test.
19 *
20 * @package enrol_cohort
21 * @category phpunit
22 * @copyright 2012 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;
29require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
30require_once($CFG->dirroot.'/cohort/lib.php');
7881024e 31require_once($CFG->dirroot.'/group/lib.php');
08403f9e
PS
32
33class enrol_cohort_testcase extends advanced_testcase {
34
35 protected function enable_plugin() {
36 $enabled = enrol_get_plugins(true);
37 $enabled['cohort'] = true;
38 $enabled = array_keys($enabled);
39 set_config('enrol_plugins_enabled', implode(',', $enabled));
40 }
41
42 protected function disable_plugin() {
43 $enabled = enrol_get_plugins(true);
44 unset($enabled['cohort']);
45 $enabled = array_keys($enabled);
46 set_config('enrol_plugins_enabled', implode(',', $enabled));
47 }
48
49 public function test_handler_sync() {
50 global $DB;
51
52 $this->resetAfterTest();
53
54 // Setup a few courses and categories.
55
56 $cohortplugin = enrol_get_plugin('cohort');
57 $manualplugin = enrol_get_plugin('manual');
58
59 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
60 $this->assertNotEmpty($studentrole);
61 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
62 $this->assertNotEmpty($teacherrole);
63 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
64 $this->assertNotEmpty($managerrole);
65
66 $cat1 = $this->getDataGenerator()->create_category();
67 $cat2 = $this->getDataGenerator()->create_category();
68
69 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
70 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
71 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
72 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
73 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
74
75 $user1 = $this->getDataGenerator()->create_user();
76 $user2 = $this->getDataGenerator()->create_user();
77 $user3 = $this->getDataGenerator()->create_user();
78 $user4 = $this->getDataGenerator()->create_user();
79
80 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
81 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
82 $cohort3 = $this->getDataGenerator()->create_cohort();
83
84 $this->enable_plugin();
85
86 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
87 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
88
89 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
90 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
91
92 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
93 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
94
95 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
96 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
97
98 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
99 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
100
101
102 // Test cohort member add event.
103
104 cohort_add_member($cohort1->id, $user1->id);
105 cohort_add_member($cohort1->id, $user2->id);
106 cohort_add_member($cohort1->id, $user4->id);
107 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
108 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
109 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
110 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
111 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
112 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
113 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
114 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
115
116 cohort_add_member($cohort2->id, $user3->id);
117 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
118 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
119 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
120 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
121 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
122 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course2->id)->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
123
124 cohort_add_member($cohort3->id, $user3->id);
125 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
126 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
127
128 // Test cohort remove action.
129
130 $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction'));
131 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
132
133 cohort_remove_member($cohort1->id, $user2->id);
134 cohort_remove_member($cohort1->id, $user4->id);
135 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
136 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
137 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
138 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
139
140 cohort_add_member($cohort1->id, $user2->id);
141 cohort_add_member($cohort1->id, $user4->id);
142 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
143 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
144 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
145 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
146
147 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
148 cohort_remove_member($cohort1->id, $user2->id);
149 cohort_remove_member($cohort1->id, $user4->id);
150 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
151 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
152 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
153 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
154 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
155 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
156
157 cohort_remove_member($cohort2->id, $user3->id);
158 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
159 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
160 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
161 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
162 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
163 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course2->id)->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
164
165
166 // Test cohort deleting.
167
168 cohort_add_member($cohort1->id, $user2->id);
169 cohort_add_member($cohort1->id, $user4->id);
170 cohort_add_member($cohort2->id, $user3->id);
171 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
172 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
173
174 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
175 cohort_delete_cohort($cohort2);
176 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
177 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
178
179 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST);
180 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST);
181
182 $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status);
183 $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status);
184 $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
185 $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
186
187 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
188 cohort_delete_cohort($cohort1);
189 $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
190 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
191 $this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id)));
192 $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
193
7881024e
PS
194
195 // Test group sync.
196
197 $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
198 $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
199 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
200 $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
201
202 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
203 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
204 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
205
206 $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
207 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
208
209 $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
210 $this->assertTrue(groups_add_member($group1, $user4));
211 $this->assertTrue(groups_add_member($group2, $user4));
212
213 $this->assertFalse(groups_is_member($group1->id, $user1->id));
214 cohort_add_member($cohort1->id, $user1->id);
215 $this->assertTrue(groups_is_member($group1->id, $user1->id));
216 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
217
218 cohort_add_member($cohort1->id, $user4->id);
219 $this->assertTrue(groups_is_member($group1->id, $user4->id));
220 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
221
222 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
223
224 cohort_remove_member($cohort1->id, $user1->id);
225 $this->assertFalse(groups_is_member($group1->id, $user1->id));
226
227 cohort_remove_member($cohort1->id, $user4->id);
228 $this->assertTrue(groups_is_member($group1->id, $user4->id));
229 $this->assertTrue(groups_is_member($group2->id, $user4->id));
230
231 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
232 cohort_add_member($cohort1->id, $user1->id);
233
234 cohort_remove_member($cohort1->id, $user1->id);
235 $this->assertTrue(groups_is_member($group1->id, $user1->id));
125e96f3
PS
236
237
238 // Test deleting of instances.
239
240 cohort_add_member($cohort1->id, $user1->id);
241 cohort_add_member($cohort1->id, $user2->id);
242 cohort_add_member($cohort1->id, $user3->id);
243
244 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
245 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
246 $this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
247 $this->assertEquals(5, $DB->count_records('groups_members', array()));
248 $this->assertEquals(3, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
249
250 $cohortplugin->delete_instance($cohortinstance1);
251
252 $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
253 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
254 $this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
255 $this->assertEquals(2, $DB->count_records('groups_members', array()));
256 $this->assertEquals(0, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
08403f9e
PS
257 }
258
259 public function test_sync_course() {
260 global $DB;
261 $this->resetAfterTest();
262
263 // Setup a few courses and categories.
264
265 $cohortplugin = enrol_get_plugin('cohort');
266 $manualplugin = enrol_get_plugin('manual');
267
268 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
269 $this->assertNotEmpty($studentrole);
270 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
271 $this->assertNotEmpty($teacherrole);
272 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
273 $this->assertNotEmpty($managerrole);
274
275 $cat1 = $this->getDataGenerator()->create_category();
276 $cat2 = $this->getDataGenerator()->create_category();
277
278 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
279 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
280 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
281 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
282 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
283
284 $user1 = $this->getDataGenerator()->create_user();
285 $user2 = $this->getDataGenerator()->create_user();
286 $user3 = $this->getDataGenerator()->create_user();
287 $user4 = $this->getDataGenerator()->create_user();
288
289 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
290 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
291 $cohort3 = $this->getDataGenerator()->create_cohort();
292
293 $this->disable_plugin(); // Prevents event sync.
294
295 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
296 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
297
298 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
299 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
300
301 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
302 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
303
304 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
305 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
306
307 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
308 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
309
310 cohort_add_member($cohort1->id, $user1->id);
311 cohort_add_member($cohort1->id, $user2->id);
312 cohort_add_member($cohort1->id, $user4->id);
313 cohort_add_member($cohort2->id, $user3->id);
314 cohort_add_member($cohort3->id, $user3->id);
315
316 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
317 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
318
319
320 // Test sync of one course only.
321
322 enrol_cohort_sync($course1->id, false);
323 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
324 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
325
326
327 $this->enable_plugin();
328 enrol_cohort_sync($course2->id, false);
329 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
330 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
331 $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events!
332 $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events!
333
334 enrol_cohort_sync($course1->id, false);
335 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
336 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
337 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
338 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
339 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
340 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
341 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
342 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
343 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
344 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
345
346 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
347 $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
348 enrol_cohort_sync($course1->id, false);
349 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
350 $this->assertEquals(6, $DB->count_records('role_assignments', array()));
351 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
352
353 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
354 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
355 enrol_cohort_sync($course1->id, false);
356 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
357 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
358 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
359 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
360 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
361 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
362
363 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
364 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
365 $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
366 enrol_cohort_sync($course1->id, false);
367 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
368 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
369
370 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
371 enrol_cohort_sync($course1->id, false);
372 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
373 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
7881024e
PS
374
375
376 // Test group sync.
377
378 $this->disable_plugin(); // No event sync
379
380 $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
381 $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
382 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
383 $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
384
385 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
386 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
387 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
388
389 $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
390 $this->assertTrue(groups_add_member($group1, $user4));
391 $this->assertTrue(groups_add_member($group2, $user4));
392
393 $this->enable_plugin(); // No event sync
394
395 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
396 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
397
398 $this->assertFalse(groups_is_member($group1->id, $user1->id));
399 cohort_add_member($cohort1->id, $user1->id);
400 cohort_add_member($cohort1->id, $user4->id);
401 cohort_add_member($cohort2->id, $user4->id);
402
403 enrol_cohort_sync($course1->id, false);
404
405 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
406 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
407
408 $this->assertTrue(groups_is_member($group1->id, $user1->id));
409 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
410
411 $this->assertTrue(groups_is_member($group1->id, $user4->id));
412 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
413
414 $cohortinstance1->customint2 = $group2->id;
415 $DB->update_record('enrol', $cohortinstance1);
416
417 enrol_cohort_sync($course1->id, false);
418 $this->assertFalse(groups_is_member($group1->id, $user1->id));
419 $this->assertTrue(groups_is_member($group2->id, $user1->id));
420 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
421
422 $this->assertTrue(groups_is_member($group1->id, $user4->id));
423 $this->assertTrue(groups_is_member($group2->id, $user4->id));
424 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
425 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
426
427 cohort_remove_member($cohort1->id, $user1->id);
428 $this->assertFalse(groups_is_member($group1->id, $user1->id));
429
430 cohort_remove_member($cohort1->id, $user4->id);
431 $this->assertTrue(groups_is_member($group1->id, $user4->id));
432 $this->assertTrue(groups_is_member($group2->id, $user4->id));
08403f9e
PS
433 }
434
435 public function test_sync_all_courses() {
436 global $DB;
437
438 $this->resetAfterTest();
439
440 // Setup a few courses and categories.
441
442 $cohortplugin = enrol_get_plugin('cohort');
443 $manualplugin = enrol_get_plugin('manual');
444
445 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
446 $this->assertNotEmpty($studentrole);
447 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
448 $this->assertNotEmpty($teacherrole);
449 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
450 $this->assertNotEmpty($managerrole);
451
452 $cat1 = $this->getDataGenerator()->create_category();
453 $cat2 = $this->getDataGenerator()->create_category();
454
455 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
456 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
457 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
458 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
459 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
460
461 $user1 = $this->getDataGenerator()->create_user();
462 $user2 = $this->getDataGenerator()->create_user();
463 $user3 = $this->getDataGenerator()->create_user();
464 $user4 = $this->getDataGenerator()->create_user();
465
466 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
467 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
468 $cohort3 = $this->getDataGenerator()->create_cohort();
469
470 $this->disable_plugin(); // Prevents event sync.
471
472 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
473 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
474
475 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
476 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
477
478 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
479 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
480
481 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
482 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
483
484 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
485 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
486
487 cohort_add_member($cohort1->id, $user1->id);
488 cohort_add_member($cohort1->id, $user2->id);
489 cohort_add_member($cohort1->id, $user4->id);
490 cohort_add_member($cohort2->id, $user3->id);
491 cohort_add_member($cohort3->id, $user3->id);
492
493 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
494 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
495
496
497 // Test sync of one course only.
498
499 enrol_cohort_sync(null, false);
500 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
501 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
502
503
504 $this->enable_plugin();
505 enrol_cohort_sync(null, false);
506 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
507 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
508 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
509 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
510 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
511 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
512 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
513 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
514 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
515 $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
516
517 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
518 $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
519 enrol_cohort_sync($course1->id, false);
520 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
521 $this->assertEquals(6, $DB->count_records('role_assignments', array()));
522 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
523
524 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
525 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
526 enrol_cohort_sync($course1->id, false);
527 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
528 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
529 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
530 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
531 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
532 $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>context_course::instance($course1->id)->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
533
534 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
535 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
536 $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
537 enrol_cohort_sync($course1->id, false);
538 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
539 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
540
541 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
542 enrol_cohort_sync($course1->id, false);
543 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
544 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
545
7881024e
PS
546
547 // Test group sync.
548
549 $this->disable_plugin(); // No event sync
550
551 $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
552 $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
553 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
554 $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
555 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course2->id));
556 $group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
557
558 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
559 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
560 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
561
562 $this->assertTrue(groups_add_member($group1, $user4));
563 $this->assertTrue(groups_add_member($group2, $user4));
564
565 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
566 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
567
568 $this->assertFalse(groups_is_member($group1->id, $user1->id));
569 cohort_add_member($cohort1->id, $user1->id);
570 cohort_add_member($cohort1->id, $user4->id);
571 cohort_add_member($cohort2->id, $user4->id);
572 cohort_add_member($cohort2->id, $user3->id);
573
574 $this->enable_plugin();
575
576 enrol_cohort_sync(null, false);
577
578 $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
579 $this->assertEquals(8, $DB->count_records('role_assignments', array()));
580
581 $this->assertTrue(groups_is_member($group1->id, $user1->id));
582 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
583
584 $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
585 $this->assertTrue(groups_is_member($group1->id, $user4->id));
586 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
587
588 $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3));
589 $this->assertFalse(groups_is_member($group3->id, $user3->id));
590
591 $cohortinstance1->customint2 = $group2->id;
592 $DB->update_record('enrol', $cohortinstance1);
593 $cohortinstance3->customint2 = $group3->id;
594 $DB->update_record('enrol', $cohortinstance3);
595
596 enrol_cohort_sync(null, false);
597 $this->assertFalse(groups_is_member($group1->id, $user1->id));
598 $this->assertTrue(groups_is_member($group2->id, $user1->id));
599 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
600
601 $this->assertTrue(groups_is_member($group1->id, $user4->id));
602 $this->assertTrue(groups_is_member($group2->id, $user4->id));
603 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
604 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
605
606 $this->assertTrue(groups_is_member($group3->id, $user3->id));
607 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group3->id, 'userid'=>$user3->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
608
609 cohort_remove_member($cohort1->id, $user1->id);
610 $this->assertFalse(groups_is_member($group1->id, $user1->id));
611
612 cohort_remove_member($cohort1->id, $user4->id);
613 $this->assertTrue(groups_is_member($group1->id, $user4->id));
614 $this->assertTrue(groups_is_member($group2->id, $user4->id));
08403f9e
PS
615 }
616}