From 8af901e9ed1283d69cfaa0a7dcaae4f726637287 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Tue, 6 Aug 2013 18:02:40 +0200 Subject: [PATCH] Framework/User: Implement getTimezone to retrieve the user's Timezone refs #4440 --- library/Icinga/User.php | 16 +++++++ test/php/library/Icinga/UserTest.php | 65 +++++++++++++++------------- 2 files changed, 51 insertions(+), 30 deletions(-) diff --git a/library/Icinga/User.php b/library/Icinga/User.php index 533dc3d94..2c250f896 100644 --- a/library/Icinga/User.php +++ b/library/Icinga/User.php @@ -316,4 +316,20 @@ class User } return null; } + + /** + * Retrieve the user's timezone + * + * If the user did not set a timezone, the default timezone set via config.ini will be returned + * + * @return string + */ + public function getTimezone() + { + $tz = $this->preferences->get('timezone'); + if ($tz === null) { + $tz = date_default_timezone_get(); + } + return $tz; + } } diff --git a/test/php/library/Icinga/UserTest.php b/test/php/library/Icinga/UserTest.php index 12cb64db4..dd4293769 100644 --- a/test/php/library/Icinga/UserTest.php +++ b/test/php/library/Icinga/UserTest.php @@ -1,67 +1,72 @@ markTestIncomplete('testListGroups is not implemented yet'); } - /** - * Test for User::IsMemberOf() - * - **/ public function testIsMemberOf() { $this->markTestIncomplete('testIsMemberOf is not implemented yet'); } - /** - * Test for User::GetPermissionList() - * - **/ public function testGetPermissionList() { $this->markTestIncomplete('testGetPermissionList is not implemented yet'); } - /** - * Test for User::HasPermission() - * - **/ public function testHasPermission() { $this->markTestIncomplete('testHasPermission is not implemented yet'); } - /** - * Test for User::GrantPermission() - * - **/ public function testGrantPermission() { $this->markTestIncomplete('testGrantPermission is not implemented yet'); } - /** - * Test for User::RevokePermission() - * - **/ public function testRevokePermission() { $this->markTestIncomplete('testRevokePermission is not implemented yet'); } + public function testGetDefaultTimezoneIfTimezoneNotSet() + { + $defaultTz = 'UTC'; + date_default_timezone_set($defaultTz); + $user = new IcingaUser('unittest'); + $prefs = new UserPreferences(array()); + $user->setPreferences($prefs); + $this->assertEquals($user->getTimezone(), $defaultTz, + 'User\'s timezone does not match the default timezone' + ); + } + + public function testGetTimezoneIfTimezoneSet() + { + $defaultTz = 'UTC'; + $explicitTz = 'Europe/Berlin'; + date_default_timezone_set($defaultTz); + $user = new IcingaUser('unittest'); + $prefs = new UserPreferences(array( + 'timezone' => $explicitTz + )); + $user->setPreferences($prefs); + $this->assertEquals($user->getTimezone(), $explicitTz, + 'User\'s timezone does not match the timezone set by himself' + ); + } + }