2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * User external PHPunit tests
22 * @copyright 2012 Jerome Mouneyrac
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
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
{
40 public function test_get_users() {
43 $this->resetAfterTest(true);
45 $course = self
::getDataGenerator()->create_course();
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',
57 'skype' => 'testuser1',
58 'yahoo' => '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
,
66 'url' => 'http://moodle.org',
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']);
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']);
122 if (!empty($generateduser->address
)) {
123 $this->assertEquals($generateduser->address
, $returneduser['address']);
125 if (!empty($generateduser->phone1
)) {
126 $this->assertEquals($generateduser->phone1
, $returneduser['phone1']);
128 if (!empty($generateduser->phone2
)) {
129 $this->assertEquals($generateduser->phone2
, $returneduser['phone2']);
131 if (!empty($generateduser->icq
)) {
132 $this->assertEquals($generateduser->icq
, $returneduser['icq']);
134 if (!empty($generateduser->skype
)) {
135 $this->assertEquals($generateduser->skype
, $returneduser['skype']);
137 if (!empty($generateduser->yahoo
)) {
138 $this->assertEquals($generateduser->yahoo
, $returneduser['yahoo']);
140 if (!empty($generateduser->aim
)) {
141 $this->assertEquals($generateduser->aim
, $returneduser['aim']);
143 if (!empty($generateduser->msn
)) {
144 $this->assertEquals($generateduser->msn
, $returneduser['msn']);
146 if (!empty($generateduser->department
)) {
147 $this->assertEquals($generateduser->department
, $returneduser['department']);
149 if (!empty($generateduser->institution
)) {
150 $this->assertEquals($generateduser->institution
, $returneduser['institution']);
152 if (!empty($generateduser->description
)) {
153 $this->assertEquals($generateduser->description
, $returneduser['description']);
155 if (!empty($generateduser->descriptionformat
)) {
156 $this->assertEquals(FORMAT_HTML
, $returneduser['descriptionformat']);
158 if (!empty($generateduser->city
)) {
159 $this->assertEquals($generateduser->city
, $returneduser['city']);
161 if (!empty($generateduser->country
)) {
162 $this->assertEquals($generateduser->country
, $returneduser['country']);
164 if (!empty($generateduser->url
)) {
165 $this->assertEquals($generateduser->url
, $returneduser['url']);
167 if (!empty($CFG->usetags
) and !empty($generateduser->interests
)) {
168 $this->assertEquals(implode(', ', $generateduser->interests
), $returneduser['interests']);
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.
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.');
197 * Test get_users_by_field
199 public function test_get_users_by_field() {
202 $this->resetAfterTest(true);
204 $course = self
::getDataGenerator()->create_course();
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
,
224 'url' => 'http://moodle.org',
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
);
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;
265 $expectedreturnedusers = 3;
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']);
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']);
283 if (!empty($generateduser->address
)) {
284 $this->assertEquals($generateduser->address
, $returneduser['address']);
286 if (!empty($generateduser->phone1
)) {
287 $this->assertEquals($generateduser->phone1
, $returneduser['phone1']);
289 if (!empty($generateduser->phone2
)) {
290 $this->assertEquals($generateduser->phone2
, $returneduser['phone2']);
292 if (!empty($generateduser->icq
)) {
293 $this->assertEquals($generateduser->icq
, $returneduser['icq']);
295 if (!empty($generateduser->skype
)) {
296 $this->assertEquals($generateduser->skype
, $returneduser['skype']);
298 if (!empty($generateduser->yahoo
)) {
299 $this->assertEquals($generateduser->yahoo
, $returneduser['yahoo']);
301 if (!empty($generateduser->aim
)) {
302 $this->assertEquals($generateduser->aim
, $returneduser['aim']);
304 if (!empty($generateduser->msn
)) {
305 $this->assertEquals($generateduser->msn
, $returneduser['msn']);
307 if (!empty($generateduser->department
)) {
308 $this->assertEquals($generateduser->department
, $returneduser['department']);
310 if (!empty($generateduser->institution
)) {
311 $this->assertEquals($generateduser->institution
, $returneduser['institution']);
313 if (!empty($generateduser->description
)) {
314 $this->assertEquals($generateduser->description
, $returneduser['description']);
316 if (!empty($generateduser->descriptionformat
) and isset($returneduser['descriptionformat'])) {
317 $this->assertEquals($generateduser->descriptionformat
, $returneduser['descriptionformat']);
319 if (!empty($generateduser->city
)) {
320 $this->assertEquals($generateduser->city
, $returneduser['city']);
322 if (!empty($generateduser->country
)) {
323 $this->assertEquals($generateduser->country
, $returneduser['country']);
325 if (!empty($generateduser->url
)) {
326 $this->assertEquals($generateduser->url
, $returneduser['url']);
328 if (!empty($CFG->usetags
) and !empty($generateduser->interests
)) {
329 $this->assertEquals(implode(', ', $generateduser->interests
), $returneduser['interests']);
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));
357 public function get_course_user_profiles_setup($capability) {
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
,
385 'url' => 'http://moodle.org',
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
);
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');
408 * Test get_course_user_profiles
410 public function test_get_course_user_profiles() {
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));
428 public function test_get_user_course_profile_as_admin() {
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']);
479 public function test_create_users() {
480 global $USER, $CFG, $DB;
482 $this->resetAfterTest(true);
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',
500 $context = context_system
::instance();
501 $roleid = $this->assignUserCapability('moodle/user:create', $context->id
);
503 // Call the external function.
504 $createdusers = core_user_external
::create_users(array($user1));
506 // We need to execute the return values cleaning process to simulate the web service server.
507 $createdusers = external_api
::clean_returnvalue(core_user_external
::create_users_returns(), $createdusers);
509 // Check we retrieve the good total number of created users + no error on capability.
510 $this->assertEquals(1, count($createdusers));
512 foreach($createdusers as $createduser) {
513 $dbuser = $DB->get_record('user', array('id' => $createduser['id']));
514 $this->assertEquals($dbuser->username
, $user1['username']);
515 $this->assertEquals($dbuser->idnumber
, $user1['idnumber']);
516 $this->assertEquals($dbuser->firstname
, $user1['firstname']);
517 $this->assertEquals($dbuser->lastname
, $user1['lastname']);
518 $this->assertEquals($dbuser->email
, $user1['email']);
519 $this->assertEquals($dbuser->description
, $user1['description']);
520 $this->assertEquals($dbuser->city
, $user1['city']);
521 $this->assertEquals($dbuser->country
, $user1['country']);
524 // Call without required capability
525 $this->unassignUserCapability('moodle/user:create', $context->id
, $roleid);
526 $this->expectException('required_capability_exception');
527 $createdusers = core_user_external
::create_users(array($user1));
533 public function test_delete_users() {
534 global $USER, $CFG, $DB;
536 $this->resetAfterTest(true);
538 $user1 = self
::getDataGenerator()->create_user();
539 $user2 = self
::getDataGenerator()->create_user();
541 // Check the users were correctly created.
542 $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
543 array('userid1' => $user1->id
, 'userid2' => $user2->id
)));
545 $context = context_system
::instance();
546 $roleid = $this->assignUserCapability('moodle/user:delete', $context->id
);
548 // Call the external function.
549 core_user_external
::delete_users(array($user1->id
, $user2->id
));
551 // Check we retrieve no users + no error on capability.
552 $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
553 array('userid1' => $user1->id
, 'userid2' => $user2->id
)));
555 // Call without required capability.
556 $this->unassignUserCapability('moodle/user:delete', $context->id
, $roleid);
557 $this->expectException('required_capability_exception');
558 core_user_external
::delete_users(array($user1->id
, $user2->id
));
564 public function test_update_users() {
565 global $USER, $CFG, $DB;
567 $this->resetAfterTest(true);
569 $wsuser = self
::getDataGenerator()->create_user();
570 self
::setUser($wsuser);
572 $context = context_user
::instance($USER->id
);
573 $contextid = $context->id
;
574 $filename = "reddot.png";
575 $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38"
576 . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
578 // Call the files api to create a file.
579 $draftfile = core_files_external
::upload($contextid, 'user', 'draft', 0, '/',
580 $filename, $filecontent, null, null);
581 $draftfile = external_api
::clean_returnvalue(core_files_external
::upload_returns(), $draftfile);
583 $draftid = $draftfile['itemid'];
585 $user1 = self
::getDataGenerator()->create_user();
589 'username' => 'usernametest1',
590 'password' => 'Moodle2012!',
591 'idnumber' => 'idnumbertest1',
592 'firstname' => 'First Name User Test 1',
593 'lastname' => 'Last Name User Test 1',
594 'middlename' => 'Middle Name User Test 1',
595 'lastnamephonetic' => '最後のお名前のテスト一号',
596 'firstnamephonetic' => 'お名前のテスト一号',
597 'alternatename' => 'Alternate Name User Test 1',
598 'email' => 'usertest1@example.com',
599 'description' => 'This is a description for user 1',
601 'userpicture' => $draftid,
605 $context = context_system
::instance();
606 $roleid = $this->assignUserCapability('moodle/user:update', $context->id
);
608 // Call the external function.
609 core_user_external
::update_users(array($user1));
611 $dbuser = $DB->get_record('user', array('id' => $user1['id']));
612 $this->assertEquals($dbuser->username
, $user1['username']);
613 $this->assertEquals($dbuser->idnumber
, $user1['idnumber']);
614 $this->assertEquals($dbuser->firstname
, $user1['firstname']);
615 $this->assertEquals($dbuser->lastname
, $user1['lastname']);
616 $this->assertEquals($dbuser->email
, $user1['email']);
617 $this->assertEquals($dbuser->description
, $user1['description']);
618 $this->assertEquals($dbuser->city
, $user1['city']);
619 $this->assertEquals($dbuser->country
, $user1['country']);
620 $this->assertNotEquals(0, $dbuser->picture
, 'Picture must be set to the new icon itemid for this user');
622 // Confirm no picture change when parameter is not supplied.
623 unset($user1['userpicture']);
624 core_user_external
::update_users(array($user1));
625 $dbusernopic = $DB->get_record('user', array('id' => $user1['id']));
626 $this->assertEquals($dbuser->picture
, $dbusernopic->picture
, 'Picture not change without the parameter.');
628 // Confirm delete of picture deletes the picture from the user record.
629 $user1['userpicture'] = 0;
630 core_user_external
::update_users(array($user1));
631 $dbuserdelpic = $DB->get_record('user', array('id' => $user1['id']));
632 $this->assertEquals(0, $dbuserdelpic->picture
, 'Picture must be deleted when sent as 0.');
635 // Call without required capability.
636 $this->unassignUserCapability('moodle/user:update', $context->id
, $roleid);
637 $this->expectException('required_capability_exception');
638 core_user_external
::update_users(array($user1));
642 * Test add_user_private_files
644 public function test_add_user_private_files() {
645 global $USER, $CFG, $DB;
647 $this->resetAfterTest(true);
649 $context = context_system
::instance();
650 $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id
);
652 $context = context_user
::instance($USER->id
);
653 $contextid = $context->id
;
658 $filename = "Simple.txt";
659 $filecontent = base64_encode("Let us create a nice simple file");
660 $contextlevel = null;
662 $browser = get_file_browser();
664 // Call the files api to create a file.
665 $draftfile = core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath,
666 $filename, $filecontent, $contextlevel, $instanceid);
667 $draftfile = external_api
::clean_returnvalue(core_files_external
::upload_returns(), $draftfile);
669 $draftid = $draftfile['itemid'];
670 // Make sure the file was created.
671 $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
672 $this->assertNotEmpty($file);
674 // Make sure the file does not exist in the user private files.
675 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
676 $this->assertEmpty($file);
678 // Call the external function.
679 core_user_external
::add_user_private_files($draftid);
681 // Make sure the file was added to the user private files.
682 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
683 $this->assertNotEmpty($file);
687 * Test add user device
689 public function test_add_user_device() {
690 global $USER, $CFG, $DB;
692 $this->resetAfterTest(true);
695 'appid' => 'com.moodle.moodlemobile',
697 'model' => 'Nexus 4',
698 'platform' => 'Android',
699 'version' => '4.2.2',
700 'pushid' => 'apushdkasdfj4835',
701 'uuid' => 'asdnfl348qlksfaasef859'
704 // Call the external function.
705 core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
706 $device['version'], $device['pushid'], $device['uuid']);
708 $created = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
709 $created = (array) $created;
711 $this->assertEquals($device, array_intersect_key((array)$created, $device));
713 // Test reuse the same pushid value.
714 $warnings = core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
715 $device['version'], $device['pushid'], $device['uuid']);
716 // We need to execute the return values cleaning process to simulate the web service server.
717 $warnings = external_api
::clean_returnvalue(core_user_external
::add_user_device_returns(), $warnings);
718 $this->assertCount(1, $warnings);
720 // Test update an existing device.
721 $device['pushid'] = 'different than before';
722 $warnings = core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
723 $device['version'], $device['pushid'], $device['uuid']);
724 $warnings = external_api
::clean_returnvalue(core_user_external
::add_user_device_returns(), $warnings);
726 $this->assertEquals(1, $DB->count_records('user_devices'));
727 $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
728 $this->assertEquals($device, array_intersect_key((array)$updated, $device));
730 // Test creating a new device just changing the uuid.
731 $device['uuid'] = 'newuidforthesameuser';
732 $device['pushid'] = 'new different than before';
733 $warnings = core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
734 $device['version'], $device['pushid'], $device['uuid']);
735 $warnings = external_api
::clean_returnvalue(core_user_external
::add_user_device_returns(), $warnings);
736 $this->assertEquals(2, $DB->count_records('user_devices'));
740 * Test remove user device
742 public function test_remove_user_device() {
743 global $USER, $CFG, $DB;
745 $this->resetAfterTest(true);
748 'appid' => 'com.moodle.moodlemobile',
750 'model' => 'Nexus 4',
751 'platform' => 'Android',
752 'version' => '4.2.2',
753 'pushid' => 'apushdkasdfj4835',
754 'uuid' => 'ABCDE3723ksdfhasfaasef859'
757 // A device with the same properties except the appid and pushid.
759 $device2['pushid'] = "0987654321";
760 $device2['appid'] = "other.app.com";
762 $this->setAdminUser();
763 // Create a user device using the external API function.
764 core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
765 $device['version'], $device['pushid'], $device['uuid']);
767 // Create the same device but for a different app.
768 core_user_external
::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'],
769 $device2['version'], $device2['pushid'], $device2['uuid']);
771 // Try to remove a device that does not exist.
772 $result = core_user_external
::remove_user_device('1234567890');
773 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
774 $this->assertFalse($result['removed']);
775 $this->assertCount(1, $result['warnings']);
777 // Try to remove a device that does not exist for an existing app.
778 $result = core_user_external
::remove_user_device('1234567890', $device['appid']);
779 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
780 $this->assertFalse($result['removed']);
781 $this->assertCount(1, $result['warnings']);
783 // Remove an existing device for an existing app. This will remove one of the two devices.
784 $result = core_user_external
::remove_user_device($device['uuid'], $device['appid']);
785 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
786 $this->assertTrue($result['removed']);
788 // Remove all the devices. This must remove the remaining device.
789 $result = core_user_external
::remove_user_device($device['uuid']);
790 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
791 $this->assertTrue($result['removed']);