Verified Commit 4e42a4f9 authored by Dave Long's avatar Dave Long
Browse files

Issue #3506931 by mondrake, brandonlira, adwivedi008, daffie, charlliequadros,...

Issue #3506931 by mondrake, brandonlira, adwivedi008, daffie, charlliequadros, smustgrave: Deprecate passing $root to \Drupal\Core\Database\Connection::createConnectionOptionsFromUrl
parent 0ed12ae5
Loading
Loading
Loading
Loading
Loading
+6 −2
Original line number Diff line number Diff line
@@ -1315,8 +1315,8 @@ public function __sleep(): array {
   * @param string $url
   *   The URL.
   * @param string $root
   *   The root directory of the Drupal installation. Some database drivers,
   *   like for example SQLite, need this information.
   *   (deprecated) The root directory of the Drupal installation. Some
   *   database drivers, like for example SQLite, need this information.
   *
   * @return array
   *   The connection options.
@@ -1332,6 +1332,10 @@ public function __sleep(): array {
   * @see \Drupal\Core\Database\Database::convertDbUrlToConnectionInfo()
   */
  public static function createConnectionOptionsFromUrl($url, $root) {
    if ($root !== NULL) {
      @trigger_error("Passing the \$root value to " . __METHOD__ . "() is deprecated in drupal:11.2.0 and will be removed in drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3511287", E_USER_DEPRECATED);
    }

    $url_components = parse_url($url);
    if (!isset($url_components['scheme'], $url_components['host'], $url_components['path'])) {
      throw new \InvalidArgumentException("The database connection URL '$url' is invalid. The minimum requirement is: 'driver://host/database'");
+1 −1
Original line number Diff line number Diff line
@@ -559,7 +559,7 @@ public static function convertDbUrlToConnectionInfo($url, $root, ?bool $include_

    $additional_class_loader->register(TRUE);

    $options = $connection_class::createConnectionOptionsFromUrl($url, $root);
    $options = $connection_class::createConnectionOptionsFromUrl($url, NULL);

    // Add the necessary information to autoload code.
    // @see \Drupal\Core\Site\Settings::initialize()
+4 −1
Original line number Diff line number Diff line
@@ -442,7 +442,10 @@ public function getFullQualifiedTableName($table) {
   * {@inheritdoc}
   */
  public static function createConnectionOptionsFromUrl($url, $root) {
    $database = parent::createConnectionOptionsFromUrl($url, $root);
    if ($root !== NULL) {
      @trigger_error("Passing the \$root value to " . __METHOD__ . "() is deprecated in drupal:11.2.0 and will be removed in drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3511287", E_USER_DEPRECATED);
    }
    $database = parent::createConnectionOptionsFromUrl($url, NULL);

    // A SQLite database path with two leading slashes indicates a system path.
    // Otherwise the path is relative to the Drupal root.
+15 −2
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
use Drupal\sqlite\Driver\Database\sqlite\Connection;
use Drupal\Tests\Core\Database\Stub\StubPDO;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;

/**
 * @coversDefaultClass \Drupal\sqlite\Driver\Database\sqlite\Connection
@@ -24,9 +25,8 @@ class ConnectionTest extends UnitTestCase {
   *   Expected connection option.
   */
  public function testCreateConnectionOptionsFromUrl(string $url, string $expected): void {
    $root = dirname(__DIR__, 8);
    $sqlite_connection = new Connection($this->createMock(StubPDO::class), []);
    $database = $sqlite_connection->createConnectionOptionsFromUrl($url, $root);
    $database = $sqlite_connection->createConnectionOptionsFromUrl($url, NULL);
    $this->assertEquals('sqlite', $database['driver']);
    $this->assertEquals($expected, $database['database']);
  }
@@ -47,4 +47,17 @@ public static function providerCreateConnectionOptionsFromUrl(): array {
    ];
  }

  /**
   * Confirms deprecation of the $root argument.
   */
  #[IgnoreDeprecations]
  public function testDeprecationOfRootInConnectionOptionsFromUrl(): void {
    $this->expectDeprecation('Passing the $root value to Drupal\sqlite\Driver\Database\sqlite\Connection::createConnectionOptionsFromUrl() is deprecated in drupal:11.2.0 and will be removed in drupal:12.0.0. There is no replacement. See https://www.drupal.org/node/3511287');
    $root = dirname(__DIR__, 8);
    $sqlite_connection = new Connection($this->createMock(StubPDO::class), []);
    $database = $sqlite_connection->createConnectionOptionsFromUrl('sqlite://localhost/tmp/test', $root);
    $this->assertEquals('sqlite', $database['driver']);
    $this->assertEquals('tmp/test', $database['database']);
  }

}