Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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