Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

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