diff --git a/core/lib/Drupal/Core/Test/EnvironmentCleaner.php b/core/lib/Drupal/Core/Test/EnvironmentCleaner.php index 15852696..6016bcf4 100644 --- a/core/lib/Drupal/Core/Test/EnvironmentCleaner.php +++ b/core/lib/Drupal/Core/Test/EnvironmentCleaner.php @@ -47,7 +47,7 @@ class EnvironmentCleaner implements EnvironmentCleanerInterface { protected $output; /** - * Construct an environment cleaner. + * Constructs a test environment cleaner. * * @param string $root * The path to the root of the Drupal installation. @@ -163,14 +163,8 @@ protected function doCleanTemporaryDirectories() { /** * {@inheritdoc} */ - public function cleanResults($test_id = NULL) { - if ($test_id) { - $test_run = TestRun::get($this->testRunResultsStorage, $test_id); - return $test_run->removeResults(); - } - else { - return $this->testRunResultsStorage->cleanUp(); - } + public function cleanResults(TestRun $test_run = NULL) { + return $test_run ? $test_run->removeResults() : $this->testRunResultsStorage->cleanUp(); } } diff --git a/core/lib/Drupal/Core/Test/EnvironmentCleanerInterface.php b/core/lib/Drupal/Core/Test/EnvironmentCleanerInterface.php index dc5d40cd..e786962c 100644 --- a/core/lib/Drupal/Core/Test/EnvironmentCleanerInterface.php +++ b/core/lib/Drupal/Core/Test/EnvironmentCleanerInterface.php @@ -8,11 +8,9 @@ * This interface is marked internal. It does not imply an API. * * @todo Formalize this interface in - * https://www.drupal.org/project/drupal/issues/3075490 and - * https://www.drupal.org/project/drupal/issues/3075608 + * https://www.drupal.org/project/drupal/issues/3075490 * * @see https://www.drupal.org/project/drupal/issues/3075490 - * @see https://www.drupal.org/project/drupal/issues/3075608 * * @internal */ @@ -25,7 +23,7 @@ * under test. * * @param bool $clear_results - * (optional) Whether to clear the test results database. Defaults to TRUE. + * (optional) Whether to clear the test results storage. Defaults to TRUE. * @param bool $clear_temp_directories * (optional) Whether to clear the test site directories. Defaults to TRUE. * @param bool $clear_database @@ -46,12 +44,13 @@ public function cleanTemporaryDirectories(); /** * Clears test results from the results storage. * - * @param $test_id - * Test ID to remove results for, or NULL to remove all results. + * @param \Drupal\Core\Test\TestRun $test_run + * The test run object to remove results for, or NULL to remove all + * results. * * @return int * The number of results that were removed. */ - public function cleanResults($test_id = NULL); + public function cleanResults(TestRun $test_run = NULL); } diff --git a/core/lib/Drupal/Core/Test/SimpletestTestRunResultsStorage.php b/core/lib/Drupal/Core/Test/SimpletestTestRunResultsStorage.php index 9780dc33..74486367 100644 --- a/core/lib/Drupal/Core/Test/SimpletestTestRunResultsStorage.php +++ b/core/lib/Drupal/Core/Test/SimpletestTestRunResultsStorage.php @@ -100,6 +100,24 @@ public function insertLogEntry(TestRun $test_run, array $entry) { ->execute(); } + /** + * Delete a single log entry by message ID. + * + * This method is introduced for BC. + * + * @param $message_id + * Message ID of the log entry to delete. + * + * @return + * TRUE if the log entry was deleted, FALSE otherwise. + */ + public function removeLogEntry($message_id) { + return (bool) $this->connection + ->delete('simpletest') + ->condition('message_id', $message_id) + ->execute(); + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Test/TestSetupTrait.php b/core/lib/Drupal/Core/Test/TestSetupTrait.php index c7d211e1..0fd7428a 100644 --- a/core/lib/Drupal/Core/Test/TestSetupTrait.php +++ b/core/lib/Drupal/Core/Test/TestSetupTrait.php @@ -108,8 +108,14 @@ * * @return \Drupal\Core\Database\Connection * The database connection to use for inserting assertions. + * + * @deprecated in drupal:x.x.x and is removed from drupal:y.y.y. There is no + * replacement. + * + * @see https://www.drupal.org/node/xxx */ public static function getDatabaseConnection() { + @trigger_error(__METHOD__ . ' in drupal:x.x.x and is removed from drupal:y.y.y. There is no replacement. See https://www.drupal.org/node/xxx', E_USER_DEPRECATED); return SimpletestTestRunResultsStorage::getConnection(); } diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 9bbb58de..c6e8c14f 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -692,18 +692,19 @@ function simpletest_clean_temporary_directories() { * The number of results that were removed. * * @deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the - * environment_cleaner service and call its cleanResultsTable() method, or use - * \Drupal\Core\Test\EnvironmentCleaner::cleanResultsTable() instead. + * environment_cleaner service and call its cleanResults() method, or use + * \Drupal\Core\Test\EnvironmentCleaner::cleanResults() instead. * * @see https://www.drupal.org/node/3076634 */ function simpletest_clean_results_table($test_id = NULL) { - @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanResultsTable() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanResultsTable() instead. See https://www.drupal.org/node/3076634', E_USER_DEPRECATED); + @trigger_error(__FUNCTION__ . ' is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanResults() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanResults() instead. See https://www.drupal.org/node/3076634', E_USER_DEPRECATED); $count = 0; if (\Drupal::config('simpletest.settings')->get('clear_results')) { /* @var $cleaner \Drupal\simpletest\EnvironmentCleanerService */ $cleaner = \Drupal::service('environment_cleaner'); - $count = $cleaner->cleanResults($test_id); + $test_run = isset($test_id) ? TestRun::get(new SimpletestTestRunResultsStorage(), $test_id) : NULL; + $count = $cleaner->cleanResults($test_run); } return $count; } diff --git a/core/modules/simpletest/src/Form/SimpletestResultsForm.php b/core/modules/simpletest/src/Form/SimpletestResultsForm.php index 616caee6..d3c33752 100644 --- a/core/modules/simpletest/src/Form/SimpletestResultsForm.php +++ b/core/modules/simpletest/src/Form/SimpletestResultsForm.php @@ -174,7 +174,8 @@ public function buildForm(array $form, FormStateInterface $form_state, $test_id ]; if (is_numeric($test_id)) { - $this->cleaner->cleanResults($test_id); + $test_run = TestRun::get((new SimpletestTestRunResultsStorage($this->database)), $test_id); + $this->cleaner->cleanResults($test_run); } return $form; diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index 3c456bf8..3d73e626 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -298,7 +298,7 @@ protected function checkRequirements() { * The message ID. */ protected function storeAssertion(array $assertion) { - return TestRun::get(new SimpletestTestRunResultsStorage(self::getDatabaseConnection()), $this->testId)->insertLogEntry($assertion); + return TestRun::get(new SimpletestTestRunResultsStorage(), $this->testId)->insertLogEntry($assertion); } /** @@ -415,7 +415,7 @@ public static function insertAssert($test_id, $test_class, $status, $message = ' ]; // We can't use storeAssertion() because this method is static. - return TestRun::get(new SimpletestTestRunResultsStorage(self::getDatabaseConnection()), $test_id)->insertLogEntry($assertion); + return TestRun::get(new SimpletestTestRunResultsStorage(), $test_id)->insertLogEntry($assertion); } /** @@ -431,10 +431,7 @@ public static function insertAssert($test_id, $test_class, $status, $message = ' */ public static function deleteAssert($message_id) { // We can't use storeAssertion() because this method is static. - return (bool) self::getDatabaseConnection() - ->delete('simpletest') - ->condition('message_id', $message_id) - ->execute(); + return (new SimpletestTestRunResultsStorage())->removeLogEntry($message_id); } /** @@ -1037,7 +1034,7 @@ private function prepareDatabasePrefix() { // As soon as the database prefix is set, the test might start to execute. // All assertions as well as the SimpleTest batch operations are associated // with the testId, so the database prefix has to be associated with it. - TestRun::get(new SimpletestTestRunResultsStorage(self::getDatabaseConnection()), $this->testId)->setDatabasePrefix($this->databasePrefix); + TestRun::get(new SimpletestTestRunResultsStorage(), $this->testId)->setDatabasePrefix($this->databasePrefix); } /** @@ -1241,7 +1238,7 @@ private function restoreEnvironment() { // In case a fatal error occurred that was not in the test process read the // log to pick up any fatal errors. - $test_run = TestRun::get(new SimpletestTestRunResultsStorage(self::getDatabaseConnection()), $this->testId); + $test_run = TestRun::get(new SimpletestTestRunResultsStorage(), $this->testId); $test_database = new TestDatabase($this->databasePrefix); $test_run->processPhpErrorLogFile($test_database->getPhpErrorLogPath(), get_class($this)); diff --git a/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php b/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php index 0f68cdad..5ed55313 100644 --- a/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php +++ b/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php @@ -4,6 +4,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\Test\EnvironmentCleanerInterface; +use Drupal\Core\Test\TestRun; use Drupal\KernelTests\KernelTestBase; use Symfony\Component\DependencyInjection\Definition; @@ -30,7 +31,7 @@ public function register(ContainerBuilder $container) { * @expectedDeprecation simpletest_clean_environment is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanEnvironment() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanEnvironment() instead.. See https://www.drupal.org/node/3076634 * @expectedDeprecation simpletest_clean_database is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanDatabase() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanDatabase() instead. See https://www.drupal.org/node/3076634 * @expectedDeprecation simpletest_clean_temporary_directories is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanTemporaryDirectories() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanTemporaryDirectories() instead. See https://www.drupal.org/node/3076634 - * @expectedDeprecation simpletest_clean_results_table is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanResultsTable() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanResultsTable() instead. See https://www.drupal.org/node/3076634 + * @expectedDeprecation simpletest_clean_results_table is deprecated in drupal:8.8.0 and is removed from drupal:9.0.0. Access the environment_cleaner service and call its cleanResults() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanResults() instead. See https://www.drupal.org/node/3076634 */ public function testDeprecatedCleanFunctions() { $this->assertNull(simpletest_clean_environment()); @@ -54,7 +55,7 @@ public function cleanEnvironment($clear_results = TRUE, $clear_temp_directories } - public function cleanResults($test_id = NULL) { + public function cleanResults(TestRun $test_run = NULL) { } diff --git a/core/tests/Drupal/KernelTests/Core/Test/TestSetupTraitTest.php b/core/tests/Drupal/KernelTests/Core/Test/TestSetupTraitTest.php new file mode 100644 index 00000000..e6875687 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Test/TestSetupTraitTest.php @@ -0,0 +1,32 @@ +assertNotNull($this->getDatabaseConnection()); + } + +}