Skip to content
Snippets Groups Projects
Commit 55cb5675 authored by Kunal Sachdev's avatar Kunal Sachdev Committed by Ted Bowman
Browse files

Issue #3244358 by kunal.sachdev, tedbow: Add check to UpdateVersionValidator...

Issue #3244358 by kunal.sachdev, tedbow: Add check to UpdateVersionValidator to error on update version being less than the current version
parent d55c360c
No related branches found
No related tags found
1 merge request!83Issue #3244358: Add check to UpdateVersionValidator to error on update version being less than the current version.
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\automatic_updates\Validator; namespace Drupal\automatic_updates\Validator;
use Composer\Semver\Semver;
use Drupal\automatic_updates\AutomaticUpdatesEvents; use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\ReadinessCheckEvent; use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent; use Drupal\automatic_updates\Event\UpdateEvent;
...@@ -50,14 +51,23 @@ class UpdateVersionValidator implements EventSubscriberInterface { ...@@ -50,14 +51,23 @@ class UpdateVersionValidator implements EventSubscriberInterface {
* The event object. * The event object.
*/ */
public function checkUpdateVersion(UpdateEvent $event): void { public function checkUpdateVersion(UpdateEvent $event): void {
$from_version = ExtensionVersion::createFromVersionString($this->getCoreVersion()); $from_version_string = $this->getCoreVersion();
$from_version = ExtensionVersion::createFromVersionString($from_version_string);
$core_package_names = $event->getActiveComposer()->getCorePackageNames(); $core_package_names = $event->getActiveComposer()->getCorePackageNames();
// All the core packages will be updated to the same version, so it doesn't // All the core packages will be updated to the same version, so it doesn't
// matter which specific package we're looking at. // matter which specific package we're looking at.
$core_package_name = reset($core_package_names); $core_package_name = reset($core_package_names);
$to_version = ExtensionVersion::createFromVersionString($event->getPackageVersions()[$core_package_name]); $to_version_string = $event->getPackageVersions()[$core_package_name];
$to_version = ExtensionVersion::createFromVersionString($to_version_string);
if ($from_version->getMajorVersion() !== $to_version->getMajorVersion()) { if (Semver::satisfies($to_version_string, "< $from_version_string")) {
$messages[] = $this->t('Update version @to_version is lower than @from_version, downgrading is not supported.', [
'@to_version' => $to_version_string,
'@from_version' => $from_version_string,
]);
$error = ValidationResult::createError($messages);
$event->addValidationResult($error);
}
elseif ($from_version->getMajorVersion() !== $to_version->getMajorVersion()) {
$error = ValidationResult::createError([ $error = ValidationResult::createError([
$this->t('Updating from one major version to another is not supported.'), $this->t('Updating from one major version to another is not supported.'),
]); ]);
...@@ -69,6 +79,7 @@ class UpdateVersionValidator implements EventSubscriberInterface { ...@@ -69,6 +79,7 @@ class UpdateVersionValidator implements EventSubscriberInterface {
]); ]);
$event->addValidationResult($error); $event->addValidationResult($error);
} }
} }
/** /**
......
...@@ -46,4 +46,13 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase { ...@@ -46,4 +46,13 @@ class UpdateVersionValidatorTest extends AutomaticUpdatesKernelTestBase {
$this->assertCheckerResultsFromManager([$result], TRUE); $this->assertCheckerResultsFromManager([$result], TRUE);
} }
/**
* Tests an update version that is a lower version than the current.
*/
public function testDowngrading(): void {
$this->setCoreVersion('9.8.2');
$result = ValidationResult::createError(['Update version 9.8.1 is lower than 9.8.2, downgrading is not supported.']);
$this->assertCheckerResultsFromManager([$result], TRUE);
}
} }
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