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 400] [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  namespace enrol_self;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once($CFG->dirroot.'/enrol/self/lib.php');
  23  require_once($CFG->dirroot.'/enrol/self/locallib.php');
  24  
  25  /**
  26   * Self enrolment plugin tests.
  27   *
  28   * @package    enrol_self
  29   * @category   phpunit
  30   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  class self_test extends \advanced_testcase {
  34  
  35      public function test_basics() {
  36          $this->assertTrue(enrol_is_enabled('self'));
  37          $plugin = enrol_get_plugin('self');
  38          $this->assertInstanceOf('enrol_self_plugin', $plugin);
  39          $this->assertEquals(1, get_config('enrol_self', 'defaultenrol'));
  40          $this->assertEquals(ENROL_EXT_REMOVED_KEEP, get_config('enrol_self', 'expiredaction'));
  41      }
  42  
  43      public function test_sync_nothing() {
  44          global $SITE;
  45  
  46          $selfplugin = enrol_get_plugin('self');
  47  
  48          $trace = new \null_progress_trace();
  49  
  50          // Just make sure the sync does not throw any errors when nothing to do.
  51          $selfplugin->sync($trace, null);
  52          $selfplugin->sync($trace, $SITE->id);
  53      }
  54  
  55      public function test_longtimnosee() {
  56          global $DB;
  57          $this->resetAfterTest();
  58  
  59          $selfplugin = enrol_get_plugin('self');
  60          $manualplugin = enrol_get_plugin('manual');
  61          $this->assertNotEmpty($manualplugin);
  62  
  63          $now = time();
  64  
  65          $trace = new \progress_trace_buffer(new \text_progress_trace(), false);
  66  
  67          // Prepare some data.
  68  
  69          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  70          $this->assertNotEmpty($studentrole);
  71          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
  72          $this->assertNotEmpty($teacherrole);
  73  
  74          $record = array('firstaccess'=>$now-60*60*24*800);
  75          $record['lastaccess'] = $now-60*60*24*100;
  76          $user1 = $this->getDataGenerator()->create_user($record);
  77          $record['lastaccess'] = $now-60*60*24*10;
  78          $user2 = $this->getDataGenerator()->create_user($record);
  79          $record['lastaccess'] = $now-60*60*24*1;
  80          $user3 = $this->getDataGenerator()->create_user($record);
  81          $record['lastaccess'] = $now-10;
  82          $user4 = $this->getDataGenerator()->create_user($record);
  83  
  84          $course1 = $this->getDataGenerator()->create_course();
  85          $course2 = $this->getDataGenerator()->create_course();
  86          $course3 = $this->getDataGenerator()->create_course();
  87          $context1 = \context_course::instance($course1->id);
  88          $context2 = \context_course::instance($course2->id);
  89          $context3 = \context_course::instance($course3->id);
  90  
  91          $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
  92          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
  93          $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
  94          $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
  95          $id = $selfplugin->add_instance($course3, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
  96          $instance3b = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST);
  97          unset($id);
  98  
  99          $this->assertEquals($studentrole->id, $instance1->roleid);
 100          $instance1->customint2 = 60*60*24*14;
 101          $DB->update_record('enrol', $instance1);
 102          $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id);
 103          $selfplugin->enrol_user($instance1, $user2->id, $studentrole->id);
 104          $selfplugin->enrol_user($instance1, $user3->id, $studentrole->id);
 105          $this->assertEquals(3, $DB->count_records('user_enrolments'));
 106          $DB->insert_record('user_lastaccess', array('userid'=>$user2->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60*60*24*20));
 107          $DB->insert_record('user_lastaccess', array('userid'=>$user3->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60*60*24*2));
 108          $DB->insert_record('user_lastaccess', array('userid'=>$user4->id, 'courseid'=>$course1->id, 'timeaccess'=>$now-60));
 109  
 110          $this->assertEquals($studentrole->id, $instance3->roleid);
 111          $instance3->customint2 = 60*60*24*50;
 112          $DB->update_record('enrol', $instance3);
 113          $selfplugin->enrol_user($instance3, $user1->id, $studentrole->id);
 114          $selfplugin->enrol_user($instance3, $user2->id, $studentrole->id);
 115          $selfplugin->enrol_user($instance3, $user3->id, $studentrole->id);
 116          $selfplugin->enrol_user($instance3b, $user1->id, $teacherrole->id);
 117          $selfplugin->enrol_user($instance3b, $user4->id, $teacherrole->id);
 118          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 119          $DB->insert_record('user_lastaccess', array('userid'=>$user2->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*11));
 120          $DB->insert_record('user_lastaccess', array('userid'=>$user3->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*200));
 121          $DB->insert_record('user_lastaccess', array('userid'=>$user4->id, 'courseid'=>$course3->id, 'timeaccess'=>$now-60*60*24*200));
 122  
 123          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 124          $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 125  
 126          $manualplugin->enrol_user($maninstance2, $user1->id, $studentrole->id);
 127          $manualplugin->enrol_user($maninstance3, $user1->id, $teacherrole->id);
 128  
 129          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 130          $this->assertEquals(9, $DB->count_records('role_assignments'));
 131          $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 132          $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 133  
 134          // Execute sync - this is the same thing used from cron.
 135  
 136          $selfplugin->sync($trace, $course2->id);
 137          $output = $trace->get_buffer();
 138          $trace->reset_buffer();
 139          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 140          $this->assertStringContainsString('No expired enrol_self enrolments detected', $output);
 141          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id)));
 142          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
 143          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
 144          $this->assertTrue($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
 145  
 146          $selfplugin->sync($trace, null);
 147          $output = $trace->get_buffer();
 148          $trace->reset_buffer();
 149          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 150          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user1->id)));
 151          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user2->id)));
 152          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
 153          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user3->id)));
 154          $this->assertStringContainsString('unenrolling user ' . $user1->id . ' from course ' . $course1->id .
 155              ' as they did not log in for at least 14 days', $output);
 156          $this->assertStringContainsString('unenrolling user ' . $user1->id . ' from course ' . $course3->id .
 157              ' as they did not log in for at least 50 days', $output);
 158          $this->assertStringContainsString('unenrolling user ' . $user2->id . ' from course ' . $course1->id .
 159              ' as they did not access the course for at least 14 days', $output);
 160          $this->assertStringContainsString('unenrolling user ' . $user3->id . ' from course ' . $course3->id .
 161              ' as they did not access the course for at least 50 days', $output);
 162          $this->assertStringNotContainsString('unenrolling user ' . $user4->id, $output);
 163  
 164          $this->assertEquals(6, $DB->count_records('role_assignments'));
 165          $this->assertEquals(4, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 166          $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 167      }
 168  
 169      public function test_expired() {
 170          global $DB;
 171          $this->resetAfterTest();
 172  
 173          $selfplugin = enrol_get_plugin('self');
 174          $manualplugin = enrol_get_plugin('manual');
 175          $this->assertNotEmpty($manualplugin);
 176  
 177          $now = time();
 178  
 179          $trace = new \null_progress_trace();
 180  
 181          // Prepare some data.
 182  
 183          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 184          $this->assertNotEmpty($studentrole);
 185          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 186          $this->assertNotEmpty($teacherrole);
 187          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 188          $this->assertNotEmpty($managerrole);
 189  
 190          $user1 = $this->getDataGenerator()->create_user();
 191          $user2 = $this->getDataGenerator()->create_user();
 192          $user3 = $this->getDataGenerator()->create_user();
 193          $user4 = $this->getDataGenerator()->create_user();
 194  
 195          $course1 = $this->getDataGenerator()->create_course();
 196          $course2 = $this->getDataGenerator()->create_course();
 197          $course3 = $this->getDataGenerator()->create_course();
 198          $context1 = \context_course::instance($course1->id);
 199          $context2 = \context_course::instance($course2->id);
 200          $context3 = \context_course::instance($course3->id);
 201  
 202          $this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'self')));
 203          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
 204          $this->assertEquals($studentrole->id, $instance1->roleid);
 205          $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
 206          $this->assertEquals($studentrole->id, $instance2->roleid);
 207          $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
 208          $this->assertEquals($studentrole->id, $instance3->roleid);
 209          $id = $selfplugin->add_instance($course3, array('status'=>ENROL_INSTANCE_ENABLED, 'roleid'=>$teacherrole->id));
 210          $instance3b = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST);
 211          $this->assertEquals($teacherrole->id, $instance3b->roleid);
 212          unset($id);
 213  
 214          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 215          $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 216  
 217          $manualplugin->enrol_user($maninstance2, $user1->id, $studentrole->id);
 218          $manualplugin->enrol_user($maninstance3, $user1->id, $teacherrole->id);
 219  
 220          $this->assertEquals(2, $DB->count_records('user_enrolments'));
 221          $this->assertEquals(2, $DB->count_records('role_assignments'));
 222          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 223          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 224  
 225          $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id);
 226          $selfplugin->enrol_user($instance1, $user2->id, $studentrole->id);
 227          $selfplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
 228  
 229          $selfplugin->enrol_user($instance3, $user1->id, $studentrole->id, 0, 0);
 230          $selfplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now-60*60);
 231          $selfplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now+60*60);
 232          $selfplugin->enrol_user($instance3b, $user1->id, $teacherrole->id, $now-60*60*24*7, $now-60);
 233          $selfplugin->enrol_user($instance3b, $user4->id, $teacherrole->id);
 234  
 235          role_assign($managerrole->id, $user3->id, $context1->id);
 236  
 237          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 238          $this->assertEquals(10, $DB->count_records('role_assignments'));
 239          $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 240          $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 241  
 242          // Execute tests.
 243  
 244          $this->assertEquals(ENROL_EXT_REMOVED_KEEP, $selfplugin->get_config('expiredaction'));
 245          $selfplugin->sync($trace, null);
 246          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 247          $this->assertEquals(10, $DB->count_records('role_assignments'));
 248  
 249  
 250          $selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 251          $selfplugin->sync($trace, $course2->id);
 252          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 253          $this->assertEquals(10, $DB->count_records('role_assignments'));
 254  
 255          $selfplugin->sync($trace, null);
 256          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 257          $this->assertEquals(7, $DB->count_records('role_assignments'));
 258          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 259          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 260          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
 261          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
 262          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
 263          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
 264  
 265  
 266          $selfplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
 267  
 268          role_assign($studentrole->id, $user3->id, $context1->id);
 269          role_assign($studentrole->id, $user2->id, $context3->id);
 270          role_assign($teacherrole->id, $user1->id, $context3->id);
 271          $this->assertEquals(10, $DB->count_records('user_enrolments'));
 272          $this->assertEquals(10, $DB->count_records('role_assignments'));
 273          $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 274          $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 275  
 276          $selfplugin->sync($trace, null);
 277          $this->assertEquals(7, $DB->count_records('user_enrolments'));
 278          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
 279          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user2->id)));
 280          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3b->id, 'userid'=>$user1->id)));
 281          $this->assertEquals(6, $DB->count_records('role_assignments'));
 282          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 283          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 284      }
 285  
 286      public function test_send_expiry_notifications() {
 287          global $DB, $CFG;
 288          $this->resetAfterTest();
 289          $this->preventResetByRollback(); // Messaging does not like transactions...
 290  
 291          /** @var $selfplugin enrol_self_plugin */
 292          $selfplugin = enrol_get_plugin('self');
 293          /** @var $manualplugin enrol_manual_plugin */
 294          $manualplugin = enrol_get_plugin('manual');
 295          $now = time();
 296          $admin = get_admin();
 297  
 298          $trace = new \null_progress_trace();
 299  
 300          // Note: hopefully nobody executes the unit tests the last second before midnight...
 301  
 302          $selfplugin->set_config('expirynotifylast', $now - 60*60*24);
 303          $selfplugin->set_config('expirynotifyhour', 0);
 304  
 305          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 306          $this->assertNotEmpty($studentrole);
 307          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 308          $this->assertNotEmpty($editingteacherrole);
 309          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 310          $this->assertNotEmpty($managerrole);
 311  
 312          $user1 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser1'));
 313          $user2 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser2'));
 314          $user3 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser3'));
 315          $user4 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser4'));
 316          $user5 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser5'));
 317          $user6 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
 318          $user7 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
 319          $user8 = $this->getDataGenerator()->create_user(array('lastname'=>'xuser6'));
 320  
 321          $course1 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse1'));
 322          $course2 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse2'));
 323          $course3 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse3'));
 324          $course4 = $this->getDataGenerator()->create_course(array('fullname'=>'xcourse4'));
 325  
 326          $this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'manual')));
 327          $this->assertEquals(4, $DB->count_records('enrol', array('enrol'=>'self')));
 328  
 329          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 330          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
 331          $instance1->expirythreshold = 60*60*24*4;
 332          $instance1->expirynotify    = 1;
 333          $instance1->notifyall       = 1;
 334          $instance1->status          = ENROL_INSTANCE_ENABLED;
 335          $DB->update_record('enrol', $instance1);
 336  
 337          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 338          $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
 339          $instance2->expirythreshold = 60*60*24*1;
 340          $instance2->expirynotify    = 1;
 341          $instance2->notifyall       = 1;
 342          $instance2->status          = ENROL_INSTANCE_ENABLED;
 343          $DB->update_record('enrol', $instance2);
 344  
 345          $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 346          $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
 347          $instance3->expirythreshold = 60*60*24*1;
 348          $instance3->expirynotify    = 1;
 349          $instance3->notifyall       = 0;
 350          $instance3->status          = ENROL_INSTANCE_ENABLED;
 351          $DB->update_record('enrol', $instance3);
 352  
 353          $maninstance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 354          $instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'self'), '*', MUST_EXIST);
 355          $instance4->expirythreshold = 60*60*24*1;
 356          $instance4->expirynotify    = 0;
 357          $instance4->notifyall       = 0;
 358          $instance4->status          = ENROL_INSTANCE_ENABLED;
 359          $DB->update_record('enrol', $instance4);
 360  
 361          $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id, 0, $now + 60*60*24*1, ENROL_USER_SUSPENDED); // Suspended users are not notified.
 362          $selfplugin->enrol_user($instance1, $user2->id, $studentrole->id, 0, $now + 60*60*24*5);                       // Above threshold are not notified.
 363          $selfplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now + 60*60*24*3 + 60*60);               // Less than one day after threshold - should be notified.
 364          $selfplugin->enrol_user($instance1, $user4->id, $studentrole->id, 0, $now + 60*60*24*4 - 60*3);                // Less than one day after threshold - should be notified.
 365          $selfplugin->enrol_user($instance1, $user5->id, $studentrole->id, 0, $now + 60*60);                            // Should have been already notified.
 366          $selfplugin->enrol_user($instance1, $user6->id, $studentrole->id, 0, $now - 60);                               // Already expired.
 367          $manualplugin->enrol_user($maninstance1, $user7->id, $editingteacherrole->id);
 368          $manualplugin->enrol_user($maninstance1, $user8->id, $managerrole->id);                                        // Highest role --> enroller.
 369  
 370          $selfplugin->enrol_user($instance2, $user1->id, $studentrole->id);
 371          $selfplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60*3);                // Above threshold are not notified.
 372          $selfplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);               // Less than one day after threshold - should be notified.
 373  
 374          $manualplugin->enrol_user($maninstance3, $user1->id, $editingteacherrole->id);
 375          $selfplugin->enrol_user($instance3, $user2->id, $studentrole->id, 0, $now + 60*60*24*1 + 60);                  // Above threshold are not notified.
 376          $selfplugin->enrol_user($instance3, $user3->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);               // Less than one day after threshold - should be notified.
 377  
 378          $manualplugin->enrol_user($maninstance4, $user4->id, $editingteacherrole->id);
 379          $selfplugin->enrol_user($instance4, $user5->id, $studentrole->id, 0, $now + 60*60*24*1 + 60);
 380          $selfplugin->enrol_user($instance4, $user6->id, $studentrole->id, 0, $now + 60*60*24*1 - 60*60);
 381  
 382          // The notification is sent out in fixed order first individual users,
 383          // then summary per course by enrolid, user lastname, etc.
 384          $this->assertGreaterThan($instance1->id, $instance2->id);
 385          $this->assertGreaterThan($instance2->id, $instance3->id);
 386  
 387          $sink = $this->redirectMessages();
 388  
 389          $selfplugin->send_expiry_notifications($trace);
 390  
 391          $messages = $sink->get_messages();
 392  
 393          $this->assertEquals(2+1 + 1+1 + 1 + 0, count($messages));
 394  
 395          // First individual notifications from course1.
 396          $this->assertEquals($user3->id, $messages[0]->useridto);
 397          $this->assertEquals($user8->id, $messages[0]->useridfrom);
 398          $this->assertStringContainsString('xcourse1', $messages[0]->fullmessagehtml);
 399  
 400          $this->assertEquals($user4->id, $messages[1]->useridto);
 401          $this->assertEquals($user8->id, $messages[1]->useridfrom);
 402          $this->assertStringContainsString('xcourse1', $messages[1]->fullmessagehtml);
 403  
 404          // Then summary for course1.
 405          $this->assertEquals($user8->id, $messages[2]->useridto);
 406          $this->assertEquals($admin->id, $messages[2]->useridfrom);
 407          $this->assertStringContainsString('xcourse1', $messages[2]->fullmessagehtml);
 408          $this->assertStringNotContainsString('xuser1', $messages[2]->fullmessagehtml);
 409          $this->assertStringNotContainsString('xuser2', $messages[2]->fullmessagehtml);
 410          $this->assertStringContainsString('xuser3', $messages[2]->fullmessagehtml);
 411          $this->assertStringContainsString('xuser4', $messages[2]->fullmessagehtml);
 412          $this->assertStringContainsString('xuser5', $messages[2]->fullmessagehtml);
 413          $this->assertStringNotContainsString('xuser6', $messages[2]->fullmessagehtml);
 414  
 415          // First individual notifications from course2.
 416          $this->assertEquals($user3->id, $messages[3]->useridto);
 417          $this->assertEquals($admin->id, $messages[3]->useridfrom);
 418          $this->assertStringContainsString('xcourse2', $messages[3]->fullmessagehtml);
 419  
 420          // Then summary for course2.
 421          $this->assertEquals($admin->id, $messages[4]->useridto);
 422          $this->assertEquals($admin->id, $messages[4]->useridfrom);
 423          $this->assertStringContainsString('xcourse2', $messages[4]->fullmessagehtml);
 424          $this->assertStringNotContainsString('xuser1', $messages[4]->fullmessagehtml);
 425          $this->assertStringNotContainsString('xuser2', $messages[4]->fullmessagehtml);
 426          $this->assertStringContainsString('xuser3', $messages[4]->fullmessagehtml);
 427          $this->assertStringNotContainsString('xuser4', $messages[4]->fullmessagehtml);
 428          $this->assertStringNotContainsString('xuser5', $messages[4]->fullmessagehtml);
 429          $this->assertStringNotContainsString('xuser6', $messages[4]->fullmessagehtml);
 430  
 431          // Only summary in course3.
 432          $this->assertEquals($user1->id, $messages[5]->useridto);
 433          $this->assertEquals($admin->id, $messages[5]->useridfrom);
 434          $this->assertStringContainsString('xcourse3', $messages[5]->fullmessagehtml);
 435          $this->assertStringNotContainsString('xuser1', $messages[5]->fullmessagehtml);
 436          $this->assertStringNotContainsString('xuser2', $messages[5]->fullmessagehtml);
 437          $this->assertStringContainsString('xuser3', $messages[5]->fullmessagehtml);
 438          $this->assertStringNotContainsString('xuser4', $messages[5]->fullmessagehtml);
 439          $this->assertStringNotContainsString('xuser5', $messages[5]->fullmessagehtml);
 440          $this->assertStringNotContainsString('xuser6', $messages[5]->fullmessagehtml);
 441  
 442  
 443          // Make sure that notifications are not repeated.
 444          $sink->clear();
 445  
 446          $selfplugin->send_expiry_notifications($trace);
 447          $this->assertEquals(0, $sink->count());
 448  
 449          // use invalid notification hour to verify that before the hour the notifications are not sent.
 450          $selfplugin->set_config('expirynotifylast', time() - 60*60*24);
 451          $selfplugin->set_config('expirynotifyhour', '24');
 452  
 453          $selfplugin->send_expiry_notifications($trace);
 454          $this->assertEquals(0, $sink->count());
 455  
 456          $selfplugin->set_config('expirynotifyhour', '0');
 457          $selfplugin->send_expiry_notifications($trace);
 458          $this->assertEquals(6, $sink->count());
 459      }
 460  
 461      public function test_show_enrolme_link() {
 462          global $DB, $CFG;
 463          $this->resetAfterTest();
 464          $this->preventResetByRollback(); // Messaging does not like transactions...
 465  
 466          /** @var $selfplugin enrol_self_plugin */
 467          $selfplugin = enrol_get_plugin('self');
 468  
 469          $user1 = $this->getDataGenerator()->create_user();
 470          $user2 = $this->getDataGenerator()->create_user();
 471  
 472          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 473          $this->assertNotEmpty($studentrole);
 474  
 475          $course1 = $this->getDataGenerator()->create_course();
 476          $course2 = $this->getDataGenerator()->create_course();
 477          $course3 = $this->getDataGenerator()->create_course();
 478          $course4 = $this->getDataGenerator()->create_course();
 479          $course5 = $this->getDataGenerator()->create_course();
 480          $course6 = $this->getDataGenerator()->create_course();
 481          $course7 = $this->getDataGenerator()->create_course();
 482          $course8 = $this->getDataGenerator()->create_course();
 483          $course9 = $this->getDataGenerator()->create_course();
 484          $course10 = $this->getDataGenerator()->create_course();
 485          $course11 = $this->getDataGenerator()->create_course();
 486  
 487          $cohort1 = $this->getDataGenerator()->create_cohort();
 488          $cohort2 = $this->getDataGenerator()->create_cohort();
 489  
 490          // New enrolments are allowed and enrolment instance is enabled.
 491          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
 492          $instance1->customint6 = 1;
 493          $DB->update_record('enrol', $instance1);
 494          $selfplugin->update_status($instance1, ENROL_INSTANCE_ENABLED);
 495  
 496          // New enrolments are not allowed, but enrolment instance is enabled.
 497          $instance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'self'), '*', MUST_EXIST);
 498          $instance2->customint6 = 0;
 499          $DB->update_record('enrol', $instance2);
 500          $selfplugin->update_status($instance2, ENROL_INSTANCE_ENABLED);
 501  
 502          // New enrolments are allowed , but enrolment instance is disabled.
 503          $instance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'self'), '*', MUST_EXIST);
 504          $instance3->customint6 = 1;
 505          $DB->update_record('enrol', $instance3);
 506          $selfplugin->update_status($instance3, ENROL_INSTANCE_DISABLED);
 507  
 508          // New enrolments are not allowed and enrolment instance is disabled.
 509          $instance4 = $DB->get_record('enrol', array('courseid'=>$course4->id, 'enrol'=>'self'), '*', MUST_EXIST);
 510          $instance4->customint6 = 0;
 511          $DB->update_record('enrol', $instance4);
 512          $selfplugin->update_status($instance4, ENROL_INSTANCE_DISABLED);
 513  
 514          // Cohort member test.
 515          $instance5 = $DB->get_record('enrol', array('courseid'=>$course5->id, 'enrol'=>'self'), '*', MUST_EXIST);
 516          $instance5->customint6 = 1;
 517          $instance5->customint5 = $cohort1->id;
 518          $DB->update_record('enrol', $instance1);
 519          $selfplugin->update_status($instance5, ENROL_INSTANCE_ENABLED);
 520  
 521          $id = $selfplugin->add_instance($course5, $selfplugin->get_instance_defaults());
 522          $instance6 = $DB->get_record('enrol', array('id'=>$id), '*', MUST_EXIST);
 523          $instance6->customint6 = 1;
 524          $instance6->customint5 = $cohort2->id;
 525          $DB->update_record('enrol', $instance1);
 526          $selfplugin->update_status($instance6, ENROL_INSTANCE_ENABLED);
 527  
 528          // Enrol start date is in future.
 529          $instance7 = $DB->get_record('enrol', array('courseid'=>$course6->id, 'enrol'=>'self'), '*', MUST_EXIST);
 530          $instance7->customint6 = 1;
 531          $instance7->enrolstartdate = time() + 60;
 532          $DB->update_record('enrol', $instance7);
 533          $selfplugin->update_status($instance7, ENROL_INSTANCE_ENABLED);
 534  
 535          // Enrol start date is in past.
 536          $instance8 = $DB->get_record('enrol', array('courseid'=>$course7->id, 'enrol'=>'self'), '*', MUST_EXIST);
 537          $instance8->customint6 = 1;
 538          $instance8->enrolstartdate = time() - 60;
 539          $DB->update_record('enrol', $instance8);
 540          $selfplugin->update_status($instance8, ENROL_INSTANCE_ENABLED);
 541  
 542          // Enrol end date is in future.
 543          $instance9 = $DB->get_record('enrol', array('courseid'=>$course8->id, 'enrol'=>'self'), '*', MUST_EXIST);
 544          $instance9->customint6 = 1;
 545          $instance9->enrolenddate = time() + 60;
 546          $DB->update_record('enrol', $instance9);
 547          $selfplugin->update_status($instance9, ENROL_INSTANCE_ENABLED);
 548  
 549          // Enrol end date is in past.
 550          $instance10 = $DB->get_record('enrol', array('courseid'=>$course9->id, 'enrol'=>'self'), '*', MUST_EXIST);
 551          $instance10->customint6 = 1;
 552          $instance10->enrolenddate = time() - 60;
 553          $DB->update_record('enrol', $instance10);
 554          $selfplugin->update_status($instance10, ENROL_INSTANCE_ENABLED);
 555  
 556          // Maximum enrolments reached.
 557          $instance11 = $DB->get_record('enrol', array('courseid'=>$course10->id, 'enrol'=>'self'), '*', MUST_EXIST);
 558          $instance11->customint6 = 1;
 559          $instance11->customint3 = 1;
 560          $DB->update_record('enrol', $instance11);
 561          $selfplugin->update_status($instance11, ENROL_INSTANCE_ENABLED);
 562          $selfplugin->enrol_user($instance11, $user2->id, $studentrole->id);
 563  
 564          // Maximum enrolments not reached.
 565          $instance12 = $DB->get_record('enrol', array('courseid'=>$course11->id, 'enrol'=>'self'), '*', MUST_EXIST);
 566          $instance12->customint6 = 1;
 567          $instance12->customint3 = 1;
 568          $DB->update_record('enrol', $instance12);
 569          $selfplugin->update_status($instance12, ENROL_INSTANCE_ENABLED);
 570  
 571          $this->setUser($user1);
 572          $this->assertTrue($selfplugin->show_enrolme_link($instance1));
 573          $this->assertFalse($selfplugin->show_enrolme_link($instance2));
 574          $this->assertFalse($selfplugin->show_enrolme_link($instance3));
 575          $this->assertFalse($selfplugin->show_enrolme_link($instance4));
 576          $this->assertFalse($selfplugin->show_enrolme_link($instance7));
 577          $this->assertTrue($selfplugin->show_enrolme_link($instance8));
 578          $this->assertTrue($selfplugin->show_enrolme_link($instance9));
 579          $this->assertFalse($selfplugin->show_enrolme_link($instance10));
 580          $this->assertFalse($selfplugin->show_enrolme_link($instance11));
 581          $this->assertTrue($selfplugin->show_enrolme_link($instance12));
 582  
 583          require_once("$CFG->dirroot/cohort/lib.php");
 584          cohort_add_member($cohort1->id, $user1->id);
 585  
 586          $this->assertTrue($selfplugin->show_enrolme_link($instance5));
 587          $this->assertFalse($selfplugin->show_enrolme_link($instance6));
 588      }
 589  
 590      /**
 591       * This will check user enrolment only, rest has been tested in test_show_enrolme_link.
 592       */
 593      public function test_can_self_enrol() {
 594          global $DB, $CFG, $OUTPUT;
 595          $this->resetAfterTest();
 596          $this->preventResetByRollback();
 597  
 598          $selfplugin = enrol_get_plugin('self');
 599  
 600          $expectederrorstring = get_string('canntenrol', 'enrol_self');
 601  
 602          $user1 = $this->getDataGenerator()->create_user();
 603          $user2 = $this->getDataGenerator()->create_user();
 604          $guest = $DB->get_record('user', array('id' => $CFG->siteguest));
 605  
 606          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 607          $this->assertNotEmpty($studentrole);
 608          $editingteacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 609          $this->assertNotEmpty($editingteacherrole);
 610  
 611          $course1 = $this->getDataGenerator()->create_course();
 612  
 613          $instance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'self'), '*', MUST_EXIST);
 614          $instance1->customint6 = 1;
 615          $DB->update_record('enrol', $instance1);
 616          $selfplugin->update_status($instance1, ENROL_INSTANCE_ENABLED);
 617          $selfplugin->enrol_user($instance1, $user2->id, $editingteacherrole->id);
 618  
 619          $this->setUser($guest);
 620          $this->assertStringContainsString(get_string('noguestaccess', 'enrol'),
 621                  $selfplugin->can_self_enrol($instance1, true));
 622  
 623          $this->setUser($user1);
 624          $this->assertTrue($selfplugin->can_self_enrol($instance1, true));
 625  
 626          // Active enroled user.
 627          $this->setUser($user2);
 628          $selfplugin->enrol_user($instance1, $user1->id, $studentrole->id);
 629          $this->setUser($user1);
 630          $this->assertSame($expectederrorstring, $selfplugin->can_self_enrol($instance1, true));
 631      }
 632  
 633      /**
 634       * Test enrol_self_check_group_enrolment_key
 635       */
 636      public function test_enrol_self_check_group_enrolment_key() {
 637          global $DB;
 638          self::resetAfterTest(true);
 639  
 640          // Test in course with groups.
 641          $course = self::getDataGenerator()->create_course(array('groupmode' => SEPARATEGROUPS, 'groupmodeforce' => 1));
 642  
 643          $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
 644          $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id, 'enrolmentkey' => 'thepassword'));
 645  
 646          $result = enrol_self_check_group_enrolment_key($course->id, 'invalidpassword');
 647          $this->assertFalse($result);
 648  
 649          $result = enrol_self_check_group_enrolment_key($course->id, 'thepassword');
 650          $this->assertTrue($result);
 651  
 652          // Test disabling group options.
 653          $course->groupmode = NOGROUPS;
 654          $course->groupmodeforce = 0;
 655          $DB->update_record('course', $course);
 656  
 657          $result = enrol_self_check_group_enrolment_key($course->id, 'invalidpassword');
 658          $this->assertFalse($result);
 659  
 660          $result = enrol_self_check_group_enrolment_key($course->id, 'thepassword');
 661          $this->assertTrue($result);
 662  
 663          // Test without groups.
 664          $othercourse = self::getDataGenerator()->create_course();
 665          $result = enrol_self_check_group_enrolment_key($othercourse->id, 'thepassword');
 666          $this->assertFalse($result);
 667  
 668      }
 669  
 670      /**
 671       * Test get_welcome_email_contact().
 672       */
 673      public function test_get_welcome_email_contact() {
 674          global $DB;
 675          self::resetAfterTest(true);
 676  
 677          $user1 = $this->getDataGenerator()->create_user(['lastname' => 'Marsh']);
 678          $user2 = $this->getDataGenerator()->create_user(['lastname' => 'Victoria']);
 679          $user3 = $this->getDataGenerator()->create_user(['lastname' => 'Burch']);
 680          $user4 = $this->getDataGenerator()->create_user(['lastname' => 'Cartman']);
 681          $noreplyuser = \core_user::get_noreply_user();
 682  
 683          $course1 = $this->getDataGenerator()->create_course();
 684          $context = \context_course::instance($course1->id);
 685  
 686          // Get editing teacher role.
 687          $editingteacherrole = $DB->get_record('role', ['shortname' => 'editingteacher']);
 688          $this->assertNotEmpty($editingteacherrole);
 689  
 690          // Enable self enrolment plugin and set to send email from course contact.
 691          $selfplugin = enrol_get_plugin('self');
 692          $instance1 = $DB->get_record('enrol', ['courseid' => $course1->id, 'enrol' => 'self'], '*', MUST_EXIST);
 693          $instance1->customint6 = 1;
 694          $instance1->customint4 = ENROL_SEND_EMAIL_FROM_COURSE_CONTACT;
 695          $DB->update_record('enrol', $instance1);
 696          $selfplugin->update_status($instance1, ENROL_INSTANCE_ENABLED);
 697  
 698          // We do not have a teacher enrolled at this point, so it should send as no reply user.
 699          $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context);
 700          $this->assertEquals($noreplyuser, $contact);
 701  
 702          // By default, course contact is assigned to teacher role.
 703          // Enrol a teacher, now it should send emails from teacher email's address.
 704          $selfplugin->enrol_user($instance1, $user1->id, $editingteacherrole->id);
 705  
 706          // We should get the teacher email.
 707          $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context);
 708          $this->assertEquals($user1->username, $contact->username);
 709          $this->assertEquals($user1->email, $contact->email);
 710  
 711          // Now let's enrol another teacher.
 712          $selfplugin->enrol_user($instance1, $user2->id, $editingteacherrole->id);
 713          $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_COURSE_CONTACT, $context);
 714          $this->assertEquals($user1->username, $contact->username);
 715          $this->assertEquals($user1->email, $contact->email);
 716  
 717          // Get manager role, and enrol user as manager.
 718          $managerrole = $DB->get_record('role', ['shortname' => 'manager']);
 719          $this->assertNotEmpty($managerrole);
 720          $instance1->customint4 = ENROL_SEND_EMAIL_FROM_KEY_HOLDER;
 721          $DB->update_record('enrol', $instance1);
 722          $selfplugin->enrol_user($instance1, $user3->id, $managerrole->id);
 723  
 724          // Give manager role holdkey capability.
 725          assign_capability('enrol/self:holdkey', CAP_ALLOW, $managerrole->id, $context);
 726  
 727          // We should get the manager email contact.
 728          $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_KEY_HOLDER, $context);
 729          $this->assertEquals($user3->username, $contact->username);
 730          $this->assertEquals($user3->email, $contact->email);
 731  
 732          // Now let's enrol another manager.
 733          $selfplugin->enrol_user($instance1, $user4->id, $managerrole->id);
 734          $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_KEY_HOLDER, $context);
 735          $this->assertEquals($user3->username, $contact->username);
 736          $this->assertEquals($user3->email, $contact->email);
 737  
 738          $instance1->customint4 = ENROL_SEND_EMAIL_FROM_NOREPLY;
 739          $DB->update_record('enrol', $instance1);
 740  
 741          $contact = $selfplugin->get_welcome_email_contact(ENROL_SEND_EMAIL_FROM_NOREPLY, $context);
 742          $this->assertEquals($noreplyuser, $contact);
 743      }
 744  
 745      /**
 746       * Test for getting user enrolment actions.
 747       */
 748      public function test_get_user_enrolment_actions() {
 749          global $CFG, $DB, $PAGE;
 750          $this->resetAfterTest();
 751  
 752          // Set page URL to prevent debugging messages.
 753          $PAGE->set_url('/enrol/editinstance.php');
 754  
 755          $pluginname = 'self';
 756  
 757          // Only enable the self enrol plugin.
 758          $CFG->enrol_plugins_enabled = $pluginname;
 759  
 760          $generator = $this->getDataGenerator();
 761  
 762          // Get the enrol plugin.
 763          $plugin = enrol_get_plugin($pluginname);
 764  
 765          // Create a course.
 766          $course = $generator->create_course();
 767  
 768          // Create a teacher.
 769          $teacher = $generator->create_user();
 770          // Enrol the teacher to the course.
 771          $enrolresult = $generator->enrol_user($teacher->id, $course->id, 'editingteacher', $pluginname);
 772          $this->assertTrue($enrolresult);
 773          // Create a student.
 774          $student = $generator->create_user();
 775          // Enrol the student to the course.
 776          $enrolresult = $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 777          $this->assertTrue($enrolresult);
 778  
 779          // Login as the teacher.
 780          $this->setUser($teacher);
 781          require_once($CFG->dirroot . '/enrol/locallib.php');
 782          $manager = new \course_enrolment_manager($PAGE, $course);
 783          $userenrolments = $manager->get_user_enrolments($student->id);
 784          $this->assertCount(1, $userenrolments);
 785  
 786          $ue = reset($userenrolments);
 787          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 788          // Self enrol has 2 enrol actions -- edit and unenrol.
 789          $this->assertCount(2, $actions);
 790      }
 791  }