Commit 00953101 authored by Tino Goratsch's avatar Tino Goratsch

reworked the DateRange helper class to return much more meaningful output and covered it with tests

parent e0701b62
......@@ -34,7 +34,7 @@ class DateRange
}
/**
* Gibt die Formularfelder f�r den Ver�ffentlichungszeitraum aus
* Formats a given single date or date range into the desired format
*
* @param string $start
* @param string $end
......@@ -44,16 +44,27 @@ class DateRange
*/
public function formatTimeRange($start, $end = '', $format = 'long')
{
$rfcStart = $this->date->format($start, 'c');
if ($end === '' || $start >= $end) {
if ($end === '') {
$title = $this->date->format($start, $format);
} else {
$title = sprintf($this->lang->t('system', 'date_published_since'), $this->date->format($start, $format));
}
return '<time datetime="' . $start . '" title="' . $title . '">' . $this->date->format($start, Date::DEFAULT_DATE_FORMAT_LONG) . '</time>';
return '<time datetime="' . $rfcStart . '" title="' . $title . '">' . $this->date->format($start, $format) . '</time>';
} else {
$title = sprintf($this->lang->t('system', 'date_time_range'), $this->date->format($start, $format), $this->date->format($end, $format));
return '<time datetime="' . $start . '/' . $end . '" title="' . $title . '">' . $this->date->format($start, Date::DEFAULT_DATE_FORMAT_LONG) . '&ndash;' . $this->date->format($end, $datetimeFormat) . '</time>';
$rfcEnd = $this->date->format($end, 'c');
$dateRange = '<time datetime="' . $rfcStart . '">';
$dateRange.= $this->date->format($start, $format);
$dateRange.= '</time>';
$dateRange.= '&ndash;';
$dateRange.= '<time datetime="' . $rfcEnd . '">';
$dateRange.= $this->date->format($end, $format);
$dateRange.= '</time>';
return $dateRange;
}
}
......
<?php
class DateRangeTest extends PHPUnit_Framework_TestCase
{
/**
* @var \ACP3\Core\Config|PHPUnit_Framework_MockObject_MockObject
*/
private $configMock;
/**
* @var \ACP3\Core\Date
*/
private $date;
/**
* @var \ACP3\Core\Lang|\PHPUnit_Framework_MockObject_MockObject
*/
private $langMock;
/**
* @var \ACP3\Core\User|PHPUnit_Framework_MockObject_MockObject
*/
private $userMock;
/**
* @var \ACP3\Core\Helpers\Formatter\DateRange
*/
private $dateRange;
protected function setUp()
{
$this->langMock = $this->getMockBuilder(\ACP3\Core\Lang::class)
->disableOriginalConstructor()
->setMethods(['t'])
->getMock();
$this->userMock = $this->getMockBuilder(\ACP3\Core\User::class)
->disableOriginalConstructor()
->getMock();
$this->configMock = $this->getMockBuilder(\ACP3\Core\Config::class)
->disableOriginalConstructor()
->getMock();
$this->userMock->expects($this->once())
->method('getUserInfo')
->willReturn([
'date_format_long' => 'Y-m-d H:i',
'date_format_short' => 'Y-m-d',
'time_zone' => 'Europe/Berlin',
]);
$this->date = new \ACP3\Core\Date(
$this->userMock,
$this->langMock,
$this->configMock
);
$this->dateRange = new \ACP3\Core\Helpers\Formatter\DateRange(
$this->date,
$this->langMock
);
}
/**
* @param string $langKey
* @param string $langValue
*/
private function setUpLangMockExpectation($langKey, $langValue)
{
$this->langMock->expects($this->once())
->method('t')
->with('system', $langKey)
->willReturn($langValue);
}
public function testSingleDateWithLongFormat()
{
$dateString = '2012-12-20 12:12:12';
$expected = '<time datetime="2012-12-20T13:12:12+01:00" title="2012-12-20 13:12">2012-12-20 13:12</time>';
$this->assertEquals($expected, $this->dateRange->formatTimeRange($dateString));
}
public function testSingleDateWithShortFormat()
{
$dateString = '2012-12-20 12:12:12';
$expected = '<time datetime="2012-12-20T13:12:12+01:00" title="2012-12-20">2012-12-20</time>';
$this->assertEquals($expected, $this->dateRange->formatTimeRange($dateString, '', 'short'));
}
public function testDateRangeWithLongFormat()
{
$dateStart = '2012-12-20 12:12:12';
$dateEnd = '2012-12-25 12:12:12';
$expected = '<time datetime="2012-12-20T13:12:12+01:00">2012-12-20 13:12</time>&ndash;<time datetime="2012-12-25T13:12:12+01:00">2012-12-25 13:12</time>';
$this->assertEquals($expected, $this->dateRange->formatTimeRange($dateStart, $dateEnd));
}
public function testInvalidDateRangeWithLongFormat()
{
$this->setUpLangMockExpectation('date_published_since', 'Published since %s');
$dateStart = '2012-12-20 12:12:12';
$dateEnd = '2012-12-19 12:12:12';
$expected = '<time datetime="2012-12-20T13:12:12+01:00" title="Published since 2012-12-20 13:12">2012-12-20 13:12</time>';
$this->assertEquals($expected, $this->dateRange->formatTimeRange($dateStart, $dateEnd));
}
public function testInvalidDateRangeWithShortFormat()
{
$this->setUpLangMockExpectation('date_published_since', 'Published since %s');
$dateStart = '2012-12-20 12:12:12';
$dateEnd = '2012-12-19 12:12:12';
$expected = '<time datetime="2012-12-20T13:12:12+01:00" title="Published since 2012-12-20">2012-12-20</time>';
$this->assertEquals($expected, $this->dateRange->formatTimeRange($dateStart, $dateEnd, 'short'));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment