[!!!][BUGFIX] Cast negative int to boolean (true) in BooleanNode 84/26784/9
authorAlexander Schnitzler <alex.schnitzler@typovision.de>
Mon, 13 Jan 2014 13:52:56 +0000 (14:52 +0100)
committerNicole Cordes <typo3@cordes.co>
Sat, 22 Mar 2014 01:19:59 +0000 (02:19 +0100)
Currently Fluid treats negative integers as false which
results in <f:if condition="-9"> rendering else instead
of then.

When casting integers to boolean using php all integers
other than 0 (zero) are casted to true, just 0 results in
false. Therefore, casting of BooleanNode::convertToBoolean
must be adjusted to act like plain php.

This bugfix is a breaking change as people might have
deliberately used the wrong behaviour.

Releases: 6.2
Resolves: #54243
Resolves: #33597
Change-Id: Iebaa578cf071909610ca0e1abfc9bfaad7459231
Reviewed-on: https://review.typo3.org/26784
Reviewed-by: Oliver Hader
Tested-by: Oliver Hader
Reviewed-by: Stefan Neufeind
Reviewed-by: Nicole Cordes
Tested-by: Nicole Cordes
typo3/sysext/fluid/Classes/Core/Parser/SyntaxTree/BooleanNode.php
typo3/sysext/fluid/Tests/Unit/Core/Parser/SyntaxTree/BooleanNodeTest.php

index d49c7e2..6700efe 100644 (file)
@@ -318,18 +318,25 @@ class BooleanNode extends \TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\AbstractNode {
                if (is_bool($value)) {
                        return $value;
                }
+
+               if (is_integer($value) || is_float($value)) {
+                       return !empty($value);
+               }
+
                if (is_numeric($value)) {
-                       return $value > 0;
+                       return ($value != 0);
                }
+
                if (is_string($value)) {
                        return (!empty($value) && strtolower($value) !== 'false');
                }
                if (is_array($value) || (is_object($value) && $value instanceof \Countable)) {
-                       return count($value) > 0;
+                       return (bool) count($value);
                }
                if (is_object($value)) {
                        return TRUE;
                }
+
                return FALSE;
        }
 }
index fc371ba..956af48 100644 (file)
@@ -409,13 +409,41 @@ class BooleanNodeTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         */
        public function convertToBooleanProperlyConvertsNumericValues() {
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(FALSE));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(TRUE));
+
                $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0));
-               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(-1));
-               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('-1'));
-               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(-0.5));
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0'));
+
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0.0));
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0.0'));
+
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0x0));
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0x0'));
+
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0x1));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0x1'));
+
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0e0));
+               $this->assertFalse(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0e0'));
+
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(1e0));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('1e0'));
+
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(-1));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('-1'));
+
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(-0.5));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('-0.5'));
 
                $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(1));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('1'));
+
                $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0.5));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0.5'));
+
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean(0x1));
+               $this->assertTrue(\TYPO3\CMS\Fluid\Core\Parser\SyntaxTree\BooleanNode::convertToBoolean('0x10'));
        }
 
        /**