Search moodle.org's
Developer Documentation


  • Bug fixes for general core bugs in 2.8.x ended 9 November 2015 (12 months).
  • Bug fixes for security issues in 2.8.x ended 9 May 2016 (18 months).
  • minimum PHP 5.4.4 (always use latest PHP 5.4.x or 5.5.x on Windows - http://windows.php.net/download/), PHP 7 is NOT supported
  • Differences Between: [Versions 28 and 31] [Versions 28 and 32] [Versions 28 and 33] [Versions 28 and 34] [Versions 28 and 35] [Versions 28 and 36] [Versions 28 and 37]

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

    Search This Site: