diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 79689c0..82989dd 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -689,26 +689,41 @@ function simpletest_clean_database() { * (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. + * + * @return bool + * TRUE if directory is removed, FALSE otherwise. Additionally watchdog + * messages will be emitted if any path attempted to remove fails. + * + * @see file_unmanaged_delete() */ function simpletest_clean_temporary_directories($directory = NULL) { + $directories = []; $count = 0; - $root = \Drupal::root() . '/sites/simpletest/'; - - if (is_dir($root)) { - // 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]; + $result = FALSE; + $simpletest_root = \Drupal::root() . '/sites/simpletest/'; + + if (is_dir($simpletest_root)) { + // Get the type of $directory argument for debugging in the watchdog error + // message if it is not valid string or NULL. + $directory = is_string($directory) && !empty($directory) ? $directory : gettype($directory); + // Do not recognize any, except expected directory pattern as it may cause + // catastrophic consequences on the entire Drupal site. + preg_match('/^(sites\/simpletest\/)(.*)/', $directory, $matches); + + if (!empty($matches[2]) && is_dir($simpletest_root . $matches[2])) { + $directories[] = $matches[2]; + } + elseif ($directory == 'NULL') { + $directories = scandir($simpletest_root); } else { - $files = scandir($root); + $logger = \Drupal::logger('Testing'); + $logger->error('The %path is not eligible for removing in %func().', ['%path' => $directory, '%func' => __FUNCTION__]); } - foreach ($files as $file) { + foreach ($directories as $file) { if ($file[0] != '.') { - $path = $root . $file; + $path = $simpletest_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 @@ -716,11 +731,6 @@ function simpletest_clean_temporary_directories($directory = NULL) { $result = file_unmanaged_delete_recursive($path, function ($any_path) { @chmod($any_path, 0700); }); - if ($directory) { - // We do not need any more if this function was called from within a - // running test instead of using "Clean environment" button. - return $result; - } $count++; } } @@ -732,6 +742,8 @@ function simpletest_clean_temporary_directories($directory = NULL) { else { drupal_set_message(t('No temporary directories to remove.')); } + + return $result; } /**