Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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

   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   * Manual enrolment tests.
  19   *
  20   * @package    enrol_manual
  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  namespace enrol_manual;
  26  
  27  use course_enrolment_manager;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  
  32  /**
  33   * Manual enrolment tests.
  34   *
  35   * @package    enrol_manual
  36   * @category   phpunit
  37   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class lib_test extends \advanced_testcase {
  41      /**
  42       * Test enrol migration function used when uninstalling enrol plugins.
  43       */
  44      public function test_migrate_plugin_enrolments() {
  45          global $DB, $CFG;
  46          require_once($CFG->dirroot.'/enrol/manual/locallib.php');
  47  
  48          $this->resetAfterTest();
  49  
  50          /** @var $manplugin enrol_manual_plugin */
  51          $manplugin = enrol_get_plugin('manual');
  52  
  53          // Setup a few courses and users.
  54  
  55          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  56          $this->assertNotEmpty($studentrole);
  57          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
  58          $this->assertNotEmpty($teacherrole);
  59  
  60          $course1 = $this->getDataGenerator()->create_course();
  61          $course2 = $this->getDataGenerator()->create_course();
  62          $course3 = $this->getDataGenerator()->create_course();
  63          $course4 = $this->getDataGenerator()->create_course();
  64          $course5 = $this->getDataGenerator()->create_course();
  65  
  66          $context1 = \context_course::instance($course1->id);
  67          $context2 = \context_course::instance($course2->id);
  68          $context3 = \context_course::instance($course3->id);
  69          $context4 = \context_course::instance($course4->id);
  70  
  71          $user1 = $this->getDataGenerator()->create_user();
  72          $user2 = $this->getDataGenerator()->create_user();
  73          $user3 = $this->getDataGenerator()->create_user();
  74          $user4 = $this->getDataGenerator()->create_user();
  75  
  76          // We expect manual, self and guest instances to be created by default.
  77  
  78          $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'manual')));
  79          $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'self')));
  80          $this->assertEquals(5, $DB->count_records('enrol', array('enrol'=>'guest')));
  81          $this->assertEquals(15, $DB->count_records('enrol', array()));
  82  
  83          $this->assertEquals(0, $DB->count_records('user_enrolments', array()));
  84  
  85          // Enrol some users to manual instances.
  86  
  87          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  88          $DB->set_field('enrol', 'status', ENROL_INSTANCE_DISABLED, array('id'=>$maninstance1->id));
  89          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  90          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
  91          $DB->delete_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'));
  92          $DB->delete_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'));
  93          $DB->delete_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual'));
  94  
  95          $manplugin->enrol_user($maninstance1, $user1->id, $studentrole->id);
  96          $manplugin->enrol_user($maninstance1, $user2->id, $studentrole->id);
  97          $manplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id);
  98          $manplugin->enrol_user($maninstance2, $user3->id, $teacherrole->id);
  99  
 100          $this->assertEquals(4, $DB->count_records('user_enrolments', array()));
 101  
 102          // Set up some bogus enrol plugin instances and enrolments.
 103  
 104          $xxxinstance1 = $DB->insert_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED));
 105          $xxxinstance1 = $DB->get_record('enrol', array('id'=>$xxxinstance1));
 106          $xxxinstance3 = $DB->insert_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED));
 107          $xxxinstance3 = $DB->get_record('enrol', array('id'=>$xxxinstance3));
 108          $xxxinstance4 = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_ENABLED));
 109          $xxxinstance4 = $DB->get_record('enrol', array('id'=>$xxxinstance4));
 110          $xxxinstance4b = $DB->insert_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx', 'status'=>ENROL_INSTANCE_DISABLED));
 111          $xxxinstance4b = $DB->get_record('enrol', array('id'=>$xxxinstance4b));
 112  
 113  
 114          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED));
 115          role_assign($studentrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
 116          role_assign($teacherrole->id, $user1->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
 117          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE));
 118          role_assign($studentrole->id, $user4->id, $context1->id, 'enrol_xxx', $xxxinstance1->id);
 119          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id)));
 120          $this->assertEquals(6, $DB->count_records('role_assignments', array('contextid'=>$context1->id)));
 121  
 122  
 123          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE));
 124          role_assign($studentrole->id, $user1->id, $context3->id, 'enrol_xxx', $xxxinstance3->id);
 125          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED));
 126          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id)));
 127          $this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id)));
 128  
 129          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE));
 130          role_assign($studentrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4->id);
 131          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE));
 132          role_assign($studentrole->id, $user2->id, $context4->id, 'enrol_xxx', $xxxinstance4->id);
 133          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_SUSPENDED));
 134          role_assign($teacherrole->id, $user1->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id);
 135          $DB->insert_record('user_enrolments', array('enrolid'=>$xxxinstance4b->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE));
 136          role_assign($teacherrole->id, $user4->id, $context4->id, 'enrol_xxx', $xxxinstance4b->id);
 137          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id)));
 138          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id)));
 139          $this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id)));
 140  
 141          // Finally do the migration.
 142  
 143          enrol_manual_migrate_plugin_enrolments('xxx');
 144  
 145          // Verify results.
 146  
 147          $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual')));
 148          $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course1->id, 'enrol'=>'xxx')));
 149          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 150          $this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance1->status);
 151          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
 152          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)));
 153          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
 154          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_ACTIVE)));
 155          $this->assertEquals(4, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance1->id)));
 156          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance1->id)));
 157          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
 158          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id)));
 159          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
 160          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user3->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context1->id)));
 161          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'contextid'=>$context1->id)));
 162          $this->assertEquals(5, $DB->count_records('role_assignments', array('contextid'=>$context1->id)));
 163  
 164  
 165          $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual')));
 166          $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course2->id, 'enrol'=>'xxx')));
 167          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 168          $this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance2->status);
 169  
 170  
 171          $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual')));
 172          $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course3->id, 'enrol'=>'xxx')));
 173          $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 174          $this->assertEquals(ENROL_INSTANCE_DISABLED, $maninstance3->status);
 175          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
 176          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance3->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_SUSPENDED)));
 177          $this->assertEquals(2, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance3->id)));
 178          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance3->id)));
 179          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context3->id)));
 180          $this->assertEquals(1, $DB->count_records('role_assignments', array('contextid'=>$context3->id)));
 181  
 182  
 183          $this->assertEquals(1, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual')));
 184          $this->assertEquals(2, $DB->count_records('enrol', array('courseid'=>$course4->id, 'enrol'=>'xxx')));
 185          $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 186          $this->assertEquals(ENROL_INSTANCE_ENABLED, $maninstance4->status);
 187          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user1->id, 'status'=>ENROL_USER_ACTIVE)));
 188          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user2->id, 'status'=>ENROL_USER_ACTIVE)));
 189          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance4->id, 'userid'=>$user4->id, 'status'=>ENROL_USER_SUSPENDED)));
 190          $this->assertEquals(3, $DB->count_records('user_enrolments', array('enrolid'=>$maninstance4->id)));
 191          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4->id)));
 192          $this->assertEquals(0, $DB->count_records('user_enrolments', array('enrolid'=>$xxxinstance4b->id)));
 193          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id)));
 194          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user1->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id)));
 195          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user2->id, 'roleid'=>$studentrole->id, 'contextid'=>$context4->id)));
 196          $this->assertTrue($DB->record_exists('role_assignments', array('itemid'=>0, 'component'=>'', 'userid'=>$user4->id, 'roleid'=>$teacherrole->id, 'contextid'=>$context4->id)));
 197          $this->assertEquals(4, $DB->count_records('role_assignments', array('contextid'=>$context4->id)));
 198  
 199  
 200          $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'manual')));
 201          $this->assertEquals(0, $DB->count_records('enrol', array('courseid'=>$course5->id, 'enrol'=>'xxx')));
 202  
 203          // Make sure wrong params do not produce errors or notices.
 204  
 205          enrol_manual_migrate_plugin_enrolments('manual');
 206          enrol_manual_migrate_plugin_enrolments('yyyy');
 207      }
 208  
 209      public function test_expired() {
 210          global $DB;
 211          $this->resetAfterTest();
 212  
 213          /** @var $manualplugin enrol_manual_plugin */
 214          $manualplugin = enrol_get_plugin('manual');
 215  
 216          $trace = new \null_progress_trace();
 217  
 218          $now = time();
 219  
 220          // Prepare some data.
 221  
 222          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 223          $this->assertNotEmpty($studentrole);
 224          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 225          $this->assertNotEmpty($teacherrole);
 226          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 227          $this->assertNotEmpty($managerrole);
 228  
 229          $user1 = $this->getDataGenerator()->create_user();
 230          $user2 = $this->getDataGenerator()->create_user();
 231          $user3 = $this->getDataGenerator()->create_user();
 232          $user4 = $this->getDataGenerator()->create_user();
 233  
 234          $course1 = $this->getDataGenerator()->create_course();
 235          $course2 = $this->getDataGenerator()->create_course();
 236          $course3 = $this->getDataGenerator()->create_course();
 237          $context1 = \context_course::instance($course1->id);
 238          $context2 = \context_course::instance($course2->id);
 239          $context3 = \context_course::instance($course3->id);
 240  
 241          $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'manual')));
 242          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 243          $this->assertEquals($studentrole->id, $instance1->roleid);
 244          $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 245          $this->assertEquals($studentrole->id, $instance2->roleid);
 246          $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 247          $this->assertEquals($studentrole->id, $instance3->roleid);
 248  
 249          $this->assertEquals(0, $DB->count_records('user_enrolments'));
 250          $this->assertEquals(0, $DB->count_records('role_assignments'));
 251  
 252          $manualplugin->enrol_user($instance1, $user1->id, $studentrole->id);
 253          $manualplugin->enrol_user($instance1, $user2->id, $studentrole->id);
 254          $manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
 255  
 256          $manualplugin->enrol_user($instance3, $user1->id, $studentrole->id, 0, 0);
 257          $manualplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now+60*60);
 258          $manualplugin->enrol_user($instance3, $user3->id, $teacherrole->id, 0, $now-60*60);
 259  
 260          role_assign($managerrole->id, $user4->id, $context1->id);
 261  
 262          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 263          $this->assertEquals(7, $DB->count_records('role_assignments'));
 264          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 265          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 266          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 267  
 268          // Execute tests.
 269  
 270          $this->assertEquals(ENROL_EXT_REMOVED_KEEP, $manualplugin->get_config('expiredaction'));
 271          $manualplugin->sync($trace, null);
 272          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 273          $this->assertEquals(7, $DB->count_records('role_assignments'));
 274  
 275  
 276          $manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 277          $manualplugin->sync($trace, $course2->id);
 278          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 279          $this->assertEquals(7, $DB->count_records('role_assignments'));
 280  
 281          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
 282          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id)));
 283          $manualplugin->sync($trace, null);
 284          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 285          $this->assertEquals(5, $DB->count_records('role_assignments'));
 286          $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 287          $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 288          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
 289          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user3->id, 'roleid'=>$teacherrole->id)));
 290  
 291  
 292          $manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
 293  
 294          role_assign($studentrole->id, $user3->id, $context1->id);
 295          role_assign($teacherrole->id, $user3->id, $context3->id);
 296          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 297          $this->assertEquals(7, $DB->count_records('role_assignments'));
 298          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 299          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 300          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 301  
 302          $manualplugin->sync($trace, null);
 303          $this->assertEquals(4, $DB->count_records('user_enrolments'));
 304          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
 305          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
 306          $this->assertEquals(5, $DB->count_records('role_assignments'));
 307          $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 308          $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 309          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 310  
 311  
 312          $manualplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPEND);
 313          $manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
 314          $manualplugin->enrol_user($instance3, $user3->id, $teacherrole->id, 0, $now-60*60);
 315          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 316          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 317  
 318          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 319          $this->assertEquals(7, $DB->count_records('role_assignments'));
 320          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 321          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 322          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
 323          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_ACTIVE)));
 324  
 325          $manualplugin->sync($trace, null);
 326          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 327          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
 328          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
 329          $this->assertEquals(7, $DB->count_records('role_assignments'));
 330          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 331          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 332          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance1->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_SUSPENDED)));
 333          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$maninstance2->id, 'userid'=>$user3->id, 'status'=>ENROL_USER_SUSPENDED)));
 334      }
 335  
 336      public function test_send_expiry_notifications() {
 337          global $DB, $CFG;
 338          $this->resetAfterTest();
 339          $this->preventResetByRollback(); // Messaging does not like transactions...
 340  
 341          $trace = new \null_progress_trace();
 342  
 343          /** @var $manualplugin enrol_manual_plugin */
 344          $manualplugin = enrol_get_plugin('manual');
 345          $now = time();
 346          $admin = get_admin();
 347  
 348          // Note: hopefully nobody executes the unit tests the last second before midnight...
 349  
 350          $manualplugin->set_config('expirynotifylast', $now - 60*60*24);
 351          $manualplugin->set_config('expirynotifyhour', 0);
 352  
 353          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 354          $this->assertNotEmpty($studentrole);
 355          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 356          $this->assertNotEmpty($editingteacherrole);
 357          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 358          $this->assertNotEmpty($managerrole);
 359  
 360          $user1 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser1'));
 361          $user2 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser2'));
 362          $user3 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser3'));
 363          $user4 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser4'));
 364          $user5 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser5'));
 365          $user6 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
 366          $user7 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
 367          $user8 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
 368  
 369          $course1 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse1'));
 370          $course2 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse2'));
 371          $course3 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse3'));
 372          $course4 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse4'));
 373  
 374          $this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'manual')));
 375  
 376          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 377          $instance1->expirythreshold = 60*60*24*4;
 378          $instance1->expirynotify    = 1;
 379          $instance1->notifyall       = 1;
 380          $DB->update_record('enrol', $instance1);
 381  
 382          $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 383          $instance2->expirythreshold = 60*60*24*1;
 384          $instance2->expirynotify    = 1;
 385          $instance2->notifyall       = 1;
 386          $DB->update_record('enrol', $instance2);
 387  
 388          $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 389          $instance3->expirythreshold = 60*60*24*1;
 390          $instance3->expirynotify    = 1;
 391          $instance3->notifyall       = 0;
 392          $DB->update_record('enrol', $instance3);
 393  
 394          $instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 395          $instance4->expirythreshold = 60*60*24*1;
 396          $instance4->expirynotify    = 0;
 397          $instance4->notifyall       = 0;
 398          $DB->update_record('enrol', $instance4);
 399  
 400          $manualplugin->enrol_user($instance1, $user1->id, $editingteacherrole->id, 0, $now + 60*60*24*1, ENROL_USER_SUSPENDED); // Suspended users are not notified.
 401          $manualplugin->enrol_user($instance1, $user2->id, $studentrole->id, 0, $now + 60*60*24*5);                       // Above threshold are not notified.
 402          $manualplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now + 60*60*24*3 + 60*60);               // Less than one day after threshold - should be notified.
 403          $manualplugin->enrol_user($instance1, $user4->id, $studentrole->id, 0, $now + 60*60*24*4 - 60*3);                // Less than one day after threshold - should be notified.
 404          $manualplugin->enrol_user($instance1, $user5->id, $studentrole->id, 0, $now + 60*60);                            // Should have been already notified.
 405          $manualplugin->enrol_user($instance1, $user6->id, $studentrole->id, 0, $now - 60);                               // Already expired.
 406          $manualplugin->enrol_user($instance1, $user7->id, $editingteacherrole->id);
 407          $manualplugin->enrol_user($instance1, $user8->id, $managerrole->id);                                             // Highest role --> enroller.
 408  
 409          $manualplugin->enrol_user($instance2, $user1->id, $studentrole->id);
 410          $manualplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60*3);                // Above threshold are not notified.
 411          $manualplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);               // Less than one day after threshold - should be notified.
 412  
 413          $manualplugin->enrol_user($instance3, $user1->id, $editingteacherrole->id);
 414          $manualplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60);                  // Above threshold are not notified.
 415          $manualplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);               // Less than one day after threshold - should be notified.
 416  
 417          $manualplugin->enrol_user($instance4, $user4->id, $editingteacherrole->id);
 418          $manualplugin->enrol_user($instance4, $user5->id, $studentrole->id, 0, $now + 60*60*24*1 + 60);
 419          $manualplugin->enrol_user($instance4, $user6->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);
 420  
 421          // The notification is sent out in fixed order first individual users,
 422          // then summary per course by enrolid, user lastname, etc.
 423          $this->assertGreaterThan($instance1->id, $instance2->id);
 424          $this->assertGreaterThan($instance2->id, $instance3->id);
 425  
 426          $sink = $this->redirectMessages();
 427  
 428          $manualplugin->send_expiry_notifications($trace);
 429  
 430          $messages = $sink->get_messages();
 431  
 432          $this->assertEquals(2+1 + 1+1 + 1 + 0, count($messages));
 433  
 434          // First individual notifications from course1.
 435          $this->assertEquals($user3->id, $messages[0]->useridto);
 436          $this->assertEquals($user8->id, $messages[0]->useridfrom);
 437          $this->assertStringContainsString('xcourse1', $messages[0]->fullmessagehtml);
 438  
 439          $this->assertEquals($user4->id, $messages[1]->useridto);
 440          $this->assertEquals($user8->id, $messages[1]->useridfrom);
 441          $this->assertStringContainsString('xcourse1', $messages[1]->fullmessagehtml);
 442  
 443          // Then summary for course1.
 444          $this->assertEquals($user8->id, $messages[2]->useridto);
 445          $this->assertEquals($admin->id, $messages[2]->useridfrom);
 446          $this->assertStringContainsString('xcourse1', $messages[2]->fullmessagehtml);
 447          $this->assertStringNotContainsString('xuser1', $messages[2]->fullmessagehtml);
 448          $this->assertStringNotContainsString('xuser2', $messages[2]->fullmessagehtml);
 449          $this->assertStringContainsString('xuser3', $messages[2]->fullmessagehtml);
 450          $this->assertStringContainsString('xuser4', $messages[2]->fullmessagehtml);
 451          $this->assertStringContainsString('xuser5', $messages[2]->fullmessagehtml);
 452          $this->assertStringNotContainsString('xuser6', $messages[2]->fullmessagehtml);
 453  
 454          // First individual notifications from course2.
 455          $this->assertEquals($user3->id, $messages[3]->useridto);
 456          $this->assertEquals($admin->id, $messages[3]->useridfrom);
 457          $this->assertStringContainsString('xcourse2', $messages[3]->fullmessagehtml);
 458  
 459          // Then summary for course2.
 460          $this->assertEquals($admin->id, $messages[4]->useridto);
 461          $this->assertEquals($admin->id, $messages[4]->useridfrom);
 462          $this->assertStringContainsString('xcourse2', $messages[4]->fullmessagehtml);
 463          $this->assertStringNotContainsString('xuser1', $messages[4]->fullmessagehtml);
 464          $this->assertStringNotContainsString('xuser2', $messages[4]->fullmessagehtml);
 465          $this->assertStringContainsString('xuser3', $messages[4]->fullmessagehtml);
 466          $this->assertStringNotContainsString('xuser4', $messages[4]->fullmessagehtml);
 467          $this->assertStringNotContainsString('xuser5', $messages[4]->fullmessagehtml);
 468          $this->assertStringNotContainsString('xuser6', $messages[4]->fullmessagehtml);
 469  
 470          // Only summary in course3.
 471          $this->assertEquals($user1->id, $messages[5]->useridto);
 472          $this->assertEquals($admin->id, $messages[5]->useridfrom);
 473          $this->assertStringContainsString('xcourse3', $messages[5]->fullmessagehtml);
 474          $this->assertStringNotContainsString('xuser1', $messages[5]->fullmessagehtml);
 475          $this->assertStringNotContainsString('xuser2', $messages[5]->fullmessagehtml);
 476          $this->assertStringContainsString('xuser3', $messages[5]->fullmessagehtml);
 477          $this->assertStringNotContainsString('xuser4', $messages[5]->fullmessagehtml);
 478          $this->assertStringNotContainsString('xuser5', $messages[5]->fullmessagehtml);
 479          $this->assertStringNotContainsString('xuser6', $messages[5]->fullmessagehtml);
 480  
 481  
 482          // Make sure that notifications are not repeated.
 483          $sink->clear();
 484  
 485          $manualplugin->send_expiry_notifications($trace);
 486          $this->assertEquals(0, $sink->count());
 487  
 488          // use invalid notification hour to verify that before the hour the notifications are not sent.
 489          $manualplugin->set_config('expirynotifylast', time() - 60*60*24);
 490          $manualplugin->set_config('expirynotifyhour', '24');
 491  
 492          $manualplugin->send_expiry_notifications($trace);
 493          $this->assertEquals(0, $sink->count());
 494  
 495          $manualplugin->set_config('expirynotifyhour', '0');
 496          $manualplugin->send_expiry_notifications($trace);
 497          $this->assertEquals(6, $sink->count());
 498      }
 499  
 500      /**
 501       * Test for getting user enrolment actions.
 502       */
 503      public function test_get_user_enrolment_actions() {
 504          global $CFG, $PAGE;
 505          $this->resetAfterTest();
 506  
 507          // Set page URL to prevent debugging messages.
 508          $PAGE->set_url('/enrol/editinstance.php');
 509  
 510          $pluginname = 'manual';
 511  
 512          // Only enable the manual enrol plugin.
 513          $CFG->enrol_plugins_enabled = $pluginname;
 514  
 515          $generator = $this->getDataGenerator();
 516  
 517          // Get the enrol plugin.
 518          $plugin = enrol_get_plugin($pluginname);
 519  
 520          // Create a course.
 521          $course = $generator->create_course();
 522          // Enable this enrol plugin for the course.
 523          $plugin->add_instance($course);
 524  
 525          // Create a teacher.
 526          $teacher = $generator->create_user();
 527          // Enrol the teacher to the course.
 528          $generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname);
 529          // Create a student.
 530          $student = $generator->create_user();
 531          // Enrol the student to the course.
 532          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 533  
 534          // Login as the teacher.
 535          $this->setUser($teacher);
 536          require_once($CFG->dirroot . '/enrol/locallib.php');
 537          $manager = new course_enrolment_manager($PAGE, $course);
 538          $userenrolments = $manager->get_user_enrolments($student->id);
 539          $this->assertCount(1, $userenrolments);
 540  
 541          $ue = reset($userenrolments);
 542          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 543          // Manual enrol has 2 enrol actions -- edit and unenrol.
 544          $this->assertCount(2, $actions);
 545      }
 546  }