[BUGFIX] Wrong calculation of maximum value for checkbox fields 56/24656/2
authorNicole Cordes <typo3@cordes.co>
Sat, 12 Oct 2013 17:37:46 +0000 (19:37 +0200)
committerNicole Cordes <typo3@cordes.co>
Sat, 12 Oct 2013 17:48:55 +0000 (19:48 +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/24655
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
Reviewed-on: https://review.typo3.org/24656

t3lib/class.t3lib_tcemain.php
tests/t3lib/t3lib_tcemainTest.php

index 0123175..1ca7560 100644 (file)
@@ -1422,7 +1422,7 @@ class t3lib_TCEmain {
                if (!$itemC) {
                        $itemC = 1;
                }
-               $maxV = pow(2, $itemC);
+               $maxV = pow(2, $itemC) - 1;
 
                if ($value < 0) {
                        $value = 0;
index 5fdf40d..2bbf75b 100644 (file)
@@ -327,5 +327,55 @@ class t3lib_tcemainTest extends tx_phpunit_testcase {
                $expected = 'foo' . $logDetails . 'bar';
                $this->assertStringEndsWith($expected, $this->fixture->errorLog[0]);
        }
+
+       /**
+        * @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