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

Issue #3314787 by kunal.sachdev, phenaproxima, tedbow: StagedDBUpdateValidator...

Issue #3314787 by kunal.sachdev, phenaproxima, tedbow: StagedDBUpdateValidator will throw an exception during status check if the stage exists, but is unclaimed
parent 1f509610
No related branches found
No related tags found
1 merge request!531Issue #3314787: StagedDBUpdateValidator will throw an exception during status check if the stage exists, but is unclaimed
...@@ -8,7 +8,6 @@ use Drupal\Core\Extension\ThemeExtensionList; ...@@ -8,7 +8,6 @@ use Drupal\Core\Extension\ThemeExtensionList;
use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\Event\StatusCheckEvent; use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\PathLocator; use Drupal\package_manager\PathLocator;
use Drupal\package_manager\Stage;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/** /**
...@@ -67,13 +66,15 @@ class StagedDBUpdateValidator implements EventSubscriberInterface { ...@@ -67,13 +66,15 @@ class StagedDBUpdateValidator implements EventSubscriberInterface {
* The event object. * The event object.
*/ */
public function checkForStagedDatabaseUpdates(StatusCheckEvent $event): void { public function checkForStagedDatabaseUpdates(StatusCheckEvent $event): void {
$stage = $event->getStage(); try {
if ($stage->isAvailable()) { $stage_dir = $event->getStage()->getStageDirectory();
// No staged updates exist, therefore we don't need to run this check. }
catch (\LogicException $e) {
// Stage directory can't be determined, so there's nothing to validate.
return; return;
} }
$extensions_with_updates = $this->getExtensionsWithDatabaseUpdates($stage); $extensions_with_updates = $this->getExtensionsWithDatabaseUpdates($stage_dir);
if ($extensions_with_updates) { if ($extensions_with_updates) {
$event->addWarning($extensions_with_updates, $this->t('Possible database updates have been detected in the following extensions.')); $event->addWarning($extensions_with_updates, $this->t('Possible database updates have been detected in the following extensions.'));
} }
...@@ -82,8 +83,8 @@ class StagedDBUpdateValidator implements EventSubscriberInterface { ...@@ -82,8 +83,8 @@ class StagedDBUpdateValidator implements EventSubscriberInterface {
/** /**
* Determines if a staged extension has changed update functions. * Determines if a staged extension has changed update functions.
* *
* @param \Drupal\package_manager\Stage $stage * @param string $stage_dir
* The updater which is controlling the update process. * The path of the staging area.
* @param \Drupal\Core\Extension\Extension $extension * @param \Drupal\Core\Extension\Extension $extension
* The extension to check. * The extension to check.
* *
...@@ -103,9 +104,8 @@ class StagedDBUpdateValidator implements EventSubscriberInterface { ...@@ -103,9 +104,8 @@ class StagedDBUpdateValidator implements EventSubscriberInterface {
* *
* @see https://www.drupal.org/project/automatic_updates/issues/3253828 * @see https://www.drupal.org/project/automatic_updates/issues/3253828
*/ */
public function hasStagedUpdates(Stage $stage, Extension $extension): bool { public function hasStagedUpdates(string $stage_dir, Extension $extension): bool {
$active_dir = $this->pathLocator->getProjectRoot(); $active_dir = $this->pathLocator->getProjectRoot();
$stage_dir = $stage->getStageDirectory();
$web_root = $this->pathLocator->getWebRoot(); $web_root = $this->pathLocator->getWebRoot();
if ($web_root) { if ($web_root) {
...@@ -159,21 +159,21 @@ class StagedDBUpdateValidator implements EventSubscriberInterface { ...@@ -159,21 +159,21 @@ class StagedDBUpdateValidator implements EventSubscriberInterface {
} }
/** /**
* Gets extensions that have database updates. * Gets extensions that have database updates in the staging area.
* *
* @param \Drupal\package_manager\Stage $stage * @param string $stage_dir
* The stage. * The path of the staging area.
* *
* @return string[] * @return string[]
* The names of the extensions that have possible database updates. * The names of the extensions that have possible database updates.
*/ */
public function getExtensionsWithDatabaseUpdates(Stage $stage): array { public function getExtensionsWithDatabaseUpdates(string $stage_dir): array {
$extensions_with_updates = []; $extensions_with_updates = [];
// Check all installed extensions for database updates. // Check all installed extensions for database updates.
$lists = [$this->moduleList, $this->themeList]; $lists = [$this->moduleList, $this->themeList];
foreach ($lists as $list) { foreach ($lists as $list) {
foreach ($list->getAllInstalledInfo() as $name => $info) { foreach ($list->getAllInstalledInfo() as $name => $info) {
if ($this->hasStagedUpdates($stage, $list->get($name))) { if ($this->hasStagedUpdates($stage_dir, $list->get($name))) {
$extensions_with_updates[] = $info['name']; $extensions_with_updates[] = $info['name'];
} }
} }
......
...@@ -5,7 +5,6 @@ namespace Drupal\package_manager_test_validation; ...@@ -5,7 +5,6 @@ namespace Drupal\package_manager_test_validation;
use Drupal\package_manager\Validator\StagedDBUpdateValidator as BaseValidator; use Drupal\package_manager\Validator\StagedDBUpdateValidator as BaseValidator;
use Drupal\Core\Extension\Extension; use Drupal\Core\Extension\Extension;
use Drupal\Core\State\StateInterface; use Drupal\Core\State\StateInterface;
use Drupal\package_manager\Stage;
/** /**
* Allows tests to dictate which extensions have staged database updates. * Allows tests to dictate which extensions have staged database updates.
...@@ -43,12 +42,12 @@ class StagedDatabaseUpdateValidator extends BaseValidator { ...@@ -43,12 +42,12 @@ class StagedDatabaseUpdateValidator extends BaseValidator {
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function hasStagedUpdates(Stage $stage, Extension $extension): bool { public function hasStagedUpdates(string $stage_dir, Extension $extension): bool {
$extensions = $this->state->get(static::class); $extensions = $this->state->get(static::class);
if (isset($extensions)) { if (isset($extensions)) {
return in_array($extension->getName(), $extensions, TRUE); return in_array($extension->getName(), $extensions, TRUE);
} }
return parent::hasStagedUpdates($stage, $extension); return parent::hasStagedUpdates($stage_dir, $extension);
} }
} }
...@@ -141,4 +141,15 @@ class StagedDBUpdateValidatorTest extends PackageManagerKernelTestBase { ...@@ -141,4 +141,15 @@ class StagedDBUpdateValidatorTest extends PackageManagerKernelTestBase {
$this->assertStatusCheckResults([$result], $stage); $this->assertStatusCheckResults([$result], $stage);
} }
/**
* Tests that the validator disregards unclaimed stages.
*/
public function testUnclaimedStage(): void {
$stage = $this->createStage();
$stage->create();
$this->assertStatusCheckResults([], $stage);
// A new, unclaimed stage should be ignored by the validator.
$this->assertStatusCheckResults([], $this->createStage());
}
} }
...@@ -53,7 +53,7 @@ class StagedDatabaseUpdateValidator implements EventSubscriberInterface { ...@@ -53,7 +53,7 @@ class StagedDatabaseUpdateValidator implements EventSubscriberInterface {
return; return;
} }
$invalid_extensions = $this->stagedDBUpdateValidator->getExtensionsWithDatabaseUpdates($stage); $invalid_extensions = $this->stagedDBUpdateValidator->getExtensionsWithDatabaseUpdates($stage->getStageDirectory());
if ($invalid_extensions) { if ($invalid_extensions) {
$event->addError($invalid_extensions, $this->t('The update cannot proceed because possible database updates have been detected in the following extensions.')); $event->addError($invalid_extensions, $this->t('The update cannot proceed because possible database updates have been detected in the following extensions.'));
} }
......
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