From b064d94449d7e06c3f16fc97869ac80cb8066f23 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Mon, 17 Jun 2024 18:17:55 -0500
Subject: [PATCH] Issue #3445847 by Alexander Allen, mstrelan, catch, xjm,
 longwave, mark_fullmer: 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 +
 .../Drupal/Tests/Core/Recipe/RecipeQuickStartTest.php |  7 +++++--
 core/tests/Drupal/Tests/UnitTestCase.php              |  1 +
 core/tests/bootstrap.php                              |  4 ----
 8 files changed, 19 insertions(+), 8 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 f9efa6d3427e..6abba5431e11 100644
--- a/core/tests/Drupal/Tests/BrowserTestBase.php
+++ b/core/tests/Drupal/Tests/BrowserTestBase.php
@@ -351,6 +351,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/Core/Recipe/RecipeQuickStartTest.php b/core/tests/Drupal/Tests/Core/Recipe/RecipeQuickStartTest.php
index 35505fe1fe5b..c71cd91bff05 100644
--- a/core/tests/Drupal/Tests/Core/Recipe/RecipeQuickStartTest.php
+++ b/core/tests/Drupal/Tests/Core/Recipe/RecipeQuickStartTest.php
@@ -92,14 +92,17 @@ public function testQuickStartRecipeCommand(): void {
     // Install a site using the standard recipe to ensure the one time login
     // link generation works.
 
+    $script = $this->root . '/core/scripts/drupal';
     $install_command = [
       $this->php,
-      'core/scripts/drupal',
+      $script,
       'quick-start',
       'core/recipes/standard',
       "--site-name='Test site {$this->testDb->getDatabasePrefix()}'",
       '--suppress-login',
     ];
+    $this->assertFileExists($script, "Install script is found in $script");
+
     $process = new Process($install_command, NULL, ['DRUPAL_DEV_SITE_PATH' => $this->testDb->getTestSitePath()]);
     $process->setTimeout(500);
     $process->start();
@@ -123,7 +126,7 @@ public function testQuickStartRecipeCommand(): void {
 
     // Generate a cookie so we can make a request against the installed site.
     define('DRUPAL_TEST_IN_CHILD_SITE', FALSE);
-    chmod($this->testDb->getTestSitePath(), 0755);
+    chmod($this->root . '/' . $this->testDb->getTestSitePath(), 0755);
     $cookieJar = CookieJar::fromArray([
       'SIMPLETEST_USER_AGENT' => drupal_generate_test_ua($this->testDb->getDatabasePrefix()),
     ], '127.0.0.1');
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