From 02041b3509f8210e52d671186a0d662e96f74bc3 Mon Sep 17 00:00:00 2001
From: "kunal.sachdev" <kunal.sachdev@3685163.no-reply.drupal.org>
Date: Mon, 2 May 2022 15:01:02 +0000
Subject: [PATCH] Issue #3275316 by kunal.sachdev: Change
 AutomaticUpdatesKernelTestBase::setReleaseMetadata() to return metadata for
 different projects based on request path

---
 .../Kernel/AutomaticUpdatesKernelTestBase.php | 20 +++++++---
 tests/src/Kernel/CronUpdaterTest.php          | 23 +++++------
 tests/src/Kernel/ProjectInfoTest.php          | 38 +------------------
 .../UpdateVersionValidatorTest.php            |  2 +-
 tests/src/Kernel/ReleaseChooserTest.php       |  2 +-
 tests/src/Kernel/UpdaterTest.php              |  2 +-
 6 files changed, 32 insertions(+), 55 deletions(-)

diff --git a/tests/src/Kernel/AutomaticUpdatesKernelTestBase.php b/tests/src/Kernel/AutomaticUpdatesKernelTestBase.php
index da4da98abc..a3c260ac61 100644
--- a/tests/src/Kernel/AutomaticUpdatesKernelTestBase.php
+++ b/tests/src/Kernel/AutomaticUpdatesKernelTestBase.php
@@ -13,6 +13,7 @@ use GuzzleHttp\Handler\MockHandler;
 use GuzzleHttp\HandlerStack;
 use GuzzleHttp\Psr7\Response;
 use GuzzleHttp\Psr7\Utils;
+use Psr\Http\Message\RequestInterface;
 
 /**
  * Base class for kernel tests of the Automatic Updates module.
@@ -67,7 +68,7 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
     // By default, pretend we're running Drupal core 9.8.0 and a non-security
     // update to 9.8.1 is available.
     $this->setCoreVersion('9.8.1');
-    $this->setReleaseMetadata([__DIR__ . '/../../fixtures/release-history/drupal.9.8.2.xml']);
+    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2.xml']);
 
     // Set a last cron run time so that the cron frequency validator will run
     // from a sane state.
@@ -118,15 +119,24 @@ abstract class AutomaticUpdatesKernelTestBase extends PackageManagerKernelTestBa
    * Sets the release metadata file to use when fetching available updates.
    *
    * @param string[] $files
-   *   The paths of the XML metadata files to use.
+   *   The paths of the XML metadata files to use, keyed by project name.
    */
   protected function setReleaseMetadata(array $files): void {
     $responses = [];
-    foreach ($files as $file) {
+
+    foreach ($files as $project => $file) {
       $metadata = Utils::tryFopen($file, 'r');
-      $responses[] = new Response(200, [], Utils::streamFor($metadata));
+      $responses["/release-history/$project/current"] = new Response(200, [], Utils::streamFor($metadata));
     }
-    $handler = new MockHandler($responses);
+    $callable = function (RequestInterface $request) use ($responses): Response {
+      return $responses[$request->getUri()->getPath()] ?? new Response(404);
+    };
+
+    // The mock handler's queue consist of same callable as many times as the
+    // number of requests we expect to be made for update XML because it will
+    // retrieve one item off the queue for each request.
+    // @see \GuzzleHttp\Handler\MockHandler::__invoke()
+    $handler = new MockHandler(array_fill(0, count($responses), $callable));
     $this->client = new Client([
       'handler' => HandlerStack::create($handler),
     ]);
diff --git a/tests/src/Kernel/CronUpdaterTest.php b/tests/src/Kernel/CronUpdaterTest.php
index b40f6a1de7..9c2b57b167 100644
--- a/tests/src/Kernel/CronUpdaterTest.php
+++ b/tests/src/Kernel/CronUpdaterTest.php
@@ -89,32 +89,32 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
     return [
       'disabled, normal release' => [
         CronUpdater::DISABLED,
-        "$fixture_dir/drupal.9.8.2.xml",
+        ['drupal' => "$fixture_dir/drupal.9.8.2.xml"],
         FALSE,
       ],
       'disabled, security release' => [
         CronUpdater::DISABLED,
-        "$fixture_dir/drupal.9.8.1-security.xml",
+        ['drupal' => "$fixture_dir/drupal.9.8.1-security.xml"],
         FALSE,
       ],
       'security only, security release' => [
         CronUpdater::SECURITY,
-        "$fixture_dir/drupal.9.8.1-security.xml",
+        ['drupal' => "$fixture_dir/drupal.9.8.1-security.xml"],
         TRUE,
       ],
       'security only, normal release' => [
         CronUpdater::SECURITY,
-        "$fixture_dir/drupal.9.8.2.xml",
+        ['drupal' => "$fixture_dir/drupal.9.8.2.xml"],
         FALSE,
       ],
       'enabled, normal release' => [
         CronUpdater::ALL,
-        "$fixture_dir/drupal.9.8.2.xml",
+        ['drupal' => "$fixture_dir/drupal.9.8.2.xml"],
         TRUE,
       ],
       'enabled, security release' => [
         CronUpdater::ALL,
-        "$fixture_dir/drupal.9.8.1-security.xml",
+        ['drupal' => "$fixture_dir/drupal.9.8.1-security.xml"],
         TRUE,
       ],
     ];
@@ -126,18 +126,19 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
    * @param string $setting
    *   Whether automatic updates should be enabled during cron. Possible values
    *   are 'disable', 'security', and 'patch'.
-   * @param string $release_data
+   * @param array $release_data
    *   If automatic updates are enabled, the path of the fake release metadata
-   *   that should be served when fetching information on available updates.
+   *   that should be served when fetching information on available updates,
+   *   keyed by project name.
    * @param bool $will_update
    *   Whether an update should be performed, given the previous two arguments.
    *
    * @dataProvider providerUpdaterCalled
    */
-  public function testUpdaterCalled(string $setting, string $release_data, bool $will_update): void {
+  public function testUpdaterCalled(string $setting, array $release_data, bool $will_update): void {
     // Our form alter does not refresh information on available updates, so
     // ensure that the appropriate update data is loaded beforehand.
-    $this->setReleaseMetadata([$release_data]);
+    $this->setReleaseMetadata($release_data);
     $this->setCoreVersion('9.8.0');
     update_get_available(TRUE);
 
@@ -247,7 +248,7 @@ class CronUpdaterTest extends AutomaticUpdatesKernelTestBase {
     $this->installConfig('automatic_updates');
     $this->setCoreVersion('9.8.0');
     // Ensure that there is a security release to which we should update.
-    $this->setReleaseMetadata([__DIR__ . "/../../fixtures/release-history/drupal.9.8.1-security.xml"]);
+    $this->setReleaseMetadata(['drupal' => __DIR__ . "/../../fixtures/release-history/drupal.9.8.1-security.xml"]);
 
     // If the pre- or post-destroy events throw an exception, it will not be
     // caught by the cron updater, but it *will* be caught by the main cron
diff --git a/tests/src/Kernel/ProjectInfoTest.php b/tests/src/Kernel/ProjectInfoTest.php
index 15c8c14238..e19e20b2d5 100644
--- a/tests/src/Kernel/ProjectInfoTest.php
+++ b/tests/src/Kernel/ProjectInfoTest.php
@@ -3,12 +3,6 @@
 namespace Drupal\Tests\automatic_updates\Kernel;
 
 use Drupal\automatic_updates\ProjectInfo;
-use GuzzleHttp\Client;
-use GuzzleHttp\Handler\MockHandler;
-use GuzzleHttp\HandlerStack;
-use GuzzleHttp\Psr7\Response;
-use GuzzleHttp\Psr7\Utils;
-use Psr\Http\Message\RequestInterface;
 
 /**
  * @coversDefaultClass \Drupal\automatic_updates\ProjectInfo
@@ -49,7 +43,7 @@ class ProjectInfoTest extends AutomaticUpdatesKernelTestBase {
       $metadata_fixtures['drupal'] = $fixtures_directory . 'drupal.9.8.2.xml';
     }
     $metadata_fixtures[$project] = "$fixtures_directory$fixture";
-    $this->setReleaseMetadataForProjects($metadata_fixtures);
+    $this->setReleaseMetadata($metadata_fixtures);
     $project_info = new ProjectInfo($project);
     $actual_releases = $project_info->getInstallableReleases();
     // Assert that we returned the correct releases in the expected order.
@@ -119,39 +113,11 @@ class ProjectInfoTest extends AutomaticUpdatesKernelTestBase {
    * @covers ::getInstallableReleases()
    */
   public function testNotPublishedProject() {
-    $this->setReleaseMetadataForProjects(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2_unknown_status.xml']);
+    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2_unknown_status.xml']);
     $project_info = new ProjectInfo('drupal');
     $this->expectException('RuntimeException');
     $this->expectExceptionMessage("The project 'drupal' can not be updated because its status is any status besides published");
     $project_info->getInstallableReleases();
   }
 
-  /**
-   * Sets the release metadata file to use when fetching available updates.
-   *
-   * @param string[] $files
-   *   The paths of the XML metadata files to use, keyed by project name.
-   */
-  protected function setReleaseMetadataForProjects(array $files): void {
-    $responses = [];
-
-    foreach ($files as $project => $file) {
-      $metadata = Utils::tryFopen($file, 'r');
-      $responses["/release-history/$project/current"] = new Response(200, [], Utils::streamFor($metadata));
-    }
-    $callable = function (RequestInterface $request) use ($responses): Response {
-      return $responses[$request->getUri()->getPath()] ?? new Response(404);
-    };
-
-    // The mock handler's queue consist of same callable as many times as the
-    // number of requests we expect to be made for update XML because it will
-    // retrieve one item off the queue for each request.
-    // @see \GuzzleHttp\Handler\MockHandler::__invoke()
-    $handler = new MockHandler(array_fill(0, count($responses), $callable));
-    $this->client = new Client([
-      'handler' => HandlerStack::create($handler),
-    ]);
-    $this->container->set('http_client', $this->client);
-  }
-
 }
diff --git a/tests/src/Kernel/ReadinessValidation/UpdateVersionValidatorTest.php b/tests/src/Kernel/ReadinessValidation/UpdateVersionValidatorTest.php
index 06d24f3bdd..0d6aae08e7 100644
--- a/tests/src/Kernel/ReadinessValidation/UpdateVersionValidatorTest.php
+++ b/tests/src/Kernel/ReadinessValidation/UpdateVersionValidatorTest.php
@@ -131,7 +131,7 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase {
     // In order to test what happens when only security updates are enabled
     // during cron (the default behavior), ensure that the latest available
     // release is a security update.
-    $this->setReleaseMetadata([__DIR__ . '/../../../fixtures/release-history/drupal.9.8.1-security.xml']);
+    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../../fixtures/release-history/drupal.9.8.1-security.xml']);
 
     $this->setCoreVersion('9.7.1');
     $this->assertCheckerResultsFromManager([], TRUE);
diff --git a/tests/src/Kernel/ReleaseChooserTest.php b/tests/src/Kernel/ReleaseChooserTest.php
index 10d2177d80..1576f53947 100644
--- a/tests/src/Kernel/ReleaseChooserTest.php
+++ b/tests/src/Kernel/ReleaseChooserTest.php
@@ -21,7 +21,7 @@ class ReleaseChooserTest extends AutomaticUpdatesKernelTestBase {
    */
   protected function setUp(): void {
     parent::setUp();
-    $this->setReleaseMetadata([__DIR__ . '/../../fixtures/release-history/drupal.9.8.2-older-sec-release.xml']);
+    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.2-older-sec-release.xml']);
 
   }
 
diff --git a/tests/src/Kernel/UpdaterTest.php b/tests/src/Kernel/UpdaterTest.php
index 613764d670..9a5662bf46 100644
--- a/tests/src/Kernel/UpdaterTest.php
+++ b/tests/src/Kernel/UpdaterTest.php
@@ -37,7 +37,7 @@ class UpdaterTest extends AutomaticUpdatesKernelTestBase {
     // Simulate that we're running Drupal 9.8.0 and a security update to 9.8.1
     // is available.
     $this->setCoreVersion('9.8.0');
-    $this->setReleaseMetadata([__DIR__ . '/../../fixtures/release-history/drupal.9.8.1-security.xml']);
+    $this->setReleaseMetadata(['drupal' => __DIR__ . '/../../fixtures/release-history/drupal.9.8.1-security.xml']);
 
     // Create a user who will own the stage even after the container is rebuilt.
     $user = $this->createUser([], NULL, TRUE, ['uid' => 2]);
-- 
GitLab