111cc00870b10ebd05b5e4c2b74716f04c8ad07e
[yaffs-website] / web / core / modules / system / src / Tests / System / CronRunTest.php
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     $this->drupalPostForm(NULL, [], t('Save configuration'));
109     $this->assertText(t('The configuration options have been saved.'));
110     $this->assertUrl('admin/config/system/cron');
111   }
112
113   /**
114    * Ensure that the manual cron run is working.
115    */
116   public function testManualCron() {
117     $admin_user = $this->drupalCreateUser(['administer site configuration']);
118     $this->drupalLogin($admin_user);
119
120     $this->drupalGet('admin/reports/status/run-cron');
121     $this->assertResponse(403);
122
123     $this->drupalGet('admin/reports/status');
124     $this->clickLink(t('Run cron'));
125     $this->assertResponse(200);
126     $this->assertText(t('Cron ran successfully.'));
127   }
128
129 }