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
29 use core_external\external_api
;
30 use core_files_external
;
31 use core_user_external
;
32 use externallib_advanced_testcase
;
34 defined('MOODLE_INTERNAL') ||
die();
38 require_once($CFG->dirroot
. '/webservice/tests/helpers.php');
39 require_once($CFG->dirroot
. '/user/externallib.php');
40 require_once($CFG->dirroot
. '/files/externallib.php');
43 * Tests for the user external functions.
46 * @covers \core_user_external
48 final class externallib_test
extends externallib_advanced_testcase
{
53 public function test_get_users(): void
{
56 $this->resetAfterTest(true);
58 $course = self
::getDataGenerator()->create_course();
61 'username' => 'usernametest1',
62 'idnumber' => 'idnumbertest1',
63 'firstname' => 'First Name User Test 1',
64 'lastname' => 'Last Name User Test 1',
65 'email' => 'usertest1@example.com',
66 'address' => '2 Test Street Perth 6000 WA',
67 'phone1' => '01010101010',
68 'phone2' => '02020203',
69 'department' => 'Department of user 1',
70 'institution' => 'Institution of user 1',
71 'description' => 'This is a description for user 1',
72 'descriptionformat' => FORMAT_MOODLE
,
77 $user1 = self
::getDataGenerator()->create_user($user1);
78 set_config('usetags', 1);
79 require_once($CFG->dirroot
. '/user/editlib.php');
80 $user1->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
81 useredit_update_interests($user1, $user1->interests
);
83 $user2 = self
::getDataGenerator()->create_user(
84 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
86 $generatedusers = array();
87 $generatedusers[$user1->id
] = $user1;
88 $generatedusers[$user2->id
] = $user2;
90 $context = \context_course
::instance($course->id
);
91 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id
);
93 // Enrol the users in the course.
94 $this->getDataGenerator()->enrol_user($user1->id
, $course->id
, $roleid);
95 $this->getDataGenerator()->enrol_user($user2->id
, $course->id
, $roleid);
96 $this->getDataGenerator()->enrol_user($USER->id
, $course->id
, $roleid);
98 // call as admin and receive all possible fields.
99 $this->setAdminUser();
101 $searchparams = array(
102 array('key' => 'invalidkey', 'value' => 'invalidkey'),
103 array('key' => 'email', 'value' => $user1->email
),
104 array('key' => 'firstname', 'value' => $user1->firstname
));
106 // Call the external function.
107 $result = core_user_external
::get_users($searchparams);
109 // We need to execute the return values cleaning process to simulate the web service server
110 $result = external_api
::clean_returnvalue(core_user_external
::get_users_returns(), $result);
112 // Check we retrieve the good total number of enrolled users + no error on capability.
113 $expectedreturnedusers = 1;
114 $returnedusers = $result['users'];
115 $this->assertEquals($expectedreturnedusers, count($returnedusers));
117 foreach($returnedusers as $returneduser) {
118 $generateduser = ($returneduser['id'] == $USER->id
) ?
119 $USER : $generatedusers[$returneduser['id']];
120 $this->assertEquals($generateduser->username
, $returneduser['username']);
121 if (!empty($generateduser->idnumber
)) {
122 $this->assertEquals($generateduser->idnumber
, $returneduser['idnumber']);
124 $this->assertEquals($generateduser->firstname
, $returneduser['firstname']);
125 $this->assertEquals($generateduser->lastname
, $returneduser['lastname']);
126 if ($generateduser->email
!= $USER->email
) { // Don't check the tmp modified $USER email.
127 $this->assertEquals($generateduser->email
, $returneduser['email']);
129 if (!empty($generateduser->address
)) {
130 $this->assertEquals($generateduser->address
, $returneduser['address']);
132 if (!empty($generateduser->phone1
)) {
133 $this->assertEquals($generateduser->phone1
, $returneduser['phone1']);
135 if (!empty($generateduser->phone2
)) {
136 $this->assertEquals($generateduser->phone2
, $returneduser['phone2']);
138 if (!empty($generateduser->department
)) {
139 $this->assertEquals($generateduser->department
, $returneduser['department']);
141 if (!empty($generateduser->institution
)) {
142 $this->assertEquals($generateduser->institution
, $returneduser['institution']);
144 if (!empty($generateduser->description
)) {
145 $this->assertEquals($generateduser->description
, $returneduser['description']);
147 if (!empty($generateduser->descriptionformat
)) {
148 $this->assertEquals(FORMAT_HTML
, $returneduser['descriptionformat']);
150 if (!empty($generateduser->city
)) {
151 $this->assertEquals($generateduser->city
, $returneduser['city']);
153 if (!empty($generateduser->country
)) {
154 $this->assertEquals($generateduser->country
, $returneduser['country']);
156 if (!empty($CFG->usetags
) and !empty($generateduser->interests
)) {
157 $this->assertEquals(implode(', ', $generateduser->interests
), $returneduser['interests']);
161 // Test the invalid key warning.
162 $warnings = $result['warnings'];
163 $this->assertEquals(count($warnings), 1);
164 $warning = array_pop($warnings);
165 $this->assertEquals($warning['item'], 'invalidkey');
166 $this->assertEquals($warning['warningcode'], 'invalidfieldparameter');
168 // Test sending twice the same search field.
170 $searchparams = array(
171 array('key' => 'firstname', 'value' => 'Canard'),
172 array('key' => 'email', 'value' => $user1->email
),
173 array('key' => 'firstname', 'value' => $user1->firstname
));
175 // Call the external function.
176 $result = core_user_external
::get_users($searchparams);
177 $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
178 } catch (\moodle_exception
$e) {
179 $this->assertEquals('keyalreadyset', $e->errorcode
);
180 } catch (\Exception
$e) {
181 $this->fail('Expecting \'keyalreadyset\' moodle_exception to be thrown.');
186 * Test get_users_by_field
188 public function test_get_users_by_field(): void
{
191 $this->resetAfterTest(true);
193 $generator = self
::getDataGenerator();
195 // Create complex user profile field supporting multi-lang.
196 filter_set_global_state('multilang', TEXTFILTER_ON
);
197 $name = '<span lang="en" class="multilang">Employment status</span>'.
198 '<span lang="es" class="multilang">Estado de Empleo</span>';
199 $statuses = 'UE\nSE\n<span lang="en" class="multilang">Other</span><span lang="es" class="multilang">Otro</span>';
200 $generator->create_custom_profile_field(
202 'datatype' => 'menu',
203 'shortname' => 'employmentstatus',
205 'param1' => $statuses
209 $course = $generator->create_course();
211 'username' => 'usernametest1',
212 'idnumber' => 'idnumbertest1',
213 'firstname' => 'First Name User Test 1',
214 'lastname' => 'Last Name User Test 1',
215 'email' => 'usertest1@example.com',
216 'address' => '2 Test Street Perth 6000 WA',
217 'phone1' => '01010101010',
218 'phone2' => '02020203',
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
,
225 'profile_field_jobposition' => 'Manager',
226 'profile_field_employmentstatus' => explode('\n', $statuses)[2],
228 $user1 = $generator->create_user($user1);
229 if (!empty($CFG->usetags
)) {
230 require_once($CFG->dirroot
. '/user/editlib.php');
231 $user1->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
232 useredit_update_interests($user1, $user1->interests
);
234 $user2 = $generator->create_user(
235 array('username' => 'usernametest2', 'idnumber' => 'idnumbertest2'));
237 $generatedusers = array();
238 $generatedusers[$user1->id
] = $user1;
239 $generatedusers[$user2->id
] = $user2;
241 $context = \context_course
::instance($course->id
);
242 $roleid = $this->assignUserCapability('moodle/user:viewdetails', $context->id
);
244 // Enrol the users in the course.
245 $generator->enrol_user($user1->id
, $course->id
, $roleid, 'manual');
246 $generator->enrol_user($user2->id
, $course->id
, $roleid, 'manual');
247 $generator->enrol_user($USER->id
, $course->id
, $roleid, 'manual');
249 // call as admin and receive all possible fields.
250 $this->setAdminUser();
252 $fieldstosearch = array('id', 'idnumber', 'username', 'email');
254 foreach ($fieldstosearch as $fieldtosearch) {
256 // Call the external function.
257 $returnedusers = core_user_external
::get_users_by_field($fieldtosearch,
258 array($USER->{$fieldtosearch}, $user1->{$fieldtosearch}, $user2->{$fieldtosearch}));
259 $returnedusers = external_api
::clean_returnvalue(core_user_external
::get_users_by_field_returns(), $returnedusers);
261 // Expected result differ following the searched field
262 // Admin user in the PHPunit framework doesn't have an idnumber.
263 if ($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->department
)) {
294 $this->assertEquals($generateduser->department
, $returneduser['department']);
296 if (!empty($generateduser->institution
)) {
297 $this->assertEquals($generateduser->institution
, $returneduser['institution']);
299 if (!empty($generateduser->description
)) {
300 $this->assertEquals($generateduser->description
, $returneduser['description']);
302 if (!empty($generateduser->descriptionformat
) and isset($returneduser['descriptionformat'])) {
303 $this->assertEquals($generateduser->descriptionformat
, $returneduser['descriptionformat']);
305 if (!empty($generateduser->city
)) {
306 $this->assertEquals($generateduser->city
, $returneduser['city']);
308 if (!empty($generateduser->country
)) {
309 $this->assertEquals($generateduser->country
, $returneduser['country']);
311 if (!empty($CFG->usetags
) and !empty($generateduser->interests
)) {
312 $this->assertEquals(implode(', ', $generateduser->interests
), $returneduser['interests']);
314 // Default language and no theme were used for the user.
315 $this->assertEquals($CFG->lang
, $returneduser['lang']);
316 $this->assertEquals($generateduser->trackforums
, $returneduser['trackforums']);
317 $this->assertEmpty($returneduser['theme']);
319 if ($returneduser['id'] == $user1->id
) {
320 $this->assertCount(1, $returneduser['customfields']);
321 $dbvalue = explode('\n', $statuses)[2];
322 $this->assertEquals($dbvalue, $returneduser['customfields'][0]['value']);
323 $this->assertEquals('Employment status', $returneduser['customfields'][0]['name']);
324 $this->assertEquals('Other', $returneduser['customfields'][0]['displayvalue']);
329 // Test that no result are returned for search by username if we are not admin
330 $this->setGuestUser();
332 // Call the external function.
333 $returnedusers = core_user_external
::get_users_by_field('username',
334 array($USER->username
, $user1->username
, $user2->username
));
335 $returnedusers = external_api
::clean_returnvalue(core_user_external
::get_users_by_field_returns(), $returnedusers);
337 // Only the own $USER username should be returned
338 $this->assertEquals(1, count($returnedusers));
340 // And finally test as one of the enrolled users.
341 $this->setUser($user1);
343 // Call the external function.
344 $returnedusers = core_user_external
::get_users_by_field('username',
345 array($USER->username
, $user1->username
, $user2->username
));
346 $returnedusers = external_api
::clean_returnvalue(core_user_external
::get_users_by_field_returns(), $returnedusers);
348 // Only the own $USER username should be returned still.
349 $this->assertEquals(1, count($returnedusers));
352 public function get_course_user_profiles_setup($capability) {
355 $this->resetAfterTest(true);
357 $return = new \
stdClass();
359 $generator = self
::getDataGenerator();
361 // Create complex user profile field supporting multi-lang.
362 filter_set_global_state('multilang', TEXTFILTER_ON
);
363 $name = '<span lang="en" class="multilang">Employment status</span>' .
364 '<span lang="es" class="multilang">Estado de Empleo</span>';
365 $statuses = 'UE\nSE\n<span lang="en" class="multilang">Other</span><span lang="es" class="multilang">Otro</span>';
366 $generator->create_custom_profile_field(
368 'datatype' => 'menu',
369 'shortname' => 'employmentstatus',
371 'param1' => $statuses,
375 // Create the course and fetch its context.
376 $return->course
= self
::getDataGenerator()->create_course();
377 $return->user1
= array(
378 'username' => 'usernametest1',
379 'idnumber' => 'idnumbertest1',
380 'firstname' => 'First Name User Test 1',
381 'lastname' => 'Last Name User Test 1',
382 'email' => 'usertest1@example.com',
383 'address' => '2 Test Street Perth 6000 WA',
384 'phone1' => '01010101010',
385 'phone2' => '02020203',
386 'department' => 'Department of user 1',
387 'institution' => 'Institution of user 1',
388 'description' => 'This is a description for user 1',
389 'descriptionformat' => FORMAT_MOODLE
,
392 'profile_field_employmentstatus' => explode('\n', $statuses)[2],
394 $return->user1
= self
::getDataGenerator()->create_user($return->user1
);
395 if (!empty($CFG->usetags
)) {
396 require_once($CFG->dirroot
. '/user/editlib.php');
397 $return->user1
->interests
= array('Cinema', 'Tennis', 'Dance', 'Guitar', 'Cooking');
398 useredit_update_interests($return->user1
, $return->user1
->interests
);
400 $return->user2
= self
::getDataGenerator()->create_user();
402 $context = \context_course
::instance($return->course
->id
);
403 $return->roleid
= $this->assignUserCapability($capability, $context->id
);
405 // Enrol the users in the course.
406 $this->getDataGenerator()->enrol_user($return->user1
->id
, $return->course
->id
, $return->roleid
, 'manual');
407 $this->getDataGenerator()->enrol_user($return->user2
->id
, $return->course
->id
, $return->roleid
, 'manual');
408 $this->getDataGenerator()->enrol_user($USER->id
, $return->course
->id
, $return->roleid
, 'manual');
410 $group1 = $this->getDataGenerator()->create_group(['courseid' => $return->course
->id
, 'name' => 'G1']);
411 $group2 = $this->getDataGenerator()->create_group(['courseid' => $return->course
->id
, 'name' => 'G2']);
413 groups_add_member($group1->id
, $return->user1
->id
);
414 groups_add_member($group2->id
, $return->user2
->id
);
420 * Test get_course_user_profiles
422 public function test_get_course_user_profiles(): void
{
425 $this->resetAfterTest(true);
427 $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails');
429 // Call the external function.
430 $enrolledusers = core_user_external
::get_course_user_profiles(array(
431 array('userid' => $USER->id
, 'courseid' => $data->course
->id
)));
433 // We need to execute the return values cleaning process to simulate the web service server.
434 $enrolledusers = external_api
::clean_returnvalue(core_user_external
::get_course_user_profiles_returns(), $enrolledusers);
436 // Check we retrieve the good total number of enrolled users + no error on capability.
437 $this->assertEquals(1, count($enrolledusers));
440 public function test_get_user_course_profile_as_admin(): void
{
445 $this->resetAfterTest(true);
447 $data = $this->get_course_user_profiles_setup('moodle/user:viewdetails');
449 // Do the same call as admin to receive all possible fields.
450 $this->setAdminUser();
451 $USER->email
= "admin@example.com";
453 // Call the external function.
454 $enrolledusers = core_user_external
::get_course_user_profiles(array(
455 array('userid' => $data->user1
->id
, 'courseid' => $data->course
->id
)));
457 // We need to execute the return values cleaning process to simulate the web service server.
458 $enrolledusers = external_api
::clean_returnvalue(core_user_external
::get_course_user_profiles_returns(), $enrolledusers);
459 // Check we get the requested user and that is in a group.
460 $this->assertCount(1, $enrolledusers);
461 $this->assertCount(1, $enrolledusers[0]['groups']);
463 foreach($enrolledusers as $enrolleduser) {
464 if ($enrolleduser['username'] == $data->user1
->username
) {
465 $this->assertEquals($data->user1
->idnumber
, $enrolleduser['idnumber']);
466 $this->assertEquals($data->user1
->firstname
, $enrolleduser['firstname']);
467 $this->assertEquals($data->user1
->lastname
, $enrolleduser['lastname']);
468 $this->assertEquals($data->user1
->email
, $enrolleduser['email']);
469 $this->assertEquals($data->user1
->address
, $enrolleduser['address']);
470 $this->assertEquals($data->user1
->phone1
, $enrolleduser['phone1']);
471 $this->assertEquals($data->user1
->phone2
, $enrolleduser['phone2']);
472 $this->assertEquals($data->user1
->department
, $enrolleduser['department']);
473 $this->assertEquals($data->user1
->institution
, $enrolleduser['institution']);
474 $this->assertEquals($data->user1
->description
, $enrolleduser['description']);
475 $this->assertEquals(FORMAT_HTML
, $enrolleduser['descriptionformat']);
476 $this->assertEquals($data->user1
->city
, $enrolleduser['city']);
477 $this->assertEquals($data->user1
->country
, $enrolleduser['country']);
478 // Default language was used for the user.
479 $this->assertEquals($CFG->lang
, $enrolleduser['lang']);
480 $this->assertEquals('Employment status', $enrolleduser['customfields'][0]['name']);
481 $this->assertEquals('Other', $enrolleduser['customfields'][0]['displayvalue']);
483 if (!empty($CFG->usetags
)) {
484 $this->assertEquals(implode(', ', $data->user1
->interests
), $enrolleduser['interests']);
493 public function test_create_users(): void
{
496 $this->resetAfterTest(true);
499 'username' => 'usernametest1',
500 'password' => 'Moodle2012!',
501 'idnumber' => 'idnumbertest1',
502 'firstname' => 'First Name User Test 1',
503 'lastname' => 'Last Name User Test 1',
504 'middlename' => 'Middle Name User Test 1',
505 'lastnamephonetic' => '最後のお名前のテスト一号',
506 'firstnamephonetic' => 'お名前のテスト一号',
507 'alternatename' => 'Alternate Name User Test 1',
508 'email' => 'usertest1@example.com',
509 'description' => 'This is a description for user 1',
513 'type' => 'htmleditor',
516 'type' => 'invalidpreference',
520 'department' => 'College of Science',
521 'institution' => 'National Institute of Physics',
522 'phone1' => '01 2345 6789',
524 'interests' => 'badminton, basketball, cooking, '
527 // User with an authentication method done externally.
529 'username' => 'usernametest2',
530 'firstname' => 'First Name User Test 2',
531 'lastname' => 'Last Name User Test 2',
532 'email' => 'usertest2@example.com',
536 $context = \context_system
::instance();
537 $roleid = $this->assignUserCapability('moodle/user:create', $context->id
);
538 $this->assignUserCapability('moodle/user:editprofile', $context->id
, $roleid);
540 // Call the external function.
541 $createdusers = core_user_external
::create_users(array($user1, $user2));
543 // We need to execute the return values cleaning process to simulate the web service server.
544 $createdusers = external_api
::clean_returnvalue(core_user_external
::create_users_returns(), $createdusers);
546 // Check we retrieve the good total number of created users + no error on capability.
547 $this->assertCount(2, $createdusers);
549 foreach($createdusers as $createduser) {
550 $dbuser = $DB->get_record('user', array('id' => $createduser['id']));
552 if ($createduser['username'] === $user1['username']) {
553 $usertotest = $user1;
554 $this->assertEquals('atto', get_user_preferences('htmleditor', null, $dbuser));
555 $this->assertEquals(null, get_user_preferences('invalidpreference', null, $dbuser));
556 // Confirm user interests have been saved.
557 $interests = \core_tag_tag
::get_item_tags_array('core', 'user', $createduser['id'],
558 \core_tag_tag
::BOTH_STANDARD_AND_NOT
, 0, false);
559 // There should be 3 user interests.
560 $this->assertCount(3, $interests);
562 } else if ($createduser['username'] === $user2['username']) {
563 $usertotest = $user2;
566 foreach ($dbuser as $property => $value) {
567 if ($property === 'password') {
568 if ($usertotest === $user2) {
569 // External auth mechanisms don't store password in the user table.
570 $this->assertEquals(AUTH_PASSWORD_NOT_CACHED
, $value);
572 // Skip hashed passwords.
576 // Confirm that the values match.
577 if (isset($usertotest[$property])) {
578 $this->assertEquals($usertotest[$property], $value);
583 // Call without required capability
584 $this->unassignUserCapability('moodle/user:create', $context->id
, $roleid);
585 $this->expectException('required_capability_exception');
586 core_user_external
::create_users(array($user1));
590 * Test create_users with password and createpassword parameter not set.
592 public function test_create_users_empty_password(): void
{
593 $this->resetAfterTest();
594 $this->setAdminUser();
597 'username' => 'usernametest1',
598 'firstname' => 'First Name User Test 1',
599 'lastname' => 'Last Name User Test 1',
600 'email' => 'usertest1@example.com',
603 // This should throw an exception because either password or createpassword param must be passed for auth_manual.
604 $this->expectException(\invalid_parameter_exception
::class);
605 core_user_external
::create_users([$user]);
609 * Data provider for \core_user_externallib_testcase::test_create_users_with_same_emails().
611 public function create_users_provider_with_same_emails() {
613 'Same emails allowed, same case' => [
616 'Same emails allowed, different case' => [
619 'Same emails disallowed, same case' => [
622 'Same emails disallowed, different case' => [
629 * Test for \core_user_external::create_users() when user using the same email addresses are being created.
631 * @dataProvider create_users_provider_with_same_emails
632 * @param int $sameemailallowed The value to set for $CFG->allowaccountssameemail.
633 * @param boolean $differentcase Whether to user a different case for the other user.
635 public function test_create_users_with_same_emails($sameemailallowed, $differentcase): void
{
638 $this->resetAfterTest();
639 $this->setAdminUser();
641 // Allow multiple users with the same email address.
642 set_config('allowaccountssameemail', $sameemailallowed);
646 'firstname' => 'Johnny',
647 'lastname' => 'Bravo',
648 'email' => 's1@example.com',
649 'password' => 'Passw0rd!'
653 'firstname' => 'John',
655 'email' => $differentcase ?
'S1@EXAMPLE.COM' : 's1@example.com',
656 'password' => 'Passw0rd!'
660 if (!$sameemailallowed) {
661 // This should throw an exception when $CFG->allowaccountssameemail is empty.
662 $this->expectException(\invalid_parameter_exception
::class);
666 core_user_external
::create_users($users);
668 // Confirm that the users have been created.
669 list($insql, $params) = $DB->get_in_or_equal(['s1', 's2']);
670 $this->assertEquals(2, $DB->count_records_select('user', 'username ' . $insql, $params));
674 * Test create_users with invalid parameters
676 * @dataProvider data_create_users_invalid_parameter
677 * @param array $data User data to attempt to register.
678 * @param string $expectmessage Expected exception message.
680 public function test_create_users_invalid_parameter(array $data, $expectmessage): void
{
681 global $USER, $CFG, $DB;
683 $this->resetAfterTest(true);
684 $this->assignUserCapability('moodle/user:create', SYSCONTEXTID
);
686 $this->expectException('invalid_parameter_exception');
687 $this->expectExceptionMessage($expectmessage);
689 core_user_external
::create_users(array($data));
693 * Data provider for {@see self::test_create_users_invalid_parameter()}.
697 public function data_create_users_invalid_parameter() {
699 'blank_username' => [
702 'firstname' => 'Foo',
704 'email' => 'foobar@example.com',
705 'createpassword' => 1,
707 'expectmessage' => 'The field username cannot be blank',
709 'blank_firtname' => [
711 'username' => 'foobar',
712 'firstname' => "\t \n",
714 'email' => 'foobar@example.com',
715 'createpassword' => 1,
717 'expectmessage' => 'The field firstname cannot be blank',
719 'blank_lastname' => [
721 'username' => 'foobar',
724 'email' => 'foobar@example.com',
725 'createpassword' => 1,
727 'expectmessage' => 'The field lastname cannot be blank',
731 'username' => 'foobar',
732 'firstname' => 'Foo',
734 'email' => '@foobar',
735 'createpassword' => 1,
737 'expectmessage' => 'Email address is invalid',
739 'missing_password' => [
741 'username' => 'foobar',
742 'firstname' => 'Foo',
744 'email' => 'foobar@example.com',
746 'expectmessage' => 'Invalid password: you must provide a password, or set createpassword',
754 public function test_delete_users(): void
{
755 global $USER, $CFG, $DB;
757 $this->resetAfterTest(true);
759 $user1 = self
::getDataGenerator()->create_user();
760 $user2 = self
::getDataGenerator()->create_user();
762 // Check the users were correctly created.
763 $this->assertEquals(2, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
764 array('userid1' => $user1->id
, 'userid2' => $user2->id
)));
766 $context = \context_system
::instance();
767 $roleid = $this->assignUserCapability('moodle/user:delete', $context->id
);
769 // Call the external function.
770 core_user_external
::delete_users(array($user1->id
, $user2->id
));
772 // Check we retrieve no users + no error on capability.
773 $this->assertEquals(0, $DB->count_records_select('user', 'deleted = 0 AND (id = :userid1 OR id = :userid2)',
774 array('userid1' => $user1->id
, 'userid2' => $user2->id
)));
776 // Call without required capability.
777 $this->unassignUserCapability('moodle/user:delete', $context->id
, $roleid);
778 $this->expectException('required_capability_exception');
779 core_user_external
::delete_users(array($user1->id
, $user2->id
));
785 public function test_update_users(): void
{
786 global $USER, $CFG, $DB;
788 $this->resetAfterTest(true);
789 $this->preventResetByRollback();
791 $wsuser = self
::getDataGenerator()->create_user();
792 self
::setUser($wsuser);
794 $context = \context_user
::instance($USER->id
);
795 $contextid = $context->id
;
796 $filename = "reddot.png";
797 $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38"
798 . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
800 // Call the files api to create a file.
801 $draftfile = core_files_external
::upload($contextid, 'user', 'draft', 0, '/',
802 $filename, $filecontent, null, null);
803 $draftfile = external_api
::clean_returnvalue(core_files_external
::upload_returns(), $draftfile);
805 $draftid = $draftfile['itemid'];
807 $user1 = self
::getDataGenerator()->create_user();
811 'username' => 'usernametest1',
812 'password' => 'Moodle2012!',
813 'idnumber' => 'idnumbertest1',
814 'firstname' => 'First Name User Test 1',
815 'lastname' => 'Last Name User Test 1',
816 'middlename' => 'Middle Name User Test 1',
817 'lastnamephonetic' => '最後のお名前のテスト一号',
818 'firstnamephonetic' => 'お名前のテスト一号',
819 'alternatename' => 'Alternate Name User Test 1',
820 'email' => 'usertest1@example.com',
821 'description' => 'This is a description for user 1',
823 'userpicture' => $draftid,
826 'type' => 'htmleditor',
829 'type' => 'invialidpreference',
833 'department' => 'College of Science',
834 'institution' => 'National Institute of Physics',
835 'phone1' => '01 2345 6789',
837 'interests' => 'badminton, basketball, cooking, '
840 $context = \context_system
::instance();
841 $roleid = $this->assignUserCapability('moodle/user:update', $context->id
);
842 $this->assignUserCapability('moodle/user:editprofile', $context->id
, $roleid);
844 // Check we can't update deleted users, guest users, site admin.
845 $user2 = $user3 = $user4 = $user1;
846 $user2['id'] = $CFG->siteguest
;
848 $siteadmins = explode(',', $CFG->siteadmins
);
849 $user3['id'] = array_shift($siteadmins);
851 $userdeleted = self
::getDataGenerator()->create_user();
852 $user4['id'] = $userdeleted->id
;
853 user_delete_user($userdeleted);
855 $user5 = self
::getDataGenerator()->create_user();
856 $user5 = array('id' => $user5->id
, 'email' => $user5->email
);
858 // Call the external function.
859 $returnvalue = core_user_external
::update_users(array($user1, $user2, $user3, $user4));
860 $returnvalue = external_api
::clean_returnvalue(core_user_external
::update_users_returns(), $returnvalue);
863 $this->assertEquals($user2['id'], $returnvalue['warnings'][0]['itemid']); // Guest user.
864 $this->assertEquals('usernotupdatedguest', $returnvalue['warnings'][0]['warningcode']);
865 $this->assertEquals($user3['id'], $returnvalue['warnings'][1]['itemid']); // Admin user.
866 $this->assertEquals('usernotupdatedadmin', $returnvalue['warnings'][1]['warningcode']);
867 $this->assertEquals($user4['id'], $returnvalue['warnings'][2]['itemid']); // Deleted user.
868 $this->assertEquals('usernotupdateddeleted', $returnvalue['warnings'][2]['warningcode']);
870 $dbuser2 = $DB->get_record('user', array('id' => $user2['id']));
871 $this->assertNotEquals($dbuser2->username
, $user2['username']);
872 $dbuser3 = $DB->get_record('user', array('id' => $user3['id']));
873 $this->assertNotEquals($dbuser3->username
, $user3['username']);
874 $dbuser4 = $DB->get_record('user', array('id' => $user4['id']));
875 $this->assertNotEquals($dbuser4->username
, $user4['username']);
877 $dbuser = $DB->get_record('user', array('id' => $user1['id']));
878 $this->assertEquals($dbuser->username
, $user1['username']);
879 $this->assertEquals($dbuser->idnumber
, $user1['idnumber']);
880 $this->assertEquals($dbuser->firstname
, $user1['firstname']);
881 $this->assertEquals($dbuser->lastname
, $user1['lastname']);
882 $this->assertEquals($dbuser->email
, $user1['email']);
883 $this->assertEquals($dbuser->description
, $user1['description']);
884 $this->assertEquals($dbuser->city
, $user1['city']);
885 $this->assertEquals($dbuser->country
, $user1['country']);
886 $this->assertNotEquals(0, $dbuser->picture
, 'Picture must be set to the new icon itemid for this user');
887 $this->assertEquals($dbuser->department
, $user1['department']);
888 $this->assertEquals($dbuser->institution
, $user1['institution']);
889 $this->assertEquals($dbuser->phone1
, $user1['phone1']);
890 $this->assertEquals($dbuser->maildisplay
, $user1['maildisplay']);
891 $this->assertEquals('atto', get_user_preferences('htmleditor', null, $dbuser));
892 $this->assertEquals(null, get_user_preferences('invalidpreference', null, $dbuser));
894 // Confirm user interests have been saved.
895 $interests = \core_tag_tag
::get_item_tags_array('core', 'user', $user1['id'], \core_tag_tag
::BOTH_STANDARD_AND_NOT
, 0, false);
896 // There should be 3 user interests.
897 $this->assertCount(3, $interests);
899 // Confirm no picture change when parameter is not supplied.
900 unset($user1['userpicture']);
901 core_user_external
::update_users(array($user1));
902 $dbusernopic = $DB->get_record('user', array('id' => $user1['id']));
903 $this->assertEquals($dbuser->picture
, $dbusernopic->picture
, 'Picture not change without the parameter.');
905 // Confirm delete of picture deletes the picture from the user record.
906 $user1['userpicture'] = 0;
907 core_user_external
::update_users(array($user1));
908 $dbuserdelpic = $DB->get_record('user', array('id' => $user1['id']));
909 $this->assertEquals(0, $dbuserdelpic->picture
, 'Picture must be deleted when sent as 0.');
911 // Updating user with an invalid email.
912 $user5['email'] = 'bogus';
913 $returnvalue = core_user_external
::update_users(array($user5));
914 $returnvalue = external_api
::clean_returnvalue(core_user_external
::update_users_returns(), $returnvalue);
915 $this->assertEquals('useremailinvalid', $returnvalue['warnings'][0]['warningcode']);
916 $this->assertStringContainsString('Invalid email address',
917 $returnvalue['warnings'][0]['message']);
919 // Updating user with a duplicate email.
920 $user5['email'] = $user1['email'];
921 $returnvalue = core_user_external
::update_users(array($user1, $user5));
922 $returnvalue = external_api
::clean_returnvalue(core_user_external
::update_users_returns(), $returnvalue);
923 $this->assertEquals('useremailduplicate', $returnvalue['warnings'][0]['warningcode']);
924 $this->assertStringContainsString('Duplicate email address',
925 $returnvalue['warnings'][0]['message']);
927 // Updating a user that does not exist.
929 $returnvalue = core_user_external
::update_users(array($user5));
930 $returnvalue = external_api
::clean_returnvalue(core_user_external
::update_users_returns(), $returnvalue);
931 $this->assertEquals('invaliduserid', $returnvalue['warnings'][0]['warningcode']);
932 $this->assertStringContainsString('Invalid user ID',
933 $returnvalue['warnings'][0]['message']);
935 // Updating a remote user.
936 $user1['mnethostid'] = 5;
937 user_update_user($user1); // Update user not using webservice.
938 unset($user1['mnethostid']); // The mnet host ID field is not in the allowed field list for the webservice.
939 $returnvalue = core_user_external
::update_users(array($user1));
940 $returnvalue = external_api
::clean_returnvalue(core_user_external
::update_users_returns(), $returnvalue);
941 $this->assertEquals('usernotupdatedremote', $returnvalue['warnings'][0]['warningcode']);
942 $this->assertStringContainsString('User is a remote user',
943 $returnvalue['warnings'][0]['message']);
945 // Call without required capability.
946 $this->unassignUserCapability('moodle/user:update', $context->id
, $roleid);
947 $this->expectException('required_capability_exception');
948 core_user_external
::update_users(array($user1));
952 * Data provider for testing \core_user_external::update_users() for users with same emails
956 public function users_with_same_emails() {
958 'Same emails not allowed: Update name using exactly the same email' => [
959 0, 'John', 's1@example.com', 'Johnny', 's1@example.com', false, true
961 'Same emails not allowed: Update using someone else\'s email' => [
962 0, 'John', 's1@example.com', 'Johnny', 's2@example.com', true, false
964 'Same emails allowed: Update using someone else\'s email' => [
965 1, 'John', 's1@example.com', 'Johnny', 's2@example.com', true, true
967 'Same emails not allowed: Update using same email but with different case' => [
968 0, 'John', 's1@example.com', 'Johnny', 'S1@EXAMPLE.COM', false, true
970 'Same emails not allowed: Update using another user\'s email similar to user but with different case' => [
971 0, 'John', 's1@example.com', 'Johnny', 'S1@EXAMPLE.COM', true, false
973 'Same emails allowed: Update using another user\'s email similar to user but with different case' => [
974 1, 'John', 's1@example.com', 'Johnny', 'S1@EXAMPLE.COM', true, true
980 * Test update_users using similar emails with varying cases.
982 * @dataProvider users_with_same_emails
983 * @param boolean $allowsameemail The value to set for $CFG->allowaccountssameemail.
984 * @param string $currentname The user's current name.
985 * @param string $currentemail The user's current email.
986 * @param string $newname The user's new name.
987 * @param string $newemail The user's new email.
988 * @param boolean $withanotheruser Whether to create another user that has the same email as the target user's new email.
989 * @param boolean $successexpected Whether we expect that the target user's email/name will be updated.
991 public function test_update_users_emails_with_different_cases($allowsameemail, $currentname, $currentemail,
992 $newname, $newemail, $withanotheruser, $successexpected): void
{
995 $this->resetAfterTest();
996 $this->setAdminUser();
998 // Set the value for $CFG->allowaccountssameemail.
999 set_config('allowaccountssameemail', $allowsameemail);
1001 $generator = self
::getDataGenerator();
1003 // Create the user that we wish to update.
1004 $usertoupdate = $generator->create_user(['email' => $currentemail, 'firstname' => $currentname]);
1006 if ($withanotheruser) {
1007 // Create another user that has the same email as the new email that we'd like to update for our target user.
1008 $generator->create_user(['email' => $newemail]);
1011 // Build the user update parameters.
1013 'id' => $usertoupdate->id
,
1014 'email' => $newemail,
1015 'firstname' => $newname
1017 // Let's try to update the user's information.
1018 core_user_external
::update_users([$updateparams]);
1020 // Fetch the updated user record.
1021 $userrecord = $DB->get_record('user', ['id' => $usertoupdate->id
], 'id, email, firstname');
1023 // If we expect the update to succeed, then the email/name would have been changed.
1024 if ($successexpected) {
1025 $expectedemail = $newemail;
1026 $expectedname = $newname;
1028 $expectedemail = $currentemail;
1029 $expectedname = $currentname;
1031 // Confirm that our expectations are met.
1032 $this->assertEquals($expectedemail, $userrecord->email
);
1033 $this->assertEquals($expectedname, $userrecord->firstname
);
1037 * Test add_user_private_files
1039 public function test_add_user_private_files(): void
{
1040 global $USER, $CFG, $DB;
1042 $this->resetAfterTest(true);
1044 $context = \context_system
::instance();
1045 $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id
);
1047 $context = \context_user
::instance($USER->id
);
1048 $contextid = $context->id
;
1049 $component = "user";
1050 $filearea = "draft";
1053 $filename = "Simple.txt";
1054 $filecontent = base64_encode("Let us create a nice simple file");
1055 $contextlevel = null;
1057 $browser = get_file_browser();
1059 // Call the files api to create a file.
1060 $draftfile = core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath,
1061 $filename, $filecontent, $contextlevel, $instanceid);
1062 $draftfile = external_api
::clean_returnvalue(core_files_external
::upload_returns(), $draftfile);
1064 $draftid = $draftfile['itemid'];
1065 // Make sure the file was created.
1066 $file = $browser->get_file_info($context, $component, $filearea, $draftid, $filepath, $filename);
1067 $this->assertNotEmpty($file);
1069 // Make sure the file does not exist in the user private files.
1070 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
1071 $this->assertEmpty($file);
1073 // Call the external function.
1074 core_user_external
::add_user_private_files($draftid);
1076 // Make sure the file was added to the user private files.
1077 $file = $browser->get_file_info($context, $component, 'private', 0, $filepath, $filename);
1078 $this->assertNotEmpty($file);
1083 * Test add_user_private_files quota
1085 public function test_add_user_private_files_quota(): void
{
1086 global $USER, $CFG, $DB;
1088 $this->resetAfterTest(true);
1090 $context = \context_system
::instance();
1091 $roleid = $this->assignUserCapability('moodle/user:manageownfiles', $context->id
);
1093 $context = \context_user
::instance($USER->id
);
1094 $contextid = $context->id
;
1095 $component = "user";
1096 $filearea = "draft";
1099 $filename = "Simple.txt";
1100 $filecontent = base64_encode("Let us create a nice simple file");
1101 $contextlevel = null;
1103 $browser = get_file_browser();
1105 // Call the files api to create a file.
1106 $draftfile = core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath,
1107 $filename, $filecontent, $contextlevel, $instanceid);
1108 $draftfile = external_api
::clean_returnvalue(core_files_external
::upload_returns(), $draftfile);
1109 $draftid = $draftfile['itemid'];
1111 // Call the external function to add the file to private files.
1112 core_user_external
::add_user_private_files($draftid);
1114 // Force the quota so we are sure it won't be space to add the new file.
1115 $fileareainfo = file_get_file_area_info($contextid, 'user', 'private');
1116 $CFG->userquota
= $fileareainfo['filesize_without_references'] +
1;
1118 // Generate a new draftitemid for the same testfile.
1119 $draftfile = core_files_external
::upload($contextid, $component, $filearea, $itemid, $filepath,
1120 $filename, $filecontent, $contextlevel, $instanceid);
1121 $draftid = $draftfile['itemid'];
1123 $this->expectException('moodle_exception');
1124 $this->expectExceptionMessage(get_string('maxareabytes', 'error'));
1126 // Call the external function to include the new file.
1127 core_user_external
::add_user_private_files($draftid);
1131 * Test add user device
1133 public function test_add_user_device(): void
{
1134 global $USER, $CFG, $DB;
1136 $this->resetAfterTest(true);
1139 'appid' => 'com.moodle.moodlemobile',
1141 'model' => 'Nexus 4',
1142 'platform' => 'Android',
1143 'version' => '4.2.2',
1144 'pushid' => 'apushdkasdfj4835',
1145 'uuid' => 'asdnfl348qlksfaasef859',
1146 'publickey' => null,
1149 // Call the external function.
1150 core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
1151 $device['version'], $device['pushid'], $device['uuid']);
1153 $created = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
1154 $created = (array) $created;
1156 $this->assertEquals($device, array_intersect_key((array)$created, $device));
1158 // Test reuse the same pushid value.
1159 $warnings = core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
1160 $device['version'], $device['pushid'], $device['uuid']);
1161 // We need to execute the return values cleaning process to simulate the web service server.
1162 $warnings = external_api
::clean_returnvalue(core_user_external
::add_user_device_returns(), $warnings);
1163 $this->assertCount(1, $warnings);
1165 // Test update an existing device.
1166 $device['pushid'] = 'different than before';
1167 $device['publickey'] = 'MFsxCzAJBgNVBAYTAkZSMRMwEQYDVQQ';
1168 $warnings = core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
1169 $device['version'], $device['pushid'], $device['uuid'], $device['publickey']);
1170 $warnings = external_api
::clean_returnvalue(core_user_external
::add_user_device_returns(), $warnings);
1172 $this->assertEquals(1, $DB->count_records('user_devices'));
1173 $updated = $DB->get_record('user_devices', array('pushid' => $device['pushid']));
1174 $this->assertEquals($device, array_intersect_key((array)$updated, $device));
1176 // Test creating a new device just changing the uuid.
1177 $device['uuid'] = 'newuidforthesameuser';
1178 $device['pushid'] = 'new different than before';
1179 $warnings = core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
1180 $device['version'], $device['pushid'], $device['uuid']);
1181 $warnings = external_api
::clean_returnvalue(core_user_external
::add_user_device_returns(), $warnings);
1182 $this->assertEquals(2, $DB->count_records('user_devices'));
1186 * Test remove user device
1188 public function test_remove_user_device(): void
{
1189 global $USER, $CFG, $DB;
1191 $this->resetAfterTest(true);
1194 'appid' => 'com.moodle.moodlemobile',
1196 'model' => 'Nexus 4',
1197 'platform' => 'Android',
1198 'version' => '4.2.2',
1199 'pushid' => 'apushdkasdfj4835',
1200 'uuid' => 'ABCDE3723ksdfhasfaasef859'
1203 // A device with the same properties except the appid and pushid.
1205 $device2['pushid'] = "0987654321";
1206 $device2['appid'] = "other.app.com";
1208 $this->setAdminUser();
1209 // Create a user device using the external API function.
1210 core_user_external
::add_user_device($device['appid'], $device['name'], $device['model'], $device['platform'],
1211 $device['version'], $device['pushid'], $device['uuid']);
1213 // Create the same device but for a different app.
1214 core_user_external
::add_user_device($device2['appid'], $device2['name'], $device2['model'], $device2['platform'],
1215 $device2['version'], $device2['pushid'], $device2['uuid']);
1217 // Try to remove a device that does not exist.
1218 $result = core_user_external
::remove_user_device('1234567890');
1219 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
1220 $this->assertFalse($result['removed']);
1221 $this->assertCount(1, $result['warnings']);
1223 // Try to remove a device that does not exist for an existing app.
1224 $result = core_user_external
::remove_user_device('1234567890', $device['appid']);
1225 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
1226 $this->assertFalse($result['removed']);
1227 $this->assertCount(1, $result['warnings']);
1229 // Remove an existing device for an existing app. This will remove one of the two devices.
1230 $result = core_user_external
::remove_user_device($device['uuid'], $device['appid']);
1231 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
1232 $this->assertTrue($result['removed']);
1234 // Remove all the devices. This must remove the remaining device.
1235 $result = core_user_external
::remove_user_device($device['uuid']);
1236 $result = external_api
::clean_returnvalue(core_user_external
::remove_user_device_returns(), $result);
1237 $this->assertTrue($result['removed']);
1241 * Test get_user_preferences
1243 public function test_get_user_preferences(): void
{
1244 $this->resetAfterTest(true);
1246 $user = self
::getDataGenerator()->create_user();
1247 set_user_preference('calendar_maxevents', 1, $user);
1248 set_user_preference('some_random_text', 'text', $user);
1250 $this->setUser($user);
1252 $result = core_user_external
::get_user_preferences();
1253 $result = external_api
::clean_returnvalue(core_user_external
::get_user_preferences_returns(), $result);
1254 $this->assertCount(0, $result['warnings']);
1255 // Expect 3, _lastloaded is always returned.
1256 $this->assertCount(3, $result['preferences']);
1258 foreach ($result['preferences'] as $pref) {
1259 if ($pref['name'] === '_lastloaded') {
1262 // Check we receive the expected preferences.
1263 $this->assertEquals(get_user_preferences($pref['name']), $pref['value']);
1266 // Retrieve just one preference.
1267 $result = core_user_external
::get_user_preferences('some_random_text');
1268 $result = external_api
::clean_returnvalue(core_user_external
::get_user_preferences_returns(), $result);
1269 $this->assertCount(0, $result['warnings']);
1270 $this->assertCount(1, $result['preferences']);
1271 $this->assertEquals('text', $result['preferences'][0]['value']);
1273 // Retrieve non-existent preference.
1274 $result = core_user_external
::get_user_preferences('non_existent');
1275 $result = external_api
::clean_returnvalue(core_user_external
::get_user_preferences_returns(), $result);
1276 $this->assertCount(0, $result['warnings']);
1277 $this->assertCount(1, $result['preferences']);
1278 $this->assertEquals(null, $result['preferences'][0]['value']);
1280 // Check that as admin we can retrieve all the preferences for any user.
1281 $this->setAdminUser();
1282 $result = core_user_external
::get_user_preferences('', $user->id
);
1283 $result = external_api
::clean_returnvalue(core_user_external
::get_user_preferences_returns(), $result);
1284 $this->assertCount(0, $result['warnings']);
1285 $this->assertCount(3, $result['preferences']);
1287 foreach ($result['preferences'] as $pref) {
1288 if ($pref['name'] === '_lastloaded') {
1291 // Check we receive the expected preferences.
1292 $this->assertEquals(get_user_preferences($pref['name'], null, $user), $pref['value']);
1295 // Check that as a non admin user we cannot retrieve other users preferences.
1296 $anotheruser = self
::getDataGenerator()->create_user();
1297 $this->setUser($anotheruser);
1299 $this->expectException('required_capability_exception');
1300 $result = core_user_external
::get_user_preferences('', $user->id
);
1304 * Test update_picture
1306 public function test_update_picture(): void
{
1309 $this->resetAfterTest(true);
1311 $user = self
::getDataGenerator()->create_user();
1312 self
::setUser($user);
1314 $context = \context_user
::instance($USER->id
);
1315 $contextid = $context->id
;
1316 $filename = "reddot.png";
1317 $filecontent = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38"
1318 . "GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==";
1320 // Call the files api to create a file.
1321 $draftfile = core_files_external
::upload($contextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null);
1322 $draftid = $draftfile['itemid'];
1324 // Change user profile image.
1325 $result = core_user_external
::update_picture($draftid);
1326 $result = external_api
::clean_returnvalue(core_user_external
::update_picture_returns(), $result);
1327 $picture = $DB->get_field('user', 'picture', array('id' => $user->id
));
1328 // The new revision is in the url for the user.
1329 $this->assertStringContainsString($picture, $result['profileimageurl']);
1330 // Check expected URL for serving the image.
1331 $this->assertStringContainsString("/$contextid/user/icon", $result['profileimageurl']);
1334 $result = core_user_external
::update_picture(0, true);
1335 $result = external_api
::clean_returnvalue(core_user_external
::update_picture_returns(), $result);
1336 $picture = $DB->get_field('user', 'picture', array('id' => $user->id
));
1338 $this->assertEquals(0, $picture);
1340 // Add again the user profile image (as admin).
1341 $this->setAdminUser();
1343 $context = \context_user
::instance($USER->id
);
1344 $admincontextid = $context->id
;
1345 $draftfile = core_files_external
::upload($admincontextid, 'user', 'draft', 0, '/', $filename, $filecontent, null, null);
1346 $draftid = $draftfile['itemid'];
1348 $result = core_user_external
::update_picture($draftid, false, $user->id
);
1349 $result = external_api
::clean_returnvalue(core_user_external
::update_picture_returns(), $result);
1350 // The new revision is in the url for the user.
1351 $picture = $DB->get_field('user', 'picture', array('id' => $user->id
));
1352 $this->assertStringContainsString($picture, $result['profileimageurl']);
1353 $this->assertStringContainsString("/$contextid/user/icon", $result['profileimageurl']);
1357 * Test update_picture disabled
1359 public function test_update_picture_disabled(): void
{
1361 $this->resetAfterTest(true);
1362 $CFG->disableuserimages
= true;
1364 $this->setAdminUser();
1365 $this->expectException('moodle_exception');
1366 core_user_external
::update_picture(0);
1370 * Test set_user_preferences
1372 public function test_set_user_preferences_save(): void
{
1374 $this->resetAfterTest(true);
1376 $user1 = self
::getDataGenerator()->create_user();
1377 $user2 = self
::getDataGenerator()->create_user();
1379 // Save users preferences.
1380 $this->setAdminUser();
1381 $preferences = array(
1383 'name' => 'htmleditor',
1385 'userid' => $user1->id
,
1388 'name' => 'htmleditor',
1390 'userid' => $user2->id
,
1394 $result = core_user_external
::set_user_preferences($preferences);
1395 $result = external_api
::clean_returnvalue(core_user_external
::set_user_preferences_returns(), $result);
1396 $this->assertCount(0, $result['warnings']);
1397 $this->assertCount(2, $result['saved']);
1399 // Get preference from DB to avoid cache.
1400 $this->assertEquals('atto', $DB->get_field('user_preferences', 'value',
1401 array('userid' => $user1->id
, 'name' => 'htmleditor')));
1402 $this->assertEquals('tiny', $DB->get_field('user_preferences', 'value',
1403 array('userid' => $user2->id
, 'name' => 'htmleditor')));
1407 * Test set_user_preferences
1409 public function test_set_user_preferences_save_invalid_pref(): void
{
1411 $this->resetAfterTest(true);
1413 $user1 = self
::getDataGenerator()->create_user();
1415 // Save users preferences.
1416 $this->setAdminUser();
1417 $preferences = array(
1419 'name' => 'some_random_pref',
1421 'userid' => $user1->id
,
1425 $result = core_user_external
::set_user_preferences($preferences);
1426 $result = external_api
::clean_returnvalue(core_user_external
::set_user_preferences_returns(), $result);
1427 $this->assertCount(1, $result['warnings']);
1428 $this->assertCount(0, $result['saved']);
1429 $this->assertEquals('nopermission', $result['warnings'][0]['warningcode']);
1431 // Nothing was written to DB.
1432 $this->assertEmpty($DB->count_records('user_preferences', array('name' => 'some_random_pref')));
1436 * Test set_user_preferences for an invalid user
1438 public function test_set_user_preferences_invalid_user(): void
{
1439 $this->resetAfterTest(true);
1441 $this->setAdminUser();
1442 $preferences = array(
1444 'name' => 'calendar_maxevents',
1450 $result = core_user_external
::set_user_preferences($preferences);
1451 $result = external_api
::clean_returnvalue(core_user_external
::set_user_preferences_returns(), $result);
1452 $this->assertCount(1, $result['warnings']);
1453 $this->assertCount(0, $result['saved']);
1454 $this->assertEquals('invaliduser', $result['warnings'][0]['warningcode']);
1455 $this->assertEquals(-2, $result['warnings'][0]['itemid']);
1459 * Test set_user_preferences using an invalid preference
1461 public function test_set_user_preferences_invalid_preference(): void
{
1464 $this->resetAfterTest(true);
1465 // Create a very long value.
1466 $this->setAdminUser();
1467 $preferences = array(
1469 'name' => 'calendar_maxevents',
1470 'value' => str_repeat('a', 1334),
1471 'userid' => $USER->id
1475 $result = core_user_external
::set_user_preferences($preferences);
1476 $result = external_api
::clean_returnvalue(core_user_external
::set_user_preferences_returns(), $result);
1477 $this->assertCount(0, $result['warnings']);
1478 $this->assertCount(1, $result['saved']);
1479 // Cleaned valud of the preference was saved.
1480 $this->assertEquals(1, $DB->get_field('user_preferences', 'value',
1481 array('userid' => $USER->id
, 'name' => 'calendar_maxevents')));
1485 * Test set_user_preferences for other user not being admin
1487 public function test_set_user_preferences_capability(): void
{
1488 $this->resetAfterTest(true);
1490 $user1 = self
::getDataGenerator()->create_user();
1491 $user2 = self
::getDataGenerator()->create_user();
1493 $this->setUser($user1);
1494 $preferences = array(
1496 'name' => 'calendar_maxevents',
1498 'userid' => $user2->id
1502 $result = core_user_external
::set_user_preferences($preferences);
1504 $this->assertCount(1, $result['warnings']);
1505 $this->assertCount(0, $result['saved']);
1506 $this->assertEquals('nopermission', $result['warnings'][0]['warningcode']);
1507 $this->assertEquals($user2->id
, $result['warnings'][0]['itemid']);
1511 * Test update_user_preferences unsetting an existing preference.
1513 public function test_update_user_preferences_unset(): void
{
1515 $this->resetAfterTest(true);
1517 $user = self
::getDataGenerator()->create_user();
1519 // Save users preferences.
1520 $this->setAdminUser();
1521 $preferences = array(
1523 'name' => 'htmleditor',
1525 'userid' => $user->id
,
1529 $result = core_user_external
::set_user_preferences($preferences);
1530 $result = external_api
::clean_returnvalue(core_user_external
::set_user_preferences_returns(), $result);
1531 $this->assertCount(0, $result['warnings']);
1532 $this->assertCount(1, $result['saved']);
1534 // Get preference from DB to avoid cache.
1535 $this->assertEquals('atto', $DB->get_field('user_preferences', 'value',
1536 array('userid' => $user->id
, 'name' => 'htmleditor')));
1539 $result = core_user_external
::update_user_preferences($user->id
, null, array(array('type' => 'htmleditor')));
1541 $this->assertEquals(0, $DB->count_records('user_preferences', array('userid' => $user->id
, 'name' => 'htmleditor')));
1545 * Test agree_site_policy
1547 public function test_agree_site_policy(): void
{
1548 global $CFG, $DB, $USER;
1549 $this->resetAfterTest(true);
1551 $user = self
::getDataGenerator()->create_user();
1552 $this->setUser($user);
1554 // Site policy not set.
1555 $result = core_user_external
::agree_site_policy();
1556 $result = external_api
::clean_returnvalue(core_user_external
::agree_site_policy_returns(), $result);
1557 $this->assertFalse($result['status']);
1558 $this->assertCount(1, $result['warnings']);
1559 $this->assertEquals('nositepolicy', $result['warnings'][0]['warningcode']);
1561 // Set a policy issue.
1562 $CFG->sitepolicy
= 'https://moodle.org';
1563 $this->assertEquals(0, $USER->policyagreed
);
1565 $result = core_user_external
::agree_site_policy();
1566 $result = external_api
::clean_returnvalue(core_user_external
::agree_site_policy_returns(), $result);
1567 $this->assertTrue($result['status']);
1568 $this->assertCount(0, $result['warnings']);
1569 $this->assertEquals(1, $USER->policyagreed
);
1570 $this->assertEquals(1, $DB->get_field('user', 'policyagreed', array('id' => $USER->id
)));
1572 // Try again, we should get a warning.
1573 $result = core_user_external
::agree_site_policy();
1574 $result = external_api
::clean_returnvalue(core_user_external
::agree_site_policy_returns(), $result);
1575 $this->assertFalse($result['status']);
1576 $this->assertCount(1, $result['warnings']);
1577 $this->assertEquals('alreadyagreed', $result['warnings'][0]['warningcode']);
1579 // Set something to make require_login throws an exception.
1580 $otheruser = self
::getDataGenerator()->create_user();
1581 $this->setUser($otheruser);
1583 $DB->set_field('user', 'lastname', '', array('id' => $USER->id
));
1584 $USER->lastname
= '';
1586 $result = core_user_external
::agree_site_policy();
1587 $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown');
1588 } catch (\moodle_exception
$e) {
1589 $this->assertEquals('usernotfullysetup', $e->errorcode
);
1590 } catch (\Exception
$e) {
1591 $this->fail('Expecting \'usernotfullysetup\' moodle_exception to be thrown.');
1596 * Test get_private_files_info
1598 public function test_get_private_files_info(): void
{
1600 $this->resetAfterTest(true);
1601 $user = self
::getDataGenerator()->create_user();
1602 $this->setUser($user);
1603 $usercontext = \context_user
::instance($user->id
);
1605 $filerecord = array(
1606 'contextid' => $usercontext->id
,
1607 'component' => 'user',
1608 'filearea' => 'private',
1611 'filename' => 'thefile',
1614 $fs = get_file_storage();
1615 $file = $fs->create_file_from_string($filerecord, 'abc');
1617 // Get my private files information.
1618 $result = core_user_external
::get_private_files_info();
1619 $result = external_api
::clean_returnvalue(core_user_external
::get_private_files_info_returns(), $result);
1620 $this->assertEquals(1, $result['filecount']);
1621 $this->assertEquals($file->get_filesize(), $result['filesize']);
1622 $this->assertEquals(0, $result['foldercount']);
1623 $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']);
1625 // As admin, get user information.
1626 $this->setAdminUser();
1627 $result = core_user_external
::get_private_files_info($user->id
);
1628 $result = external_api
::clean_returnvalue(core_user_external
::get_private_files_info_returns(), $result);
1629 $this->assertEquals(1, $result['filecount']);
1630 $this->assertEquals($file->get_filesize(), $result['filesize']);
1631 $this->assertEquals(0, $result['foldercount']);
1632 $this->assertEquals($file->get_filesize(), $result['filesizewithoutreferences']);
1636 * Test get_private_files_info missing permissions.
1638 public function test_get_private_files_info_missing_permissions(): void
{
1640 $this->resetAfterTest(true);
1641 $user1 = self
::getDataGenerator()->create_user();
1642 $user2 = self
::getDataGenerator()->create_user();
1643 $this->setUser($user1);
1645 $this->expectException('required_capability_exception');
1646 // Try to retrieve other user private files info.
1647 core_user_external
::get_private_files_info($user2->id
);
1651 * Test the functionality of the {@see \core_user\external\search_identity} class.
1653 public function test_external_search_identity(): void
{
1656 $this->resetAfterTest(true);
1657 $this->setAdminUser();
1659 $user1 = self
::getDataGenerator()->create_user([
1660 'firstname' => 'Firstone',
1661 'lastname' => 'Lastone',
1662 'username' => 'usernameone',
1663 'idnumber' => 'idnumberone',
1664 'email' => 'userone@example.com',
1667 'department' => 'Department Foo',
1668 'institution' => 'Institution Foo',
1669 'city' => 'City One',
1673 $user2 = self
::getDataGenerator()->create_user([
1674 'firstname' => 'Firsttwo',
1675 'lastname' => 'Lasttwo',
1676 'username' => 'usernametwo',
1677 'idnumber' => 'idnumbertwo',
1678 'email' => 'usertwo@example.com',
1681 'department' => 'Department Foo',
1682 'institution' => 'Institution Foo',
1683 'city' => 'City One',
1687 $user3 = self
::getDataGenerator()->create_user([
1688 'firstname' => 'Firstthree',
1689 'lastname' => 'Lastthree',
1690 'username' => 'usernamethree',
1691 'idnumber' => 'idnumberthree',
1692 'email' => 'userthree@example.com',
1695 'department' => 'Department Foo',
1696 'institution' => 'Institution Foo',
1697 'city' => 'City One',
1701 $CFG->showuseridentity
= 'email,idnumber,city';
1702 $CFG->maxusersperpage
= 3;
1704 $result = \core_user\external\search_identity
::execute('Lastt');
1705 $result = external_api
::clean_returnvalue(\core_user\external\search_identity
::execute_returns(), $result);
1707 $this->assertEquals(2, count($result['list']));
1708 $this->assertEquals(3, $result['maxusersperpage']);
1709 $this->assertEquals(false, $result['overflow']);
1711 foreach ($result['list'] as $user) {
1712 $this->assertEquals(3, count($user['extrafields']));
1713 $this->assertEquals('email', $user['extrafields'][0]['name']);
1714 $this->assertEquals('idnumber', $user['extrafields'][1]['name']);
1715 $this->assertEquals('city', $user['extrafields'][2]['name']);
1718 $CFG->showuseridentity
= 'username';
1719 $CFG->maxusersperpage
= 2;
1721 $result = \core_user\external\search_identity
::execute('Firstt');
1722 $result = external_api
::clean_returnvalue(\core_user\external\search_identity
::execute_returns(), $result);
1724 $this->assertEquals(2, count($result['list']));
1725 $this->assertEquals(2, $result['maxusersperpage']);
1726 $this->assertEquals(false, $result['overflow']);
1728 foreach ($result['list'] as $user) {
1729 $this->assertEquals(1, count($user['extrafields']));
1730 $this->assertEquals('username', $user['extrafields'][0]['name']);
1733 $CFG->showuseridentity
= 'email';
1734 $CFG->maxusersperpage
= 2;
1736 $result = \core_user\external\search_identity
::execute('City One');
1737 $result = external_api
::clean_returnvalue(\core_user\external\search_identity
::execute_returns(), $result);
1739 $this->assertEquals(0, count($result['list']));
1740 $this->assertEquals(2, $result['maxusersperpage']);
1741 $this->assertEquals(false, $result['overflow']);
1743 $CFG->showuseridentity
= 'city';
1744 $CFG->maxusersperpage
= 2;
1746 foreach ($result['list'] as $user) {
1747 $this->assertEquals(1, count($user['extrafields']));
1748 $this->assertEquals('username', $user['extrafields'][0]['name']);
1751 $result = \core_user\external\search_identity
::execute('City One');
1752 $result = external_api
::clean_returnvalue(\core_user\external\search_identity
::execute_returns(), $result);
1754 $this->assertEquals(2, count($result['list']));
1755 $this->assertEquals(2, $result['maxusersperpage']);
1756 $this->assertEquals(true, $result['overflow']);
1760 * Test functionality of the {@see \core_user\external\search_identity} class with alternativefullnameformat defined.
1762 public function test_external_search_identity_with_alternativefullnameformat(): void
{
1765 $this->resetAfterTest(true);
1766 $this->setAdminUser();
1768 $user1 = self
::getDataGenerator()->create_user([
1770 'lastnamephonetic' => 'Koyanagi',
1772 'firstnamephonetic' => 'Aki',
1773 'email' => 'koyanagiaki@example.com',
1777 $CFG->showuseridentity
= 'email';
1778 $CFG->maxusersperpage
= 3;
1779 $CFG->alternativefullnameformat
=
1780 '<ruby>lastname firstname <rp>(</rp><rt>lastnamephonetic firstnamephonetic</rt><rp>)</rp></ruby>';
1782 $result = \core_user\external\search_identity
::execute('Ak');
1783 $result = external_api
::clean_returnvalue(\core_user\external\search_identity
::execute_returns(), $result);
1785 $this->assertEquals(1, count($result['list']));
1786 $this->assertEquals(3, $result['maxusersperpage']);
1787 $this->assertEquals(false, $result['overflow']);
1789 foreach ($result['list'] as $user) {
1790 $this->assertEquals(1, count($user['extrafields']));
1791 $this->assertEquals('email', $user['extrafields'][0]['name']);
1796 * Test verifying that update_user_preferences prevents changes to the default homepage for other users.
1798 public function test_update_user_preferences_homepage_permission_callback(): void
{
1800 $this->resetAfterTest();
1802 $user = self
::getDataGenerator()->create_user();
1803 $this->setUser($user);
1804 $adminuser = get_admin();
1806 // Allow user selection of the default homepage via preferences.
1807 set_config('defaulthomepage', HOMEPAGE_USER
);
1809 // Try to save another user's home page preference which uses the permissioncallback.
1812 'name' => 'user_home_page_preference',
1814 'userid' => $adminuser->id
,
1817 $result = core_user_external
::set_user_preferences($preferences);
1818 $result = external_api
::clean_returnvalue(core_user_external
::set_user_preferences_returns(), $result);
1819 $this->assertCount(1, $result['warnings']);
1820 $this->assertCount(0, $result['saved']);
1822 // Verify no change to the preference, checking from DB to avoid cache.
1823 $this->assertEquals(null, $DB->get_field('user_preferences', 'value',
1824 ['userid' => $adminuser->id
, 'name' => 'user_home_page_preference']));