Skip to content

Commit

Permalink
[BUGFIX] Fix range handling for eval double
Browse files Browse the repository at this point in the history
If a field has a '''double''' eval, '''upper''' and '''lower''' range
checks don't work as expected as the value is always cast to an int
before comparison.

Switching to floor and ceil helps with these edge cases while retaining
the casting to int behaviour.

Resolves: #94103
Releases: master,10.4
Change-Id: I28b77e2360b86e27e0297a08c5b6d20e0637a0dd
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69437
Tested-by: core-ci <typo3@b13.com>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
  • Loading branch information
pschriner authored and maddy2101 committed Jun 15, 2021
1 parent ae55eef commit adce6db
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
10 changes: 7 additions & 3 deletions typo3/sysext/core/Classes/DataHandling/DataHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1915,12 +1915,16 @@ protected function checkValueForInput($value, $tcaFieldConf, $table, $id, $realP
if (
isset($tcaFieldConf['range']) && $tcaFieldConf['range']
&& (!isset($tcaFieldConf['checkbox']) || $res['value'] != $tcaFieldConf['checkbox'])
&& (!isset($tcaFieldConf['default']) || (int)$res['value'] !== (int)$tcaFieldConf['default'])
&& (
!isset($tcaFieldConf['default'])
|| floor($res['value']) !== (int)$tcaFieldConf['default']
|| ceil($res['value']) !== (int)$tcaFieldConf['default']
)
) {
if (isset($tcaFieldConf['range']['upper']) && (int)$res['value'] > (int)$tcaFieldConf['range']['upper']) {
if (isset($tcaFieldConf['range']['upper']) && ceil($res['value']) > (int)$tcaFieldConf['range']['upper']) {
$res['value'] = (int)$tcaFieldConf['range']['upper'];
}
if (isset($tcaFieldConf['range']['lower']) && (int)$res['value'] < (int)$tcaFieldConf['range']['lower']) {
if (isset($tcaFieldConf['range']['lower']) && floor($res['value']) < (int)$tcaFieldConf['range']['lower']) {
$res['value'] = (int)$tcaFieldConf['range']['lower'];
}
}
Expand Down
72 changes: 72 additions & 0 deletions typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,78 @@ public function inputValueCheckRecognizesStringValuesAsIntegerValuesCorrectly($v
self::assertSame($returnValue['value'], $expectedReturnValue);
}

/**
* Data provider for inputValuesRangeDoubleDataProvider
*
* @return array
*/
public function inputValuesRangeDoubleDataProvider()
{
return [
'"0" returns zero as string' => [
'0',
'0'
],
'"-0.5" is interpreted correctly as -0.5 but is lower than 0 and set to 0' => [
'-0.5',
0
],
'"0.5" is interpreted correctly as 0.5 and is equal to 0.5' => [
'0.5',
'0.5'
],
'"39.9" is interpreted correctly as 39.9 and is equal to 39.9' => [
'39.9',
'39.9'
],
'"42.3" is interpreted correctly as 42.3 but is greater then 42 and set to 42' => [
'42.3',
42
],
];
}

/**
* @test
* @dataProvider inputValuesRangeDoubleDataProvider
* @param string $value
* @param int $expectedReturnValue
*/
public function inputValueCheckRespectsRightLowerAndUpperLimitForDouble($value, $expectedReturnValue)
{
$tcaFieldConf = [
'input' => [],
'eval' => 'double',
'range' => [
'lower' => '0',
'upper' => '42'
]
];
$returnValue = $this->subject->_call('checkValueForInput', $value, $tcaFieldConf, '', 0, 0, '');
self::assertSame($returnValue['value'], $expectedReturnValue);
}

/**
* @test
* @dataProvider inputValuesRangeDoubleDataProvider
* @param string $value
* @param int $expectedReturnValue
*/
public function inputValueCheckRespectsRightLowerAndUpperLimitWithDefaultValueForDouble($value, $expectedReturnValue)
{
$tcaFieldConf = [
'input' => [],
'eval' => 'double',
'range' => [
'lower' => '0',
'upper' => '42'
],
'default' => 0
];
$returnValue = $this->subject->_call('checkValueForInput', $value, $tcaFieldConf, '', 0, 0, '');
self::assertSame($returnValue['value'], $expectedReturnValue);
}

/**
* @return array
*/
Expand Down

0 comments on commit adce6db

Please sign in to comment.