CronRunTest.php in Drupal 10
File
core/modules/system/tests/src/Functional/System/CronRunTest.php
View source
<?php
namespace Drupal\Tests\system\Functional\System;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\Traits\Core\CronRunTrait;
class CronRunTest extends BrowserTestBase {
use CronRunTrait;
protected static $modules = [
'common_test',
'common_test_cron_helper',
'automated_cron',
];
protected $defaultTheme = 'stark';
public function testCronRun() {
$this
->drupalGet('cron');
$this
->assertSession()
->statusCodeEquals(404);
$key = $this
->randomMachineName(16);
$this
->drupalGet('cron/' . $key);
$this
->assertSession()
->statusCodeEquals(403);
$key = \Drupal::state()
->get('system.cron_key');
$this
->drupalGet('cron/' . $key);
$this
->assertSession()
->statusCodeEquals(204);
}
public function testAutomatedCron() {
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$cron_last = time();
$cron_safe_interval = 100;
\Drupal::state()
->set('system.cron_last', $cron_last);
$this
->config('automated_cron.settings')
->set('interval', $cron_safe_interval)
->save();
$this
->drupalGet('');
$this
->assertSame($cron_last, \Drupal::state()
->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.');
$cron_last = time() - 200;
\Drupal::state()
->set('system.cron_last', $cron_last);
$this
->drupalGet('');
sleep(1);
$this
->assertLessThan(\Drupal::state()
->get('system.cron_last'), $cron_last);
$this
->drupalGet('admin/config/system/cron');
$this
->submitForm([
'interval' => 0,
], 'Save configuration');
$this
->assertSession()
->pageTextContains('The configuration options have been saved.');
$this
->drupalLogout();
$cron_last = time() - 200;
\Drupal::state()
->set('system.cron_last', $cron_last);
$this
->drupalGet('');
$this
->assertSame($cron_last, \Drupal::state()
->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.');
}
public function testCronExceptions() {
\Drupal::state()
->delete('common_test.cron');
$this
->cronRun();
$result = \Drupal::state()
->get('common_test.cron');
$this
->assertEquals('success', $result, 'Cron correctly handles exceptions thrown during hook_cron() invocations.');
}
public function testCronUI() {
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/config/system/cron');
$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');
$this
->assertEquals($cron_last, \Drupal::state()
->get('system.cron_last'), 'Cron does not run when saving the configuration form.');
$this
->submitForm([], 'Run cron');
$this
->assertLessThan(\Drupal::state()
->get('system.cron_last'), $cron_last);
}
public function testManualCron() {
$admin_user = $this
->drupalCreateUser([
'administer site configuration',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/reports/status/run-cron');
$this
->assertSession()
->statusCodeEquals(403);
$this
->drupalGet('admin/reports/status');
$this
->clickLink('Run cron');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('Cron ran successfully.');
}
}