From b2679cc1776b2c9e7d11100fd48494840b2dc3b7 Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Tue, 5 Apr 2022 18:27:23 +0000
Subject: [PATCH] Issue #3271078 by phenaproxima, tedbow: Tests make requests
 to updates.drupal.org instead of test fixtures sometimes

---
 .../tests/src/Functional/UpdaterFormTest.php  |  2 +-
 .../automatic_updates_test.routing.yml        |  9 ----
 .../src/TestController.php                    | 35 ---------------
 ...atic_updates_test_release_history.info.yml |  7 +++
 ...c_updates_test_release_history.routing.yml |  9 ++++
 .../src/TestController.php                    | 45 +++++++++++++++++++
 tests/src/Build/UpdateTestBase.php            |  4 +-
 .../AutomaticUpdatesFunctionalTestBase.php    | 21 +++++++--
 tests/src/Functional/UpdateLockTest.php       |  1 -
 9 files changed, 82 insertions(+), 51 deletions(-)
 create mode 100644 tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.info.yml
 create mode 100644 tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.routing.yml
 create mode 100644 tests/modules/automatic_updates_test_release_history/src/TestController.php

diff --git a/automatic_updates_extensions/tests/src/Functional/UpdaterFormTest.php b/automatic_updates_extensions/tests/src/Functional/UpdaterFormTest.php
index 73b062bc80..dc51055e58 100644
--- a/automatic_updates_extensions/tests/src/Functional/UpdaterFormTest.php
+++ b/automatic_updates_extensions/tests/src/Functional/UpdaterFormTest.php
@@ -54,7 +54,7 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
    */
   protected function setProjectInstalledVersion($version) {
     $this->config('update.settings')
-      ->set('fetch.url', $this->baseUrl . '/automatic-update-test')
+      ->set('fetch.url', $this->baseUrl . '/test-release-history')
       ->save();
     $system_info = [
       $this->updateProject => [
diff --git a/tests/modules/automatic_updates_test/automatic_updates_test.routing.yml b/tests/modules/automatic_updates_test/automatic_updates_test.routing.yml
index a25a826a86..c7bc51971b 100644
--- a/tests/modules/automatic_updates_test/automatic_updates_test.routing.yml
+++ b/tests/modules/automatic_updates_test/automatic_updates_test.routing.yml
@@ -1,12 +1,3 @@
-automatic_updates_test.metadata:
-  path: '/automatic-update-test/{project_name}/{version}'
-  defaults:
-    _title: 'Update test'
-    _controller: '\Drupal\automatic_updates_test\TestController::metadata'
-  requirements:
-    _access: 'TRUE'
-  options:
-    _maintenance_access: TRUE
 automatic_updates_test.update:
   path: '/automatic-update-test/update/{to_version}'
   defaults:
diff --git a/tests/modules/automatic_updates_test/src/TestController.php b/tests/modules/automatic_updates_test/src/TestController.php
index 083b2ea592..82dbb17b66 100644
--- a/tests/modules/automatic_updates_test/src/TestController.php
+++ b/tests/modules/automatic_updates_test/src/TestController.php
@@ -6,7 +6,6 @@ use Drupal\automatic_updates\Exception\UpdateException;
 use Drupal\Component\Utility\Environment;
 use Drupal\Core\Controller\ControllerBase;
 use Drupal\Core\Render\HtmlResponse;
-use Symfony\Component\HttpFoundation\BinaryFileResponse;
 use Symfony\Component\HttpFoundation\Response;
 
 class TestController extends ControllerBase {
@@ -62,38 +61,4 @@ class TestController extends ControllerBase {
     return new HtmlResponse($content, $status);
   }
 
-  /**
-   * Page callback: Prints mock XML for the Update Manager module.
-   *
-   * This is a wholesale copy of
-   * \Drupal\update_test\Controller\UpdateTestController::updateTest() for
-   * testing automatic updates. This was done in order to use a different
-   * directory of mock XML files.
-   */
-  public function metadata($project_name = 'drupal', $version = NULL): Response {
-    $xml_map = $this->config('update_test.settings')->get('xml_map');
-    if (isset($xml_map[$project_name])) {
-      $availability_scenario = $xml_map[$project_name];
-    }
-    elseif (isset($xml_map['#all'])) {
-      $availability_scenario = $xml_map['#all'];
-    }
-    else {
-      // The test didn't specify (for example, the webroot has other modules and
-      // themes installed but they're disabled by the version of the site
-      // running the test. So, we default to a file we know won't exist, so at
-      // least we'll get an empty xml response instead of a bunch of Drupal page
-      // output.
-      $availability_scenario = '#broken#';
-    }
-
-    $file = __DIR__ . "/../../../fixtures/release-history/$project_name.$availability_scenario.xml";
-    $headers = ['Content-Type' => 'text/xml; charset=utf-8'];
-    if (!is_file($file)) {
-      // Return an empty response.
-      return new Response('', 200, $headers);
-    }
-    return new BinaryFileResponse($file, 200, $headers);
-  }
-
 }
diff --git a/tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.info.yml b/tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.info.yml
new file mode 100644
index 0000000000..a66c6682cc
--- /dev/null
+++ b/tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.info.yml
@@ -0,0 +1,7 @@
+name: 'Automatic Updates Test - Release history'
+type: module
+description: 'Provides a mechanism for serving fake release history metadata in functional tests.'
+package: Testing
+dependencies:
+  - drupal:update
+  - drupal:update_test
diff --git a/tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.routing.yml b/tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.routing.yml
new file mode 100644
index 0000000000..2adff0876e
--- /dev/null
+++ b/tests/modules/automatic_updates_test_release_history/automatic_updates_test_release_history.routing.yml
@@ -0,0 +1,9 @@
+automatic_updates_test_release_history.metadata:
+  path: '/test-release-history/{project_name}/{version}'
+  defaults:
+    _title: 'Update test'
+    _controller: '\Drupal\automatic_updates_test_release_history\TestController::metadata'
+  requirements:
+    _access: 'TRUE'
+  options:
+    _maintenance_access: TRUE
diff --git a/tests/modules/automatic_updates_test_release_history/src/TestController.php b/tests/modules/automatic_updates_test_release_history/src/TestController.php
new file mode 100644
index 0000000000..6e76fccf32
--- /dev/null
+++ b/tests/modules/automatic_updates_test_release_history/src/TestController.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Drupal\automatic_updates_test_release_history;
+
+use Drupal\Core\Controller\ControllerBase;
+use Symfony\Component\HttpFoundation\BinaryFileResponse;
+use Symfony\Component\HttpFoundation\Response;
+
+class TestController extends ControllerBase {
+
+  /**
+   * Page callback: Prints mock XML for the Update Manager module.
+   *
+   * This is a wholesale copy of
+   * \Drupal\update_test\Controller\UpdateTestController::updateTest() for
+   * testing automatic updates. This was done in order to use a different
+   * directory of mock XML files.
+   */
+  public function metadata($project_name = 'drupal', $version = NULL): Response {
+    $xml_map = $this->config('update_test.settings')->get('xml_map');
+    if (isset($xml_map[$project_name])) {
+      $availability_scenario = $xml_map[$project_name];
+    }
+    elseif (isset($xml_map['#all'])) {
+      $availability_scenario = $xml_map['#all'];
+    }
+    else {
+      // The test didn't specify (for example, the webroot has other modules and
+      // themes installed but they're disabled by the version of the site
+      // running the test. So, we default to a file we know won't exist, so at
+      // least we'll get an empty xml response instead of a bunch of Drupal page
+      // output.
+      $availability_scenario = '#broken#';
+    }
+
+    $file = __DIR__ . "/../../../fixtures/release-history/$project_name.$availability_scenario.xml";
+    $headers = ['Content-Type' => 'text/xml; charset=utf-8'];
+    if (!is_file($file)) {
+      // Return an empty response.
+      return new Response('', 200, $headers);
+    }
+    return new BinaryFileResponse($file, 200, $headers);
+  }
+
+}
diff --git a/tests/src/Build/UpdateTestBase.php b/tests/src/Build/UpdateTestBase.php
index eb859abb1d..cb93d1dcf6 100644
--- a/tests/src/Build/UpdateTestBase.php
+++ b/tests/src/Build/UpdateTestBase.php
@@ -39,7 +39,7 @@ abstract class UpdateTestBase extends TemplateProjectTestBase {
     $this->installModules([
       'automatic_updates',
       'automatic_updates_test',
-      'update_test',
+      'automatic_updates_test_release_history',
     ]);
   }
 
@@ -65,7 +65,7 @@ END;
       $port = $this->findAvailablePort();
       $this->metadataServer = $this->instantiateServer($port);
       $code .= <<<END
-\$config['update.settings']['fetch']['url'] = 'http://localhost:$port/automatic-update-test';
+\$config['update.settings']['fetch']['url'] = 'http://localhost:$port/test-release-history';
 END;
     }
     $this->writeSettings($code);
diff --git a/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
index 3938ce6699..9864fa6831 100644
--- a/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
+++ b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
@@ -4,6 +4,7 @@ namespace Drupal\Tests\automatic_updates\Functional;
 
 use Drupal\Core\Site\Settings;
 use Drupal\Tests\BrowserTestBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;
 
 /**
  * Base class for functional tests of the Automatic Updates module.
@@ -16,8 +17,6 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
   protected static $modules = [
     'automatic_updates_test_disable_validators',
     'package_manager_bypass',
-    'update',
-    'update_test',
   ];
 
   /**
@@ -54,6 +53,22 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
     $this->disableValidators($this->disableValidators);
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  protected function installModulesFromClassProperty(ContainerInterface $container) {
+    $container->get('module_installer')->install([
+      'automatic_updates_test_release_history',
+    ]);
+    $this->container = $container->get('kernel')->getContainer();
+
+    // To prevent tests from making real requests to the Internet, use fake
+    // release metadata that exposes a pretend Drupal 9.8.2 release.
+    $this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.9.8.2.xml');
+
+    parent::installModulesFromClassProperty($container);
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -117,7 +132,7 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
    */
   protected function setReleaseMetadata(string $file): void {
     $this->config('update.settings')
-      ->set('fetch.url', $this->baseUrl . '/automatic-update-test')
+      ->set('fetch.url', $this->baseUrl . '/test-release-history')
       ->save();
 
     [$project, $fixture] = explode('.', basename($file, '.xml'), 2);
diff --git a/tests/src/Functional/UpdateLockTest.php b/tests/src/Functional/UpdateLockTest.php
index 2c41af418d..07634ed400 100644
--- a/tests/src/Functional/UpdateLockTest.php
+++ b/tests/src/Functional/UpdateLockTest.php
@@ -31,7 +31,6 @@ class UpdateLockTest extends AutomaticUpdatesFunctionalTestBase {
   protected function setUp(): void {
     parent::setUp();
 
-    $this->setReleaseMetadata(__DIR__ . '/../../fixtures/release-history/drupal.9.8.2.xml');
     $this->drupalLogin($this->rootUser);
     $this->checkForUpdates();
   }
-- 
GitLab