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]

   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  namespace enrol_database;
  25  
  26  use course_enrolment_manager;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  /**
  32   * Database enrolment tests.
  33   *
  34   * @package    enrol_database
  35   * @copyright  2017 Jun Pataleta
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class lib_test extends \advanced_testcase {
  39  
  40      public static function tearDownAfterClass(): void {
  41          global $DB;
  42          // Apply sqlsrv native driver error and logging default
  43          // settings while finishing the AdoDB tests.
  44          if ($DB->get_dbfamily() === 'mssql') {
  45              sqlsrv_configure("WarningsReturnAsErrors", false);
  46              sqlsrv_configure("LogSubsystems", SQLSRV_LOG_SYSTEM_OFF);
  47              sqlsrv_configure("LogSeverity", SQLSRV_LOG_SEVERITY_ERROR);
  48          }
  49      }
  50  
  51      /**
  52       * Test for getting user enrolment actions.
  53       */
  54      public function test_get_user_enrolment_actions() {
  55          global $CFG, $PAGE;
  56          $this->resetAfterTest();
  57  
  58          // Set page URL to prevent debugging messages.
  59          $PAGE->set_url('/enrol/editinstance.php');
  60  
  61          $pluginname = 'database';
  62  
  63          // Only enable the database enrol plugin.
  64          $CFG->enrol_plugins_enabled = $pluginname;
  65  
  66          $generator = $this->getDataGenerator();
  67  
  68          // Get the enrol plugin.
  69          $plugin = enrol_get_plugin($pluginname);
  70  
  71          // Create a course.
  72          $course = $generator->create_course();
  73          // Enable this enrol plugin for the course.
  74          $plugin->add_instance($course);
  75  
  76          // Create a student.
  77          $student = $generator->create_user();
  78          // Enrol the student to the course.
  79          $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
  80  
  81          // Teachers don't have enrol/database:unenrol capability by default. Login as admin for simplicity.
  82          $this->setAdminUser();
  83          require_once($CFG->dirroot . '/enrol/locallib.php');
  84          $manager = new course_enrolment_manager($PAGE, $course);
  85          $userenrolments = $manager->get_user_enrolments($student->id);
  86          $this->assertCount(1, $userenrolments);
  87  
  88          $ue = reset($userenrolments);
  89          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
  90          // Database enrol has 0 enrol actions for active users.
  91          $this->assertCount(0, $actions);
  92  
  93          // Enrol actions for a suspended student.
  94          // Suspend the student.
  95          $ue->status = ENROL_USER_SUSPENDED;
  96  
  97          $actions = $plugin->get_user_enrolment_actions($manager, $ue);
  98          // Database enrol has enrol actions for suspended students -- unenrol.
  99          $this->assertCount(1, $actions);
 100      }
 101  }