MDL-52098 enrol: cohort now looks at instance status
[moodle.git] / enrol / cohort / tests / sync_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
30 require_once($CFG->dirroot.'/cohort/lib.php');
31 require_once($CFG->dirroot.'/group/lib.php');
33 class enrol_cohort_testcase extends advanced_testcase {
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     }
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     }
49     public function test_handler_sync() {
50         global $DB;
52         $this->resetAfterTest();
53         $trace = new null_progress_trace();
55         // Setup a few courses and categories.
57         $cohortplugin = enrol_get_plugin('cohort');
58         $manualplugin = enrol_get_plugin('manual');
60         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
61         $this->assertNotEmpty($studentrole);
62         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
63         $this->assertNotEmpty($teacherrole);
64         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
65         $this->assertNotEmpty($managerrole);
67         $cat1 = $this->getDataGenerator()->create_category();
68         $cat2 = $this->getDataGenerator()->create_category();
70         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
71         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
72         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
73         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
74         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
76         $user1 = $this->getDataGenerator()->create_user();
77         $user2 = $this->getDataGenerator()->create_user();
78         $user3 = $this->getDataGenerator()->create_user();
79         $user4 = $this->getDataGenerator()->create_user();
81         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
82         $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
83         $cohort3 = $this->getDataGenerator()->create_cohort();
85         $this->enable_plugin();
87         $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
88         $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
90         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
91         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
93         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
94         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
96         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
97         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
99         $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
100         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
102         $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
103         $cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
105         // Test cohort member add event.
107         cohort_add_member($cohort1->id, $user1->id);
108         cohort_add_member($cohort1->id, $user2->id);
109         cohort_add_member($cohort1->id, $user4->id);
110         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
111         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
112         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
113         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
114         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
115         $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)));
116         $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)));
117         $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)));
119         cohort_add_member($cohort2->id, $user3->id);
120         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
121         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
122         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
123         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->id, 'userid' => $user3->id)));
124         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
125         $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)));
126         $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)));
128         cohort_add_member($cohort3->id, $user3->id);
129         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
130         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
132         // Test cohort remove action.
134         $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction'));
135         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
137         cohort_remove_member($cohort1->id, $user2->id);
138         cohort_remove_member($cohort1->id, $user4->id);
139         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
140         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
141         $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)));
142         $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)));
144         cohort_add_member($cohort1->id, $user2->id);
145         cohort_add_member($cohort1->id, $user4->id);
146         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
147         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
148         $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)));
149         $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)));
151         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
152         cohort_remove_member($cohort1->id, $user2->id);
153         cohort_remove_member($cohort1->id, $user4->id);
154         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
155         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
156         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
157         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
158         $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)));
159         $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)));
161         cohort_remove_member($cohort2->id, $user3->id);
162         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
163         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
164         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
165         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
166         $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)));
167         $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)));
170         // Test cohort deleting.
172         cohort_add_member($cohort1->id, $user2->id);
173         cohort_add_member($cohort1->id, $user4->id);
174         cohort_add_member($cohort2->id, $user3->id);
175         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
176         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
178         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
179         cohort_delete_cohort($cohort2);
180         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
181         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
183         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST);
184         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST);
186         $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status);
187         $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status);
188         $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
189         $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
191         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
192         cohort_delete_cohort($cohort1);
193         $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
194         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
195         $this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id)));
196         $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
198         // Cleanup after previous test (remove the extra user_enrolment).
199         enrol_cohort_sync($trace, $course1->id);
200         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
202         // Test group sync.
204         $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
205         $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
206         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
207         $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
209         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
210         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
211         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
213         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
214         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
216         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
217         $this->assertTrue(groups_add_member($group1, $user4));
218         $this->assertTrue(groups_add_member($group2, $user4));
220         $this->assertFalse(groups_is_member($group1->id, $user1->id));
221         cohort_add_member($cohort1->id, $user1->id);
222         $this->assertTrue(groups_is_member($group1->id, $user1->id));
223         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
225         cohort_add_member($cohort1->id, $user4->id);
226         $this->assertTrue(groups_is_member($group1->id, $user4->id));
227         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
229         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
231         cohort_remove_member($cohort1->id, $user1->id);
232         $this->assertFalse(groups_is_member($group1->id, $user1->id));
234         cohort_remove_member($cohort1->id, $user4->id);
235         $this->assertTrue(groups_is_member($group1->id, $user4->id));
236         $this->assertTrue(groups_is_member($group2->id, $user4->id));
238         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
239         cohort_add_member($cohort1->id, $user1->id);
241         cohort_remove_member($cohort1->id, $user1->id);
242         $this->assertTrue(groups_is_member($group1->id, $user1->id));
245         // Test deleting of instances.
247         cohort_add_member($cohort1->id, $user1->id);
248         cohort_add_member($cohort1->id, $user2->id);
249         cohort_add_member($cohort1->id, $user3->id);
251         $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
252         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
253         $this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
254         $this->assertEquals(5, $DB->count_records('groups_members', array()));
255         $this->assertEquals(3, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
257         $cohortplugin->delete_instance($cohortinstance1);
259         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
260         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
261         $this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
262         $this->assertEquals(2, $DB->count_records('groups_members', array()));
263         $this->assertEquals(0, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
264     }
266     public function test_sync_course() {
267         global $DB;
268         $this->resetAfterTest();
270         $trace = new null_progress_trace();
272         // Setup a few courses and categories.
274         $cohortplugin = enrol_get_plugin('cohort');
275         $manualplugin = enrol_get_plugin('manual');
277         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
278         $this->assertNotEmpty($studentrole);
279         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
280         $this->assertNotEmpty($teacherrole);
281         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
282         $this->assertNotEmpty($managerrole);
284         $cat1 = $this->getDataGenerator()->create_category();
285         $cat2 = $this->getDataGenerator()->create_category();
287         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
288         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
289         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
290         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
291         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
293         $user1 = $this->getDataGenerator()->create_user();
294         $user2 = $this->getDataGenerator()->create_user();
295         $user3 = $this->getDataGenerator()->create_user();
296         $user4 = $this->getDataGenerator()->create_user();
298         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
299         $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
300         $cohort3 = $this->getDataGenerator()->create_cohort();
302         $this->disable_plugin(); // Prevents event sync.
304         $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
305         $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
307         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
308         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
310         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
311         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
313         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
314         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
316         $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
317         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
319         $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
320         $cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
322         cohort_add_member($cohort1->id, $user1->id);
323         cohort_add_member($cohort1->id, $user2->id);
324         cohort_add_member($cohort1->id, $user4->id);
325         cohort_add_member($cohort2->id, $user3->id);
326         cohort_add_member($cohort3->id, $user3->id);
328         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
329         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
332         // Test sync of one course only.
334         enrol_cohort_sync($trace, $course1->id);
335         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
336         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
339         $this->enable_plugin();
340         enrol_cohort_sync($trace, $course2->id);
341         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
342         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
343         $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events!
344         $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events!
346         enrol_cohort_sync($trace, $course1->id);
347         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
348         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
349         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
350         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
351         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
352         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->id, 'userid' => $user3->id)));
353         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
354         $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)));
355         $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)));
356         $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)));
357         $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)));
359         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
360         $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
361         enrol_cohort_sync($trace, $course1->id);
362         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
363         $this->assertEquals(6, $DB->count_records('role_assignments', array()));
364         $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)));
366         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
367         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
368         enrol_cohort_sync($trace, $course1->id);
369         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
370         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
371         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
372         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
373         $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)));
374         $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)));
376         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
377         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
378         $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
379         enrol_cohort_sync($trace, $course1->id);
380         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
381         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
383         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
384         enrol_cohort_sync($trace, $course1->id);
385         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
386         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
389         // Test group sync.
391         $this->disable_plugin(); // No event sync.
393         // Trigger sync to remove left over role assignments.
394         enrol_cohort_sync($trace, $course1->id);
395         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
397         $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
398         $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
399         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
400         $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
402         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
403         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
404         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
406         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
407         $this->assertTrue(groups_add_member($group1, $user4));
408         $this->assertTrue(groups_add_member($group2, $user4));
410         $this->enable_plugin(); // No event sync.
412         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
413         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
415         $this->assertFalse(groups_is_member($group1->id, $user1->id));
416         cohort_add_member($cohort1->id, $user1->id);
417         cohort_add_member($cohort1->id, $user4->id);
418         cohort_add_member($cohort2->id, $user4->id);
420         enrol_cohort_sync($trace, $course1->id);
422         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
423         // This used to be 7 - but now add_instance triggers an immediate sync.
424         $this->assertEquals(6, $DB->count_records('role_assignments', array()));
426         $this->assertTrue(groups_is_member($group1->id, $user1->id));
427         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
429         $this->assertTrue(groups_is_member($group1->id, $user4->id));
430         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
432         $cohortinstance1->customint2 = $group2->id;
433         $DB->update_record('enrol', $cohortinstance1);
435         enrol_cohort_sync($trace, $course1->id);
436         $this->assertFalse(groups_is_member($group1->id, $user1->id));
437         $this->assertTrue(groups_is_member($group2->id, $user1->id));
438         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
440         $this->assertTrue(groups_is_member($group1->id, $user4->id));
441         $this->assertTrue(groups_is_member($group2->id, $user4->id));
442         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
443         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
445         cohort_remove_member($cohort1->id, $user1->id);
446         $this->assertFalse(groups_is_member($group1->id, $user1->id));
448         cohort_remove_member($cohort1->id, $user4->id);
449         $this->assertTrue(groups_is_member($group1->id, $user4->id));
450         $this->assertTrue(groups_is_member($group2->id, $user4->id));
451     }
453     public function test_sync_all_courses() {
454         global $DB;
456         $this->resetAfterTest();
458         $trace = new null_progress_trace();
460         // Setup a few courses and categories.
462         $cohortplugin = enrol_get_plugin('cohort');
463         $manualplugin = enrol_get_plugin('manual');
465         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
466         $this->assertNotEmpty($studentrole);
467         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
468         $this->assertNotEmpty($teacherrole);
469         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
470         $this->assertNotEmpty($managerrole);
472         $cat1 = $this->getDataGenerator()->create_category();
473         $cat2 = $this->getDataGenerator()->create_category();
475         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
476         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
477         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
478         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
479         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
481         $user1 = $this->getDataGenerator()->create_user();
482         $user2 = $this->getDataGenerator()->create_user();
483         $user3 = $this->getDataGenerator()->create_user();
484         $user4 = $this->getDataGenerator()->create_user();
486         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
487         $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
488         $cohort3 = $this->getDataGenerator()->create_cohort();
490         $this->disable_plugin(); // Prevents event sync.
492         $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
493         $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
495         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
496         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
498         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
499         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
501         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
502         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
504         $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
505         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
507         cohort_add_member($cohort1->id, $user1->id);
508         cohort_add_member($cohort1->id, $user2->id);
509         cohort_add_member($cohort1->id, $user4->id);
510         cohort_add_member($cohort2->id, $user3->id);
511         cohort_add_member($cohort3->id, $user3->id);
513         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
514         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
517         // Test sync of one course only.
519         enrol_cohort_sync($trace, null);
520         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
521         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
524         $this->enable_plugin();
525         enrol_cohort_sync($trace, null);
526         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
527         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
528         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
529         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
530         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
531         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
532         $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)));
533         $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)));
534         $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)));
535         $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)));
537         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
538         $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
539         enrol_cohort_sync($trace, $course1->id);
540         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
541         $this->assertEquals(6, $DB->count_records('role_assignments', array()));
542         $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)));
544         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
545         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
546         enrol_cohort_sync($trace, $course1->id);
547         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
548         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
549         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
550         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
551         $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)));
552         $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)));
554         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
555         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
556         $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
557         enrol_cohort_sync($trace, $course1->id);
558         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
559         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
561         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
562         enrol_cohort_sync($trace, $course1->id);
563         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
564         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
567         // Test group sync.
569         $this->disable_plugin(); // No event sync
570         // Trigger sync to remove extra role assignments.
571         enrol_cohort_sync($trace, $course1->id);
572         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
574         $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
575         $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
576         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
577         $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
578         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course2->id));
579         $group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
581         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
582         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
583         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
585         $this->assertTrue(groups_add_member($group1, $user4));
586         $this->assertTrue(groups_add_member($group2, $user4));
588         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
589         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
591         $this->assertFalse(groups_is_member($group1->id, $user1->id));
592         cohort_add_member($cohort1->id, $user1->id);
593         cohort_add_member($cohort1->id, $user4->id);
594         cohort_add_member($cohort2->id, $user4->id);
595         cohort_add_member($cohort2->id, $user3->id);
597         $this->enable_plugin();
599         enrol_cohort_sync($trace, null);
601         $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
602         $this->assertEquals(8, $DB->count_records('role_assignments', array()));
604         $this->assertTrue(groups_is_member($group1->id, $user1->id));
605         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
607         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
608         $this->assertTrue(groups_is_member($group1->id, $user4->id));
609         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
611         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3));
612         $this->assertFalse(groups_is_member($group3->id, $user3->id));
614         $cohortinstance1->customint2 = $group2->id;
615         $DB->update_record('enrol', $cohortinstance1);
616         $cohortinstance3->customint2 = $group3->id;
617         $DB->update_record('enrol', $cohortinstance3);
619         enrol_cohort_sync($trace, null);
620         $this->assertFalse(groups_is_member($group1->id, $user1->id));
621         $this->assertTrue(groups_is_member($group2->id, $user1->id));
622         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
624         $this->assertTrue(groups_is_member($group1->id, $user4->id));
625         $this->assertTrue(groups_is_member($group2->id, $user4->id));
626         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
627         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
629         $this->assertTrue(groups_is_member($group3->id, $user3->id));
630         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group3->id, 'userid'=>$user3->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
632         cohort_remove_member($cohort1->id, $user1->id);
633         $this->assertFalse(groups_is_member($group1->id, $user1->id));
635         cohort_remove_member($cohort1->id, $user4->id);
636         $this->assertTrue(groups_is_member($group1->id, $user4->id));
637         $this->assertTrue(groups_is_member($group2->id, $user4->id));
638     }