3 namespace Drupal\Tests\system\Functional\System;
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Tests\Traits\Core\CronRunTrait;
13 class CronRunTest extends BrowserTestBase {
22 public static $modules = ['common_test', 'common_test_cron_helper', 'automated_cron'];
27 public function testCronRun() {
28 // Run cron anonymously without any cron key.
29 $this->drupalGet('cron');
30 $this->assertResponse(404);
32 // Run cron anonymously with a random cron key.
33 $key = $this->randomMachineName(16);
34 $this->drupalGet('cron/' . $key);
35 $this->assertResponse(403);
37 // Run cron anonymously with the valid cron key.
38 $key = \Drupal::state()->get('system.cron_key');
39 $this->drupalGet('cron/' . $key);
40 $this->assertResponse(204);
44 * Ensure that the automated cron run module is working.
46 * In these tests we do not use REQUEST_TIME to track start time, because we
47 * need the exact time when cron is triggered.
49 public function testAutomatedCron() {
50 // Test with a logged in user; anonymous users likely don't cause Drupal to
51 // fully bootstrap, because of the internal page cache or an external
52 // reverse proxy. Reuse this user for disabling cron later in the test.
53 $admin_user = $this->drupalCreateUser(['administer site configuration']);
54 $this->drupalLogin($admin_user);
56 // Ensure cron does not run when a non-zero cron interval is specified and
59 $cron_safe_interval = 100;
60 \Drupal::state()->set('system.cron_last', $cron_last);
61 $this->config('automated_cron.settings')
62 ->set('interval', $cron_safe_interval)
65 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.');
67 // Test if cron runs when the cron interval was passed.
68 $cron_last = time() - 200;
69 \Drupal::state()->set('system.cron_last', $cron_last);
72 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron interval is passed.');
74 // Disable cron through the interface by setting the interval to zero.
75 $this->drupalPostForm('admin/config/system/cron', ['interval' => 0], t('Save configuration'));
76 $this->assertText(t('The configuration options have been saved.'));
77 $this->drupalLogout();
79 // Test if cron does not run when the cron interval is set to zero.
80 $cron_last = time() - 200;
81 \Drupal::state()->set('system.cron_last', $cron_last);
83 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.');
87 * Make sure exceptions thrown on hook_cron() don't affect other modules.
89 public function testCronExceptions() {
90 \Drupal::state()->delete('common_test.cron');
91 // The common_test module throws an exception. If it isn't caught, the tests
92 // won't finish successfully.
93 // The common_test_cron_helper module sets the 'common_test_cron' variable.
95 $result = \Drupal::state()->get('common_test.cron');
96 $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.');
100 * Make sure the cron UI reads from the state storage.
102 public function testCronUI() {
103 $admin_user = $this->drupalCreateUser(['administer site configuration']);
104 $this->drupalLogin($admin_user);
105 $this->drupalGet('admin/config/system/cron');
106 // Don't use REQUEST to calculate the exact time, because that will
107 // fail randomly. Look for the word 'years', because without a timestamp,
108 // the time will start at 1 January 1970.
109 $this->assertNoText('years');
111 $cron_last = time() - 200;
112 \Drupal::state()->set('system.cron_last', $cron_last);
114 $this->drupalPostForm(NULL, [], 'Save configuration');
115 $this->assertText('The configuration options have been saved.');
116 $this->assertUrl('admin/config/system/cron');
118 // Check that cron does not run when saving the configuration form.
119 $this->assertEqual($cron_last, \Drupal::state()->get('system.cron_last'), 'Cron does not run when saving the configuration form.');
121 // Check that cron runs when triggered manually.
122 $this->drupalPostForm(NULL, [], 'Run cron');
123 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when triggered manually.');
127 * Ensure that the manual cron run is working.
129 public function testManualCron() {
130 $admin_user = $this->drupalCreateUser(['administer site configuration']);
131 $this->drupalLogin($admin_user);
133 $this->drupalGet('admin/reports/status/run-cron');
134 $this->assertResponse(403);
136 $this->drupalGet('admin/reports/status');
137 $this->clickLink(t('Run cron'));
138 $this->assertResponse(200);
139 $this->assertText(t('Cron ran successfully.'));