MDL-34864 add enrol_category unit tests for sync
[moodle.git] / enrol / category / 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  * Category enrolment sync functional test.
19  *
20  * @package    enrol_category
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/category/locallib.php');
31 class enrol_category_testcase extends advanced_testcase {
33     protected function enable_plugin() {
34         $enabled = enrol_get_plugins(true);
35         $enabled['category'] = true;
36         $enabled = array_keys($enabled);
37         set_config('enrol_plugins_enabled', implode(',', $enabled));
38     }
40     protected function disable_plugin() {
41         $enabled = enrol_get_plugins(true);
42         unset($enabled['category']);
43         $enabled = array_keys($enabled);
44         set_config('enrol_plugins_enabled', implode(',', $enabled));
45     }
47     protected function enable_role_sync($roleid) {
48         global $DB;
50         $syscontext = context_system::instance();
52         if ($rc = $DB->record_exists('role_capabilities', array('capability'=>'enrol/category:synchronised', 'roleid'=>$roleid, 'contextid'=>$syscontext->id))) {
53             if ($rc->permission != CAP_ALLOW) {
54                 $rc->permission = CAP_ALLOW;
55                 $DB->update_record('role_capabilities', $rc);
56             }
57         } else {
58             $rc = new stdClass();
59             $rc->capability = 'enrol/category:synchronised';
60             $rc->roleid = $roleid;
61             $rc->contextid = $syscontext->id;
62             $rc->permission = CAP_ALLOW;
63             $rc->timemodified = time();
64             $rc->modifierid = 0;
65             $DB->insert_record('role_capabilities', $rc);
66         }
67     }
69     protected function disable_role_sync($roleid) {
70         global $DB;
72         $syscontext = context_system::instance();
74         $DB->delete_records('role_capabilities', array('capability'=>'enrol/category:synchronised', 'roleid'=>$roleid, 'contextid'=>$syscontext->id));
75     }
77     /**
78      * Test utility methods used in syn test, fail here means something
79      * in core accesslib was changed, but it is possible that only this test
80      * is affected, nto the plugin itself...
81      */
82     public function test_utils() {
83         global $DB;
85         $this->resetAfterTest();
87         $syscontext = context_system::instance();
89         $this->assertFalse(enrol_is_enabled('category'));
90         $this->enable_plugin();
91         $this->assertTrue(enrol_is_enabled('category'));
93         $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
94         $this->assertEmpty($roles);
96         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
97         $this->assertNotEmpty($studentrole);
99         $this->enable_role_sync($studentrole->id);
100         $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
101         $this->assertEquals(1, count($roles));
102         $this->assertEquals($studentrole, reset($roles));
104         $this->disable_role_sync($studentrole->id);
105         $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
106         $this->assertEmpty($roles);
107     }
109     public function test_handler_sync() {
110         global $DB;
112         $this->resetAfterTest();
114         // Setup a few courses and categories.
116         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
117         $this->assertNotEmpty($studentrole);
118         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
119         $this->assertNotEmpty($teacherrole);
120         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
121         $this->assertNotEmpty($managerrole);
123         $cat1 = $this->getDataGenerator()->create_category();
124         $cat2 = $this->getDataGenerator()->create_category();
125         $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id));
127         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
128         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
129         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
130         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
132         $user1 = $this->getDataGenerator()->create_user();
133         $user2 = $this->getDataGenerator()->create_user();
134         $user3 = $this->getDataGenerator()->create_user();
135         $user4 = $this->getDataGenerator()->create_user();
137         $this->enable_role_sync($studentrole->id);
138         $this->enable_role_sync($teacherrole->id);
139         $this->enable_plugin();
141         $this->assertEquals(0, $DB->count_records('role_assignments', array()));
142         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
144         // Test assign event.
146         role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id));
147         role_assign($managerrole->id, $user3->id, context_course::instance($course1->id));
148         role_assign($managerrole->id, $user3->id, context_course::instance($course2->id));
149         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
151         role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id));
152         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
153         $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id));
154         $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id));
155         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
157         role_assign($managerrole->id, $user2->id, context_coursecat::instance($cat3->id));
158         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
160         role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id));
161         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
162         $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
164         // Test role unassigned event.
166         role_unassign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id)->id);
167         $this->assertFalse(is_enrolled(context_course::instance($course1->id), $user4->id));
168         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
170         // Make sure handlers are disabled when plugin disabled.
172         $this->disable_plugin();
173         role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id);
174         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
176         role_assign($studentrole->id, $user3->id, context_coursecat::instance($cat1->id));
177         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
179     }
181     public function test_sync_course() {
182         global $DB;
184         $this->resetAfterTest();
186         // Setup a few courses and categories.
188         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
189         $this->assertNotEmpty($studentrole);
190         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
191         $this->assertNotEmpty($teacherrole);
192         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
193         $this->assertNotEmpty($managerrole);
195         $cat1 = $this->getDataGenerator()->create_category();
196         $cat2 = $this->getDataGenerator()->create_category();
197         $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id));
199         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
200         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
201         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
202         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
204         $user1 = $this->getDataGenerator()->create_user();
205         $user2 = $this->getDataGenerator()->create_user();
206         $user3 = $this->getDataGenerator()->create_user();
207         $user4 = $this->getDataGenerator()->create_user();
209         $this->enable_role_sync($studentrole->id);
210         $this->enable_role_sync($teacherrole->id);
211         $this->enable_plugin();
213         $this->assertEquals(0, $DB->count_records('role_assignments', array()));
214         role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id));
215         role_assign($managerrole->id, $user3->id, context_course::instance($course1->id));
216         role_assign($managerrole->id, $user3->id, context_course::instance($course2->id));
217         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
220         $this->disable_plugin(); // Stops the event handlers.
221         role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id));
222         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
223         $this->enable_plugin();
224         enrol_category_sync_course($course2);
225         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
226         $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user1->id));
227         $this->assertFalse(is_enrolled(context_course::instance($course4->id), $user1->id));
228         $this->assertEquals(1, $DB->count_records('user_enrolments', array()));
230         enrol_category_sync_course($course2);
231         enrol_category_sync_course($course3);
232         enrol_category_sync_course($course4);
233         $this->assertFalse(is_enrolled(context_course::instance($course1->id), $user1->id));
234         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
235         $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id));
236         $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id));
237         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
239         $this->disable_plugin(); // Stops the event handlers.
240         role_assign($studentrole->id, $user2->id, context_coursecat::instance($cat1->id));
241         role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id));
242         role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id);
243         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
244         $this->enable_plugin();
245         enrol_category_sync_course($course2);
246         $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user1->id));
247         $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user2->id));
248         $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user4->id));
249         enrol_category_sync_course($course1);
250         enrol_category_sync_course($course3);
251         enrol_category_sync_course($course4);
252         $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
253         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user2->id));
254         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
256         $this->disable_role_sync($studentrole->id);
257         enrol_category_sync_course($course1);
258         enrol_category_sync_course($course2);
259         enrol_category_sync_course($course3);
260         enrol_category_sync_course($course4);
261         $this->assertEquals(1, $DB->count_records('user_enrolments', array()));
262         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
264         $this->assertEquals(1, $DB->count_records('enrol', array('enrol'=>'category')));
265         $this->disable_role_sync($teacherrole->id);
266         enrol_category_sync_course($course1);
267         enrol_category_sync_course($course2);
268         enrol_category_sync_course($course3);
269         enrol_category_sync_course($course4);
270         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
271         $this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'category')));
272     }
274     public function test_sync_full() {
275         global $DB;
277         $this->resetAfterTest();
279         // Setup a few courses and categories.
281         $studentrole = $DB->get_record('role', array('shortname'=>'student'));
282         $this->assertNotEmpty($studentrole);
283         $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
284         $this->assertNotEmpty($teacherrole);
285         $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
286         $this->assertNotEmpty($managerrole);
288         $cat1 = $this->getDataGenerator()->create_category();
289         $cat2 = $this->getDataGenerator()->create_category();
290         $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id));
292         $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
293         $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
294         $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
295         $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
297         $user1 = $this->getDataGenerator()->create_user();
298         $user2 = $this->getDataGenerator()->create_user();
299         $user3 = $this->getDataGenerator()->create_user();
300         $user4 = $this->getDataGenerator()->create_user();
302         $this->enable_role_sync($studentrole->id);
303         $this->enable_role_sync($teacherrole->id);
304         $this->enable_plugin();
306         $this->assertEquals(0, $DB->count_records('role_assignments', array()));
307         role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id));
308         role_assign($managerrole->id, $user3->id, context_course::instance($course1->id));
309         role_assign($managerrole->id, $user3->id, context_course::instance($course2->id));
310         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
312         $result = enrol_category_sync_full();
313         $this->assertSame(0, $result);
315         $this->disable_plugin();
316         role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id));
317         $this->enable_plugin();
318         $result = enrol_category_sync_full();
319         $this->assertSame(0, $result);
320         $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
321         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
322         $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id));
323         $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id));
325         $this->disable_plugin();
326         role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id);
327         role_assign($studentrole->id, $user2->id, context_coursecat::instance($cat1->id));
328         role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id));
329         role_assign($teacherrole->id, $user3->id, context_coursecat::instance($cat2->id));
330         role_assign($managerrole->id, $user3->id, context_course::instance($course3->id));
331         $this->enable_plugin();
332         $result = enrol_category_sync_full();
333         $this->assertSame(0, $result);
334         $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
335         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user2->id));
336         $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
337         $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3->id));
338         $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user3->id));
339         $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user3->id));
341         // Cleanup everything.
343         $this->assertNotEmpty($DB->count_records('role_assignments', array()));
344         $this->assertNotEmpty($DB->count_records('user_enrolments', array()));
346         $this->disable_plugin();
347         role_unassign_all(array('roleid'=>$studentrole->id));
348         role_unassign_all(array('roleid'=>$managerrole->id));
349         role_unassign_all(array('roleid'=>$teacherrole->id));
351         $result = enrol_category_sync_full();
352         $this->assertSame(2, $result);
353         $this->assertEquals(0, $DB->count_records('role_assignments', array()));
354         $this->assertNotEmpty($DB->count_records('user_enrolments', array()));
355         $this->disable_role_sync($studentrole->id);
356         $this->disable_role_sync($teacherrole->id);
358         $this->enable_plugin();
359         $result = enrol_category_sync_full();
360         $this->assertSame(0, $result);
361         $this->assertEquals(0, $DB->count_records('role_assignments', array()));
362         $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
363         $this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'category')));
364     }