diff --git a/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php b/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php index dba8564..040befe 100644 --- a/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php +++ b/core/lib/Drupal/Core/Database/DatabaseDriverDiscovery.php @@ -8,23 +8,27 @@ class DatabaseDriverDiscovery { /** - * Directory locations for database drivers. - * - * See composer.json for discoverable namespaces. + * Discovered database drivers. * * @var array */ - protected static $discoveryLocations = [ - 'Drupal\\Core\\Database\\Driver' => __DIR__ . '/Driver', - 'Drupal\\Driver\\Database' => __DIR__ . '/../../../../../drivers/lib/Drupal/Driver/Database', - ]; + protected static $discoveredDrivers; /** - * Discovered database drivers. + * Returns locations of directories for database drivers discovery. * - * @var array + * See composer.json for discoverable namespaces. + * + * @return array + * An associative array with key the possible drivers namespaces, and value + * the directory where the database driver code should be located. */ - protected static $discoveredDrivers; + protected function getDiscoveryLocations() { + return [ + 'Drupal\\Core\\Database\\Driver' => __DIR__ . '/Driver', + 'Drupal\\Driver\\Database' => __DIR__ . '/../../../../../drivers/lib/Drupal/Driver/Database', + ]; + } /** * Returns the installer name of the installable database drivers. @@ -91,6 +95,9 @@ public function getInstaller($driver_name) { * - 'connectionClass' => the FQCN of the Connection class for this driver; * - 'installer' => the database driver installer object; * - 'installable' => whether the database driver can be installed. + * + * @throws \RuntimeException + * When there are duplicated driver names. */ public function discoverDatabaseDrivers() { if (isset(static::$discoveredDrivers)) { @@ -100,13 +107,13 @@ public function discoverDatabaseDrivers() { // Scan the discovery locations and build an array of candidate discovered // drivers. $candidate_drivers = []; - foreach (static::$discoveryLocations as $namespace => $location) { + foreach ($this->getDiscoveryLocations() as $namespace => $location) { if (is_dir($location) && ($handle = @opendir($location))) { // Each database driver name should correspond to a subdirectory of the // location being scanned. while (($driver_name = readdir($handle)) != FALSE) { // Skip this file if it starts with a dot. - if ($driver_name[0] != '.') { + if ($driver_name[0] != '.' && is_dir($location . '/' . $driver_name)) { if (isset($candidate_drivers[$driver_name])) { throw new \RuntimeException("Duplicate database driver name found: " . $driver_name); } @@ -120,21 +127,33 @@ public function discoverDatabaseDrivers() { // Use class_exist to check that actual classes for Connection and // Install\Tasks are defined and can be loaded. foreach ($candidate_drivers as $driver_name => $namespace) { - static::$discoveredDrivers[$driver_name]['namespace'] = $namespace; - $connection_class = $namespace . '\\Connection'; - if (class_exists($connection_class)) { - static::$discoveredDrivers[$driver_name]['connectionClass'] = $connection_class; + if (!class_exists($connection_class)) { + continue; } - + static::$discoveredDrivers[$driver_name]['namespace'] = $namespace; + static::$discoveredDrivers[$driver_name]['connectionClass'] = $connection_class; $install_tasks_class = $namespace . '\\Install\\Tasks'; if (class_exists($install_tasks_class)) { $installer = new $install_tasks_class(); static::$discoveredDrivers[$driver_name]['installer'] = $installer; static::$discoveredDrivers[$driver_name]['installable'] = $installer->installable(); } + else { + static::$discoveredDrivers[$driver_name]['installer'] = NULL; + static::$discoveredDrivers[$driver_name]['installable'] = FALSE; + } } return static::$discoveredDrivers; } + + /** + * Resets the static list of discovered database drivers. + * + * Should be only used for testing purposes. + */ + public function resetDiscoveredDatabaseDrivers() { + static::$discoveredDrivers = NULL; + } } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php new file mode 100644 index 0000000..ce125ef --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseDriverDiscoveryTest.php @@ -0,0 +1,92 @@ +getMockBuilder(DatabaseDriverDiscovery::class); + $this->databaseDriverDiscovery = $mock_builder + ->setMethods(['getDiscoveryLocations']) + ->getMock(); + $this->databaseDriverDiscovery + ->expects($this->any()) + ->method('getDiscoveryLocations') + ->will($this->returnValue([ + 'Drupal\\Core\\Database\\Driver' => __DIR__ . '/../../../../../lib/Drupal/Core/Database/Driver', + 'Drupal\\Tests\\Core\\Database' => __DIR__ . '/../../../Tests/Core/Database', + ])); + + // Reset the static list of drivers to allow rescanning the locations + // during the test. + $this->databaseDriverDiscovery->resetDiscoveredDatabaseDrivers(); + } + + /** + * @covers ::getInstallableDriversInstallerNames + */ + public function testGetInstallableDriversInstallerNames() { + $expected = [ + 'mysql' => 'MySQL, MariaDB, Percona Server, or equivalent', + 'sqlite' => 'SQLite', + 'pgsql' => 'PostgreSQL', + ]; + $this->assertEquals($expected, $this->databaseDriverDiscovery->getInstallableDriversInstallerNames()); + } + + /** + * @covers ::getInstallableDriversInstallers + */ + public function testGetInstallableDriversInstallers() { + $installable_drivers_installers = $this->databaseDriverDiscovery->getInstallableDriversInstallers(); + + $this->assertInstanceOf(MySqlTasks::class, $installable_drivers_installers['mysql']); + $this->assertInstanceOf(SqliteTasks::class, $installable_drivers_installers['sqlite']); + $this->assertInstanceOf(PgSqlTasks::class, $installable_drivers_installers['pgsql']); + } + + /** + * @covers ::discoverDatabaseDrivers + */ + public function testDiscoverDatabaseDrivers() { + $drivers = $this->databaseDriverDiscovery->discoverDatabaseDrivers(); + + // The 'Stub' driver should have been discovered. + $this->assertArrayHasKey('mysql', $drivers); + $this->assertArrayHasKey('pgsql', $drivers); + $this->assertArrayHasKey('sqlite', $drivers); + $this->assertArrayHasKey('Stub', $drivers); + + // The 'Stub' driver is not installable. + $this->assertFalse($drivers['Stub']['installable']); + + // The 'Stub' driver installer has a name. + $this->assertEquals('Stub database for testing', $drivers['Stub']['installer']->name()); + } +} diff --git a/core/tests/Drupal/Tests/Core/Database/Stub/Install/Tasks.php b/core/tests/Drupal/Tests/Core/Database/Stub/Install/Tasks.php new file mode 100644 index 0000000..6b0d733 --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Database/Stub/Install/Tasks.php @@ -0,0 +1,26 @@ +