diff --git a/core/lib/Drupal/Core/Database/Database.php b/core/lib/Drupal/Core/Database/Database.php index a799926..55f3501 100644 --- a/core/lib/Drupal/Core/Database/Database.php +++ b/core/lib/Drupal/Core/Database/Database.php @@ -443,4 +443,79 @@ public static function closeConnection($target = NULL, $key = NULL) { public static function ignoreTarget($key, $target) { self::$ignoreTargets[$key][$target] = TRUE; } + + /** + * Converts a URL to a database connection info array. + * + * @param string $url + * The URL. + * + * @return array + * The database connection info. + */ + public static function convertDbUrlToConnectionInfo($url) { + $info = parse_url($url); + if (!isset($info['scheme'], $info['host'], $info['path'])) { + throw new \InvalidArgumentException('Invalid --dburl. Minimum requirement: driver://host/database.'); + } + $info += array( + 'user' => '', + 'pass' => '', + 'fragment' => '', + ); + if ($info['path'][0] === '/') { + $info['path'] = substr($info['path'], 1); + } + if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') { + $info['path'] = DRUPAL_ROOT . '/' . $info['path']; + } + $database = array( + 'driver' => $info['scheme'], + 'username' => $info['user'], + 'password' => $info['pass'], + 'host' => $info['host'], + 'database' => $info['path'], + ); + if (isset($info['port'])) { + $database['port'] = $info['port']; + } + return $database; + } + + /** + * Gets database connection info as a URL. + * + * @param string $key + * The database connection key. + * + * @return string + * The connection info as a URL. + */ + public static function getConnectionInfoAsUrl($key = 'default') { + $db_info = static::getConnectionInfo($key); + if ($db_info['default']['driver'] == 'sqlite') { + $db_url = $db_info['default']['driver'] . '://localhost/' . $db_info['default']['database']; + } + else { + $user = ''; + if ($db_info['default']['username']) { + $user = $db_info['default']['username']; + if ($db_info['default']['password']) { + $user .= ':' . $db_info['default']['password']; + } + $user .= '@'; + } + + $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host']; + if (isset($db_info['default']['port'])) { + $db_url .= ':' . $db_info['default']['port']; + } + $db_url .= '/' . $db_info['default']['database']; + } + if ($db_info['default']['prefix']['default']) { + $db_url .= '#' . $db_info['default']['prefix']['default']; + } + return $db_url; + } + } diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 45c8c54..327ff51 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -240,30 +240,7 @@ function simpletest_phpunit_configuration_filepath() { function simpletest_phpunit_run_command(array $unescaped_test_classnames, $phpunit_file) { // Set the up an environment variable containing the database connection so // that functional tests can connect to the database. - $db_info = Database::getConnectionInfo(); - if ($db_info['default']['driver'] == 'sqlite') { - $db_url = $db_info['default']['driver'] . '://localhost/' . $db_info['default']['database']; - } - else { - $user = ''; - if ($db_info['default']['username']) { - $user = $db_info['default']['username']; - if ($db_info['default']['password']) { - $user .= ':' . $db_info['default']['password']; - } - $user .= '@'; - } - - $db_url = $db_info['default']['driver'] . '://' . $user . $db_info['default']['host']; - if (isset($db_info['default']['port'])) { - $db_url .= ':' . $db_info['default']['port']; - } - $db_url .= '/' . $db_info['default']['database']; - } - if ($db_info['default']['prefix']['default']) { - $db_url .= '#' . $db_info['default']['prefix']['default']; - } - putenv('SIMPLETEST_DB=' . $db_url); + putenv('SIMPLETEST_DB=' . Database::getConnectionInfoAsUrl()); $phpunit_bin = simpletest_phpunit_command(); $command = array( diff --git a/core/modules/simpletest/src/BrowserTestBase.php b/core/modules/simpletest/src/BrowserTestBase.php index e5643cc..c8aa69b 100644 --- a/core/modules/simpletest/src/BrowserTestBase.php +++ b/core/modules/simpletest/src/BrowserTestBase.php @@ -1064,31 +1064,7 @@ private function changeDatabasePrefix() { // If the test is run with argument dburl then use it. $db_url = getenv('SIMPLETEST_DB'); if (!empty($db_url)) { - $info = parse_url($db_url); - if (!isset($info['scheme'], $info['host'], $info['path'])) { - throw new \InvalidArgumentException('Invalid --dburl. Minimum requirement: driver://host/database.'); - } - $info += array( - 'user' => '', - 'pass' => '', - 'fragment' => '', - ); - if ($info['path'][0] === '/') { - $info['path'] = substr($info['path'], 1); - } - if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') { - $info['path'] = DRUPAL_ROOT . '/' . $info['path']; - } - $database = array( - 'driver' => $info['scheme'], - 'username' => $info['user'], - 'password' => $info['pass'], - 'host' => $info['host'], - 'database' => $info['path'], - ); - if (isset($info['port'])) { - $database['port'] = $info['port']; - } + $database = Database::convertDbUrlToConnectionInfo($db_url); Database::addConnectionInfo('default', 'default', $database); } diff --git a/core/scripts/run-tests.sh b/core/scripts/run-tests.sh index 6e9a950..209e237 100644 --- a/core/scripts/run-tests.sh +++ b/core/scripts/run-tests.sh @@ -413,35 +413,12 @@ function simpletest_script_setup_database($new = FALSE) { if (!empty($args['dburl'])) { // Remove a possibly existing default connection (from settings.php). Database::removeConnection('default'); - - $info = parse_url($args['dburl']); - if (!isset($info['scheme'], $info['host'], $info['path'])) { - simpletest_script_print_error('Invalid --dburl. Minimum requirement: driver://host/database'); - exit(1); - } - $info += array( - 'user' => '', - 'pass' => '', - 'fragment' => '', - ); - if ($info['path'][0] === '/') { - $info['path'] = substr($info['path'], 1); - } - if ($info['scheme'] === 'sqlite' && $info['path'][0] !== '/') { - $info['path'] = DRUPAL_ROOT . '/' . $info['path']; + try { + $databases['default']['default'] = Database::convertDbUrlToConnectionInfo($args['dburl']); } - $databases['default']['default'] = array( - 'driver' => $info['scheme'], - 'username' => $info['user'], - 'password' => $info['pass'], - 'host' => $info['host'], - 'database' => $info['path'], - 'prefix' => array( - 'default' => $info['fragment'], - ), - ); - if (isset($info['port'])) { - $databases['default']['default']['port'] = $info['port']; + catch (\InvalidArgumentException $e) { + simpletest_script_print_error('Invalid --dburl. Reason: ' . $e->getMessage()); + exit(1); } } // Otherwise, use the default database connection from settings.php.