[BUGFIX] Wrong calculation of maximum value for checkbox fields 53/24653/2
authorNicole Cordes <typo3@cordes.co>
Sat, 12 Oct 2013 16:36:30 +0000 (18:36 +0200)
committerNicole Cordes <typo3@cordes.co>
Sat, 12 Oct 2013 17:22:51 +0000 (19:22 +0200)
This patch corrects the calculation of the maximum value for a group
of checkboxes which is stored as bit flag value in the database. The
formular for the maximum value is 2nd power of the item count minus one.

Resolves: #52104
Releases: 6.2, 6.1, 6.0, 4.7, 4.5
Change-Id: I0eb430b72a072838c6ac3bc3f5e339ff2509c455
Reviewed-on: https://review.typo3.org/24653
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
typo3/sysext/core/Classes/DataHandling/DataHandler.php
typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php

index cad6d75..84ba83d 100644 (file)
@@ -1531,7 +1531,7 @@ class DataHandler {
                if (!$itemC) {
                        $itemC = 1;
                }
-               $maxV = pow(2, $itemC);
+               $maxV = pow(2, $itemC) - 1;
                if ($value < 0) {
                        $value = 0;
                }
index 1503e76..06bcaf8 100644 (file)
@@ -502,6 +502,56 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        ),
                );
        }
+
+       /**
+        * @return array
+        */
+       public function checkValue_checkReturnsExpectedValuesDataProvider() {
+               return array(
+                       'None item selected' => array(
+                               0,
+                               0
+                       ),
+                       'All items selected' => array(
+                               7,
+                               7
+                       ),
+                       'Item 1 and 2 are selected' => array(
+                               3,
+                               3
+                       ),
+                       'Value is higher than allowed' => array(
+                               15,
+                               7
+                       ),
+                       'Negative value' => array(
+                               -5,
+                               0
+                       )
+               );
+       }
+
+       /**
+        * @param string $value
+        * @param string $expectedValue
+        *
+        * @dataProvider checkValue_checkReturnsExpectedValuesDataProvider
+        * @test
+        */
+       public function checkValue_checkReturnsExpectedValues($value, $expectedValue) {
+               $expectedResult = array(
+                       'value' => $expectedValue
+               );
+               $result = array();
+               $tcaFieldConfiguration = array(
+                       'items' => array(
+                               array('Item 1', 0),
+                               array('Item 2', 0),
+                               array('Item 3', 0)
+                       )
+               );
+               $this->assertSame($expectedResult, $this->fixture->checkValue_check($result, $value, $tcaFieldConfiguration, array()));
+       }
 }
 
-?>
\ No newline at end of file
+?>