Differences Between: [Versions 310 and 403] [Versions 39 and 403]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 namespace enrol_category; 18 19 /** 20 * Category enrolment sync functional test. 21 * 22 * @package enrol_category 23 * @category phpunit 24 * @copyright 2012 Petr Skoda {@link http://skodak.org} 25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 26 */ 27 class plugin_test extends \advanced_testcase { 28 29 protected function enable_plugin() { 30 $enabled = enrol_get_plugins(true); 31 $enabled['category'] = true; 32 $enabled = array_keys($enabled); 33 set_config('enrol_plugins_enabled', implode(',', $enabled)); 34 } 35 36 protected function disable_plugin() { 37 $enabled = enrol_get_plugins(true); 38 unset($enabled['category']); 39 $enabled = array_keys($enabled); 40 set_config('enrol_plugins_enabled', implode(',', $enabled)); 41 } 42 43 protected function enable_role_sync($roleid) { 44 $syscontext = \context_system::instance(); 45 46 assign_capability('enrol/category:synchronised', CAP_ALLOW, $roleid, $syscontext, true); 47 } 48 49 protected function disable_role_sync($roleid) { 50 $syscontext = \context_system::instance(); 51 52 unassign_capability('enrol/category:synchronised', $roleid, $syscontext); 53 } 54 55 /** 56 * Test utility methods used in syn test, fail here means something 57 * in core accesslib was changed, but it is possible that only this test 58 * is affected, nto the plugin itself... 59 */ 60 public function test_utils() { 61 global $DB; 62 63 $this->resetAfterTest(); 64 65 $syscontext = \context_system::instance(); 66 67 $this->assertFalse(enrol_is_enabled('category')); 68 $this->enable_plugin(); 69 $this->assertTrue(enrol_is_enabled('category')); 70 71 $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext); 72 $this->assertEmpty($roles); 73 74 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 75 $this->assertNotEmpty($studentrole); 76 77 $this->enable_role_sync($studentrole->id); 78 $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext); 79 $this->assertEquals(1, count($roles)); 80 $this->assertEquals($studentrole, reset($roles)); 81 82 $this->disable_role_sync($studentrole->id); 83 $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext); 84 $this->assertEmpty($roles); 85 } 86 87 public function test_handler_sync() { 88 global $DB, $CFG; 89 require_once($CFG->dirroot.'/enrol/category/locallib.php'); 90 91 $this->resetAfterTest(); 92 93 // Setup a few courses and categories. 94 95 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 96 $this->assertNotEmpty($studentrole); 97 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 98 $this->assertNotEmpty($teacherrole); 99 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 100 $this->assertNotEmpty($managerrole); 101 102 $cat1 = $this->getDataGenerator()->create_category(); 103 $cat2 = $this->getDataGenerator()->create_category(); 104 $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id)); 105 106 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); 107 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); 108 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id)); 109 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id)); 110 111 $user1 = $this->getDataGenerator()->create_user(); 112 $user2 = $this->getDataGenerator()->create_user(); 113 $user3 = $this->getDataGenerator()->create_user(); 114 $user4 = $this->getDataGenerator()->create_user(); 115 116 $this->enable_role_sync($studentrole->id); 117 $this->enable_role_sync($teacherrole->id); 118 $this->enable_plugin(); 119 120 $this->assertEquals(0, $DB->count_records('role_assignments', array())); 121 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 122 123 // Test assign event. 124 125 role_assign($managerrole->id, $user1->id, \context_coursecat::instance($cat1->id)); 126 role_assign($managerrole->id, $user3->id, \context_course::instance($course1->id)); 127 role_assign($managerrole->id, $user3->id, \context_course::instance($course2->id)); 128 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 129 130 role_assign($studentrole->id, $user1->id, \context_coursecat::instance($cat2->id)); 131 $this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user1->id)); 132 $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user1->id)); 133 $this->assertTrue(is_enrolled(\context_course::instance($course4->id), $user1->id)); 134 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 135 136 role_assign($managerrole->id, $user2->id, \context_coursecat::instance($cat3->id)); 137 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 138 139 role_assign($teacherrole->id, $user4->id, \context_coursecat::instance($cat1->id)); 140 $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4->id)); 141 $this->assertEquals(4, $DB->count_records('user_enrolments', array())); 142 143 // Test role unassigned event. 144 145 role_unassign($teacherrole->id, $user4->id, \context_coursecat::instance($cat1->id)->id); 146 $this->assertFalse(is_enrolled(\context_course::instance($course1->id), $user4->id)); 147 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 148 149 // Make sure handlers are disabled when plugin disabled. 150 151 $this->disable_plugin(); 152 role_unassign($studentrole->id, $user1->id, \context_coursecat::instance($cat2->id)->id); 153 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 154 155 role_assign($studentrole->id, $user3->id, \context_coursecat::instance($cat1->id)); 156 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 157 158 } 159 160 public function test_sync_course() { 161 global $DB, $CFG; 162 require_once($CFG->dirroot.'/enrol/category/locallib.php'); 163 164 $this->resetAfterTest(); 165 166 // Setup a few courses and categories. 167 168 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 169 $this->assertNotEmpty($studentrole); 170 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 171 $this->assertNotEmpty($teacherrole); 172 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 173 $this->assertNotEmpty($managerrole); 174 175 $cat1 = $this->getDataGenerator()->create_category(); 176 $cat2 = $this->getDataGenerator()->create_category(); 177 $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id)); 178 179 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); 180 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); 181 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id)); 182 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id)); 183 184 $user1 = $this->getDataGenerator()->create_user(); 185 $user2 = $this->getDataGenerator()->create_user(); 186 $user3 = $this->getDataGenerator()->create_user(); 187 $user4 = $this->getDataGenerator()->create_user(); 188 189 $this->enable_role_sync($studentrole->id); 190 $this->enable_role_sync($teacherrole->id); 191 $this->enable_plugin(); 192 193 $this->assertEquals(0, $DB->count_records('role_assignments', array())); 194 role_assign($managerrole->id, $user1->id, \context_coursecat::instance($cat1->id)); 195 role_assign($managerrole->id, $user3->id, \context_course::instance($course1->id)); 196 role_assign($managerrole->id, $user3->id, \context_course::instance($course2->id)); 197 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 198 199 200 $this->disable_plugin(); // Stops the event handlers. 201 role_assign($studentrole->id, $user1->id, \context_coursecat::instance($cat2->id)); 202 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 203 $this->enable_plugin(); 204 enrol_category_sync_course($course2); 205 $this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user1->id)); 206 $this->assertFalse(is_enrolled(\context_course::instance($course3->id), $user1->id)); 207 $this->assertFalse(is_enrolled(\context_course::instance($course4->id), $user1->id)); 208 $this->assertEquals(1, $DB->count_records('user_enrolments', array())); 209 210 enrol_category_sync_course($course2); 211 enrol_category_sync_course($course3); 212 enrol_category_sync_course($course4); 213 $this->assertFalse(is_enrolled(\context_course::instance($course1->id), $user1->id)); 214 $this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user1->id)); 215 $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user1->id)); 216 $this->assertTrue(is_enrolled(\context_course::instance($course4->id), $user1->id)); 217 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 218 219 $this->disable_plugin(); // Stops the event handlers. 220 role_assign($studentrole->id, $user2->id, \context_coursecat::instance($cat1->id)); 221 role_assign($teacherrole->id, $user4->id, \context_coursecat::instance($cat1->id)); 222 role_unassign($studentrole->id, $user1->id, \context_coursecat::instance($cat2->id)->id); 223 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 224 $this->enable_plugin(); 225 enrol_category_sync_course($course2); 226 $this->assertFalse(is_enrolled(\context_course::instance($course2->id), $user1->id)); 227 $this->assertFalse(is_enrolled(\context_course::instance($course2->id), $user2->id)); 228 $this->assertFalse(is_enrolled(\context_course::instance($course2->id), $user4->id)); 229 enrol_category_sync_course($course1); 230 enrol_category_sync_course($course3); 231 enrol_category_sync_course($course4); 232 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 233 $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user2->id)); 234 $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4->id)); 235 236 $this->disable_role_sync($studentrole->id); 237 enrol_category_sync_course($course1); 238 enrol_category_sync_course($course2); 239 enrol_category_sync_course($course3); 240 enrol_category_sync_course($course4); 241 $this->assertEquals(1, $DB->count_records('user_enrolments', array())); 242 $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4->id)); 243 244 $this->assertEquals(1, $DB->count_records('enrol', array('enrol'=>'category'))); 245 $this->disable_role_sync($teacherrole->id); 246 enrol_category_sync_course($course1); 247 enrol_category_sync_course($course2); 248 enrol_category_sync_course($course3); 249 enrol_category_sync_course($course4); 250 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 251 $this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'category'))); 252 } 253 254 public function test_sync_full() { 255 global $DB, $CFG; 256 require_once($CFG->dirroot.'/enrol/category/locallib.php'); 257 258 $this->resetAfterTest(); 259 260 $trace = new \null_progress_trace(); 261 262 // Setup a few courses and categories. 263 264 $studentrole = $DB->get_record('role', array('shortname'=>'student')); 265 $this->assertNotEmpty($studentrole); 266 $teacherrole = $DB->get_record('role', array('shortname'=>'teacher')); 267 $this->assertNotEmpty($teacherrole); 268 $managerrole = $DB->get_record('role', array('shortname'=>'manager')); 269 $this->assertNotEmpty($managerrole); 270 271 $cat1 = $this->getDataGenerator()->create_category(); 272 $cat2 = $this->getDataGenerator()->create_category(); 273 $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id)); 274 275 $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id)); 276 $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id)); 277 $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id)); 278 $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id)); 279 280 $user1 = $this->getDataGenerator()->create_user(); 281 $user2 = $this->getDataGenerator()->create_user(); 282 $user3 = $this->getDataGenerator()->create_user(); 283 $user4 = $this->getDataGenerator()->create_user(); 284 285 $this->enable_role_sync($studentrole->id); 286 $this->enable_role_sync($teacherrole->id); 287 $this->enable_plugin(); 288 289 $this->assertEquals(0, $DB->count_records('role_assignments', array())); 290 role_assign($managerrole->id, $user1->id, \context_coursecat::instance($cat1->id)); 291 role_assign($managerrole->id, $user3->id, \context_course::instance($course1->id)); 292 role_assign($managerrole->id, $user3->id, \context_course::instance($course2->id)); 293 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 294 295 $result = enrol_category_sync_full($trace); 296 $this->assertSame(0, $result); 297 298 $this->disable_plugin(); 299 role_assign($studentrole->id, $user1->id, \context_coursecat::instance($cat2->id)); 300 $this->enable_plugin(); 301 $result = enrol_category_sync_full($trace); 302 $this->assertSame(0, $result); 303 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 304 $this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user1->id)); 305 $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user1->id)); 306 $this->assertTrue(is_enrolled(\context_course::instance($course4->id), $user1->id)); 307 308 $this->disable_plugin(); 309 role_unassign($studentrole->id, $user1->id, \context_coursecat::instance($cat2->id)->id); 310 role_assign($studentrole->id, $user2->id, \context_coursecat::instance($cat1->id)); 311 role_assign($teacherrole->id, $user4->id, \context_coursecat::instance($cat1->id)); 312 role_assign($teacherrole->id, $user3->id, \context_coursecat::instance($cat2->id)); 313 role_assign($managerrole->id, $user3->id, \context_course::instance($course3->id)); 314 $this->enable_plugin(); 315 $result = enrol_category_sync_full($trace); 316 $this->assertSame(0, $result); 317 $this->assertEquals(5, $DB->count_records('user_enrolments', array())); 318 $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user2->id)); 319 $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4->id)); 320 $this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user3->id)); 321 $this->assertTrue(is_enrolled(\context_course::instance($course3->id), $user3->id)); 322 $this->assertTrue(is_enrolled(\context_course::instance($course4->id), $user3->id)); 323 324 // Cleanup everything. 325 326 $this->assertNotEmpty($DB->count_records('role_assignments', array())); 327 $this->assertNotEmpty($DB->count_records('user_enrolments', array())); 328 329 $this->disable_plugin(); 330 role_unassign_all(array('roleid'=>$studentrole->id)); 331 role_unassign_all(array('roleid'=>$managerrole->id)); 332 role_unassign_all(array('roleid'=>$teacherrole->id)); 333 334 $result = enrol_category_sync_full($trace); 335 $this->assertSame(2, $result); 336 $this->assertEquals(0, $DB->count_records('role_assignments', array())); 337 $this->assertNotEmpty($DB->count_records('user_enrolments', array())); 338 $this->disable_role_sync($studentrole->id); 339 $this->disable_role_sync($teacherrole->id); 340 341 $this->enable_plugin(); 342 $result = enrol_category_sync_full($trace); 343 $this->assertSame(0, $result); 344 $this->assertEquals(0, $DB->count_records('role_assignments', array())); 345 $this->assertEquals(0, $DB->count_records('user_enrolments', array())); 346 $this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'category'))); 347 } 348 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body