Commit 0ae874a3 authored by Kunal Sachdev's avatar Kunal Sachdev Committed by Ted Bowman
Browse files

Issue #3314805 by phenaproxima, kunal.sachdev: Create a base class for...

Issue #3314805 by phenaproxima, kunal.sachdev: Create a base class for UpdaterForm and UpdateReady to help run status checks and display the results
parent a7e2c9d0
Loading
Loading
Loading
Loading
+3 −26
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@

namespace Drupal\automatic_updates_extensions\Form;

use Drupal\automatic_updates\Validation\ReadinessTrait;
use Drupal\automatic_updates\Form\UpdateFormBase;
use Drupal\package_manager\Event\StatusCheckEvent;
use Drupal\package_manager\Exception\ApplyFailedException;
use Drupal\package_manager\ProjectInfo;
@@ -12,7 +12,6 @@ use Drupal\automatic_updates\BatchProcessor as AutoUpdatesBatchProcessor;
use Drupal\automatic_updates_extensions\ExtensionUpdater;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Messenger\MessengerInterface;
@@ -30,11 +29,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 * @internal
 *   Form classes are internal.
 */
final class UpdateReady extends FormBase {

  use ReadinessTrait {
    formatResult as traitFormatResult;
  }
final class UpdateReady extends UpdateFormBase {

  /**
   * The updater service.
@@ -176,7 +171,7 @@ final class UpdateReady extends FormBase {
      /** @var \Drupal\package_manager\ValidationResult[] $results */
      $results = $event->getResults();
      // This will have no effect if $results is empty.
      $this->displayResults($results, $this->messenger(), $this->renderer);
      $this->displayResults($results, $this->renderer);
      // If any errors occurred, return the form early so the user cannot
      // continue.
      if (ValidationResult::getOverallSeverity($results) === SystemManager::REQUIREMENT_ERROR) {
@@ -338,22 +333,4 @@ final class UpdateReady extends FormBase {
    ];
  }

  /**
   * {@inheritdoc}
   *
   * @todo Remove this in https://www.drupal.org/project/automatic_updates/issues/3313414.
   */
  protected function formatResult(ValidationResult $result) {
    $messages = $result->getMessages();

    if (count($messages) > 1) {
      return [
        '#theme' => 'item_list__automatic_updates_validation_results',
        '#prefix' => $result->getSummary(),
        '#items' => $messages,
      ];
    }
    return $this->traitFormatResult($result);
  }

}
+3 −6
Original line number Diff line number Diff line
@@ -3,11 +3,10 @@
namespace Drupal\automatic_updates_extensions\Form;

use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Validation\ReadinessTrait;
use Drupal\automatic_updates\Form\UpdateFormBase;
use Drupal\automatic_updates_extensions\BatchProcessor;
use Drupal\automatic_updates_extensions\ExtensionUpdater;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Messenger\MessengerInterface;
@@ -28,9 +27,7 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface;
 * @internal
 *   Form classes are internal.
 */
final class UpdaterForm extends FormBase {

  use ReadinessTrait;
final class UpdaterForm extends UpdateFormBase {

  /**
   * The extension updater service.
@@ -170,7 +167,7 @@ final class UpdaterForm extends FormBase {
      $this->eventDispatcher->dispatch($event);
      $results = $event->getResults();
    }
    $this->displayResults($results, $this->messenger(), $this->renderer);
    $this->displayResults($results, $this->renderer);
    $security_level = ValidationResult::getOverallSeverity($results);

    if ($update_projects && $security_level !== SystemManager::REQUIREMENT_ERROR) {
+2 −3
Original line number Diff line number Diff line
@@ -193,9 +193,8 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
    $assert_session->pageTextNotContains('The following dependencies will also be updated:');
    // Ensure that a list of pending database updates is visible, along with a
    // short explanation, in the warning messages.
    $possible_update_message = 'Possible database updates have been detected in the following extensions.<ul><li>System</li><li>Automatic Updates Theme With Updates</li></ul>';
    $warning_messages = $assert_session->elementExists('css', 'div[data-drupal-messages] div[aria-label="Warning message"]');
    $this->assertStringContainsString($possible_update_message, $warning_messages->getHtml());
    $warning_messages = $assert_session->elementExists('xpath', '//div[@data-drupal-messages]//div[@aria-label="Warning message"]');
    $this->assertStringContainsString('Possible database updates have been detected in the following extensions.<ul><li>System</li><li>Automatic Updates Theme With Updates</li></ul>', $warning_messages->getHtml());

    $page->pressButton('Continue');
    $this->checkForMetaRefresh();
+1 −0
Original line number Diff line number Diff line
@@ -3,3 +3,4 @@ updater's
stager's
syncer
syncers
unrequested
+85 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\automatic_updates\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\package_manager\ValidationResult;
use Drupal\system\SystemManager;

/**
 * Base class for update forms provided by Automatic Updates.
 *
 * @internal
 *   This is an internal part of Automatic Updates and may be changed or removed
 *   at any time without warning. External code should not extend this class.
 */
abstract class UpdateFormBase extends FormBase {

  /**
   * Gets a message, based on severity, when readiness checkers fail.
   *
   * @param int $severity
   *   The severity. Should be one of the SystemManager::REQUIREMENT_*
   *   constants.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup
   *   The message.
   *
   * @see \Drupal\system\SystemManager::REQUIREMENT_ERROR
   * @see \Drupal\system\SystemManager::REQUIREMENT_WARNING
   */
  protected function getFailureMessageForSeverity(int $severity): TranslatableMarkup {
    return $severity === SystemManager::REQUIREMENT_WARNING ?
      // @todo Link "automatic updates" to documentation in
      //   https://www.drupal.org/node/3168405.
      $this->t('Your site does not pass some readiness checks for automatic updates. Depending on the nature of the failures, it might affect the eligibility for automatic updates.') :
      $this->t('Your site does not pass some readiness checks for automatic updates. It cannot be automatically updated until further action is performed.');
  }

  /**
   * Adds a set of validation results to the messages.
   *
   * @param \Drupal\package_manager\ValidationResult[] $results
   *   The validation results.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  protected function displayResults(array $results, RendererInterface $renderer): void {
    $severity = ValidationResult::getOverallSeverity($results);

    if ($severity === SystemManager::REQUIREMENT_OK) {
      return;
    }

    // Format the results as a single item list prefixed by a preamble message.
    $build = [
      '#theme' => 'item_list__automatic_updates_validation_results',
      '#prefix' => $this->getFailureMessageForSeverity($severity),
    ];
    foreach ($results as $result) {
      $messages = $result->getMessages();

      if (count($messages) > 1) {
        $build['#items'][] = [
          '#theme' => $build['#theme'],
          '#prefix' => $result->getSummary(),
          '#items' => $messages,
        ];
      }
      else {
        $build['#items'][] = reset($messages);
      }
    }
    $message = $renderer->renderRoot($build);

    if ($severity === SystemManager::REQUIREMENT_ERROR) {
      $this->messenger()->addError($message);
    }
    else {
      $this->messenger()->addWarning($message);
    }
  }

}
Loading