From fbc845c72c08997831a07f6fd60b3fc12f85bb3a Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Mon, 7 Feb 2022 22:40:36 +0000
Subject: [PATCH] Issue #3262965 by phenaproxima: Use a test-only module to
 disable validators in functional tests

---
 ...c_updates_test_disable_validators.info.yml |  4 +++
 ...esTestDisableValidatorsServiceProvider.php | 24 +++++++++++++
 .../AutomaticUpdatesFunctionalTestBase.php    | 34 ++++++++++++-------
 3 files changed, 50 insertions(+), 12 deletions(-)
 create mode 100644 tests/modules/automatic_updates_test_disable_validators/automatic_updates_test_disable_validators.info.yml
 create mode 100644 tests/modules/automatic_updates_test_disable_validators/src/AutomaticUpdatesTestDisableValidatorsServiceProvider.php

diff --git a/tests/modules/automatic_updates_test_disable_validators/automatic_updates_test_disable_validators.info.yml b/tests/modules/automatic_updates_test_disable_validators/automatic_updates_test_disable_validators.info.yml
new file mode 100644
index 0000000000..be3bce29ef
--- /dev/null
+++ b/tests/modules/automatic_updates_test_disable_validators/automatic_updates_test_disable_validators.info.yml
@@ -0,0 +1,4 @@
+name: 'Automatic Updates Test - Disable validators'
+type: module
+description: 'Provides a mechanism to disable specific readiness validators during functional tests'
+package: Testing
diff --git a/tests/modules/automatic_updates_test_disable_validators/src/AutomaticUpdatesTestDisableValidatorsServiceProvider.php b/tests/modules/automatic_updates_test_disable_validators/src/AutomaticUpdatesTestDisableValidatorsServiceProvider.php
new file mode 100644
index 0000000000..1f009782b6
--- /dev/null
+++ b/tests/modules/automatic_updates_test_disable_validators/src/AutomaticUpdatesTestDisableValidatorsServiceProvider.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\automatic_updates_test_disable_validators;
+
+use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Drupal\Core\DependencyInjection\ServiceProviderBase;
+use Drupal\Core\Site\Settings;
+
+/**
+ * Disables specific readiness validators in the service container.
+ */
+class AutomaticUpdatesTestDisableValidatorsServiceProvider extends ServiceProviderBase {
+
+  /**
+   * {@inheritdoc}
+   */
+  public function alter(ContainerBuilder $container) {
+    parent::alter($container);
+
+    $validators = Settings::get('automatic_updates_test_disable_validators', []);
+    array_walk($validators, [$container, 'removeDefinition']);
+  }
+
+}
diff --git a/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
index 6f694e6180..acefb94b18 100644
--- a/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
+++ b/tests/src/Functional/AutomaticUpdatesFunctionalTestBase.php
@@ -2,7 +2,7 @@
 
 namespace Drupal\Tests\automatic_updates\Functional;
 
-use Drupal\Component\Serialization\Yaml;
+use Drupal\Core\Site\Settings;
 use Drupal\Tests\BrowserTestBase;
 
 /**
@@ -13,7 +13,11 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
   /**
    * {@inheritdoc}
    */
-  protected static $modules = ['update', 'update_test'];
+  protected static $modules = [
+    'automatic_updates_test_disable_validators',
+    'update',
+    'update_test',
+  ];
 
   /**
    * The service IDs of any validators to disable.
@@ -39,26 +43,32 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
   }
 
   /**
-   * Disables validators in the test site's services.yml.
+   * Disables validators in the test site's settings.
    *
    * This modifies the service container such that the disabled validators are
-   * instances of stdClass, and not subscribed to any events.
+   * not defined at all. This method will have no effect unless the
+   * automatic_updates_test_disable_validators module is installed.
    *
    * @param string[] $validators
    *   The service IDs of the validators to disable.
+   *
+   * @see \Drupal\automatic_updates_test_disable_validators\AutomaticUpdatesTestDisableValidatorsServiceProvider::alter()
    */
   protected function disableValidators(array $validators): void {
-    $services_file = $this->getDrupalRoot() . '/' . $this->siteDirectory . '/services.yml';
-    $this->assertFileIsWritable($services_file);
-    $services = file_get_contents($services_file);
-    $services = Yaml::decode($services);
+    $key = 'automatic_updates_test_disable_validators';
+    $disabled_validators = Settings::get($key, []);
 
     foreach ($validators as $service_id) {
-      $services['services'][$service_id] = [
-        'class' => 'stdClass',
-      ];
+      $disabled_validators[] = $service_id;
     }
-    file_put_contents($services_file, Yaml::encode($services));
+    $this->writeSettings([
+      'settings' => [
+        $key => (object) [
+          'value' => $disabled_validators,
+          'required' => TRUE,
+        ],
+      ],
+    ]);
     $this->rebuildContainer();
   }
 
-- 
GitLab