[CLEANUP] Minor code improvements in DatabaseRowInitializeNew 93/43293/3
authorLoek Hilgersom <loek@netcoop.nl>
Mon, 14 Sep 2015 11:41:19 +0000 (13:41 +0200)
committerChristian Kuhn <lolli@schwarzbu.ch>
Tue, 15 Sep 2015 09:12:13 +0000 (11:12 +0200)
Removes a condition checking if values are set before overwriting:
just changing the order will do.
Removes a non-functional isset check.

Resolves: #69781
Releases: master
Change-Id: I3cdf7a1f54d2e237df3fa90d7733160d3cda4d42
Reviewed-on: http://review.typo3.org/43293
Reviewed-by: Nicole Cordes <typo3@cordes.co>
Tested-by: Nicole Cordes <typo3@cordes.co>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php
typo3/sysext/backend/Tests/Unit/Form/FormDataProvider/DatabaseRowInitializeNewTest.php

index a375f28..f41c769 100644 (file)
@@ -24,6 +24,7 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface {
 
        /**
         * Initialize new row with default values from various sources
+        * There are 4 sources of default values. Mind the order, the last takes precedence.
         *
         * @param array $result
         * @return array
@@ -60,38 +61,34 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface {
                        }
                }
 
-               // Apply default values from GET / POST
-               // @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7
-               $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
-               if (isset($defaultValuesFromGetPost[$tableName])
-                       && is_array($defaultValuesFromGetPost[$tableName])
-               ) {
-                       foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
-                               if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
-                                       $databaseRow[$fieldName] = $fieldValue;
-                               }
-                       }
-               }
-
                // If a neighbor row is given (if vanillaUid was negative), field can be initialized with values
                // from neighbor for fields registered in TCA['ctrl']['useColumnsForDefaultValues'].
                if (is_array($result['neighborRow'])
-                       && isset($result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'])
                        && !empty($result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'])
                ) {
                        $defaultColumns = GeneralUtility::trimExplode(',', $result['vanillaTableTca']['ctrl']['useColumnsForDefaultValues'], TRUE);
                        foreach ($defaultColumns as $fieldName) {
                                if (isset($result['vanillaTableTca']['columns'][$fieldName])
                                        && isset($result['neighborRow'][$fieldName])
-                                       // Set only if not initialized by other default
-                                       // @todo: Why shouldn't neighbor override a different default, where is that useful?
-                                       && !isset($databaseRow[$fieldName])
                                ) {
                                        $databaseRow[$fieldName] = $result['neighborRow'][$fieldName];
                                }
                        }
                }
 
+               // Apply default values from GET / POST
+               // @todo: Fetch this stuff from request object as soon as modules were moved to PSR-7
+               $defaultValuesFromGetPost = GeneralUtility::_GP('defVals');
+               if (isset($defaultValuesFromGetPost[$tableName])
+                       && is_array($defaultValuesFromGetPost[$tableName])
+               ) {
+                       foreach ($defaultValuesFromGetPost[$tableName] as $fieldName => $fieldValue) {
+                               if (isset($result['vanillaTableTca']['columns'][$fieldName])) {
+                                       $databaseRow[$fieldName] = $fieldValue;
+                               }
+                       }
+               }
+
                // Set pid to vanillaUid. This means, it *can* be negative, if the record is added relative to another record
                $databaseRow['pid'] = $result['vanillaUid'];
 
index 7f4a6d4..5da96bb 100644 (file)
@@ -170,6 +170,115 @@ class DatabaseRowInitializeNewTest extends UnitTestCase {
        /**
         * @test
         */
+       public function addDataSetsDefaultDataOverrulingFormPageTs() {
+               $input = [
+                       'command' => 'new',
+                       'tableName' => 'aTable',
+                       'vanillaUid' => 23,
+                       'databaseRow' => [],
+                       'pageTsConfig' => [
+                               'TCAdefaults.' => [
+                                       'aTable.' => [
+                                               'aField' => 'pageTsValue',
+                                       ],
+                               ],
+                       ],
+                       'userTsConfig' => [
+                               'TCAdefaults.' => [
+                                       'aTable.' => [
+                                               'aField' => 'userTsValue',
+                                       ],
+                               ],
+                       ],
+                       'vanillaTableTca' => [
+                               'columns' => [
+                                       'aField' => [],
+                               ],
+                       ]
+               ];
+               $expected = [
+                       'aField' => 'pageTsValue',
+                       'pid' => 23,
+               ];
+               $result = $this->subject->addData($input);
+               $this->assertSame($expected, $result['databaseRow']);
+       }
+
+       /**
+        * @test
+        */
+       public function addDataSetsDefaultFromNeighborRow() {
+               $input = [
+                       'command' => 'new',
+                       'tableName' => 'aTable',
+                       'vanillaUid' => 23,
+                       'databaseRow' => [],
+                       'neighborRow' => [
+                               'aField' => 'valueFromNeighbor',
+                       ],
+                       'vanillaTableTca' => [
+                               'ctrl' => [
+                                       'useColumnsForDefaultValues' => 'aField',
+                               ],
+                               'columns' => [
+                                       'aField' => [],
+                               ],
+                       ],
+               ];
+               $expected = [
+                       'aField' => 'valueFromNeighbor',
+                       'pid' => 23,
+               ];
+               $result = $this->subject->addData($input);
+               $this->assertSame($expected, $result['databaseRow']);
+       }
+
+       /**
+        * @test
+        */
+       public function addDataSetsDefaultDataOverrulingFromNeighborRow() {
+               $input = [
+                       'command' => 'new',
+                       'tableName' => 'aTable',
+                       'vanillaUid' => 23,
+                       'databaseRow' => [],
+                       'neighborRow' => [
+                               'aField' => 'valueFromNeighbor',
+                       ],
+                       'pageTsConfig' => [
+                               'TCAdefaults.' => [
+                                       'aTable.' => [
+                                               'aField' => 'pageTsValue',
+                                       ],
+                               ],
+                       ],
+                       'userTsConfig' => [
+                               'TCAdefaults.' => [
+                                       'aTable.' => [
+                                               'aField' => 'userTsValue',
+                                       ],
+                               ],
+                       ],
+                       'vanillaTableTca' => [
+                               'ctrl' => [
+                                       'useColumnsForDefaultValues' => 'aField',
+                               ],
+                               'columns' => [
+                                       'aField' => [],
+                               ],
+                       ],
+               ];
+               $expected = [
+                       'aField' => 'valueFromNeighbor',
+                       'pid' => 23,
+               ];
+               $result = $this->subject->addData($input);
+               $this->assertSame($expected, $result['databaseRow']);
+       }
+
+       /**
+        * @test
+        */
        public function addDataSetsDefaultDataFormGetIfColumnIsDenfinedInTca() {
                $input = [
                        'command' => 'new',
@@ -308,7 +417,7 @@ class DatabaseRowInitializeNewTest extends UnitTestCase {
        /**
         * @test
         */
-       public function addDataSetsDefaultFromNeighborRow() {
+       public function addDataSetsDefaultDataOverrulingGetPost() {
                $input = [
                        'command' => 'new',
                        'tableName' => 'aTable',
@@ -317,34 +426,19 @@ class DatabaseRowInitializeNewTest extends UnitTestCase {
                        'neighborRow' => [
                                'aField' => 'valueFromNeighbor',
                        ],
-                       'vanillaTableTca' => [
-                               'ctrl' => [
-                                       'useColumnsForDefaultValues' => 'aField',
-                               ],
-                               'columns' => [
-                                       'aField' => [],
+                       'pageTsConfig' => [
+                               'TCAdefaults.' => [
+                                       'aTable.' => [
+                                               'aField' => 'pageTsValue',
+                                       ],
                                ],
                        ],
-               ];
-               $expected = [
-                       'aField' => 'valueFromNeighbor',
-                       'pid' => 23,
-               ];
-               $result = $this->subject->addData($input);
-               $this->assertSame($expected, $result['databaseRow']);
-       }
-
-       /**
-        * @test
-        */
-       public function addDataSetsDoesNotOverrideDefaultFromNeighborRowIfOtherDefaultHasSetDataAlready() {
-               $input = [
-                       'command' => 'new',
-                       'tableName' => 'aTable',
-                       'vanillaUid' => 23,
-                       'databaseRow' => [],
-                       'neighborRow' => [
-                               'aField' => 'valueFromNeighbor',
+                       'userTsConfig' => [
+                               'TCAdefaults.' => [
+                                       'aTable.' => [
+                                               'aField' => 'userTsValue',
+                                       ],
+                               ],
                        ],
                        'vanillaTableTca' => [
                                'ctrl' => [