diff --git a/typo3/sysext/form/Classes/Domain/Model/FormElements/AbstractFormElement.php b/typo3/sysext/form/Classes/Domain/Model/FormElements/AbstractFormElement.php index 240e9f2663a1abe7bf62a2814cacbfeff0df67f9..05bb5826b91c60c28c5497e8a856eac9cb94f90b 100644 --- a/typo3/sysext/form/Classes/Domain/Model/FormElements/AbstractFormElement.php +++ b/typo3/sysext/form/Classes/Domain/Model/FormElements/AbstractFormElement.php @@ -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; @@ -146,7 +147,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; + } } /** diff --git a/typo3/sysext/form/Classes/Domain/Model/FormElements/Section.php b/typo3/sysext/form/Classes/Domain/Model/FormElements/Section.php index b2497677f4048b506ee5e6d31e8bb68c2c03551e..bb87519570c85928d9688e8b48080d8f0dd6a362 100644 --- a/typo3/sysext/form/Classes/Domain/Model/FormElements/Section.php +++ b/typo3/sysext/form/Classes/Domain/Model/FormElements/Section.php @@ -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; @@ -116,7 +117,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 index 0000000000000000000000000000000000000000..15f3a94d2c724107d9d46347b3057261685cf453 --- /dev/null +++ b/typo3/sysext/form/Tests/Unit/Domain/FormElements/AbstractFormElementTest.php @@ -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 index 0000000000000000000000000000000000000000..fa4ca60843a3deb3a32c03f7d453de7669b33e1a --- /dev/null +++ b/typo3/sysext/form/Tests/Unit/Domain/FormElements/SectionTest.php @@ -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']); + } +}