From f8790390d001c4ba4def43c9011391e357be5e39 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 14 Sep 2021 09:41:58 +0100 Subject: [PATCH] MDL-72458 user: add timezone to user profile page. --- lib/myprofilelib.php | 6 ++++ lib/tests/myprofilelib_test.php | 51 +++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/lib/myprofilelib.php b/lib/myprofilelib.php index bed91290310..57a90741849 100644 --- a/lib/myprofilelib.php +++ b/lib/myprofilelib.php @@ -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); diff --git a/lib/tests/myprofilelib_test.php b/lib/tests/myprofilelib_test.php index 47cab7d4c37..4c7fc4f30eb 100644 --- a/lib/tests/myprofilelib_test.php +++ b/lib/tests/myprofilelib_test.php @@ -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.