annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\System;
Chris@0 4
Chris@0 5 use Drupal\simpletest\WebTestBase;
Chris@0 6
Chris@0 7 /**
Chris@0 8 * Tests cron runs.
Chris@0 9 *
Chris@0 10 * @group system
Chris@0 11 */
Chris@0 12 class CronRunTest extends WebTestBase {
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Modules to enable.
Chris@0 16 *
Chris@0 17 * @var array
Chris@0 18 */
Chris@0 19 public static $modules = ['common_test', 'common_test_cron_helper', 'automated_cron'];
Chris@0 20
Chris@0 21 /**
Chris@0 22 * Test cron runs.
Chris@0 23 */
Chris@0 24 public function testCronRun() {
Chris@0 25 // Run cron anonymously without any cron key.
Chris@0 26 $this->drupalGet('cron');
Chris@0 27 $this->assertResponse(404);
Chris@0 28
Chris@0 29 // Run cron anonymously with a random cron key.
Chris@0 30 $key = $this->randomMachineName(16);
Chris@0 31 $this->drupalGet('cron/' . $key);
Chris@0 32 $this->assertResponse(403);
Chris@0 33
Chris@0 34 // Run cron anonymously with the valid cron key.
Chris@0 35 $key = \Drupal::state()->get('system.cron_key');
Chris@0 36 $this->drupalGet('cron/' . $key);
Chris@0 37 $this->assertResponse(204);
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Ensure that the automated cron run module is working.
Chris@0 42 *
Chris@0 43 * In these tests we do not use REQUEST_TIME to track start time, because we
Chris@0 44 * need the exact time when cron is triggered.
Chris@0 45 */
Chris@0 46 public function testAutomatedCron() {
Chris@0 47 // Test with a logged in user; anonymous users likely don't cause Drupal to
Chris@0 48 // fully bootstrap, because of the internal page cache or an external
Chris@0 49 // reverse proxy. Reuse this user for disabling cron later in the test.
Chris@0 50 $admin_user = $this->drupalCreateUser(['administer site configuration']);
Chris@0 51 $this->drupalLogin($admin_user);
Chris@0 52
Chris@0 53 // Ensure cron does not run when a non-zero cron interval is specified and
Chris@0 54 // was not passed.
Chris@0 55 $cron_last = time();
Chris@0 56 $cron_safe_interval = 100;
Chris@0 57 \Drupal::state()->set('system.cron_last', $cron_last);
Chris@0 58 $this->config('automated_cron.settings')
Chris@0 59 ->set('interval', $cron_safe_interval)
Chris@0 60 ->save();
Chris@0 61 $this->drupalGet('');
Chris@0 62 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.');
Chris@0 63
Chris@0 64 // Test if cron runs when the cron interval was passed.
Chris@0 65 $cron_last = time() - 200;
Chris@0 66 \Drupal::state()->set('system.cron_last', $cron_last);
Chris@0 67 $this->drupalGet('');
Chris@0 68 sleep(1);
Chris@0 69 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron interval is passed.');
Chris@0 70
Chris@0 71 // Disable cron through the interface by setting the interval to zero.
Chris@0 72 $this->drupalPostForm('admin/config/system/cron', ['interval' => 0], t('Save configuration'));
Chris@0 73 $this->assertText(t('The configuration options have been saved.'));
Chris@0 74 $this->drupalLogout();
Chris@0 75
Chris@0 76 // Test if cron does not run when the cron interval is set to zero.
Chris@0 77 $cron_last = time() - 200;
Chris@0 78 \Drupal::state()->set('system.cron_last', $cron_last);
Chris@0 79 $this->drupalGet('');
Chris@0 80 $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.');
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Make sure exceptions thrown on hook_cron() don't affect other modules.
Chris@0 85 */
Chris@0 86 public function testCronExceptions() {
Chris@0 87 \Drupal::state()->delete('common_test.cron');
Chris@0 88 // The common_test module throws an exception. If it isn't caught, the tests
Chris@0 89 // won't finish successfully.
Chris@0 90 // The common_test_cron_helper module sets the 'common_test_cron' variable.
Chris@0 91 $this->cronRun();
Chris@0 92 $result = \Drupal::state()->get('common_test.cron');
Chris@0 93 $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.');
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * Make sure the cron UI reads from the state storage.
Chris@0 98 */
Chris@0 99 public function testCronUI() {
Chris@0 100 $admin_user = $this->drupalCreateUser(['administer site configuration']);
Chris@0 101 $this->drupalLogin($admin_user);
Chris@0 102 $this->drupalGet('admin/config/system/cron');
Chris@0 103 // Don't use REQUEST to calculate the exact time, because that will
Chris@0 104 // fail randomly. Look for the word 'years', because without a timestamp,
Chris@0 105 // the time will start at 1 January 1970.
Chris@0 106 $this->assertNoText('years');
Chris@0 107
Chris@0 108 $cron_last = time() - 200;
Chris@0 109 \Drupal::state()->set('system.cron_last', $cron_last);
Chris@0 110
Chris@0 111 $this->drupalPostForm(NULL, [], 'Save configuration');
Chris@0 112 $this->assertText('The configuration options have been saved.');
Chris@0 113 $this->assertUrl('admin/config/system/cron');
Chris@0 114
Chris@0 115 // Check that cron does not run when saving the configuration form.
Chris@0 116 $this->assertEqual($cron_last, \Drupal::state()->get('system.cron_last'), 'Cron does not run when saving the configuration form.');
Chris@0 117
Chris@0 118 // Check that cron runs when triggered manually.
Chris@0 119 $this->drupalPostForm(NULL, [], 'Run cron');
Chris@0 120 $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when triggered manually.');
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * Ensure that the manual cron run is working.
Chris@0 125 */
Chris@0 126 public function testManualCron() {
Chris@0 127 $admin_user = $this->drupalCreateUser(['administer site configuration']);
Chris@0 128 $this->drupalLogin($admin_user);
Chris@0 129
Chris@0 130 $this->drupalGet('admin/reports/status/run-cron');
Chris@0 131 $this->assertResponse(403);
Chris@0 132
Chris@0 133 $this->drupalGet('admin/reports/status');
Chris@0 134 $this->clickLink(t('Run cron'));
Chris@0 135 $this->assertResponse(200);
Chris@0 136 $this->assertText(t('Cron ran successfully.'));
Chris@0 137 }
Chris@0 138
Chris@0 139 }