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@email.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 require_once($CFG->dirroot
. '/tag/lib.php');
74 $user1->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
75 useredit_update_interests($user1, $user1->interests
);
77 $user2 = self
::getDataGenerator()->create_user(
78 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
80 $generatedusers = array();
81 $generatedusers[$user1->id
] = $user1;
82 $generatedusers[$user2->id
] = $user2;
84 $context = context_course
::instance($course->id
);
85 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id
);
87 // Enrol the users in the course.
88 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
, $roleid);
89 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
, $roleid);
90 $this->getDataGenerator()->enrol_user($USER->id
, $course->id
, $roleid);
92 // call as admin and receive all possible fields.
93 $this->setAdminUser();
95 $searchparams = array(
96 array('key' => 'invalidkey', 'value' => 'invalidkey'),
97 array('key' => 'email', 'value' => $user1->email
),
98 array('key' => 'firstname', 'value' => $user1->firstname
));
100 // Call the external function.
101 $result = core_user_external
::get_users($searchparams);
103 // We need to execute the return values cleaning process to simulate the web service server
104 $result = external_api
::clean_returnvalue(core_user_external
::get_users_returns(), $result);
106 // Check we retrieve the good total number of enrolled users + no error on capability.
107 $expectedreturnedusers = 1;
108 $returnedusers = $result['users'];
109 $this->assertEquals($expectedreturnedusers, count($returnedusers));
111 foreach($returnedusers as $returneduser) {
112 $generateduser = ($returneduser['id'] == $USER->id
) ?
113 $USER : $generatedusers[$returneduser['id']];
114 $this->assertEquals($generateduser->username
, $returneduser['username']);
115 if (!empty($generateduser->idnumber
)) {
116 $this->assertEquals($generateduser->idnumber
, $returneduser['idnumber']);
118 $this->assertEquals($generateduser->firstname
, $returneduser['firstname']);
119 $this->assertEquals($generateduser->lastname
, $returneduser['lastname']);
120 if ($generateduser->email
!= $USER->email
) { // Don't check the tmp modified $USER email.
121 $this->assertEquals($generateduser->email
, $returneduser['email']);
123 if (!empty($generateduser->address
)) {
124 $this->assertEquals($generateduser->address
, $returneduser['address']);
126 if (!empty($generateduser->phone1
)) {
127 $this->assertEquals($generateduser->phone1
, $returneduser['phone1']);
129 if (!empty($generateduser->phone2
)) {
130 $this->assertEquals($generateduser->phone2
, $returneduser['phone2']);
132 if (!empty($generateduser->icq
)) {
133 $this->assertEquals($generateduser->icq
, $returneduser['icq']);
135 if (!empty($generateduser->skype
)) {
136 $this->assertEquals($generateduser->skype
, $returneduser['skype']);
138 if (!empty($generateduser->yahoo
)) {
139 $this->assertEquals($generateduser->yahoo
, $returneduser['yahoo']);
141 if (!empty($generateduser->aim
)) {
142 $this->assertEquals($generateduser->aim
, $returneduser['aim']);
144 if (!empty($generateduser->msn
)) {
145 $this->assertEquals($generateduser->msn
, $returneduser['msn']);
147 if (!empty($generateduser->department
)) {
148 $this->assertEquals($generateduser->department
, $returneduser['department']);
150 if (!empty($generateduser->institution
)) {
151 $this->assertEquals($generateduser->institution
, $returneduser['institution']);
153 if (!empty($generateduser->description
)) {
154 $this->assertEquals($generateduser->description
, $returneduser['description']);
156 if (!empty($generateduser->descriptionformat
)) {
157 $this->assertEquals(FORMAT_HTML
, $returneduser['descriptionformat']);
159 if (!empty($generateduser->city
)) {
160 $this->assertEquals($generateduser->city
, $returneduser['city']);
162 if (!empty($generateduser->country
)) {
163 $this->assertEquals($generateduser->country
, $returneduser['country']);
165 if (!empty($generateduser->url
)) {
166 $this->assertEquals($generateduser->url
, $returneduser['url']);
168 if (!empty($CFG->usetags
) and !empty($generateduser->interests
)) {
169 $this->assertEquals(implode(', ', $generateduser->interests
), $returneduser['interests']);
173 // Test the invalid key warning.
174 $warnings = $result['warnings'];
175 $this->assertEquals(count($warnings), 1);
176 $warning = array_pop($warnings);
177 $this->assertEquals($warning['item'], 'invalidkey');
178 $this->assertEquals($warning['warningcode'], 'invalidfieldparameter');
180 // Test sending twice the same search field.
182 $searchparams = array(
183 array('key' => 'firstname', 'value' => 'Canard'),
184 array('key' => 'email', 'value' => $user1->email
),
185 array('key' => 'firstname', 'value' => $user1->firstname
));
187 // Call the external function.
188 $result = core_user_external
::get_users($searchparams);
189 $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
190 } catch (moodle_exception
$e) {
191 $this->assertEquals('keyalreadyset', $e->errorcode
);
192 } catch (Exception
$e) {
193 $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
198 * Test get_users_by_field
200 public function test_get_users_by_field() {
203 $this->resetAfterTest(true);
205 $course = self
::getDataGenerator()->create_course();
207 'username' => 'usernametest1',
208 'idnumber' => 'idnumbertest1',
209 'firstname' => 'First Name User Test 1',
210 'lastname' => 'Last Name User Test 1',
211 'email' => 'usertest1@email.com',
212 'address' => '2 Test Street Perth 6000 WA',
213 'phone1' => '01010101010',
214 'phone2' => '02020203',
215 'icq' => 'testuser1',
216 'skype' => 'testuser1',
217 'yahoo' => 'testuser1',
218 'aim' => 'testuser1',
219 'msn' => 'testuser1',
220 'department' => 'Department of user 1',
221 'institution' => 'Institution of user 1',
222 'description' => 'This is a description for user 1',
223 'descriptionformat' => FORMAT_MOODLE
,
225 'url' => 'http://moodle.org',
228 $user1 = self
::getDataGenerator()->create_user($user1);
229 if (!empty($CFG->usetags
)) {
230 require_once($CFG->dirroot
. '/user/editlib.php');
231 require_once($CFG->dirroot
. '/tag/lib.php');
232 $user1->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
233 useredit_update_interests($user1, $user1->interests
);
235 $user2 = self
::getDataGenerator()->create_user(
236 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
238 $generatedusers = array();
239 $generatedusers[$user1->id
] = $user1;
240 $generatedusers[$user2->id
] = $user2;
242 $context = context_course
::instance($course->id
);
243 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id
);
245 // Enrol the users in the course.
246 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
, $roleid, 'manual');
247 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
, $roleid, 'manual');
248 $this->getDataGenerator()->enrol_user($USER->id
, $course->id
, $roleid, 'manual');
250 // call as admin and receive all possible fields.
251 $this->setAdminUser();
253 $fieldstosearch = array('id', 'idnumber', 'username', 'email');
255 foreach ($fieldstosearch as $fieldtosearch) {
257 // Call the external function.
258 $returnedusers = core_user_external
::get_users_by_field($fieldtosearch,
259 array($USER->{$fieldtosearch}, $user1->{$fieldtosearch}, $user2->{$fieldtosearch}));
261 // Expected result differ following the searched field
262 // Admin user in the PHPunit framework doesn't have email or idnumber.
263 if ($fieldtosearch == 'email' or $fieldtosearch == 'idnumber') {
264 $expectedreturnedusers = 2;
266 $expectedreturnedusers = 3;
269 // Check we retrieve the good total number of enrolled users + no error on capability.
270 $this->assertEquals($expectedreturnedusers, count($returnedusers));
272 foreach($returnedusers as $returneduser) {
273 $generateduser = ($returneduser['id'] == $USER->id
) ?
274 $USER : $generatedusers[$returneduser['id']];
275 $this->assertEquals($generateduser->username
, $returneduser['username']);
276 if (!empty($generateduser->idnumber
)) {
277 $this->assertEquals($generateduser->idnumber
, $returneduser['idnumber']);
279 $this->assertEquals($generateduser->firstname
, $returneduser['firstname']);
280 $this->assertEquals($generateduser->lastname
, $returneduser['lastname']);
281 if ($generateduser->email
!= $USER->email
) { //don't check the tmp modified $USER email
282 $this->assertEquals($generateduser->email
, $returneduser['email']);
284 if (!empty($generateduser->address
)) {
285 $this->assertEquals($generateduser->address
, $returneduser['address']);
287 if (!empty($generateduser->phone1
)) {
288 $this->assertEquals($generateduser->phone1
, $returneduser['phone1']);
290 if (!empty($generateduser->phone2
)) {
291 $this->assertEquals($generateduser->phone2
, $returneduser['phone2']);
293 if (!empty($generateduser->icq
)) {
294 $this->assertEquals($generateduser->icq
, $returneduser['icq']);
296 if (!empty($generateduser->skype
)) {
297 $this->assertEquals($generateduser->skype
, $returneduser['skype']);
299 if (!empty($generateduser->yahoo
)) {
300 $this->assertEquals($generateduser->yahoo
, $returneduser['yahoo']);
302 if (!empty($generateduser->aim
)) {
303 $this->assertEquals($generateduser->aim
, $returneduser['aim']);
305 if (!empty($generateduser->msn
)) {
306 $this->assertEquals($generateduser->msn
, $returneduser['msn']);
308 if (!empty($generateduser->department
)) {
309 $this->assertEquals($generateduser->department
, $returneduser['department']);
311 if (!empty($generateduser->institution
)) {
312 $this->assertEquals($generateduser->institution
, $returneduser['institution']);
314 if (!empty($generateduser->description
)) {
315 $this->assertEquals($generateduser->description
, $returneduser['description']);
317 if (!empty($generateduser->descriptionformat
) and isset($returneduser['descriptionformat'])) {
318 $this->assertEquals($generateduser->descriptionformat
, $returneduser['descriptionformat']);
320 if (!empty($generateduser->city
)) {
321 $this->assertEquals($generateduser->city
, $returneduser['city']);
323 if (!empty($generateduser->country
)) {
324 $this->assertEquals($generateduser->country
, $returneduser['country']);
326 if (!empty($generateduser->url
)) {
327 $this->assertEquals($generateduser->url
, $returneduser['url']);
329 if (!empty($CFG->usetags
) and !empty($generateduser->interests
)) {
330 $this->assertEquals(implode(', ', $generateduser->interests
), $returneduser['interests']);
335 // Test that no result are returned for search by username if we are not admin
336 $this->setGuestUser();
338 // Call the external function.
339 $returnedusers = core_user_external
::get_users_by_field('username',
340 array($USER->username
, $user1->username
, $user2->username
));
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
));
352 // Only the own $USER username should be returned still.
353 $this->assertEquals(1, count($returnedusers));
357 * Test get_course_user_profiles
359 public function test_get_course_user_profiles() {
362 $this->resetAfterTest(true);
364 $course = self
::getDataGenerator()->create_course();
366 'username' => 'usernametest1',
367 'idnumber' => 'idnumbertest1',
368 'firstname' => 'First Name User Test 1',
369 'lastname' => 'Last Name User Test 1',
370 'email' => 'usertest1@email.com',
371 'address' => '2 Test Street Perth 6000 WA',
372 'phone1' => '01010101010',
373 'phone2' => '02020203',
374 'icq' => 'testuser1',
375 'skype' => 'testuser1',
376 'yahoo' => 'testuser1',
377 'aim' => 'testuser1',
378 'msn' => 'testuser1',
379 'department' => 'Department of user 1',
380 'institution' => 'Institution of user 1',
381 'description' => 'This is a description for user 1',
382 'descriptionformat' => FORMAT_MOODLE
,
384 'url' => 'http://moodle.org',
387 $user1 = self
::getDataGenerator()->create_user($user1);
388 if (!empty($CFG->usetags
)) {
389 require_once($CFG->dirroot
. '/user/editlib.php');
390 require_once($CFG->dirroot
. '/tag/lib.php');
391 $user1->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
392 useredit_update_interests($user1, $user1->interests
);
394 $user2 = self
::getDataGenerator()->create_user();
396 $context = context_course
::instance($course->id
);
397 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id
);
399 // Enrol the users in the course.
400 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
, $roleid, 'manual');
401 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
, $roleid, 'manual');
402 $this->getDataGenerator()->enrol_user($USER->id
, $course->id
, $roleid, 'manual');
404 // Call the external function.
405 $enrolledusers = core_user_external
::get_course_user_profiles(array(
406 array('userid' => $USER->id
, 'courseid' => $course->id
),
407 array('userid' => $user1->id
, 'courseid' => $course->id
),
408 array('userid' => $user2->id
, 'courseid' => $course->id
)));
410 // We need to execute the return values cleaning process to simulate the web service server.
411 $enrolledusers = external_api
::clean_returnvalue(core_user_external
::get_course_user_profiles_returns(), $enrolledusers);
413 // Check we retrieve the good total number of enrolled users + no error on capability.
414 $this->assertEquals(3, count($enrolledusers));
416 // Do the same call as admin to receive all possible fields.
417 $this->setAdminUser();
418 $USER->email
= "admin@fakeemail.com";
420 // Call the external function.
421 $enrolledusers = core_user_external
::get_course_user_profiles(array(
422 array('userid' => $USER->id
, 'courseid' => $course->id
),
423 array('userid' => $user1->id
, 'courseid' => $course->id
),
424 array('userid' => $user2->id
, 'courseid' => $course->id
)));
426 // We need to execute the return values cleaning process to simulate the web service server.
427 $enrolledusers = external_api
::clean_returnvalue(core_user_external
::get_course_user_profiles_returns(), $enrolledusers);
429 foreach($enrolledusers as $enrolleduser) {
430 if ($enrolleduser['username'] == $user1->username
) {
431 $this->assertEquals($user1->idnumber
, $enrolleduser['idnumber']);
432 $this->assertEquals($user1->firstname
, $enrolleduser['firstname']);
433 $this->assertEquals($user1->lastname
, $enrolleduser['lastname']);
434 $this->assertEquals($user1->email
, $enrolleduser['email']);
435 $this->assertEquals($user1->address
, $enrolleduser['address']);
436 $this->assertEquals($user1->phone1
, $enrolleduser['phone1']);
437 $this->assertEquals($user1->phone2
, $enrolleduser['phone2']);
438 $this->assertEquals($user1->icq
, $enrolleduser['icq']);
439 $this->assertEquals($user1->skype
, $enrolleduser['skype']);
440 $this->assertEquals($user1->yahoo
, $enrolleduser['yahoo']);
441 $this->assertEquals($user1->aim
, $enrolleduser['aim']);
442 $this->assertEquals($user1->msn
, $enrolleduser['msn']);
443 $this->assertEquals($user1->department
, $enrolleduser['department']);
444 $this->assertEquals($user1->institution
, $enrolleduser['institution']);
445 $this->assertEquals($user1->description
, $enrolleduser['description']);
446 $this->assertEquals(FORMAT_HTML
, $enrolleduser['descriptionformat']);
447 $this->assertEquals($user1->city
, $enrolleduser['city']);
448 $this->assertEquals($user1->country
, $enrolleduser['country']);
449 $this->assertEquals($user1->url
, $enrolleduser['url']);
450 if (!empty($CFG->usetags
)) {
451 $this->assertEquals(implode(', ', $user1->interests
), $enrolleduser['interests']);
460 public function test_create_users() {
461 global $USER, $CFG, $DB;
463 $this->resetAfterTest(true);
466 'username' => 'usernametest1',
467 'password' => 'Moodle2012!',
468 'idnumber' => 'idnumbertest1',
469 'firstname' => 'First Name User Test 1',
470 'lastname' => 'Last Name User Test 1',
471 'middlename' => 'Middle Name User Test 1',
472 'lastnamephonetic' => '最後のお名前のテスト一号',
473 'firstnamephonetic' => 'お名前のテスト一号',
474 'alternatename' => 'Alternate Name User Test 1',
475 'email' => 'usertest1@email.com',
476 'description' => 'This is a description for user 1',
481 $context = context_system
::instance();
482 $roleid = $this->assignUserCapability('moodle/user:create', $context->id
);
484 // Call the external function.
485 $createdusers = core_user_external
::create_users(array($user1));
487 // We need to execute the return values cleaning process to simulate the web service server.
488 $createdusers = external_api
::clean_returnvalue(core_user_external
::create_users_returns(), $createdusers);
490 // Check we retrieve the good total number of created users + no error on capability.
491 $this->assertEquals(1, count($createdusers));
493 foreach($createdusers as $createduser) {
494 $dbuser = $DB->get_record('user', array('id' => $createduser['id']));
495 $this->assertEquals($dbuser->username
, $user1['username']);
496 $this->assertEquals($dbuser->idnumber
, $user1['idnumber']);
497 $this->assertEquals($dbuser->firstname
, $user1['firstname']);
498 $this->assertEquals($dbuser->lastname
, $user1['lastname']);
499 $this->assertEquals($dbuser->email
, $user1['email']);
500 $this->assertEquals($dbuser->description
, $user1['description']);
501 $this->assertEquals($dbuser->city
, $user1['city']);
502 $this->assertEquals($dbuser->country
, $user1['country']);
505 // Call without required capability
506 $this->unassignUserCapability('moodle/user:create', $context->id
, $roleid);
507 $this->setExpectedException('required_capability_exception');
508 $createdusers = core_user_external
::create_users(array($user1));
514 public function test_delete_users() {
515 global $USER, $CFG, $DB;
517 $this->resetAfterTest(true);
519 $user1 = self
::getDataGenerator()->create_user();
520 $user2 = self
::getDataGenerator()->create_user();
522 // Check the users were correctly created.
523 $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
524 array('userid1' => $user1->id
, 'userid2' => $user2->id
)));
526 $context = context_system
::instance();
527 $roleid = $this->assignUserCapability('moodle/user:delete', $context->id
);
529 // Call the external function.
530 core_user_external
::delete_users(array($user1->id
, $user2->id
));
532 // Check we retrieve no users + no error on capability.
533 $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
534 array('userid1' => $user1->id
, 'userid2' => $user2->id
)));
536 // Call without required capability.
537 $this->unassignUserCapability('moodle/user:delete', $context->id
, $roleid);
538 $this->setExpectedException('required_capability_exception');
539 core_user_external
::delete_users(array($user1->id
, $user2->id
));
543 * Test get_users_by_id
545 public function test_get_users_by_id() {
548 $this->resetAfterTest(true);
551 'username' => 'usernametest1',
552 'idnumber' => 'idnumbertest1',
553 'firstname' => 'First Name User Test 1',
554 'lastname' => 'Last Name User Test 1',
555 'email' => 'usertest1@email.com',
556 'address' => '2 Test Street Perth 6000 WA',
557 'phone1' => '01010101010',
558 'phone2' => '02020203',
559 'icq' => 'testuser1',
560 'skype' => 'testuser1',
561 'yahoo' => 'testuser1',
562 'aim' => 'testuser1',
563 'msn' => 'testuser1',
564 'department' => 'Department of user 1',
565 'institution' => 'Institution of user 1',
566 'description' => 'This is a description for user 1',
567 'descriptionformat' => FORMAT_MOODLE
,
569 'url' => 'http://moodle.org',
572 $user1 = self
::getDataGenerator()->create_user($user1);
573 if (!empty($CFG->usetags
)) {
574 require_once($CFG->dirroot
. '/user/editlib.php');
575 require_once($CFG->dirroot
. '/tag/lib.php');
576 $user1->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
577 useredit_update_interests($user1, $user1->interests
);
579 $user2 = self
::getDataGenerator()->create_user();
581 $context = context_system
::instance();
582 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id
);
584 // Call the external function.
585 $returnedusers = core_user_external
::get_users_by_id(array(
586 $USER->id
, $user1->id
, $user2->id
));
588 // We need to execute the return values cleaning process to simulate the web service server.
589 $returnedusers = external_api
::clean_returnvalue(core_user_external
::get_users_by_id_returns(), $returnedusers);
591 // Check we retrieve the good total number of enrolled users + no error on capability.
592 $this->assertEquals(3, count($returnedusers));
594 // Do the same call as admin to receive all possible fields.
595 $this->setAdminUser();
596 $USER->email
= "admin@fakeemail.com";
598 // Call the external function.
599 $returnedusers = core_user_external
::get_users_by_id(array(
600 $USER->id
, $user1->id
, $user2->id
));
602 // We need to execute the return values cleaning process to simulate the web service server.
603 $returnedusers = external_api
::clean_returnvalue(core_user_external
::get_users_by_id_returns(), $returnedusers);
605 foreach($returnedusers as $enrolleduser) {
606 if ($enrolleduser['username'] == $user1->username
) {
607 $this->assertEquals($user1->idnumber
, $enrolleduser['idnumber']);
608 $this->assertEquals($user1->firstname
, $enrolleduser['firstname']);
609 $this->assertEquals($user1->lastname
, $enrolleduser['lastname']);
610 $this->assertEquals($user1->email
, $enrolleduser['email']);
611 $this->assertEquals($user1->address
, $enrolleduser['address']);
612 $this->assertEquals($user1->phone1
, $enrolleduser['phone1']);
613 $this->assertEquals($user1->phone2
, $enrolleduser['phone2']);
614 $this->assertEquals($user1->icq
, $enrolleduser['icq']);
615 $this->assertEquals($user1->skype
, $enrolleduser['skype']);
616 $this->assertEquals($user1->yahoo
, $enrolleduser['yahoo']);
617 $this->assertEquals($user1->aim
, $enrolleduser['aim']);
618 $this->assertEquals($user1->msn
, $enrolleduser['msn']);
619 $this->assertEquals($user1->department
, $enrolleduser['department']);
620 $this->assertEquals($user1->institution
, $enrolleduser['institution']);
621 $this->assertEquals($user1->description
, $enrolleduser['description']);
622 $this->assertEquals(FORMAT_HTML
, $enrolleduser['descriptionformat']);
623 $this->assertEquals($user1->city
, $enrolleduser['city']);
624 $this->assertEquals($user1->country
, $enrolleduser['country']);
625 $this->assertEquals($user1->url
, $enrolleduser['url']);
626 if (!empty($CFG->usetags
)) {
627 $this->assertEquals(implode(', ', $user1->interests
), $enrolleduser['interests']);
636 public function test_update_users() {
637 global $USER, $CFG, $DB;
639 $this->resetAfterTest(true);
641 $user1 = self
::getDataGenerator()->create_user();
645 'username' => 'usernametest1',
646 'password' => 'Moodle2012!',
647 'idnumber' => 'idnumbertest1',
648 'firstname' => 'First Name User Test 1',
649 'lastname' => 'Last Name User Test 1',
650 'middlename' => 'Middle Name User Test 1',
651 'lastnamephonetic' => '最後のお名前のテスト一号',
652 'firstnamephonetic' => 'お名前のテスト一号',
653 'alternatename' => 'Alternate Name User Test 1',
654 'email' => 'usertest1@email.com',
655 'description' => 'This is a description for user 1',
660 $context = context_system
::instance();
661 $roleid = $this->assignUserCapability('moodle/user:update', $context->id
);
663 // Call the external function.
664 core_user_external
::update_users(array($user1));
666 $dbuser = $DB->get_record('user', array('id' => $user1['id']));
667 $this->assertEquals($dbuser->username
, $user1['username']);
668 $this->assertEquals($dbuser->idnumber
, $user1['idnumber']);
669 $this->assertEquals($dbuser->firstname
, $user1['firstname']);
670 $this->assertEquals($dbuser->lastname
, $user1['lastname']);
671 $this->assertEquals($dbuser->email
, $user1['email']);
672 $this->assertEquals($dbuser->description
, $user1['description']);
673 $this->assertEquals($dbuser->city
, $user1['city']);
674 $this->assertEquals($dbuser->country
, $user1['country']);
676 // Call without required capability.
677 $this->unassignUserCapability('moodle/user:update', $context->id
, $roleid);
678 $this->setExpectedException('required_capability_exception');
679 core_user_external
::update_users(array($user1));
683 * Test add_user_private_files
685 public function test_add_user_private_files() {
686 global $USER, $CFG, $DB;
688 $this->resetAfterTest(true);
690 $context = context_system
::instance();
691 $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id
);
693 $context = context_user
::instance($USER->id
);
694 $contextid = $context->id
;
699 $filename = "Simple.txt";
700 $filecontent = base64_encode("Let us create a nice simple file");
701 $contextlevel = null;
703 $browser = get_file_browser();
705 // Call the files api to create a file.
706 $draftfile = core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath,
707 $filename, $filecontent, $contextlevel, $instanceid);
709 $draftid = $draftfile['itemid'];
710 // Make sure the file was created.
711 $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
712 $this->assertNotEmpty($file);
714 // Make sure the file does not exist in the user private files.
715 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
716 $this->assertEmpty($file);
718 // Call the external function.
719 core_user_external
::add_user_private_files($draftid);
721 // Make sure the file was added to the user private files.
722 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
723 $this->assertNotEmpty($file);
727 * Test add user device
729 public function test_add_user_device() {
730 global $USER, $CFG, $DB;
732 $this->resetAfterTest(true);
735 'appid' => 'com.moodle.moodlemobile',
737 'model' => 'Nexus 4',
738 'platform' => 'Android',
739 'version' => '4.2.2',
740 'pushid' => 'apushdkasdfj4835',
741 'uuid' => 'asdnfl348qlksfaasef859'
744 // Call the external function.
745 core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
746 $device['version'], $device['pushid'], $device['uuid']);
748 $created = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
749 $created = (array) $created;
751 $this->assertEquals($device, array_intersect_key((array)$created, $device));