MDL-57840 libraries: Fixed missing fields in get_users_by_field().

This commit is contained in:
Daniel Thee Roperto 2017-02-01 14:52:17 +11:00
parent 9ec952f237
commit b80caca188
2 changed files with 39 additions and 0 deletions

View File

@ -554,6 +554,15 @@ function user_get_user_details($user, $course = null, array $userfields = array(
$userdetails['preferences'] = $preferences;
}
if ($currentuser or has_capability('moodle/user:viewalldetails', $context)) {
$extrafields = ['auth', 'confirmed', 'lang', 'theme', 'timezone', 'mailformat'];
foreach ($extrafields as $extrafield) {
if (in_array($extrafield, $userfields) && isset($user->$extrafield)) {
$userdetails[$extrafield] = $user->$extrafield;
}
}
}
return $userdetails;
}

View File

@ -624,4 +624,34 @@ class core_userliblib_testcase extends advanced_testcase {
$this->expectException('moodle_exception');
$result = user_get_user_details($student, $course1, array('wrongrequiredfield'));
}
/**
* Regression test for MDL-57840.
*
* Ensure the fields "auth, confirmed, idnumber, lang, theme, timezone and mailformat" are present when
* calling user_get_user_details() function.
*/
public function test_user_get_user_details_missing_fields() {
$this->resetAfterTest(true);
$this->setAdminUser(); // We need capabilities to view the data.
$user = self::getDataGenerator()->create_user([
'auth' => 'auth_something',
'confirmed' => '0',
'idnumber' => 'someidnumber',
'lang' => 'en_ar',
'theme' => 'mytheme',
'timezone' => '50',
'mailformat' => '0',
]);
// Fields that should get by default.
$got = user_get_user_details($user);
self::assertSame('auth_something', $got['auth']);
self::assertSame('0', $got['confirmed']);
self::assertSame('someidnumber', $got['idnumber']);
self::assertSame('en_ar', $got['lang']);
self::assertSame('mytheme', $got['theme']);
self::assertSame('50', $got['timezone']);
self::assertSame('0', $got['mailformat']);
}
}