From 35f272f29ebb2484a0fd3d17a22b2fee63f2da1a Mon Sep 17 00:00:00 2001
From: Nikita Hovratov <nikita.h@live.de>
Date: Sat, 10 Jul 2021 16:41:10 +0200
Subject: [PATCH] [BUGFIX] Remove always true part of if condition

This patch fixes several small bugs regarding DataHandler range
validation:

1. The strict comparison of the value against the default value might
compare floats with integer values. This causes the condition to be
always true. Besides of that, it's not smart to skip range validation
if the value equals the default value as the default value itself can
be out of range due to user error.

2. The TCA option "checkbox" for fields of type "input" is not used
anymore since TYPO3 4.5. This can be safely removed as the todo stated.

3. The tests for range validation with values evaluated as type double
used an incorrect eval value: "double". The correct value is "double2".
Tests are fixed according to that.

Resolves: #94527
Releases: master, 10.4
Change-Id: I9cdf836272b4cce64f9e35a09107499d25fc955f
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/69804
Tested-by: core-ci <typo3@b13.com>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Benni Mack <benni@typo3.org>
---
 .../sysext/core/Classes/DataHandling/DataHandler.php  | 11 +----------
 .../core/Tests/Unit/DataHandling/DataHandlerTest.php  | 10 +++++-----
 2 files changed, 6 insertions(+), 15 deletions(-)

diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
index 86c5b64b6934..0348f920bf7f 100644
--- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php
+++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
@@ -1742,16 +1742,7 @@ class DataHandler implements LoggerAwareInterface
         }
 
         // Checking range of value:
-        // @todo: The "checkbox" option was removed for type=input, this check could be probably relaxed?
-        if (
-            isset($tcaFieldConf['range']) && $tcaFieldConf['range']
-            && (!isset($tcaFieldConf['checkbox']) || $res['value'] != $tcaFieldConf['checkbox'])
-            && (
-                !isset($tcaFieldConf['default'])
-                || floor($res['value']) !== (int)$tcaFieldConf['default']
-                || ceil($res['value']) !== (int)$tcaFieldConf['default']
-            )
-        ) {
+        if (isset($tcaFieldConf['range']) && is_array($tcaFieldConf['range'])) {
             if (isset($tcaFieldConf['range']['upper']) && ceil($res['value']) > (int)$tcaFieldConf['range']['upper']) {
                 $res['value'] = (int)$tcaFieldConf['range']['upper'];
             }
diff --git a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
index b877b05e439b..fe7d66c2b56c 100644
--- a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
+++ b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
@@ -289,7 +289,7 @@ class DataHandlerTest extends UnitTestCase
         return [
             '"0" returns zero as string' => [
                 '0',
-                '0'
+                '0.00'
             ],
             '"-0.5" is interpreted correctly as -0.5 but is lower than 0 and set to 0' => [
                 '-0.5',
@@ -297,11 +297,11 @@ class DataHandlerTest extends UnitTestCase
             ],
             '"0.5" is interpreted correctly as 0.5 and is equal to 0.5' => [
                 '0.5',
-                '0.5'
+                '0.50'
             ],
             '"39.9" is interpreted correctly as 39.9 and is equal to 39.9' => [
                 '39.9',
-                '39.9'
+                '39.90'
             ],
             '"42.3" is interpreted correctly as 42.3 but is greater then 42 and set to 42' => [
                 '42.3',
@@ -320,7 +320,7 @@ class DataHandlerTest extends UnitTestCase
     {
         $tcaFieldConf = [
             'input' => [],
-            'eval' => 'double',
+            'eval' => 'double2',
             'range' => [
                 'lower' => '0',
                 'upper' => '42'
@@ -340,7 +340,7 @@ class DataHandlerTest extends UnitTestCase
     {
         $tcaFieldConf = [
             'input' => [],
-            'eval' => 'double',
+            'eval' => 'double2',
             'range' => [
                 'lower' => '0',
                 'upper' => '42'
-- 
GitLab