You are here

public function UpdaterFormTest::testUpdateErrors in Automatic Updates 8.2

Tests handling of errors and warnings during the update process.

File

tests/src/Functional/UpdaterFormTest.php, line 138

Class

UpdaterFormTest
@covers \Drupal\automatic_updates\Form\UpdaterForm

Namespace

Drupal\Tests\automatic_updates\Functional

Code

public function testUpdateErrors() : void {
  $session = $this
    ->getSession();
  $assert_session = $this
    ->assertSession();
  $page = $session
    ->getPage();

  // Store a fake readiness error, which will be cached.
  $message = t("You've not experienced Shakespeare until you have read him in the original Klingon.");
  $error = ValidationResult::createError([
    $message,
  ]);
  TestChecker1::setTestResult([
    $error,
  ]);
  $this
    ->drupalGet('/admin/reports/status');
  $page
    ->clickLink('Run readiness checks');
  $assert_session
    ->pageTextContainsOnce((string) $message);

  // Ensure that the fake error is cached.
  $session
    ->reload();
  $assert_session
    ->pageTextContainsOnce((string) $message);
  $this
    ->setCoreVersion('9.8.0');
  $this
    ->checkForUpdates();

  // Set up a new fake error.
  $this
    ->createTestValidationResults();
  $expected_results = $this->testResults['checker_1']['1 error'];
  TestChecker1::setTestResult($expected_results);

  // If a validator raises an error during readiness checking, the form should
  // not have a submit button.
  $this
    ->drupalGet('/admin/modules/automatic-update');
  $assert_session
    ->buttonNotExists('Update');

  // Since this is an administrative page, the error message should be visible
  // thanks to automatic_updates_page_top(). The readiness checks were re-run
  // during the form build, which means the new error should be cached and
  // displayed instead of the previously cached error.
  $assert_session
    ->pageTextContainsOnce((string) $expected_results[0]
    ->getMessages()[0]);
  $assert_session
    ->pageTextContainsOnce(static::$errorsExplanation);
  $assert_session
    ->pageTextNotContains(static::$warningsExplanation);
  $assert_session
    ->pageTextNotContains((string) $message);
  TestChecker1::setTestResult(NULL);

  // Repackage the validation error as an exception, so we can test what
  // happens if a validator throws once the update has started.
  $error = new UpdateException($expected_results, 'The update exploded.');
  TestChecker1::setTestResult($error, AutomaticUpdatesEvents::PRE_START);
  $session
    ->reload();
  $assert_session
    ->pageTextNotContains(static::$errorsExplanation);
  $assert_session
    ->pageTextNotContains(static::$warningsExplanation);
  $page
    ->pressButton('Update');
  $this
    ->checkForMetaRefresh();
  $assert_session
    ->pageTextContainsOnce('An error has occurred.');
  $page
    ->clickLink('the error page');
  $assert_session
    ->pageTextContainsOnce((string) $expected_results[0]
    ->getMessages()[0]);

  // Since there's only one error message, we shouldn't see the summary...
  $assert_session
    ->pageTextNotContains($expected_results[0]
    ->getSummary());

  // ...but we should see the exception message.
  $assert_session
    ->pageTextContainsOnce('The update exploded.');

  // If a validator flags an error, but doesn't throw, the update should still
  // be halted.
  TestChecker1::setTestResult($expected_results, AutomaticUpdatesEvents::PRE_START);
  $this
    ->deleteStagedUpdate();
  $page
    ->pressButton('Update');
  $this
    ->checkForMetaRefresh();
  $assert_session
    ->pageTextContainsOnce('An error has occurred.');
  $page
    ->clickLink('the error page');

  // Since there's only one message, we shouldn't see the summary.
  $assert_session
    ->pageTextNotContains($expected_results[0]
    ->getSummary());
  $assert_session
    ->pageTextContainsOnce((string) $expected_results[0]
    ->getMessages()[0]);

  // If a validator flags a warning, but doesn't throw, the update should
  // continue.
  $expected_results = $this->testResults['checker_1']['1 warning'];
  TestChecker1::setTestResult($expected_results, AutomaticUpdatesEvents::PRE_START);
  $session
    ->reload();
  $this
    ->deleteStagedUpdate();
  $page
    ->pressButton('Update');
  $this
    ->checkForMetaRefresh();
  $assert_session
    ->pageTextContains('Ready to update');
}