Skip to content
Snippets Groups Projects
Commit bef240b8 authored by Yash Rode's avatar Yash Rode Committed by Adam G-H
Browse files

Issue #3308711 by yash.rode, phenaproxima: Dispatch StatusCheckEvent in...

Issue #3308711 by yash.rode, phenaproxima: Dispatch StatusCheckEvent in UpdateReady forms and do not allow the update to continue if there are errors
parent be131cb3
No related branches found
No related tags found
1 merge request!472Issue #3308711: Dispatch StatusCheckEvent in UpdateReady forms and do not allow the update to continue if there are errors
......@@ -4,6 +4,7 @@ namespace Drupal\automatic_updates\Form;
use Drupal\automatic_updates\BatchProcessor;
use Drupal\automatic_updates\Updater;
use Drupal\automatic_updates\Validation\ReadinessTrait;
use Drupal\automatic_updates\Validator\StagedDatabaseUpdateValidator;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Extension\ModuleExtensionList;
......@@ -13,9 +14,11 @@ use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\State\StateInterface;
use Drupal\package_manager\Exception\ApplyFailedException;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\Exception\StageException;
use Drupal\package_manager\Exception\StageOwnershipException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Defines a form to commit staged updates.
......@@ -25,6 +28,8 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
*/
final class UpdateReady extends FormBase {
use ReadinessTrait;
/**
* The updater service.
*
......@@ -60,6 +65,13 @@ final class UpdateReady extends FormBase {
*/
protected $renderer;
/**
* The event dispatcher.
*
* @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
*/
protected $eventDispatcher;
/**
* Constructs a new UpdateReady object.
*
......@@ -75,14 +87,17 @@ final class UpdateReady extends FormBase {
* The staged database update validator service.
* @param \Drupal\Core\Render\RendererInterface $renderer
* The renderer service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* Event dispatcher service.
*/
public function __construct(Updater $updater, MessengerInterface $messenger, StateInterface $state, ModuleExtensionList $module_list, StagedDatabaseUpdateValidator $staged_database_update_validator, RendererInterface $renderer) {
public function __construct(Updater $updater, MessengerInterface $messenger, StateInterface $state, ModuleExtensionList $module_list, StagedDatabaseUpdateValidator $staged_database_update_validator, RendererInterface $renderer, EventDispatcherInterface $event_dispatcher) {
$this->updater = $updater;
$this->setMessenger($messenger);
$this->state = $state;
$this->moduleList = $module_list;
$this->stagedDatabaseUpdateValidator = $staged_database_update_validator;
$this->renderer = $renderer;
$this->eventDispatcher = $event_dispatcher;
}
/**
......@@ -102,7 +117,8 @@ final class UpdateReady extends FormBase {
$container->get('state'),
$container->get('extension.list.module'),
$container->get('automatic_updates.validator.staged_database_updates'),
$container->get('renderer')
$container->get('renderer'),
$container->get('event_dispatcher')
);
}
......@@ -196,11 +212,21 @@ final class UpdateReady extends FormBase {
];
}
// Don't run the status checks once the form has been submitted.
if (!$form_state->getUserInput()) {
$event = new StatusCheckEvent($this->updater);
$this->eventDispatcher->dispatch($event);
/** @var \Drupal\package_manager\ValidationResult[] $results */
$results = $event->getResults();
if (!empty($results)) {
$this->displayResults($results, $this->messenger(), $this->renderer);
return $form;
}
}
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Continue'),
];
return $form;
}
......
......@@ -9,10 +9,12 @@ use Drupal\package_manager\Event\PostRequireEvent;
use Drupal\package_manager\Event\PreApplyEvent;
use Drupal\package_manager\Event\PostApplyEvent;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\ValidationResult;
use Drupal\automatic_updates_test\EventSubscriber\TestSubscriber1;
use Drupal\package_manager_bypass\Committer;
use Drupal\package_manager_bypass\Stager;
use Drupal\package_manager_test_validation\EventSubscriber\TestSubscriber;
use Drupal\system\SystemManager;
use Drupal\Tests\automatic_updates\Traits\ValidationTestTrait;
use Drupal\Tests\package_manager\Traits\PackageManagerBypassTestTrait;
......@@ -759,6 +761,26 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
$assert_session->buttonExists('Update');
}
/**
* Tests that update cannot be completed via the UI if a status check fails.
*/
public function testNoContinueOnError(): void {
$session = $this->getSession();
$assert_session = $this->assertSession();
$page = $session->getPage();
$this->setCoreVersion('9.8.0');
$this->checkForUpdates();
$this->drupalGet('/admin/modules/automatic-update');
$page->pressButton('Update to 9.8.1');
$this->checkForMetaRefresh();
$this->assertUpdateStagedTimes(1);
$error = ValidationResult::createError(['Error occured.']);
TestSubscriber::setTestResult([$error], StatusCheckEvent::class);
$this->getSession()->reload();
$assert_session->buttonNotExists('Continue');
$assert_session->buttonExists('Cancel update');
}
/**
* Sets an error message, runs readiness checks, and asserts it is displayed.
*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment