Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * Database enrolment tests.
  19   *
  20   * @package    enrol_database
  21   * @copyright  2017 Jun Pataleta
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  
  28  /**
  29   * Database enrolment tests.
  30   *
  31   * @package    enrol_database
  32   * @copyright  2017 Jun Pataleta
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class enrol_database_lib_testcase extends advanced_testcase {
  36      /**
  37       * Test for getting user enrolment actions.
  38       */
  39      public function test_get_user_enrolment_actions() {
  40          global $CFG, $PAGE;
  41          $this->resetAfterTest();
  42  
  43          // Set page URL to prevent debugging messages.
  44          $PAGE->set_url('/enrol/editinstance.php');
  45  
  46          $pluginname = 'database';
  47  
  48          // Only enable the database enrol plugin.
  49          $CFG->enrol_plugins_enabled = $pluginname;
  50  
  51          $generator = $this->getDataGenerator();
  52  
  53          // Get the enrol plugin.
  54          $plugin = enrol_get_plugin($pluginname);
  55  
  56          // Create a course.
  57          $course = $generator->create_course();
  58          // Enable this enrol plugin for the course.
  59          $plugin->add_instance($course);
  60  
  61          // Create a student.
  62          $student = $generator->create_user();
  63          // Enrol the student to the course.
  64          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
  65  
  66          // Teachers don't have enrol/database:unenrol capability by default. Login as admin for simplicity.
  67          $this->setAdminUser();
  68          require_once($CFG->dirroot . '/enrol/locallib.php');
  69          $manager = new course_enrolment_manager($PAGE, $course);
  70          $userenrolments = $manager->get_user_enrolments($student->id);
  71          $this->assertCount(1, $userenrolments);
  72  
  73          $ue = reset($userenrolments);
  74          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
  75          // Database enrol has 0 enrol actions for active users.
  76          $this->assertCount(0, $actions);
  77  
  78          // Enrol actions for a suspended student.
  79          // Suspend the student.
  80          $ue->status = ENROL_USER_SUSPENDED;
  81  
  82          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
  83          // Database enrol has enrol actions for suspended students -- unenrol.
  84          $this->assertCount(1, $actions);
  85      }
  86  }