2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Database enrolment tests.
20 * @package enrol_database
21 * @copyright 2017 Jun Pataleta
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
29 * Database enrolment tests.
31 * @package enrol_database
32 * @copyright 2017 Jun Pataleta
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 class enrol_database_lib_testcase
extends advanced_testcase
{
37 * Test for getting user enrolment actions.
39 public function test_get_user_enrolment_actions() {
41 $this->resetAfterTest();
43 // Set page URL to prevent debugging messages.
44 $PAGE->set_url('/enrol/editinstance.php');
46 $pluginname = 'database';
48 // Only enable the database enrol plugin.
49 $CFG->enrol_plugins_enabled
= $pluginname;
51 $generator = $this->getDataGenerator();
53 // Get the enrol plugin.
54 $plugin = enrol_get_plugin($pluginname);
57 $course = $generator->create_course();
58 // Enable this enrol plugin for the course.
59 $plugin->add_instance($course);
62 $student = $generator->create_user();
63 // Enrol the student to the course.
64 $generator->enrol_user($student->id
, $course->id
, 'student', $pluginname);
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);
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);
78 // Enrol actions for a suspended student.
79 // Suspend the student.
80 $ue->status
= ENROL_USER_SUSPENDED
;
82 $actions = $plugin->get_user_enrolment_actions($manager, $ue);
83 // Database enrol has enrol actions for suspended students -- unenrol.
84 $this->assertCount(1, $actions);