Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

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

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * flatfile enrolment sync tests.
  19   *
  20   * @package    enrol_flatfile
  21   * @category   test
  22   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  namespace enrol_flatfile;
  27  
  28  use enrol_flatfile\task\flatfile_sync_task;
  29  
  30  /**
  31   * flatfile enrolment sync tests.
  32   *
  33   * @package    enrol_flatfile
  34   * @category   test
  35   * @copyright  2012 Petr Skoda {@link http://skodak.org}
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class flatfile_test extends \advanced_testcase {
  39  
  40      protected function enable_plugin() {
  41          $enabled = enrol_get_plugins(true);
  42          $enabled['flatfile'] = true;
  43          $enabled = array_keys($enabled);
  44          set_config('enrol_plugins_enabled', implode(',', $enabled));
  45      }
  46  
  47      protected function disable_plugin() {
  48          $enabled = enrol_get_plugins(true);
  49          unset($enabled['flatfile']);
  50          $enabled = array_keys($enabled);
  51          set_config('enrol_plugins_enabled', implode(',', $enabled));
  52      }
  53  
  54      public function test_basics() {
  55          $this->assertFalse(enrol_is_enabled('flatfile'));
  56          $plugin = enrol_get_plugin('flatfile');
  57          $this->assertInstanceOf('enrol_flatfile_plugin', $plugin);
  58          $this->assertEquals(ENROL_EXT_REMOVED_SUSPENDNOROLES, get_config('enrol_flatfile', 'expiredaction'));
  59      }
  60  
  61      public function test_sync_nothing() {
  62          $this->resetAfterTest();
  63  
  64          $this->disable_plugin();
  65          $flatfileplugin = enrol_get_plugin('flatfile');
  66  
  67          // Just make sure the sync does not throw any errors when nothing to do.
  68          $flatfileplugin->sync(new \null_progress_trace());
  69          $this->enable_plugin();
  70          $flatfileplugin->sync(new \null_progress_trace());
  71      }
  72  
  73      public function test_sync() {
  74          global $CFG, $DB;
  75          $this->resetAfterTest();
  76  
  77          /** @var enrol_flatfile_plugin $flatfileplugin  */
  78          $flatfileplugin = enrol_get_plugin('flatfile');
  79          /** @var enrol_manual_plugin $manualplugin  */
  80          $manualplugin = enrol_get_plugin('manual');
  81          $this->assertNotEmpty($manualplugin);
  82  
  83          $trace = new \null_progress_trace();
  84          $this->enable_plugin();
  85          $file = "$CFG->dataroot/enrol.txt";
  86  
  87          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
  88          $this->assertNotEmpty($studentrole);
  89          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
  90          $this->assertNotEmpty($teacherrole);
  91          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
  92          $this->assertNotEmpty($managerrole);
  93  
  94          $user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'u1'));
  95          $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'u2'));
  96          $user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'u3'));
  97          $user4 = $this->getDataGenerator()->create_user(array('idnumber'=>'čtvrtý'));
  98          $user5 = $this->getDataGenerator()->create_user(array('idnumber'=>'u5'));
  99          $user6 = $this->getDataGenerator()->create_user(array('idnumber'=>'u6'));
 100          $user7 = $this->getDataGenerator()->create_user(array('idnumber'=>''));
 101  
 102          $course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'c1'));
 103          $course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'c2'));
 104          $course3 = $this->getDataGenerator()->create_course(array('idnumber'=>'c3'));
 105          $context1 = \context_course::instance($course1->id);
 106          $context2 = \context_course::instance($course2->id);
 107          $context3 = \context_course::instance($course3->id);
 108  
 109          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 110          $maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 111          $maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 112  
 113          // Rename teacher role.
 114          $flatfileplugin->set_config('map_'.$teacherrole->id, 'ucitel');
 115          // Disable manager role.
 116          $flatfileplugin->set_config('map_'.$managerrole->id, '');
 117          // Set file location.
 118          $flatfileplugin->set_config('location', $file);
 119  
 120          $now = time();
 121          $before = $now - 60;
 122          $future = $now + 60*60*5;
 123          $farfuture = $now + 60*60*24*5;
 124  
 125  
 126          // Test add action.
 127  
 128          $data ="'add','student','u1','c1'
 129  
 130              \"add\" , \"ucitel\", u2 , c2
 131              add,manager,u3,c1
 132              add,student,čtvrtý,c2,$before
 133              add,student,u5,c1,0,0,1
 134              add,student,u5,c2,20,10
 135              add,student,u6,c1,0,$future
 136              add,student,u6,c2,$future,0
 137              add,student,u6,c3,$future,$farfuture
 138              add,student,,c2";
 139          file_put_contents($file, $data);
 140  
 141          $this->assertEquals(0, $DB->count_records('user_enrolments'));
 142          $this->assertEquals(0, $DB->count_records('role_assignments'));
 143          $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
 144  
 145          $this->assertTrue(file_exists($file));
 146          $flatfileplugin->sync($trace);
 147          $this->assertFalse(file_exists($file));
 148  
 149          $this->assertEquals(4, $DB->count_records('user_enrolments'));
 150          $this->assertEquals(4, $DB->count_records('role_assignments'));
 151          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 152          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user2->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_flatfile')));
 153          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 154          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 155  
 156  
 157          // Test buffer.
 158  
 159          $this->assertEquals(2, $DB->count_records('enrol_flatfile'));
 160  
 161          $flatfileplugin->sync($trace);
 162          $this->assertEquals(2, $DB->count_records('enrol_flatfile'));
 163          $this->assertEquals(4, $DB->count_records('user_enrolments'));
 164          $this->assertEquals(4, $DB->count_records('role_assignments'));
 165  
 166          $DB->set_field('enrol_flatfile', 'timestart', time()-60, array('timestart'=>$future, 'timeend'=>0));
 167  
 168          $flatfileplugin->sync($trace);
 169          $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
 170          $this->assertEquals(5, $DB->count_records('user_enrolments'));
 171          $this->assertEquals(5, $DB->count_records('role_assignments'));
 172          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 173          $this->assertTrue($DB->record_exists('enrol_flatfile', array('userid'=>$user6->id, 'roleid'=>$studentrole->id, 'timeend'=>$farfuture)));
 174  
 175  
 176          // Test encoding.
 177  
 178          $data = "add;student;čtvrtý;c3";
 179          $data = \core_text::convert($data, 'utf-8', 'iso-8859-2');
 180          file_put_contents($file, $data);
 181          $flatfileplugin->set_config('encoding', 'iso-8859-2');
 182  
 183          $flatfileplugin->sync($trace);
 184          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 185          $this->assertEquals(6, $DB->count_records('role_assignments'));
 186          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 187          $flatfileplugin->set_config('encoding', 'UTF-8');
 188  
 189          // Test unenrolling purges buffer.
 190  
 191          $manualplugin->enrol_user($maninstance1, $user1->id, $teacherrole->id);
 192          $manualplugin->enrol_user($maninstance3, $user5->id, $teacherrole->id);
 193  
 194          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 195          $this->assertEquals(8, $DB->count_records('role_assignments'));
 196          $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
 197          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
 198  
 199          $flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
 200  
 201  
 202          $data = "del,student,u1,c1\ndel,teacher,u6,c3";
 203          file_put_contents($file, $data);
 204  
 205          $flatfileplugin->sync($trace);
 206          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 207          $this->assertEquals(8, $DB->count_records('role_assignments'));
 208          $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
 209  
 210          $data = "del,student,u6,c3";
 211          file_put_contents($file, $data);
 212  
 213          $flatfileplugin->sync($trace);
 214          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 215          $this->assertEquals(8, $DB->count_records('role_assignments'));
 216          $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
 217  
 218  
 219          $flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 220  
 221          $data = "
 222              del,student,u1,c1
 223              del,grrr,u5,c1
 224              del,guest,u5,c2
 225              del,student,u6,c2
 226              del,ucitel,u5,c3";
 227          file_put_contents($file, $data);
 228  
 229          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 230          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
 231          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
 232  
 233          $flatfileplugin->sync($trace);
 234          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 235          $this->assertEquals(5, $DB->count_records('role_assignments'));
 236          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 237          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
 238          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
 239          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
 240  
 241  
 242          $flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
 243  
 244          $manualplugin->enrol_user($maninstance3, $user5->id, $teacherrole->id);
 245          $data = "
 246              add,student,u1,c1
 247              add,student,u6,c2";
 248          file_put_contents($file, $data);
 249  
 250          $flatfileplugin->sync($trace);
 251  
 252          $this->assertEquals(8, $DB->count_records('user_enrolments'));
 253          $this->assertEquals(8, $DB->count_records('role_assignments'));
 254          $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
 255          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 256          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
 257          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
 258          $this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user5->id, 'enrolid'=>$maninstance3->id)));
 259          $this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user1->id, 'enrolid'=>$maninstance1->id)));
 260  
 261          $data = "
 262              del,student,u1,c1
 263              del,grrr,u5,c1
 264              del,guest,u5,c2
 265              del,student,u6,c2
 266              del,ucitel,u5,c3";
 267          file_put_contents($file, $data);
 268  
 269          $flatfileplugin->sync($trace);
 270          $this->assertEquals(5, $DB->count_records('user_enrolments'));
 271          $this->assertEquals(5, $DB->count_records('role_assignments'));
 272          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
 273          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
 274          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
 275          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
 276          $this->assertFalse($DB->record_exists('user_enrolments', array('userid'=>$user5->id, 'enrolid'=>$maninstance3->id)));
 277          $this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user1->id, 'enrolid'=>$maninstance1->id)));
 278      }
 279  
 280      public function test_notification() {
 281          global $CFG, $DB;
 282          $this->resetAfterTest();
 283  
 284          $this->preventResetByRollback();
 285  
 286          /** @var enrol_flatfile_plugin $flatfileplugin  */
 287          $flatfileplugin = enrol_get_plugin('flatfile');
 288          /** @var enrol_manual_plugin $manualplugin  */
 289          $manualplugin = enrol_get_plugin('manual');
 290          $this->assertNotEmpty($manualplugin);
 291  
 292          $this->enable_plugin();
 293  
 294          $trace = new \progress_trace_buffer(new \text_progress_trace(), false);
 295          $file = "$CFG->dataroot/enrol.txt";
 296          $flatfileplugin->set_config('location', $file);
 297  
 298          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 299          $this->assertNotEmpty($studentrole);
 300          $teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
 301          $this->assertNotEmpty($teacherrole);
 302  
 303          $user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'u1'));
 304          $user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'u2'));
 305          $user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'u3'));
 306          $admin = get_admin();
 307  
 308          $course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'c1'));
 309          $course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'c2'));
 310          $context1 = \context_course::instance($course1->id);
 311          $context2 = \context_course::instance($course2->id);
 312  
 313          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 314  
 315          $now = time();
 316          $future = $now + 60*60*5;
 317          $farfuture = $now + 60*60*24*5;
 318  
 319          $manualplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id);
 320  
 321          $data =
 322              "add,student,u1,c1
 323              add,student,u2,c2
 324              add,student,u2,c1,$future,$farfuture";
 325          file_put_contents($file, $data);
 326  
 327          $this->assertEquals(1, $DB->count_records('user_enrolments'));
 328          $this->assertEquals(1, $DB->count_records('role_assignments'));
 329          $this->assertEquals(0, $DB->count_records('enrol_flatfile'));
 330  
 331          $flatfileplugin->set_config('mailadmins', 1);
 332          $flatfileplugin->set_config('mailteachers', 1);
 333          $flatfileplugin->set_config('mailstudents', 1);
 334  
 335          $sink = $this->redirectMessages();
 336  
 337          $flatfileplugin->sync($trace);
 338  
 339          $this->assertEquals(3, $DB->count_records('user_enrolments'));
 340          $this->assertEquals(3, $DB->count_records('role_assignments'));
 341          $this->assertEquals(1, $DB->count_records('enrol_flatfile'));
 342  
 343          $messages = $sink->get_messages();
 344          $this->assertCount(5, $messages);
 345  
 346          // Notify student from teacher.
 347          $this->assertEquals($user1->id, $messages[0]->useridto);
 348          $this->assertEquals($user3->id, $messages[0]->useridfrom);
 349  
 350          // Notify teacher.
 351          $this->assertEquals($user3->id, $messages[1]->useridto);
 352          $this->assertEquals($admin->id, $messages[1]->useridfrom);
 353  
 354          // Notify student when teacher not present.
 355          $this->assertEquals($user2->id, $messages[2]->useridto);
 356          $this->assertEquals($admin->id, $messages[2]->useridfrom);
 357  
 358          // Notify admin when teacher not present.
 359          $this->assertEquals($admin->id, $messages[3]->useridto);
 360          $this->assertEquals($admin->id, $messages[3]->useridfrom);
 361  
 362          // Sent report to admin from self.
 363          $this->assertEquals($admin->id, $messages[4]->useridto);
 364          $this->assertEquals($admin->id, $messages[4]->useridfrom);
 365      }
 366  
 367      public function test_expired() {
 368          global $DB;
 369          $this->resetAfterTest();
 370  
 371          /** @var enrol_flatfile_plugin $flatfileplugin  */
 372          $flatfileplugin = enrol_get_plugin('flatfile');
 373          /** @var enrol_manual_plugin $manualplugin  */
 374          $manualplugin = enrol_get_plugin('manual');
 375          $this->assertNotEmpty($manualplugin);
 376  
 377          $now = time();
 378          $trace = new \null_progress_trace();
 379          $this->enable_plugin();
 380  
 381  
 382          // Prepare some data.
 383  
 384          $studentrole = $DB->get_record('role', array('shortname'=>'student'));
 385          $this->assertNotEmpty($studentrole);
 386          $teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
 387          $this->assertNotEmpty($teacherrole);
 388          $managerrole = $DB->get_record('role', array('shortname'=>'manager'));
 389          $this->assertNotEmpty($managerrole);
 390  
 391          $user1 = $this->getDataGenerator()->create_user();
 392          $user2 = $this->getDataGenerator()->create_user();
 393          $user3 = $this->getDataGenerator()->create_user();
 394          $user4 = $this->getDataGenerator()->create_user();
 395  
 396          $course1 = $this->getDataGenerator()->create_course();
 397          $course2 = $this->getDataGenerator()->create_course();
 398          $context1 = \context_course::instance($course1->id);
 399          $context2 = \context_course::instance($course2->id);
 400  
 401          $data = array('roleid'=>$studentrole->id, 'courseid'=>$course1->id);
 402          $id = $flatfileplugin->add_instance($course1, $data);
 403          $instance1  = $DB->get_record('enrol', array('id'=>$id));
 404          $data = array('roleid'=>$studentrole->id, 'courseid'=>$course2->id);
 405          $id = $flatfileplugin->add_instance($course2, $data);
 406          $instance2 = $DB->get_record('enrol', array('id'=>$id));
 407          $data = array('roleid'=>$teacherrole->id, 'courseid'=>$course2->id);
 408          $id = $flatfileplugin->add_instance($course2, $data);
 409          $instance3 = $DB->get_record('enrol', array('id'=>$id));
 410  
 411          $maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
 412  
 413          $manualplugin->enrol_user($maninstance1, $user3->id, $studentrole->id);
 414  
 415          $this->assertEquals(1, $DB->count_records('user_enrolments'));
 416          $this->assertEquals(1, $DB->count_records('role_assignments'));
 417          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 418  
 419          $flatfileplugin->enrol_user($instance1, $user1->id, $studentrole->id);
 420          $flatfileplugin->enrol_user($instance1, $user2->id, $studentrole->id);
 421          $flatfileplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
 422  
 423          $flatfileplugin->enrol_user($instance2, $user1->id, $studentrole->id, 0, 0);
 424          $flatfileplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now-60*60);
 425          $flatfileplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now+60*60);
 426  
 427          $flatfileplugin->enrol_user($instance3, $user1->id, $teacherrole->id, $now-60*60*24*7, $now-60);
 428          $flatfileplugin->enrol_user($instance3, $user4->id, $teacherrole->id);
 429  
 430          role_assign($managerrole->id, $user3->id, $context1->id);
 431  
 432          $this->assertEquals(9, $DB->count_records('user_enrolments'));
 433          $this->assertEquals(10, $DB->count_records('role_assignments'));
 434          $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 435          $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 436          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 437  
 438          // Execute tests.
 439  
 440          $flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_KEEP);
 441          $code = $flatfileplugin->sync($trace);
 442          $this->assertSame(0, $code);
 443          $this->assertEquals(9, $DB->count_records('user_enrolments'));
 444          $this->assertEquals(10, $DB->count_records('role_assignments'));
 445  
 446  
 447          $flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
 448          $flatfileplugin->sync($trace);
 449          $this->assertEquals(9, $DB->count_records('user_enrolments'));
 450          $this->assertEquals(7, $DB->count_records('role_assignments'));
 451          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 452          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 453          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 454          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
 455          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
 456          $this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
 457          $this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
 458  
 459  
 460          $flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
 461          role_assign($studentrole->id, $user3->id, $context1->id, 'enrol_flatfile', $instance1->id);
 462          role_assign($studentrole->id, $user2->id, $context2->id, 'enrol_flatfile', $instance2->id);
 463          role_assign($teacherrole->id, $user1->id, $context2->id, 'enrol_flatfile', $instance3->id);
 464          $this->assertEquals(9, $DB->count_records('user_enrolments'));
 465          $this->assertEquals(10, $DB->count_records('role_assignments'));
 466          $this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 467          $this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 468          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 469          $flatfileplugin->sync($trace);
 470          $this->assertEquals(6, $DB->count_records('user_enrolments'));
 471          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
 472          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance2->id, 'userid'=>$user2->id)));
 473          $this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
 474          $this->assertEquals(6, $DB->count_records('role_assignments'));
 475          $this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
 476          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
 477          $this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
 478      }
 479  
 480      /**
 481       * Flatfile enrolment sync task test.
 482       */
 483      public function test_flatfile_sync_task() {
 484          global $CFG, $DB;
 485          $this->resetAfterTest();
 486  
 487          $flatfileplugin = enrol_get_plugin('flatfile');
 488  
 489          $trace = new \null_progress_trace();
 490          $this->enable_plugin();
 491          $file = "$CFG->dataroot/enrol.txt";
 492          $flatfileplugin->set_config('location', $file);
 493  
 494          $studentrole = $DB->get_record('role', array('shortname' => 'student'));
 495          $this->assertNotEmpty($studentrole);
 496  
 497          $user1 = $this->getDataGenerator()->create_user(array('idnumber' => 'u1'));
 498          $course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'c1'));
 499          $context1 = \context_course::instance($course1->id);
 500  
 501          $data =
 502              "add,student,u1,c1";
 503          file_put_contents($file, $data);
 504  
 505          $task = new flatfile_sync_task;
 506          $task->execute();
 507  
 508          $this->assertEquals(1, $DB->count_records('role_assignments', array('roleid' => $studentrole->id)));
 509      }
 510  
 511      /**
 512       * Test for getting user enrolment actions.
 513       */
 514      public function test_get_user_enrolment_actions() {
 515          global $CFG, $PAGE;
 516          $this->resetAfterTest();
 517  
 518          // Set page URL to prevent debugging messages.
 519          $PAGE->set_url('/enrol/editinstance.php');
 520  
 521          $pluginname = 'flatfile';
 522  
 523          // Only enable the flatfile enrol plugin.
 524          $CFG->enrol_plugins_enabled = $pluginname;
 525  
 526          $generator = $this->getDataGenerator();
 527  
 528          // Get the enrol plugin.
 529          $plugin = enrol_get_plugin($pluginname);
 530  
 531          // Create a course.
 532          $course = $generator->create_course();
 533          // Enable this enrol plugin for the course.
 534          $plugin->add_instance($course);
 535  
 536          // Create a student.
 537          $student = $generator->create_user();
 538          // Enrol the student to the course.
 539          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
 540  
 541          // Teachers don't have enrol/flatfile:manage and enrol/flatfile:unenrol capabilities by default.
 542          // Login as admin for simplicity.
 543          $this->setAdminUser();
 544  
 545          require_once($CFG->dirroot . '/enrol/locallib.php');
 546          $manager = new \course_enrolment_manager($PAGE, $course);
 547          $userenrolments = $manager->get_user_enrolments($student->id);
 548          $this->assertCount(1, $userenrolments);
 549  
 550          $ue = reset($userenrolments);
 551          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
 552          // Flatfile enrolment has 2 enrol actions for active users -- edit and unenrol.
 553          $this->assertCount(2, $actions);
 554      }
 555  }