[BUGFIX] FormEngine: Handle empty arrays in display conditions 14/45214/2
authorMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 11 Dec 2015 14:38:59 +0000 (15:38 +0100)
committerMarkus Klein <markus.klein@typo3.org>
Fri, 11 Dec 2015 14:52:24 +0000 (15:52 +0100)
Records passed to the EvaluateDisplayConditions provider can contain
array values which can be empty in the case of new records. Handle this
condition in the evaluator.

Resolves: #71824
Releases: master
Change-Id: Ie5aa3f74913059f2a1e5933a8314dee6cb0fa1fa
Reviewed-on: https://review.typo3.org/45214
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Reviewed-by: Markus Klein <markus.klein@typo3.org>
Tested-by: Markus Klein <markus.klein@typo3.org>
typo3/sysext/backend/Classes/Form/FormDataProvider/EvaluateDisplayConditions.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/EvaluateDisplayConditionsTest.php

index ce89d25..64cdd69 100644 (file)
@@ -344,7 +344,7 @@ class EvaluateDisplayConditions implements FormDataProviderInterface
         $result = false;
         switch ($operator) {
             case 'REQ':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 if (strtoupper($operand) === 'TRUE') {
@@ -354,32 +354,32 @@ class EvaluateDisplayConditions implements FormDataProviderInterface
                 }
                 break;
             case '>':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 $result = $fieldValue > $operand;
                 break;
             case '<':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 $result = $fieldValue < $operand;
                 break;
             case '>=':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 $result = $fieldValue >= $operand;
                 break;
             case '<=':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 $result = $fieldValue <= $operand;
                 break;
             case '-':
             case '!-':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 list($minimum, $maximum) = explode('-', $operand);
@@ -390,7 +390,7 @@ class EvaluateDisplayConditions implements FormDataProviderInterface
                 break;
             case '=':
             case '!=':
-                if (is_array($fieldValue) && count($fieldValue) === 1) {
+                if (is_array($fieldValue) && count($fieldValue) <= 1) {
                     $fieldValue = array_shift($fieldValue);
                 }
                 $result = $fieldValue == $operand;
index 706f8f3..609d4cf 100644 (file)
@@ -420,6 +420,36 @@ class EvaluateDisplayConditionsTest extends UnitTestCase
                 [],
                 false,
             ],
+            'Field is not present if empty array given' => [
+                'REQ:foo:TRUE',
+                ['foo' => []],
+                false,
+            ],
+            'Field is not greater zero if empty array given' => [
+                'FIELD:foo:>:0',
+                ['foo' => []],
+                false,
+            ],
+            'Field is not greater than or equal to zero if empty array given' => [
+                'FIELD:foo:>=:0',
+                ['foo' => []],
+                false,
+            ],
+            'Field is less than 1 if empty array given' => [
+                'FIELD:foo:<:1',
+                ['foo' => []],
+                true,
+            ],
+            'Field is less than or equal to 1 if empty array given' => [
+                'FIELD:foo:<=:1',
+                ['foo' => []],
+                true,
+            ],
+            'Field does not equal 0 if empty array given' => [
+                'FIELD:foo:=:0',
+                ['foo' => []],
+                false,
+            ],
             'Field value string comparison' => [
                 'FIELD:foo:=:bar',
                 ['foo' => 'bar'],