Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

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