diff --git a/core/lib/Drupal/Core/Test/TestDatabase.php b/core/lib/Drupal/Core/Test/TestDatabase.php new file mode 100644 index 0000000000000000000000000000000000000000..a0121dc22603114328a198d0cb91e14c081038e9 --- /dev/null +++ b/core/lib/Drupal/Core/Test/TestDatabase.php @@ -0,0 +1,44 @@ +<?php + +namespace Drupal\Core\Test; + +use Drupal\Core\Database\ConnectionNotDefinedException; +use Drupal\Core\Database\Database; + +/** + * Provides helper methods for interacting with the Simpletest database. + */ +class TestDatabase { + + /** + * Returns the database connection to the site running Simpletest. + * + * @return \Drupal\Core\Database\Connection + * The database connection to use for inserting assertions. + * + * @see \Drupal\simpletest\TestBase::prepareEnvironment() + */ + public static function getConnection() { + // Check whether there is a test runner connection. + // @see run-tests.sh + // @todo Convert Simpletest UI runner to create + use this connection, too. + try { + $connection = Database::getConnection('default', 'test-runner'); + } + catch (ConnectionNotDefinedException $e) { + // Check whether there is a backup of the original default connection. + // @see TestBase::prepareEnvironment() + try { + $connection = Database::getConnection('default', 'simpletest_original_default'); + } + catch (ConnectionNotDefinedException $e) { + // If TestBase::prepareEnvironment() or TestBase::restoreEnvironment() + // failed, the test-specific database connection does not exist + // yet/anymore, so fall back to the default of the (UI) test runner. + $connection = Database::getConnection('default', 'default'); + } + } + return $connection; + } + +} diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 58ede7c1d5678102a53a66fd42de61cc4506f8f5..d8b653e87e362a7a658c07abe163ca4702d32b14 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -10,6 +10,7 @@ use Drupal\Core\Render\Element; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\simpletest\TestBase; +use Drupal\Core\Test\TestDatabase; use Drupal\simpletest\TestDiscovery; use Symfony\Component\Process\PhpExecutableFinder; @@ -212,7 +213,7 @@ function simpletest_process_phpunit_results($phpunit_results) { // Insert the results of the PHPUnit test run into the database so the results // are displayed along with Simpletest's results. if (!empty($phpunit_results)) { - $query = TestBase::getDatabaseConnection() + $query = TestDatabase::getConnection() ->insert('simpletest') ->fields(array_keys($phpunit_results[0])); foreach ($phpunit_results as $result) { @@ -428,12 +429,12 @@ function _simpletest_batch_finished($success, $results, $operations, $elapsed) { * that ran. */ function simpletest_last_test_get($test_id) { - $last_prefix = TestBase::getDatabaseConnection() + $last_prefix = TestDatabase::getConnection() ->queryRange('SELECT last_prefix FROM {simpletest_test_id} WHERE test_id = :test_id', 0, 1, array( ':test_id' => $test_id, )) ->fetchField(); - $last_test_class = TestBase::getDatabaseConnection() + $last_test_class = TestDatabase::getConnection() ->queryRange('SELECT test_class FROM {simpletest} WHERE test_id = :test_id ORDER BY message_id DESC', 0, 1, array( ':test_id' => $test_id, )) @@ -643,7 +644,7 @@ function simpletest_clean_temporary_directories() { */ function simpletest_clean_results_table($test_id = NULL) { if (\Drupal::config('simpletest.settings')->get('clear_results')) { - $connection = TestBase::getDatabaseConnection(); + $connection = TestDatabase::getConnection(); if ($test_id) { $count = $connection->query('SELECT COUNT(test_id) FROM {simpletest_test_id} WHERE test_id = :test_id', array(':test_id' => $test_id))->fetchField(); diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index 9bc2c5bed771da5bf1618eb427decccc5b4a30a4..6bb84fda7c058d5199a2ef97493743e60b52ed14 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -9,10 +9,10 @@ use Drupal\Core\Database\Database; use Drupal\Core\Config\ConfigImporter; use Drupal\Core\Config\StorageComparer; -use Drupal\Core\Database\ConnectionNotDefinedException; use Drupal\Core\Config\StorageInterface; use Drupal\Core\Site\Settings; use Drupal\Core\StreamWrapper\PublicStream; +use Drupal\Core\Test\TestDatabase; use Drupal\Core\Utility\Error; use Drupal\Tests\RandomGeneratorTrait; use Drupal\Tests\SessionTestTrait; @@ -510,26 +510,7 @@ public static function deleteAssert($message_id) { * The database connection to use for inserting assertions. */ public static function getDatabaseConnection() { - // Check whether there is a test runner connection. - // @see run-tests.sh - // @todo Convert Simpletest UI runner to create + use this connection, too. - try { - $connection = Database::getConnection('default', 'test-runner'); - } - catch (ConnectionNotDefinedException $e) { - // Check whether there is a backup of the original default connection. - // @see TestBase::prepareEnvironment() - try { - $connection = Database::getConnection('default', 'simpletest_original_default'); - } - catch (ConnectionNotDefinedException $e) { - // If TestBase::prepareEnvironment() or TestBase::restoreEnvironment() - // failed, the test-specific database connection does not exist - // yet/anymore, so fall back to the default of the (UI) test runner. - $connection = Database::getConnection('default', 'default'); - } - } - return $connection; + return TestDatabase::getConnection(); } /** diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php index aa71fc128132e4587111bf9531edc40553d7736e..8f2e671036968d14477500eccb8f481484e0a386 100644 --- a/core/tests/Drupal/Tests/BrowserTestBase.php +++ b/core/tests/Drupal/Tests/BrowserTestBase.php @@ -10,7 +10,6 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Cache\Cache; -use Drupal\Core\Database\ConnectionNotDefinedException; use Drupal\Core\Database\Database; use Drupal\Core\DrupalKernel; use Drupal\Core\Session\AccountInterface; @@ -20,6 +19,7 @@ use Drupal\Core\StreamWrapper\StreamWrapperInterface; use Drupal\Core\Test\TestRunnerKernel; use Drupal\Core\Url; +use Drupal\Core\Test\TestDatabase; use Drupal\user\Entity\Role; use Drupal\user\Entity\User; use Drupal\user\UserInterface; @@ -1250,26 +1250,7 @@ protected function prepareEnvironment() { * The database connection to use for inserting assertions. */ public static function getDatabaseConnection() { - // Check whether there is a test runner connection. - // @see run-tests.sh - try { - $connection = Database::getConnection('default', 'test-runner'); - } - catch (ConnectionNotDefinedException $e) { - // Check whether there is a backup of the original default connection. - // @see BrowserTestBase::prepareEnvironment() - try { - $connection = Database::getConnection('default', 'simpletest_original_default'); - } - catch (ConnectionNotDefinedException $e) { - // If BrowserTestBase::prepareEnvironment() or - // BrowserTestBase::restoreEnvironment() failed, the test-specific - // database connection does not exist yet/anymore, so fall back to the - // default of the (UI) test runner. - $connection = Database::getConnection('default', 'default'); - } - } - return $connection; + return TestDatabase::getConnection(); } /**