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

Issue #3276031 by phenaproxima, kunal.sachdev: Create installed version validator

parent b2d03aa2
No related branches found
No related tags found
1 merge request!287Issue #3276031: Create installed version validator
...@@ -133,6 +133,10 @@ services: ...@@ -133,6 +133,10 @@ services:
class: Drupal\automatic_updates\Validator\XdebugValidator class: Drupal\automatic_updates\Validator\XdebugValidator
tags: tags:
- { name: event_subscriber } - { name: event_subscriber }
automatic_updates.validator.installed_version:
class: Drupal\automatic_updates\Validator\InstalledVersionValidator
tags:
- { name: event_subscriber }
automatic_updates.validator.target_release: automatic_updates.validator.target_release:
class: Drupal\automatic_updates\Validator\UpdateReleaseValidator class: Drupal\automatic_updates\Validator\UpdateReleaseValidator
tags: tags:
......
<?php
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\ProjectInfo;
use Drupal\automatic_updates\Updater;
use Drupal\Core\Extension\ExtensionVersion;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreOperationStageEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Validates that the site can update from the installed version of Drupal.
*/
class InstalledVersionValidator implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* Checks that the site can update from the installed version of Drupal.
*
* @param \Drupal\package_manager\Event\PreOperationStageEvent $event
* The event object.
*/
public function checkInstalledVersion(PreOperationStageEvent $event): void {
// This check only works with Automatic Updates.
if (!$event->getStage() instanceof Updater) {
return;
}
$installed_version = (new ProjectInfo('drupal'))->getInstalledVersion();
$extra = ExtensionVersion::createFromVersionString($installed_version)
->getVersionExtra();
if ($extra === 'dev') {
$message = $this->t('Drupal cannot be automatically updated from the installed version, @installed_version, because automatic updates from a dev version to any other version are not supported.', [
'@installed_version' => $installed_version,
]);
$event->addError([$message]);
}
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
return [
ReadinessCheckEvent::class => 'checkInstalledVersion',
PreCreateEvent::class => 'checkInstalledVersion',
];
}
}
...@@ -172,13 +172,9 @@ class UpdateVersionValidator implements EventSubscriberInterface { ...@@ -172,13 +172,9 @@ class UpdateVersionValidator implements EventSubscriberInterface {
'@from_version' => $from_version_string, '@from_version' => $from_version_string,
]; ];
$from_version = ExtensionVersion::createFromVersionString($from_version_string); $from_version = ExtensionVersion::createFromVersionString($from_version_string);
// @todo Return multiple validation messages and summary in // @todo Return multiple validation messages and summary in
// https://www.drupal.org/project/automatic_updates/issues/3272068. // https://www.drupal.org/project/automatic_updates/issues/3272068.
if ($from_version->getVersionExtra() === 'dev') {
return ValidationResult::createError([
$this->t('Drupal cannot be automatically updated from the installed version, @from_version, because automatic updates from a dev version to any other version are not supported.', $variables),
]);
}
if (Comparator::lessThan($to_version_string, $from_version_string)) { if (Comparator::lessThan($to_version_string, $from_version_string)) {
return ValidationResult::createError([ return ValidationResult::createError([
$this->t('Update version @to_version is lower than @from_version, downgrading is not supported.', $variables), $this->t('Update version @to_version is lower than @from_version, downgrading is not supported.', $variables),
......
<?php
namespace Drupal\Tests\automatic_updates\Kernel\ReadinessValidation;
use Drupal\package_manager\ValidationResult;
use Drupal\Tests\automatic_updates\Kernel\AutomaticUpdatesKernelTestBase;
/**
* @covers \Drupal\automatic_updates\Validator\InstalledVersionValidator
*
* @group automatic_updates
*/
class InstalledVersionValidatorTest extends AutomaticUpdatesKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['automatic_updates'];
/**
* Tests that the installed version of Drupal is checked for updateability.
*/
public function testInstalledVersionValidation(): void {
$this->setCoreVersion('9.8.0-dev');
$result = ValidationResult::createError([
'Drupal cannot be automatically updated from the installed version, 9.8.0-dev, because automatic updates from a dev version to any other version are not supported.',
]);
$this->assertCheckerResultsFromManager([$result], TRUE);
}
}
...@@ -256,6 +256,7 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -256,6 +256,7 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase {
$unstable_current_version = ValidationResult::createError([ $unstable_current_version = ValidationResult::createError([
'Drupal cannot be automatically updated during cron from its current version, 9.8.0-alpha1, because Automatic Updates only supports updating from stable versions during cron.', 'Drupal cannot be automatically updated during cron from its current version, 9.8.0-alpha1, because Automatic Updates only supports updating from stable versions during cron.',
]); ]);
return [ return [
'unstable current version, cron disabled' => [ 'unstable current version, cron disabled' => [
CronUpdater::DISABLED, CronUpdater::DISABLED,
...@@ -275,21 +276,6 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -275,21 +276,6 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase {
'9.8.0-alpha1', '9.8.0-alpha1',
[$unstable_current_version], [$unstable_current_version],
], ],
'dev current version, cron disabled' => [
CronUpdater::DISABLED,
'9.8.x-dev',
[],
],
'dev current version, security updates allowed' => [
CronUpdater::SECURITY,
'9.8.x-dev',
[],
],
'dev current version, all updates allowed' => [
CronUpdater::ALL,
'9.8.x-dev',
[],
],
// @todo In the 3 following test cases the installed version is not // @todo In the 3 following test cases the installed version is not
// in a supported branch. These test expectations should be changed or // in a supported branch. These test expectations should be changed or
// moved to a new test when we add a validator to check if the installed // moved to a new test when we add a validator to check if the installed
......
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