[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Cohort enrolment sync functional test. 19 * 20 * @package enrol_cohort 21 * @category phpunit 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 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'); 32 33 class enrol_cohort_testcase extends advanced_testcase { 34 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 } 41 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 } 48 49 public function test_handler_sync() { 50 global $DB; 51 52 $this->resetAfterTest(); 53 $trace = new null_progress_trace(); 54 55 // Setup a few courses and categories. 56 57 $cohortplugin = enrol_get_plugin('cohort'); 58 $manualplugin = enrol_get_plugin('manual'); 59 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); 66 67 $cat1 = $this->getDataGenerator()->create_category(); 68 $cat2 = $this->getDataGenerator()->create_category(); 69 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); 75 76 $user1 = $this->getDataGenerator()->create_user(); 77 $user2 = $this->getDataGenerator()->create_user(); 78 $user3 = $this->getDataGenerator()->create_user(); 79 $user4 = $this->getDataGenerator()->create_user(); 80 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(); 84 85 $this->enable_plugin(); 86 87 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); 88 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); 89 90 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 91 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 92 93 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id)); 94 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id)); 95 96 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id)); 97 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id)); 98 99 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id)); 100 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id)); 101 102 103 // Test cohort member add event. 104 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))); 116 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))); 124 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())); 128 129 // Test cohort remove action. 130 131 $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction')); 132 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 133 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))); 140 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))); 147 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))); 157 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))); 165 166 167 // Test cohort deleting. 168 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())); 174 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())); 179 180 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST); 181 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST); 182 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))); 187 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))); 194 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())); 198 199 // Test group sync. 200 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); 205 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)); 209 210 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 211 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 212 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)); 216 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))); 221 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))); 225 226 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL); 227 228 cohort_remove_member($cohort1->id, $user1->id); 229 $this->assertFalse(groups_is_member($group1->id, $user1->id)); 230 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)); 234 235 $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES); 236 cohort_add_member($cohort1->id, $user1->id); 237 238 cohort_remove_member($cohort1->id, $user1->id); 239 $this->assertTrue(groups_is_member($group1->id, $user1->id)); 240 241 242 // Test deleting of instances. 243 244 cohort_add_member($cohort1->id, $user1->id); 245 cohort_add_member($cohort1->id, $user2->id); 246 cohort_add_member($cohort1->id, $user3->id); 247 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))); 253 254 $cohortplugin->delete_instance($cohortinstance1); 255 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 } 262 263 public function test_sync_course() { 264 global $DB; 265 $this->resetAfterTest(); 266 267 $trace = new null_progress_trace(); 268 269 // Setup a few courses and categories. 270 271 $cohortplugin = enrol_get_plugin('cohort'); 272 $manualplugin = enrol_get_plugin('manual'); 273 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); 280 281 $cat1 = $this->getDataGenerator()->create_category(); 282 $cat2 = $this->getDataGenerator()->create_category(); 283 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); 289 290 $user1 = $this->getDataGenerator()->create_user(); 291 $user2 = $this->getDataGenerator()->create_user(); 292 $user3 = $this->getDataGenerator()->create_user(); 293 $user4 = $this->getDataGenerator()->create_user(); 294 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(); 298 299 $this->disable_plugin(); // Prevents event sync. 300 301 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); 302 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); 303 304 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 305 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 306 307 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id)); 308 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id)); 309 310 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id)); 311 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id)); 312 313 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id)); 314 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id)); 315 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); 321 322 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 323 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 324 325 326 // Test sync of one course only. 327 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())); 331 332 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! 339 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))); 351 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))); 358 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))); 368 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())); 375 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())); 380 381 382 // Test group sync. 383 384 $this->disable_plugin(); // No event sync. 385 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())); 389 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); 394 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)); 398 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)); 402 403 $this->enable_plugin(); // No event sync. 404 405 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 406 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 407 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); 412 413 enrol_cohort_sync($trace, $course1->id); 414 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())); 418 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))); 421 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))); 424 425 $cohortinstance1->customint2 = $group2->id; 426 $DB->update_record('enrol', $cohortinstance1); 427 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))); 432 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))); 437 438 cohort_remove_member($cohort1->id, $user1->id); 439 $this->assertFalse(groups_is_member($group1->id, $user1->id)); 440 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 } 445 446 public function test_sync_all_courses() { 447 global $DB; 448 449 $this->resetAfterTest(); 450 451 $trace = new null_progress_trace(); 452 453 // Setup a few courses and categories. 454 455 $cohortplugin = enrol_get_plugin('cohort'); 456 $manualplugin = enrol_get_plugin('manual'); 457 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); 464 465 $cat1 = $this->getDataGenerator()->create_category(); 466 $cat2 = $this->getDataGenerator()->create_category(); 467 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); 473 474 $user1 = $this->getDataGenerator()->create_user(); 475 $user2 = $this->getDataGenerator()->create_user(); 476 $user3 = $this->getDataGenerator()->create_user(); 477 $user4 = $this->getDataGenerator()->create_user(); 478 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(); 482 483 $this->disable_plugin(); // Prevents event sync. 484 485 $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id); 486 $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id); 487 488 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 489 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 490 491 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id)); 492 $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id)); 493 494 $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id)); 495 $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id)); 496 497 $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id)); 498 $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id)); 499 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); 505 506 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 507 $this->assertEquals(2, $DB->count_records('user_enrolments', array())); 508 509 510 // Test sync of one course only. 511 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())); 515 516 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))); 529 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))); 536 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))); 546 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())); 553 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())); 558 559 560 // Test group sync. 561 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())); 566 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); 573 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)); 577 578 $this->assertTrue(groups_add_member($group1, $user4)); 579 $this->assertTrue(groups_add_member($group2, $user4)); 580 581 $this->assertEquals(3, $DB->count_records('user_enrolments', array())); 582 $this->assertEquals(2, $DB->count_records('role_assignments', array())); 583 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); 589 590 $this->enable_plugin(); 591 592 enrol_cohort_sync($trace, null); 593 594 $this->assertEquals(8, $DB->count_records('user_enrolments', array())); 595 $this->assertEquals(8, $DB->count_records('role_assignments', array())); 596 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))); 599 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))); 603 604 $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3)); 605 $this->assertFalse(groups_is_member($group3->id, $user3->id)); 606 607 $cohortinstance1->customint2 = $group2->id; 608 $DB->update_record('enrol', $cohortinstance1); 609 $cohortinstance3->customint2 = $group3->id; 610 $DB->update_record('enrol', $cohortinstance3); 611 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))); 616 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))); 621 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))); 624 625 cohort_remove_member($cohort1->id, $user1->id); 626 $this->assertFalse(groups_is_member($group1->id, $user1->id)); 627 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 } 632 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |