function CronRunTest::testCronUI

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/System/CronRunTest.php \Drupal\Tests\system\Functional\System\CronRunTest::testCronUI()
  2. 8.9.x core/modules/system/tests/src/Functional/System/CronRunTest.php \Drupal\Tests\system\Functional\System\CronRunTest::testCronUI()
  3. 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 120

Class

CronRunTest
Tests cron runs.

Namespace

Drupal\Tests\system\Functional\System

Code

public function testCronUI() : void {
    // To prevent race conditions between the admin_user login triggering cron
    // and updating its state, and this test doing the same thing, we use
    // \Drupal\Tests\WaitTerminateTestTrait::setWaitForTerminate().
    $this->setWaitForTerminate();
    $admin_user = $this->drupalCreateUser([
        'administer site configuration',
    ]);
    $this->drupalLogin($admin_user);
    \Drupal::state()->delete('system.cron_last');
    $this->drupalGet('admin/config/system/cron');
    // Check that cron has never run.
    $this->assertSession()
        ->pageTextContains('Last run: never');
    // Now check that it has run.
    // Sleep to allow cron time to complete since it happens during kernel
    // terminate after the page response is set.
    sleep(3);
    $this->drupalGet('admin/config/system/cron');
    $this->assertSession()
        ->pageTextNotContains('Last run: never');
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.