Test TimezoneDetect

refs #2716
This commit is contained in:
Alexander A. Klimov 2017-02-10 15:42:02 +01:00
parent e5f462c2fa
commit 1e58e15ac7
3 changed files with 47 additions and 4 deletions

View File

@ -344,7 +344,7 @@ abstract class ApplicationBootstrap
*/
public function setupAutoloader()
{
require $this->libDir . '/Icinga/Application/ClassLoader.php';
require_once $this->libDir . '/Icinga/Application/ClassLoader.php';
$this->loader = new ClassLoader();
$this->loader->registerNamespace('Icinga', $this->libDir . '/Icinga');

View File

@ -3,8 +3,6 @@
namespace Icinga\Util;
use Icinga\Application\Platform;
/**
* Retrieve timezone information from cookie
*/
@ -52,7 +50,7 @@ class TimezoneDetect
return;
}
if (Platform::isCli() === false && array_key_exists(self::$cookieName, $_COOKIE)) {
if (array_key_exists(self::$cookieName, $_COOKIE)) {
$matches = array();
if (preg_match('/\A(-?\d+)[\-,](\d+)\z/', $_COOKIE[self::$cookieName], $matches)) {
$offset = $matches[1];

View File

@ -0,0 +1,45 @@
<?php
/* Icinga Web 2 | (c) 2017 Icinga Development Team | GPLv2+ */
namespace Tests\Icinga\Util;
use Icinga\Test\BaseTestCase;
use Icinga\Util\TimezoneDetect;
class TimezoneDetectTest extends BaseTestCase
{
public function testPositiveTimezoneOffsetSeparatedByComma()
{
$this->assertTimezoneDetection('3600,0', 'Europe/Paris');
}
public function testPositiveTimezoneOffsetSeparatedByHyphen()
{
$this->assertTimezoneDetection('3600-0', 'Europe/Paris');
}
public function testNegativeTimezoneOffsetSeparatedByComma()
{
$this->assertTimezoneDetection('-3600,0', 'Atlantic/Azores');
}
public function testNegativeTimezoneOffsetSeparatedByHyphen()
{
$this->assertTimezoneDetection('-3600-0', 'Atlantic/Azores');
}
protected function assertTimezoneDetection($cookieValue, $expectedTimezoneName)
{
$tzDetect = new TimezoneDetect();
$tzDetect->reset();
$_COOKIE[TimezoneDetect::$cookieName] = $cookieValue;
$tzDetect = new TimezoneDetect();
$this->assertSame(
$tzDetect->getTimezoneName(),
$expectedTimezoneName,
'Failed asserting that the timezone "' . $expectedTimezoneName
. '" is being detected from the cookie value "' . $cookieValue . '"'
);
}
}