[BUGFIX] need to parse field value for maxitems = 1 with MM table 31/43531/5
authorAndreas Allacher <andreas.allacher@gmx.at>
Thu, 24 Sep 2015 16:01:17 +0000 (18:01 +0200)
committerMorton Jonuschat <m.jonuschat@mojocode.de>
Fri, 25 Sep 2015 14:04:26 +0000 (16:04 +0200)
If a select field has an MM table like sys_category_record_mm
it is necessary to parse the field value even if maxitems = 1
Furthermore, this really fixes #69836 as that was actually
only a workaround.

Resolves: #70081
Relates: #69836
Releases: master

Change-Id: I13c286048d666fa196d5dc83a72a3612090c4e25
Reviewed-on: http://review.typo3.org/43531
Reviewed-by: Markus Sommer <markussom@posteo.de>
Tested-by: Markus Sommer <markussom@posteo.de>
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
typo3/sysext/backend/Classes/Form/Element/SelectTreeElement.php
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaSelectItems.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaSelectItemsTest.php

index d3464f3..e28b012 100644 (file)
@@ -46,9 +46,6 @@ class SelectTreeElement extends AbstractFormElement {
                $possibleSelectboxItems = $config['items'];
 
                $selectedNodes = $parameterArray['itemFormElValue'];
-               if ($config['maxitems'] === 1 && count($selectedNodes) === 1 && empty($selectedNodes[0])) {
-                       $selectedNodes = array();
-               }
 
                $selectedNodesForApi = array();
                foreach ($selectedNodes as $selectedNode) {
index 8619ed8..cf62162 100644 (file)
@@ -1005,7 +1005,7 @@ class TcaSelectItems extends AbstractItemProvider implements FormDataProviderInt
                // will take care of showing the "Invalid value" text.
                // For maxitems=1 select fields is is also possible to select empty values.
                // @todo: move handling of invalid values to this data provider.
-               if ($fieldConfig['config']['maxitems'] === 1) {
+               if ($fieldConfig['config']['maxitems'] === 1 && empty($fieldConfig['config']['MM'])) {
                        return array($result['databaseRow'][$fieldName]);
                }
 
index d500c2c..fc3d582 100644 (file)
@@ -2424,4 +2424,100 @@ class TcaSelectItemsTest extends UnitTestCase {
                $this->assertEquals($expected, $this->subject->addData($input));
 
        }
+
+       /**
+        * @test
+        * @dataProvider correctValuesForMmRelationWithSingleValueAllowedDataProvider
+        */
+       public function correctValuesForMmRelationWithSingleValueAllowed($input, $relationHandlerUids) {
+
+               $GLOBALS['TCA']['foreignTable'] = [];
+
+               /** @var BackendUserAuthentication|ObjectProphecy $backendUserProphecy */
+               $backendUserProphecy = $this->prophesize(BackendUserAuthentication::class);
+               $GLOBALS['BE_USER'] = $backendUserProphecy->reveal();
+
+               /** @var DatabaseConnection|ObjectProphecy $database */
+               $database = $this->prophesize(DatabaseConnection::class);
+               $GLOBALS['TYPO3_DB'] = $database->reveal();
+
+               $fieldConfig = $input['processedTca']['columns']['aField']['config'];
+               /** @var RelationHandler|ObjectProphecy $relationHandlerProphecy */
+               $relationHandlerProphecy = $this->prophesize(RelationHandler::class);
+               GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphecy->reveal());
+
+               $field = $input['databaseRow']['aField'];
+               $foreignTable = $input['processedTca']['columns']['aField']['config']['foreign_table'];
+               $mmTable = $input['processedTca']['columns']['aField']['config']['MM'];
+               $uid = $input['databaseRow']['uid'];
+               $tableName = $input['tableName'];
+               $fieldConfig = $input['processedTca']['columns']['aField']['config'];
+
+               $relationHandlerProphecy->start($field, $foreignTable, $mmTable, $uid, $tableName, $fieldConfig)->shouldBeCalled();
+               $relationHandlerProphecy->getValueArray()->shouldBeCalled()->willReturn($relationHandlerUids);
+
+               $expected = $input;
+               $expected['databaseRow']['aField'] = $relationHandlerUids;
+
+               $this->assertEquals($expected, $this->subject->addData($input));
+       }
+
+       /**
+        * Data Provider
+        */
+       public function correctValuesForMmRelationWithSingleValueAllowedDataProvider() {
+               return array(
+                       'Relation with MM table and maxitems = 1 processes field value (item count)' => [
+                               [
+                                       'tableName' => 'aTable',
+                                       'databaseRow' => [
+                                               'uid' => 42,
+                                               // MM relation with one item has 1 in field value
+                                               'aField' => 1,
+                                       ],
+                                       'processedTca' => [
+                                               'columns' => [
+                                                       'aField' => [
+                                                               'config' => [
+                                                                       'type' => 'select',
+                                                                       'maxitems' => 1,
+                                                                       'MM' => 'mm_aTable_foreignTable',
+                                                                       'foreign_table' => 'foreignTable',
+                                                                       'items' => [],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                               [
+                                       24
+                               ]
+                       ],
+                       'Relation with MM table and maxitems = 1 results in empty array if no items are set' => [
+                               [
+                                       'tableName' => 'aTable',
+                                       'databaseRow' => [
+                                               'uid' => 58,
+                                               // MM relation with no items has 0 in field value
+                                               'aField' => 0,
+                                       ],
+                                       'processedTca' => [
+                                               'columns' => [
+                                                       'aField' => [
+                                                               'config' => [
+                                                                       'type' => 'select',
+                                                                       'maxitems' => 1,
+                                                                       'MM' => 'mm_aTable_foreignTable',
+                                                                       'foreign_table' => 'foreignTable',
+                                                                       'items' => [],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                               [
+                               ]
+                       ]
+               );
+       }
 }