[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/enrol/category/tests/ -> plugin_test.php (source)

   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          global $DB;
  46  
  47          $syscontext = context_system::instance();
  48  
  49          if ($rc = $DB->record_exists('role_capabilities', array('capability'=>'enrol/category:synchronised', 'roleid'=>$roleid, 'contextid'=>$syscontext->id))) {
  50              if ($rc->permission != CAP_ALLOW) {
  51                  $rc->permission = CAP_ALLOW;
  52                  $DB->update_record('role_capabilities', $rc);
  53              }
  54          } else {
  55              $rc = new stdClass();
  56              $rc->capability = 'enrol/category:synchronised';
  57              $rc->roleid = $roleid;
  58              $rc->contextid = $syscontext->id;
  59              $rc->permission = CAP_ALLOW;
  60              $rc->timemodified = time();
  61              $rc->modifierid = 0;
  62              $DB->insert_record('role_capabilities', $rc);
  63          }
  64      }
  65  
  66      protected function disable_role_sync($roleid) {
  67          global $DB;
  68  
  69          $syscontext = context_system::instance();
  70  
  71          $DB->delete_records('role_capabilities', array('capability'=>'enrol/category:synchronised', 'roleid'=>$roleid, 'contextid'=>$syscontext->id));
  72      }
  73  
  74      /**
  75       * Test utility methods used in syn test, fail here means something
  76       * in core accesslib was changed, but it is possible that only this test
  77       * is affected, nto the plugin itself...
  78       */
  79      public function test_utils() {
  80          global $DB;
  81  
  82          $this->resetAfterTest();
  83  
  84          $syscontext = context_system::instance();
  85  
  86          $this->assertFalse(enrol_is_enabled('category'));
  87          $this->enable_plugin();
  88          $this->assertTrue(enrol_is_enabled('category'));
  89  
  90          $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
  91          $this->assertEmpty($roles);
  92  
  93          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  94          $this->assertNotEmpty($studentrole);
  95  
  96          $this->enable_role_sync($studentrole->id);
  97          $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
  98          $this->assertEquals(1, count($roles));
  99          $this->assertEquals($studentrole, reset($roles));
 100  
 101          $this->disable_role_sync($studentrole->id);
 102          $roles = get_roles_with_capability('enrol/category:synchronised', CAP_ALLOW, $syscontext);
 103          $this->assertEmpty($roles);
 104      }
 105  
 106      public function test_handler_sync() {
 107          global $DB, $CFG;
 108          require_once($CFG->dirroot.'/enrol/category/locallib.php');
 109  
 110          $this->resetAfterTest();
 111  
 112          // Setup a few courses and categories.
 113  
 114          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 115          $this->assertNotEmpty($studentrole);
 116          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 117          $this->assertNotEmpty($teacherrole);
 118          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 119          $this->assertNotEmpty($managerrole);
 120  
 121          $cat1 = $this->getDataGenerator()->create_category();
 122          $cat2 = $this->getDataGenerator()->create_category();
 123          $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id));
 124  
 125          $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 126          $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 127          $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
 128          $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
 129  
 130          $user1 = $this->getDataGenerator()->create_user();
 131          $user2 = $this->getDataGenerator()->create_user();
 132          $user3 = $this->getDataGenerator()->create_user();
 133          $user4 = $this->getDataGenerator()->create_user();
 134  
 135          $this->enable_role_sync($studentrole->id);
 136          $this->enable_role_sync($teacherrole->id);
 137          $this->enable_plugin();
 138  
 139          $this->assertEquals(0, $DB->count_records('role_assignments', array()));
 140          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 141  
 142          // Test assign event.
 143  
 144          role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id));
 145          role_assign($managerrole->id, $user3->id, context_course::instance($course1->id));
 146          role_assign($managerrole->id, $user3->id, context_course::instance($course2->id));
 147          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 148  
 149          role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id));
 150          $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
 151          $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id));
 152          $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id));
 153          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 154  
 155          role_assign($managerrole->id, $user2->id, context_coursecat::instance($cat3->id));
 156          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 157  
 158          role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id));
 159          $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
 160          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 161  
 162          // Test role unassigned event.
 163  
 164          role_unassign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id)->id);
 165          $this->assertFalse(is_enrolled(context_course::instance($course1->id), $user4->id));
 166          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 167  
 168          // Make sure handlers are disabled when plugin disabled.
 169  
 170          $this->disable_plugin();
 171          role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id);
 172          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 173  
 174          role_assign($studentrole->id, $user3->id, context_coursecat::instance($cat1->id));
 175          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 176  
 177      }
 178  
 179      public function test_sync_course() {
 180          global $DB, $CFG;
 181          require_once($CFG->dirroot.'/enrol/category/locallib.php');
 182  
 183          $this->resetAfterTest();
 184  
 185          // Setup a few courses and categories.
 186  
 187          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 188          $this->assertNotEmpty($studentrole);
 189          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 190          $this->assertNotEmpty($teacherrole);
 191          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 192          $this->assertNotEmpty($managerrole);
 193  
 194          $cat1 = $this->getDataGenerator()->create_category();
 195          $cat2 = $this->getDataGenerator()->create_category();
 196          $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id));
 197  
 198          $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 199          $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 200          $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
 201          $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
 202  
 203          $user1 = $this->getDataGenerator()->create_user();
 204          $user2 = $this->getDataGenerator()->create_user();
 205          $user3 = $this->getDataGenerator()->create_user();
 206          $user4 = $this->getDataGenerator()->create_user();
 207  
 208          $this->enable_role_sync($studentrole->id);
 209          $this->enable_role_sync($teacherrole->id);
 210          $this->enable_plugin();
 211  
 212          $this->assertEquals(0, $DB->count_records('role_assignments', array()));
 213          role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id));
 214          role_assign($managerrole->id, $user3->id, context_course::instance($course1->id));
 215          role_assign($managerrole->id, $user3->id, context_course::instance($course2->id));
 216          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 217  
 218  
 219          $this->disable_plugin(); // Stops the event handlers.
 220          role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id));
 221          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 222          $this->enable_plugin();
 223          enrol_category_sync_course($course2);
 224          $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
 225          $this->assertFalse(is_enrolled(context_course::instance($course3->id), $user1->id));
 226          $this->assertFalse(is_enrolled(context_course::instance($course4->id), $user1->id));
 227          $this->assertEquals(1, $DB->count_records('user_enrolments', array()));
 228  
 229          enrol_category_sync_course($course2);
 230          enrol_category_sync_course($course3);
 231          enrol_category_sync_course($course4);
 232          $this->assertFalse(is_enrolled(context_course::instance($course1->id), $user1->id));
 233          $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
 234          $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id));
 235          $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id));
 236          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 237  
 238          $this->disable_plugin(); // Stops the event handlers.
 239          role_assign($studentrole->id, $user2->id, context_coursecat::instance($cat1->id));
 240          role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id));
 241          role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id);
 242          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 243          $this->enable_plugin();
 244          enrol_category_sync_course($course2);
 245          $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user1->id));
 246          $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user2->id));
 247          $this->assertFalse(is_enrolled(context_course::instance($course2->id), $user4->id));
 248          enrol_category_sync_course($course1);
 249          enrol_category_sync_course($course3);
 250          enrol_category_sync_course($course4);
 251          $this->assertEquals(2, $DB->count_records('user_enrolments', array()));
 252          $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user2->id));
 253          $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
 254  
 255          $this->disable_role_sync($studentrole->id);
 256          enrol_category_sync_course($course1);
 257          enrol_category_sync_course($course2);
 258          enrol_category_sync_course($course3);
 259          enrol_category_sync_course($course4);
 260          $this->assertEquals(1, $DB->count_records('user_enrolments', array()));
 261          $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
 262  
 263          $this->assertEquals(1, $DB->count_records('enrol', array('enrol'=>'category')));
 264          $this->disable_role_sync($teacherrole->id);
 265          enrol_category_sync_course($course1);
 266          enrol_category_sync_course($course2);
 267          enrol_category_sync_course($course3);
 268          enrol_category_sync_course($course4);
 269          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 270          $this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'category')));
 271      }
 272  
 273      public function test_sync_full() {
 274          global $DB, $CFG;
 275          require_once($CFG->dirroot.'/enrol/category/locallib.php');
 276  
 277          $this->resetAfterTest();
 278  
 279          $trace = new null_progress_trace();
 280  
 281          // Setup a few courses and categories.
 282  
 283          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 284          $this->assertNotEmpty($studentrole);
 285          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 286          $this->assertNotEmpty($teacherrole);
 287          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 288          $this->assertNotEmpty($managerrole);
 289  
 290          $cat1 = $this->getDataGenerator()->create_category();
 291          $cat2 = $this->getDataGenerator()->create_category();
 292          $cat3 = $this->getDataGenerator()->create_category(array('parent'=>$cat2->id));
 293  
 294          $course1 = $this->getDataGenerator()->create_course(array('category'=>$cat1->id));
 295          $course2 = $this->getDataGenerator()->create_course(array('category'=>$cat2->id));
 296          $course3 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
 297          $course4 = $this->getDataGenerator()->create_course(array('category'=>$cat3->id));
 298  
 299          $user1 = $this->getDataGenerator()->create_user();
 300          $user2 = $this->getDataGenerator()->create_user();
 301          $user3 = $this->getDataGenerator()->create_user();
 302          $user4 = $this->getDataGenerator()->create_user();
 303  
 304          $this->enable_role_sync($studentrole->id);
 305          $this->enable_role_sync($teacherrole->id);
 306          $this->enable_plugin();
 307  
 308          $this->assertEquals(0, $DB->count_records('role_assignments', array()));
 309          role_assign($managerrole->id, $user1->id, context_coursecat::instance($cat1->id));
 310          role_assign($managerrole->id, $user3->id, context_course::instance($course1->id));
 311          role_assign($managerrole->id, $user3->id, context_course::instance($course2->id));
 312          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 313  
 314          $result = enrol_category_sync_full($trace);
 315          $this->assertSame(0, $result);
 316  
 317          $this->disable_plugin();
 318          role_assign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id));
 319          $this->enable_plugin();
 320          $result = enrol_category_sync_full($trace);
 321          $this->assertSame(0, $result);
 322          $this->assertEquals(3, $DB->count_records('user_enrolments', array()));
 323          $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user1->id));
 324          $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user1->id));
 325          $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user1->id));
 326  
 327          $this->disable_plugin();
 328          role_unassign($studentrole->id, $user1->id, context_coursecat::instance($cat2->id)->id);
 329          role_assign($studentrole->id, $user2->id, context_coursecat::instance($cat1->id));
 330          role_assign($teacherrole->id, $user4->id, context_coursecat::instance($cat1->id));
 331          role_assign($teacherrole->id, $user3->id, context_coursecat::instance($cat2->id));
 332          role_assign($managerrole->id, $user3->id, context_course::instance($course3->id));
 333          $this->enable_plugin();
 334          $result = enrol_category_sync_full($trace);
 335          $this->assertSame(0, $result);
 336          $this->assertEquals(5, $DB->count_records('user_enrolments', array()));
 337          $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user2->id));
 338          $this->assertTrue(is_enrolled(context_course::instance($course1->id), $user4->id));
 339          $this->assertTrue(is_enrolled(context_course::instance($course2->id), $user3->id));
 340          $this->assertTrue(is_enrolled(context_course::instance($course3->id), $user3->id));
 341          $this->assertTrue(is_enrolled(context_course::instance($course4->id), $user3->id));
 342  
 343          // Cleanup everything.
 344  
 345          $this->assertNotEmpty($DB->count_records('role_assignments', array()));
 346          $this->assertNotEmpty($DB->count_records('user_enrolments', array()));
 347  
 348          $this->disable_plugin();
 349          role_unassign_all(array('roleid'=>$studentrole->id));
 350          role_unassign_all(array('roleid'=>$managerrole->id));
 351          role_unassign_all(array('roleid'=>$teacherrole->id));
 352  
 353          $result = enrol_category_sync_full($trace);
 354          $this->assertSame(2, $result);
 355          $this->assertEquals(0, $DB->count_records('role_assignments', array()));
 356          $this->assertNotEmpty($DB->count_records('user_enrolments', array()));
 357          $this->disable_role_sync($studentrole->id);
 358          $this->disable_role_sync($teacherrole->id);
 359  
 360          $this->enable_plugin();
 361          $result = enrol_category_sync_full($trace);
 362          $this->assertSame(0, $result);
 363          $this->assertEquals(0, $DB->count_records('role_assignments', array()));
 364          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
 365          $this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'category')));
 366      }
 367  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1