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/>.
17 defined('MOODLE_INTERNAL') ||
die();
21 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
22 require_once($CFG->dirroot
. '/enrol/externallib.php');
25 * Role external PHPunit tests
29 * @copyright 2012 Jerome Mouneyrac
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 class core_enrol_role_external_testcase
extends externallib_advanced_testcase
{
38 protected function setUp() {
40 require_once($CFG->dirroot
. '/enrol/externallib.php');
46 public function test_assign_roles() {
49 $this->resetAfterTest(true);
51 $course = self
::getDataGenerator()->create_course();
53 // Set the required capabilities by the external function.
54 $context = context_course
::instance($course->id
);
55 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id
);
56 $this->assignUserCapability('moodle/course:view', $context->id
, $roleid);
58 // Add manager role to $USER.
59 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
60 role_assign(1, $USER->id
, context_system
::instance()->id
);
62 // Check the teacher role has not been assigned to $USER.
63 $users = get_role_users(3, $context);
64 $this->assertEquals(count($users), 0);
66 // Call the external function. Assign teacher role to $USER with contextid.
67 core_role_external
::assign_roles(array(
68 array('roleid' => 3, 'userid' => $USER->id
, 'contextid' => $context->id
)));
70 // Check the role has been assigned.
71 $users = get_role_users(3, $context);
72 $this->assertEquals(count($users), 1);
75 role_unassign(3, $USER->id
, $context->id
);
76 $users = get_role_users(3, $context);
77 $this->assertEquals(count($users), 0);
79 // Call the external function. Assign teacher role to $USER.
80 core_role_external
::assign_roles(array(
81 array('roleid' => 3, 'userid' => $USER->id
, 'contextlevel' => "course", 'instanceid' => $course->id
)));
82 $users = get_role_users(3, $context);
83 $this->assertEquals(count($users), 1);
85 // Call without required capability.
86 $this->unassignUserCapability('moodle/role:assign', $context->id
, $roleid);
87 $this->expectException('moodle_exception');
88 $categories = core_role_external
::assign_roles(
89 array('roleid' => 3, 'userid' => $USER->id
, 'contextid' => $context->id
));
95 public function test_unassign_roles() {
98 $this->resetAfterTest(true);
100 $course = self
::getDataGenerator()->create_course();
102 // Set the required capabilities by the external function.
103 $context = context_course
::instance($course->id
);
104 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id
);
105 $this->assignUserCapability('moodle/course:view', $context->id
, $roleid);
107 // Add manager role to $USER.
108 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
109 role_assign(1, $USER->id
, context_system
::instance()->id
);
111 // Add teacher role to $USER on course context.
112 role_assign(3, $USER->id
, $context->id
);
114 // Check the teacher role has been assigned to $USER on course context.
115 $users = get_role_users(3, $context);
116 $this->assertEquals(count($users), 1);
118 // Call the external function. Unassign teacher role using contextid.
119 core_role_external
::unassign_roles(array(
120 array('roleid' => 3, 'userid' => $USER->id
, 'contextid' => $context->id
)));
122 // Check the role has been unassigned on course context.
123 $users = get_role_users(3, $context);
124 $this->assertEquals(count($users), 0);
126 // Add teacher role to $USER on course context.
127 role_assign(3, $USER->id
, $context->id
);
128 $users = get_role_users(3, $context);
129 $this->assertEquals(count($users), 1);
131 // Call the external function. Unassign teacher role using context level and instanceid.
132 core_role_external
::unassign_roles(array(
133 array('roleid' => 3, 'userid' => $USER->id
, 'contextlevel' => "course", 'instanceid' => $course->id
)));
135 // Check the role has been unassigned on course context.
136 $users = get_role_users(3, $context);
137 $this->assertEquals(count($users), 0);
139 // Call without required capability.
140 $this->unassignUserCapability('moodle/role:assign', $context->id
, $roleid);
141 $this->expectException('moodle_exception');
142 $categories = core_role_external
::unassign_roles(
143 array('roleid' => 3, 'userid' => $USER->id
, 'contextid' => $context->id
));