Unverified Commit c3b10697 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3118832 by effulgentsia, alexpott, daffie: Allow custom database...

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 c4f0d962)
parent 39d5abeb
Loading
Loading
Loading
Loading
+2 −2
Changes for core/includes/install.inc: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -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();
  }
}
+6 −6
Changes for core/lib/Drupal/Core/Database/Database.php: 6 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -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");
      }
    }

+68 −0
Changes for core/tests/Drupal/Tests/Core/Database/InstallerObjectTest.php: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?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],
    ];
  }

}
+16 −1
Changes for core/tests/Drupal/Tests/Core/Database/UrlConversionTest.php: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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',
        ],
      ],
    ];
  }

+14 −0
Changes for core/tests/fixtures/database_drivers/core/corefake/Connection.php: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\Core\Database\Driver\corefake;

use Drupal\Driver\Database\fake\Connection as BaseConnection;

class Connection extends BaseConnection {

  /**
   * {@inheritdoc}
   */
  public $driver = 'corefake';

}
Loading