1 system.test CronRunTestCase::testAutomaticCron()

Ensure that the automatic cron run feature is working.

In these tests we do not use REQUEST_TIME to track start time, because we need the exact time when cron is triggered.

File

core/modules/system/tests/system.test, line 682
Tests for system.module.

Class

CronRunTestCase

Code

function testAutomaticCron() {
  // Ensure cron does not run when the cron threshold is enabled and was
  // not passed.
  $cron_last = time();
  $cron_safe_threshold = 100;
  state_set('cron_last', $cron_last);
  config('system.core')
    ->set('cron_safe_threshold', $cron_safe_threshold)
    ->save();
  $this->backdropGet('');
  $this->assertTrue($cron_last == state_get('cron_last'), 'Cron does not run when the cron threshold is not passed.');

  // Test if cron runs when the cron threshold was passed.
  $cron_last = time() - 200;
  state_set('cron_last', $cron_last);
  $this->backdropGet('');
  sleep(1);
  $this->assertTrue($cron_last < state_get('cron_last'), 'Cron runs when the cron threshold is passed.');

  // Disable the cron threshold through the interface.
  $admin_user = $this->backdropCreateUser(array('administer site configuration'));
  $this->backdropLogin($admin_user);
  $this->backdropPost('admin/config/system/cron', array('cron_safe_threshold' => 0), t('Save configuration'));
  $this->assertText(t('The configuration options have been saved.'));
  $this->backdropLogout();

  // Test if cron does not run when the cron threshold is disabled.
  $cron_last = time() - 200;
  state_set('cron_last', $cron_last);
  $this->backdropGet('');
  $this->assertTrue($cron_last == state_get('cron_last'), 'Cron does not run when the cron threshold is disabled.');
}