| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 | <?php/** * @file * * Tests for Ultimate Cron's cron parser */class UltimateCronRulesUnitTestCase extends DrupalUnitTestCase {  function setUp() {    parent::setUp('ultimate_cron');  }  public static function getInfo() {    return array(      'name' => 'Rules',      'description' => 'Test crontab rules parser.',      'group' => 'Cron',    );  }  private function runTest($options) {    // Setup values    $options['rules'] = is_array($options['rules']) ? $options['rules'] : array($options['rules']);    $options['catch_up'] = isset($options['catch_up']) ? $options['catch_up'] : 86400 * 365; // @todo Adapting Elysia Cron test cases with a catchup of 1 year    // Generate result message    require_once dirname(__FILE__) . '/../CronRule.class.php';    $message = array();    foreach ($options['rules'] as $rule) {      $cron = new CronRule($rule);      $intervals = $cron->getIntervals();      $parsed_rule = '';      foreach ($intervals as $key => $value) {        $parsed_rule .= "$key: " . implode(',', $value) . "\n";      }      #$parsed_rule = str_replace(" ", "\n", $cron->rebuildRule($cron->getIntervals()));      $last_scheduled = $cron->getLastRan(strtotime($options['now']));      $message[] = "<span title=\"$parsed_rule\">$rule</span> @ " . date('Y-m-d H:i:s', $last_scheduled);    }    $message[] = 'now      @ ' . $options['now'];    $message[] = 'last-run @ ' . $options['last_run'];    $message[] = 'catch-up @ ' . $options['catch_up'];    $message[] = ($options['result'] ? '' : 'not ') . 'expected to run';    // Do the actual test    $result = ultimate_cron_should_run($options['rules'], strtotime($options['last_run']), strtotime($options['now']), $options['catch_up']);    return array($options['result'] == $result, implode('<br/>', $message));  }  function testRules() {    $result = $this->runTest(array('result' => FALSE, 'rules' => '0 12 * * *'                  , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-02 12:01:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '0 12 * * *'                  , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-02 15:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '0 12 * * *'                  , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-03 11:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '0 12 * * *'                  , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-03 12:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * * *'                 , 'last_run' => '2008-01-02 23:59:00', 'now' => '2008-01-03 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *'                 , 'last_run' => '2008-01-02 23:59:00', 'now' => '2008-01-03 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *'                 , 'last_run' => '2008-01-02 23:59:00', 'now' => '2008-01-04 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *'                 , 'last_run' => '2008-01-02 23:58:00', 'now' => '2008-01-02 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *'                 , 'last_run' => '2008-01-02 23:58:00', 'now' => '2008-01-03 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * * 0'                 , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-05 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * * 0'                 , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-06 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * 0'                 , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-06 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * 0'                 , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-07 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-06 23:29:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-06 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-05 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-06 23:58:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:58:00', 'now' => '2008-01-06 23:28:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:28:00', 'now' => '2008-01-05 23:29:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:28:00', 'now' => '2008-01-05 23:30:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:28:00', 'now' => '2008-01-05 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '29,59 23 * * 0'              , 'last_run' => '2008-01-05 23:28:00', 'now' => '2008-01-06 23:29:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '29,59 23 * * 5'              , 'last_run' => '2008-02-22 23:59:00', 'now' => '2008-02-28 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '29,59 23 * * 5'              , 'last_run' => '2008-02-22 23:59:00', 'now' => '2008-02-29 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '29,59 23 * * 5'              , 'last_run' => '2008-02-22 23:59:00', 'now' => '2008-03-01 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * * 3'                 , 'last_run' => '2008-12-31 23:59:00', 'now' => '2009-01-01 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * * 3'                 , 'last_run' => '2008-12-31 23:59:00', 'now' => '2009-01-07 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * 3'                 , 'last_run' => '2008-12-31 23:59:00', 'now' => '2009-01-07 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * 2 5'                 , 'last_run' => '2008-02-22 23:59:00', 'now' => '2008-02-29 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * 2 5'                 , 'last_run' => '2008-02-22 23:59:00', 'now' => '2008-03-01 00:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * 2 5'                 , 'last_run' => '2008-02-29 23:59:00', 'now' => '2008-03-07 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * 2 5'                 , 'last_run' => '2008-02-29 23:59:00', 'now' => '2009-02-06 23:58:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * 2 5'                 , 'last_run' => '2008-02-29 23:59:00', 'now' => '2009-02-06 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 */10 * *'              , 'last_run' => '2008-01-10 23:58:00', 'now' => '2008-01-10 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 */10 * *'              , 'last_run' => '2008-01-10 23:59:00', 'now' => '2008-01-11 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 */10 * *'              , 'last_run' => '2008-01-10 23:59:00', 'now' => '2008-01-20 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5,10-15 * *'         , 'last_run' => '2008-01-04 23:59:00', 'now' => '2008-01-05 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5,10-15 * *'         , 'last_run' => '2008-01-04 23:59:00', 'now' => '2008-01-06 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 1-5,10-15 * *'         , 'last_run' => '2008-01-05 23:59:00', 'now' => '2008-01-06 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 1-5,10-15 * *'         , 'last_run' => '2008-01-05 23:59:00', 'now' => '2008-01-10 23:58:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5,10-15 * *'         , 'last_run' => '2008-01-05 23:59:00', 'now' => '2008-01-10 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5,10-15 * *'         , 'last_run' => '2008-01-05 23:59:00', 'now' => '2008-01-16 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-01-04 23:59:00', 'now' => '2008-01-05 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-01-05 23:59:00', 'now' => '2008-01-06 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-01-06 23:59:00', 'now' => '2008-01-07 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-01-06 23:59:00', 'now' => '2008-01-13 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-02-04 23:59:00', 'now' => '2008-02-05 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-02-05 23:59:00', 'now' => '2008-02-10 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 1-5 1 0'               , 'last_run' => '2008-02-10 23:59:00', 'now' => '2008-02-17 23:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 08:58:00', 'now' => '2008-02-10 08:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 08:59:00', 'now' => '2008-02-10 09:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => FALSE, 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 08:59:00', 'now' => '2008-02-10 17:59:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 08:59:00', 'now' => '2008-02-10 18:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 18:00:00', 'now' => '2008-02-10 18:01:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 18:00:00', 'now' => '2008-02-10 19:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '* 0,1,2,3,4,5,6,7,8,18,19,20,21,22,23 * * *', 'last_run' => '2008-02-10 18:00:00', 'now' => '2008-03-10 09:00:00'));    $this->assertTRUE($result[0], $result[1]);  }  function testRulesExtended() {    $result = $this->runTest(array('result' => FALSE, 'rules' => '0 0 * jan,oct *', 'last_run' => '2008-01-31 00:00:00', 'now' => '2008-03-10 09:00:00'));    $this->assertTRUE($result[0], $result[1]);    $result = $this->runTest(array('result' => TRUE , 'rules' => '0 0 * jan,oct *', 'last_run' => '2008-01-31 00:00:00', 'now' => '2008-10-01 00:00:00'));    $this->assertTRUE($result[0], $result[1]);  }}
 |