[BUGFIX] EXT:form - merge properties with configuration settings 64/54964/2
authorRalf Zimmermann <ralf.zimmermann@tritum.de>
Tue, 12 Sep 2017 09:58:44 +0000 (11:58 +0200)
committerSusanne Moog <susanne.moog@typo3.org>
Thu, 7 Dec 2017 13:57:06 +0000 (14:57 +0100)
Merge the properties set through the formDefinition with the
predefined properties from the base configuration.
Add tests for property part of FormElementInterface implementation.

Resolves: #82243
Releases: master, 8.7
Change-Id: Ia6a4428aa52508ec6756eaa6a9d9c471e932baf8
Reviewed-on: https://review.typo3.org/54964
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
typo3/sysext/form/Classes/Domain/Model/FormElements/AbstractFormElement.php
typo3/sysext/form/Classes/Domain/Model/FormElements/Section.php
typo3/sysext/form/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php [new file with mode: 0644]
typo3/sysext/form/Tests/Unit/Domain/FormElements/SectionTest.php [new file with mode: 0644]

index 737f0e8..3938402 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Form\Domain\Model\FormElements;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
 use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
@@ -152,7 +153,11 @@ abstract class AbstractFormElement extends AbstractRenderable implements FormEle
      */
     public function setProperty(string $key, $value)
     {
-        $this->properties[$key] = $value;
+        if (is_array($value) && is_array($this->properties[$key])) {
+            ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
+        } else {
+            $this->properties[$key] = $value;
+        }
     }
 
     /**
index b36a8ae..74eb148 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Form\Domain\Model\FormElements;
  * The TYPO3 project - inspiring people to share!
  */
 
+use TYPO3\CMS\Core\Utility\ArrayUtility;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
 use TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface;
@@ -121,7 +122,11 @@ class Section extends AbstractSection implements FormElementInterface
      */
     public function setProperty(string $key, $value)
     {
-        $this->properties[$key] = $value;
+        if (is_array($value) && is_array($this->properties[$key])) {
+            ArrayUtility::mergeRecursiveWithOverrule($this->properties[$key], $value);
+        } else {
+            $this->properties[$key] = $value;
+        }
     }
 
     /**
diff --git a/typo3/sysext/form/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php b/typo3/sysext/form/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php
new file mode 100644 (file)
index 0000000..15f3a94
--- /dev/null
@@ -0,0 +1,88 @@
+<?php
+namespace TYPO3\CMS\Form\Tests\Unit\Domain\FormElements;
+
+use TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement;
+
+/**
+ * Test TYPO3\CMS\Form\Domain\Model\FormElements\AbstractFormElement class
+ *
+ * Class AbstractFormElementTest
+ */
+class AbstractFormElementTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    protected static $IDENTIFIER = 'an_id';
+    protected static $TYPE = 'a_type';
+
+    /**
+     * An accessible instance of abstract class under test
+     * @var AbstractFormElement
+     */
+    protected $abstractFormElementInstance = null;
+
+    /**
+     * @before
+     */
+    public function setUp()
+    {
+        parent::setUp();
+        $this->abstractFormElementInstance = new class(self::$IDENTIFIER, self::$TYPE) extends AbstractFormElement {
+        };
+    }
+
+    /**
+     * @test
+     */
+    public function newInstanceHasNoProperties()
+    {
+        $this->assertNotNull($this->abstractFormElementInstance);
+        $this->assertCount(0, $this->abstractFormElementInstance->getProperties());
+    }
+
+    /**
+     * @test
+     */
+    public function setSimpleProperties()
+    {
+        $this->abstractFormElementInstance->setProperty('foo', 'bar');
+        $this->abstractFormElementInstance->setProperty('buz', 'qax');
+        $properties = $this->abstractFormElementInstance->getProperties();
+
+        $this->assertCount(2, $properties);
+        $this->assertTrue(array_key_exists('foo', $properties));
+        $this->assertEquals('bar', $properties['foo']);
+        $this->assertTrue(array_key_exists('buz', $properties));
+        $this->assertEquals('qax', $properties['buz']);
+    }
+
+    /**
+     * @test
+     */
+    public function overrideProperties()
+    {
+        $this->abstractFormElementInstance->setProperty('foo', 'bar');
+        $this->abstractFormElementInstance->setProperty('foo', 'buz');
+
+        $properties = $this->abstractFormElementInstance->getProperties();
+        $this->assertEquals(1, count($properties));
+        $this->assertTrue(array_key_exists('foo', $properties));
+        $this->assertEquals('buz', $properties['foo']);
+    }
+
+    /**
+     * @test
+     */
+    public function setArrayProperties()
+    {
+        $this->abstractFormElementInstance->setProperty('foo', ['bar' => 'baz', 'bla' => 'blubb']);
+        $properties = $this->abstractFormElementInstance->getProperties();
+
+        $this->assertCount(1, $properties);
+        $this->assertTrue(array_key_exists('foo', $properties));
+
+        //check arrays details
+        $this->assertTrue(is_array($properties['foo']));
+        $this->assertCount(2, $properties['foo']);
+        $this->assertTrue(array_key_exists('bar', $properties['foo']));
+        $this->assertEquals('baz', $properties['foo']['bar']);
+    }
+}
diff --git a/typo3/sysext/form/Tests/Unit/Domain/FormElements/SectionTest.php b/typo3/sysext/form/Tests/Unit/Domain/FormElements/SectionTest.php
new file mode 100644 (file)
index 0000000..fa4ca60
--- /dev/null
@@ -0,0 +1,87 @@
+<?php
+namespace TYPO3\CMS\Form\Tests\Unit\Domain\FormElements;
+
+use TYPO3\CMS\Form\Domain\Model\FormElements\Section;
+
+/**
+ * Test TYPO3\CMS\Form\Domain\Model\FormElements\Section class
+ *
+ * Class AbstractFormElementTest
+ */
+class SectionTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
+{
+    protected static $IDENTIFIER = 'an_id';
+    protected static $TYPE = 'a_type';
+
+    /**
+     * An instance of section
+     * @var Section
+     */
+    protected $sectionInstance = null;
+
+    /**
+     * @before
+     */
+    public function setUp()
+    {
+        parent::setUp();
+        $this->sectionInstance = new Section(self::$IDENTIFIER, self::$TYPE);
+    }
+
+    /**
+     * @test
+     */
+    public function newInstanceHasNoProperties()
+    {
+        $this->assertNotNull($this->sectionInstance);
+        $this->assertCount(0, $this->sectionInstance->getProperties());
+    }
+
+    /**
+     * @test
+     */
+    public function setSimpleProperties()
+    {
+        $this->sectionInstance->setProperty('foo', 'bar');
+        $this->sectionInstance->setProperty('buz', 'qax');
+        $properties = $this->sectionInstance->getProperties();
+
+        $this->assertCount(2, $properties, json_encode($properties));
+        $this->assertTrue(array_key_exists('foo', $properties));
+        $this->assertEquals('bar', $properties['foo']);
+        $this->assertTrue(array_key_exists('buz', $properties));
+        $this->assertEquals('qax', $properties['buz']);
+    }
+
+    /**
+     * @test
+     */
+    public function overrideProperties()
+    {
+        $this->sectionInstance->setProperty('foo', 'bar');
+        $this->sectionInstance->setProperty('foo', 'buz');
+
+        $properties = $this->sectionInstance->getProperties();
+        $this->assertEquals(1, count($properties));
+        $this->assertTrue(array_key_exists('foo', $properties));
+        $this->assertEquals('buz', $properties['foo']);
+    }
+
+    /**
+     * @test
+     */
+    public function setArrayProperties()
+    {
+        $this->sectionInstance->setProperty('foo', ['bar' => 'baz', 'bla' => 'blubb']);
+        $properties = $this->sectionInstance->getProperties();
+
+        $this->assertCount(1, $properties);
+        $this->assertTrue(array_key_exists('foo', $properties));
+
+        //check arrays details
+        $this->assertTrue(is_array($properties['foo']));
+        $this->assertCount(2, $properties['foo']);
+        $this->assertTrue(array_key_exists('bar', $properties['foo']));
+        $this->assertEquals('baz', $properties['foo']['bar']);
+    }
+}