[BUGFIX] Prevent enforcing opened database connection with initCommands 48/55548/8
authorNicole Cordes <typo3@cordes.co>
Sun, 4 Feb 2018 11:20:05 +0000 (12:20 +0100)
committerHelmut Hummel <typo3@helhum.io>
Sun, 4 Feb 2018 22:10:28 +0000 (23:10 +0100)
This patch moves the execution of configured initCommands to the end
of connect function. This prevents a database connection from being
opened by default due to the execution of commands right after class
initialization and still ensures a connection is configured properly
before first command execution.

Furthermore this patch adds a new functional test for the ErrorHandler.
The test uses some predefined initCommands to enforce the strict mode
of MySQL database to be set. Due to the predefined initCommands the
test failed before as the connection was forced to be opened. For
further information see the referenced forge ticket.

Resolves: #83770
Releases: master, 8.7
Change-Id: I2287600959b3ec06d338e26373562850307ce0b1
Reviewed-on: https://review.typo3.org/55548
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Helmut Hummel <typo3@helhum.io>
Tested-by: Helmut Hummel <typo3@helhum.io>
typo3/sysext/core/Classes/Database/Connection.php
typo3/sysext/core/Tests/Functional/Error/ErrorHandlerTest.php [new file with mode: 0644]

index efcc4e0..c96f272 100644 (file)
@@ -68,6 +68,11 @@ class Connection extends \Doctrine\DBAL\Connection implements LoggerAwareInterfa
     const PARAM_BOOL = \PDO::PARAM_BOOL; // 5
 
     /**
+     * @var array
+     */
+    private $prepareConnectionCommands = [];
+
+    /**
      * Prevents the duplicate registration of event handlers and types for this connection.
      * If true the events will not be initialized any more in the connect() method.
      *
@@ -152,6 +157,12 @@ class Connection extends \Doctrine\DBAL\Connection implements LoggerAwareInterfa
 
         $this->customTypesAndEventsInitialized = true;
 
+        foreach ($this->prepareConnectionCommands as $command) {
+            if ($this->executeUpdate($command) === false) {
+                $this->logger->critical('Could not initialize DB connection with query "' . $command . '": ' . $this->errorInfo());
+            }
+        }
+
         return true;
     }
 
@@ -468,7 +479,7 @@ class Connection extends \Doctrine\DBAL\Connection implements LoggerAwareInterfa
             return;
         }
 
-        $commandsToPerform = GeneralUtility::trimExplode(
+        $this->prepareConnectionCommands = GeneralUtility::trimExplode(
             LF,
             str_replace(
                 '\' . LF . \'',
@@ -477,12 +488,6 @@ class Connection extends \Doctrine\DBAL\Connection implements LoggerAwareInterfa
             ),
             true
         );
-
-        foreach ($commandsToPerform as $command) {
-            if ($this->executeUpdate($command) === false) {
-                $this->logger->critical('Could not initialize DB connection with query "' . $command . '": ' . $this->errorInfo());
-            }
-        }
     }
 
     /**
diff --git a/typo3/sysext/core/Tests/Functional/Error/ErrorHandlerTest.php b/typo3/sysext/core/Tests/Functional/Error/ErrorHandlerTest.php
new file mode 100644 (file)
index 0000000..91a7302
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Core\Tests\Functional\Error;
+
+/*
+ * 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!
+ */
+
+/**
+ * Testcase for class \TYPO3\CMS\Core\Error\ErrorHandler
+ */
+class ErrorHandlerTest extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
+{
+    /**
+     * @var array
+     */
+    protected $coreExtensionsToLoad = [
+        'workspaces',
+    ];
+
+    /**
+     * @var array
+     */
+    protected $configurationToUseInTestInstance = [
+        'DB' => [
+            'Connections' => [
+                'Default' => [
+                    'initCommands' => 'SET SESSION sql_mode = \'STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY\';',
+                ],
+            ],
+        ],
+    ];
+
+    /**
+     * @test
+     */
+    public function handleErrorFetchesDeprecations()
+    {
+        $this->assertTrue(\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('version'));
+    }
+}