MDL-72458 user: add timezone to user profile page.

This commit is contained in:
Paul Holden 2021-09-14 09:41:58 +01:00
parent 941a29925e
commit f8790390d0
2 changed files with 57 additions and 0 deletions

View File

@ -186,6 +186,12 @@ function core_myprofile_navigation(core_user\output\myprofile\tree $tree, $user,
$tree->add_node($node);
}
if (!isset($hiddenfields['timezone'])) {
$node = new core_user\output\myprofile\node('contact', 'timezone', get_string('timezone'), null, null,
core_date::get_user_timezone($user));
$tree->add_node($node);
}
if (isset($identityfields['address']) && $user->address) {
$node = new core_user\output\myprofile\node('contact', 'address', get_string('address'), null, null, $user->address);
$tree->add_node($node);

View File

@ -230,6 +230,57 @@ class core_myprofilelib_testcase extends advanced_testcase {
}
}
/**
* Data provider for {@see test_core_myprofile_navigation_contact_timezone}
*
* @return array[]
*/
public function core_myprofile_navigation_contact_timezone_provider(): array {
return [
'Hidden field' => ['timezone', '99', '99', null],
'Forced timezone' => ['', 'Europe/London', 'Pacific/Tahiti', 'Europe/London'],
'User timezone (default)' => ['', '99', '99', 'Australia/Perth'],
'User timezone (selected)' => ['', '99', 'Pacific/Tahiti', 'Pacific/Tahiti'],
];
}
/**
* Test timezone node added to user profile navigation
*
* @param string $hiddenuserfields
* @param string $forcetimezone Timezone identifier or '99' (User can choose their own)
* @param string $usertimezone Timezone identifier or '99' (Use server default)
* @param string|null $expectresult
* @return bool
*
* @dataProvider core_myprofile_navigation_contact_timezone_provider
*/
public function test_core_myprofile_navigation_contact_timezone(string $hiddenuserfields, string $forcetimezone,
string $usertimezone, ?string $expectresult = null): void {
set_config('hiddenuserfields', $hiddenuserfields);
set_config('forcetimezone', $forcetimezone);
// Set the timezone of our test user, and load their navigation tree.
$this->user->timezone = $usertimezone;
$this->setUser($this->user);
core_myprofile_navigation($this->tree, $this->user, true, null);
$reflector = new ReflectionObject($this->tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
/** @var \core_user\output\myprofile\node[] $tree */
$tree = $nodes->getValue($this->tree);
if ($expectresult !== null) {
$this->assertArrayHasKey('timezone', $tree);
$this->assertEquals($expectresult, $tree['timezone']->content);
} else {
$this->assertArrayNotHasKey('timezone', $tree);
}
}
/**
* Tests the core_myprofile_navigation() function as an admin viewing another user's
* profile ensuring the login activity links are shown.