Skip to content
Snippets Groups Projects
Commit fbc845c7 authored by Adam G-H's avatar Adam G-H Committed by Ted Bowman
Browse files

Issue #3262965 by phenaproxima: Use a test-only module to disable validators in functional tests

parent 6794407e
No related branches found
No related tags found
No related merge requests found
name: 'Automatic Updates Test - Disable validators'
type: module
description: 'Provides a mechanism to disable specific readiness validators during functional tests'
package: Testing
<?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']);
}
}
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
namespace Drupal\Tests\automatic_updates\Functional; namespace Drupal\Tests\automatic_updates\Functional;
use Drupal\Component\Serialization\Yaml; use Drupal\Core\Site\Settings;
use Drupal\Tests\BrowserTestBase; use Drupal\Tests\BrowserTestBase;
/** /**
...@@ -13,7 +13,11 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { ...@@ -13,7 +13,11 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase {
/** /**
* {@inheritdoc} * {@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. * The service IDs of any validators to disable.
...@@ -39,26 +43,32 @@ abstract class AutomaticUpdatesFunctionalTestBase extends BrowserTestBase { ...@@ -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 * 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 * @param string[] $validators
* The service IDs of the validators to disable. * The service IDs of the validators to disable.
*
* @see \Drupal\automatic_updates_test_disable_validators\AutomaticUpdatesTestDisableValidatorsServiceProvider::alter()
*/ */
protected function disableValidators(array $validators): void { protected function disableValidators(array $validators): void {
$services_file = $this->getDrupalRoot() . '/' . $this->siteDirectory . '/services.yml'; $key = 'automatic_updates_test_disable_validators';
$this->assertFileIsWritable($services_file); $disabled_validators = Settings::get($key, []);
$services = file_get_contents($services_file);
$services = Yaml::decode($services);
foreach ($validators as $service_id) { foreach ($validators as $service_id) {
$services['services'][$service_id] = [ $disabled_validators[] = $service_id;
'class' => 'stdClass',
];
} }
file_put_contents($services_file, Yaml::encode($services)); $this->writeSettings([
'settings' => [
$key => (object) [
'value' => $disabled_validators,
'required' => TRUE,
],
],
]);
$this->rebuildContainer(); $this->rebuildContainer();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment