You are here

public function ReadinessValidationTest::testReadinessChecksAdminPages in Automatic Updates 8.2

Tests readiness checkers results on admin pages..

File

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

Class

ReadinessValidationTest
Tests readiness validation.

Namespace

Drupal\Tests\automatic_updates\Functional

Code

public function testReadinessChecksAdminPages() : void {
  $assert = $this
    ->assertSession();
  $messages_section_selector = '[data-drupal-messages]';

  // 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 site is ready for updates no message will be displayed on admin pages.
  $this
    ->drupalLogin($this->reportViewerUser);
  $this
    ->drupalGet('admin/reports/status');
  $this
    ->assertReadinessReportMatches('Your site is ready for automatic updates.', 'checked', FALSE);
  $this
    ->drupalGet('admin/structure');
  $assert
    ->elementNotExists('css', $messages_section_selector);

  // 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.
  $expected_results = $this->testResults['checker_1']['1 error'];
  TestChecker1::setTestResult($expected_results);

  // @todo Change this to use ::delayRequestTime() to simulate running cron
  //   after a 24 wait instead of directly deleting 'readiness_validation_last_run'
  //   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');

  // A user without the permission to run the checkers will not see a message
  // on other pages if the checkers need to be run again.
  $this
    ->drupalGet('admin/structure');
  $assert
    ->elementNotExists('css', $messages_section_selector);

  // Confirm that a user with the correct permission can also run the checkers
  // on another admin page.
  $this
    ->drupalLogin($this->checkerRunnerUser);
  $this
    ->drupalGet('admin/structure');
  $assert
    ->elementExists('css', $messages_section_selector);
  $assert
    ->pageTextContainsOnce('Your site has not recently run an update readiness check. Run readiness checks now.');
  $this
    ->clickLink('Run readiness checks now.');
  $assert
    ->addressEquals('admin/structure');
  $assert
    ->pageTextContainsOnce($expected_results[0]
    ->getMessages()[0]);
  $expected_results = $this->testResults['checker_1']['1 error 1 warning'];
  TestChecker1::setTestResult($expected_results);

  // Confirm a new message is displayed if the cron is run after an hour.
  $this
    ->delayRequestTime();
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/structure');
  $assert
    ->pageTextContainsOnce(static::$errorsExplanation);

  // Confirm on admin pages that a single error will be displayed instead of a
  // summary.
  $this
    ->assertSame(SystemManager::REQUIREMENT_ERROR, $expected_results['1:error']
    ->getSeverity());
  $assert
    ->pageTextContainsOnce($expected_results['1:error']
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($expected_results['1:error']
    ->getSummary());

  // Warnings are not displayed on admin pages if there are any errors.
  $this
    ->assertSame(SystemManager::REQUIREMENT_WARNING, $expected_results['1:warning']
    ->getSeverity());
  $assert
    ->pageTextNotContains($expected_results['1:warning']
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($expected_results['1:warning']
    ->getSummary());

  // Confirm that if cron runs less than hour after it previously ran it will
  // not run the checkers again.
  $unexpected_results = $this->testResults['checker_1']['2 errors 2 warnings'];
  TestChecker1::setTestResult($unexpected_results);
  $this
    ->delayRequestTime(30);
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/structure');
  $assert
    ->pageTextNotContains($unexpected_results['1:errors']
    ->getSummary());
  $assert
    ->pageTextContainsOnce($expected_results['1:error']
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($unexpected_results['1:warnings']
    ->getSummary());
  $assert
    ->pageTextNotContains($expected_results['1:warning']
    ->getMessages()[0]);

  // Confirm that is if cron is run over an hour after the checkers were
  // previously run the checkers will be run again.
  $this
    ->delayRequestTime(31);
  $this
    ->cronRun();
  $expected_results = $unexpected_results;
  $this
    ->drupalGet('admin/structure');

  // Confirm on admin pages only the error summary will be displayed if there
  // is more than 1 error.
  $this
    ->assertSame(SystemManager::REQUIREMENT_ERROR, $expected_results['1:errors']
    ->getSeverity());
  $assert
    ->pageTextNotContains($expected_results['1:errors']
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($expected_results['1:errors']
    ->getMessages()[1]);
  $assert
    ->pageTextContainsOnce($expected_results['1:errors']
    ->getSummary());
  $assert
    ->pageTextContainsOnce(static::$errorsExplanation);

  // Warnings are not displayed on admin pages if there are any errors.
  $this
    ->assertSame(SystemManager::REQUIREMENT_WARNING, $expected_results['1:warnings']
    ->getSeverity());
  $assert
    ->pageTextNotContains($expected_results['1:warnings']
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($expected_results['1:warnings']
    ->getMessages()[1]);
  $assert
    ->pageTextNotContains($expected_results['1:warnings']
    ->getSummary());
  $expected_results = $this->testResults['checker_1']['2 warnings'];
  TestChecker1::setTestResult($expected_results);
  $this
    ->delayRequestTime();
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/structure');

  // Confirm that the warnings summary is displayed on admin pages if there
  // are no errors.
  $assert
    ->pageTextNotContains(static::$errorsExplanation);
  $this
    ->assertSame(SystemManager::REQUIREMENT_WARNING, $expected_results[0]
    ->getSeverity());
  $assert
    ->pageTextNotContains($expected_results[0]
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($expected_results[0]
    ->getMessages()[1]);
  $assert
    ->pageTextContainsOnce(static::$warningsExplanation);
  $assert
    ->pageTextContainsOnce($expected_results[0]
    ->getSummary());
  $expected_results = $this->testResults['checker_1']['1 warning'];
  TestChecker1::setTestResult($expected_results);
  $this
    ->delayRequestTime();
  $this
    ->cronRun();
  $this
    ->drupalGet('admin/structure');
  $assert
    ->pageTextNotContains(static::$errorsExplanation);

  // Confirm that a single warning is displayed and not the summary on admin
  // pages if there is only 1 warning and there are no errors.
  $this
    ->assertSame(SystemManager::REQUIREMENT_WARNING, $expected_results[0]
    ->getSeverity());
  $assert
    ->pageTextContainsOnce(static::$warningsExplanation);
  $assert
    ->pageTextContainsOnce($expected_results[0]
    ->getMessages()[0]);
  $assert
    ->pageTextNotContains($expected_results[0]
    ->getSummary());
}