You are here

public function ReadinessValidationTest::testReadinessChecksStatusReport in Automatic Updates 8.2

Tests readiness checkers on status report page.

File

tests/src/Functional/ReadinessValidationTest.php, line 74

Class

ReadinessValidationTest
Tests readiness validation.

Namespace

Drupal\Tests\automatic_updates\Functional

Code

public function testReadinessChecksStatusReport() : void {
  $assert = $this
    ->assertSession();

  // Ensure automated_cron is disabled before installing automatic_updates. This
  // ensures we are testing that automatic_updates runs the checkers when the
  // module itself is installed and they weren't run on cron.
  $this
    ->assertFalse($this->container
    ->get('module_handler')
    ->moduleExists('automated_cron'));
  $this->container
    ->get('module_installer')
    ->install([
    'automatic_updates',
    'automatic_updates_test',
  ]);

  // If the site is ready for updates, the users will see the same output
  // regardless of whether the user has permission to run updates.
  $this
    ->drupalLogin($this->reportViewerUser);
  $this
    ->checkForUpdates();
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates.', 'checked', FALSE);
  $this
    ->drupalLogin($this->checkerRunnerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates. Run readiness checks now.', 'checked', FALSE);

  // Confirm a user without the permission to run readiness checks does not
  // have a link to run the checks when the checks need to be run again.
  // @todo Change this to fake the request time in
  //   https://www.drupal.org/node/3113971.

  /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $key_value */
  $key_value = $this->container
    ->get('keyvalue.expirable')
    ->get('automatic_updates');
  $key_value
    ->delete('readiness_validation_last_run');
  $this
    ->drupalLogin($this->reportViewerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates.', 'checked', FALSE);
  $this
    ->drupalLogin($this->checkerRunnerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates. Run readiness checks now.', 'checked', FALSE);

  // Confirm a user with the permission to run readiness checks does have a
  // link to run the checks when the checks need to be run again.
  $this
    ->drupalLogin($this->reportViewerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates.', 'checked', FALSE);
  $this
    ->drupalLogin($this->checkerRunnerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates. Run readiness checks now.', 'checked', FALSE);

  /** @var \Drupal\automatic_updates\Validation\ValidationResult[] $expected_results */
  $expected_results = $this->testResults['checker_1']['1 error'];
  TestChecker1::setTestResult($expected_results);

  // Run the readiness checks.
  $this
    ->clickLink('Run readiness checks');
  $assert
    ->statusCodeEquals(200);

  // Confirm redirect back to status report page.
  $assert
    ->addressEquals('/admin/reports/status');

  // Assert that when the runners are run manually the message that updates
  // will not be performed because of errors is displayed on the top of the
  // page in message.
  $assert
    ->pageTextMatchesCount(2, '/' . preg_quote(static::$errorsExplanation) . '/');
  $this
    ->assertReadinessReportMatches($expected_results[0]
    ->getMessages()[0] . 'Run readiness checks now.', 'error', static::$errorsExplanation);

  // @todo Should we always show when the checks were last run and a link to
  //   run when there is an error?
  // Confirm a user without permission to run the checks sees the same error.
  $this
    ->drupalLogin($this->reportViewerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches($expected_results[0]
    ->getMessages()[0], 'error', static::$errorsExplanation);
  $expected_results = $this->testResults['checker_1']['1 error 1 warning'];
  TestChecker1::setTestResult($expected_results);
  $key_value
    ->delete('readiness_validation_last_run');

  // Confirm a new message is displayed if the stored messages are deleted.
  $this
    ->drupalGet('admin/reports/status');

  // Confirm that on the status page if there is only 1 warning or error the
  // the summaries will not be displayed.
  $this
    ->assertReadinessReportMatches($expected_results['1:error']
    ->getMessages()[0], 'error', static::$errorsExplanation);
  $this
    ->assertReadinessReportMatches($expected_results['1:warning']
    ->getMessages()[0], 'warning', static::$warningsExplanation);
  $assert
    ->pageTextNotContains($expected_results['1:error']
    ->getSummary());
  $assert
    ->pageTextNotContains($expected_results['1:warning']
    ->getSummary());
  $key_value
    ->delete('readiness_validation_last_run');
  $expected_results = $this->testResults['checker_1']['2 errors 2 warnings'];
  TestChecker1::setTestResult($expected_results);
  $this
    ->drupalGet('admin/reports/status');

  // Confirm that both messages and summaries will be displayed on status
  // report when there multiple messages.
  $this
    ->assertReadinessReportMatches($expected_results['1:errors']
    ->getSummary() . ' ' . implode('', $expected_results['1:errors']
    ->getMessages()), 'error', static::$errorsExplanation);
  $this
    ->assertReadinessReportMatches($expected_results['1:warnings']
    ->getSummary() . ' ' . implode('', $expected_results['1:warnings']
    ->getMessages()), 'warning', static::$warningsExplanation);
  $key_value
    ->delete('readiness_validation_last_run');
  $expected_results = $this->testResults['checker_1']['2 warnings'];
  TestChecker1::setTestResult($expected_results);
  $this
    ->drupalGet('admin/reports/status');
  $assert
    ->pageTextContainsOnce('Update readiness checks');

  // Confirm that warnings will display on the status report if there are no
  // errors.
  $this
    ->assertReadinessReportMatches($expected_results[0]
    ->getSummary() . ' ' . implode('', $expected_results[0]
    ->getMessages()), 'warning', static::$warningsExplanation);
  $key_value
    ->delete('readiness_validation_last_run');
  $expected_results = $this->testResults['checker_1']['1 warning'];
  TestChecker1::setTestResult($expected_results);
  $this
    ->drupalGet('admin/reports/status');
  $assert
    ->pageTextContainsOnce('Update readiness checks');
  $this
    ->assertReadinessReportMatches($expected_results[0]
    ->getMessages()[0], 'warning', static::$warningsExplanation);
}