diff --git a/core/lib/Drupal/Core/Test/EnvironmentCleaner.php b/core/lib/Drupal/Core/Test/EnvironmentCleaner.php index 1785359dbb..022451669a 100644 --- a/core/lib/Drupal/Core/Test/EnvironmentCleaner.php +++ b/core/lib/Drupal/Core/Test/EnvironmentCleaner.php @@ -94,7 +94,8 @@ public function cleanEnvironment($clear_results = TRUE, $clear_temp_directories * {@inheritdoc} */ public function cleanDatabase() { - if ($this->doCleanDatabase() > 0) { + $count = $this->doCleanDatabase(); + if ($count > 0) { $this->output->write('Leftover tables removed: ' . $count); } else { diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 0ef7feb12e..df35d9ee3d 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -678,11 +678,14 @@ function simpletest_generate_file($filename, $width, $lines, $type = 'binary-tex /** * Removes all temporary database tables and directories. * - * @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use + * @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 */ function simpletest_clean_environment() { - @trigger_error(__FUNCTION__, 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 cleanEnvironment() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanEnvironment() instead.. See https://www.drupal.org/node/3076634', E_USER_DEPRECATED); /* @var $cleaner \Drupal\simpletest\EnvironmentCleanerService */ $cleaner = \Drupal::service('environment_cleaner'); $cleaner->cleanEnvironment(); @@ -691,11 +694,14 @@ function simpletest_clean_environment() { /** * Removes prefixed tables from the database from crashed tests. * - * @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use + * @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 */ function simpletest_clean_database() { - @trigger_error(__FUNCTION__, 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 cleanDatabase() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanDatabase() instead. See https://www.drupal.org/node/3076634', E_USER_DEPRECATED); /* @var $cleaner \Drupal\simpletest\EnvironmentCleanerService */ $cleaner = \Drupal::service('environment_cleaner'); $cleaner->cleanDatabase(); @@ -704,11 +710,15 @@ function simpletest_clean_database() { /** * Finds all leftover temporary directories and removes them. * - * @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use + * @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 */ function simpletest_clean_temporary_directories() { - @trigger_error(__FUNCTION__, 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 cleanTemporaryDirectories() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanTemporaryDirectories() instead. See https://www.drupal.org/node/3076634', E_USER_DEPRECATED); /* @var $cleaner \Drupal\simpletest\EnvironmentCleanerService */ $cleaner = \Drupal::service('environment_cleaner'); $cleaner->cleanTemporaryDirectories(); @@ -723,11 +733,14 @@ function simpletest_clean_temporary_directories() { * @return int * The number of results that were removed. * - * @deprecated in Drupal 8.8.x and will be removed before Drupal 9.0.0. Use + * @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 */ function simpletest_clean_results_table($test_id = NULL) { - @trigger_error(__FUNCTION__, 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 cleanResultsTable() method, or use \Drupal\Core\Test\EnvironmentCleaner::cleanResultsTable() instead. See https://www.drupal.org/node/3076634', E_USER_DEPRECATED); if (\Drupal::config('simpletest.settings')->get('clear_results')) { /* @var $cleaner \Drupal\simpletest\EnvironmentCleanerService */ $cleaner = \Drupal::service('environment_cleaner'); diff --git a/core/modules/simpletest/simpletest.services.yml b/core/modules/simpletest/simpletest.services.yml index 85fda43284..cede4646b5 100644 --- a/core/modules/simpletest/simpletest.services.yml +++ b/core/modules/simpletest/simpletest.services.yml @@ -1,7 +1,7 @@ services: test_discovery: - # @todo Change this service so that it uses \Drupal\Core\Test\TestDiscovery - # in https://www.drupal.org/node/1667822 + # Drupal\simpletest\TestDiscovery is deprecated, but we still want to use it + # as the service here to support BC hooks. class: Drupal\simpletest\TestDiscovery arguments: ['@app.root', '@class_loader', '@module_handler'] diff --git a/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php b/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php new file mode 100644 index 0000000000..a5efa34a1e --- /dev/null +++ b/core/modules/simpletest/tests/src/Kernel/DeprecatedCleanupTest.php @@ -0,0 +1,65 @@ +setDefinition('environment_cleaner', $cleaner_definition); + } + + /** + * @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 + */ + public function testDeprecatedCleanFunctions() { + $this->assertNull(simpletest_clean_environment()); + $this->assertNull(simpletest_clean_database()); + $this->assertNull(simpletest_clean_temporary_directories()); + $this->assertNull(simpletest_clean_results_table()); + } + +} + +/** + * Mock environment_cleaner service that does not perform any cleaning. + */ +class StubEnvironmentCleanerService implements EnvironmentCleanerInterface { + + public function cleanDatabase() { + + } + + public function cleanEnvironment($clear_results = TRUE, $clear_temp_directories = TRUE, $clear_database = TRUE) { + + } + + public function cleanResultsTable($test_id = NULL) { + + } + + public function cleanTemporaryDirectories() { + + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Test/EnvironmentCleanerTest.php b/core/tests/Drupal/KernelTests/Core/Test/EnvironmentCleanerTest.php new file mode 100644 index 0000000000..0ca6692600 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Test/EnvironmentCleanerTest.php @@ -0,0 +1,55 @@ + [ + 'simpletest' => [ + 'delete_dir' => [ + 'delete.me' => 'I am a gonner.', + ], + 'delete_me.too' => 'delete this file.', + ], + ], + ]); + + $connection = $this->prophesize(Connection::class); + + $cleaner = new EnvironmentCleaner( + vfsStream::url('cleanup_test'), + $connection->reveal(), + $connection->reveal(), + new NullOutput(), + \Drupal::service('file_system') + ); + + $do_cleanup_ref = new \ReflectionMethod($cleaner, 'doCleanTemporaryDirectories'); + $do_cleanup_ref->setAccessible(TRUE); + + $this->assertFileExists(vfsStream::url('cleanup_test/sites/simpletest/delete_dir/delete.me')); + $this->assertFileExists(vfsStream::url('cleanup_test/sites/simpletest/delete_me.too')); + + $this->assertEquals(2, $do_cleanup_ref->invoke($cleaner)); + + $this->assertDirectoryNotExists(vfsStream::url('cleanup_test/sites/simpletest/delete_dir')); + $this->assertFileNotExists(vfsStream::url('cleanup_test/sites/simpletest/delete_dir/delete.me')); + $this->assertFileNotExists(vfsStream::url('cleanup_test/sites/simpletest/delete_me.too')); + } + +}