MDL-35839 fix invalid enrol instance delete cleanup
[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));
08403f9e
PS
236 }
237
238 public function test_sync_course() {
239 global $DB;
240 $this->resetAfterTest();
241
242 // Setup a few courses and categories.
243
244 $cohortplugin = enrol_get_plugin('cohort');
245 $manualplugin = enrol_get_plugin('manual');
246
247 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
248 $this->assertNotEmpty($studentrole);
249 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
250 $this->assertNotEmpty($teacherrole);
251 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
252 $this->assertNotEmpty($managerrole);
253
254 $cat1 = $this->getDataGenerator()->create_category();
255 $cat2 = $this->getDataGenerator()->create_category();
256
257 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
258 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
259 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
260 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
261 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
262
263 $user1 = $this->getDataGenerator()->create_user();
264 $user2 = $this->getDataGenerator()->create_user();
265 $user3 = $this->getDataGenerator()->create_user();
266 $user4 = $this->getDataGenerator()->create_user();
267
268 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
269 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
270 $cohort3 = $this->getDataGenerator()->create_cohort();
271
272 $this->disable_plugin(); // Prevents event sync.
273
274 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
275 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
276
277 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
278 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
279
280 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
281 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
282
283 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
284 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
285
286 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
287 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
288
289 cohort_add_member($cohort1->id, $user1->id);
290 cohort_add_member($cohort1->id, $user2->id);
291 cohort_add_member($cohort1->id, $user4->id);
292 cohort_add_member($cohort2->id, $user3->id);
293 cohort_add_member($cohort3->id, $user3->id);
294
295 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
296 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
297
298
299 // Test sync of one course only.
300
301 enrol_cohort_sync($course1->id, false);
302 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
303 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
304
305
306 $this->enable_plugin();
307 enrol_cohort_sync($course2->id, false);
308 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
309 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
310 $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events!
311 $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events!
312
313 enrol_cohort_sync($course1->id, false);
314 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
315 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
316 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
317 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
318 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
319 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
320 $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)));
321 $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)));
322 $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)));
323 $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)));
324
325 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
326 $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
327 enrol_cohort_sync($course1->id, false);
328 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
329 $this->assertEquals(6, $DB->count_records('role_assignments', array()));
330 $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)));
331
332 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
333 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
334 enrol_cohort_sync($course1->id, false);
335 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
336 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
337 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
338 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
339 $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)));
340 $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)));
341
342 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
343 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
344 $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
345 enrol_cohort_sync($course1->id, false);
346 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
347 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
348
349 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
350 enrol_cohort_sync($course1->id, false);
351 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
352 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
7881024e
PS
353
354
355 // Test group sync.
356
357 $this->disable_plugin(); // No event sync
358
359 $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
360 $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
361 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
362 $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
363
364 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
365 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
366 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
367
368 $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
369 $this->assertTrue(groups_add_member($group1, $user4));
370 $this->assertTrue(groups_add_member($group2, $user4));
371
372 $this->enable_plugin(); // No event sync
373
374 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
375 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
376
377 $this->assertFalse(groups_is_member($group1->id, $user1->id));
378 cohort_add_member($cohort1->id, $user1->id);
379 cohort_add_member($cohort1->id, $user4->id);
380 cohort_add_member($cohort2->id, $user4->id);
381
382 enrol_cohort_sync($course1->id, false);
383
384 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
385 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
386
387 $this->assertTrue(groups_is_member($group1->id, $user1->id));
388 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
389
390 $this->assertTrue(groups_is_member($group1->id, $user4->id));
391 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
392
393 $cohortinstance1->customint2 = $group2->id;
394 $DB->update_record('enrol', $cohortinstance1);
395
396 enrol_cohort_sync($course1->id, false);
397 $this->assertFalse(groups_is_member($group1->id, $user1->id));
398 $this->assertTrue(groups_is_member($group2->id, $user1->id));
399 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
400
401 $this->assertTrue(groups_is_member($group1->id, $user4->id));
402 $this->assertTrue(groups_is_member($group2->id, $user4->id));
403 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
404 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
405
406 cohort_remove_member($cohort1->id, $user1->id);
407 $this->assertFalse(groups_is_member($group1->id, $user1->id));
408
409 cohort_remove_member($cohort1->id, $user4->id);
410 $this->assertTrue(groups_is_member($group1->id, $user4->id));
411 $this->assertTrue(groups_is_member($group2->id, $user4->id));
08403f9e
PS
412 }
413
414 public function test_sync_all_courses() {
415 global $DB;
416
417 $this->resetAfterTest();
418
419 // Setup a few courses and categories.
420
421 $cohortplugin = enrol_get_plugin('cohort');
422 $manualplugin = enrol_get_plugin('manual');
423
424 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
425 $this->assertNotEmpty($studentrole);
426 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
427 $this->assertNotEmpty($teacherrole);
428 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
429 $this->assertNotEmpty($managerrole);
430
431 $cat1 = $this->getDataGenerator()->create_category();
432 $cat2 = $this->getDataGenerator()->create_category();
433
434 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
435 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
436 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
437 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
438 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
439
440 $user1 = $this->getDataGenerator()->create_user();
441 $user2 = $this->getDataGenerator()->create_user();
442 $user3 = $this->getDataGenerator()->create_user();
443 $user4 = $this->getDataGenerator()->create_user();
444
445 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
446 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
447 $cohort3 = $this->getDataGenerator()->create_cohort();
448
449 $this->disable_plugin(); // Prevents event sync.
450
451 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
452 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
453
454 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
455 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
456
457 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
458 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
459
460 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
461 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
462
463 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
464 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
465
466 cohort_add_member($cohort1->id, $user1->id);
467 cohort_add_member($cohort1->id, $user2->id);
468 cohort_add_member($cohort1->id, $user4->id);
469 cohort_add_member($cohort2->id, $user3->id);
470 cohort_add_member($cohort3->id, $user3->id);
471
472 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
473 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
474
475
476 // Test sync of one course only.
477
478 enrol_cohort_sync(null, false);
479 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
480 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
481
482
483 $this->enable_plugin();
484 enrol_cohort_sync(null, false);
485 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
486 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
487 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
488 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
489 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
490 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
491 $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)));
492 $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)));
493 $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)));
494 $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)));
495
496 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
497 $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
498 enrol_cohort_sync($course1->id, false);
499 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
500 $this->assertEquals(6, $DB->count_records('role_assignments', array()));
501 $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)));
502
503 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
504 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
505 enrol_cohort_sync($course1->id, false);
506 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
507 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
508 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
509 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
510 $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)));
511 $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)));
512
513 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
514 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
515 $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
516 enrol_cohort_sync($course1->id, false);
517 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
518 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
519
520 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
521 enrol_cohort_sync($course1->id, false);
522 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
523 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
524
7881024e
PS
525
526 // Test group sync.
527
528 $this->disable_plugin(); // No event sync
529
530 $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
531 $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
532 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
533 $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
534 $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course2->id));
535 $group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
536
537 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
538 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
539 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
540
541 $this->assertTrue(groups_add_member($group1, $user4));
542 $this->assertTrue(groups_add_member($group2, $user4));
543
544 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
545 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
546
547 $this->assertFalse(groups_is_member($group1->id, $user1->id));
548 cohort_add_member($cohort1->id, $user1->id);
549 cohort_add_member($cohort1->id, $user4->id);
550 cohort_add_member($cohort2->id, $user4->id);
551 cohort_add_member($cohort2->id, $user3->id);
552
553 $this->enable_plugin();
554
555 enrol_cohort_sync(null, false);
556
557 $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
558 $this->assertEquals(8, $DB->count_records('role_assignments', array()));
559
560 $this->assertTrue(groups_is_member($group1->id, $user1->id));
561 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
562
563 $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
564 $this->assertTrue(groups_is_member($group1->id, $user4->id));
565 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
566
567 $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3));
568 $this->assertFalse(groups_is_member($group3->id, $user3->id));
569
570 $cohortinstance1->customint2 = $group2->id;
571 $DB->update_record('enrol', $cohortinstance1);
572 $cohortinstance3->customint2 = $group3->id;
573 $DB->update_record('enrol', $cohortinstance3);
574
575 enrol_cohort_sync(null, false);
576 $this->assertFalse(groups_is_member($group1->id, $user1->id));
577 $this->assertTrue(groups_is_member($group2->id, $user1->id));
578 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
579
580 $this->assertTrue(groups_is_member($group1->id, $user4->id));
581 $this->assertTrue(groups_is_member($group2->id, $user4->id));
582 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
583 $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
584
585 $this->assertTrue(groups_is_member($group3->id, $user3->id));
586 $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group3->id, 'userid'=>$user3->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
587
588 cohort_remove_member($cohort1->id, $user1->id);
589 $this->assertFalse(groups_is_member($group1->id, $user1->id));
590
591 cohort_remove_member($cohort1->id, $user4->id);
592 $this->assertTrue(groups_is_member($group1->id, $user4->id));
593 $this->assertTrue(groups_is_member($group2->id, $user4->id));
08403f9e
PS
594 }
595}