[BUGFIX] Correctly deprecate globals usage in FormEngine 43/56443/4
authorNicole Cordes <typo3@cordes.co>
Sat, 24 Mar 2018 19:03:21 +0000 (20:03 +0100)
committerAlexander Opitz <opitz.alexander@googlemail.com>
Fri, 30 Mar 2018 20:43:47 +0000 (22:43 +0200)
Resolves: #84530
Releases: master
Change-Id: Idb8fc143d7126840f0d964697b2a6dbe71e72cb3
Reviewed-on: https://review.typo3.org/56443
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Joerg Boesche <typo3@joergboesche.de>
Reviewed-by: Tobi Kretschmann <tobi@tobishome.de>
Reviewed-by: Steffen Frese <steffenf14@gmail.com>
Reviewed-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Tested-by: Stefan Neufeind <typo3.neufeind@speedpartner.de>
Reviewed-by: Alexander Opitz <opitz.alexander@googlemail.com>
Tested-by: Alexander Opitz <opitz.alexander@googlemail.com>
typo3/sysext/backend/Classes/Form/FormDataProvider/DatabaseRowInitializeNew.php
typo3/sysext/backend/Tests/UnitDeprecated/Form/FormDataProvider/DatabaseRowInitializeNewTest.php [new file with mode: 0644]
typo3/sysext/core/Documentation/Changelog/master/Deprecation-84530-DefaultValuesFromGlobalsDeprecatedInFormEngine.rst [new file with mode: 0644]

index 4ce2c47..5eeb7fd 100644 (file)
@@ -131,6 +131,7 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
      */
     protected function setDefaultsFromDefaultValues(array $result)
     {
+        $result = $this->setDefaultValuesFromGetPost($result);
         $tableName = $result['tableName'];
         $defaultValues = $result['defaultValues'] ?? [];
         if (isset($defaultValues[$tableName]) && is_array($defaultValues[$tableName])) {
@@ -144,6 +145,30 @@ class DatabaseRowInitializeNew implements FormDataProviderInterface
     }
 
     /**
+     * @param array $result
+     * @return array
+     * @deprecated since v9 will be removed in v10 - see $result['defaultValues']
+     */
+    protected function setDefaultValuesFromGetPost(array $result)
+    {
+        if (!empty($result['defaultValues'])) {
+            return $result;
+        }
+
+        $defaultValues = GeneralUtility::_GP('defVals');
+        if (!empty($defaultValues)) {
+            trigger_error(
+                'Default values for new database rows should be set from controller context. Applying default values'
+                . ' via GET/POST parameters is deprecated since 9.2 and will be removed in version 10',
+                \E_USER_DEPRECATED
+            );
+            $result['defaultValues'] = $defaultValues;
+        }
+
+        return $result;
+    }
+
+    /**
      * Inline scenario if a new intermediate record to an existing child-child is
      * compiled. Set "foreign_selector" field of this intermediate row to given
      * "childChildUid". See TcaDataCompiler array comment of inlineChildChildUid
diff --git a/typo3/sysext/backend/Tests/UnitDeprecated/Form/FormDataProvider/DatabaseRowInitializeNewTest.php b/typo3/sysext/backend/Tests/UnitDeprecated/Form/FormDataProvider/DatabaseRowInitializeNewTest.php
new file mode 100644 (file)
index 0000000..a9fe258
--- /dev/null
@@ -0,0 +1,232 @@
+<?php
+declare(strict_types=1);
+namespace TYPO3\CMS\Backend\Tests\UnitDeprecated\Form\FormDataProvider;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew;
+use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
+
+/**
+ * Test case for TcaFlexPrepare to render the functionality when a TCA migration happened
+ */
+class DatabaseRowInitializeNewTest extends UnitTestCase
+{
+    /**
+     * @test
+     */
+    public function addDataSetsDefaultDataFromGetIfColumnIsDefinedInTca()
+    {
+        $input = [
+            'command' => 'new',
+            'tableName' => 'aTable',
+            'vanillaUid' => 23,
+            'neighborRow' => null,
+            'inlineChildChildUid' => null,
+            'isInlineChild' => false,
+            'databaseRow' => [],
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [],
+                ],
+            ],
+        ];
+        $GLOBALS['_GET'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'getValue',
+                ],
+            ],
+        ];
+        $expected = [
+            'aField' => 'getValue',
+            'pid' => 23,
+        ];
+        $result = (new DatabaseRowInitializeNew())->addData($input);
+        $this->assertSame($expected, $result['databaseRow']);
+    }
+
+    /**
+     * @test
+     */
+    public function addDataSetsDefaultDataFromPostIfColumnIsDefinedInTca()
+    {
+        $input = [
+            'command' => 'new',
+            'tableName' => 'aTable',
+            'vanillaUid' => 23,
+            'neighborRow' => null,
+            'inlineChildChildUid' => null,
+            'isInlineChild' => false,
+            'databaseRow' => [],
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [],
+                ],
+            ],
+        ];
+        $GLOBALS['_POST'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'postValue',
+                ],
+            ],
+        ];
+        $expected = [
+            'aField' => 'postValue',
+            'pid' => 23,
+        ];
+        $result = (new DatabaseRowInitializeNew())->addData($input);
+        $this->assertSame($expected, $result['databaseRow']);
+    }
+
+    /**
+     * @test
+     */
+    public function addDataSetsPrioritizesDefaultPostOverDefaultGet()
+    {
+        $input = [
+            'command' => 'new',
+            'tableName' => 'aTable',
+            'vanillaUid' => 23,
+            'neighborRow' => null,
+            'inlineChildChildUid' => null,
+            'isInlineChild' => false,
+            'databaseRow' => [],
+            'processedTca' => [
+                'columns' => [
+                    'aField' => [],
+                ],
+            ],
+        ];
+        $GLOBALS['_GET'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'getValue',
+                ],
+            ],
+        ];
+        $GLOBALS['_POST'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'postValue',
+                ],
+            ],
+        ];
+        $expected = [
+            'aField' => 'postValue',
+            'pid' => 23,
+        ];
+        $result = (new DatabaseRowInitializeNew())->addData($input);
+        $this->assertSame($expected, $result['databaseRow']);
+    }
+
+    /**
+     * @test
+     */
+    public function addDataDoesNotSetDefaultDataFromGetPostIfColumnIsMissingInTca()
+    {
+        $input = [
+            'command' => 'new',
+            'tableName' => 'aTable',
+            'vanillaUid' => 23,
+            'neighborRow' => null,
+            'inlineChildChildUid' => null,
+            'isInlineChild' => false,
+            'databaseRow' => [],
+            'userTsConfig' => [
+                'TCAdefaults.' => [
+                    'aTable.' => [
+                        'aField' => 'pageTsValue',
+                    ],
+                ],
+            ],
+            'processedTca' => [
+                'columns' => [],
+            ],
+        ];
+        $GLOBALS['_GET'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'getValue',
+                ],
+            ],
+        ];
+        $GLOBALS['_POST'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'postValue',
+                ],
+            ],
+        ];
+        $expected = [
+            'pid' => 23,
+        ];
+        $result = (new DatabaseRowInitializeNew())->addData($input);
+        $this->assertSame($expected, $result['databaseRow']);
+    }
+
+    /**
+     * @test
+     */
+    public function addDataSetsDefaultDataOverrulingGetPost()
+    {
+        $input = [
+            'command' => 'new',
+            'tableName' => 'aTable',
+            'vanillaUid' => 23,
+            'inlineChildChildUid' => null,
+            'isInlineChild' => false,
+            'databaseRow' => [],
+            'neighborRow' => [
+                'aField' => 'valueFromNeighbor',
+            ],
+            'pageTsConfig' => [
+                'TCAdefaults.' => [
+                    'aTable.' => [
+                        'aField' => 'pageTsValue',
+                    ],
+                ],
+            ],
+            'userTsConfig' => [
+                'TCAdefaults.' => [
+                    'aTable.' => [
+                        'aField' => 'userTsValue',
+                    ],
+                ],
+            ],
+            'processedTca' => [
+                'ctrl' => [
+                    'useColumnsForDefaultValues' => 'aField',
+                ],
+                'columns' => [
+                    'aField' => [],
+                ],
+            ],
+        ];
+        $GLOBALS['_POST'] = [
+            'defVals' => [
+                'aTable' => [
+                    'aField' => 'postValue',
+                ],
+            ],
+        ];
+        $expected = [
+            'aField' => 'postValue',
+            'pid' => 23,
+        ];
+        $result = (new DatabaseRowInitializeNew())->addData($input);
+        $this->assertSame($expected, $result['databaseRow']);
+    }
+}
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-84530-DefaultValuesFromGlobalsDeprecatedInFormEngine.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-84530-DefaultValuesFromGlobalsDeprecatedInFormEngine.rst
new file mode 100644 (file)
index 0000000..89fa59d
--- /dev/null
@@ -0,0 +1,36 @@
+.. include:: ../../Includes.txt
+
+==========================================================================
+Deprecation: #84530 - Default values from globals deprecated in FormEngine
+==========================================================================
+
+See :issue:`84530`
+
+Description
+===========
+
+Setting default values for new database records from GET/POST `defVals` parameter
+has been deprecated in 9.2 and will be removed in version 10.
+
+
+Impact
+======
+
+If not already provided within the new configuration setting `$result['defaultValues']`, the
+default values are applied from GET/POST `defVals` configuration, but will trigger a
+deprecation warning.
+
+
+Affected Installations
+======================
+
+Installations that use the FormEngine within extensions might need to be changed.
+
+
+Migration
+=========
+
+Use the `defaultValues` configuration to set default values for new database rows
+ in the \TYPO3\CMS\Backend\Form\FormDataCompiler::compile call.
+
+.. index:: Backend, PHP-API, NotScanned