MDL-31437 add enrol_cohort tests
[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');
31
32class enrol_cohort_testcase extends advanced_testcase {
33
34 protected function enable_plugin() {
35 $enabled = enrol_get_plugins(true);
36 $enabled['cohort'] = true;
37 $enabled = array_keys($enabled);
38 set_config('enrol_plugins_enabled', implode(',', $enabled));
39 }
40
41 protected function disable_plugin() {
42 $enabled = enrol_get_plugins(true);
43 unset($enabled['cohort']);
44 $enabled = array_keys($enabled);
45 set_config('enrol_plugins_enabled', implode(',', $enabled));
46 }
47
48 public function test_handler_sync() {
49 global $DB;
50
51 $this->resetAfterTest();
52
53 // Setup a few courses and categories.
54
55 $cohortplugin = enrol_get_plugin('cohort');
56 $manualplugin = enrol_get_plugin('manual');
57
58 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
59 $this->assertNotEmpty($studentrole);
60 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
61 $this->assertNotEmpty($teacherrole);
62 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
63 $this->assertNotEmpty($managerrole);
64
65 $cat1 = $this->getDataGenerator()->create_category();
66 $cat2 = $this->getDataGenerator()->create_category();
67
68 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
69 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
70 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
71 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
72 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
73
74 $user1 = $this->getDataGenerator()->create_user();
75 $user2 = $this->getDataGenerator()->create_user();
76 $user3 = $this->getDataGenerator()->create_user();
77 $user4 = $this->getDataGenerator()->create_user();
78
79 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
80 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
81 $cohort3 = $this->getDataGenerator()->create_cohort();
82
83 $this->enable_plugin();
84
85 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
86 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
87
88 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
89 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
90
91 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
92 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
93
94 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
95 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
96
97 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
98 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
99
100
101 // Test cohort member add event.
102
103 cohort_add_member($cohort1->id, $user1->id);
104 cohort_add_member($cohort1->id, $user2->id);
105 cohort_add_member($cohort1->id, $user4->id);
106 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
107 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
108 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
109 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
110 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
111 $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)));
112 $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)));
113 $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)));
114
115 cohort_add_member($cohort2->id, $user3->id);
116 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
117 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
118 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
119 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
120 $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)));
121 $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)));
122
123 cohort_add_member($cohort3->id, $user3->id);
124 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
125 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
126
127 // Test cohort remove action.
128
129 $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction'));
130 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
131
132 cohort_remove_member($cohort1->id, $user2->id);
133 cohort_remove_member($cohort1->id, $user4->id);
134 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
135 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
136 $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)));
137 $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)));
138
139 cohort_add_member($cohort1->id, $user2->id);
140 cohort_add_member($cohort1->id, $user4->id);
141 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
142 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
143 $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)));
144 $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)));
145
146 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
147 cohort_remove_member($cohort1->id, $user2->id);
148 cohort_remove_member($cohort1->id, $user4->id);
149 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
150 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
151 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
152 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
153 $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)));
154 $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)));
155
156 cohort_remove_member($cohort2->id, $user3->id);
157 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
158 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
159 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
160 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
161 $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)));
162 $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)));
163
164
165 // Test cohort deleting.
166
167 cohort_add_member($cohort1->id, $user2->id);
168 cohort_add_member($cohort1->id, $user4->id);
169 cohort_add_member($cohort2->id, $user3->id);
170 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
171 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
172
173 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
174 cohort_delete_cohort($cohort2);
175 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
176 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
177
178 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST);
179 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST);
180
181 $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status);
182 $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status);
183 $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
184 $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
185
186 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
187 cohort_delete_cohort($cohort1);
188 $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
189 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
190 $this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id)));
191 $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
192
193 }
194
195 public function test_sync_course() {
196 global $DB;
197 $this->resetAfterTest();
198
199 // Setup a few courses and categories.
200
201 $cohortplugin = enrol_get_plugin('cohort');
202 $manualplugin = enrol_get_plugin('manual');
203
204 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
205 $this->assertNotEmpty($studentrole);
206 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
207 $this->assertNotEmpty($teacherrole);
208 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
209 $this->assertNotEmpty($managerrole);
210
211 $cat1 = $this->getDataGenerator()->create_category();
212 $cat2 = $this->getDataGenerator()->create_category();
213
214 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
215 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
216 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
217 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
218 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
219
220 $user1 = $this->getDataGenerator()->create_user();
221 $user2 = $this->getDataGenerator()->create_user();
222 $user3 = $this->getDataGenerator()->create_user();
223 $user4 = $this->getDataGenerator()->create_user();
224
225 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
226 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
227 $cohort3 = $this->getDataGenerator()->create_cohort();
228
229 $this->disable_plugin(); // Prevents event sync.
230
231 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
232 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
233
234 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
235 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
236
237 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
238 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
239
240 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
241 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
242
243 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
244 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
245
246 cohort_add_member($cohort1->id, $user1->id);
247 cohort_add_member($cohort1->id, $user2->id);
248 cohort_add_member($cohort1->id, $user4->id);
249 cohort_add_member($cohort2->id, $user3->id);
250 cohort_add_member($cohort3->id, $user3->id);
251
252 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
253 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
254
255
256 // Test sync of one course only.
257
258 enrol_cohort_sync($course1->id, false);
259 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
260 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
261
262
263 $this->enable_plugin();
264 enrol_cohort_sync($course2->id, false);
265 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
266 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
267 $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events!
268 $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events!
269
270 enrol_cohort_sync($course1->id, false);
271 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
272 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
273 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
274 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
275 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
276 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
277 $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)));
278 $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)));
279 $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)));
280 $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)));
281
282 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
283 $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
284 enrol_cohort_sync($course1->id, false);
285 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
286 $this->assertEquals(6, $DB->count_records('role_assignments', array()));
287 $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)));
288
289 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
290 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
291 enrol_cohort_sync($course1->id, false);
292 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
293 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
294 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
295 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
296 $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)));
297 $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)));
298
299 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
300 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
301 $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
302 enrol_cohort_sync($course1->id, false);
303 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
304 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
305
306 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
307 enrol_cohort_sync($course1->id, false);
308 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
309 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
310 }
311
312 public function test_sync_all_courses() {
313 global $DB;
314
315 $this->resetAfterTest();
316
317 // Setup a few courses and categories.
318
319 $cohortplugin = enrol_get_plugin('cohort');
320 $manualplugin = enrol_get_plugin('manual');
321
322 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
323 $this->assertNotEmpty($studentrole);
324 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
325 $this->assertNotEmpty($teacherrole);
326 $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
327 $this->assertNotEmpty($managerrole);
328
329 $cat1 = $this->getDataGenerator()->create_category();
330 $cat2 = $this->getDataGenerator()->create_category();
331
332 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
333 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
334 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
335 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
336 $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
337
338 $user1 = $this->getDataGenerator()->create_user();
339 $user2 = $this->getDataGenerator()->create_user();
340 $user3 = $this->getDataGenerator()->create_user();
341 $user4 = $this->getDataGenerator()->create_user();
342
343 $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
344 $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
345 $cohort3 = $this->getDataGenerator()->create_cohort();
346
347 $this->disable_plugin(); // Prevents event sync.
348
349 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
350 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
351
352 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
353 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
354
355 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
356 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
357
358 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
359 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
360
361 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
362 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
363
364 cohort_add_member($cohort1->id, $user1->id);
365 cohort_add_member($cohort1->id, $user2->id);
366 cohort_add_member($cohort1->id, $user4->id);
367 cohort_add_member($cohort2->id, $user3->id);
368 cohort_add_member($cohort3->id, $user3->id);
369
370 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
371 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
372
373
374 // Test sync of one course only.
375
376 enrol_cohort_sync(null, false);
377 $this->assertEquals(2, $DB->count_records('role_assignments', array()));
378 $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
379
380
381 $this->enable_plugin();
382 enrol_cohort_sync(null, false);
383 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
384 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
385 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
386 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
387 $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
388 $this->assertEquals(7, $DB->count_records('role_assignments', array()));
389 $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)));
390 $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)));
391 $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)));
392 $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)));
393
394 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
395 $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
396 enrol_cohort_sync($course1->id, false);
397 $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
398 $this->assertEquals(6, $DB->count_records('role_assignments', array()));
399 $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)));
400
401 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
402 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
403 enrol_cohort_sync($course1->id, false);
404 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
405 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
406 $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
407 $this->assertEquals(5, $DB->count_records('role_assignments', array()));
408 $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)));
409 $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)));
410
411 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
412 $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
413 $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
414 enrol_cohort_sync($course1->id, false);
415 $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
416 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
417
418 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
419 enrol_cohort_sync($course1->id, false);
420 $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
421 $this->assertEquals(3, $DB->count_records('role_assignments', array()));
422
423 }
424}