From 1eef08b459d6d080c6454846b33a415155d59695 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Wed, 22 May 2024 13:06:34 +0100
Subject: [PATCH] Issue #3445847 by mstrelan: PHPUnit 10 behaves differently
 when invoked outside web root

---
 .../file/tests/src/Kernel/FileSaveUploadTest.php      | 11 +++++++++--
 .../Core/Database/DriverSpecificKernelTestBase.php    |  1 +
 core/tests/Drupal/KernelTests/KernelTestBase.php      |  1 +
 core/tests/Drupal/Tests/BrowserTestBase.php           |  1 +
 .../Drupal/Tests/Core/Command/QuickStartTest.php      |  1 +
 core/tests/Drupal/Tests/UnitTestCase.php              |  1 +
 core/tests/bootstrap.php                              |  4 ----
 7 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/core/modules/file/tests/src/Kernel/FileSaveUploadTest.php b/core/modules/file/tests/src/Kernel/FileSaveUploadTest.php
index 435c9766806b..1e8205e420bc 100644
--- a/core/modules/file/tests/src/Kernel/FileSaveUploadTest.php
+++ b/core/modules/file/tests/src/Kernel/FileSaveUploadTest.php
@@ -33,9 +33,8 @@ class FileSaveUploadTest extends KernelTestBase {
    * {@inheritdoc}
    */
   protected function setUp(): void {
-    \file_put_contents('test.bbb', 'test');
-
     parent::setUp();
+    \file_put_contents('test.bbb', 'test');
     $request = new Request();
     $request->files->set('files', [
       'file' => new UploadedFile(
@@ -53,6 +52,14 @@ protected function setUp(): void {
     $this->container->set('request_stack', $requestStack);
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function tearDown(): void {
+    \unlink('test.bbb');
+    parent::tearDown();
+  }
+
   /**
    * Tests file_save_upload() with empty extensions.
    */
diff --git a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php
index a8f590b33d2e..564f0ddc80c5 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php
@@ -31,6 +31,7 @@ protected function setUp(): void {
     // is not the one the test requires, skip before test database
     // initialization so to save cycles.
     $this->root = static::getDrupalRoot();
+    chdir($this->root);
     $connectionInfo = $this->getDatabaseConnectionInfo();
     $test_class_parts = explode('\\', get_class($this));
     $expected_provider = $test_class_parts[2] ?? '';
diff --git a/core/tests/Drupal/KernelTests/KernelTestBase.php b/core/tests/Drupal/KernelTests/KernelTestBase.php
index 94ee7a7d8746..c10ca038928a 100644
--- a/core/tests/Drupal/KernelTests/KernelTestBase.php
+++ b/core/tests/Drupal/KernelTests/KernelTestBase.php
@@ -242,6 +242,7 @@ protected function setUp(): void {
     $this->registerComparator(new MarkupInterfaceComparator());
 
     $this->root = static::getDrupalRoot();
+    chdir($this->root);
     $this->initFileCache();
     $this->bootEnvironment();
     $this->bootKernel();
diff --git a/core/tests/Drupal/Tests/BrowserTestBase.php b/core/tests/Drupal/Tests/BrowserTestBase.php
index 486e86def42d..9ed02c276ae1 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -352,6 +352,7 @@ protected function setUp(): void {
     parent::setUp();
 
     $this->setUpAppRoot();
+    chdir($this->root);
 
     // Allow tests to compare MarkupInterface objects via assertEquals().
     $this->registerComparator(new MarkupInterfaceComparator());
diff --git a/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php b/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php
index 535b1d8c3168..688b54b753f5 100644
--- a/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php
+++ b/core/tests/Drupal/Tests/Core/Command/QuickStartTest.php
@@ -56,6 +56,7 @@ protected function setUp(): void {
     $php_executable_finder = new PhpExecutableFinder();
     $this->php = $php_executable_finder->find();
     $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
+    chdir($this->root);
     if (!is_writable("{$this->root}/sites/simpletest")) {
       $this->markTestSkipped('This test requires a writable sites/simpletest directory');
     }
diff --git a/core/tests/Drupal/Tests/UnitTestCase.php b/core/tests/Drupal/Tests/UnitTestCase.php
index 9d00d0a77826..73a2925cc6d0 100644
--- a/core/tests/Drupal/Tests/UnitTestCase.php
+++ b/core/tests/Drupal/Tests/UnitTestCase.php
@@ -62,6 +62,7 @@ protected function setUp(): void {
     FileCacheFactory::setPrefix('prefix');
 
     $this->root = dirname(substr(__DIR__, 0, -strlen(__NAMESPACE__)), 2);
+    chdir($this->root);
   }
 
   /**
diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php
index 0d663410407d..ae4ffac1a518 100644
--- a/core/tests/bootstrap.php
+++ b/core/tests/bootstrap.php
@@ -183,7 +183,3 @@ class_alias('\Drupal\Tests\DocumentElement', '\Behat\Mink\Element\DocumentElemen
 if ($browserTestOutputDirectory !== FALSE) {
   HtmlOutputLogger::init($browserTestOutputDirectory, (bool) getenv('BROWSERTEST_OUTPUT_VERBOSE') ?? FALSE);
 }
-
-// Drupal expects to be run from its root directory. This ensures all test types
-// are consistent.
-chdir(dirname(__DIR__, 2));
-- 
GitLab