[BUGFIX] Fix PHP warning with date function in FormEngine 61/33661/2
authorWouter Wolters <typo3@wouterwolters.nl>
Fri, 24 Oct 2014 09:49:50 +0000 (11:49 +0200)
committerOliver Hader <oliver.hader@typo3.org>
Fri, 31 Oct 2014 09:02:00 +0000 (10:02 +0100)
The timestamp handed to the date function must be of type int
otherwise a warning is issued. Cast the value to int before
passing it to the date function. The important scenarios are
when the timestamp is "0" or "" (blank string).

Add unit test for formatValue function and the possible format
configurations.

Resolves: #62032
Releases: master, 6.2, 6.1, 4.5
Change-Id: I5207ef5b562dd70b9b5e574eef1b9ee59fa836f0
Reviewed-on: http://review.typo3.org/33661
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
typo3/sysext/backend/Classes/Form/FormEngine.php
typo3/sysext/backend/Tests/Unit/Form/FormEngineTest.php [new file with mode: 0644]

index c22db9b..0068945 100644 (file)
@@ -3580,24 +3580,32 @@ TBE_EDITOR.customEvalFunctions[\'' . $evalData . '\'] = function(value) {
                                break;
                        case 'datetime':
                                // compatibility with "eval" (type "input")
-                               $itemValue = date('H:i d-m-Y', $itemValue);
+                               if ($itemValue !== '') {
+                                       $itemValue = date('H:i d-m-Y', (int)$itemValue);
+                               }
                                break;
                        case 'time':
                                // compatibility with "eval" (type "input")
-                               $itemValue = date('H:i', $itemValue);
+                               if ($itemValue !== '') {
+                                       $itemValue = date('H:i', (int)$itemValue);
+                               }
                                break;
                        case 'timesec':
                                // compatibility with "eval" (type "input")
-                               $itemValue = date('H:i:s', $itemValue);
+                               if ($itemValue !== '') {
+                                       $itemValue = date('H:i:s', (int)$itemValue);
+                               }
                                break;
                        case 'year':
                                // compatibility with "eval" (type "input")
-                               $itemValue = date('Y', $itemValue);
+                               if ($itemValue !== '') {
+                                       $itemValue = date('Y', (int)$itemValue);
+                               }
                                break;
                        case 'int':
                                $baseArr = array('dec' => 'd', 'hex' => 'x', 'HEX' => 'X', 'oct' => 'o', 'bin' => 'b');
                                $base = trim($config['format.']['base']);
-                               $format = $baseArr[$base] ? $baseArr[$base] : 'd';
+                               $format = $baseArr[$base] ?: 'd';
                                $itemValue = sprintf('%' . $format, $itemValue);
                                break;
                        case 'float':
@@ -3612,6 +3620,8 @@ TBE_EDITOR.customEvalFunctions[\'' . $evalData . '\'] = function(value) {
                                $itemValue = md5($itemValue);
                                break;
                        case 'filesize':
+                               // We need to cast to int here, otherwise empty values result in empty output,
+                               // but we expect zero.
                                $value = GeneralUtility::formatSize((int)$itemValue);
                                if ($config['format.']['appendByteSize']) {
                                        $value .= ' (' . $itemValue . ')';
@@ -3631,7 +3641,7 @@ TBE_EDITOR.customEvalFunctions[\'' . $evalData . '\'] = function(value) {
                                }
                                break;
                        default:
-                               // Do nothing
+                               // Do nothing e.g. when $format === ''
                }
                return $itemValue;
        }
diff --git a/typo3/sysext/backend/Tests/Unit/Form/FormEngineTest.php b/typo3/sysext/backend/Tests/Unit/Form/FormEngineTest.php
new file mode 100644 (file)
index 0000000..b6a953c
--- /dev/null
@@ -0,0 +1,261 @@
+<?php
+namespace TYPO3\CMS\Backend\Tests\Unit\Form;
+
+/**
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Core\Tests\UnitTestCase;
+
+/**
+ * Test case
+ */
+class FormEngineTest extends UnitTestCase {
+
+       /**
+        * @var \TYPO3\CMS\Backend\Form\FormEngine|\PHPUnit_Framework_MockObject_MockObject
+        */
+       protected $subject;
+
+       /**
+        * Sets up this test case.
+        */
+       protected function setUp() {
+               $this->subject = $this->getMock('TYPO3\\CMS\\Backend\\Form\\FormEngine', array('dummy'), array(), '', FALSE);
+       }
+
+       /**
+        * @return array
+        */
+       public function formatValueDataProvider() {
+               return array(
+                       'format with empty format configuration' => array(
+                               array(
+                                       'format' => '',
+                               ),
+                               '',
+                               '',
+                       ),
+                       'format to date' => array(
+                               array(
+                                       'format' => 'date',
+                               ),
+                               '1412358894',
+                               '03-10-2014'
+                       ),
+                       'format to date with empty timestamp' => array(
+                               array(
+                                       'format' => 'date',
+                               ),
+                               '0',
+                               ''
+                       ),
+                       'format to date with blank timestamp' => array(
+                               array(
+                                       'format' => 'date',
+                               ),
+                               '',
+                               ''
+                       ),
+                       'format to date with option strftime' => array(
+                               array(
+                                       'format' => 'date',
+                                       'format.' => array(
+                                               'option' => '%d-%m',
+                                               'strftime' => TRUE,
+                                       ),
+                               ),
+                               '1412358894',
+                               '03-10'
+                       ),
+                       'format to date with option' => array(
+                               array(
+                                       'format' => 'date',
+                                       'format.' => array(
+                                               'option' => 'd-m',
+                                       ),
+                               ),
+                               '1412358894',
+                               '03-10'
+                       ),
+                       'format to datetime' => array(
+                               array(
+                                       'format' => 'datetime',
+                               ),
+                               '1412358894',
+                               '17:54 03-10-2014'
+                       ),
+                       'format to datetime with empty value' => array(
+                               array(
+                                       'format' => 'datetime',
+                               ),
+                               '',
+                               ''
+                       ),
+                       'format to time' => array(
+                               array(
+                                       'format' => 'time',
+                               ),
+                               '1412358894',
+                               '17:54'
+                       ),
+                       'format to time with empty value' => array(
+                               array(
+                                       'format' => 'time',
+                               ),
+                               '',
+                               ''
+                       ),
+                       'format to timesec' => array(
+                               array(
+                                       'format' => 'timesec',
+                               ),
+                               '1412358894',
+                               '17:54:54'
+                       ),
+                       'format to timesec with empty value' => array(
+                               array(
+                                       'format' => 'timesec',
+                               ),
+                               '',
+                               ''
+                       ),
+                       'format to year' => array(
+                               array(
+                                       'format' => 'year',
+                               ),
+                               '1412358894',
+                               '2014'
+                       ),
+                       'format to int' => array(
+                               array(
+                                       'format' => 'int',
+                               ),
+                               '123.00',
+                               '123'
+                       ),
+                       'format to int with base' => array(
+                               array(
+                                       'format' => 'int',
+                                       'format.' => array(
+                                               'base' => 'oct',
+                                       ),
+                               ),
+                               '123',
+                               '173'
+                       ),
+                       'format to int with empty value' => array(
+                               array(
+                                       'format' => 'int',
+                               ),
+                               '',
+                               '0'
+                       ),
+                       'format to float' => array(
+                               array(
+                                       'format' => 'float',
+                               ),
+                               '123',
+                               '123.00'
+                       ),
+                       'format to float with precision' => array(
+                               array(
+                                       'format' => 'float',
+                                       'format.' => array(
+                                               'precision' => '4',
+                                       ),
+                               ),
+                               '123',
+                               '123.0000'
+                       ),
+                       'format to float with empty value' => array(
+                               array(
+                                       'format' => 'float',
+                               ),
+                               '',
+                               '0.00'
+                       ),
+                       'format to number' => array(
+                               array(
+                                       'format' => 'number',
+                                       'format.' => array(
+                                               'option' => 'b',
+                                       ),
+                               ),
+                               '123',
+                               '1111011'
+                       ),
+                       'format to number with empty option' => array(
+                               array(
+                                       'format' => 'number',
+                               ),
+                               '123',
+                               ''
+                       ),
+                       'format to md5' => array(
+                               array(
+                                       'format' => 'md5',
+                               ),
+                               'joh316',
+                               'bacb98acf97e0b6112b1d1b650b84971'
+                       ),
+                       'format to md5 with empty value' => array(
+                               array(
+                                       'format' => 'md5',
+                               ),
+                               '',
+                               'd41d8cd98f00b204e9800998ecf8427e'
+                       ),
+                       'format to filesize' => array(
+                               array(
+                                       'format' => 'filesize',
+                               ),
+                               '100000',
+                               '98 K'
+                       ),
+                       'format to filesize with empty value' => array(
+                               array(
+                                       'format' => 'filesize',
+                               ),
+                               '',
+                               '0 '
+                       ),
+                       'format to filesize with option appendByteSize' => array(
+                               array(
+                                       'format' => 'filesize',
+                                       'format.' => array(
+                                               'appendByteSize' => TRUE,
+                                       ),
+                               ),
+                               '100000',
+                               '98 K (100000)'
+                       ),
+               );
+       }
+
+       /**
+        * @param array $config
+        * @param string $itemValue
+        * @param string $expectedResult
+        * @dataProvider formatValueDataProvider
+        * @test
+        */
+       public function formatValueWithGivenConfiguration($config, $itemValue, $expectedResult) {
+               $timezoneBackup = date_default_timezone_get();
+               date_default_timezone_set('UTC');
+               $result = $this->subject->formatValue($config, $itemValue);
+               date_default_timezone_set($timezoneBackup);
+
+               $this->assertEquals($expectedResult, $result);
+       }
+
+}