MDL-40531 mod_lti: Various 1.1/1.1.1 fixes.
[moodle.git] / enrol / tests / externallib_test.php
blob0f053ebf5e1d12f175d222781af9dbd01df0217b
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 * Enrol 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_external_testcase extends externallib_advanced_testcase {
35 /**
36 * Test get_enrolled_users
38 public function test_get_enrolled_users() {
39 global $USER;
41 $this->resetAfterTest(true);
43 $course = self::getDataGenerator()->create_course();
44 $user1 = self::getDataGenerator()->create_user();
45 $user2 = self::getDataGenerator()->create_user();
47 // Set the required capabilities by the external function.
48 $context = context_course::instance($course->id);
49 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id);
50 $this->assignUserCapability('moodle/user:viewdetails', $context->id, $roleid);
52 // Enrol the users in the course.
53 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
54 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
55 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
57 // Call the external function.
58 $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
60 // We need to execute the return values cleaning process to simulate the web service server.
61 $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
63 // Check we retrieve the good total number of enrolled users.
64 $this->assertEquals(3, count($enrolledusers));
66 // Call without required capability.
67 $this->unassignUserCapability('moodle/course:viewparticipants', $context->id, $roleid);
68 $this->setExpectedException('moodle_exception');
69 $categories = core_enrol_external::get_enrolled_users($course->id);
72 /**
73 * Test get_users_courses
75 public function test_get_users_courses() {
76 global $USER;
78 $this->resetAfterTest(true);
80 $course1 = self::getDataGenerator()->create_course();
81 $course2 = self::getDataGenerator()->create_course();
82 $courses = array($course1, $course2);
84 // Enrol $USER in the courses.
85 // We use the manual plugin.
86 $roleid = null;
87 foreach ($courses as $course) {
88 $context = context_course::instance($course->id);
89 $roleid = $this->assignUserCapability('moodle/course:viewparticipants',
90 $context->id, $roleid);
92 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
95 // Call the external function.
96 $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
98 // We need to execute the return values cleaning process to simulate the web service server.
99 $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
101 // Check we retrieve the good total number of enrolled users.
102 $this->assertEquals(2, count($enrolledincourses));
106 * Test get_enrolled_users_with_capability
108 public function test_get_enrolled_users_with_capability () {
109 global $DB, $USER;
111 $this->resetAfterTest(true);
113 $coursedata['idnumber'] = 'idnumbercourse1';
114 $coursedata['fullname'] = 'Lightwork Course 1';
115 $coursedata['summary'] = 'Lightwork Course 1 description';
116 $coursedata['summaryformat'] = FORMAT_MOODLE;
117 $course1 = self::getDataGenerator()->create_course($coursedata);
119 // Create a manual enrolment record.
120 $manual_enrol_data['enrol'] = 'manual';
121 $manual_enrol_data['status'] = 0;
122 $manual_enrol_data['courseid'] = $course1->id;
123 $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
125 // Create the user and give them capabilities in the course context.
126 $context = context_course::instance($course1->id);
127 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
129 // Create a student.
130 $student1 = self::getDataGenerator()->create_user();
132 // Enrol both the user and the student in the course.
133 $user_enrolment_data['status'] = 0;
134 $user_enrolment_data['enrolid'] = $enrolid;
135 $user_enrolment_data['userid'] = $USER->id;
136 $DB->insert_record('user_enrolments', $user_enrolment_data);
138 $user_enrolment_data['status'] = 0;
139 $user_enrolment_data['enrolid'] = $enrolid;
140 $user_enrolment_data['userid'] = $student1->id;
141 $DB->insert_record('user_enrolments', $user_enrolment_data);
143 $params = array("coursecapabilities" =>array
144 ('courseid' => $course1->id, 'capabilities' => array('moodle/course:viewparticipants')));
145 $options = array();
146 $result = core_enrol_external::get_enrolled_users_with_capability($params, $options);
148 // We need to execute the return values cleaning process to simulate the web service server.
149 $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
151 // Check an array containing the expected user for the course capability is returned.
152 $expecteduserlist = $result[0];
153 $this->assertEquals($course1->id, $expecteduserlist['courseid']);
154 $this->assertEquals('moodle/course:viewparticipants', $expecteduserlist['capability']);
155 $this->assertEquals(1, count($expecteduserlist['users']));
161 * Role external PHPunit tests
163 * @package core_enrol
164 * @category external
165 * @copyright 2012 Jerome Mouneyrac
166 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
167 * @since Moodle 2.4
169 class core_role_external_testcase extends externallib_advanced_testcase {
172 * Tests set up
174 protected function setUp() {
175 global $CFG;
176 require_once($CFG->dirroot . '/enrol/externallib.php');
180 * Test assign_roles
182 public function test_assign_roles() {
183 global $USER;
185 $this->resetAfterTest(true);
187 $course = self::getDataGenerator()->create_course();
189 // Set the required capabilities by the external function.
190 $context = context_course::instance($course->id);
191 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
192 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
194 // Add manager role to $USER.
195 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
196 role_assign(1, $USER->id, context_system::instance()->id);
198 // Check the teacher role has not been assigned to $USER.
199 $users = get_role_users(3, $context);
200 $this->assertEquals(count($users), 0);
202 // Call the external function. Assign teacher role to $USER.
203 core_role_external::assign_roles(array(
204 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
206 // Check the role has been assigned.
207 $users = get_role_users(3, $context);
208 $this->assertEquals(count($users), 1);
210 // Call without required capability.
211 $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
212 $this->setExpectedException('moodle_exception');
213 $categories = core_role_external::assign_roles(
214 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));
218 * Test unassign_roles
220 public function test_unassign_roles() {
221 global $USER;
223 $this->resetAfterTest(true);
225 $course = self::getDataGenerator()->create_course();
227 // Set the required capabilities by the external function.
228 $context = context_course::instance($course->id);
229 $roleid = $this->assignUserCapability('moodle/role:assign', $context->id);
230 $this->assignUserCapability('moodle/course:view', $context->id, $roleid);
232 // Add manager role to $USER.
233 // So $USER is allowed to assign 'manager', 'editingteacher', 'teacher' and 'student'.
234 role_assign(1, $USER->id, context_system::instance()->id);
236 // Add teacher role to $USER on course context.
237 role_assign(3, $USER->id, $context->id);
239 // Check the teacher role has been assigned to $USER on course context.
240 $users = get_role_users(3, $context);
241 $this->assertEquals(count($users), 1);
243 // Call the external function. Assign teacher role to $USER.
244 core_role_external::unassign_roles(array(
245 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id)));
247 // Check the role has been unassigned on course context.
248 $users = get_role_users(3, $context);
249 $this->assertEquals(count($users), 0);
251 // Call without required capability.
252 $this->unassignUserCapability('moodle/role:assign', $context->id, $roleid);
253 $this->setExpectedException('moodle_exception');
254 $categories = core_role_external::unassign_roles(
255 array('roleid' => 3, 'userid' => $USER->id, 'contextid' => $context->id));