diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 6a60ce9..79689c0 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -684,22 +684,36 @@ function simpletest_clean_database() { /** * Finds all leftover temporary directories and removes them. + * + * @param string $directory + * (optional) The relative path to directory of a particular test site. Must + * to be of the following pattern: sites/simpletest/12345678. If ommited, all + * the test sites' directories found in sites/simpletest will be removed. */ function simpletest_clean_temporary_directories($directory = NULL) { $count = 0; $root = \Drupal::root() . '/sites/simpletest/'; - $dir = $directory ? explode('sites/simpletest/', $directory) : FALSE; if (is_dir($root)) { - $files = !$dir ? scandir($root) : (isset($dir[1]) && is_dir("{$root}{$dir[1]}") ? [$dir[1]] : []); + // Prevent removing wrong directory accidentally passed as an argument. + $directory = $directory ? explode('sites/simpletest/', $directory) : FALSE; + $files = []; + + if (isset($directory[1]) && is_dir($root . $directory[1])) { + $files[] = $directory[1]; + } + else { + $files = scandir($root); + } + foreach ($files as $file) { if ($file[0] != '.') { - $path = "{$root}{$file}"; + $path = $root . $file; + // When the webserver runs with the same system user as the test + // runner, we can make read-only files writable again. If not, chmod + // will fail while the file deletion still works if file permissions + // have been configured correctly. Thus, we ignore any chmod errors. $result = file_unmanaged_delete_recursive($path, function ($any_path) { - // When the webserver runs with the same system user as the test - // runner, we can make read-only files writable again. If not, chmod - // will fail while the file deletion still works if file permissions - // have been configured correctly. Thus, we ignore any chmod errors. @chmod($any_path, 0700); }); if ($directory) {