MDL-69521 core: Move all comments in code from 4.3 to 4.1
[moodle.git] / enrol / tests / role_external_test.php
blob4009101fbf52826f12060be2241997a4aef679ad
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.
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();
19 global $CFG;
21 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22 require_once($CFG->dirroot . '/enrol/externallib.php');
24 /**
25 * Role external PHPunit tests
27 * @package core_enrol
28 * @category external
29 * @copyright 2012 Jerome Mouneyrac
30 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31 * @since Moodle 2.4
33 class core_enrol_role_external_testcase extends externallib_advanced_testcase {
35 /**
36 * Tests set up
38 protected function setUp() {
39 global $CFG;
40 require_once($CFG->dirroot . '/enrol/externallib.php');
43 /**
44 * Test assign_roles
46 public function test_assign_roles() {
47 global $USER;
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);
74 // Unassign role.
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));
92 /**
93 * Test unassign_roles
95 public function test_unassign_roles() {
96 global $USER;
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));