[BUGFIX] Fix rendering with empty date 67/43367/6
authorOliver Thiele <mail@oliver-thiele.de>
Tue, 15 Sep 2015 11:57:36 +0000 (13:57 +0200)
committerNicole Cordes <typo3@cordes.co>
Wed, 16 Sep 2015 12:57:36 +0000 (14:57 +0200)
Resolves: #69858
Releases: master
Change-Id: I02ffe4fcd53f1a956e73bc39f043bdcb6e0a9d9e
Reviewed-on: http://review.typo3.org/43367
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Jan Helke <typo3@helke.de>
Tested-by: Jan Helke <typo3@helke.de>
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Oliver Thiele <mail@oliver-thiele.de>
Tested-by: Oliver Thiele <mail@oliver-thiele.de>
typo3/sysext/fluid/Classes/ViewHelpers/Format/DateViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Format/DateViewHelperTest.php

index 309e8ed..ec72633 100644 (file)
@@ -122,6 +122,7 @@ class DateViewHelper extends AbstractViewHelper implements CompilableInterface {
                $date = $arguments['date'];
                $format = $arguments['format'];
                $base = $arguments['base'] === NULL ? time() : $arguments['base'];
+
                if ($format === '') {
                        $format = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
                }
@@ -132,6 +133,11 @@ class DateViewHelper extends AbstractViewHelper implements CompilableInterface {
                                return '';
                        }
                }
+
+               if ($date === '') {
+                       $date = 'now';
+               }
+
                if (!$date instanceof \DateTime) {
                        try {
                                $base = $base instanceof \DateTime ? $base->format('U') : strtotime((MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
@@ -139,7 +145,7 @@ class DateViewHelper extends AbstractViewHelper implements CompilableInterface {
                                $date = new \DateTime('@' . $dateTimestamp);
                                $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
                        } catch (\Exception $exception) {
-                               throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor.', 1241722579);
+                               throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor: ' . $exception->getMessage(), 1241722579);
                        }
                }
 
index 7fa95cb..968ffeb 100644 (file)
@@ -86,7 +86,7 @@ class DateViewHelperTest extends UnitTestCase {
        /**
         * @test
         */
-       public function viewHelperReturnsEmptyStringIfNULLIsGiven() {
+       public function viewHelperReturnsEmptyStringIfChildrenIsNULL() {
                $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
                $actualResult = $this->subject->render();
                $this->assertEquals('', $actualResult);
@@ -95,6 +95,25 @@ class DateViewHelperTest extends UnitTestCase {
        /**
         * @test
         */
+       public function viewHelperReturnsCurrentDateIfEmptyStringIsGiven() {
+               $actualResult = $this->subject->render('');
+               $expectedResult = (new \DateTime())->format('Y-m-d');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
+       public function viewHelperReturnsCurrentDateIfChildrenIsEmptyString() {
+               $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
+               $actualResult = $this->subject->render();
+               $expectedResult = (new \DateTime())->format('Y-m-d');
+               $this->assertEquals($expectedResult, $actualResult);
+       }
+
+       /**
+        * @test
+        */
        public function viewHelperUsesDefaultIfNoSystemFormatIsAvailable() {
                $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
                $actualResult = $this->subject->render('@1391876733');
@@ -113,8 +132,9 @@ class DateViewHelperTest extends UnitTestCase {
        /**
         * @test
         * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
+        * @expectedExceptionMessageRegExp /"foo" could not be parsed by \\DateTime constructor: .* Unexpected character$/
         */
-       public function viewHelperThrowsExceptionIfDateStringCantBeParsed() {
+       public function viewHelperThrowsExceptionWithOriginalMessageIfDateStringCantBeParsed() {
                $this->subject->render('foo');
        }