Chris@0: drupalGet('cron'); Chris@0: $this->assertResponse(404); Chris@0: Chris@0: // Run cron anonymously with a random cron key. Chris@0: $key = $this->randomMachineName(16); Chris@0: $this->drupalGet('cron/' . $key); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Run cron anonymously with the valid cron key. Chris@0: $key = \Drupal::state()->get('system.cron_key'); Chris@0: $this->drupalGet('cron/' . $key); Chris@0: $this->assertResponse(204); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that the automated cron run module is working. Chris@0: * Chris@0: * In these tests we do not use REQUEST_TIME to track start time, because we Chris@0: * need the exact time when cron is triggered. Chris@0: */ Chris@0: public function testAutomatedCron() { Chris@0: // Test with a logged in user; anonymous users likely don't cause Drupal to Chris@0: // fully bootstrap, because of the internal page cache or an external Chris@0: // reverse proxy. Reuse this user for disabling cron later in the test. Chris@0: $admin_user = $this->drupalCreateUser(['administer site configuration']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: // Ensure cron does not run when a non-zero cron interval is specified and Chris@0: // was not passed. Chris@0: $cron_last = time(); Chris@0: $cron_safe_interval = 100; Chris@0: \Drupal::state()->set('system.cron_last', $cron_last); Chris@0: $this->config('automated_cron.settings') Chris@0: ->set('interval', $cron_safe_interval) Chris@0: ->save(); Chris@0: $this->drupalGet(''); Chris@0: $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron interval is not passed.'); Chris@0: Chris@0: // Test if cron runs when the cron interval was passed. Chris@0: $cron_last = time() - 200; Chris@0: \Drupal::state()->set('system.cron_last', $cron_last); Chris@0: $this->drupalGet(''); Chris@0: sleep(1); Chris@0: $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when the cron interval is passed.'); Chris@0: Chris@0: // Disable cron through the interface by setting the interval to zero. Chris@0: $this->drupalPostForm('admin/config/system/cron', ['interval' => 0], t('Save configuration')); Chris@0: $this->assertText(t('The configuration options have been saved.')); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Test if cron does not run when the cron interval is set to zero. Chris@0: $cron_last = time() - 200; Chris@0: \Drupal::state()->set('system.cron_last', $cron_last); Chris@0: $this->drupalGet(''); Chris@0: $this->assertTrue($cron_last == \Drupal::state()->get('system.cron_last'), 'Cron does not run when the cron threshold is disabled.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Make sure exceptions thrown on hook_cron() don't affect other modules. Chris@0: */ Chris@0: public function testCronExceptions() { Chris@0: \Drupal::state()->delete('common_test.cron'); Chris@0: // The common_test module throws an exception. If it isn't caught, the tests Chris@0: // won't finish successfully. Chris@0: // The common_test_cron_helper module sets the 'common_test_cron' variable. Chris@0: $this->cronRun(); Chris@0: $result = \Drupal::state()->get('common_test.cron'); Chris@0: $this->assertEqual($result, 'success', 'Cron correctly handles exceptions thrown during hook_cron() invocations.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Make sure the cron UI reads from the state storage. Chris@0: */ Chris@0: public function testCronUI() { Chris@0: $admin_user = $this->drupalCreateUser(['administer site configuration']); Chris@0: $this->drupalLogin($admin_user); Chris@0: $this->drupalGet('admin/config/system/cron'); Chris@0: // Don't use REQUEST to calculate the exact time, because that will Chris@0: // fail randomly. Look for the word 'years', because without a timestamp, Chris@0: // the time will start at 1 January 1970. Chris@0: $this->assertNoText('years'); Chris@0: Chris@0: $cron_last = time() - 200; Chris@0: \Drupal::state()->set('system.cron_last', $cron_last); Chris@0: Chris@0: $this->drupalPostForm(NULL, [], 'Save configuration'); Chris@0: $this->assertText('The configuration options have been saved.'); Chris@0: $this->assertUrl('admin/config/system/cron'); Chris@0: Chris@0: // Check that cron does not run when saving the configuration form. Chris@0: $this->assertEqual($cron_last, \Drupal::state()->get('system.cron_last'), 'Cron does not run when saving the configuration form.'); Chris@0: Chris@0: // Check that cron runs when triggered manually. Chris@0: $this->drupalPostForm(NULL, [], 'Run cron'); Chris@0: $this->assertTrue($cron_last < \Drupal::state()->get('system.cron_last'), 'Cron runs when triggered manually.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensure that the manual cron run is working. Chris@0: */ Chris@0: public function testManualCron() { Chris@0: $admin_user = $this->drupalCreateUser(['administer site configuration']); Chris@0: $this->drupalLogin($admin_user); Chris@0: Chris@0: $this->drupalGet('admin/reports/status/run-cron'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: $this->drupalGet('admin/reports/status'); Chris@0: $this->clickLink(t('Run cron')); Chris@0: $this->assertResponse(200); Chris@0: $this->assertText(t('Cron ran successfully.')); Chris@0: } Chris@0: Chris@0: }