From c3b1069774a8db00e573d32dfa15edfeefcd505c Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Tue, 17 Mar 2020 10:08:29 +0000
Subject: [PATCH] Issue #3118832 by effulgentsia, alexpott, daffie: Allow
 custom database drivers to extend and have the same name as the core ones

(cherry picked from commit c4f0d96291f898a01d473ef418a59d3d4616354e)
---
 core/includes/install.inc                     |  4 +-
 core/lib/Drupal/Core/Database/Database.php    | 12 ++--
 .../Core/Database/InstallerObjectTest.php     | 68 +++++++++++++++++++
 .../Tests/Core/Database/UrlConversionTest.php | 17 ++++-
 .../core/corefake/Connection.php              | 14 ++++
 .../core/corefake/Install/Tasks.php           | 16 +++++
 .../custom/corefake/Connection.php            |  7 ++
 .../custom/corefake/Install/Tasks.php         |  9 +++
 .../custom}/fake/Connection.php               |  0
 .../custom/fake/Install/Tasks.php             | 16 +++++
 10 files changed, 154 insertions(+), 9 deletions(-)
 create mode 100644 core/tests/Drupal/Tests/Core/Database/InstallerObjectTest.php
 create mode 100644 core/tests/fixtures/database_drivers/core/corefake/Connection.php
 create mode 100644 core/tests/fixtures/database_drivers/core/corefake/Install/Tasks.php
 create mode 100644 core/tests/fixtures/database_drivers/custom/corefake/Connection.php
 create mode 100644 core/tests/fixtures/database_drivers/custom/corefake/Install/Tasks.php
 rename core/tests/{Drupal/Tests/Core/Database/fixtures/driver => fixtures/database_drivers/custom}/fake/Connection.php (100%)
 create mode 100644 core/tests/fixtures/database_drivers/custom/fake/Install/Tasks.php

diff --git a/core/includes/install.inc b/core/includes/install.inc
index 2c5d4167cdb8..e8a53f4e7036 100644
--- a/core/includes/install.inc
+++ b/core/includes/install.inc
@@ -1178,12 +1178,12 @@ function install_profile_info($profile, $langcode = 'en') {
 function db_installer_object($driver) {
   // We cannot use Database::getConnection->getDriverClass() here, because
   // the connection object is not yet functional.
-  $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
+  $task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
   if (class_exists($task_class)) {
     return new $task_class();
   }
   else {
-    $task_class = "Drupal\\Driver\\Database\\{$driver}\\Install\\Tasks";
+    $task_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Install\\Tasks";
     return new $task_class();
   }
 }
diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php
index d13ea80862a3..173caf3ece54 100644
--- a/core/lib/Drupal/Core/Database/Database.php
+++ b/core/lib/Drupal/Core/Database/Database.php
@@ -457,14 +457,14 @@ public static function convertDbUrlToConnectionInfo($url, $root) {
     }
     $driver = $matches[1];
 
-    // Discover if the URL has a valid driver scheme. Try with core drivers
-    // first.
-    $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
+    // Discover if the URL has a valid driver scheme. Try with custom drivers
+    // first, since those can override/extend the core ones.
+    $connection_class = $custom_connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
     if (!class_exists($connection_class)) {
-      // If the URL is not relative to a core driver, try with custom ones.
-      $connection_class = "Drupal\\Driver\\Database\\{$driver}\\Connection";
+      // If the URL is not relative to a custom driver, try with core ones.
+      $connection_class = "Drupal\\Core\\Database\\Driver\\{$driver}\\Connection";
       if (!class_exists($connection_class)) {
-        throw new \InvalidArgumentException("Can not convert '$url' to a database connection, class '$connection_class' does not exist");
+        throw new \InvalidArgumentException("Can not convert '$url' to a database connection, class '$custom_connection_class' does not exist");
       }
     }
 
diff --git a/core/tests/Drupal/Tests/Core/Database/InstallerObjectTest.php b/core/tests/Drupal/Tests/Core/Database/InstallerObjectTest.php
new file mode 100644
index 000000000000..87cf3208a86e
--- /dev/null
+++ b/core/tests/Drupal/Tests/Core/Database/InstallerObjectTest.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Drupal\Tests\Core\Database;
+
+use Composer\Autoload\ClassLoader;
+use Drupal\Core\Database\Driver\mysql\Install\Tasks as MysqlInstallTasks;
+use Drupal\Driver\Database\fake\Install\Tasks as FakeInstallTasks;
+use Drupal\Driver\Database\corefake\Install\Tasks as CustomCoreFakeInstallTasks;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * Tests the db_installer_object() function that is used during installation.
+ *
+ * These tests run in isolation to prevent the autoloader additions from
+ * affecting other tests.
+ *
+ * @covers ::db_installer_object
+ *
+ * @runTestsInSeparateProcesses
+ * @preserveGlobalState disabled
+ *
+ * @group Database
+ */
+class InstallerObjectTest extends UnitTestCase {
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+    require_once __DIR__ . '/../../../../../includes/install.inc';
+    $additional_class_loader = new ClassLoader();
+    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/fake");
+    $additional_class_loader->addPsr4("Drupal\\Core\\Database\\Driver\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/core/corefake");
+    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/corefake");
+    $additional_class_loader->register(TRUE);
+  }
+
+  /**
+   * @dataProvider providerDbInstallerObject
+   */
+  public function testDbInstallerObject($driver, $expected_class_name) {
+    $object = db_installer_object($driver);
+    $this->assertEquals(get_class($object), $expected_class_name);
+  }
+
+  /**
+   * Dataprovider for testDbUrltoConnectionConversion().
+   *
+   * @return array
+   *   Array of arrays with the following elements:
+   *   - driver: The driver name.
+   *   - class: The fully qualified class name of the expected install task.
+   */
+  public function providerDbInstallerObject() {
+    return [
+      // A driver only in the core namespace.
+      ['mysql', MysqlInstallTasks::class],
+
+      // A driver only in the custom namespace.
+      ['fake', FakeInstallTasks::class],
+
+      // A driver in both namespaces. The custom one takes precedence.
+      ['corefake', CustomCoreFakeInstallTasks::class],
+    ];
+  }
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php b/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
index b48e5c05146b..8f9b9705b045 100644
--- a/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
+++ b/core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php
@@ -27,7 +27,9 @@ class UrlConversionTest extends UnitTestCase {
   protected function setUp() {
     parent::setUp();
     $additional_class_loader = new ClassLoader();
-    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/fixtures/driver/fake");
+    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\fake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/fake");
+    $additional_class_loader->addPsr4("Drupal\\Core\\Database\\Driver\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/core/corefake");
+    $additional_class_loader->addPsr4("Drupal\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/custom/corefake");
     $additional_class_loader->register(TRUE);
   }
 
@@ -127,6 +129,19 @@ public function providerConvertDbUrlToConnectionInfo() {
           'namespace' => 'Drupal\Driver\Database\fake',
         ],
       ],
+      'Fake core driver with custom override, without prefix' => [
+        '',
+        'corefake://fake_user:fake_pass@fake_host:3456/fake_database',
+        [
+          'driver' => 'corefake',
+          'username' => 'fake_user',
+          'password' => 'fake_pass',
+          'host' => 'fake_host',
+          'database' => 'fake_database',
+          'port' => 3456,
+          'namespace' => 'Drupal\Driver\Database\corefake',
+        ],
+      ],
     ];
   }
 
diff --git a/core/tests/fixtures/database_drivers/core/corefake/Connection.php b/core/tests/fixtures/database_drivers/core/corefake/Connection.php
new file mode 100644
index 000000000000..b3228a2137db
--- /dev/null
+++ b/core/tests/fixtures/database_drivers/core/corefake/Connection.php
@@ -0,0 +1,14 @@
+<?php
+
+namespace Drupal\Core\Database\Driver\corefake;
+
+use Drupal\Driver\Database\fake\Connection as BaseConnection;
+
+class Connection extends BaseConnection {
+
+  /**
+   * {@inheritdoc}
+   */
+  public $driver = 'corefake';
+
+}
diff --git a/core/tests/fixtures/database_drivers/core/corefake/Install/Tasks.php b/core/tests/fixtures/database_drivers/core/corefake/Install/Tasks.php
new file mode 100644
index 000000000000..4cb9083ffa19
--- /dev/null
+++ b/core/tests/fixtures/database_drivers/core/corefake/Install/Tasks.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Drupal\Core\Database\Driver\corefake\Install;
+
+use Drupal\Core\Database\Install\Tasks as InstallTasks;
+
+class Tasks extends InstallTasks {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function name() {
+    return 'corefake';
+  }
+
+}
diff --git a/core/tests/fixtures/database_drivers/custom/corefake/Connection.php b/core/tests/fixtures/database_drivers/custom/corefake/Connection.php
new file mode 100644
index 000000000000..4c5188085e70
--- /dev/null
+++ b/core/tests/fixtures/database_drivers/custom/corefake/Connection.php
@@ -0,0 +1,7 @@
+<?php
+
+namespace Drupal\Driver\Database\corefake;
+
+use Drupal\Core\Database\Driver\corefake\Connection as CoreFakeConnection;
+
+class Connection extends CoreFakeConnection {}
diff --git a/core/tests/fixtures/database_drivers/custom/corefake/Install/Tasks.php b/core/tests/fixtures/database_drivers/custom/corefake/Install/Tasks.php
new file mode 100644
index 000000000000..d131a3f1c7bc
--- /dev/null
+++ b/core/tests/fixtures/database_drivers/custom/corefake/Install/Tasks.php
@@ -0,0 +1,9 @@
+<?php
+
+namespace Drupal\Driver\Database\corefake\Install;
+
+use Drupal\Core\Database\Driver\corefake\Install\Tasks as BaseInstallTasks;
+
+class Tasks extends BaseInstallTasks {
+
+}
diff --git a/core/tests/Drupal/Tests/Core/Database/fixtures/driver/fake/Connection.php b/core/tests/fixtures/database_drivers/custom/fake/Connection.php
similarity index 100%
rename from core/tests/Drupal/Tests/Core/Database/fixtures/driver/fake/Connection.php
rename to core/tests/fixtures/database_drivers/custom/fake/Connection.php
diff --git a/core/tests/fixtures/database_drivers/custom/fake/Install/Tasks.php b/core/tests/fixtures/database_drivers/custom/fake/Install/Tasks.php
new file mode 100644
index 000000000000..2cd65190a50f
--- /dev/null
+++ b/core/tests/fixtures/database_drivers/custom/fake/Install/Tasks.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Drupal\Driver\Database\fake\Install;
+
+use Drupal\Core\Database\Install\Tasks as InstallTasks;
+
+class Tasks extends InstallTasks {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function name() {
+    return 'fake';
+  }
+
+}
-- 
GitLab