Commit 152b14b2 authored by Oliver Hader's avatar Oliver Hader Committed by Oliver Hader
Browse files

[BUGFIX] Fix PHP warning with date function in FormEngine

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/33665


Reviewed-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader's avatarOliver Hader <oliver.hader@typo3.org>
parent 79ba8820
......@@ -3088,16 +3088,24 @@ class t3lib_TCEforms {
$itemValue = $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');
......@@ -3117,6 +3125,8 @@ class t3lib_TCEforms {
$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 = t3lib_div::formatSize(intval($itemValue));
if ($config['format.']['appendByteSize']) {
$value .= ' (' . $itemValue . ')';
......@@ -3136,7 +3146,7 @@ class t3lib_TCEforms {
}
break;
default:
break;
// Do nothing e.g. when $format === ''
}
return $itemValue;
......
<?php
/**
* 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!
*/
/**
* Test case for the t3lib_TCEforms class in the TYPO3 core.
*
* @package TYPO3
* @subpackage t3lib
*/
class t3lib_TCEformsTest extends tx_phpunit_testcase {
/**
* Enable backup of global and system variables
*
* @var boolean
*/
protected $backupGlobals = TRUE;
/**
* @var t3lib_TCEforms|PHPUnit_Framework_MockObject_MockObject
*/
protected $subject;
/**
* Sets up this test case.
*/
protected function setUp() {
$this->subject = $this->getMock('t3lib_TCEforms', 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);
}
}
?>
\ No newline at end of file
Supports Markdown
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