You are here

public function CronRunTest::testCronUI in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/src/Functional/System/CronRunTest.php \Drupal\Tests\system\Functional\System\CronRunTest::testCronUI()
  2. 10 core/modules/system/tests/src/Functional/System/CronRunTest.php \Drupal\Tests\system\Functional\System\CronRunTest::testCronUI()

Make sure the cron UI reads from the state storage.

File

core/modules/system/tests/src/Functional/System/CronRunTest.php, line 113

Class

CronRunTest
Tests cron runs.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testCronUI() {
  $admin_user = $this
    ->drupalCreateUser([
    'administer site configuration',
  ]);
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('admin/config/system/cron');

  // Don't use REQUEST to calculate the exact time, because that will
  // fail randomly. Look for the word 'years', because without a timestamp,
  // the time will start at 1 January 1970.
  $this
    ->assertSession()
    ->pageTextNotContains('years');
  $cron_last = time() - 200;
  \Drupal::state()
    ->set('system.cron_last', $cron_last);
  $this
    ->submitForm([], 'Save configuration');
  $this
    ->assertSession()
    ->pageTextContains('The configuration options have been saved.');
  $this
    ->assertSession()
    ->addressEquals('admin/config/system/cron');

  // Check that cron does not run when saving the configuration form.
  $this
    ->assertEquals($cron_last, \Drupal::state()
    ->get('system.cron_last'), 'Cron does not run when saving the configuration form.');

  // Check that cron runs when triggered manually.
  $this
    ->submitForm([], 'Run cron');

  // Verify that cron runs when triggered manually.
  $this
    ->assertLessThan(\Drupal::state()
    ->get('system.cron_last'), $cron_last);
}