Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / system / tests / src / Functional / System / CronRunTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\System;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\Tests\Traits\Core\CronRunTrait;
7
8 /**
9  * Tests cron runs.
10  *
11  * @group system
12  */
13 class CronRunTest extends BrowserTestBase {
14
15   use CronRunTrait;
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['common_test', 'common_test_cron_helper', 'automated_cron'];
23
24   /**
25    * Test cron runs.
26    */
27   public function testCronRun() {
28     // Run cron anonymously without any cron key.
29     $this->drupalGet('cron');
30     $this->assertResponse(404);
31
32     // Run cron anonymously with a random cron key.
33     $key = $this->randomMachineName(16);
34     $this->drupalGet('cron/' . $key);
35     $this->assertResponse(403);
36
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);
41   }
42
43   /**
44    * Ensure that the automated cron run module is working.
45    *
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.
48    */
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);
55
56     // Ensure cron does not run when a non-zero cron interval is specified and
57     // was not passed.
58     $cron_last = time();
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)
63       ->save();
64     $this->drupalGet('');
65     $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.');
66
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);
70     $this->drupalGet('');
71     sleep(1);
72     $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron interval is passed.');
73
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();
78
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);
82     $this->drupalGet('');
83     $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.');
84   }
85
86   /**
87    * Make sure exceptions thrown on hook_cron() don't affect other modules.
88    */
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.
94     $this->cronRun();
95     $result = \Drupal::state()->get('common_test.cron');
96     $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.');
97   }
98
99   /**
100    * Make sure the cron UI reads from the state storage.
101    */
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');
110
111     $cron_last = time() - 200;
112     \Drupal::state()->set('system.cron_last', $cron_last);
113
114     $this->drupalPostForm(NULL, [], 'Save configuration');
115     $this->assertText('The configuration options have been saved.');
116     $this->assertUrl('admin/config/system/cron');
117
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.');
120
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.');
124   }
125
126   /**
127    * Ensure that the manual cron run is working.
128    */
129   public function testManualCron() {
130     $admin_user = $this->drupalCreateUser(['administer site configuration']);
131     $this->drupalLogin($admin_user);
132
133     $this->drupalGet('admin/reports/status/run-cron');
134     $this->assertResponse(403);
135
136     $this->drupalGet('admin/reports/status');
137     $this->clickLink(t('Run cron'));
138     $this->assertResponse(200);
139     $this->assertText(t('Cron ran successfully.'));
140   }
141
142 }