[BUGFIX] Correctly parse timestamps in format.date VH 23/34323/2
authorMarkus Klein <klein.t3@reelworx.at>
Thu, 13 Nov 2014 19:18:17 +0000 (20:18 +0100)
committerWouter Wolters <typo3@wouterwolters.nl>
Wed, 19 Nov 2014 18:25:42 +0000 (19:25 +0100)
If the timestamp is retrieved from child nodes, the date format
ViewHelper has to ensure the timestamp is properly recognized.
As child nodes are always returning a string, the former check with
is_integer() would never return TRUE.
Therefore adapt this check to use the MathUtility method as this
guarantees a proper check.

Resolves: #62932
Releases: master, 6.2
Change-Id: I6968991bf97f99f82fc942d7d17678c9b473e796
Reviewed-on: http://review.typo3.org/34323
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
typo3/sysext/fluid/Classes/ViewHelpers/Format/DateViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/DateViewHelperTest.php

index 9141065..c8affce 100644 (file)
@@ -10,6 +10,7 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
+use TYPO3\CMS\Core\Utility\MathUtility;
 
 /**
  * Formats a \DateTime object.
@@ -40,14 +41,6 @@ namespace TYPO3\CMS\Fluid\ViewHelpers\Format;
  * (depending on the current time, see http://www.php.net/manual/en/function.strtotime.php)
  * </output>
  *
- * <code title="output date from unix timestamp">
- * <f:format.date format="d.m.Y - H:i:s">@{someTimestamp}</f:format.date>
- * </code>
- * <output>
- * 13.12.1980 - 21:03:42
- * (depending on the current time. Don't forget the "@" in front of the timestamp see http://www.php.net/manual/en/function.strtotime.php)
- * </output>
- *
  * <code title="Localized dates using strftime date format">
  * <f:format.date format="%d. %B %Y">{dateObject}</f:format.date>
  * </code>
@@ -103,7 +96,7 @@ class DateViewHelper extends \TYPO3\CMS\Fluid\Core\ViewHelper\AbstractViewHelper
                }
                if (!$date instanceof \DateTime) {
                        try {
-                               if (is_integer($date)) {
+                               if (MathUtility::canBeInterpretedAsInteger($date)) {
                                        $date = new \DateTime('@' . $date);
                                } else {
                                        $date = new \DateTime($date);
index 4e288cf..9ddd7a7 100644 (file)
@@ -10,6 +10,7 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
  *                                                                        *
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
+use TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper;
 
 /**
  * Test case
@@ -51,7 +52,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperFormatsDateCorrectly() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
+               $viewHelper = new DateViewHelper();
                $actualResult = $viewHelper->render(new \DateTime('1980-12-13'));
                $this->assertEquals('1980-12-13', $actualResult);
        }
@@ -60,7 +61,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperFormatsDateStringCorrectly() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
+               $viewHelper = new DateViewHelper();
                $actualResult = $viewHelper->render('1980-12-13');
                $this->assertEquals('1980-12-13', $actualResult);
        }
@@ -69,7 +70,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRespectsCustomFormat() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
+               $viewHelper = new DateViewHelper();
                $actualResult = $viewHelper->render(new \DateTime('1980-02-01'), 'd.m.Y');
                $this->assertEquals('01.02.1980', $actualResult);
        }
@@ -78,6 +79,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperReturnsEmptyStringIfNULLIsGiven() {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
                $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
                $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
                $actualResult = $viewHelper->render();
@@ -89,7 +91,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function viewHelperUsesDefaultIfNoSystemFormatIsAvailable() {
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
+               $viewHelper = new DateViewHelper();
                $actualResult = $viewHelper->render('@1391876733');
                $this->assertEquals('2014-02-08', $actualResult);
        }
@@ -99,7 +101,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         */
        public function viewHelperUsesSystemFormat() {
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
+               $viewHelper = new DateViewHelper();
                $actualResult = $viewHelper->render('@1391876733');
                $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
        }
@@ -109,7 +111,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
         */
        public function viewHelperThrowsExceptionIfDateStringCantBeParsed() {
-               $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
+               $viewHelper = new DateViewHelper();
                $viewHelper->render('foo');
        }
 
@@ -117,6 +119,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperUsesChildNodesIfDateAttributeIsNotSpecified() {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
                $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
                $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
                $actualResult = $viewHelper->render();
@@ -126,7 +129,19 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        /**
         * @test
         */
+       public function viewHelperUsesChildNodesWithTimestamp() {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
+               $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
+               $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('1359891658'));
+               $actualResult = $viewHelper->render();
+               $this->assertEquals('2013-02-03', $actualResult);
+       }
+
+       /**
+        * @test
+        */
        public function dateArgumentHasPriorityOverChildNodes() {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
                $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
                $viewHelper->expects($this->never())->method('renderChildren');
                $actualResult = $viewHelper->render('1980-12-12');
@@ -156,6 +171,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @dataProvider viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider
         */
        public function viewHelperRespectsDefaultTimezoneForIntegerTimestamp($timezone, $expected) {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
                $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
 
                $date = 1359891658; // 2013-02-03 11:40 UTC
@@ -201,6 +217,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected) {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
                $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
                $format = 'Y-m-d H:i';
 
@@ -232,6 +249,7 @@ class DateViewHelperTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function dateViewHelperFormatsDateLocalized($locale, $expected) {
+               /** @var DateViewHelper|\PHPUnit_Framework_MockObject_MockObject $viewHelper */
                $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
                $format = '%d. %B %Y';
                // 2013-02-03 11:40 UTC