b938206cbd2bddb7dd7ee03c9a7b7e453ac7a1fd
[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));
103         // Test cohort member add event.
105         cohort_add_member($cohort1->id, $user1->id);
106         cohort_add_member($cohort1->id, $user2->id);
107         cohort_add_member($cohort1->id, $user4->id);
108         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
109         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
110         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
111         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
112         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
113         $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)));
114         $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)));
115         $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)));
117         cohort_add_member($cohort2->id, $user3->id);
118         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
119         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
120         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
121         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
122         $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)));
123         $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)));
125         cohort_add_member($cohort3->id, $user3->id);
126         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
127         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
129         // Test cohort remove action.
131         $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction'));
132         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
134         cohort_remove_member($cohort1->id, $user2->id);
135         cohort_remove_member($cohort1->id, $user4->id);
136         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
137         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
138         $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)));
139         $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)));
141         cohort_add_member($cohort1->id, $user2->id);
142         cohort_add_member($cohort1->id, $user4->id);
143         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
144         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
145         $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)));
146         $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)));
148         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
149         cohort_remove_member($cohort1->id, $user2->id);
150         cohort_remove_member($cohort1->id, $user4->id);
151         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
152         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
153         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
154         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
155         $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)));
156         $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)));
158         cohort_remove_member($cohort2->id, $user3->id);
159         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
160         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
161         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
162         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
163         $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)));
164         $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)));
167         // Test cohort deleting.
169         cohort_add_member($cohort1->id, $user2->id);
170         cohort_add_member($cohort1->id, $user4->id);
171         cohort_add_member($cohort2->id, $user3->id);
172         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
173         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
175         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
176         cohort_delete_cohort($cohort2);
177         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
178         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
180         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST);
181         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST);
183         $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status);
184         $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status);
185         $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
186         $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
188         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
189         cohort_delete_cohort($cohort1);
190         $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
191         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
192         $this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id)));
193         $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
195         // Cleanup after previous test (remove the extra user_enrolment).
196         enrol_cohort_sync($trace, $course1->id);
197         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
199         // Test group sync.
201         $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
202         $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
203         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
204         $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
206         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
207         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
208         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
210         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
211         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
213         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
214         $this->assertTrue(groups_add_member($group1, $user4));
215         $this->assertTrue(groups_add_member($group2, $user4));
217         $this->assertFalse(groups_is_member($group1->id, $user1->id));
218         cohort_add_member($cohort1->id, $user1->id);
219         $this->assertTrue(groups_is_member($group1->id, $user1->id));
220         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
222         cohort_add_member($cohort1->id, $user4->id);
223         $this->assertTrue(groups_is_member($group1->id, $user4->id));
224         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
226         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
228         cohort_remove_member($cohort1->id, $user1->id);
229         $this->assertFalse(groups_is_member($group1->id, $user1->id));
231         cohort_remove_member($cohort1->id, $user4->id);
232         $this->assertTrue(groups_is_member($group1->id, $user4->id));
233         $this->assertTrue(groups_is_member($group2->id, $user4->id));
235         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
236         cohort_add_member($cohort1->id, $user1->id);
238         cohort_remove_member($cohort1->id, $user1->id);
239         $this->assertTrue(groups_is_member($group1->id, $user1->id));
242         // Test deleting of instances.
244         cohort_add_member($cohort1->id, $user1->id);
245         cohort_add_member($cohort1->id, $user2->id);
246         cohort_add_member($cohort1->id, $user3->id);
248         $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
249         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
250         $this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
251         $this->assertEquals(5, $DB->count_records('groups_members', array()));
252         $this->assertEquals(3, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
254         $cohortplugin->delete_instance($cohortinstance1);
256         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
257         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
258         $this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
259         $this->assertEquals(2, $DB->count_records('groups_members', array()));
260         $this->assertEquals(0, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
261     }
263     public function test_sync_course() {
264         global $DB;
265         $this->resetAfterTest();
267         $trace = new null_progress_trace();
269         // Setup a few courses and categories.
271         $cohortplugin = enrol_get_plugin('cohort');
272         $manualplugin = enrol_get_plugin('manual');
274         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
275         $this->assertNotEmpty($studentrole);
276         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
277         $this->assertNotEmpty($teacherrole);
278         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
279         $this->assertNotEmpty($managerrole);
281         $cat1 = $this->getDataGenerator()->create_category();
282         $cat2 = $this->getDataGenerator()->create_category();
284         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
285         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
286         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
287         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
288         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
290         $user1 = $this->getDataGenerator()->create_user();
291         $user2 = $this->getDataGenerator()->create_user();
292         $user3 = $this->getDataGenerator()->create_user();
293         $user4 = $this->getDataGenerator()->create_user();
295         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
296         $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
297         $cohort3 = $this->getDataGenerator()->create_cohort();
299         $this->disable_plugin(); // Prevents event sync.
301         $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
302         $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
304         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
305         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
307         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
308         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
310         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
311         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
313         $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
314         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
316         cohort_add_member($cohort1->id, $user1->id);
317         cohort_add_member($cohort1->id, $user2->id);
318         cohort_add_member($cohort1->id, $user4->id);
319         cohort_add_member($cohort2->id, $user3->id);
320         cohort_add_member($cohort3->id, $user3->id);
322         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
323         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
326         // Test sync of one course only.
328         enrol_cohort_sync($trace, $course1->id);
329         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
330         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
333         $this->enable_plugin();
334         enrol_cohort_sync($trace, $course2->id);
335         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
336         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
337         $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events!
338         $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events!
340         enrol_cohort_sync($trace, $course1->id);
341         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
342         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
343         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
344         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
345         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
346         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
347         $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)));
348         $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)));
349         $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)));
350         $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)));
352         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
353         $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
354         enrol_cohort_sync($trace, $course1->id);
355         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
356         $this->assertEquals(6, $DB->count_records('role_assignments', array()));
357         $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)));
359         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
360         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
361         enrol_cohort_sync($trace, $course1->id);
362         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
363         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
364         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
365         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
366         $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)));
367         $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)));
369         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
370         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
371         $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
372         enrol_cohort_sync($trace, $course1->id);
373         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
374         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
376         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
377         enrol_cohort_sync($trace, $course1->id);
378         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
379         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
382         // Test group sync.
384         $this->disable_plugin(); // No event sync.
386         // Trigger sync to remove left over role assignments.
387         enrol_cohort_sync($trace, $course1->id);
388         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
390         $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
391         $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
392         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
393         $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
395         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
396         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
397         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
399         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
400         $this->assertTrue(groups_add_member($group1, $user4));
401         $this->assertTrue(groups_add_member($group2, $user4));
403         $this->enable_plugin(); // No event sync.
405         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
406         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
408         $this->assertFalse(groups_is_member($group1->id, $user1->id));
409         cohort_add_member($cohort1->id, $user1->id);
410         cohort_add_member($cohort1->id, $user4->id);
411         cohort_add_member($cohort2->id, $user4->id);
413         enrol_cohort_sync($trace, $course1->id);
415         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
416         // This used to be 7 - but now add_instance triggers an immediate sync.
417         $this->assertEquals(6, $DB->count_records('role_assignments', array()));
419         $this->assertTrue(groups_is_member($group1->id, $user1->id));
420         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
422         $this->assertTrue(groups_is_member($group1->id, $user4->id));
423         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
425         $cohortinstance1->customint2 = $group2->id;
426         $DB->update_record('enrol', $cohortinstance1);
428         enrol_cohort_sync($trace, $course1->id);
429         $this->assertFalse(groups_is_member($group1->id, $user1->id));
430         $this->assertTrue(groups_is_member($group2->id, $user1->id));
431         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
433         $this->assertTrue(groups_is_member($group1->id, $user4->id));
434         $this->assertTrue(groups_is_member($group2->id, $user4->id));
435         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
436         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
438         cohort_remove_member($cohort1->id, $user1->id);
439         $this->assertFalse(groups_is_member($group1->id, $user1->id));
441         cohort_remove_member($cohort1->id, $user4->id);
442         $this->assertTrue(groups_is_member($group1->id, $user4->id));
443         $this->assertTrue(groups_is_member($group2->id, $user4->id));
444     }
446     public function test_sync_all_courses() {
447         global $DB;
449         $this->resetAfterTest();
451         $trace = new null_progress_trace();
453         // Setup a few courses and categories.
455         $cohortplugin = enrol_get_plugin('cohort');
456         $manualplugin = enrol_get_plugin('manual');
458         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
459         $this->assertNotEmpty($studentrole);
460         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
461         $this->assertNotEmpty($teacherrole);
462         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
463         $this->assertNotEmpty($managerrole);
465         $cat1 = $this->getDataGenerator()->create_category();
466         $cat2 = $this->getDataGenerator()->create_category();
468         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
469         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
470         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
471         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
472         $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
474         $user1 = $this->getDataGenerator()->create_user();
475         $user2 = $this->getDataGenerator()->create_user();
476         $user3 = $this->getDataGenerator()->create_user();
477         $user4 = $this->getDataGenerator()->create_user();
479         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
480         $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat2->id)->id));
481         $cohort3 = $this->getDataGenerator()->create_cohort();
483         $this->disable_plugin(); // Prevents event sync.
485         $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
486         $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
488         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
489         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
491         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
492         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
494         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
495         $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
497         $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
498         $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
500         cohort_add_member($cohort1->id, $user1->id);
501         cohort_add_member($cohort1->id, $user2->id);
502         cohort_add_member($cohort1->id, $user4->id);
503         cohort_add_member($cohort2->id, $user3->id);
504         cohort_add_member($cohort3->id, $user3->id);
506         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
507         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
510         // Test sync of one course only.
512         enrol_cohort_sync($trace, null);
513         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
514         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
517         $this->enable_plugin();
518         enrol_cohort_sync($trace, null);
519         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
520         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
521         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
522         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
523         $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
524         $this->assertEquals(7, $DB->count_records('role_assignments', array()));
525         $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)));
526         $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)));
527         $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)));
528         $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)));
530         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
531         $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
532         enrol_cohort_sync($trace, $course1->id);
533         $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
534         $this->assertEquals(6, $DB->count_records('role_assignments', array()));
535         $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)));
537         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
538         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
539         enrol_cohort_sync($trace, $course1->id);
540         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
541         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
542         $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
543         $this->assertEquals(5, $DB->count_records('role_assignments', array()));
544         $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)));
545         $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)));
547         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
548         $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
549         $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
550         enrol_cohort_sync($trace, $course1->id);
551         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
552         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
554         $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
555         enrol_cohort_sync($trace, $course1->id);
556         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
557         $this->assertEquals(3, $DB->count_records('role_assignments', array()));
560         // Test group sync.
562         $this->disable_plugin(); // No event sync
563         // Trigger sync to remove extra role assignments.
564         enrol_cohort_sync($trace, $course1->id);
565         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
567         $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
568         $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
569         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
570         $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
571         $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course2->id));
572         $group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
574         $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>context_coursecat::instance($cat1->id)->id));
575         $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
576         $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
578         $this->assertTrue(groups_add_member($group1, $user4));
579         $this->assertTrue(groups_add_member($group2, $user4));
581         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
582         $this->assertEquals(2, $DB->count_records('role_assignments', array()));
584         $this->assertFalse(groups_is_member($group1->id, $user1->id));
585         cohort_add_member($cohort1->id, $user1->id);
586         cohort_add_member($cohort1->id, $user4->id);
587         cohort_add_member($cohort2->id, $user4->id);
588         cohort_add_member($cohort2->id, $user3->id);
590         $this->enable_plugin();
592         enrol_cohort_sync($trace, null);
594         $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
595         $this->assertEquals(8, $DB->count_records('role_assignments', array()));
597         $this->assertTrue(groups_is_member($group1->id, $user1->id));
598         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
600         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4));
601         $this->assertTrue(groups_is_member($group1->id, $user4->id));
602         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
604         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3));
605         $this->assertFalse(groups_is_member($group3->id, $user3->id));
607         $cohortinstance1->customint2 = $group2->id;
608         $DB->update_record('enrol', $cohortinstance1);
609         $cohortinstance3->customint2 = $group3->id;
610         $DB->update_record('enrol', $cohortinstance3);
612         enrol_cohort_sync($trace, null);
613         $this->assertFalse(groups_is_member($group1->id, $user1->id));
614         $this->assertTrue(groups_is_member($group2->id, $user1->id));
615         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
617         $this->assertTrue(groups_is_member($group1->id, $user4->id));
618         $this->assertTrue(groups_is_member($group2->id, $user4->id));
619         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
620         $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
622         $this->assertTrue(groups_is_member($group3->id, $user3->id));
623         $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group3->id, 'userid'=>$user3->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
625         cohort_remove_member($cohort1->id, $user1->id);
626         $this->assertFalse(groups_is_member($group1->id, $user1->id));
628         cohort_remove_member($cohort1->id, $user4->id);
629         $this->assertTrue(groups_is_member($group1->id, $user4->id));
630         $this->assertTrue(groups_is_member($group2->id, $user4->id));
631     }