Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 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_cohort;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot.'/enrol/cohort/locallib.php');
  23  require_once($CFG->dirroot.'/cohort/lib.php');
  24  require_once($CFG->dirroot.'/group/lib.php');
  25  
  26  /**
  27   * Cohort enrolment sync functional test.
  28   *
  29   * @package    enrol_cohort
  30   * @category   test
  31   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  class sync_test extends \advanced_testcase {
  35  
  36      protected function enable_plugin() {
  37          $enabled = enrol_get_plugins(true);
  38          $enabled['cohort'] = true;
  39          $enabled = array_keys($enabled);
  40          set_config('enrol_plugins_enabled', implode(',', $enabled));
  41      }
  42  
  43      protected function disable_plugin() {
  44          $enabled = enrol_get_plugins(true);
  45          unset($enabled['cohort']);
  46          $enabled = array_keys($enabled);
  47          set_config('enrol_plugins_enabled', implode(',', $enabled));
  48      }
  49  
  50      public function test_handler_sync() {
  51          global $DB;
  52  
  53          $this->resetAfterTest();
  54          $trace = new \null_progress_trace();
  55  
  56          // Setup a few courses and categories.
  57  
  58          $cohortplugin = enrol_get_plugin('cohort');
  59          $manualplugin = enrol_get_plugin('manual');
  60  
  61          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  62          $this->assertNotEmpty($studentrole);
  63          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
  64          $this->assertNotEmpty($teacherrole);
  65          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
  66          $this->assertNotEmpty($managerrole);
  67  
  68          $cat1 = $this->getDataGenerator()->create_category();
  69          $cat2 = $this->getDataGenerator()->create_category();
  70  
  71          $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
  72          $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
  73          $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
  74          $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
  75          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  76  
  77          $user1 = $this->getDataGenerator()->create_user();
  78          $user2 = $this->getDataGenerator()->create_user();
  79          $user3 = $this->getDataGenerator()->create_user();
  80          $user4 = $this->getDataGenerator()->create_user();
  81          $user5 = $this->getDataGenerator()->create_user();
  82  
  83          $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
  84          $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat2->id)->id));
  85          $cohort3 = $this->getDataGenerator()->create_cohort();
  86          $cohort4 = $this->getDataGenerator()->create_cohort();
  87  
  88          $this->enable_plugin();
  89  
  90          $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
  91          $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
  92  
  93          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
  94          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
  95  
  96          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
  97          $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
  98  
  99          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
 100          $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
 101  
 102          $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
 103          $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
 104  
 105          $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
 106          $cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
 107  
 108          $id = $cohortplugin->add_instance($course3, array('customint1' => $cohort4->id, 'roleid' => $studentrole->id));
 109          $cohortinstance5 = $DB->get_record('enrol', array('id' => $id));
 110  
 111          // Test cohort member add event.
 112  
 113          cohort_add_member($cohort1->id, $user1->id);
 114          cohort_add_member($cohort1->id, $user2->id);
 115          cohort_add_member($cohort1->id, $user4->id);
 116          $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
 117          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
 118          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
 119          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
 120          $this->assertEquals(5, $DB->count_records('role_assignments', array()));
 121          $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)));
 122          $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)));
 123          $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)));
 124  
 125          cohort_add_member($cohort2->id, $user3->id);
 126          $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
 127          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
 128          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
 129          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->id, 'userid' => $user3->id)));
 130          $this->assertEquals(7, $DB->count_records('role_assignments', array()));
 131          $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)));
 132          $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)));
 133  
 134          cohort_add_member($cohort3->id, $user3->id);
 135          cohort_add_member($cohort4->id, $user5->id);
 136          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 137          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 138  
 139          // Test cohort remove action.
 140  
 141          $this->assertEquals(ENROL_EXT_REMOVED_UNENROL, $cohortplugin->get_config('unenrolaction'));
 142          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 143  
 144          cohort_remove_member($cohort1->id, $user2->id);
 145          cohort_remove_member($cohort1->id, $user4->id);
 146          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 147          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 148          $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)));
 149          $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)));
 150          $this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
 151              array('userid' => $user2->id, 'enrolid' => $cohortinstance1->id)));
 152          $this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
 153              array('userid' => $user4->id, 'enrolid' => $cohortinstance1->id)));
 154  
 155          cohort_add_member($cohort1->id, $user2->id);
 156          cohort_add_member($cohort1->id, $user4->id);
 157          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 158          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 159          $this->assertTrue($DB->record_exists('role_assignments', array(
 160              'contextid' => \context_course::instance($course1->id)->id,
 161              'userid' => $user2->id, 'roleid' => $studentrole->id,
 162              'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 163          $this->assertTrue($DB->record_exists('role_assignments', array(
 164              'contextid' => \context_course::instance($course1->id)->id,
 165              'userid' => $user4->id, 'roleid' => $studentrole->id,
 166              'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 167  
 168          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 169          cohort_remove_member($cohort1->id, $user2->id);
 170          cohort_remove_member($cohort1->id, $user4->id);
 171          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 172          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 173          $this->assertTrue($DB->record_exists('role_assignments', array(
 174              'contextid' => \context_course::instance($course1->id)->id,
 175              'userid' => $user2->id, 'roleid' => $studentrole->id,
 176              'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 177          $this->assertTrue($DB->record_exists('role_assignments', array(
 178              'contextid' => \context_course::instance($course1->id)->id,
 179              'userid' => $user4->id, 'roleid' => $studentrole->id,
 180              'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 181          $this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
 182              array('userid' => $user2->id, 'enrolid' => $cohortinstance1->id)));
 183          $this->assertEquals(ENROL_USER_SUSPENDED, $DB->get_field('user_enrolments', 'status',
 184              array('userid' => $user4->id, 'enrolid' => $cohortinstance1->id)));
 185  
 186          cohort_add_member($cohort1->id, $user2->id);
 187          cohort_add_member($cohort1->id, $user4->id);
 188          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 189          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 190          $this->assertTrue($DB->record_exists('role_assignments', array(
 191              'contextid' => \context_course::instance($course1->id)->id,
 192              'userid' => $user2->id, 'roleid' => $studentrole->id,
 193              'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 194          $this->assertTrue($DB->record_exists('role_assignments', array(
 195              'contextid' => \context_course::instance($course1->id)->id,
 196              'userid' => $user4->id, 'roleid' => $studentrole->id,
 197              'component' => 'enrol_cohort', 'itemid' => $cohortinstance1->id)));
 198  
 199          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 200          cohort_remove_member($cohort1->id, $user2->id);
 201          cohort_remove_member($cohort1->id, $user4->id);
 202          $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
 203          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
 204          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
 205          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 206          $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)));
 207          $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)));
 208  
 209          cohort_remove_member($cohort2->id, $user3->id);
 210          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 211          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
 212          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance3->id, 'userid'=>$user3->id)));
 213          $this->assertEquals(4, $DB->count_records('role_assignments', array()));
 214          $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)));
 215          $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)));
 216  
 217  
 218          // Test cohort deleting.
 219  
 220          cohort_add_member($cohort1->id, $user2->id);
 221          cohort_add_member($cohort1->id, $user4->id);
 222          cohort_add_member($cohort2->id, $user3->id);
 223          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 224          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 225  
 226          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 227          cohort_delete_cohort($cohort4);
 228          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 229          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 230          $cohortinstance5 = $DB->get_record('enrol', array('id' => $cohortinstance5->id), '*', MUST_EXIST);
 231          $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance5->status);
 232  
 233          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 234          cohort_delete_cohort($cohort2);
 235          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 236          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 237  
 238          $cohortinstance2 = $DB->get_record('enrol', array('id'=>$cohortinstance2->id), '*', MUST_EXIST);
 239          $cohortinstance3 = $DB->get_record('enrol', array('id'=>$cohortinstance3->id), '*', MUST_EXIST);
 240  
 241          $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance2->status);
 242          $this->assertEquals(ENROL_INSTANCE_DISABLED, $cohortinstance3->status);
 243          $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance2->id)));
 244          $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
 245  
 246          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 247          cohort_delete_cohort($cohort1);
 248          $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
 249          $this->assertEquals(3, $DB->count_records('role_assignments', array()));
 250          $this->assertFalse($DB->record_exists('enrol', array('id'=>$cohortinstance1->id)));
 251          $this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 252  
 253          // Cleanup after previous test (remove the extra user_enrolment).
 254          enrol_cohort_sync($trace, $course1->id);
 255          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 256  
 257          // Test group sync.
 258  
 259          $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
 260          $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 261          $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
 262          $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 263  
 264          $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
 265          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
 266          $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
 267  
 268          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 269          $this->assertEquals(3, $DB->count_records('role_assignments', array()));
 270  
 271          $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4));
 272          $this->assertTrue(groups_add_member($group1, $user4));
 273          $this->assertTrue(groups_add_member($group2, $user4));
 274  
 275          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 276          cohort_add_member($cohort1->id, $user1->id);
 277          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 278          $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 279  
 280          cohort_add_member($cohort1->id, $user4->id);
 281          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 282          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 283  
 284          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 285  
 286          cohort_remove_member($cohort1->id, $user1->id);
 287          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 288  
 289          cohort_remove_member($cohort1->id, $user4->id);
 290          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 291          $this->assertTrue(groups_is_member($group2->id, $user4->id));
 292  
 293          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 294          cohort_add_member($cohort1->id, $user1->id);
 295  
 296          cohort_remove_member($cohort1->id, $user1->id);
 297          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 298  
 299          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 300          cohort_add_member($cohort1->id, $user1->id);
 301  
 302          cohort_remove_member($cohort1->id, $user1->id);
 303          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 304  
 305          // Test deleting of instances.
 306  
 307          cohort_add_member($cohort1->id, $user1->id);
 308          cohort_add_member($cohort1->id, $user2->id);
 309          cohort_add_member($cohort1->id, $user3->id);
 310  
 311          $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
 312          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 313          $this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 314          $this->assertEquals(5, $DB->count_records('groups_members', array()));
 315          $this->assertEquals(3, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 316  
 317          $cohortplugin->delete_instance($cohortinstance1);
 318  
 319          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 320          $this->assertEquals(3, $DB->count_records('role_assignments', array()));
 321          $this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 322          $this->assertEquals(2, $DB->count_records('groups_members', array()));
 323          $this->assertEquals(0, $DB->count_records('groups_members', array('component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 324      }
 325  
 326      public function test_sync_course() {
 327          global $DB;
 328          $this->resetAfterTest();
 329  
 330          $trace = new \null_progress_trace();
 331  
 332          // Setup a few courses and categories.
 333  
 334          $cohortplugin = enrol_get_plugin('cohort');
 335          $manualplugin = enrol_get_plugin('manual');
 336  
 337          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 338          $this->assertNotEmpty($studentrole);
 339          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 340          $this->assertNotEmpty($teacherrole);
 341          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 342          $this->assertNotEmpty($managerrole);
 343  
 344          $cat1 = $this->getDataGenerator()->create_category();
 345          $cat2 = $this->getDataGenerator()->create_category();
 346  
 347          $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 348          $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 349          $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 350          $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 351          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 352  
 353          $user1 = $this->getDataGenerator()->create_user();
 354          $user2 = $this->getDataGenerator()->create_user();
 355          $user3 = $this->getDataGenerator()->create_user();
 356          $user4 = $this->getDataGenerator()->create_user();
 357          $user5 = $this->getDataGenerator()->create_user();
 358  
 359          $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
 360          $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat2->id)->id));
 361          $cohort3 = $this->getDataGenerator()->create_cohort();
 362          $cohort4 = $this->getDataGenerator()->create_cohort();
 363  
 364          $this->disable_plugin(); // Prevents event sync.
 365  
 366          $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
 367          $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
 368  
 369          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 370          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 371  
 372          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
 373          $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
 374  
 375          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
 376          $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
 377  
 378          $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
 379          $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
 380  
 381          $id = $cohortplugin->add_instance($course2, array('customint1' => $cohort2->id, 'roleid' => $studentrole->id, 'status' => ENROL_INSTANCE_DISABLED));
 382          $cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
 383  
 384          $id = $cohortplugin->add_instance($course3, array('customint1' => $cohort4->id, 'roleid' => $studentrole->id));
 385          $cohortinstance5 = $DB->get_record('enrol', array('id' => $id));
 386  
 387          cohort_add_member($cohort1->id, $user1->id);
 388          cohort_add_member($cohort1->id, $user2->id);
 389          cohort_add_member($cohort1->id, $user4->id);
 390          cohort_add_member($cohort2->id, $user3->id);
 391          cohort_add_member($cohort3->id, $user3->id);
 392          cohort_add_member($cohort4->id, $user5->id);
 393  
 394          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 395          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 396  
 397  
 398          // Test sync of one course only.
 399  
 400          enrol_cohort_sync($trace, $course1->id);
 401          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 402          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 403  
 404  
 405          $this->enable_plugin();
 406          enrol_cohort_sync($trace, $course2->id);
 407          $this->assertEquals(3, $DB->count_records('role_assignments', array()));
 408          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 409          $DB->delete_records('cohort_members', array('cohortid'=>$cohort3->id)); // Use low level DB api to prevent events!
 410          $DB->delete_records('cohort', array('id'=>$cohort3->id)); // Use low level DB api to prevent events!
 411  
 412          enrol_cohort_sync($trace, $course1->id);
 413          $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
 414          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
 415          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
 416          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
 417          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
 418          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $cohortinstance4->id, 'userid' => $user3->id)));
 419          $this->assertEquals(7, $DB->count_records('role_assignments', array()));
 420          $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)));
 421          $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)));
 422          $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)));
 423          $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)));
 424  
 425          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 426          // Use low level DB api to prevent events!
 427          $DB->delete_records('cohort_members', array('cohortid' => $cohort2->id, 'userid' => $user3->id));
 428          enrol_cohort_sync($trace, $course1->id);
 429          $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
 430          $this->assertEquals(7, $DB->count_records('role_assignments', array()));
 431          $this->assertTrue($DB->record_exists('role_assignments', array(
 432              'contextid' => \context_course::instance($course1->id)->id,
 433              'userid' => $user3->id, 'roleid' => $teacherrole->id,
 434              'component' => 'enrol_cohort', 'itemid' => $cohortinstance2->id)));
 435  
 436          cohort_add_member($cohort2->id, $user3->id);
 437          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 438          $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
 439          enrol_cohort_sync($trace, $course1->id);
 440          $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
 441          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 442          $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)));
 443  
 444          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 445          $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
 446          enrol_cohort_sync($trace, $course1->id);
 447          $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
 448          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
 449          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
 450          $this->assertEquals(5, $DB->count_records('role_assignments', array()));
 451          $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)));
 452          $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)));
 453  
 454          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 455          $DB->delete_records('cohort_members', array('cohortid' => $cohort4->id));
 456          $DB->delete_records('cohort', array('id' => $cohort4->id));
 457          enrol_cohort_sync($trace, $course3->id);
 458          $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
 459          $this->assertEquals(5, $DB->count_records('role_assignments', array()));
 460  
 461          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 462          $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id)); // Use low level DB api to prevent events!
 463          $DB->delete_records('cohort', array('id'=>$cohort1->id)); // Use low level DB api to prevent events!
 464          enrol_cohort_sync($trace, $course1->id);
 465          $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
 466          $this->assertEquals(3, $DB->count_records('role_assignments', array()));
 467  
 468          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 469          enrol_cohort_sync($trace, $course1->id);
 470          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 471          $this->assertEquals(3, $DB->count_records('role_assignments', array()));
 472  
 473  
 474          // Test group sync.
 475  
 476          $this->disable_plugin(); // No event sync.
 477  
 478          // Trigger sync to remove left over role assignments.
 479          enrol_cohort_sync($trace, $course1->id);
 480          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 481  
 482          $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
 483          $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 484          $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
 485          $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 486  
 487          $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
 488          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
 489          $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
 490  
 491          $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4));
 492          $this->assertTrue(groups_add_member($group1, $user4));
 493          $this->assertTrue(groups_add_member($group2, $user4));
 494  
 495          $this->enable_plugin(); // No event sync.
 496  
 497          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 498          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 499  
 500          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 501          cohort_add_member($cohort1->id, $user1->id);
 502          cohort_add_member($cohort1->id, $user4->id);
 503          cohort_add_member($cohort2->id, $user4->id);
 504  
 505          enrol_cohort_sync($trace, $course1->id);
 506  
 507          $this->assertEquals(7, $DB->count_records('user_enrolments', array()));
 508          // This used to be 7 - but now add_instance triggers an immediate sync.
 509          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 510  
 511          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 512          $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 513  
 514          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 515          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 516  
 517          $cohortinstance1->customint2 = $group2->id;
 518          $DB->update_record('enrol', $cohortinstance1);
 519  
 520          enrol_cohort_sync($trace, $course1->id);
 521          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 522          $this->assertTrue(groups_is_member($group2->id, $user1->id));
 523          $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 524  
 525          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 526          $this->assertTrue(groups_is_member($group2->id, $user4->id));
 527          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 528          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 529  
 530          cohort_remove_member($cohort1->id, $user1->id);
 531          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 532  
 533          cohort_remove_member($cohort1->id, $user4->id);
 534          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 535          $this->assertTrue(groups_is_member($group2->id, $user4->id));
 536      }
 537  
 538      public function test_sync_all_courses() {
 539          global $DB;
 540  
 541          $this->resetAfterTest();
 542  
 543          $trace = new \null_progress_trace();
 544  
 545          // Setup a few courses and categories.
 546  
 547          $cohortplugin = enrol_get_plugin('cohort');
 548          $manualplugin = enrol_get_plugin('manual');
 549  
 550          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 551          $this->assertNotEmpty($studentrole);
 552          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 553          $this->assertNotEmpty($teacherrole);
 554          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 555          $this->assertNotEmpty($managerrole);
 556  
 557          $cat1 = $this->getDataGenerator()->create_category();
 558          $cat2 = $this->getDataGenerator()->create_category();
 559  
 560          $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 561          $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 562          $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 563          $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 564          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 565  
 566          $user1 = $this->getDataGenerator()->create_user();
 567          $user2 = $this->getDataGenerator()->create_user();
 568          $user3 = $this->getDataGenerator()->create_user();
 569          $user4 = $this->getDataGenerator()->create_user();
 570          $user5 = $this->getDataGenerator()->create_user();
 571  
 572          $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
 573          $cohort2 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat2->id)->id));
 574          $cohort3 = $this->getDataGenerator()->create_cohort();
 575          $cohort4 = $this->getDataGenerator()->create_cohort();
 576  
 577          $this->disable_plugin(); // Prevents event sync.
 578  
 579          $manualplugin->enrol_user($maninstance1, $user4->id, $teacherrole->id);
 580          $manualplugin->enrol_user($maninstance1, $user3->id, $managerrole->id);
 581  
 582          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 583          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 584  
 585          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id));
 586          $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
 587  
 588          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort2->id, 'roleid'=>$teacherrole->id));
 589          $cohortinstance2 = $DB->get_record('enrol', array('id'=>$id));
 590  
 591          $id = $cohortplugin->add_instance($course2, array('customint1'=>$cohort2->id, 'roleid'=>$studentrole->id));
 592          $cohortinstance3 = $DB->get_record('enrol', array('id'=>$id));
 593  
 594          $id = $cohortplugin->add_instance($course3, array('customint1' => $cohort4->id, 'roleid' => $studentrole->id));
 595          $cohortinstance4 = $DB->get_record('enrol', array('id' => $id));
 596  
 597          cohort_add_member($cohort1->id, $user1->id);
 598          cohort_add_member($cohort1->id, $user2->id);
 599          cohort_add_member($cohort1->id, $user4->id);
 600          cohort_add_member($cohort2->id, $user3->id);
 601          cohort_add_member($cohort3->id, $user3->id);
 602          cohort_add_member($cohort4->id, $user5->id);
 603  
 604          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 605          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 606  
 607  
 608          // Test sync of one course only.
 609  
 610          enrol_cohort_sync($trace, null);
 611          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 612          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 613  
 614  
 615          $this->enable_plugin();
 616          enrol_cohort_sync($trace, null);
 617          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 618          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
 619          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user2->id)));
 620          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user4->id)));
 621          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
 622          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 623          $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)));
 624          $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)));
 625          $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)));
 626          $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)));
 627  
 628          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 629          $DB->delete_records('cohort_members', array('cohortid' => $cohort2->id, 'userid' => $user3->id));
 630          enrol_cohort_sync($trace, $course1->id);
 631          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 632          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 633          $this->assertTrue($DB->record_exists('role_assignments', array(
 634              'contextid' => \context_course::instance($course1->id)->id,
 635              'userid' => $user3->id, 'roleid' => $teacherrole->id,
 636              'component' => 'enrol_cohort', 'itemid' => $cohortinstance2->id)));
 637  
 638          cohort_add_member($cohort2->id, $user3->id);
 639  
 640          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 641          $DB->delete_records('cohort_members', array('cohortid'=>$cohort2->id, 'userid'=>$user3->id)); // Use low level DB api to prevent events!
 642          enrol_cohort_sync($trace, $course1->id);
 643          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 644          $this->assertEquals(7, $DB->count_records('role_assignments', array()));
 645          $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)));
 646  
 647          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 648          $DB->delete_records('cohort_members', array('cohortid'=>$cohort1->id, 'userid'=>$user1->id)); // Use low level DB api to prevent events!
 649          enrol_cohort_sync($trace, $course1->id);
 650          $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
 651          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance2->id, 'userid'=>$user3->id)));
 652          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$cohortinstance1->id, 'userid'=>$user1->id)));
 653          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 654          $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)));
 655          $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)));
 656  
 657          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
 658          $DB->delete_records('cohort_members', array('cohortid' => $cohort4->id)); // Use low level DB api to prevent events!
 659          $DB->delete_records('cohort', array('id' => $cohort4->id)); // Use low level DB api to prevent events!
 660          enrol_cohort_sync($trace, $course3->id);
 661          $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
 662          $this->assertEquals(6, $DB->count_records('role_assignments', array()));
 663  
 664          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 665          $DB->delete_records('cohort_members', array('cohortid' => $cohort1->id)); // Use low level DB api to prevent events!
 666          $DB->delete_records('cohort', array('id' => $cohort1->id)); // Use low level DB api to prevent events!
 667          enrol_cohort_sync($trace, $course1->id);
 668          $this->assertEquals(6, $DB->count_records('user_enrolments', array()));
 669          $this->assertEquals(4, $DB->count_records('role_assignments', array()));
 670  
 671          $cohortplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 672          enrol_cohort_sync($trace, $course1->id);
 673          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 674          $this->assertEquals(4, $DB->count_records('role_assignments', array()));
 675  
 676  
 677          // Test group sync.
 678  
 679          $this->disable_plugin(); // No event sync
 680          // Trigger sync to remove extra role assignments.
 681          enrol_cohort_sync($trace, $course1->id);
 682          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 683  
 684          $id = groups_create_group((object)array('name'=>'Group 1', 'courseid'=>$course1->id));
 685          $group1 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 686          $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course1->id));
 687          $group2 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 688          $id = groups_create_group((object)array('name'=>'Group 2', 'courseid'=>$course2->id));
 689          $group3 = $DB->get_record('groups', array('id'=>$id), '*', MUST_EXIST);
 690  
 691          $cohort1 = $this->getDataGenerator()->create_cohort(array('contextid'=>\context_coursecat::instance($cat1->id)->id));
 692          $id = $cohortplugin->add_instance($course1, array('customint1'=>$cohort1->id, 'roleid'=>$studentrole->id, 'customint2'=>$group1->id));
 693          $cohortinstance1 = $DB->get_record('enrol', array('id'=>$id));
 694  
 695          $this->assertTrue(groups_add_member($group1, $user4));
 696          $this->assertTrue(groups_add_member($group2, $user4));
 697  
 698          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 699          $this->assertEquals(2, $DB->count_records('role_assignments', array()));
 700  
 701          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 702          cohort_add_member($cohort1->id, $user1->id);
 703          cohort_add_member($cohort1->id, $user4->id);
 704          cohort_add_member($cohort2->id, $user4->id);
 705          cohort_add_member($cohort2->id, $user3->id);
 706  
 707          $this->enable_plugin();
 708  
 709          enrol_cohort_sync($trace, null);
 710  
 711          $this->assertEquals(8, $DB->count_records('user_enrolments', array()));
 712          $this->assertEquals(8, $DB->count_records('role_assignments', array()));
 713  
 714          $this->assertTrue(groups_is_member($group1->id, $user1->id));
 715          $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 716  
 717          $this->assertTrue(is_enrolled(\context_course::instance($course1->id), $user4));
 718          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 719          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 720  
 721          $this->assertTrue(is_enrolled(\context_course::instance($course2->id), $user3));
 722          $this->assertFalse(groups_is_member($group3->id, $user3->id));
 723  
 724          $cohortinstance1->customint2 = $group2->id;
 725          $DB->update_record('enrol', $cohortinstance1);
 726          $cohortinstance3->customint2 = $group3->id;
 727          $DB->update_record('enrol', $cohortinstance3);
 728  
 729          enrol_cohort_sync($trace, null);
 730          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 731          $this->assertTrue(groups_is_member($group2->id, $user1->id));
 732          $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user1->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 733  
 734          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 735          $this->assertTrue(groups_is_member($group2->id, $user4->id));
 736          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group1->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 737          $this->assertFalse($DB->record_exists('groups_members', array('groupid'=>$group2->id, 'userid'=>$user4->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance1->id)));
 738  
 739          $this->assertTrue(groups_is_member($group3->id, $user3->id));
 740          $this->assertTrue($DB->record_exists('groups_members', array('groupid'=>$group3->id, 'userid'=>$user3->id, 'component'=>'enrol_cohort', 'itemid'=>$cohortinstance3->id)));
 741  
 742          cohort_remove_member($cohort1->id, $user1->id);
 743          $this->assertFalse(groups_is_member($group1->id, $user1->id));
 744  
 745          cohort_remove_member($cohort1->id, $user4->id);
 746          $this->assertTrue(groups_is_member($group1->id, $user4->id));
 747          $this->assertTrue(groups_is_member($group2->id, $user4->id));
 748      }
 749  }