MDL-45131 enrol: Add validation for returned parameters in WS tests
[moodle.git] / enrol / tests / externallib_test.php
CommitLineData
566596ab
JM
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
566596ab
JM
17defined('MOODLE_INTERNAL') || die();
18
19global $CFG;
20
21require_once($CFG->dirroot . '/webservice/tests/helpers.php');
22require_once($CFG->dirroot . '/enrol/externallib.php');
23
24/**
25 * Enrol external PHPunit tests
26 *
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
32 */
8252b7c2 33class core_enrol_externallib_testcase extends externallib_advanced_testcase {
566596ab
JM
34
35 /**
36 * Test get_enrolled_users
37 */
38 public function test_get_enrolled_users() {
39 global $USER;
40
41 $this->resetAfterTest(true);
42
43 $course = self::getDataGenerator()->create_course();
44 $user1 = self::getDataGenerator()->create_user();
45 $user2 = self::getDataGenerator()->create_user();
457b82f2
FM
46 $user3 = self::getDataGenerator()->create_user();
47 $this->setUser($user3);
566596ab
JM
48
49 // Set the required capabilities by the external function.
50 $context = context_course::instance($course->id);
51 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id);
52 $this->assignUserCapability('moodle/user:viewdetails', $context->id, $roleid);
53
54 // Enrol the users in the course.
909f27ac
JM
55 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
56 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
457b82f2 57 $this->getDataGenerator()->enrol_user($user3->id, $course->id, $roleid, 'manual');
566596ab
JM
58
59 // Call the external function.
60 $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
61
fb695f6e
JM
62 // We need to execute the return values cleaning process to simulate the web service server.
63 $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
64
457b82f2 65 // Check the result set.
566596ab 66 $this->assertEquals(3, count($enrolledusers));
457b82f2
FM
67 $this->assertArrayHasKey('email', $enrolledusers[0]);
68
69 // Call the function with some parameters set.
70 $enrolledusers = core_enrol_external::get_enrolled_users($course->id, array(
71 array('name' => 'limitfrom', 'value' => 2),
72 array('name' => 'limitnumber', 'value' => 1),
73 array('name' => 'userfields', 'value' => 'id')
74 ));
75
76 // We need to execute the return values cleaning process to simulate the web service server.
77 $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
78
79 // Check the result set, we should only get the 3rd result, which is $user3.
80 $this->assertCount(1, $enrolledusers);
81 $this->assertEquals($user3->id, $enrolledusers[0]['id']);
82 $this->assertArrayHasKey('id', $enrolledusers[0]);
83 $this->assertArrayNotHasKey('email', $enrolledusers[0]);
566596ab
JM
84
85 // Call without required capability.
86 $this->unassignUserCapability('moodle/course:viewparticipants', $context->id, $roleid);
87 $this->setExpectedException('moodle_exception');
88 $categories = core_enrol_external::get_enrolled_users($course->id);
89 }
90
91 /**
92 * Test get_users_courses
93 */
94 public function test_get_users_courses() {
95 global $USER;
96
97 $this->resetAfterTest(true);
98
ba62015b
JL
99 $coursedata1 = array(
100 'summary' => 'Lightwork Course 1 description',
101 'summaryformat' => FORMAT_MOODLE,
102 'lang' => 'en',
103 'enablecompletion' => true,
104 'showgrades' => true
105 );
106
9b78f0e9 107 $course1 = self::getDataGenerator()->create_course($coursedata1);
566596ab
JM
108 $course2 = self::getDataGenerator()->create_course();
109 $courses = array($course1, $course2);
110
111 // Enrol $USER in the courses.
112 // We use the manual plugin.
566596ab 113 $roleid = null;
ba62015b 114 $contexts = array();
566596ab 115 foreach ($courses as $course) {
ba62015b 116 $contexts[$course->id] = context_course::instance($course->id);
566596ab 117 $roleid = $this->assignUserCapability('moodle/course:viewparticipants',
ba62015b 118 $contexts[$course->id]->id, $roleid);
566596ab 119
909f27ac 120 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
566596ab
JM
121 }
122
123 // Call the external function.
124 $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
125
fb695f6e
JM
126 // We need to execute the return values cleaning process to simulate the web service server.
127 $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
128
566596ab
JM
129 // Check we retrieve the good total number of enrolled users.
130 $this->assertEquals(2, count($enrolledincourses));
9b78f0e9
DU
131
132 // We need to format summary and summaryformat before to compare them with those values returned by the webservice.
9b78f0e9 133 list($course1->summary, $course1->summaryformat) =
ba62015b 134 external_format_text($course1->summary, $course1->summaryformat, $contexts[$course1->id]->id, 'course', 'summary', 0);
9b78f0e9
DU
135
136 // Check there are no differences between $course1 properties and course values returned by the webservice
137 // only for those fields listed in the $coursedata1 array.
138 foreach ($enrolledincourses as $courseenrol) {
139 if ($courseenrol['id'] == $course1->id) {
140 foreach ($coursedata1 as $fieldname => $value) {
141 $this->assertEquals($courseenrol[$fieldname], $course1->$fieldname);
142 }
143 }
144 }
566596ab 145 }
b6b6c7ac 146
21d3bfb3 147 /**
b6b6c7ac
PC
148 * Test get_enrolled_users_with_capability
149 */
150 public function test_get_enrolled_users_with_capability () {
151 global $DB, $USER;
152
153 $this->resetAfterTest(true);
154
e06d3599
FM
155 $user1 = $this->getDataGenerator()->create_user();
156
b6b6c7ac
PC
157 $coursedata['idnumber'] = 'idnumbercourse1';
158 $coursedata['fullname'] = 'Lightwork Course 1';
159 $coursedata['summary'] = 'Lightwork Course 1 description';
160 $coursedata['summaryformat'] = FORMAT_MOODLE;
161 $course1 = self::getDataGenerator()->create_course($coursedata);
162
163 // Create a manual enrolment record.
164 $manual_enrol_data['enrol'] = 'manual';
165 $manual_enrol_data['status'] = 0;
166 $manual_enrol_data['courseid'] = $course1->id;
167 $enrolid = $DB->insert_record('enrol', $manual_enrol_data);
168
e06d3599 169 // Create the users and give them capabilities in the course context.
b6b6c7ac
PC
170 $context = context_course::instance($course1->id);
171 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
172
e06d3599
FM
173 // Create 2 students.
174 $student1 = self::getDataGenerator()->create_user();
175 $student2 = self::getDataGenerator()->create_user();
176
177 // Give the capability to student2.
178 assign_capability('moodle/course:viewparticipants', CAP_ALLOW, 3, $context->id);
179 role_assign(3, $student2->id, $context->id);
180 accesslib_clear_all_caches_for_unit_testing();
b6b6c7ac 181
e06d3599 182 // Enrol both the user and the students in the course.
b6b6c7ac
PC
183 $user_enrolment_data['status'] = 0;
184 $user_enrolment_data['enrolid'] = $enrolid;
185 $user_enrolment_data['userid'] = $USER->id;
186 $DB->insert_record('user_enrolments', $user_enrolment_data);
187
188 $user_enrolment_data['status'] = 0;
189 $user_enrolment_data['enrolid'] = $enrolid;
190 $user_enrolment_data['userid'] = $student1->id;
191 $DB->insert_record('user_enrolments', $user_enrolment_data);
192
e06d3599
FM
193 $user_enrolment_data['status'] = 0;
194 $user_enrolment_data['enrolid'] = $enrolid;
195 $user_enrolment_data['userid'] = $student2->id;
196 $DB->insert_record('user_enrolments', $user_enrolment_data);
197
198 $params = array("coursecapabilities" => array('courseid' => $course1->id,
199 'capabilities' => array('moodle/course:viewparticipants')));
b6b6c7ac
PC
200 $options = array();
201 $result = core_enrol_external::get_enrolled_users_with_capability($params, $options);
202
fb695f6e
JM
203 // We need to execute the return values cleaning process to simulate the web service server.
204 $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
205
b6b6c7ac
PC
206 // Check an array containing the expected user for the course capability is returned.
207 $expecteduserlist = $result[0];
208 $this->assertEquals($course1->id, $expecteduserlist['courseid']);
209 $this->assertEquals('moodle/course:viewparticipants', $expecteduserlist['capability']);
e06d3599
FM
210 $this->assertEquals(2, count($expecteduserlist['users']));
211
212 // Now doing the query again with options.
213 $params = array(
214 "coursecapabilities" => array(
215 'courseid' => $course1->id,
216 'capabilities' => array('moodle/course:viewparticipants')
217 )
218 );
219 $options = array(
220 array('name' => 'limitfrom', 'value' => 1),
221 array('name' => 'limitnumber', 'value' => 1),
222 array('name' => 'userfields', 'value' => 'id')
223 );
b6b6c7ac 224
e06d3599
FM
225 $result = core_enrol_external::get_enrolled_users_with_capability($params, $options);
226
227 // We need to execute the return values cleaning process to simulate the web service server.
228 $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
229
230 // Check an array containing the expected user for the course capability is returned.
231 $expecteduserlist = $result[0]['users'];
232 $expecteduser = reset($expecteduserlist);
233 $this->assertEquals(1, count($expecteduserlist));
234 $this->assertEquals($student2->id, $expecteduser['id']);
b6b6c7ac 235 }
be9bf94e
RT
236
237 /**
238 * Test get_course_enrolment_methods
239 */
240 public function test_get_course_enrolment_methods() {
241 global $DB;
242
243 $this->resetAfterTest(true);
244
245 // Get enrolment plugins.
246 $selfplugin = enrol_get_plugin('self');
247 $this->assertNotEmpty($selfplugin);
248 $manualplugin = enrol_get_plugin('manual');
249 $this->assertNotEmpty($manualplugin);
250
251 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
252 $this->assertNotEmpty($studentrole);
253
254 $course1 = self::getDataGenerator()->create_course();
255 $course2 = self::getDataGenerator()->create_course();
256
257 // Add enrolment methods for course.
258 $instanceid1 = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
259 'name' => 'Test instance 1',
260 'customint6' => 1,
261 'roleid' => $studentrole->id));
262 $instanceid2 = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_DISABLED,
263 'name' => 'Test instance 2',
264 'roleid' => $studentrole->id));
265
266 $instanceid3 = $manualplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
267 'name' => 'Test instance 3'));
268
269 $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course1->id, 'status' => ENROL_INSTANCE_ENABLED));
270 $this->assertCount(2, $enrolmentmethods);
271
272 // Check if information is returned.
273 $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course1->id);
b4896308
JL
274 $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
275 $enrolmentmethods);
be9bf94e
RT
276 // Enrolment information is currently returned by self enrolment plugin, so count == 1.
277 // This should be changed as we implement get_enrol_info() for other enrolment plugins.
278 $this->assertCount(1, $enrolmentmethods);
279
280 $enrolmentmethod = $enrolmentmethods[0];
281 $this->assertEquals($course1->id, $enrolmentmethod['courseid']);
282 $this->assertEquals('self', $enrolmentmethod['type']);
283 $this->assertTrue($enrolmentmethod['status']);
284 $this->assertFalse(isset($enrolmentmethod['wsfunction']));
285
286 $instanceid4 = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED,
287 'name' => 'Test instance 4',
288 'roleid' => $studentrole->id,
289 'customint6' => 1,
290 'password' => 'test'));
291 $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course2->id);
b4896308
JL
292 $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
293 $enrolmentmethods);
be9bf94e
RT
294 $this->assertCount(1, $enrolmentmethods);
295
296 $enrolmentmethod = $enrolmentmethods[0];
297 $this->assertEquals($course2->id, $enrolmentmethod['courseid']);
298 $this->assertEquals('self', $enrolmentmethod['type']);
299 $this->assertTrue($enrolmentmethod['status']);
300 $this->assertEquals('enrol_self_get_instance_info', $enrolmentmethod['wsfunction']);
301 }
566596ab 302}