3 namespace Drupal\system\Tests\System;
5 use Drupal\simpletest\WebTestBase;
12 class CronRunTest extends WebTestBase {
19 public static $modules = ['common_test', 'common_test_cron_helper', 'automated_cron'];
24 public function testCronRun() {
25 // Run cron anonymously without any cron key.
26 $this->drupalGet('cron');
27 $this->assertResponse(404);
29 // Run cron anonymously with a random cron key.
30 $key = $this->randomMachineName(16);
31 $this->drupalGet('cron/' . $key);
32 $this->assertResponse(403);
34 // Run cron anonymously with the valid cron key.
35 $key = \Drupal::state()->get('system.cron_key');
36 $this->drupalGet('cron/' . $key);
37 $this->assertResponse(204);
41 * Ensure that the automated cron run module is working.
43 * In these tests we do not use REQUEST_TIME to track start time, because we
44 * need the exact time when cron is triggered.
46 public function testAutomatedCron() {
47 // Test with a logged in user; anonymous users likely don't cause Drupal to
48 // fully bootstrap, because of the internal page cache or an external
49 // reverse proxy. Reuse this user for disabling cron later in the test.
50 $admin_user = $this->drupalCreateUser(['administer site configuration']);
51 $this->drupalLogin($admin_user);
53 // Ensure cron does not run when a non-zero cron interval is specified and
56 $cron_safe_interval = 100;
57 \Drupal::state()->set('system.cron_last', $cron_last);
58 $this->config('automated_cron.settings')
59 ->set('interval', $cron_safe_interval)
62 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.');
64 // Test if cron runs when the cron interval was passed.
65 $cron_last = time() - 200;
66 \Drupal::state()->set('system.cron_last', $cron_last);
69 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron interval is passed.');
71 // Disable cron through the interface by setting the interval to zero.
72 $this->drupalPostForm('admin/config/system/cron', ['interval' => 0], t('Save configuration'));
73 $this->assertText(t('The configuration options have been saved.'));
74 $this->drupalLogout();
76 // Test if cron does not run when the cron interval is set to zero.
77 $cron_last = time() - 200;
78 \Drupal::state()->set('system.cron_last', $cron_last);
80 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.');
84 * Make sure exceptions thrown on hook_cron() don't affect other modules.
86 public function testCronExceptions() {
87 \Drupal::state()->delete('common_test.cron');
88 // The common_test module throws an exception. If it isn't caught, the tests
89 // won't finish successfully.
90 // The common_test_cron_helper module sets the 'common_test_cron' variable.
92 $result = \Drupal::state()->get('common_test.cron');
93 $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.');
97 * Make sure the cron UI reads from the state storage.
99 public function testCronUI() {
100 $admin_user = $this->drupalCreateUser(['administer site configuration']);
101 $this->drupalLogin($admin_user);
102 $this->drupalGet('admin/config/system/cron');
103 // Don't use REQUEST to calculate the exact time, because that will
104 // fail randomly. Look for the word 'years', because without a timestamp,
105 // the time will start at 1 January 1970.
106 $this->assertNoText('years');
108 $this->drupalPostForm(NULL, [], t('Save configuration'));
109 $this->assertText(t('The configuration options have been saved.'));
110 $this->assertUrl('admin/config/system/cron');
114 * Ensure that the manual cron run is working.
116 public function testManualCron() {
117 $admin_user = $this->drupalCreateUser(['administer site configuration']);
118 $this->drupalLogin($admin_user);
120 $this->drupalGet('admin/reports/status/run-cron');
121 $this->assertResponse(403);
123 $this->drupalGet('admin/reports/status');
124 $this->clickLink(t('Run cron'));
125 $this->assertResponse(200);
126 $this->assertText(t('Cron ran successfully.'));