Unverified Commit 8fd18feb authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3450706 by daffie, smustgrave, alexpott, quietone, mondrake: Let for...

Issue #3450706 by daffie, smustgrave, alexpott, quietone, mondrake: Let for all database drivers the module name setting default to the driver name
parent c9c39665
Loading
Loading
Loading
Loading
Loading
+4 −9
Original line number Diff line number Diff line
@@ -521,16 +521,11 @@ public static function convertDbUrlToConnectionInfo($url, $root, ?bool $include_
    $url_component_query = $url_components['query'] ?? '';
    parse_str($url_component_query, $query);

    // Add the module key for core database drivers when the module key is not
    // set.
    if (!isset($query['module']) && in_array($driverName, ['mysql', 'pgsql', 'sqlite'], TRUE)) {
      $query['module'] = $driverName;
    }
    if (!isset($query['module'])) {
      throw new \InvalidArgumentException("Can not convert '$url' to a database connection, the module providing the driver '{$driverName}' is not specified");
    }
    // Use the driver name as the module name when the module name is not
    // provided.
    $module = $query['module'] ?? $driverName;

    $driverNamespace = "Drupal\\{$query['module']}\\Driver\\Database\\{$driverName}";
    $driverNamespace = "Drupal\\{$module}\\Driver\\Database\\{$driverName}";

    /** @var \Drupal\Core\Extension\DatabaseDriver $driver */
    $driver = self::getDriverList()
+7 −0
Original line number Diff line number Diff line
name: 'DummyDB'
type: module
description: 'Fake database driver for DummyDB.'
package: Testing
version: VERSION
dependencies:
  - drupal:mysql
+30 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

// cspell:ignore dummydb

namespace Drupal\dummydb\Driver\Database\dummydb;

use Drupal\mysql\Driver\Database\mysql\Connection as CoreConnection;

/**
 * DummyDB test implementation of \Drupal\Core\Database\Connection.
 */
class Connection extends CoreConnection {

  /**
   * {@inheritdoc}
   */
  public function driver() {
    return 'dummydb';
  }

  /**
   * {@inheritdoc}
   */
  public function databaseType() {
    return 'dummydb';
  }

}
+23 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

// cspell:ignore dummydb

namespace Drupal\dummydb\Driver\Database\dummydb\Install;

use Drupal\mysql\Driver\Database\mysql\Install\Tasks as CoreTasks;

/**
 * Specifies installation tasks for DummyDB test database.
 */
class Tasks extends CoreTasks {

  /**
   * {@inheritdoc}
   */
  public function name() {
    return t('DummyDB');
  }

}
+26 −4
Original line number Diff line number Diff line
@@ -8,6 +8,8 @@
use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Tests\UnitTestCase;

// cspell:ignore dummydb

/**
 * Tests for database URL to/from database connection array conversions.
 *
@@ -297,14 +299,34 @@ public static function providerInvalidArgumentsUrlConversion() {
    return [
      ['foo', '', "Missing scheme in URL 'foo'"],
      ['foo', 'bar', "Missing scheme in URL 'foo'"],
      ['foo://', 'bar', "Can not convert 'foo://' to a database connection, the module providing the driver 'foo' is not specified"],
      ['foo://bar', 'baz', "Can not convert 'foo://bar' to a database connection, the module providing the driver 'foo' is not specified"],
      ['foo://bar:port', 'baz', "Can not convert 'foo://bar:port' to a database connection, the module providing the driver 'foo' is not specified"],
      ['foo/bar/baz', 'bar2', "Missing scheme in URL 'foo/bar/baz'"],
      ['foo://bar:baz@test1', 'test2', "Can not convert 'foo://bar:baz@test1' to a database connection, the module providing the driver 'foo' is not specified"],
    ];
  }

  /**
   * Tests that connection URL with no module name defaults to driver name.
   */
  public function testNoModuleSpecifiedDefaultsToDriverName(): void {
    $url = 'dummydb://test_user:test_pass@test_host/test_database';
    $connection_info = Database::convertDbUrlToConnectionInfo($url, $this->root, TRUE);
    $expected = [
      'driver' => 'dummydb',
      'username' => 'test_user',
      'password' => 'test_pass',
      'host' => 'test_host',
      'database' => 'test_database',
      'namespace' => 'Drupal\dummydb\Driver\Database\dummydb',
      'autoload' => 'core/modules/system/tests/modules/dummydb/src/Driver/Database/dummydb/',
      'dependencies' => [
        'mysql' => [
          'namespace' => 'Drupal\mysql',
          'autoload' => 'core/modules/mysql/src/',
        ],
      ],
    ];
    $this->assertSame($expected, $connection_info);
  }

  /**
   * @covers ::getConnectionInfoAsUrl
   *