Commit 2d04af91 authored by Nikita Hovratov's avatar Nikita Hovratov Committed by Benni Mack
Browse files

[BUGFIX] Skip range validation if input value and default are both "0"

This patch fixes a regression originally introduced in #94103. The
problem there was simply an erroneous strict comparison, which further
got completely removed in #94527 due to a misconception.

The mentioned condition is important for date type fields with a lower
range set in TCA. If the user doesn't provide a value for that date
field, the range validation should be skipped. Otherwise this empty
value would always be interpreted as 0, resulting the validation to
change it to the lower range value.

As this is a special case, the condition was extracted and rewritten,
to only apply for this exact scenario. Other than that the validation
should not be skipped.

An acceptance test and a unit test are added to ensure this behavior
can't break anymore.

Resolves: #94621
Related: #94103
Related: #94527
Releases: master, 10.4
Change-Id: I54c1a815077e48064f9f6eeba9584184c5f760d7
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/70060

Tested-by: core-ci's avatarcore-ci <typo3@b13.com>
Tested-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Tested-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn's avatarChristian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack's avatarBenni Mack <benni@typo3.org>
parent fdfed0d5
...@@ -1741,8 +1741,15 @@ class DataHandler implements LoggerAwareInterface ...@@ -1741,8 +1741,15 @@ class DataHandler implements LoggerAwareInterface
} }
} }
// Skip range validation, if the default value equals 0 and the input value is 0, "0" or an empty string.
// This is needed for timestamp date fields with ['range']['lower'] set.
$skipRangeValidation =
isset($tcaFieldConf['default'])
&& (int)$tcaFieldConf['default'] === 0
&& ($res['value'] === '' || $res['value'] === '0' || $res['value'] === 0);
// Checking range of value: // Checking range of value:
if (isset($tcaFieldConf['range']) && is_array($tcaFieldConf['range'])) { if (!$skipRangeValidation && isset($tcaFieldConf['range']) && is_array($tcaFieldConf['range'])) {
if (isset($tcaFieldConf['range']['upper']) && ceil($res['value']) > (int)$tcaFieldConf['range']['upper']) { if (isset($tcaFieldConf['range']['upper']) && ceil($res['value']) > (int)$tcaFieldConf['range']['upper']) {
$res['value'] = (int)$tcaFieldConf['range']['upper']; $res['value'] = (int)$tcaFieldConf['range']['upper'];
} }
......
...@@ -122,6 +122,14 @@ class ElementsBasicInputDateCest extends AbstractElementsBasicCest ...@@ -122,6 +122,14 @@ class ElementsBasicInputDateCest extends AbstractElementsBasicCest
'expectedValueAfterSave' => '2016-02-29T05:23:00+00:00', 'expectedValueAfterSave' => '2016-02-29T05:23:00+00:00',
'comment' => 'inputdatetime_3 eval=datetime Check valid leap year input', 'comment' => 'inputdatetime_3 eval=datetime Check valid leap year input',
], ],
[
'label' => 'inputdatetime_11',
'inputValue' => '',
'expectedValue' => '',
'expectedInternalValue' => '',
'expectedValueAfterSave' => '',
'comment' => 'inputdatetime_11 eval=datetime range.lower=1627208536 Check range validation is ignored',
],
]; ];
} }
...@@ -170,31 +178,30 @@ class ElementsBasicInputDateCest extends AbstractElementsBasicCest ...@@ -170,31 +178,30 @@ class ElementsBasicInputDateCest extends AbstractElementsBasicCest
protected function dbTypeDateEvalTimeDataProvider_DbTypeDateTime() protected function dbTypeDateEvalTimeDataProvider_DbTypeDateTime()
{ {
return [ return [
[
[ 'label' => 'inputdatetime_4',
'label' => 'inputdatetime_4', 'inputValue' => '05:23 29-01-2016',
'inputValue' => '05:23 29-01-2016', 'expectedValue' => '05:23 29-01-2016',
'expectedValue' => '05:23 29-01-2016', 'expectedInternalValue' => '2016-01-29T05:23:00Z',
'expectedInternalValue' => '2016-01-29T05:23:00Z', 'expectedValueAfterSave' => '2016-01-29T05:23:00+00:00',
'expectedValueAfterSave' => '2016-01-29T05:23:00+00:00', 'comment' => 'inputdatetime_4 dbType=datetime eval=datetime no transformation',
'comment' => 'inputdatetime_4 dbType=datetime eval=datetime no transformation', ],
], [
[ 'label' => 'inputdatetime_4',
'label' => 'inputdatetime_4', 'inputValue' => '05:23 29-02-2016',
'inputValue' => '05:23 29-02-2016', 'expectedValue' => '05:23 29-02-2016',
'expectedValue' => '05:23 29-02-2016', 'expectedInternalValue' => '2016-02-29T05:23:00Z',
'expectedInternalValue' => '2016-02-29T05:23:00Z', 'expectedValueAfterSave' => '2016-02-29T05:23:00+00:00',
'expectedValueAfterSave' => '2016-02-29T05:23:00+00:00', 'comment' => 'inputdatetime_4 dbType=datetime eval=datetime Check valid leap year input',
'comment' => 'inputdatetime_4 dbType=datetime eval=datetime Check valid leap year input', ],
], [
[ 'label' => 'inputdatetime_6',
'label' => 'inputdatetime_6', 'inputValue' => '13:30:00',
'inputValue' => '13:30:00', 'expectedValue' => '13:30:00',
'expectedValue' => '13:30:00', 'expectedInternalValue' => '1970-01-01T13:30:00Z',
'expectedInternalValue' => '1970-01-01T13:30:00Z', 'expectedValueAfterSave' => '1970-01-01T13:30:00+00:00',
'expectedValueAfterSave' => '1970-01-01T13:30:00+00:00', 'comment' => 'inputdatetime_6 eval=timesec as time',
'comment' => 'inputdatetime_6 eval=timesec as time', ],
],
]; ];
} }
......
...@@ -407,6 +407,61 @@ class DataHandlerTest extends UnitTestCase ...@@ -407,6 +407,61 @@ class DataHandlerTest extends UnitTestCase
self::assertSame($returnValue['value'], $expected); self::assertSame($returnValue['value'], $expected);
} }
public function inputValueRangeCheckIsIgnoredWhenDefaultIsZeroAndInputValueIsEmptyDataProvider(): array
{
return [
'Empty string returns empty string or the number zero' => [
'',
'',
0
],
'Zero returns zero as a string or the number zero' => [
0,
'0',
0
],
'Zero as a string returns zero as a string or the number zero' => [
'0',
'0',
0
]
];
}
/**
* @dataProvider inputValueRangeCheckIsIgnoredWhenDefaultIsZeroAndInputValueIsEmptyDataProvider
* @test
* @param $inputValue
* @param $expected
* @param $expectedEvalInt
*/
public function inputValueRangeCheckIsIgnoredWhenDefaultIsZeroAndInputValueIsEmpty($inputValue, $expected, $expectedEvalInt)
{
$tcaFieldConf = [
'type' => 'input',
'eval' => 'datetime',
'default' => 0,
'range' => [
'lower' => 1627077600
]
];
$tcaFieldConfEvalInt = [
'type' => 'input',
'eval' => 'datetime,int',
'default' => '0',
'range' => [
'lower' => 1627077600
]
];
$returnValue = $this->subject->_call('checkValueForInput', $inputValue, $tcaFieldConf, '', 0, 0, '');
self::assertSame($expected, $returnValue['value']);
$returnValue = $this->subject->_call('checkValueForInput', $inputValue, $tcaFieldConfEvalInt, '', 0, 0, '');
self::assertSame($expectedEvalInt, $returnValue['value']);
}
/** /**
* @return array * @return array
*/ */
......
Markdown is supported
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