[BUGFIX] BE: correctly resolve default values for MM relations 27/43727/9
authorAndreas Allacher <andreas.allacher@gmx.at>
Fri, 2 Oct 2015 06:40:18 +0000 (08:40 +0200)
committerFrank Nägler <frank.naegler@typo3.org>
Fri, 23 Oct 2015 09:00:07 +0000 (11:00 +0200)
With this default values for MM relations are
again retrieved correctly.
This bug was introduced with the "FormEngine" changes
because the values for MM tables are retrieved via
RelationHandler now and always use the MM table.
In order for this to work for default values the
MM table needs to be ignored.

Resolves: #70275
Releases: master
Change-Id: Ice3660c3e2d3c172034f308a642a92d4cbe2f618
Reviewed-on: https://review.typo3.org/43727
Reviewed-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Tested-by: Morton Jonuschat <m.jonuschat@mojocode.de>
Reviewed-by: Frank Nägler <frank.naegler@typo3.org>
Tested-by: Frank Nägler <frank.naegler@typo3.org>
typo3/sysext/backend/Classes/Form/FormDataProvider/TcaSelectItems.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/TcaSelectItemsTest.php

index fad9afd..b25f308 100644 (file)
@@ -1044,7 +1044,7 @@ class TcaSelectItems extends AbstractItemProvider implements FormDataProviderInt
             /** @var RelationHandler $relationHandler */
             $relationHandler = GeneralUtility::makeInstance(RelationHandler::class);
             $relationHandler->registerNonTableValues = !empty($fieldConfig['config']['allowNonIdValues']);
-            if (isset($fieldConfig['config']['MM']) && !empty($fieldConfig['config']['MM'])) {
+            if (!empty($fieldConfig['config']['MM']) && $result['command'] !== 'new') {
                 // MM relation
                 $relationHandler->start(
                     $currentDatabaseValues,
index e789e2a..54cc42d 100644 (file)
@@ -2467,49 +2467,68 @@ 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
+     *
+     * @return array
      */
-    public function correctValuesForMmRelationWithSingleValueAllowedDataProvider()
+    public function processSelectFieldSetsCorrectValuesForMmRelationsDataProvider()
     {
         return array(
+            'Relation with MM table and new status with default values' => [
+                [
+                    'tableName' => 'aTable',
+                    'command' => 'new',
+                    'databaseRow' => [
+                        'uid' => 'NEW1234',
+                        'aField' => '24,35',
+                    ],
+                    'processedTca' => [
+                        'columns' => [
+                            'aField' => [
+                                'config' => [
+                                    'type' => 'select',
+                                    'maxitems' => 999,
+                                    'MM' => 'mm_aTable_foreignTable',
+                                    'foreign_table' => 'foreignTable',
+                                    'items' => [],
+                                ],
+                            ],
+                        ],
+                    ],
+                ],
+                [
+                    'MM' => ''
+                ],
+                [
+                    24, 35
+                ]
+            ],
+            'Relation with MM table and item array in list but no new status' => [
+                [
+                    'tableName' => 'aTable',
+                    'databaseRow' => [
+                        'uid' => 'NEW1234',
+                        'aField' => '24,35',
+                    ],
+                    'processedTca' => [
+                        'columns' => [
+                            'aField' => [
+                                'config' => [
+                                    'type' => 'select',
+                                    'maxitems' => 999,
+                                    'MM' => 'mm_aTable_foreignTable',
+                                    'foreign_table' => 'foreignTable',
+                                    'items' => [],
+                                ],
+                            ],
+                        ],
+                    ],
+                ],
+                [],
+                []
+            ],
             'Relation with MM table and maxitems = 1 processes field value (item count)' => [
                 [
                     'tableName' => 'aTable',
@@ -2532,6 +2551,7 @@ class TcaSelectItemsTest extends UnitTestCase
                         ],
                     ],
                 ],
+                [],
                 [
                     24
                 ]
@@ -2558,9 +2578,49 @@ class TcaSelectItemsTest extends UnitTestCase
                         ],
                     ],
                 ],
-                [
-                ]
+                [],
+                []
             ]
         );
     }
+
+    /**
+     * @test
+     * @dataProvider processSelectFieldSetsCorrectValuesForMmRelationsDataProvider
+     *
+     * @param array $input
+     * @param array $overrideRelationHandlerSettings
+     * @param array $relationHandlerUids
+     */
+    public function processSelectFieldSetsCorrectValuesForMmRelations(array $input, array $overrideRelationHandlerSettings, array $relationHandlerUids)
+    {
+        $field = $input['databaseRow']['aField'];
+        $foreignTable = isset($overrideRelationHandlerSettings['foreign_table']) ? $overrideRelationHandlerSettings['foreign_table'] : $input['processedTca']['columns']['aField']['config']['foreign_table'];
+        $mmTable = isset($overrideRelationHandlerSettings['MM']) ? $overrideRelationHandlerSettings['MM'] : $input['processedTca']['columns']['aField']['config']['MM'];
+        $uid = $input['databaseRow']['uid'];
+        $tableName = $input['tableName'];
+        $fieldConfig = $input['processedTca']['columns']['aField']['config'];
+
+        $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();
+
+        /** @var RelationHandler|ObjectProphecy $relationHandlerProphecy */
+        $relationHandlerProphecy = $this->prophesize(RelationHandler::class);
+        GeneralUtility::addInstance(RelationHandler::class, $relationHandlerProphecy->reveal());
+
+        $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));
+    }
 }