Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
 */
1441 ariadna 38
final class lib_test extends \advanced_testcase {
1 efrain 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
        }
1441 ariadna 49
        parent::tearDownAfterClass();
1 efrain 50
    }
51
 
52
    /**
53
     * Test for getting user enrolment actions.
54
     */
11 efrain 55
    public function test_get_user_enrolment_actions(): void {
1 efrain 56
        global $CFG, $PAGE;
57
        $this->resetAfterTest();
58
 
59
        // Set page URL to prevent debugging messages.
60
        $PAGE->set_url('/enrol/editinstance.php');
61
 
62
        $pluginname = 'database';
63
 
64
        // Only enable the database enrol plugin.
65
        $CFG->enrol_plugins_enabled = $pluginname;
66
 
67
        $generator = $this->getDataGenerator();
68
 
69
        // Get the enrol plugin.
70
        $plugin = enrol_get_plugin($pluginname);
71
 
72
        // Create a course.
73
        $course = $generator->create_course();
74
        // Enable this enrol plugin for the course.
75
        $plugin->add_instance($course);
76
 
77
        // Create a student.
78
        $student = $generator->create_user();
79
        // Enrol the student to the course.
80
        $generator->enrol_user($student->id, $course->id, 'student', $pluginname);
81
 
82
        // Teachers don't have enrol/database:unenrol capability by default. Login as admin for simplicity.
83
        $this->setAdminUser();
84
        require_once($CFG->dirroot . '/enrol/locallib.php');
85
        $manager = new course_enrolment_manager($PAGE, $course);
86
        $userenrolments = $manager->get_user_enrolments($student->id);
87
        $this->assertCount(1, $userenrolments);
88
 
89
        $ue = reset($userenrolments);
90
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
91
        // Database enrol has 0 enrol actions for active users.
92
        $this->assertCount(0, $actions);
93
 
94
        // Enrol actions for a suspended student.
95
        // Suspend the student.
96
        $ue->status = ENROL_USER_SUSPENDED;
97
 
98
        $actions = $plugin->get_user_enrolment_actions($manager, $ue);
99
        // Database enrol has enrol actions for suspended students -- unenrol.
100
        $this->assertCount(1, $actions);
101
    }
102
}