comparison core/modules/system/src/Tests/System/CronRunTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\System;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8 * Tests cron runs.
9 *
10 * @group system
11 */
12 class CronRunTest extends WebTestBase {
13
14 /**
15 * Modules to enable.
16 *
17 * @var array
18 */
19 public static $modules = ['common_test', 'common_test_cron_helper', 'automated_cron'];
20
21 /**
22 * Test cron runs.
23 */
24 public function testCronRun() {
25 // Run cron anonymously without any cron key.
26 $this->drupalGet('cron');
27 $this->assertResponse(404);
28
29 // Run cron anonymously with a random cron key.
30 $key = $this->randomMachineName(16);
31 $this->drupalGet('cron/' . $key);
32 $this->assertResponse(403);
33
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);
38 }
39
40 /**
41 * Ensure that the automated cron run module is working.
42 *
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.
45 */
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);
52
53 // Ensure cron does not run when a non-zero cron interval is specified and
54 // was not passed.
55 $cron_last = time();
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)
60 ->save();
61 $this->drupalGet('');
62 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.');
63
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);
67 $this->drupalGet('');
68 sleep(1);
69 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron interval is passed.');
70
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();
75
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);
79 $this->drupalGet('');
80 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.');
81 }
82
83 /**
84 * Make sure exceptions thrown on hook_cron() don't affect other modules.
85 */
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.
91 $this->cronRun();
92 $result = \Drupal::state()->get('common_test.cron');
93 $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.');
94 }
95
96 /**
97 * Make sure the cron UI reads from the state storage.
98 */
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');
107
108 $cron_last = time() - 200;
109 \Drupal::state()->set('system.cron_last', $cron_last);
110
111 $this->drupalPostForm(NULL, [], 'Save configuration');
112 $this->assertText('The configuration options have been saved.');
113 $this->assertUrl('admin/config/system/cron');
114
115 // Check that cron does not run when saving the configuration form.
116 $this->assertEqual($cron_last, \Drupal::state()->get('system.cron_last'), 'Cron does not run when saving the configuration form.');
117
118 // Check that cron runs when triggered manually.
119 $this->drupalPostForm(NULL, [], 'Run cron');
120 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when triggered manually.');
121 }
122
123 /**
124 * Ensure that the manual cron run is working.
125 */
126 public function testManualCron() {
127 $admin_user = $this->drupalCreateUser(['administer site configuration']);
128 $this->drupalLogin($admin_user);
129
130 $this->drupalGet('admin/reports/status/run-cron');
131 $this->assertResponse(403);
132
133 $this->drupalGet('admin/reports/status');
134 $this->clickLink(t('Run cron'));
135 $this->assertResponse(200);
136 $this->assertText(t('Cron ran successfully.'));
137 }
138
139 }