Skip to content
Snippets Groups Projects
Commit 3f3e11c9 authored by Adam G-H's avatar Adam G-H
Browse files

Issue #3253649 by phenaproxima: Validate update_fetch_with_http_fallback setting

parent 703b6fe3
No related branches found
No related tags found
1 merge request!144Issue #3253649: Validate update_fetch_with_http_fallback setting
......@@ -30,6 +30,12 @@ services:
- '@string_translation'
tags:
- { name: event_subscriber }
automatic_updates.validator.settings:
class: Drupal\automatic_updates\Validator\SettingsValidator
arguments:
- '@string_translation'
tags:
- { name: event_subscriber }
automatic_updates.update_version_validator:
class: Drupal\automatic_updates\Validator\UpdateVersionValidator
arguments:
......
<?php
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Updater;
use Drupal\Core\Site\Settings;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreOperationStageEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SettingsValidator implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Constructs a SettingsValidator object.
*
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* The string translation service.
*/
public function __construct(TranslationInterface $translation) {
$this->setStringTranslation($translation);
}
/**
* Validates site settings before an update starts.
*
* @param \Drupal\package_manager\Event\PreOperationStageEvent $event
* The event object.
*/
public function checkSettings(PreOperationStageEvent $event): void {
if ($event->getStage() instanceof Updater && Settings::get('update_fetch_with_http_fallback')) {
$event->addError([
$this->t('The <code>update_fetch_with_http_fallback</code> setting must be disabled for automatic updates.'),
]);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
ReadinessCheckEvent::class => 'checkSettings',
PreCreateEvent::class => 'checkSettings',
];
}
}
<?php
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\package_manager\Exception\StageValidationException;
use Drupal\package_manager\ValidationResult;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
/**
* @covers \Drupal\Tests\automatic_updates\Kernel\ReadinessValidation\SettingsValidatorTest
*
* @group automatic_updates
*/
class SettingsValidatorTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'automatic_updates',
'package_manager',
'package_manager_bypass',
];
/**
* Data provider for ::testSettingsValidation().
*
* @return array[]
* Sets of arguments to pass to the test method.
*/
public function providerSettingsValidation(): array {
$result = ValidationResult::createError([
'The <code>update_fetch_with_http_fallback</code> setting must be disabled for automatic updates.',
]);
return [
[TRUE, [$result]],
[FALSE, []],
];
}
/**
* Tests settings validation before starting an update.
*
* @param bool $setting
* The value of the update_fetch_with_http_fallback setting.
* @param \Drupal\package_manager\ValidationResult[] $expected_results
* The expected validation results.
*
* @dataProvider providerSettingsValidation
*/
public function testSettingsValidation(bool $setting, array $expected_results): void {
$this->setSetting('update_fetch_with_http_fallback', $setting);
$this->assertCheckerResultsFromManager($expected_results, TRUE);
try {
$this->container->get('automatic_updates.updater')->begin([
'drupal' => '9.8.1',
]);
// If there was no exception, ensure we're not expecting any errors.
$this->assertSame([], $expected_results);
}
catch (StageValidationException $e) {
$this->assertValidationResultsEqual($expected_results, $e->getResults());
}
}
}
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