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
namespace core_enrol;
18
 
19
defined('MOODLE_INTERNAL') || die();
20
 
21
global $CFG;
22
 
23
require_once($CFG->dirroot . '/webservice/tests/helpers.php');
24
require_once($CFG->dirroot . '/enrol/externallib.php');
25
 
26
/**
27
 * Role external PHPunit tests
28
 *
29
 * @package    core_enrol
30
 * @category   test
31
 * @copyright  2012 Jerome Mouneyrac
32
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33
 * @since Moodle 2.4
34
 */
1441 ariadna 35
final class role_external_test extends \externallib_advanced_testcase {
1 efrain 36
 
37
    /**
38
     * Tests set up
39
     */
40
    protected function setUp(): void {
41
        global $CFG;
42
        require_once($CFG->dirroot . '/enrol/externallib.php');
1441 ariadna 43
        parent::setUp();
1 efrain 44
    }
45
 
46
    /**
47
     * Test assign_roles
48
     */
11 efrain 49
    public function test_assign_roles(): void {
1 efrain 50
        global $USER;
51
 
52
        $this->resetAfterTest(true);
53
 
54
        $course = self::getDataGenerator()->create_course();
55
 
56
        // Set the required capabilities by the external function.
57
        $context = \context_course::instance($course->id);
58
        $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
59
        $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
60
 
61
        // Add manager role to $USER.
62
        // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
63
        role_assign(1, $USER->id, \context_system::instance()->id);
64
 
65
        // Check the teacher role has not been assigned to $USER.
66
        $users = get_role_users(3, $context);
67
        $this->assertEquals(count($users), 0);
68
 
69
        // Call the external function. Assign teacher role to $USER with contextid.
70
        \core_role_external::assign_roles(array(
71
            array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
72
 
73
        // Check the role has been assigned.
74
        $users = get_role_users(3, $context);
75
        $this->assertEquals(count($users), 1);
76
 
77
        // Unassign role.
78
        role_unassign(3, $USER->id, $context->id);
79
        $users = get_role_users(3, $context);
80
        $this->assertEquals(count($users), 0);
81
 
82
        // Call the external function. Assign teacher role to $USER.
83
        \core_role_external::assign_roles(array(
84
            array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id)));
85
        $users = get_role_users(3, $context);
86
        $this->assertEquals(count($users), 1);
87
 
88
        // Call without required capability.
89
        $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
90
        $this->expectException('moodle_exception');
91
        $categories = \core_role_external::assign_roles(
92
            array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
93
    }
94
 
95
    /**
96
     * Test unassign_roles
97
     */
11 efrain 98
    public function test_unassign_roles(): void {
1 efrain 99
        global $USER;
100
 
101
        $this->resetAfterTest(true);
102
 
103
        $course = self::getDataGenerator()->create_course();
104
 
105
        // Set the required capabilities by the external function.
106
        $context = \context_course::instance($course->id);
107
        $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
108
        $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
109
 
110
        // Add manager role to $USER.
111
        // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
112
        role_assign(1, $USER->id, \context_system::instance()->id);
113
 
114
        // Add teacher role to $USER on course context.
115
        role_assign(3, $USER->id, $context->id);
116
 
117
        // Check the teacher role has been assigned to $USER on course context.
118
        $users = get_role_users(3, $context);
119
        $this->assertEquals(count($users), 1);
120
 
121
        // Call the external function. Unassign teacher role using contextid.
122
        \core_role_external::unassign_roles(array(
123
            array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
124
 
125
        // Check the role has been unassigned on course context.
126
        $users = get_role_users(3, $context);
127
        $this->assertEquals(count($users), 0);
128
 
129
        // Add teacher role to $USER on course context.
130
        role_assign(3, $USER->id, $context->id);
131
        $users = get_role_users(3, $context);
132
        $this->assertEquals(count($users), 1);
133
 
134
        // Call the external function. Unassign teacher role using context level and instanceid.
135
        \core_role_external::unassign_roles(array(
136
            array('roleid' => 3, 'userid' => $USER->id, 'contextlevel' => "course", 'instanceid' => $course->id)));
137
 
138
        // Check the role has been unassigned on course context.
139
        $users = get_role_users(3, $context);
140
        $this->assertEquals(count($users), 0);
141
 
142
        // Call without required capability.
143
        $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
144
        $this->expectException('moodle_exception');
145
        $categories = \core_role_external::unassign_roles(
146
            array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
147
    }
148
}