Merge branch 'install_master' of https://git.in.moodle.com/amosbot/moodle-install
[moodle.git] / enrol / tests / externallib_test.php
blob170d064c7e04b81af4f41e72c2bee08207244779
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_externallib_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();
46 $user3 = self::getDataGenerator()->create_user();
47 $this->setUser($user3);
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);
54 // Enrol the users in the course.
55 $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
56 $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
57 $this->getDataGenerator()->enrol_user($user3->id, $course->id, $roleid, 'manual');
59 // Call the external function.
60 $enrolledusers = core_enrol_external::get_enrolled_users($course->id);
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);
65 // Check the result set.
66 $this->assertEquals(3, count($enrolledusers));
67 $this->assertArrayHasKey('email', $enrolledusers[0]);
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 ));
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);
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]);
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);
91 /**
92 * Test get_users_courses
94 public function test_get_users_courses() {
95 global $USER;
97 $this->resetAfterTest(true);
99 $coursedata1 = array(
100 'summary' => 'Lightwork Course 1 description',
101 'summaryformat' => FORMAT_MOODLE,
102 'lang' => 'en',
103 'enablecompletion' => true,
104 'showgrades' => true
107 $course1 = self::getDataGenerator()->create_course($coursedata1);
108 $course2 = self::getDataGenerator()->create_course();
109 $courses = array($course1, $course2);
111 // Enrol $USER in the courses.
112 // We use the manual plugin.
113 $roleid = null;
114 $contexts = array();
115 foreach ($courses as $course) {
116 $contexts[$course->id] = context_course::instance($course->id);
117 $roleid = $this->assignUserCapability('moodle/course:viewparticipants',
118 $contexts[$course->id]->id, $roleid);
120 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
123 // Call the external function.
124 $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
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);
129 // Check we retrieve the good total number of enrolled users.
130 $this->assertEquals(2, count($enrolledincourses));
132 // We need to format summary and summaryformat before to compare them with those values returned by the webservice.
133 list($course1->summary, $course1->summaryformat) =
134 external_format_text($course1->summary, $course1->summaryformat, $contexts[$course1->id]->id, 'course', 'summary', 0);
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);
148 * Test get_enrolled_users_with_capability
150 public function test_get_enrolled_users_with_capability () {
151 global $DB, $USER;
153 $this->resetAfterTest(true);
155 $user1 = $this->getDataGenerator()->create_user();
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);
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);
169 // Create the users and give them capabilities in the course context.
170 $context = context_course::instance($course1->id);
171 $roleid = $this->assignUserCapability('moodle/course:viewparticipants', $context->id, 3);
173 // Create 2 students.
174 $student1 = self::getDataGenerator()->create_user();
175 $student2 = self::getDataGenerator()->create_user();
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();
182 // Enrol both the user and the students in the course.
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);
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);
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);
198 $params = array("coursecapabilities" => array('courseid' => $course1->id,
199 'capabilities' => array('moodle/course:viewparticipants')));
200 $options = array();
201 $result = core_enrol_external::get_enrolled_users_with_capability($params, $options);
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);
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']);
210 $this->assertEquals(2, count($expecteduserlist['users']));
212 // Now doing the query again with options.
213 $params = array(
214 "coursecapabilities" => array(
215 'courseid' => $course1->id,
216 'capabilities' => array('moodle/course:viewparticipants')
219 $options = array(
220 array('name' => 'limitfrom', 'value' => 1),
221 array('name' => 'limitnumber', 'value' => 1),
222 array('name' => 'userfields', 'value' => 'id')
225 $result = core_enrol_external::get_enrolled_users_with_capability($params, $options);
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);
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']);
238 * Test get_course_enrolment_methods
240 public function test_get_course_enrolment_methods() {
241 global $DB;
243 $this->resetAfterTest(true);
245 // Get enrolment plugins.
246 $selfplugin = enrol_get_plugin('self');
247 $this->assertNotEmpty($selfplugin);
248 $manualplugin = enrol_get_plugin('manual');
249 $this->assertNotEmpty($manualplugin);
251 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
252 $this->assertNotEmpty($studentrole);
254 $course1 = self::getDataGenerator()->create_course();
255 $course2 = self::getDataGenerator()->create_course();
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));
266 $instanceid3 = $manualplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
267 'name' => 'Test instance 3'));
269 $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course1->id, 'status' => ENROL_INSTANCE_ENABLED));
270 $this->assertCount(2, $enrolmentmethods);
272 // Check if information is returned.
273 $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course1->id);
274 $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
275 $enrolmentmethods);
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);
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']));
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);
292 $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
293 $enrolmentmethods);
294 $this->assertCount(1, $enrolmentmethods);
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']);