[FEATURE] Add stdWrap strtotime 29/35029/12
authorMathias Brodala <mbrodala@pagemachine.de>
Thu, 4 Dec 2014 11:32:03 +0000 (12:32 +0100)
committerAndreas Fernandez <andreas.fernandez@aspedia.de>
Tue, 28 Apr 2015 11:38:02 +0000 (13:38 +0200)
This allows for conversion of formatted dates to timestamp and
complements the existing date/strftime properties.

Resolves: #63561
Releases: master
Change-Id: I4e41cabc70979da5746f5b119ab2096bdd9e0c6e
Reviewed-on: http://review.typo3.org/35029
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Reviewed-by: Jo Hasenau <info@cybercraft.de>
Tested-by: Jo Hasenau <info@cybercraft.de>
Reviewed-by: Andreas Fernandez <andreas.fernandez@aspedia.de>
Tested-by: Andreas Fernandez <andreas.fernandez@aspedia.de>
typo3/sysext/core/Documentation/Changelog/master/Feature-63561-AddTypoScriptStdWrapStrtotime.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-63561-AddTypoScriptStdWrapStrtotime.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-63561-AddTypoScriptStdWrapStrtotime.rst
new file mode 100644 (file)
index 0000000..4337f8b
--- /dev/null
@@ -0,0 +1,36 @@
+==================================================
+Feature - #63561: Add TypoScript stdWrap strtotime
+==================================================
+
+Description
+===========
+
+A new TypoScript property ``strtotime`` is now available within ``stdWrap`` which allows for conversion of formatted dates to timestamp, e.g. to perform date calculations.
+
+Possible values are ``1`` or any time string valid as first argument of the PHP ``strtotime()`` function.
+
+Basic usage to convert date string to timestamp:
+
+.. code-block:: typoscript
+
+       date_as_timestamp = TEXT
+       date_as_timestamp {
+               value = 2015-04-15
+               strtotime = 1
+       }
+
+Convert incoming date string to timestamp, perform date calculation and output as date string again:
+
+.. code-block:: typoscript
+
+       next_weekday = TEXT
+       next_weekday {
+               data = GP:selected_date
+               strtotime = + 2 weekdays
+               strftime = %Y-%m-%d
+       }
+
+Impact
+======
+
+The new property is available everywhere in TypoScript where ``stdWrap`` is applied.
index 8498b2d..2543352 100644 (file)
@@ -146,6 +146,8 @@ class ContentObjectRenderer {
                'expandList.' => 'array',
                'date' => 'dateconf',
                'date.' => 'array',
+               'strtotime' => 'strtotimeconf',
+               'strtotime.' => 'array',
                'strftime' => 'strftimeconf',
                'strftime.' => 'array',
                'age' => 'boolean',
@@ -2937,6 +2939,21 @@ class ContentObjectRenderer {
        }
 
        /**
+        * strtotime
+        * Will return a timestamp based on configuration given according to PHP strtotime
+        *
+        * @param string $content Input value undergoing processing in this function.
+        * @param array $conf stdWrap properties for strtotime.
+        * @return string The processed input value
+        */
+       public function stdWrap_strtotime($content = '', $conf = array()) {
+               if ($conf['strtotime'] !== '1') {
+                       $content .= ' ' . $conf['strtotime'];
+               }
+               return strtotime($content, $GLOBALS['EXEC_TIME']);
+       }
+
+       /**
         * age
         * Will return the age of a given timestamp based on configuration given by stdWrap properties
         *
index 0b3f09e..70f6be3 100644 (file)
@@ -1508,6 +1508,82 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        }
 
        /**
+        * Data provider for the stdWrap_strtotime test
+        *
+        * @return array
+        * @see stdWrap_strtotime
+        */
+       public function stdWrap_strtotimeReturnsTimestampDataProvider() {
+               return array(
+                       'date from content' => array(
+                               '2014-12-04',
+                               array(
+                                       'strtotime' => '1',
+                               ),
+                               1417651200,
+                       ),
+                       'manipulation of date from content' => array(
+                               '2014-12-04',
+                               array(
+                                       'strtotime' => '+ 2 weekdays',
+                               ),
+                               1417996800,
+                       ),
+                       'date from configuration' => array(
+                               '',
+                               array(
+                                       'strtotime' => '2014-12-04',
+                               ),
+                               1417651200,
+                       ),
+                       'manipulation of date from configuration' => array(
+                               '',
+                               array(
+                                       'strtotime' => '2014-12-04 + 2 weekdays',
+                               ),
+                               1417996800,
+                       ),
+                       'empty input' => array(
+                               '',
+                               array(
+                                       'strtotime' => '1',
+                               ),
+                               FALSE,
+                       ),
+                       'date from content and configuration' => array(
+                               '2014-12-04',
+                               array(
+                                       'strtotime' => '2014-12-05',
+                               ),
+                               FALSE,
+                       ),
+               );
+       }
+
+       /**
+        * @param string|NULL $content
+        * @param array $configuration
+        * @param integer $expected
+        * @dataProvider stdWrap_strtotimeReturnsTimestampDataProvider
+        * @test
+        */
+       public function stdWrap_strtotimeReturnsTimestamp($content, $configuration, $expected) {
+               // Set exec_time to a hard timestamp
+               $GLOBALS['EXEC_TIME'] = 1417392000;
+               // Save current timezone and set to UTC to make the system under test behave
+               // the same in all server timezone settings
+               $timezoneBackup = date_default_timezone_get();
+               date_default_timezone_set('UTC');
+
+               $result = $this->subject->stdWrap_strtotime($content, $configuration);
+
+               // Reset timezone
+               date_default_timezone_set($timezoneBackup);
+
+               $this->assertEquals($expected, $result);
+       }
+
+       /**
         * @param string|NULL $content
         * @param array $configuration
         * @param string $expected