From d4ac1f79f50933cdc9834b4f8e0f8455fb3b5734 Mon Sep 17 00:00:00 2001
From: tedbow <tedbow@240860.no-reply.drupal.org>
Date: Sat, 26 Feb 2022 13:54:20 +0000
Subject: [PATCH] Issue #3266640 by tedbow: Simplify testing a pending update

---
 .../automatic_updates_test.install            | 17 +++++++++++
 .../automatic_updates_test.services.yml       |  4 ---
 .../AutomaticUpdatesTestServiceProvider.php   | 24 ---------------
 .../src/Form/TestUpdateReady.php              | 19 ------------
 .../src/Routing/RouteSubscriber.php           | 22 --------------
 .../Validator/TestPendingUpdatesValidator.php | 30 -------------------
 tests/src/Functional/UpdaterFormTest.php      | 13 +++-----
 7 files changed, 21 insertions(+), 108 deletions(-)
 create mode 100644 tests/modules/automatic_updates_test/automatic_updates_test.install
 delete mode 100644 tests/modules/automatic_updates_test/src/AutomaticUpdatesTestServiceProvider.php
 delete mode 100644 tests/modules/automatic_updates_test/src/Form/TestUpdateReady.php
 delete mode 100644 tests/modules/automatic_updates_test/src/Routing/RouteSubscriber.php
 delete mode 100644 tests/modules/automatic_updates_test/src/Validator/TestPendingUpdatesValidator.php

diff --git a/tests/modules/automatic_updates_test/automatic_updates_test.install b/tests/modules/automatic_updates_test/automatic_updates_test.install
new file mode 100644
index 0000000000..8bc60b1dae
--- /dev/null
+++ b/tests/modules/automatic_updates_test/automatic_updates_test.install
@@ -0,0 +1,17 @@
+<?php
+
+/**
+ * @file
+ * Contains install and update hooks.
+ */
+
+if (\Drupal::state()->get('automatic_updates_test.new_update')) {
+
+  /**
+   * Dynamic automatic_updates_update_9001.
+   */
+  function automatic_updates_update_9001(&$sandbox) {
+
+  }
+
+}
diff --git a/tests/modules/automatic_updates_test/automatic_updates_test.services.yml b/tests/modules/automatic_updates_test/automatic_updates_test.services.yml
index 2c7c10f707..165cde6e64 100644
--- a/tests/modules/automatic_updates_test/automatic_updates_test.services.yml
+++ b/tests/modules/automatic_updates_test/automatic_updates_test.services.yml
@@ -1,8 +1,4 @@
 services:
-  automatic_updates_test.route_subscriber:
-    class: \Drupal\automatic_updates_test\Routing\RouteSubscriber
-    tags:
-      - { name: event_subscriber }
   automatic_updates_test.checker:
     class: Drupal\automatic_updates_test\EventSubscriber\TestSubscriber1
     tags:
diff --git a/tests/modules/automatic_updates_test/src/AutomaticUpdatesTestServiceProvider.php b/tests/modules/automatic_updates_test/src/AutomaticUpdatesTestServiceProvider.php
deleted file mode 100644
index 50e1b43149..0000000000
--- a/tests/modules/automatic_updates_test/src/AutomaticUpdatesTestServiceProvider.php
+++ /dev/null
@@ -1,24 +0,0 @@
-<?php
-
-namespace Drupal\automatic_updates_test;
-
-use Drupal\automatic_updates_test\Validator\TestPendingUpdatesValidator;
-use Drupal\Core\DependencyInjection\ContainerBuilder;
-use Drupal\Core\DependencyInjection\ServiceProviderBase;
-
-/**
- * Modifies container services for testing purposes.
- */
-class AutomaticUpdatesTestServiceProvider extends ServiceProviderBase {
-
-  /**
-   * {@inheritdoc}
-   */
-  public function alter(ContainerBuilder $container) {
-    parent::alter($container);
-
-    $container->getDefinition('package_manager.validator.pending_updates')
-      ->setClass(TestPendingUpdatesValidator::class);
-  }
-
-}
diff --git a/tests/modules/automatic_updates_test/src/Form/TestUpdateReady.php b/tests/modules/automatic_updates_test/src/Form/TestUpdateReady.php
deleted file mode 100644
index fb749d88bb..0000000000
--- a/tests/modules/automatic_updates_test/src/Form/TestUpdateReady.php
+++ /dev/null
@@ -1,19 +0,0 @@
-<?php
-
-namespace Drupal\automatic_updates_test\Form;
-
-use Drupal\automatic_updates\Form\UpdateReady;
-
-/**
- * A test-only version of the form displayed before applying an update.
- */
-class TestUpdateReady extends UpdateReady {
-
-  /**
-   * {@inheritdoc}
-   */
-  protected function getModulesWithStagedDatabaseUpdates(): array {
-    return $this->state->get('automatic_updates_test.staged_database_updates', parent::getModulesWithStagedDatabaseUpdates());
-  }
-
-}
diff --git a/tests/modules/automatic_updates_test/src/Routing/RouteSubscriber.php b/tests/modules/automatic_updates_test/src/Routing/RouteSubscriber.php
deleted file mode 100644
index 3edebbeb35..0000000000
--- a/tests/modules/automatic_updates_test/src/Routing/RouteSubscriber.php
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php
-
-namespace Drupal\automatic_updates_test\Routing;
-
-use Drupal\automatic_updates_test\Form\TestUpdateReady;
-use Drupal\Core\Routing\RouteSubscriberBase;
-use Symfony\Component\Routing\RouteCollection;
-
-/**
- * Alters route definitions for testing purposes.
- */
-class RouteSubscriber extends RouteSubscriberBase {
-
-  /**
-   * {@inheritdoc}
-   */
-  protected function alterRoutes(RouteCollection $collection) {
-    $collection->get('automatic_updates.confirmation_page')
-      ->setDefault('_form', TestUpdateReady::class);
-  }
-
-}
diff --git a/tests/modules/automatic_updates_test/src/Validator/TestPendingUpdatesValidator.php b/tests/modules/automatic_updates_test/src/Validator/TestPendingUpdatesValidator.php
deleted file mode 100644
index f12963c7b4..0000000000
--- a/tests/modules/automatic_updates_test/src/Validator/TestPendingUpdatesValidator.php
+++ /dev/null
@@ -1,30 +0,0 @@
-<?php
-
-namespace Drupal\automatic_updates_test\Validator;
-
-use Drupal\package_manager\Validator\PendingUpdatesValidator;
-
-/**
- * Defines a test-only implementation of the pending updates validator.
- */
-class TestPendingUpdatesValidator extends PendingUpdatesValidator {
-
-  /**
-   * {@inheritdoc}
-   */
-  public function updatesExist(): bool {
-    $pending_updates = \Drupal::state()
-      ->get('automatic_updates_test.staged_database_updates', []);
-
-    // If the System module should expose a pending update, create one that will
-    // be detected by the update hook registry. We only do this for System so
-    // that there is NO way we could possibly evaluate any user input (i.e.,
-    // if malicious code were somehow injected into state).
-    if (array_key_exists('system', $pending_updates)) {
-      // @codingStandardsIgnoreLine
-      eval('function system_update_4294967294() {}');
-    }
-    return parent::updatesExist();
-  }
-
-}
diff --git a/tests/src/Functional/UpdaterFormTest.php b/tests/src/Functional/UpdaterFormTest.php
index 2e4f6ed525..69093a54d9 100644
--- a/tests/src/Functional/UpdaterFormTest.php
+++ b/tests/src/Functional/UpdaterFormTest.php
@@ -363,15 +363,10 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
     $this->checkForMetaRefresh();
     $this->assertUpdateStagedTimes(1);
     $this->assertUpdateReady();
-    // Simulate a staged database update in the System module. We must do this
-    // after the update has started, because the pending updates validator
-    // will prevent an update from starting.
-    $this->container->get('state')
-      ->set('automatic_updates_test.staged_database_updates', [
-        'system' => [
-          'name' => 'System',
-        ],
-      ]);
+    // Simulate a staged database update in the automatic_updates_test module.
+    // We must do this after the update has started, because the pending updates
+    // validator will prevent an update from starting.
+    $this->container->get('state')->set('automatic_updates_test.new_update', TRUE);
     // The warning from the updater form should be not be repeated, but we
     // should see a warning about pending database updates, and once the staged
     // changes have been applied, we should be redirected to update.php, where
-- 
GitLab