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' + ); + } + }