MDL-29938 get_users - search users external function
[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');
34 class core_user_external_testcase extends externallib_advanced_testcase {
36     /**
37      * Test get_users
38      */
39     public function test_get_users() {
40         global $USER, $CFG;
42         $this->resetAfterTest(true);
44         $course = self::getDataGenerator()->create_course();
45         $user1 = array(
46             'username' => 'usernametest1',
47             'idnumber' => 'idnumbertest1',
48             'firstname' => 'First Name User Test 1',
49             'lastname' => 'Last Name User Test 1',
50             'email' => 'usertest1@email.com',
51             'address' => '2 Test Street Perth 6000 WA',
52             'phone1' => '01010101010',
53             'phone2' => '02020203',
54             'icq' => 'testuser1',
55             'skype' => 'testuser1',
56             'yahoo' => 'testuser1',
57             'aim' => 'testuser1',
58             'msn' => 'testuser1',
59             'department' => 'Department of user 1',
60             'institution' => 'Institution of user 1',
61             'description' => 'This is a description for user 1',
62             'descriptionformat' => FORMAT_MOODLE,
63             'city' => 'Perth',
64             'url' => 'http://moodle.org',
65             'country' => 'au'
66             );
67         $user1 = self::getDataGenerator()->create_user($user1);
68         if (!empty($CFG->usetags)) {
69             require_once($CFG->dirroot . '/user/editlib.php');
70             require_once($CFG->dirroot . '/tag/lib.php');
71             $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
72             useredit_update_interests($user1, $user1->interests);
73         }
74         $user2 = self::getDataGenerator()->create_user(
75                 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
77         $generatedusers = array();
78         $generatedusers[$user1->id] = $user1;
79         $generatedusers[$user2->id] = $user2;
81         $context = context_course::instance($course->id);
82         $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
84         // Enrol the users in the course.
85         // We use the manual plugin.
86         $enrol = enrol_get_plugin('manual');
87         $enrolinstances = enrol_get_instances($course->id, true);
88         foreach ($enrolinstances as $courseenrolinstance) {
89             if ($courseenrolinstance->enrol == "manual") {
90                 $instance = $courseenrolinstance;
91                 break;
92             }
93         }
94         $enrol->enrol_user($instance, $user1->id, $roleid);
95         $enrol->enrol_user($instance, $user2->id, $roleid);
96         $enrol->enrol_user($instance, $USER->id, $roleid);
98         // call as admin and receive all possible fields.
99         $this->setAdminUser();
101         $searchparams = array(
102             array('key' => 'email', 'value' => $user1->email),
103             array('key' => 'firstname', 'value' => $user1->firstname));
105         // Call the external function.
106         $result = core_user_external::get_users($searchparams);
108         // Check we retrieve the good total number of enrolled users + no error on capability.
109         $expectedreturnedusers = 1;
110         $returnedusers = $result['users'];
111         $this->assertEquals($expectedreturnedusers, count($returnedusers));
113         foreach($returnedusers as $returneduser) {
114             $generateduser = ($returneduser['id'] == $USER->id) ?
115                                 $USER : $generatedusers[$returneduser['id']];
116             $this->assertEquals($generateduser->username, $returneduser['username']);
117             if (!empty($generateduser->idnumber)) {
118                 $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']);
119             }
120             $this->assertEquals($generateduser->firstname, $returneduser['firstname']);
121             $this->assertEquals($generateduser->lastname, $returneduser['lastname']);
122             if ($generateduser->email != $USER->email) { //don't check the tmp modified $USER email
123                 $this->assertEquals($generateduser->email, $returneduser['email']);
124             }
125             if (!empty($generateduser->address)) {
126                 $this->assertEquals($generateduser->address, $returneduser['address']);
127             }
128             if (!empty($generateduser->phone1)) {
129                 $this->assertEquals($generateduser->phone1, $returneduser['phone1']);
130             }
131             if (!empty($generateduser->phone2)) {
132                 $this->assertEquals($generateduser->phone2, $returneduser['phone2']);
133             }
134             if (!empty($generateduser->icq)) {
135                 $this->assertEquals($generateduser->icq, $returneduser['icq']);
136             }
137             if (!empty($generateduser->skype)) {
138                 $this->assertEquals($generateduser->skype, $returneduser['skype']);
139             }
140             if (!empty($generateduser->yahoo)) {
141                 $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']);
142             }
143             if (!empty($generateduser->aim)) {
144                 $this->assertEquals($generateduser->aim, $returneduser['aim']);
145             }
146             if (!empty($generateduser->msn)) {
147                 $this->assertEquals($generateduser->msn, $returneduser['msn']);
148             }
149             if (!empty($generateduser->department)) {
150                 $this->assertEquals($generateduser->department, $returneduser['department']);
151             }
152             if (!empty($generateduser->institution)) {
153                 $this->assertEquals($generateduser->institution, $returneduser['institution']);
154             }
155             if (!empty($generateduser->description)) {
156                 $this->assertEquals($generateduser->description, $returneduser['description']);
157             }
158             if (!empty($generateduser->descriptionformat)) {
159                 $this->assertEquals(FORMAT_HTML, $returneduser['descriptionformat']);
160             }
161             if (!empty($generateduser->city)) {
162                 $this->assertEquals($generateduser->city, $returneduser['city']);
163             }
164             if (!empty($generateduser->country)) {
165                 $this->assertEquals($generateduser->country, $returneduser['country']);
166             }
167             if (!empty($generateduser->url)) {
168                 $this->assertEquals($generateduser->url, $returneduser['url']);
169             }
170             if (!empty($CFG->usetags) and !empty($generateduser->interests)) {
171                 $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']);
172             }
173         }
175         // Test that no result are returned for search by username if we are not admin
176         $this->setGuestUser();
178         // Call the external function.
179         $returnedusers = core_user_external::get_users_by_field('username',
180                     array($USER->username, $user1->username, $user2->username));
182         // Only the own $USER username should be returned
183         $this->assertEquals(1, count($returnedusers));
184     }
186     /**
187      * Test get_users_by_field
188      */
189     public function test_get_users_by_field() {
190         global $USER, $CFG;
192         $this->resetAfterTest(true);
194         $course = self::getDataGenerator()->create_course();
195         $user1 = array(
196             'username' => 'usernametest1',
197             'idnumber' => 'idnumbertest1',
198             'firstname' => 'First Name User Test 1',
199             'lastname' => 'Last Name User Test 1',
200             'email' => 'usertest1@email.com',
201             'address' => '2 Test Street Perth 6000 WA',
202             'phone1' => '01010101010',
203             'phone2' => '02020203',
204             'icq' => 'testuser1',
205             'skype' => 'testuser1',
206             'yahoo' => 'testuser1',
207             'aim' => 'testuser1',
208             'msn' => 'testuser1',
209             'department' => 'Department of user 1',
210             'institution' => 'Institution of user 1',
211             'description' => 'This is a description for user 1',
212             'descriptionformat' => FORMAT_MOODLE,
213             'city' => 'Perth',
214             'url' => 'http://moodle.org',
215             'country' => 'au'
216             );
217         $user1 = self::getDataGenerator()->create_user($user1);
218         if (!empty($CFG->usetags)) {
219             require_once($CFG->dirroot . '/user/editlib.php');
220             require_once($CFG->dirroot . '/tag/lib.php');
221             $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
222             useredit_update_interests($user1, $user1->interests);
223         }
224         $user2 = self::getDataGenerator()->create_user(
225                 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
227         $generatedusers = array();
228         $generatedusers[$user1->id] = $user1;
229         $generatedusers[$user2->id] = $user2;
231         $context = context_course::instance($course->id);
232         $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
234         // Enrol the users in the course.
235         // We use the manual plugin.
236         $enrol = enrol_get_plugin('manual');
237         $enrolinstances = enrol_get_instances($course->id, true);
238         foreach ($enrolinstances as $courseenrolinstance) {
239             if ($courseenrolinstance->enrol == "manual") {
240                 $instance = $courseenrolinstance;
241                 break;
242             }
243         }
244         $enrol->enrol_user($instance, $user1->id, $roleid);
245         $enrol->enrol_user($instance, $user2->id, $roleid);
246         $enrol->enrol_user($instance, $USER->id, $roleid);
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}));
259             // Expected result differ following the searched field
260             // Admin user in the PHPunit framework doesn't have email or idnumber.
261             if ($fieldtosearch == 'email' or $fieldtosearch == 'idnumber') {
262                 $expectedreturnedusers = 2;
263             } else {
264                 $expectedreturnedusers = 3;
265             }
267             // Check we retrieve the good total number of enrolled users + no error on capability.
268             $this->assertEquals($expectedreturnedusers, count($returnedusers));
270             foreach($returnedusers as $returneduser) {
271                 $generateduser = ($returneduser['id'] == $USER->id) ?
272                                     $USER : $generatedusers[$returneduser['id']];
273                 $this->assertEquals($generateduser->username, $returneduser['username']);
274                 if (!empty($generateduser->idnumber)) {
275                     $this->assertEquals($generateduser->idnumber, $returneduser['idnumber']);
276                 }
277                 $this->assertEquals($generateduser->firstname, $returneduser['firstname']);
278                 $this->assertEquals($generateduser->lastname, $returneduser['lastname']);
279                 if ($generateduser->email != $USER->email) { //don't check the tmp modified $USER email
280                     $this->assertEquals($generateduser->email, $returneduser['email']);
281                 }
282                 if (!empty($generateduser->address)) {
283                     $this->assertEquals($generateduser->address, $returneduser['address']);
284                 }
285                 if (!empty($generateduser->phone1)) {
286                     $this->assertEquals($generateduser->phone1, $returneduser['phone1']);
287                 }
288                 if (!empty($generateduser->phone2)) {
289                     $this->assertEquals($generateduser->phone2, $returneduser['phone2']);
290                 }
291                 if (!empty($generateduser->icq)) {
292                     $this->assertEquals($generateduser->icq, $returneduser['icq']);
293                 }
294                 if (!empty($generateduser->skype)) {
295                     $this->assertEquals($generateduser->skype, $returneduser['skype']);
296                 }
297                 if (!empty($generateduser->yahoo)) {
298                     $this->assertEquals($generateduser->yahoo, $returneduser['yahoo']);
299                 }
300                 if (!empty($generateduser->aim)) {
301                     $this->assertEquals($generateduser->aim, $returneduser['aim']);
302                 }
303                 if (!empty($generateduser->msn)) {
304                     $this->assertEquals($generateduser->msn, $returneduser['msn']);
305                 }
306                 if (!empty($generateduser->department)) {
307                     $this->assertEquals($generateduser->department, $returneduser['department']);
308                 }
309                 if (!empty($generateduser->institution)) {
310                     $this->assertEquals($generateduser->institution, $returneduser['institution']);
311                 }
312                 if (!empty($generateduser->description)) {
313                     $this->assertEquals($generateduser->description, $returneduser['description']);
314                 }
315                 if (!empty($generateduser->descriptionformat)) {
316                     $this->assertEquals(FORMAT_HTML, $returneduser['descriptionformat']);
317                 }
318                 if (!empty($generateduser->city)) {
319                     $this->assertEquals($generateduser->city, $returneduser['city']);
320                 }
321                 if (!empty($generateduser->country)) {
322                     $this->assertEquals($generateduser->country, $returneduser['country']);
323                 }
324                 if (!empty($generateduser->url)) {
325                     $this->assertEquals($generateduser->url, $returneduser['url']);
326                 }
327                 if (!empty($CFG->usetags) and !empty($generateduser->interests)) {
328                     $this->assertEquals(implode(', ', $generateduser->interests), $returneduser['interests']);
329                 }
330             }
331         }
333         // Test that no result are returned for search by username if we are not admin
334         $this->setGuestUser();
336         // Call the external function.
337         $returnedusers = core_user_external::get_users_by_field('username',
338                     array($USER->username, $user1->username, $user2->username));
340         // Only the own $USER username should be returned
341         $this->assertEquals(1, count($returnedusers));
342     }
344     /**
345      * Test get_course_user_profiles
346      */
347     public function test_get_course_user_profiles() {
348         global $USER, $CFG;
350         $this->resetAfterTest(true);
352         $course = self::getDataGenerator()->create_course();
353         $user1 = array(
354             'username' => 'usernametest1',
355             'idnumber' => 'idnumbertest1',
356             'firstname' => 'First Name User Test 1',
357             'lastname' => 'Last Name User Test 1',
358             'email' => 'usertest1@email.com',
359             'address' => '2 Test Street Perth 6000 WA',
360             'phone1' => '01010101010',
361             'phone2' => '02020203',
362             'icq' => 'testuser1',
363             'skype' => 'testuser1',
364             'yahoo' => 'testuser1',
365             'aim' => 'testuser1',
366             'msn' => 'testuser1',
367             'department' => 'Department of user 1',
368             'institution' => 'Institution of user 1',
369             'description' => 'This is a description for user 1',
370             'descriptionformat' => FORMAT_MOODLE,
371             'city' => 'Perth',
372             'url' => 'http://moodle.org',
373             'country' => 'au'
374             );
375         $user1 = self::getDataGenerator()->create_user($user1);
376         if (!empty($CFG->usetags)) {
377             require_once($CFG->dirroot . '/user/editlib.php');
378             require_once($CFG->dirroot . '/tag/lib.php');
379             $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
380             useredit_update_interests($user1, $user1->interests);
381         }
382         $user2 = self::getDataGenerator()->create_user();
384         $context = context_course::instance($course->id);
385         $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
387         // Enrol the users in the course.
388         // We use the manual plugin.
389         $enrol = enrol_get_plugin('manual');
390         $enrolinstances = enrol_get_instances($course->id, true);
391         foreach ($enrolinstances as $courseenrolinstance) {
392             if ($courseenrolinstance->enrol == "manual") {
393                 $instance = $courseenrolinstance;
394                 break;
395             }
396         }
397         $enrol->enrol_user($instance, $user1->id, $roleid);
398         $enrol->enrol_user($instance, $user2->id, $roleid);
399         $enrol->enrol_user($instance, $USER->id, $roleid);
401         // Call the external function.
402         $enrolledusers = core_user_external::get_course_user_profiles(array(
403                     array('userid' => $USER->id, 'courseid' => $course->id),
404                     array('userid' => $user1->id, 'courseid' => $course->id),
405                     array('userid' => $user2->id, 'courseid' => $course->id)));
407         // We need to execute the return values cleaning process to simulate the web service server.
408         $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);
410         // Check we retrieve the good total number of enrolled users + no error on capability.
411         $this->assertEquals(3, count($enrolledusers));
413         // Do the same call as admin to receive all possible fields.
414         $this->setAdminUser();
415         $USER->email = "admin@fakeemail.com";
417         // Call the external function.
418         $enrolledusers = core_user_external::get_course_user_profiles(array(
419                     array('userid' => $USER->id, 'courseid' => $course->id),
420                     array('userid' => $user1->id, 'courseid' => $course->id),
421                     array('userid' => $user2->id, 'courseid' => $course->id)));
423         // We need to execute the return values cleaning process to simulate the web service server.
424         $enrolledusers = external_api::clean_returnvalue(core_user_external::get_course_user_profiles_returns(), $enrolledusers);
426         foreach($enrolledusers as $enrolleduser) {
427             if ($enrolleduser['username'] == $user1->username) {
428                 $this->assertEquals($user1->idnumber, $enrolleduser['idnumber']);
429                 $this->assertEquals($user1->firstname, $enrolleduser['firstname']);
430                 $this->assertEquals($user1->lastname, $enrolleduser['lastname']);
431                 $this->assertEquals($user1->email, $enrolleduser['email']);
432                 $this->assertEquals($user1->address, $enrolleduser['address']);
433                 $this->assertEquals($user1->phone1, $enrolleduser['phone1']);
434                 $this->assertEquals($user1->phone2, $enrolleduser['phone2']);
435                 $this->assertEquals($user1->icq, $enrolleduser['icq']);
436                 $this->assertEquals($user1->skype, $enrolleduser['skype']);
437                 $this->assertEquals($user1->yahoo, $enrolleduser['yahoo']);
438                 $this->assertEquals($user1->aim, $enrolleduser['aim']);
439                 $this->assertEquals($user1->msn, $enrolleduser['msn']);
440                 $this->assertEquals($user1->department, $enrolleduser['department']);
441                 $this->assertEquals($user1->institution, $enrolleduser['institution']);
442                 $this->assertEquals($user1->description, $enrolleduser['description']);
443                 $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']);
444                 $this->assertEquals($user1->city, $enrolleduser['city']);
445                 $this->assertEquals($user1->country, $enrolleduser['country']);
446                 $this->assertEquals($user1->url, $enrolleduser['url']);
447                 if (!empty($CFG->usetags)) {
448                     $this->assertEquals(implode(', ', $user1->interests), $enrolleduser['interests']);
449                 }
450             }
451         }
452     }
454     /**
455      * Test create_users
456      */
457     public function test_create_users() {
458          global $USER, $CFG, $DB;
460         $this->resetAfterTest(true);
462         $user1 = array(
463             'username' => 'usernametest1',
464             'password' => 'Moodle2012!',
465             'idnumber' => 'idnumbertest1',
466             'firstname' => 'First Name User Test 1',
467             'lastname' => 'Last Name User Test 1',
468             'email' => 'usertest1@email.com',
469             'description' => 'This is a description for user 1',
470             'city' => 'Perth',
471             'country' => 'au'
472             );
474         $context = context_system::instance();
475         $roleid = $this->assignUserCapability('moodle/user:create', $context->id);
477         // Call the external function.
478         $createdusers = core_user_external::create_users(array($user1));
480         // We need to execute the return values cleaning process to simulate the web service server.
481         $createdusers = external_api::clean_returnvalue(core_user_external::create_users_returns(), $createdusers);
483         // Check we retrieve the good total number of created users + no error on capability.
484         $this->assertEquals(1, count($createdusers));
486         foreach($createdusers as $createduser) {
487             $dbuser = $DB->get_record('user', array('id' => $createduser['id']));
488             $this->assertEquals($dbuser->username, $user1['username']);
489             $this->assertEquals($dbuser->idnumber, $user1['idnumber']);
490             $this->assertEquals($dbuser->firstname, $user1['firstname']);
491             $this->assertEquals($dbuser->lastname, $user1['lastname']);
492             $this->assertEquals($dbuser->email, $user1['email']);
493             $this->assertEquals($dbuser->description, $user1['description']);
494             $this->assertEquals($dbuser->city, $user1['city']);
495             $this->assertEquals($dbuser->country, $user1['country']);
496         }
498         // Call without required capability
499         $this->unassignUserCapability('moodle/user:create', $context->id, $roleid);
500         $this->setExpectedException('required_capability_exception');
501         $createdusers = core_user_external::create_users(array($user1));
502     }
504     /**
505      * Test delete_users
506      */
507     public function test_delete_users() {
508         global $USER, $CFG, $DB;
510         $this->resetAfterTest(true);
512         $user1 = self::getDataGenerator()->create_user();
513         $user2 = self::getDataGenerator()->create_user();
515         // Check the users were correctly created.
516         $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
517                 array('userid1' => $user1->id, 'userid2' => $user2->id)));
519         $context = context_system::instance();
520         $roleid = $this->assignUserCapability('moodle/user:delete', $context->id);
522         // Call the external function.
523         core_user_external::delete_users(array($user1->id, $user2->id));
525         // Check we retrieve no users + no error on capability.
526         $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
527                 array('userid1' => $user1->id, 'userid2' => $user2->id)));
529         // Call without required capability.
530         $this->unassignUserCapability('moodle/user:delete', $context->id, $roleid);
531         $this->setExpectedException('required_capability_exception');
532         core_user_external::delete_users(array($user1->id, $user2->id));
533     }
535     /**
536      * Test get_users_by_id
537      */
538     public function test_get_users_by_id() {
539         global $USER, $CFG;
541         $this->resetAfterTest(true);
543         $user1 = array(
544             'username' => 'usernametest1',
545             'idnumber' => 'idnumbertest1',
546             'firstname' => 'First Name User Test 1',
547             'lastname' => 'Last Name User Test 1',
548             'email' => 'usertest1@email.com',
549             'address' => '2 Test Street Perth 6000 WA',
550             'phone1' => '01010101010',
551             'phone2' => '02020203',
552             'icq' => 'testuser1',
553             'skype' => 'testuser1',
554             'yahoo' => 'testuser1',
555             'aim' => 'testuser1',
556             'msn' => 'testuser1',
557             'department' => 'Department of user 1',
558             'institution' => 'Institution of user 1',
559             'description' => 'This is a description for user 1',
560             'descriptionformat' => FORMAT_MOODLE,
561             'city' => 'Perth',
562             'url' => 'http://moodle.org',
563             'country' => 'au'
564             );
565         $user1 = self::getDataGenerator()->create_user($user1);
566         if (!empty($CFG->usetags)) {
567             require_once($CFG->dirroot . '/user/editlib.php');
568             require_once($CFG->dirroot . '/tag/lib.php');
569             $user1->interests = array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
570             useredit_update_interests($user1, $user1->interests);
571         }
572         $user2 = self::getDataGenerator()->create_user();
574         $context = context_system::instance();
575         $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id);
577         // Call the external function.
578         $returnedusers = core_user_external::get_users_by_id(array(
579                     $USER->id, $user1->id, $user2->id));
581         // We need to execute the return values cleaning process to simulate the web service server.
582         $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_id_returns(), $returnedusers);
584         // Check we retrieve the good total number of enrolled users + no error on capability.
585         $this->assertEquals(3, count($returnedusers));
587         // Do the same call as admin to receive all possible fields.
588         $this->setAdminUser();
589         $USER->email = "admin@fakeemail.com";
591         // Call the external function.
592         $returnedusers = core_user_external::get_users_by_id(array(
593                     $USER->id, $user1->id, $user2->id));
595         // We need to execute the return values cleaning process to simulate the web service server.
596         $returnedusers = external_api::clean_returnvalue(core_user_external::get_users_by_id_returns(), $returnedusers);
598         foreach($returnedusers as $enrolleduser) {
599             if ($enrolleduser['username'] == $user1->username) {
600                 $this->assertEquals($user1->idnumber, $enrolleduser['idnumber']);
601                 $this->assertEquals($user1->firstname, $enrolleduser['firstname']);
602                 $this->assertEquals($user1->lastname, $enrolleduser['lastname']);
603                 $this->assertEquals($user1->email, $enrolleduser['email']);
604                 $this->assertEquals($user1->address, $enrolleduser['address']);
605                 $this->assertEquals($user1->phone1, $enrolleduser['phone1']);
606                 $this->assertEquals($user1->phone2, $enrolleduser['phone2']);
607                 $this->assertEquals($user1->icq, $enrolleduser['icq']);
608                 $this->assertEquals($user1->skype, $enrolleduser['skype']);
609                 $this->assertEquals($user1->yahoo, $enrolleduser['yahoo']);
610                 $this->assertEquals($user1->aim, $enrolleduser['aim']);
611                 $this->assertEquals($user1->msn, $enrolleduser['msn']);
612                 $this->assertEquals($user1->department, $enrolleduser['department']);
613                 $this->assertEquals($user1->institution, $enrolleduser['institution']);
614                 $this->assertEquals($user1->description, $enrolleduser['description']);
615                 $this->assertEquals(FORMAT_HTML, $enrolleduser['descriptionformat']);
616                 $this->assertEquals($user1->city, $enrolleduser['city']);
617                 $this->assertEquals($user1->country, $enrolleduser['country']);
618                 $this->assertEquals($user1->url, $enrolleduser['url']);
619                 if (!empty($CFG->usetags)) {
620                     $this->assertEquals(implode(', ', $user1->interests), $enrolleduser['interests']);
621                 }
622             }
623         }
624     }
626     /**
627      * Test update_users
628      */
629     public function test_update_users() {
630         global $USER, $CFG, $DB;
632         $this->resetAfterTest(true);
634         $user1 = self::getDataGenerator()->create_user();
636         $user1 = array(
637             'id' => $user1->id,
638             'username' => 'usernametest1',
639             'password' => 'Moodle2012!',
640             'idnumber' => 'idnumbertest1',
641             'firstname' => 'First Name User Test 1',
642             'lastname' => 'Last Name User Test 1',
643             'email' => 'usertest1@email.com',
644             'description' => 'This is a description for user 1',
645             'city' => 'Perth',
646             'country' => 'au'
647             );
649         $context = context_system::instance();
650         $roleid = $this->assignUserCapability('moodle/user:update', $context->id);
652         // Call the external function.
653         core_user_external::update_users(array($user1));
655         $dbuser = $DB->get_record('user', array('id' => $user1['id']));
656         $this->assertEquals($dbuser->username, $user1['username']);
657         $this->assertEquals($dbuser->idnumber, $user1['idnumber']);
658         $this->assertEquals($dbuser->firstname, $user1['firstname']);
659         $this->assertEquals($dbuser->lastname, $user1['lastname']);
660         $this->assertEquals($dbuser->email, $user1['email']);
661         $this->assertEquals($dbuser->description, $user1['description']);
662         $this->assertEquals($dbuser->city, $user1['city']);
663         $this->assertEquals($dbuser->country, $user1['country']);
665         // Call without required capability.
666         $this->unassignUserCapability('moodle/user:update', $context->id, $roleid);
667         $this->setExpectedException('required_capability_exception');
668         core_user_external::update_users(array($user1));
669     }