parent
f7051ca992
commit
2415604035
|
@ -1,6 +1,4 @@
|
|||
<?php
|
||||
// @codingStandardsIgnoreStart
|
||||
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
/**
|
||||
* This file is part of Icinga Web 2.
|
||||
|
@ -29,10 +27,10 @@
|
|||
*/
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
use \Icinga\Application\Icinga;
|
||||
use \Icinga\Application\Config;
|
||||
use \Icinga\Util\DateTimeFactory;
|
||||
use \Icinga\Web\Form\Validator\DateTimeValidator;
|
||||
use Icinga\Application\Icinga;
|
||||
use Icinga\Application\Config;
|
||||
use Icinga\Util\DateTimeFactory;
|
||||
use Icinga\Web\Form\Validator\DateTimeValidator;
|
||||
|
||||
/**
|
||||
* Helper to format date and time. Utilizes DateTimeFactory to ensure time zone awareness
|
||||
|
@ -46,7 +44,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
*
|
||||
* @var Zend_Controller_Request_Abstract
|
||||
*/
|
||||
private $request;
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
|
@ -79,6 +77,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
*
|
||||
* @param int $timestamp
|
||||
* @param string $format
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function format($timestamp, $format)
|
||||
|
@ -89,6 +88,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
} else {
|
||||
return $timestamp;
|
||||
}
|
||||
|
||||
return $dt->format($format);
|
||||
}
|
||||
|
||||
|
@ -96,6 +96,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
* Format date according to user's format
|
||||
*
|
||||
* @param int $timestamp A unix timestamp
|
||||
*
|
||||
* @return string The formatted date string
|
||||
*/
|
||||
public function formatDate($timestamp)
|
||||
|
@ -107,6 +108,7 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
* Format time according to user's format
|
||||
*
|
||||
* @param int $timestamp A unix timestamp
|
||||
*
|
||||
* @return string The formatted time string
|
||||
*/
|
||||
public function formatTime($timestamp)
|
||||
|
@ -133,7 +135,8 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
public function getDateFormat()
|
||||
{
|
||||
return $this->request->getUser()->getPreferences()->get(
|
||||
'app.dateFormat', Config::app()->global !== null ? Config::app()->global->get('dateFormat', 'd/m/Y') : 'd/m/Y'
|
||||
'app.dateFormat',
|
||||
Config::app()->global !== null ? Config::app()->global->get('dateFormat', 'd/m/Y') : 'd/m/Y'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -145,7 +148,8 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
public function getTimeFormat()
|
||||
{
|
||||
return $this->request->getUser()->getPreferences()->get(
|
||||
'app.timeFormat', Config::app()->global !== null ? Config::app()->global->get('timeFormat', 'g:i A') : 'g:i A'
|
||||
'app.timeFormat',
|
||||
Config::app()->global !== null ? Config::app()->global->get('timeFormat', 'g:i A') : 'g:i A'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -159,5 +163,3 @@ class Zend_View_Helper_DateFormat extends Zend_View_Helper_Abstract
|
|||
return $this->getDateFormat() . ' ' . $this->getTimeFormat();
|
||||
}
|
||||
}
|
||||
|
||||
// @codingStandardsIgnoreStop
|
||||
|
|
|
@ -0,0 +1,166 @@
|
|||
<?php
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
// {{{ICINGA_LICENSE_HEADER}}}
|
||||
|
||||
namespace Tests\Icinga\Views\Helper;
|
||||
|
||||
use Mockery;
|
||||
use DateTimeZone;
|
||||
use Zend_View_Helper_DateFormat;
|
||||
use Icinga\Test\BaseTestCase;
|
||||
use Icinga\Application\Config;
|
||||
use Icinga\Util\DateTimeFactory;
|
||||
|
||||
require_once BaseTestCase::$appDir . '/views/helpers/DateFormat.php';
|
||||
|
||||
class DateFormatTest extends BaseTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->oldConfigDir = Config::$configDir;
|
||||
Config::$configDir = dirname(__FILE__) . '/DateFormatTest';
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
parent::tearDown();
|
||||
Config::$configDir = $this->oldConfigDir;
|
||||
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone(date_default_timezone_get())));
|
||||
}
|
||||
|
||||
public function testFormatReturnsCorrectDateWithTimezoneApplied()
|
||||
{
|
||||
DateTimeFactory::setConfig(array('timezone' => new DateTimeZone('Europe/Berlin')));
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
||||
|
||||
$this->assertEquals(
|
||||
'12:05',
|
||||
$helper->format(1397729100, 'H:i'),
|
||||
'Zend_View_Helper_DateFormat::format does not return a valid' .
|
||||
' formatted time or does not apply the user\'s timezone'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFormatDateReturnsCorrectDate()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d_m_y'));
|
||||
|
||||
$this->assertEquals(
|
||||
'17_04_14',
|
||||
$helper->formatDate(1397729100),
|
||||
'Zend_View_Helper_DateFormat::formatDate does not return a valid formatted date'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFormatTimeReturnsCorrectTime()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock(null, 'H:i'));
|
||||
|
||||
$this->assertEquals(
|
||||
'10:05',
|
||||
$helper->formatTime(1397729100),
|
||||
'Zend_View_Helper_DateFormat::formatTime does not return a valid formatted time'
|
||||
);
|
||||
}
|
||||
|
||||
public function testFormatDatetimeReturnsCorrectDatetime()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d m Y', 'H:i a'));
|
||||
|
||||
$this->assertEquals(
|
||||
'17 04 2014 10:05 am',
|
||||
$helper->formatDateTime(1397729100),
|
||||
'Zend_View_Helper_DateFormat::formatDateTime does not return a valid formatted date and time'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetDateFormatReturnsCorrectFormat()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d/m-y'));
|
||||
|
||||
$this->assertEquals(
|
||||
'd/m-y',
|
||||
$helper->getDateFormat(),
|
||||
'Zend_View_Helper_DateFormat::getDateFormat does not return the user\'s date format'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetTimeFormatReturnsCorrectFormat()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock(null, 'H.i A'));
|
||||
|
||||
$this->assertEquals(
|
||||
'H.i A',
|
||||
$helper->getTimeFormat(),
|
||||
'Zend_View_Helper_DateFormat::getTimeFormat does not return the user\'s time format'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetDatetimeFormatReturnsCorrectFormat()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock('d/m-y', 'H.i A'));
|
||||
|
||||
$this->assertEquals(
|
||||
'd/m-y H.i A',
|
||||
$helper->getDateTimeFormat(),
|
||||
'Zend_View_Helper_DateFormat::getDateTimeFormat does not return the user\'s date and time format'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetDateFormatReturnsFormatFromConfig()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
||||
|
||||
$this->assertEquals(
|
||||
'd-m-y',
|
||||
$helper->getDateFormat(),
|
||||
'Zend_View_Helper_DateFormat::getDateFormat does not return the format set' .
|
||||
' in the global configuration if the user\'s preferences do not provide one'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetTimeFormatReturnsFormatFromConfig()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
||||
|
||||
$this->assertEquals(
|
||||
'G:i a',
|
||||
$helper->getTimeFormat(),
|
||||
'Zend_View_Helper_DateFormat::getTimeFormat does not return the format set' .
|
||||
' in the global configuration if the user\'s preferences do not provide one'
|
||||
);
|
||||
}
|
||||
|
||||
public function testGetDatetimeFormatReturnsFormatFromConfig()
|
||||
{
|
||||
$helper = new Zend_View_Helper_DateFormat($this->getRequestMock());
|
||||
|
||||
$this->assertEquals(
|
||||
'd-m-y G:i a',
|
||||
$helper->getDateTimeFormat(),
|
||||
'Zend_View_Helper_DateFormat::getDateTimeFormat does not return the format set' .
|
||||
' in the global configuration if the user\'s preferences do not provide one'
|
||||
);
|
||||
}
|
||||
|
||||
protected function getRequestMock($dateFormat = null, $timeFormat = null)
|
||||
{
|
||||
$mock = Mockery::mock('\Zend_Controller_Request_Abstract');
|
||||
$mock->shouldReceive('getUser->getPreferences->get')
|
||||
->with(Mockery::type('string'), Mockery::type('string'))
|
||||
->andReturnUsing(
|
||||
function ($ident, $default) use ($dateFormat, $timeFormat) {
|
||||
if ($dateFormat !== null && $ident === 'app.dateFormat') {
|
||||
return $dateFormat;
|
||||
} elseif ($timeFormat !== null && $ident === 'app.timeFormat') {
|
||||
return $timeFormat;
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
return $mock;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
[global]
|
||||
dateFormat = "d-m-y"
|
||||
timeFormat = "G:i a"
|
Loading…
Reference in New Issue