Merge branch 'MDL-59200-33' of git://github.com/damyon/moodle into MOODLE_33_STABLE
[moodle.git] / enrol / tests / externallib_test.php
blobed1c8b3d9a888b9bed5f513889ba13e00cf9ecf7
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 * dataProvider for test_get_enrolled_users_visibility().
38 public function get_enrolled_users_visibility_provider() {
39 return array(
40 'Course without groups, default behavior (not filtering by cap, group, active)' =>
41 array(
42 'settings' => array(
43 'coursegroupmode' => NOGROUPS,
44 'withcapability' => null,
45 'groupid' => null,
46 'onlyactive' => false,
47 'allowedcaps' => array(),
49 'results' => array( // Everybody can view everybody.
50 'user0' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
51 'user1' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
52 'user2' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
53 'user31' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
54 'userall' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
58 'Course with visible groups, default behavior (not filtering by cap, group, active)' =>
59 array(
60 'settings' => array(
61 'coursegroupmode' => VISIBLEGROUPS,
62 'withcapability' => null,
63 'groupid' => null,
64 'onlyactive' => false,
65 'allowedcaps' => array(),
67 'results' => array( // Everybody can view everybody.
68 'user0' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
69 'user1' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
70 'user2' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
71 'user31' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
72 'userall' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
76 'Course with separate groups, default behavior (not filtering by cap, group, active)' =>
77 array(
78 'settings' => array(
79 'coursegroupmode' => SEPARATEGROUPS,
80 'withcapability' => null,
81 'groupid' => null,
82 'onlyactive' => false,
83 'allowedcaps' => array(),
85 'results' => array( // Only users from own groups are visible.
86 'user0' => array('canview' => array()), // Poor guy, cannot see anybody, himself included.
87 'user1' => array('canview' => array('user1', 'userall')),
88 'user2' => array('canview' => array('user2', 'user2su', 'userall')),
89 'user31' => array('canview' => array('user31', 'user32', 'userall')),
90 'userall' => array('canview' => array('user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
94 'Course with separate groups, default behavior (not filtering but having moodle/site:accessallgroups)' =>
95 array(
96 'settings' => array(
97 'coursegroupmode' => VISIBLEGROUPS,
98 'withcapability' => null,
99 'groupid' => null,
100 'onlyactive' => false,
101 'allowedcaps' => array('moodle/site:accessallgroups'),
103 'results' => array( // Everybody can view everybody.
104 'user0' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
105 'user1' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
106 'user2' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
107 'user31' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
108 'userall' => array('canview' => array('user0', 'user1', 'user2', 'user2su', 'user31', 'user32', 'userall')),
112 'Course with separate groups, filtering onlyactive (missing moodle/course:enrolreview)' =>
113 array(
114 'settings' => array(
115 'coursegroupmode' => SEPARATEGROUPS,
116 'withcapability' => null,
117 'groupid' => null,
118 'onlyactive' => true,
119 'allowedcaps' => array(),
121 'results' => array( // returns exception, cannot view anybody without the cap.
122 'user2' => array('exception' => array(
123 'type' => 'required_capability_exception',
124 'message' => 'Review course enrolments')),
125 'userall' => array('exception' => array(
126 'type' => 'required_capability_exception',
127 'message' => 'Review course enrolments')),
131 'Course with separate groups, filtering onlyactive (having moodle/course:enrolreview)' =>
132 array(
133 'settings' => array(
134 'coursegroupmode' => SEPARATEGROUPS,
135 'withcapability' => null,
136 'groupid' => null,
137 'onlyactive' => true,
138 'allowedcaps' => array('moodle/course:enrolreview'),
140 'results' => array( // Suspended are not returned.
141 'user2' => array('canview' => array('user2', 'userall')),
142 'user31' => array('canview' => array('user31', 'user32', 'userall')),
143 'userall' => array('canview' => array('user1', 'user2', 'user31', 'user32', 'userall')),
147 'Course with separate groups, filtering by groupid (not having moodle/site:accessallgroups)' =>
148 array(
149 'settings' => array(
150 'coursegroupmode' => SEPARATEGROUPS,
151 'withcapability' => null,
152 'groupid' => 'group2',
153 'onlyactive' => false,
154 'allowedcaps' => array(),
156 'results' => array( // Only group 2 members and only for members. Exception for non-members.
157 'user0' => array('exception' => array(
158 'type' => 'required_capability_exception',
159 'message' => 'Access all groups')),
160 'user1' => array('exception' => array(
161 'type' => 'required_capability_exception',
162 'message' => 'Access all groups')),
163 'user2' => array('canview' => array('user2', 'user2su', 'userall')),
164 'userall' => array('canview' => array('user2', 'user2su', 'userall')),
168 'Course with separate groups, filtering by groupid (having moodle/site:accessallgroups)' =>
169 array(
170 'settings' => array(
171 'coursegroupmode' => SEPARATEGROUPS,
172 'withcapability' => null,
173 'groupid' => 'group2',
174 'onlyactive' => false,
175 'allowedcaps' => array('moodle/site:accessallgroups'),
177 'results' => array( // All users with 'moodle/site:accessallgroups' can view group 2
178 'user0' => array('canview' => array('user2', 'user2su', 'userall')),
179 'user1' => array('canview' => array('user2', 'user2su', 'userall')),
180 'user2' => array('canview' => array('user2', 'user2su', 'userall')),
181 'userall' => array('canview' => array('user2', 'user2su', 'userall')),
185 'Course with separate groups, filtering by withcapability (not having moodle/role:review)' =>
186 array(
187 'settings' => array(
188 'coursegroupmode' => SEPARATEGROUPS,
189 'withcapability' => 'moodle/course:bulkmessaging',
190 'groupid' => null,
191 'onlyactive' => false,
192 'allowedcaps' => array(),
194 'results' => array( // No user has 'moodle/role:review' so exception.
195 'user0' => array('exception' => array(
196 'type' => 'required_capability_exception',
197 'message' => 'Review permissions for others')),
198 'user1' => array('exception' => array(
199 'type' => 'required_capability_exception',
200 'message' => 'Review permissions for others')),
201 'user2' => array('exception' => array(
202 'type' => 'required_capability_exception',
203 'message' => 'Review permissions for others')),
204 'userall' => array('exception' => array(
205 'type' => 'required_capability_exception',
206 'message' => 'Review permissions for others')),
210 'Course with separate groups, filtering by withcapability (having moodle/role:review)' =>
211 array(
212 'settings' => array(
213 'coursegroupmode' => SEPARATEGROUPS,
214 'withcapability' => 'moodle/course:bulkmessaging',
215 'groupid' => null,
216 'onlyactive' => false,
217 'allowedcaps' => array('moodle/role:review'),
219 'results' => array( // No user has withcapability, but all have 'moodle/role:review'. Empties.
220 'user0' => array('canview' => array()),
221 'user1' => array('canview' => array()),
222 'user2' => array('canview' => array()),
223 'userall' => array('canview' => array()),
227 'Course with separate groups, filtering by withcapability (having moodle/role:review)' =>
228 array(
229 'settings' => array(
230 'coursegroupmode' => SEPARATEGROUPS,
231 'withcapability' => 'moodle/course:bulkmessaging',
232 'groupid' => null,
233 'onlyactive' => false,
234 'allowedcaps' => array('moodle/role:review', 'moodle/course:bulkmessaging'),
236 'results' => array( // Users (previous) have withcapability, and all have 'moodle/role:review'.
237 'user0' => array('canview' => array()),
238 'user1' => array('canview' => array('user1')),
239 'user2' => array('canview' => array('user2')),
240 'userall' => array('canview' => array('user1', 'user2', 'userall')),
247 * Verify get_enrolled_users() returned users are the expected in every situation.
249 * @dataProvider get_enrolled_users_visibility_provider
251 public function test_get_enrolled_users_visibility($settings, $results) {
253 global $USER;
255 $this->resetAfterTest();
257 // Create the course and the users.
258 $course = $this->getDataGenerator()->create_course(array('groupmode' => $settings['coursegroupmode']));
259 $coursecontext = context_course::instance($course->id);
260 $user0 = $this->getDataGenerator()->create_user(array('username' => 'user0')); // A user without group.
261 $user1 = $this->getDataGenerator()->create_user(array('username' => 'user1')); // User for group 1.
262 $user2 = $this->getDataGenerator()->create_user(array('username' => 'user2')); // Two users for group 2.
263 $user2su = $this->getDataGenerator()->create_user(array('username' => 'user2su')); // (one suspended).
264 $user31 = $this->getDataGenerator()->create_user(array('username' => 'user31')); // Two users for group 3.
265 $user32 = $this->getDataGenerator()->create_user(array('username' => 'user32')); // (both enabled).
266 $userall = $this->getDataGenerator()->create_user(array('username' => 'userall')); // A user in all groups.
268 // Create utility array of created users, to produce better assertion messages.
269 $createdusers = array();
270 foreach (array($user0, $user1, $user2, $user2su, $user31, $user32, $userall) as $createduser) {
271 $createdusers[$createduser->id] = $createduser->username;
274 // Enrol the users in the course.
275 $this->getDataGenerator()->enrol_user($user0->id, $course->id);
276 $this->getDataGenerator()->enrol_user($user1->id, $course->id);
277 $this->getDataGenerator()->enrol_user($user2->id, $course->id);
278 $this->getDataGenerator()->enrol_user($user2su->id, $course->id, null, 'manual', 0, 0, ENROL_USER_SUSPENDED);
279 $this->getDataGenerator()->enrol_user($user31->id, $course->id);
280 $this->getDataGenerator()->enrol_user($user32->id, $course->id);
281 $this->getDataGenerator()->enrol_user($userall->id, $course->id);
283 // Create 3 groups.
284 $group1 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
285 $group2 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
286 $group3 = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
288 // Add the users to the groups.
289 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $user1->id));
290 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2->id));
291 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $user2su->id));
292 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user31->id));
293 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $user32->id));
294 $this->getDataGenerator()->create_group_member(array('groupid' => $group1->id, 'userid' => $userall->id));
295 $this->getDataGenerator()->create_group_member(array('groupid' => $group2->id, 'userid' => $userall->id));
296 $this->getDataGenerator()->create_group_member(array('groupid' => $group3->id, 'userid' => $userall->id));
298 // Create a role to add the allowedcaps. Users will have this role assigned.
299 $roleid = $this->getDataGenerator()->create_role();
300 // Allow the specified capabilities.
301 if (!empty($settings['allowedcaps'])) {
302 foreach ($settings['allowedcaps'] as $capability) {
303 assign_capability($capability, CAP_ALLOW, $roleid, $coursecontext);
307 // For each of the users, configure everything, perform the call, and assert results.
308 foreach ($results as $user => $expectations) {
309 // Convert canview expectations into a nice array of ids for easier handling.
310 $canview = array();
311 $exception = null;
312 // Analyse the expectations.
313 if (isset($expectations['canview'])) {
314 foreach ($expectations['canview'] as $canviewuser) {
315 $canview[] = $createdusers[${$canviewuser}->id];
317 } else if (isset($expectations['exception'])) {
318 $exception = $expectations['exception'];
319 $this->expectException($exception['type']);
320 $this->expectExceptionMessage($exception['message']);
321 } else {
322 // Failed, only canview and exception are supported.
323 $this->markTestIncomplete('Incomplete, only canview and exception are supported');
325 // Switch to the user and assign the role.
326 $this->setUser(${$user});
327 role_assign($roleid, $USER->id, $coursecontext);
329 // Convert groupid to proper id.
330 $groupid = 0;
331 if (isset($settings['groupid'])) {
332 $groupid = ${$settings['groupid']}->id;
335 // Call to the function.
336 $options = array(
337 array('name' => 'withcapability', 'value' => $settings['withcapability']),
338 array('name' => 'groupid', 'value' => $groupid),
339 array('name' => 'onlyactive', 'value' => $settings['onlyactive']),
340 array('name' => 'userfields', 'value' => 'id')
342 $enrolledusers = core_enrol_external::get_enrolled_users($course->id, $options);
344 // We need to execute the return values cleaning process to simulate the web service server.
345 $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
347 // We are only interested in ids to check visibility.
348 $viewed = array();
349 // Verify the user canview the expected users.
350 foreach ($enrolledusers as $enrolleduser) {
351 $viewed[] = $createdusers[$enrolleduser['id']];
353 // Verify viewed matches canview expectation (using canonicalize to ignore ordering).
354 $this->assertEquals($canview, $viewed, "Problem checking visible users for '{$createdusers[$USER->id]}'", 0, 1, true);
359 * Test get_users_courses
361 public function test_get_users_courses() {
362 global $USER;
364 $this->resetAfterTest(true);
366 $timenow = time();
367 $coursedata1 = array(
368 'fullname' => '<b>Course 1</b>', // Adding tags here to check that external_format_string works.
369 'shortname' => '<b>Course 1</b>', // Adding tags here to check that external_format_string works.
370 'summary' => 'Lightwork Course 1 description',
371 'summaryformat' => FORMAT_MOODLE,
372 'lang' => 'en',
373 'enablecompletion' => true,
374 'showgrades' => true,
375 'startdate' => $timenow,
376 'enddate' => $timenow + WEEKSECS
379 $coursedata2 = array(
380 'lang' => 'kk', // Check invalid language pack.
383 $course1 = self::getDataGenerator()->create_course($coursedata1);
384 $course2 = self::getDataGenerator()->create_course($coursedata2);
385 $courses = array($course1, $course2);
387 // Enrol $USER in the courses.
388 // We use the manual plugin.
389 $roleid = null;
390 $contexts = array();
391 foreach ($courses as $course) {
392 $contexts[$course->id] = context_course::instance($course->id);
393 $roleid = $this->assignUserCapability('moodle/course:viewparticipants',
394 $contexts[$course->id]->id, $roleid);
396 $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
399 // Call the external function.
400 $enrolledincourses = core_enrol_external::get_users_courses($USER->id);
402 // We need to execute the return values cleaning process to simulate the web service server.
403 $enrolledincourses = external_api::clean_returnvalue(core_enrol_external::get_users_courses_returns(), $enrolledincourses);
405 // Check we retrieve the good total number of enrolled users.
406 $this->assertEquals(2, count($enrolledincourses));
408 // We need to format summary and summaryformat before to compare them with those values returned by the webservice.
409 list($course1->summary, $course1->summaryformat) =
410 external_format_text($course1->summary, $course1->summaryformat, $contexts[$course1->id]->id, 'course', 'summary', 0);
412 // Check there are no differences between $course1 properties and course values returned by the webservice
413 // only for those fields listed in the $coursedata1 array.
414 $course1->fullname = external_format_string($course1->fullname, $contexts[$course1->id]->id);
415 $course1->shortname = external_format_string($course1->shortname, $contexts[$course1->id]->id);
416 foreach ($enrolledincourses as $courseenrol) {
417 if ($courseenrol['id'] == $course1->id) {
418 foreach ($coursedata1 as $fieldname => $value) {
419 $this->assertEquals($courseenrol[$fieldname], $course1->$fieldname);
421 } else {
422 // Check language pack. Should be empty since an incorrect one was used when creating the course.
423 $this->assertEmpty($courseenrol['lang']);
429 * Test get_course_enrolment_methods
431 public function test_get_course_enrolment_methods() {
432 global $DB;
434 $this->resetAfterTest(true);
436 // Get enrolment plugins.
437 $selfplugin = enrol_get_plugin('self');
438 $this->assertNotEmpty($selfplugin);
439 $manualplugin = enrol_get_plugin('manual');
440 $this->assertNotEmpty($manualplugin);
442 $studentrole = $DB->get_record('role', array('shortname'=>'student'));
443 $this->assertNotEmpty($studentrole);
445 $course1 = self::getDataGenerator()->create_course();
446 $coursedata = new stdClass();
447 $coursedata->visible = 0;
448 $course2 = self::getDataGenerator()->create_course($coursedata);
450 // Add enrolment methods for course.
451 $instanceid1 = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
452 'name' => 'Test instance 1',
453 'customint6' => 1,
454 'roleid' => $studentrole->id));
455 $instanceid2 = $selfplugin->add_instance($course1, array('status' => ENROL_INSTANCE_DISABLED,
456 'name' => 'Test instance 2',
457 'roleid' => $studentrole->id));
459 $instanceid3 = $manualplugin->add_instance($course1, array('status' => ENROL_INSTANCE_ENABLED,
460 'name' => 'Test instance 3'));
462 $enrolmentmethods = $DB->get_records('enrol', array('courseid' => $course1->id, 'status' => ENROL_INSTANCE_ENABLED));
463 $this->assertCount(2, $enrolmentmethods);
465 $this->setAdminUser();
467 // Check if information is returned.
468 $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course1->id);
469 $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
470 $enrolmentmethods);
471 // Enrolment information is currently returned by self enrolment plugin, so count == 1.
472 // This should be changed as we implement get_enrol_info() for other enrolment plugins.
473 $this->assertCount(1, $enrolmentmethods);
475 $enrolmentmethod = $enrolmentmethods[0];
476 $this->assertEquals($course1->id, $enrolmentmethod['courseid']);
477 $this->assertEquals('self', $enrolmentmethod['type']);
478 $this->assertTrue($enrolmentmethod['status']);
479 $this->assertFalse(isset($enrolmentmethod['wsfunction']));
481 $instanceid4 = $selfplugin->add_instance($course2, array('status' => ENROL_INSTANCE_ENABLED,
482 'name' => 'Test instance 4',
483 'roleid' => $studentrole->id,
484 'customint6' => 1,
485 'password' => 'test'));
486 $enrolmentmethods = core_enrol_external::get_course_enrolment_methods($course2->id);
487 $enrolmentmethods = external_api::clean_returnvalue(core_enrol_external::get_course_enrolment_methods_returns(),
488 $enrolmentmethods);
489 $this->assertCount(1, $enrolmentmethods);
491 $enrolmentmethod = $enrolmentmethods[0];
492 $this->assertEquals($course2->id, $enrolmentmethod['courseid']);
493 $this->assertEquals('self', $enrolmentmethod['type']);
494 $this->assertTrue($enrolmentmethod['status']);
495 $this->assertEquals('enrol_self_get_instance_info', $enrolmentmethod['wsfunction']);
497 // Try to retrieve information using a normal user for a hidden course.
498 $user = self::getDataGenerator()->create_user();
499 $this->setUser($user);
500 try {
501 core_enrol_external::get_course_enrolment_methods($course2->id);
502 } catch (moodle_exception $e) {
503 $this->assertEquals('coursehidden', $e->errorcode);
507 public function get_enrolled_users_setup($capability) {
508 global $USER;
510 $this->resetAfterTest(true);
512 $return = new stdClass();
514 $return->course = self::getDataGenerator()->create_course();
515 $return->user1 = self::getDataGenerator()->create_user();
516 $return->user2 = self::getDataGenerator()->create_user();
517 $return->user3 = self::getDataGenerator()->create_user();
518 $this->setUser($return->user3);
520 // Set the required capabilities by the external function.
521 $return->context = context_course::instance($return->course->id);
522 $return->roleid = $this->assignUserCapability($capability, $return->context->id);
523 $this->assignUserCapability('moodle/user:viewdetails', $return->context->id, $return->roleid);
525 // Enrol the users in the course.
526 $this->getDataGenerator()->enrol_user($return->user1->id, $return->course->id, $return->roleid, 'manual');
527 $this->getDataGenerator()->enrol_user($return->user2->id, $return->course->id, $return->roleid, 'manual');
528 $this->getDataGenerator()->enrol_user($return->user3->id, $return->course->id, $return->roleid, 'manual');
530 return $return;
534 * Test get_enrolled_users from core_enrol_external without additional
535 * parameters.
537 public function test_get_enrolled_users_without_parameters() {
538 $capability = 'moodle/course:viewparticipants';
539 $data = $this->get_enrolled_users_setup($capability);
541 // Call the external function.
542 $enrolledusers = core_enrol_external::get_enrolled_users($data->course->id);
544 // We need to execute the return values cleaning process to simulate the web service server.
545 $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
547 // Check the result set.
548 $this->assertEquals(3, count($enrolledusers));
549 $this->assertArrayHasKey('email', $enrolledusers[0]);
553 * Test get_enrolled_users from core_enrol_external with some parameters set.
555 public function test_get_enrolled_users_with_parameters() {
556 $capability = 'moodle/course:viewparticipants';
557 $data = $this->get_enrolled_users_setup($capability);
559 // Call the function with some parameters set.
560 $enrolledusers = core_enrol_external::get_enrolled_users($data->course->id, array(
561 array('name' => 'limitfrom', 'value' => 2),
562 array('name' => 'limitnumber', 'value' => 1),
563 array('name' => 'userfields', 'value' => 'id')
566 // We need to execute the return values cleaning process to simulate the web service server.
567 $enrolledusers = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_returns(), $enrolledusers);
569 // Check the result set, we should only get the 3rd result, which is $user3.
570 $this->assertCount(1, $enrolledusers);
571 $this->assertEquals($data->user3->id, $enrolledusers[0]['id']);
572 $this->assertArrayHasKey('id', $enrolledusers[0]);
573 $this->assertArrayNotHasKey('email', $enrolledusers[0]);
577 * Test get_enrolled_users from core_enrol_external with capability to
578 * viewparticipants removed.
580 * @expectedException moodle_exception
582 public function test_get_enrolled_users_without_capability() {
583 $capability = 'moodle/course:viewparticipants';
584 $data = $this->get_enrolled_users_setup($capability);
586 // Call without required capability.
587 $this->unassignUserCapability($capability, $data->context->id, $data->roleid);
588 $categories = core_enrol_external::get_enrolled_users($data->course->id);
591 public function get_enrolled_users_with_capability_setup($capability) {
592 global $USER, $DB;
594 $this->resetAfterTest(true);
596 $return = new stdClass();
598 // Create the course and fetch its context.
599 $return->course = self::getDataGenerator()->create_course();
600 $context = context_course::instance($return->course->id);
602 // Create one teacher, and two students.
603 $return->teacher = self::getDataGenerator()->create_user();
604 $return->student1 = self::getDataGenerator()->create_user();
605 $return->student2 = self::getDataGenerator()->create_user();
607 // Create a new student role based on the student archetype but with the capability prohibitted.
608 $fakestudentroleid = create_role('Fake student role', 'fakestudent', 'Fake student role', 'student');
609 assign_capability($capability, CAP_PROHIBIT, $fakestudentroleid, $context->id);
611 // Enrol all of the users in the course.
612 // * 'teacher' is an editing teacher.
613 // * 'student1' is a standard student.
614 // * 'student2' is a student with the capability prohibitted.
615 $editingteacherroleid = $DB->get_field('role', 'id', array('shortname' => 'editingteacher'));
616 $studentroleid = $DB->get_field('role', 'id', array('shortname' => 'student'));
617 $this->getDataGenerator()->enrol_user($return->teacher->id, $return->course->id, $editingteacherroleid);
618 $this->getDataGenerator()->enrol_user($return->student1->id, $return->course->id, $studentroleid);
619 $this->getDataGenerator()->enrol_user($return->student2->id, $return->course->id, $fakestudentroleid);
621 // Log in as the teacher.
622 $this->setUser($return->teacher);
624 // Clear caches.
625 accesslib_clear_all_caches_for_unit_testing();
627 return $return;
631 * Test get_enrolled_users_with_capability without additional paramaters.
633 public function test_get_enrolled_users_with_capability_without_parameters() {
634 $capability = 'moodle/course:viewparticipants';
635 $data = $this->get_enrolled_users_with_capability_setup($capability);
637 $result = core_enrol_external::get_enrolled_users_with_capability(
638 array(
639 'coursecapabilities' => array(
640 'courseid' => $data->course->id,
641 'capabilities' => array(
642 $capability,
646 array()
649 // We need to execute the return values cleaning process to simulate the web service server.
650 $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
652 // Check an array containing the expected user for the course capability is returned.
653 $expecteduserlist = $result[0];
654 $this->assertEquals($data->course->id, $expecteduserlist['courseid']);
655 $this->assertEquals($capability, $expecteduserlist['capability']);
656 $this->assertEquals(2, count($expecteduserlist['users']));
660 * Test get_enrolled_users_with_capability
662 public function test_get_enrolled_users_with_capability_with_parameters () {
663 $capability = 'moodle/course:viewparticipants';
664 $data = $this->get_enrolled_users_with_capability_setup($capability);
666 $result = core_enrol_external::get_enrolled_users_with_capability(
667 array(
668 'coursecapabilities' => array(
669 'courseid' => $data->course->id,
670 'capabilities' => array(
671 $capability,
675 array(
676 array('name' => 'limitfrom', 'value' => 1),
677 array('name' => 'limitnumber', 'value' => 1),
678 array('name' => 'userfields', 'value' => 'id')
682 // We need to execute the return values cleaning process to simulate the web service server.
683 $result = external_api::clean_returnvalue(core_enrol_external::get_enrolled_users_with_capability_returns(), $result);
685 // Check an array containing the expected user for the course capability is returned.
686 $expecteduserlist = $result[0]['users'];
687 $expecteduser = reset($expecteduserlist);
688 $this->assertEquals(1, count($expecteduserlist));
689 $this->assertEquals($data->student1->id, $expecteduser['id']);