bf9edd81fdcc4f3f04333b786dab98f9538a5382
[moodle.git] / user / tests / externallib_test.php
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/>.
17 /**
18  * User external PHPunit tests
19  *
20  * @package    core_user
21  * @category   external
22  * @copyright  2012 Jerome Mouneyrac
23  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  * @since Moodle 2.4
25  */
27 defined('MOODLE_INTERNAL') || die();
29 global $CFG;
31 require_once($CFG->dirroot . '/webservice/tests/helpers.php');
32 require_once($CFG->dirroot . '/user/externallib.php');
33 require_once($CFG->dirroot . '/files/externallib.php');
35 class core_user_externallib_testcase extends externallib_advanced_testcase {
37     /**
38      * Test get_users
39      */
40     public function test_get_users() {
41         global $USER, $CFG;
43         $this->resetAfterTest(true);
45         $course = self::getDataGenerator()->create_course();
47         $user1 = array(
48             'username' => 'usernametest1',
49             'idnumber' => 'idnumbertest1',
50             'firstname' => 'First Name User Test 1',
51             'lastname' => 'Last Name User Test 1',
52             'email' => 'usertest1@example.com',
53             'address' => '2 Test Street Perth 6000 WA',
54             'phone1' => '01010101010',
55             'phone2' => '02020203',
56             'icq' => 'testuser1',
57             'skype' => 'testuser1',
58             'yahoo' => 'testuser1',
59             'aim' => 'testuser1',
60             'msn' => 'testuser1',
61             'department' => 'Department of user 1',
62             'institution' => 'Institution of user 1',
63             'description' => 'This is a description for user 1',
64             'descriptionformat' => FORMAT_MOODLE,
65             'city' => 'Perth',
66             'url' => 'http://moodle.org',
67             'country' => 'AU'
68             );
70         $user1 = self::getDataGenerator()->create_user($user1);
71         set_config('usetags', 1);
72         require_once($CFG->dirroot . '/user/editlib.php');
73         $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
74         useredit_update_interests($user1, $user1->interests);
76         $user2 = self::getDataGenerator()->create_user(
77                 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
79         $generatedusers = array();
80         $generatedusers[$user1->id] = $user1;
81         $generatedusers[$user2->id] = $user2;
83         $context = context_course::instance($course->id);
84         $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
86         // Enrol the users in the course.
87         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid);
88         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid);
89         $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid);
91         // call as admin and receive all possible fields.
92         $this->setAdminUser();
94         $searchparams = array(
95             array('key' => 'invalidkey', 'value' => 'invalidkey'),
96             array('key' => 'email', 'value' => $user1->email),
97             array('key' => 'firstname', 'value' => $user1->firstname));
99         // Call the external function.
100         $result = core_user_external::get_users($searchparams);
102         // We need to execute the return values cleaning process to simulate the web service server
103         $result = external_api::clean_returnvalue(core_user_external::get_users_returns(), $result);
105         // Check we retrieve the good total number of enrolled users + no error on capability.
106         $expectedreturnedusers = 1;
107         $returnedusers = $result['users'];
108         $this->assertEquals($expectedreturnedusers, count($returnedusers));
110         foreach($returnedusers as $returneduser) {
111             $generateduser = ($returneduser['id'] == $USER->id) ?
112                                 $USER : $generatedusers[$returneduser['id']];
113             $this->assertEquals($generateduser->username, $returneduser['username']);
114             if (!empty($generateduser->idnumber)) {
115                 $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']);
116             }
117             $this->assertEquals($generateduser->firstname, $returneduser['firstname']);
118             $this->assertEquals($generateduser->lastname, $returneduser['lastname']);
119             if ($generateduser->email != $USER->email) { // Don't check the tmp modified $USER email.
120                 $this->assertEquals($generateduser->email, $returneduser['email']);
121             }
122             if (!empty($generateduser->address)) {
123                 $this->assertEquals($generateduser->address, $returneduser['address']);
124             }
125             if (!empty($generateduser->phone1)) {
126                 $this->assertEquals($generateduser->phone1, $returneduser['phone1']);
127             }
128             if (!empty($generateduser->phone2)) {
129                 $this->assertEquals($generateduser->phone2, $returneduser['phone2']);
130             }
131             if (!empty($generateduser->icq)) {
132                 $this->assertEquals($generateduser->icq, $returneduser['icq']);
133             }
134             if (!empty($generateduser->skype)) {
135                 $this->assertEquals($generateduser->skype, $returneduser['skype']);
136             }
137             if (!empty($generateduser->yahoo)) {
138                 $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']);
139             }
140             if (!empty($generateduser->aim)) {
141                 $this->assertEquals($generateduser->aim, $returneduser['aim']);
142             }
143             if (!empty($generateduser->msn)) {
144                 $this->assertEquals($generateduser->msn, $returneduser['msn']);
145             }
146             if (!empty($generateduser->department)) {
147                 $this->assertEquals($generateduser->department, $returneduser['department']);
148             }
149             if (!empty($generateduser->institution)) {
150                 $this->assertEquals($generateduser->institution, $returneduser['institution']);
151             }
152             if (!empty($generateduser->description)) {
153                 $this->assertEquals($generateduser->description, $returneduser['description']);
154             }
155             if (!empty($generateduser->descriptionformat)) {
156                 $this->assertEquals(FORMAT_HTML, $returneduser['descriptionformat']);
157             }
158             if (!empty($generateduser->city)) {
159                 $this->assertEquals($generateduser->city, $returneduser['city']);
160             }
161             if (!empty($generateduser->country)) {
162                 $this->assertEquals($generateduser->country, $returneduser['country']);
163             }
164             if (!empty($generateduser->url)) {
165                 $this->assertEquals($generateduser->url, $returneduser['url']);
166             }
167             if (!empty($CFG->usetags) and !empty($generateduser->interests)) {
168                 $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']);
169             }
170         }
172         // Test the invalid key warning.
173         $warnings = $result['warnings'];
174         $this->assertEquals(count($warnings), 1);
175         $warning = array_pop($warnings);
176         $this->assertEquals($warning['item'], 'invalidkey');
177         $this->assertEquals($warning['warningcode'], 'invalidfieldparameter');
179         // Test sending twice the same search field.
180         try {
181             $searchparams = array(
182             array('key' => 'firstname', 'value' => 'Canard'),
183             array('key' => 'email', 'value' => $user1->email),
184             array('key' => 'firstname', 'value' => $user1->firstname));
186             // Call the external function.
187             $result = core_user_external::get_users($searchparams);
188             $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
189         } catch (moodle_exception $e) {
190             $this->assertEquals('keyalreadyset', $e->errorcode);
191         } catch (Exception $e) {
192             $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
193         }
194     }
196     /**
197      * Test get_users_by_field
198      */
199     public function test_get_users_by_field() {
200         global $USER, $CFG;
202         $this->resetAfterTest(true);
204         $course = self::getDataGenerator()->create_course();
205         $user1 = array(
206             'username' => 'usernametest1',
207             'idnumber' => 'idnumbertest1',
208             'firstname' => 'First Name User Test 1',
209             'lastname' => 'Last Name User Test 1',
210             'email' => 'usertest1@example.com',
211             'address' => '2 Test Street Perth 6000 WA',
212             'phone1' => '01010101010',
213             'phone2' => '02020203',
214             'icq' => 'testuser1',
215             'skype' => 'testuser1',
216             'yahoo' => 'testuser1',
217             'aim' => 'testuser1',
218             'msn' => 'testuser1',
219             'department' => 'Department of user 1',
220             'institution' => 'Institution of user 1',
221             'description' => 'This is a description for user 1',
222             'descriptionformat' => FORMAT_MOODLE,
223             'city' => 'Perth',
224             'url' => 'http://moodle.org',
225             'country' => 'AU'
226             );
227         $user1 = self::getDataGenerator()->create_user($user1);
228         if (!empty($CFG->usetags)) {
229             require_once($CFG->dirroot . '/user/editlib.php');
230             $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
231             useredit_update_interests($user1, $user1->interests);
232         }
233         $user2 = self::getDataGenerator()->create_user(
234                 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
236         $generatedusers = array();
237         $generatedusers[$user1->id] = $user1;
238         $generatedusers[$user2->id] = $user2;
240         $context = context_course::instance($course->id);
241         $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
243         // Enrol the users in the course.
244         $this->getDataGenerator()->enrol_user($user1->id, $course->id, $roleid, 'manual');
245         $this->getDataGenerator()->enrol_user($user2->id, $course->id, $roleid, 'manual');
246         $this->getDataGenerator()->enrol_user($USER->id, $course->id, $roleid, 'manual');
248         // call as admin and receive all possible fields.
249         $this->setAdminUser();
251         $fieldstosearch = array('id', 'idnumber', 'username', 'email');
253         foreach ($fieldstosearch as $fieldtosearch) {
255             // Call the external function.
256             $returnedusers = core_user_external::get_users_by_field($fieldtosearch,
257                         array($USER->{$fieldtosearch}, $user1->{$fieldtosearch}, $user2->{$fieldtosearch}));
258             $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers);
260             // Expected result differ following the searched field
261             // Admin user in the PHPunit framework doesn't have an idnumber.
262             if ($fieldtosearch == 'idnumber') {
263                 $expectedreturnedusers = 2;
264             } else {
265                 $expectedreturnedusers = 3;
266             }
268             // Check we retrieve the good total number of enrolled users + no error on capability.
269             $this->assertEquals($expectedreturnedusers, count($returnedusers));
271             foreach($returnedusers as $returneduser) {
272                 $generateduser = ($returneduser['id'] == $USER->id) ?
273                                     $USER : $generatedusers[$returneduser['id']];
274                 $this->assertEquals($generateduser->username, $returneduser['username']);
275                 if (!empty($generateduser->idnumber)) {
276                     $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']);
277                 }
278                 $this->assertEquals($generateduser->firstname, $returneduser['firstname']);
279                 $this->assertEquals($generateduser->lastname, $returneduser['lastname']);
280                 if ($generateduser->email != $USER->email) { //don't check the tmp modified $USER email
281                     $this->assertEquals($generateduser->email, $returneduser['email']);
282                 }
283                 if (!empty($generateduser->address)) {
284                     $this->assertEquals($generateduser->address, $returneduser['address']);
285                 }
286                 if (!empty($generateduser->phone1)) {
287                     $this->assertEquals($generateduser->phone1, $returneduser['phone1']);
288                 }
289                 if (!empty($generateduser->phone2)) {
290                     $this->assertEquals($generateduser->phone2, $returneduser['phone2']);
291                 }
292                 if (!empty($generateduser->icq)) {
293                     $this->assertEquals($generateduser->icq, $returneduser['icq']);
294                 }
295                 if (!empty($generateduser->skype)) {
296                     $this->assertEquals($generateduser->skype, $returneduser['skype']);
297                 }
298                 if (!empty($generateduser->yahoo)) {
299                     $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']);
300                 }
301                 if (!empty($generateduser->aim)) {
302                     $this->assertEquals($generateduser->aim, $returneduser['aim']);
303                 }
304                 if (!empty($generateduser->msn)) {
305                     $this->assertEquals($generateduser->msn, $returneduser['msn']);
306                 }
307                 if (!empty($generateduser->department)) {
308                     $this->assertEquals($generateduser->department, $returneduser['department']);
309                 }
310                 if (!empty($generateduser->institution)) {
311                     $this->assertEquals($generateduser->institution, $returneduser['institution']);
312                 }
313                 if (!empty($generateduser->description)) {
314                     $this->assertEquals($generateduser->description, $returneduser['description']);
315                 }
316                 if (!empty($generateduser->descriptionformat) and isset($returneduser['descriptionformat'])) {
317                     $this->assertEquals($generateduser->descriptionformat, $returneduser['descriptionformat']);
318                 }
319                 if (!empty($generateduser->city)) {
320                     $this->assertEquals($generateduser->city, $returneduser['city']);
321                 }
322                 if (!empty($generateduser->country)) {
323                     $this->assertEquals($generateduser->country, $returneduser['country']);
324                 }
325                 if (!empty($generateduser->url)) {
326                     $this->assertEquals($generateduser->url, $returneduser['url']);
327                 }
328                 if (!empty($CFG->usetags) and !empty($generateduser->interests)) {
329                     $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']);
330                 }
331             }
332         }
334         // Test that no result are returned for search by username if we are not admin
335         $this->setGuestUser();
337         // Call the external function.
338         $returnedusers = core_user_external::get_users_by_field('username',
339                     array($USER->username, $user1->username, $user2->username));
340         $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers);
342         // Only the own $USER username should be returned
343         $this->assertEquals(1, count($returnedusers));
345         // And finally test as one of the enrolled users.
346         $this->setUser($user1);
348         // Call the external function.
349         $returnedusers = core_user_external::get_users_by_field('username',
350             array($USER->username, $user1->username, $user2->username));
351         $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_field_returns(), $returnedusers);
353         // Only the own $USER username should be returned still.
354         $this->assertEquals(1, count($returnedusers));
355     }
357     public function get_course_user_profiles_setup($capability) {
358         global $USER, $CFG;
360         $this->resetAfterTest(true);
362         $return = new stdClass();
364         // Create the course and fetch its context.
365         $return->course = self::getDataGenerator()->create_course();
366         $return->user1 = array(
367             'username' => 'usernametest1',
368             'idnumber' => 'idnumbertest1',
369             'firstname' => 'First Name User Test 1',
370             'lastname' => 'Last Name User Test 1',
371             'email' => 'usertest1@example.com',
372             'address' => '2 Test Street Perth 6000 WA',
373             'phone1' => '01010101010',
374             'phone2' => '02020203',
375             'icq' => 'testuser1',
376             'skype' => 'testuser1',
377             'yahoo' => 'testuser1',
378             'aim' => 'testuser1',
379             'msn' => 'testuser1',
380             'department' => 'Department of user 1',
381             'institution' => 'Institution of user 1',
382             'description' => 'This is a description for user 1',
383             'descriptionformat' => FORMAT_MOODLE,
384             'city' => 'Perth',
385             'url' => 'http://moodle.org',
386             'country' => 'AU'
387         );
388         $return->user1 = self::getDataGenerator()->create_user($return->user1);
389         if (!empty($CFG->usetags)) {
390             require_once($CFG->dirroot . '/user/editlib.php');
391             $return->user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
392             useredit_update_interests($return->user1, $return->user1->interests);
393         }
394         $return->user2 = self::getDataGenerator()->create_user();
396         $context = context_course::instance($return->course->id);
397         $return->roleid = $this->assignUserCapability($capability, $context->id);
399         // Enrol the users in the course.
400         $this->getDataGenerator()->enrol_user($return->user1->id, $return->course->id, $return->roleid, 'manual');
401         $this->getDataGenerator()->enrol_user($return->user2->id, $return->course->id, $return->roleid, 'manual');
402         $this->getDataGenerator()->enrol_user($USER->id, $return->course->id, $return->roleid, 'manual');
404         return $return;
405     }
407     /**
408      * Test get_course_user_profiles
409      */
410     public function test_get_course_user_profiles() {
411         global $USER, $CFG;
413         $this->resetAfterTest(true);
415         $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails');
417         // Call the external function.
418         $enrolledusers = core_user_external::get_course_user_profiles(array(
419                     array('userid' => $USER->id, 'courseid' => $data->course->id)));
421         // We need to execute the return values cleaning process to simulate the web service server.
422         $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);
424         // Check we retrieve the good total number of enrolled users + no error on capability.
425         $this->assertEquals(1, count($enrolledusers));
426     }
428     public function test_get_user_course_profile_as_admin() {
429         global $USER, $CFG;
431         global $USER, $CFG;
433         $this->resetAfterTest(true);
435         $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails');
437         // Do the same call as admin to receive all possible fields.
438         $this->setAdminUser();
439         $USER->email = "admin@example.com";
441         // Call the external function.
442         $enrolledusers = core_user_external::get_course_user_profiles(array(
443             array('userid' => $data->user1->id, 'courseid' => $data->course->id)));
445         // We need to execute the return values cleaning process to simulate the web service server.
446         $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);
448         foreach($enrolledusers as $enrolleduser) {
449             if ($enrolleduser['username'] == $data->user1->username) {
450                 $this->assertEquals($data->user1->idnumber, $enrolleduser['idnumber']);
451                 $this->assertEquals($data->user1->firstname, $enrolleduser['firstname']);
452                 $this->assertEquals($data->user1->lastname, $enrolleduser['lastname']);
453                 $this->assertEquals($data->user1->email, $enrolleduser['email']);
454                 $this->assertEquals($data->user1->address, $enrolleduser['address']);
455                 $this->assertEquals($data->user1->phone1, $enrolleduser['phone1']);
456                 $this->assertEquals($data->user1->phone2, $enrolleduser['phone2']);
457                 $this->assertEquals($data->user1->icq, $enrolleduser['icq']);
458                 $this->assertEquals($data->user1->skype, $enrolleduser['skype']);
459                 $this->assertEquals($data->user1->yahoo, $enrolleduser['yahoo']);
460                 $this->assertEquals($data->user1->aim, $enrolleduser['aim']);
461                 $this->assertEquals($data->user1->msn, $enrolleduser['msn']);
462                 $this->assertEquals($data->user1->department, $enrolleduser['department']);
463                 $this->assertEquals($data->user1->institution, $enrolleduser['institution']);
464                 $this->assertEquals($data->user1->description, $enrolleduser['description']);
465                 $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']);
466                 $this->assertEquals($data->user1->city, $enrolleduser['city']);
467                 $this->assertEquals($data->user1->country, $enrolleduser['country']);
468                 $this->assertEquals($data->user1->url, $enrolleduser['url']);
469                 if (!empty($CFG->usetags)) {
470                     $this->assertEquals(implode(', ', $data->user1->interests), $enrolleduser['interests']);
471                 }
472             }
473         }
474     }
476     /**
477      * Test create_users
478      */
479     public function test_create_users() {
480          global $USER, $CFG, $DB;
482         $this->resetAfterTest(true);
484         $user1 = array(
485             'username' => 'usernametest1',
486             'password' => 'Moodle2012!',
487             'idnumber' => 'idnumbertest1',
488             'firstname' => 'First Name User Test 1',
489             'lastname' => 'Last Name User Test 1',
490             'middlename' => 'Middle Name User Test 1',
491             'lastnamephonetic' => '最後のお名前のテスト一号',
492             'firstnamephonetic' => 'お名前のテスト一号',
493             'alternatename' => 'Alternate Name User Test 1',
494             'email' => 'usertest1@example.com',
495             'description' => 'This is a description for user 1',
496             'city' => 'Perth',
497             'country' => 'AU',
498             'preferences' => [[
499                     'type' => 'htmleditor',
500                     'value' => 'atto'
501                 ], [
502                     'type' => 'invalidpreference',
503                     'value' => 'abcd'
504                 ]]
505             );
507         $context = context_system::instance();
508         $roleid = $this->assignUserCapability('moodle/user:create', $context->id);
509         $this->assignUserCapability('moodle/user:editprofile', $context->id, $roleid);
511         // Call the external function.
512         $createdusers = core_user_external::create_users(array($user1));
514         // We need to execute the return values cleaning process to simulate the web service server.
515         $createdusers = external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers);
517         // Check we retrieve the good total number of created users + no error on capability.
518         $this->assertEquals(1, count($createdusers));
520         foreach($createdusers as $createduser) {
521             $dbuser = $DB->get_record('user', array('id' => $createduser['id']));
522             $this->assertEquals($dbuser->username, $user1['username']);
523             $this->assertEquals($dbuser->idnumber, $user1['idnumber']);
524             $this->assertEquals($dbuser->firstname, $user1['firstname']);
525             $this->assertEquals($dbuser->lastname, $user1['lastname']);
526             $this->assertEquals($dbuser->email, $user1['email']);
527             $this->assertEquals($dbuser->description, $user1['description']);
528             $this->assertEquals($dbuser->city, $user1['city']);
529             $this->assertEquals($dbuser->country, $user1['country']);
530             $this->assertEquals('atto', get_user_preferences('htmleditor', null, $dbuser));
531             $this->assertEquals(null, get_user_preferences('invalidpreference', null, $dbuser));
532         }
534         // Call without required capability
535         $this->unassignUserCapability('moodle/user:create', $context->id, $roleid);
536         $this->expectException('required_capability_exception');
537         $createdusers = core_user_external::create_users(array($user1));
538     }
540     /**
541      * Test delete_users
542      */
543     public function test_delete_users() {
544         global $USER, $CFG, $DB;
546         $this->resetAfterTest(true);
548         $user1 = self::getDataGenerator()->create_user();
549         $user2 = self::getDataGenerator()->create_user();
551         // Check the users were correctly created.
552         $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
553                 array('userid1' => $user1->id, 'userid2' => $user2->id)));
555         $context = context_system::instance();
556         $roleid = $this->assignUserCapability('moodle/user:delete', $context->id);
558         // Call the external function.
559         core_user_external::delete_users(array($user1->id, $user2->id));
561         // Check we retrieve no users + no error on capability.
562         $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
563                 array('userid1' => $user1->id, 'userid2' => $user2->id)));
565         // Call without required capability.
566         $this->unassignUserCapability('moodle/user:delete', $context->id, $roleid);
567         $this->expectException('required_capability_exception');
568         core_user_external::delete_users(array($user1->id, $user2->id));
569     }
571     /**
572      * Test update_users
573      */
574     public function test_update_users() {
575         global $USER, $CFG, $DB;
577         $this->resetAfterTest(true);
579         $wsuser = self::getDataGenerator()->create_user();
580         self::setUser($wsuser);
582         $context = context_user::instance($USER->id);
583         $contextid = $context->id;
584         $filename = "reddot.png";
585         $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38"
586             . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
588         // Call the files api to create a file.
589         $draftfile = core_files_external::upload($contextid, 'user', 'draft', 0, '/',
590                 $filename, $filecontent, null, null);
591         $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile);
593         $draftid = $draftfile['itemid'];
595         $user1 = self::getDataGenerator()->create_user();
597         $user1 = array(
598             'id' => $user1->id,
599             'username' => 'usernametest1',
600             'password' => 'Moodle2012!',
601             'idnumber' => 'idnumbertest1',
602             'firstname' => 'First Name User Test 1',
603             'lastname' => 'Last Name User Test 1',
604             'middlename' => 'Middle Name User Test 1',
605             'lastnamephonetic' => '最後のお名前のテスト一号',
606             'firstnamephonetic' => 'お名前のテスト一号',
607             'alternatename' => 'Alternate Name User Test 1',
608             'email' => 'usertest1@example.com',
609             'description' => 'This is a description for user 1',
610             'city' => 'Perth',
611             'userpicture' => $draftid,
612             'country' => 'AU',
613             'preferences' => [[
614                     'type' => 'htmleditor',
615                     'value' => 'atto'
616                 ], [
617                     'type' => 'invialidpreference',
618                     'value' => 'abcd'
619                 ]]
620             );
622         $context = context_system::instance();
623         $roleid = $this->assignUserCapability('moodle/user:update', $context->id);
624         $this->assignUserCapability('moodle/user:editprofile', $context->id, $roleid);
626         // Check we can't update deleted users, guest users, site admin.
627         $user2 = $user3 = $user4 = $user1;
628         $user2['id'] = $CFG->siteguest;
630         $siteadmins = explode(',', $CFG->siteadmins);
631         $user3['id'] = array_shift($siteadmins);
633         $userdeleted = self::getDataGenerator()->create_user();
634         $user4['id'] = $userdeleted->id;
635         user_delete_user($userdeleted);
637         // Call the external function.
638         core_user_external::update_users(array($user1, $user2, $user3, $user4));
640         $dbuser2 = $DB->get_record('user', array('id' => $user2['id']));
641         $this->assertNotEquals($dbuser2->username, $user2['username']);
642         $dbuser3 = $DB->get_record('user', array('id' => $user3['id']));
643         $this->assertNotEquals($dbuser3->username, $user3['username']);
644         $dbuser4 = $DB->get_record('user', array('id' => $user4['id']));
645         $this->assertNotEquals($dbuser4->username, $user4['username']);
647         $dbuser = $DB->get_record('user', array('id' => $user1['id']));
648         $this->assertEquals($dbuser->username, $user1['username']);
649         $this->assertEquals($dbuser->idnumber, $user1['idnumber']);
650         $this->assertEquals($dbuser->firstname, $user1['firstname']);
651         $this->assertEquals($dbuser->lastname, $user1['lastname']);
652         $this->assertEquals($dbuser->email, $user1['email']);
653         $this->assertEquals($dbuser->description, $user1['description']);
654         $this->assertEquals($dbuser->city, $user1['city']);
655         $this->assertEquals($dbuser->country, $user1['country']);
656         $this->assertNotEquals(0, $dbuser->picture, 'Picture must be set to the new icon itemid for this user');
657         $this->assertEquals('atto', get_user_preferences('htmleditor', null, $dbuser));
658         $this->assertEquals(null, get_user_preferences('invalidpreference', null, $dbuser));
660         // Confirm no picture change when parameter is not supplied.
661         unset($user1['userpicture']);
662         core_user_external::update_users(array($user1));
663         $dbusernopic = $DB->get_record('user', array('id' => $user1['id']));
664         $this->assertEquals($dbuser->picture, $dbusernopic->picture, 'Picture not change without the parameter.');
666         // Confirm delete of picture deletes the picture from the user record.
667         $user1['userpicture'] = 0;
668         core_user_external::update_users(array($user1));
669         $dbuserdelpic = $DB->get_record('user', array('id' => $user1['id']));
670         $this->assertEquals(0, $dbuserdelpic->picture, 'Picture must be deleted when sent as 0.');
673         // Call without required capability.
674         $this->unassignUserCapability('moodle/user:update', $context->id, $roleid);
675         $this->expectException('required_capability_exception');
676         core_user_external::update_users(array($user1));
677     }
679     /**
680      * Test add_user_private_files
681      */
682     public function test_add_user_private_files() {
683         global $USER, $CFG, $DB;
685         $this->resetAfterTest(true);
687         $context = context_system::instance();
688         $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id);
690         $context = context_user::instance($USER->id);
691         $contextid = $context->id;
692         $component = "user";
693         $filearea = "draft";
694         $itemid = 0;
695         $filepath = "/";
696         $filename = "Simple.txt";
697         $filecontent = base64_encode("Let us create a nice simple file");
698         $contextlevel = null;
699         $instanceid = null;
700         $browser = get_file_browser();
702         // Call the files api to create a file.
703         $draftfile = core_files_external::upload($contextid, $component, $filearea, $itemid, $filepath,
704                                                  $filename, $filecontent, $contextlevel, $instanceid);
705         $draftfile = external_api::clean_returnvalue(core_files_external::upload_returns(), $draftfile);
707         $draftid = $draftfile['itemid'];
708         // Make sure the file was created.
709         $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
710         $this->assertNotEmpty($file);
712         // Make sure the file does not exist in the user private files.
713         $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
714         $this->assertEmpty($file);
716         // Call the external function.
717         core_user_external::add_user_private_files($draftid);
719         // Make sure the file was added to the user private files.
720         $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
721         $this->assertNotEmpty($file);
722     }
724     /**
725      * Test add user device
726      */
727     public function test_add_user_device() {
728         global $USER, $CFG, $DB;
730         $this->resetAfterTest(true);
732         $device = array(
733                 'appid' => 'com.moodle.moodlemobile',
734                 'name' => 'occam',
735                 'model' => 'Nexus 4',
736                 'platform' => 'Android',
737                 'version' => '4.2.2',
738                 'pushid' => 'apushdkasdfj4835',
739                 'uuid' => 'asdnfl348qlksfaasef859'
740                 );
742         // Call the external function.
743         core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
744                                             $device['version'], $device['pushid'], $device['uuid']);
746         $created = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
747         $created = (array) $created;
749         $this->assertEquals($device, array_intersect_key((array)$created, $device));
751         // Test reuse the same pushid value.
752         $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
753                                                         $device['version'], $device['pushid'], $device['uuid']);
754         // We need to execute the return values cleaning process to simulate the web service server.
755         $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings);
756         $this->assertCount(1, $warnings);
758         // Test update an existing device.
759         $device['pushid'] = 'different than before';
760         $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
761                                                         $device['version'], $device['pushid'], $device['uuid']);
762         $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings);
764         $this->assertEquals(1, $DB->count_records('user_devices'));
765         $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
766         $this->assertEquals($device, array_intersect_key((array)$updated, $device));
768         // Test creating a new device just changing the uuid.
769         $device['uuid'] = 'newuidforthesameuser';
770         $device['pushid'] = 'new different than before';
771         $warnings = core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
772                                                         $device['version'], $device['pushid'], $device['uuid']);
773         $warnings = external_api::clean_returnvalue(core_user_external::add_user_device_returns(), $warnings);
774         $this->assertEquals(2, $DB->count_records('user_devices'));
775     }
777     /**
778      * Test remove user device
779      */
780     public function test_remove_user_device() {
781         global $USER, $CFG, $DB;
783         $this->resetAfterTest(true);
785         $device = array(
786                 'appid' => 'com.moodle.moodlemobile',
787                 'name' => 'occam',
788                 'model' => 'Nexus 4',
789                 'platform' => 'Android',
790                 'version' => '4.2.2',
791                 'pushid' => 'apushdkasdfj4835',
792                 'uuid' => 'ABCDE3723ksdfhasfaasef859'
793                 );
795         // A device with the same properties except the appid and pushid.
796         $device2 = $device;
797         $device2['pushid'] = "0987654321";
798         $device2['appid'] = "other.app.com";
800         $this->setAdminUser();
801         // Create a user device using the external API function.
802         core_user_external::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
803                                             $device['version'], $device['pushid'], $device['uuid']);
805         // Create the same device but for a different app.
806         core_user_external::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'],
807                                             $device2['version'], $device2['pushid'], $device2['uuid']);
809         // Try to remove a device that does not exist.
810         $result = core_user_external::remove_user_device('1234567890');
811         $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
812         $this->assertFalse($result['removed']);
813         $this->assertCount(1, $result['warnings']);
815         // Try to remove a device that does not exist for an existing app.
816         $result = core_user_external::remove_user_device('1234567890', $device['appid']);
817         $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
818         $this->assertFalse($result['removed']);
819         $this->assertCount(1, $result['warnings']);
821         // Remove an existing device for an existing app. This will remove one of the two devices.
822         $result = core_user_external::remove_user_device($device['uuid'], $device['appid']);
823         $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
824         $this->assertTrue($result['removed']);
826         // Remove all the devices. This must remove the remaining device.
827         $result = core_user_external::remove_user_device($device['uuid']);
828         $result = external_api::clean_returnvalue(core_user_external::remove_user_device_returns(), $result);
829         $this->assertTrue($result['removed']);
830     }
832     /**
833      * Test get_user_preferences
834      */
835     public function test_get_user_preferences() {
836         $this->resetAfterTest(true);
838         $user = self::getDataGenerator()->create_user();
839         set_user_preference('calendar_maxevents', 1, $user);
840         set_user_preference('some_random_text', 'text', $user);
842         $this->setUser($user);
844         $result = core_user_external::get_user_preferences();
845         $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
846         $this->assertCount(0, $result['warnings']);
847         // Expect 3, _lastloaded is always returned.
848         $this->assertCount(3, $result['preferences']);
850         foreach ($result['preferences'] as $pref) {
851             if ($pref['name'] === '_lastloaded') {
852                 continue;
853             }
854             // Check we receive the expected preferences.
855             $this->assertEquals(get_user_preferences($pref['name']), $pref['value']);
856         }
858         // Retrieve just one preference.
859         $result = core_user_external::get_user_preferences('some_random_text');
860         $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
861         $this->assertCount(0, $result['warnings']);
862         $this->assertCount(1, $result['preferences']);
863         $this->assertEquals('text', $result['preferences'][0]['value']);
865         // Retrieve non-existent preference.
866         $result = core_user_external::get_user_preferences('non_existent');
867         $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
868         $this->assertCount(0, $result['warnings']);
869         $this->assertCount(1, $result['preferences']);
870         $this->assertEquals(null, $result['preferences'][0]['value']);
872         // Check that as admin we can retrieve all the preferences for any user.
873         $this->setAdminUser();
874         $result = core_user_external::get_user_preferences('', $user->id);
875         $result = external_api::clean_returnvalue(core_user_external::get_user_preferences_returns(), $result);
876         $this->assertCount(0, $result['warnings']);
877         $this->assertCount(3, $result['preferences']);
879         foreach ($result['preferences'] as $pref) {
880             if ($pref['name'] === '_lastloaded') {
881                 continue;
882             }
883             // Check we receive the expected preferences.
884             $this->assertEquals(get_user_preferences($pref['name'], null, $user), $pref['value']);
885         }
887         // Check that as a non admin user we cannot retrieve other users preferences.
888         $anotheruser = self::getDataGenerator()->create_user();
889         $this->setUser($anotheruser);
891         $this->expectException('required_capability_exception');
892         $result = core_user_external::get_user_preferences('', $user->id);
893     }
895     /**
896      * Test update_picture
897      */
898     public function test_update_picture() {
899         global $DB, $USER;
901         $this->resetAfterTest(true);
903         $user = self::getDataGenerator()->create_user();
904         self::setUser($user);
906         $context = context_user::instance($USER->id);
907         $contextid = $context->id;
908         $filename = "reddot.png";
909         $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38"
910             . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
912         // Call the files api to create a file.
913         $draftfile = core_files_external::upload($contextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null);
914         $draftid = $draftfile['itemid'];
916         // Change user profile image.
917         $result = core_user_external::update_picture($draftid);
918         $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result);
919         $picture = $DB->get_field('user', 'picture', array('id' => $user->id));
920         // The new revision is in the url for the user.
921         $this->assertContains($picture, $result['profileimageurl']);
922         // Check expected URL for serving the image.
923         $this->assertContains("/$contextid/user/icon", $result['profileimageurl']);
925         // Delete image.
926         $result = core_user_external::update_picture(0, true);
927         $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result);
928         $picture = $DB->get_field('user', 'picture', array('id' => $user->id));
929         // No picture.
930         $this->assertEquals(0, $picture);
932         // Add again the user profile image (as admin).
933         $this->setAdminUser();
935         $context = context_user::instance($USER->id);
936         $admincontextid = $context->id;
937         $draftfile = core_files_external::upload($admincontextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null);
938         $draftid = $draftfile['itemid'];
940         $result = core_user_external::update_picture($draftid, false, $user->id);
941         $result = external_api::clean_returnvalue(core_user_external::update_picture_returns(), $result);
942         // The new revision is in the url for the user.
943         $picture = $DB->get_field('user', 'picture', array('id' => $user->id));
944         $this->assertContains($picture, $result['profileimageurl']);
945         $this->assertContains("/$contextid/user/icon", $result['profileimageurl']);
946     }
948     /**
949      * Test update_picture disabled
950      */
951     public function test_update_picture_disabled() {
952         global $CFG;
953         $this->resetAfterTest(true);
954         $CFG->disableuserimages = true;
956         $this->setAdminUser();
957         $this->expectException('moodle_exception');
958         core_user_external::update_picture(0);
959     }
961     /**
962      * Test set_user_preferences
963      */
964     public function test_set_user_preferences_save() {
965         global $DB;
966         $this->resetAfterTest(true);
968         $user1 = self::getDataGenerator()->create_user();
969         $user2 = self::getDataGenerator()->create_user();
971         // Save users preferences.
972         $this->setAdminUser();
973         $preferences = array(
974             array(
975                 'name' => 'htmleditor',
976                 'value' => 'atto',
977                 'userid' => $user1->id,
978             ),
979             array(
980                 'name' => 'htmleditor',
981                 'value' => 'tinymce',
982                 'userid' => $user2->id,
983             )
984         );
986         $result = core_user_external::set_user_preferences($preferences);
987         $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result);
988         $this->assertCount(0, $result['warnings']);
989         $this->assertCount(2, $result['saved']);
991         // Get preference from DB to avoid cache.
992         $this->assertEquals('atto', $DB->get_field('user_preferences', 'value',
993             array('userid' => $user1->id, 'name' => 'htmleditor')));
994         $this->assertEquals('tinymce', $DB->get_field('user_preferences', 'value',
995             array('userid' => $user2->id, 'name' => 'htmleditor')));
996     }
998     /**
999      * Test set_user_preferences
1000      */
1001     public function test_set_user_preferences_save_invalid_pref() {
1002         global $DB;
1003         $this->resetAfterTest(true);
1005         $user1 = self::getDataGenerator()->create_user();
1007         // Save users preferences.
1008         $this->setAdminUser();
1009         $preferences = array(
1010             array(
1011                 'name' => 'some_random_pref',
1012                 'value' => 'abc',
1013                 'userid' => $user1->id,
1014             ),
1015         );
1017         $result = core_user_external::set_user_preferences($preferences);
1018         $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result);
1019         $this->assertCount(1, $result['warnings']);
1020         $this->assertCount(0, $result['saved']);
1021         $this->assertEquals('nopermission', $result['warnings'][0]['warningcode']);
1023         // Nothing was written to DB.
1024         $this->assertEmpty($DB->count_records('user_preferences', array('name' => 'some_random_pref')));
1025     }
1027     /**
1028      * Test set_user_preferences for an invalid user
1029      */
1030     public function test_set_user_preferences_invalid_user() {
1031         $this->resetAfterTest(true);
1033         $this->setAdminUser();
1034         $preferences = array(
1035             array(
1036                 'name' => 'calendar_maxevents',
1037                 'value' => 4,
1038                 'userid' => -2
1039             )
1040         );
1042         $result = core_user_external::set_user_preferences($preferences);
1043         $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result);
1044         $this->assertCount(1, $result['warnings']);
1045         $this->assertCount(0, $result['saved']);
1046         $this->assertEquals('invaliduser', $result['warnings'][0]['warningcode']);
1047         $this->assertEquals(-2, $result['warnings'][0]['itemid']);
1048     }
1050     /**
1051      * Test set_user_preferences using an invalid preference
1052      */
1053     public function test_set_user_preferences_invalid_preference() {
1054         global $USER, $DB;
1056         $this->resetAfterTest(true);
1057         // Create a very long value.
1058         $this->setAdminUser();
1059         $preferences = array(
1060             array(
1061                 'name' => 'calendar_maxevents',
1062                 'value' => str_repeat('a', 1334),
1063                 'userid' => $USER->id
1064             )
1065         );
1067         $result = core_user_external::set_user_preferences($preferences);
1068         $result = external_api::clean_returnvalue(core_user_external::set_user_preferences_returns(), $result);
1069         $this->assertCount(0, $result['warnings']);
1070         $this->assertCount(1, $result['saved']);
1071         // Cleaned valud of the preference was saved.
1072         $this->assertEquals(1, $DB->get_field('user_preferences', 'value',
1073             array('userid' => $USER->id, 'name' => 'calendar_maxevents')));
1074     }
1076     /**
1077      * Test set_user_preferences for other user not being admin
1078      */
1079     public function test_set_user_preferences_capability() {
1080         $this->resetAfterTest(true);
1082         $user1 = self::getDataGenerator()->create_user();
1083         $user2 = self::getDataGenerator()->create_user();
1085         $this->setUser($user1);
1086         $preferences = array(
1087             array(
1088                 'name' => 'calendar_maxevents',
1089                 'value' => 4,
1090                 'userid' => $user2->id
1091             )
1092         );
1094         $result = core_user_external::set_user_preferences($preferences);
1096         $this->assertCount(1, $result['warnings']);
1097         $this->assertCount(0, $result['saved']);
1098         $this->assertEquals('nopermission', $result['warnings'][0]['warningcode']);
1099         $this->assertEquals($user2->id, $result['warnings'][0]['itemid']);
1100     }
1102     /**
1103      * Test agree_site_policy
1104      */
1105     public function test_agree_site_policy() {
1106         global $CFG, $DB, $USER;
1107         $this->resetAfterTest(true);
1109         $user = self::getDataGenerator()->create_user();
1110         $this->setUser($user);
1112         // Site policy not set.
1113         $result = core_user_external::agree_site_policy();
1114         $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result);
1115         $this->assertFalse($result['status']);
1116         $this->assertCount(1, $result['warnings']);
1117         $this->assertEquals('nositepolicy', $result['warnings'][0]['warningcode']);
1119         // Set a policy issue.
1120         $CFG->sitepolicy = 'https://moodle.org';
1121         $this->assertEquals(0, $USER->policyagreed);
1123         $result = core_user_external::agree_site_policy();
1124         $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result);
1125         $this->assertTrue($result['status']);
1126         $this->assertCount(0, $result['warnings']);
1127         $this->assertEquals(1, $USER->policyagreed);
1128         $this->assertEquals(1, $DB->get_field('user', 'policyagreed', array('id' => $USER->id)));
1130         // Try again, we should get a warning.
1131         $result = core_user_external::agree_site_policy();
1132         $result = external_api::clean_returnvalue(core_user_external::agree_site_policy_returns(), $result);
1133         $this->assertFalse($result['status']);
1134         $this->assertCount(1, $result['warnings']);
1135         $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']);
1137         // Set something to make require_login throws an exception.
1138         $otheruser = self::getDataGenerator()->create_user();
1139         $this->setUser($otheruser);
1141         $DB->set_field('user', 'lastname', '', array('id' => $USER->id));
1142         $USER->lastname = '';
1143         try {
1144             $result = core_user_external::agree_site_policy();
1145             $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown');
1146         } catch (moodle_exception $e) {
1147             $this->assertEquals('usernotfullysetup', $e->errorcode);
1148         } catch (Exception $e) {
1149             $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown.');
1150         }
1151     }
1153     /**
1154      * Test get_private_files_info
1155      */
1156     public function test_get_private_files_info() {
1158         $this->resetAfterTest(true);
1159         $user = self::getDataGenerator()->create_user();
1160         $this->setUser($user);
1161         $usercontext = context_user::instance($user->id);
1163         $filerecord = array(
1164             'contextid' => $usercontext->id,
1165             'component' => 'user',
1166             'filearea'  => 'private',
1167             'itemid'    => 0,
1168             'filepath'  => '/',
1169             'filename'  => 'thefile',
1170         );
1172         $fs = get_file_storage();
1173         $file = $fs->create_file_from_string($filerecord, 'abc');
1175         // Get my private files information.
1176         $result = core_user_external::get_private_files_info();
1177         $result = external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result);
1178         $this->assertEquals(1, $result['filecount']);
1179         $this->assertEquals($file->get_filesize(), $result['filesize']);
1180         $this->assertEquals(1, $result['foldercount']);   // Base directory.
1181         $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']);
1183         // As admin, get user information.
1184         $this->setAdminUser();
1185         $result = core_user_external::get_private_files_info($user->id);
1186         $result = external_api::clean_returnvalue(core_user_external::get_private_files_info_returns(), $result);
1187         $this->assertEquals(1, $result['filecount']);
1188         $this->assertEquals($file->get_filesize(), $result['filesize']);
1189         $this->assertEquals(1, $result['foldercount']);   // Base directory.
1190         $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']);
1191     }
1193     /**
1194      * Test get_private_files_info missing permissions.
1195      */
1196     public function test_get_private_files_info_missing_permissions() {
1198         $this->resetAfterTest(true);
1199         $user1 = self::getDataGenerator()->create_user();
1200         $user2 = self::getDataGenerator()->create_user();
1201         $this->setUser($user1);
1203         $this->setExpectedException('required_capability_exception');
1204         // Try to retrieve other user private files info.
1205         core_user_external::get_private_files_info($user2->id);
1206     }