[BUGFIX] Properly set checked attribute for objects 68/35668/9
authorMathias Brodala <mbrodala@pagemachine.de>
Tue, 30 Dec 2014 09:12:45 +0000 (10:12 +0100)
committerAndreas Fernandez <andreas.fernandez@aspedia.de>
Thu, 23 Apr 2015 15:29:16 +0000 (17:29 +0200)
This makes sure the checked attribute is properly set by the
CheckboxViewHelper for a list of objects.

Resolves: #64057
Releases: master, 6.2
Change-Id: I7b25d2086b8e1d436730283e30a6df0eda34712a
Reviewed-on: http://review.typo3.org/35668
Reviewed-by: Markus Klein <klein.t3@reelworx.at>
Tested-by: Markus Klein <klein.t3@reelworx.at>
Reviewed-by: Andreas Fernandez <andreas.fernandez@aspedia.de>
Tested-by: Andreas Fernandez <andreas.fernandez@aspedia.de>
typo3/sysext/fluid/Classes/ViewHelpers/Form/AbstractFormFieldViewHelper.php
typo3/sysext/fluid/Classes/ViewHelpers/Form/CheckboxViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Form/CheckboxViewHelperTest.php

index 4f47e56..baefad2 100644 (file)
@@ -124,7 +124,20 @@ abstract class AbstractFormFieldViewHelper extends AbstractFormViewHelper {
                        $this->addAdditionalIdentityPropertiesIfNeeded();
                }
 
-               if ($convertObjects && is_object($value)) {
+               if ($convertObjects) {
+                       $value = $this->convertToPlainValue($value);
+               }
+               return $value;
+       }
+
+       /**
+        * Converts an arbitrary value to a plain value
+        *
+        * @param mixed $value The value to convert
+        * @return mixed
+        */
+       protected function convertToPlainValue($value) {
+               if (is_object($value)) {
                        $identifier = $this->persistenceManager->getIdentifierByObject($value);
                        if ($identifier !== NULL) {
                                $value = $identifier;
@@ -269,4 +282,4 @@ abstract class AbstractFormFieldViewHelper extends AbstractFormViewHelper {
                return '';
        }
 
-}
\ No newline at end of file
+}
index 9270f09..cb8686a 100644 (file)
@@ -86,6 +86,7 @@ class CheckboxViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormF
                                $propertyValue = iterator_to_array($propertyValue);
                        }
                        if (is_array($propertyValue)) {
+                               $propertyValue = array_map(array($this, 'convertToPlainValue'), $propertyValue);
                                if ($checked === NULL) {
                                        $checked = in_array($valueAttribute, $propertyValue);
                                }
index 52ded26..6b0ad50 100644 (file)
@@ -11,19 +11,23 @@ namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form;
  * The TYPO3 project - inspiring people to share!                         *
  *                                                                        */
 
+use TYPO3\CMS\Extbase\Persistence\PersistenceManagerInterface;
+use TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder;
+use TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper;
+
 /**
  * Test for the "Checkbox" Form view helper
  */
 class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\FormFieldViewHelperBaseTestcase {
 
        /**
-        * @var \TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper
+        * @var CheckboxViewHelper|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\CMS\Core\Tests\AccessibleObjectInterface
         */
        protected $viewHelper;
 
        protected function setUp() {
                parent::setUp();
-               $this->viewHelper = $this->getAccessibleMock(\TYPO3\CMS\Fluid\ViewHelpers\Form\CheckboxViewHelper::class, array('setErrorClassAttribute', 'getName', 'getValue', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration'));
+               $this->viewHelper = $this->getAccessibleMock(CheckboxViewHelper::class, array('setErrorClassAttribute', 'getName', 'getValue', 'isObjectAccessorMode', 'getPropertyValue', 'registerFieldNameForFormTokenGeneration'));
                $this->arguments['property'] = '';
                $this->injectDependenciesIntoViewHelper($this->viewHelper);
                $this->viewHelper->initializeArguments();
@@ -33,7 +37,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderCorrectlySetsTagNameAndDefaultAttributes() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->once())->method('setTagName')->with('input');
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
@@ -52,7 +56,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderSetsCheckedAttributeIfSpecified() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
                $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
@@ -70,7 +74,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderIgnoresValueOfBoundPropertyIfCheckedIsSet() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
                $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
@@ -90,7 +94,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeBoolean() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
                $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
@@ -110,7 +114,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderAppendsSquareBracketsToNameAttributeIfBoundToAPropertyOfTypeArray() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
                $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('foo[]');
@@ -130,7 +134,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArray() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
                $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
@@ -150,7 +154,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderCorrectlySetsCheckedAttributeIfCheckboxIsBoundToAPropertyOfTypeArrayObject() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
                $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
@@ -170,7 +174,7 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
         * @test
         */
        public function renderSetsCheckedAttributeIfBoundPropertyIsNotNull() {
-               $mockTagBuilder = $this->getMock(\TYPO3\CMS\Fluid\Core\ViewHelper\TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
                $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
                $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo');
                $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 'bar');
@@ -189,6 +193,38 @@ class CheckboxViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
        /**
         * @test
         */
+       public function renderSetsCheckedAttributeForListOfObjects() {
+               $mockTagBuilder = $this->getMock(TagBuilder::class, array('setTagName', 'addAttribute'));
+               $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('type', 'checkbox');
+               $mockTagBuilder->expects($this->at(2))->method('addAttribute')->with('name', 'foo[]');
+               $mockTagBuilder->expects($this->at(3))->method('addAttribute')->with('value', 2);
+               $mockTagBuilder->expects($this->at(4))->method('addAttribute')->with('checked', 'checked');
+
+               $object1 = new \stdClass();
+               $object2 = new \stdClass();
+               $object3 = new \stdClass();
+
+               $this->viewHelper->expects($this->any())->method('getName')->willReturn('foo');
+               $this->viewHelper->expects($this->any())->method('getValue')->willReturn(2);
+               $this->viewHelper->expects($this->any())->method('isObjectAccessorMode')->willReturn(TRUE);
+               $this->viewHelper->expects($this->any())->method('getPropertyValue')->willReturn(array($object1, $object2, $object3));
+               $this->viewHelper->_set('tag', $mockTagBuilder);
+
+               $mockPersistenceManager = $this->getMock(PersistenceManagerInterface::class);
+               $mockPersistenceManager->expects($this->any())->method('getIdentifierByObject')->will($this->returnValueMap(array(
+                       array($object1, 1),
+                       array($object2, 2),
+                       array($object3, 3),
+               )));
+               $this->viewHelper->_set('persistenceManager', $mockPersistenceManager);
+
+               $this->viewHelper->initialize();
+               $this->viewHelper->render();
+       }
+
+       /**
+        * @test
+        */
        public function renderCallsSetErrorClassAttribute() {
                $this->viewHelper->expects($this->once())->method('setErrorClassAttribute');
                $this->viewHelper->render();