From 71704dd9334f299359361a8eafd85a80391d1378 Mon Sep 17 00:00:00 2001
From: drugan <drugan@1578644.no-reply.drupal.org>
Date: Thu, 27 Oct 2016 22:18:48 +0300
Subject: [PATCH] Issue #2745123 by Mile23, slasher13, drunken monkey, Yogesh
 Pawar, blazey, drugan: _simpletest_batch_finished() needs
 to set up autoloading for \Drupal\Tests

---
 core/modules/simpletest/simpletest.module |   37 ++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 6 deletions(-)

diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module
index f72ca5c..b63435e 100644
--- a/core/modules/simpletest/simpletest.module
+++ b/core/modules/simpletest/simpletest.module
@@ -676,14 +676,39 @@ function simpletest_clean_database() {
  */
 function simpletest_clean_temporary_directories() {
   $count = 0;
-  if (is_dir(DRUPAL_ROOT . '/sites/simpletest')) {
-    $files = scandir(DRUPAL_ROOT . '/sites/simpletest');
-    foreach ($files as $file) {
+  $paths = [];
+  $root = DRUPAL_ROOT . '/sites/simpletest';
+  if (is_dir($root)) {
+    $files = scandir($root);
+
+    // First, chmod files and dirs recusively.
+    while ($file = current($files)) {
       if ($file[0] != '.') {
-        $path = DRUPAL_ROOT . '/sites/simpletest/' . $file;
-        file_unmanaged_delete_recursive($path, array('Drupal\simpletest\TestBase', 'filePreDeleteCallback'));
-        $count++;
+        $paths[] = $root . '/' . explode('/', $file)[0];
+        $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 errors while running chmod.
+        @chmod($path, 0700);
+
+        if (is_dir($path)){
+          $dir = dir($path);
+          while (($entry = $dir->read()) !== FALSE) {
+            if ($entry[0] == '.') {
+              continue;
+            }
+            array_push($files, $file . '/' . $entry);
+          }
+          $dir->close();
+        }
       }
+      next($files);
+    }
+
+    foreach (array_unique($paths) as $path) {
+      file_unmanaged_delete_recursive($path);
+      $count++;
     }
   }
 
-- 
1.7.9.5

