From 018fc1e543eb9700979beae5431fdb21bad47dc2 Mon Sep 17 00:00:00 2001
From: Nicole Cordes <typo3@cordes.co>
Date: Sat, 12 Oct 2013 18:36:30 +0200
Subject: [PATCH] [BUGFIX] Wrong calculation of maximum value for checkbox
 fields

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/24650
Reviewed-by: Thorsten Kahler
Reviewed-by: Anja Leichsenring
Tested-by: Anja Leichsenring
Reviewed-by: Christian Kuhn
Tested-by: Christian Kuhn
---
 .../core/Classes/DataHandling/DataHandler.php |  2 +-
 .../Unit/DataHandling/DataHandlerTest.php     | 50 +++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/typo3/sysext/core/Classes/DataHandling/DataHandler.php b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
index 2be5492693d4..ad75c01a78b8 100644
--- a/typo3/sysext/core/Classes/DataHandling/DataHandler.php
+++ b/typo3/sysext/core/Classes/DataHandling/DataHandler.php
@@ -1693,7 +1693,7 @@ class DataHandler {
 		if (!$itemC) {
 			$itemC = 1;
 		}
-		$maxV = pow(2, $itemC);
+		$maxV = pow(2, $itemC) - 1;
 		if ($value < 0) {
 			$value = 0;
 		}
diff --git a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
index 2db34a1def03..5e1b05dac9c5 100644
--- a/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
+++ b/typo3/sysext/core/Tests/Unit/DataHandling/DataHandlerTest.php
@@ -542,4 +542,54 @@ class DataHandlerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
 		$mockDataHandler->expects($this->never())->method('deleteAction');
 		$mockDataHandler->deleteRecord_procBasedOnFieldType($table, 42, 'foo', 'bar', $conf);
 	}
+
+	/**
+	 * @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()));
+	}
 }
-- 
GitLab