rules.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * @file
  4. *
  5. * Tests for Ultimate Cron's cron parser
  6. */
  7. class UltimateCronRulesUnitTestCase extends DrupalUnitTestCase {
  8. function setUp() {
  9. parent::setUp('ultimate_cron');
  10. }
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Rules',
  14. 'description' => 'Test crontab rules parser.',
  15. 'group' => 'Cron',
  16. );
  17. }
  18. private function runTest($options) {
  19. // Setup values
  20. $options['rules'] = is_array($options['rules']) ? $options['rules'] : array($options['rules']);
  21. $options['catch_up'] = isset($options['catch_up']) ? $options['catch_up'] : 86400 * 365; // @todo Adapting Elysia Cron test cases with a catchup of 1 year
  22. // Generate result message
  23. require_once dirname(__FILE__) . '/../CronRule.class.php';
  24. $message = array();
  25. foreach ($options['rules'] as $rule) {
  26. $cron = new CronRule($rule);
  27. $intervals = $cron->getIntervals();
  28. $parsed_rule = '';
  29. foreach ($intervals as $key => $value) {
  30. $parsed_rule .= "$key: " . implode(',', $value) . "\n";
  31. }
  32. #$parsed_rule = str_replace(" ", "\n", $cron->rebuildRule($cron->getIntervals()));
  33. $last_scheduled = $cron->getLastRan(strtotime($options['now']));
  34. $message[] = "<span title=\"$parsed_rule\">$rule</span> @ " . date('Y-m-d H:i:s', $last_scheduled);
  35. }
  36. $message[] = 'now @ ' . $options['now'];
  37. $message[] = 'last-run @ ' . $options['last_run'];
  38. $message[] = 'catch-up @ ' . $options['catch_up'];
  39. $message[] = ($options['result'] ? '' : 'not ') . 'expected to run';
  40. // Do the actual test
  41. $result = ultimate_cron_should_run($options['rules'], strtotime($options['last_run']), strtotime($options['now']), $options['catch_up']);
  42. return array($options['result'] == $result, implode('<br/>', $message));
  43. }
  44. function testRules() {
  45. $result = $this->runTest(array('result' => FALSE, 'rules' => '0 12 * * *' , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-02 12:01:00'));
  46. $this->assertTRUE($result[0], $result[1]);
  47. $result = $this->runTest(array('result' => FALSE, 'rules' => '0 12 * * *' , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-02 15:00:00'));
  48. $this->assertTRUE($result[0], $result[1]);
  49. $result = $this->runTest(array('result' => FALSE, 'rules' => '0 12 * * *' , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-03 11:59:00'));
  50. $this->assertTRUE($result[0], $result[1]);
  51. $result = $this->runTest(array('result' => TRUE , 'rules' => '0 12 * * *' , 'last_run' => '2008-01-02 12:00:00', 'now' => '2008-01-03 12:00:00'));
  52. $this->assertTRUE($result[0], $result[1]);
  53. $result = $this->runTest(array('result' => FALSE, 'rules' => '59 23 * * *' , 'last_run' => '2008-01-02 23:59:00', 'now' => '2008-01-03 00:00:00'));
  54. $this->assertTRUE($result[0], $result[1]);
  55. $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *' , 'last_run' => '2008-01-02 23:59:00', 'now' => '2008-01-03 23:59:00'));
  56. $this->assertTRUE($result[0], $result[1]);
  57. $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *' , 'last_run' => '2008-01-02 23:59:00', 'now' => '2008-01-04 00:00:00'));
  58. $this->assertTRUE($result[0], $result[1]);
  59. $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *' , 'last_run' => '2008-01-02 23:58:00', 'now' => '2008-01-02 23:59:00'));
  60. $this->assertTRUE($result[0], $result[1]);
  61. $result = $this->runTest(array('result' => TRUE , 'rules' => '59 23 * * *' , 'last_run' => '2008-01-02 23:58:00', 'now' => '2008-01-03 00:00:00'));
  62. $this->assertTRUE($result[0], $result[1]);
  63. $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'));
  64. $this->assertTRUE($result[0], $result[1]);
  65. $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'));
  66. $this->assertTRUE($result[0], $result[1]);
  67. $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'));
  68. $this->assertTRUE($result[0], $result[1]);
  69. $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'));
  70. $this->assertTRUE($result[0], $result[1]);
  71. $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'));
  72. $this->assertTRUE($result[0], $result[1]);
  73. $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'));
  74. $this->assertTRUE($result[0], $result[1]);
  75. $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'));
  76. $this->assertTRUE($result[0], $result[1]);
  77. $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'));
  78. $this->assertTRUE($result[0], $result[1]);
  79. $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'));
  80. $this->assertTRUE($result[0], $result[1]);
  81. $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'));
  82. $this->assertTRUE($result[0], $result[1]);
  83. $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'));
  84. $this->assertTRUE($result[0], $result[1]);
  85. $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'));
  86. $this->assertTRUE($result[0], $result[1]);
  87. $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'));
  88. $this->assertTRUE($result[0], $result[1]);
  89. $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'));
  90. $this->assertTRUE($result[0], $result[1]);
  91. $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'));
  92. $this->assertTRUE($result[0], $result[1]);
  93. $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'));
  94. $this->assertTRUE($result[0], $result[1]);
  95. $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'));
  96. $this->assertTRUE($result[0], $result[1]);
  97. $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'));
  98. $this->assertTRUE($result[0], $result[1]);
  99. $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'));
  100. $this->assertTRUE($result[0], $result[1]);
  101. $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'));
  102. $this->assertTRUE($result[0], $result[1]);
  103. $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'));
  104. $this->assertTRUE($result[0], $result[1]);
  105. $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'));
  106. $this->assertTRUE($result[0], $result[1]);
  107. $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'));
  108. $this->assertTRUE($result[0], $result[1]);
  109. $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'));
  110. $this->assertTRUE($result[0], $result[1]);
  111. $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'));
  112. $this->assertTRUE($result[0], $result[1]);
  113. $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'));
  114. $this->assertTRUE($result[0], $result[1]);
  115. $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'));
  116. $this->assertTRUE($result[0], $result[1]);
  117. $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'));
  118. $this->assertTRUE($result[0], $result[1]);
  119. $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'));
  120. $this->assertTRUE($result[0], $result[1]);
  121. $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'));
  122. $this->assertTRUE($result[0], $result[1]);
  123. $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'));
  124. $this->assertTRUE($result[0], $result[1]);
  125. $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'));
  126. $this->assertTRUE($result[0], $result[1]);
  127. $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'));
  128. $this->assertTRUE($result[0], $result[1]);
  129. $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'));
  130. $this->assertTRUE($result[0], $result[1]);
  131. $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'));
  132. $this->assertTRUE($result[0], $result[1]);
  133. $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'));
  134. $this->assertTRUE($result[0], $result[1]);
  135. $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'));
  136. $this->assertTRUE($result[0], $result[1]);
  137. $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'));
  138. $this->assertTRUE($result[0], $result[1]);
  139. $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'));
  140. $this->assertTRUE($result[0], $result[1]);
  141. $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'));
  142. $this->assertTRUE($result[0], $result[1]);
  143. $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'));
  144. $this->assertTRUE($result[0], $result[1]);
  145. $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'));
  146. $this->assertTRUE($result[0], $result[1]);
  147. $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'));
  148. $this->assertTRUE($result[0], $result[1]);
  149. $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'));
  150. $this->assertTRUE($result[0], $result[1]);
  151. $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'));
  152. $this->assertTRUE($result[0], $result[1]);
  153. $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'));
  154. $this->assertTRUE($result[0], $result[1]);
  155. $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'));
  156. $this->assertTRUE($result[0], $result[1]);
  157. }
  158. function testRulesExtended() {
  159. $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'));
  160. $this->assertTRUE($result[0], $result[1]);
  161. $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'));
  162. $this->assertTRUE($result[0], $result[1]);
  163. }
  164. }