From 512e19e3413327a8704e1e41e23631b32bb4fa77 Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Thu, 11 Aug 2022 19:27:44 +0000
Subject: [PATCH] Issue #3303168 by phenaproxima: Remove $testStagingRoot from
 kernel tests

---
 .../tests/src/Kernel/ExtensionUpdaterTest.php | 10 +++-
 .../Kernel/PackageManagerKernelTestBase.php   | 35 +------------
 .../tests/src/Kernel/StageTest.php            | 50 +++++--------------
 3 files changed, 23 insertions(+), 72 deletions(-)

diff --git a/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php b/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php
index 61f0845de0..6b34a1258f 100644
--- a/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php
+++ b/automatic_updates_extensions/tests/src/Kernel/ExtensionUpdaterTest.php
@@ -3,6 +3,7 @@
 namespace Drupal\Tests\automatic_updates_extensions\Kernel;
 
 use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
+use Drupal\Tests\package_manager\Kernel\TestPathFactory;
 use Drupal\Tests\user\Traits\UserCreationTrait;
 
 /**
@@ -48,7 +49,14 @@ class ExtensionUpdaterTest extends AutomaticUpdatesKernelTestBase {
 
     $this->createVirtualProject(__DIR__ . '/../../fixtures/fake-site');
 
-    $id = $this->container->get('automatic_updates_extensions.updater')->begin([
+    /** @var \Drupal\automatic_updates_extensions\ExtensionUpdater $updater */
+    $updater = $this->container->get('automatic_updates_extensions.updater');
+    // @todo Remove this hack in https://www.drupal.org/i/3303174.
+    $property = new \ReflectionProperty($updater, 'pathFactory');
+    $property->setAccessible(TRUE);
+    $property->setValue($updater, new TestPathFactory());
+
+    $id = $updater->begin([
       'my_module' => '9.8.1',
       // Use a legacy version number to ensure they are converted to semantic
       // version numbers which will work with the drupal.org Composer facade.
diff --git a/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php b/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php
index 3106049198..232150a8e1 100644
--- a/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php
+++ b/package_manager/tests/src/Kernel/PackageManagerKernelTestBase.php
@@ -35,17 +35,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
     'package_manager_bypass',
   ];
 
-  /**
-   * The test staging root.
-   *
-   * This value must be set before creating a test stage instance.
-   *
-   * @var string
-   *
-   * @see \Drupal\Tests\package_manager\Kernel\TestStageTrait::__construct()
-   */
-  public static $testStagingRoot;
-
   /**
    * The service IDs of any validators to disable.
    *
@@ -207,7 +196,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
     // Create a staging root directory alongside the active directory.
     $stage_dir = vfsStream::newDirectory('stage');
     $this->vfsRoot->addChild($stage_dir);
-    static::$testStagingRoot = $stage_dir->url();
 
     // Ensure the path locator points to the virtual active directory. We assume
     // that is its own web root and that the vendor directory is at its top
@@ -215,7 +203,7 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
     $active_dir = $active_dir->url();
     /** @var \Drupal\package_manager_bypass\PathLocator $path_locator */
     $path_locator = $this->container->get('package_manager.path_locator');
-    $path_locator->setPaths($active_dir, $active_dir . '/vendor', '', NULL);
+    $path_locator->setPaths($active_dir, $active_dir . '/vendor', '', $stage_dir->url());
 
     // Ensure the active directory will be copied into the virtual staging area.
     Beginner::setFixturePath($active_dir);
@@ -246,13 +234,6 @@ abstract class PackageManagerKernelTestBase extends KernelTestBase {
  */
 trait TestStageTrait {
 
-  /**
-   * The directory where staging areas will be created.
-   *
-   * @var string
-   */
-  public static $stagingRoot;
-
   /**
    * {@inheritdoc}
    */
@@ -261,20 +242,6 @@ trait TestStageTrait {
     $this->pathFactory = new TestPathFactory();
   }
 
-  /**
-   * {@inheritdoc}
-   */
-  public function create(?int $timeout = 300): string {
-    // Ensure that tests which need to successively create multiple stages are
-    // always using the same staging root, since the stored value may be deleted
-    // if the stage encounters an error during pre-create.
-    // @see \Drupal\package_manager\Stage::markAsAvailable()
-    $constant = new \ReflectionClassConstant(Stage::class, 'TEMPSTORE_STAGING_ROOT_KEY');
-    $this->tempStore->set($constant->getValue(), PackageManagerKernelTestBase::$testStagingRoot);
-
-    return parent::create($timeout);
-  }
-
   /**
    * {@inheritdoc}
    */
diff --git a/package_manager/tests/src/Kernel/StageTest.php b/package_manager/tests/src/Kernel/StageTest.php
index 7769e672d4..65fd2edd50 100644
--- a/package_manager/tests/src/Kernel/StageTest.php
+++ b/package_manager/tests/src/Kernel/StageTest.php
@@ -3,10 +3,8 @@
 namespace Drupal\Tests\package_manager\Kernel;
 
 use Drupal\Component\Datetime\Time;
-use Drupal\Component\FileSystem\FileSystem;
 use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\Extension\ModuleUninstallValidatorException;
-use Drupal\Core\Site\Settings;
 use Drupal\package_manager\Event\PostApplyEvent;
 use Drupal\package_manager\Event\PreApplyEvent;
 use Drupal\package_manager\Event\StageEvent;
@@ -27,24 +25,6 @@ use PhpTuf\ComposerStager\Domain\Service\Precondition\PreconditionInterface;
  */
 class StageTest extends PackageManagerKernelTestBase {
 
-  /**
-   * {@inheritdoc}
-   */
-  protected static $modules = ['system'];
-
-  /**
-   * {@inheritdoc}
-   */
-  protected function setUp(): void {
-    parent::setUp();
-
-    $this->installConfig('system');
-    $this->config('system.site')->set('uuid', $this->randomMachineName())->save();
-    // Ensure that the core update system thinks that System's post-update
-    // functions have run.
-    $this->registerPostUpdateFunctions();
-  }
-
   /**
    * {@inheritdoc}
    */
@@ -62,37 +42,33 @@ class StageTest extends PackageManagerKernelTestBase {
 
   /**
    * @covers ::getStageDirectory
-   * @covers ::getStagingRoot
    */
   public function testGetStageDirectory(): void {
-    // Ensure that a site ID was generated in ::setUp().
-    $site_id = $this->config('system.site')->get('uuid');
-    $this->assertNotEmpty($site_id);
-
-    // Even though we're using a virtual project, we want to test what happens
-    // when we aren't.
-    static::$testStagingRoot = NULL;
     // Don't mirror the active directory from the virtual project into the
     // real file system.
     Beginner::setFixturePath(NULL);
 
+    /** @var \Drupal\package_manager_bypass\PathLocator $path_locator */
+    $path_locator = $this->container->get('package_manager.path_locator');
+
     $stage = $this->createStage();
     $id = $stage->create();
-    // If the file_temp_path setting is empty, the stage directory should be
-    // created in the OS's temporary directory.
-    $this->assertEmpty(Settings::get('file_temp_path'));
-    $expected_dir = FileSystem::getOsTemporaryDirectory() . "/.package_manager$site_id/$id";
-    $this->assertSame($expected_dir, $stage->getStageDirectory());
-    // If the file_temp_path setting is changed, the existing stage shouldn't be
+    $stage_dir = $stage->getStageDirectory();
+    $this->assertStringStartsWith($path_locator->getStagingRoot() . '/', $stage_dir);
+    $this->assertStringEndsWith("/$id", $stage_dir);
+    // If the staging root is changed, the existing stage shouldn't be
     // affected...
-    $this->setSetting('file_temp_path', '/junk/drawer');
-    $this->assertSame($expected_dir, $stage->getStageDirectory());
+    $active_dir = $path_locator->getProjectRoot();
+    $path_locator->setPaths($active_dir, "$active_dir/vendor", '', '/junk/drawer');
+    $this->assertSame($stage_dir, $stage->getStageDirectory());
     $stage->destroy();
     // ...but a new stage should be.
     $stage = $this->createStage();
     $another_id = $stage->create();
     $this->assertNotSame($id, $another_id);
-    $this->assertSame("/junk/drawer/.package_manager$site_id/$another_id", $stage->getStageDirectory());
+    $stage_dir = $stage->getStageDirectory();
+    $this->assertStringStartsWith('/junk/drawer/', $stage_dir);
+    $this->assertStringEndsWith("/$another_id", $stage_dir);
   }
 
   /**
-- 
GitLab