3
* Mahara: Electronic portfolio, weblog, resume builder and social networking
4
* Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
5
* http://wiki.mahara.org/Contributors
7
* This program is free software: you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation, either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22
* @author Martyn Smith <martyn@catalyst.net.nz>
23
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
24
* @copyright (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
32
* This defines (in seconds) how far PAST the next run time of a cron job
33
* we're allowed to get and still go back and run it.
35
* For both these examples we will assume a value of 300 (seconds)
37
* example 1: If we have a job that was meant to run at 10:45am, and it's now
38
* 10:48am we know that the job was meant to run 3 minutes (180 seconds) ago.
39
* This is within the threshold MAXRUNAGE and so we will run the job, and
40
* update the next run time.
42
* example 2: If we have a job that was meant to run at 9:34am, and it's now
43
* 9:40am we know the job _should_ have been run, but it's too late now. We
44
* DON'T run the job, but do update it's next run time.
46
define('MAXRUNAGE', 300);
48
require(dirname(dirname(__FILE__)).'/htdocs/init.php');
50
log_debug('********** RUNNING CRON TEST **********');
52
$cronscript = get_config('libroot') . 'cron.php';
54
test_cron_start('Job to run every minute ... ');
55
test_cron_set('get_event_subscriptions', '*', '*', '*', '*', '*', null);
56
test_cron_set('menu_items', '*', '*', '*', '*', '*', '2006-06-01 10:03');
57
test_cron_run('2006-06-01 10:06');
58
test_cron_assert('get_event_subscriptions', '2006-06-01 10:07');
59
test_cron_assert('menu_items', '2006-06-01 10:07');
62
test_cron_start('Job to run every n minutes ... ');
63
test_cron_set('get_event_subscriptions', '*/5', '*', '*', '*', '*', null);
64
test_cron_set('menu_items', '*/12', '*', '*', '*', '*', null);
65
test_cron_run('2006-06-01 10:06');
66
test_cron_assert('get_event_subscriptions', '2006-06-01 10:10');
67
test_cron_assert('menu_items', '2006-06-01 10:12');
70
test_cron_start('Job to run on nth day of the month ... ');
71
test_cron_set('get_event_subscriptions', '21', '21', '31', '*', '*', null);
72
test_cron_set('menu_items', '21', '21', '15', '*', '*', null);
73
test_cron_run('2006-06-01 10:06');
74
test_cron_assert('get_event_subscriptions', '2006-07-31 21:21');
75
test_cron_assert('menu_items', '2006-06-15 21:21');
78
test_cron_start('Job to run in a month that\'s already been this year ... ');
79
test_cron_set('get_event_subscriptions', '1', '12', '30', '4', '*', null);
80
test_cron_set('menu_items', '12', '13', '10', '5', '*', null);
81
test_cron_run('2006-06-01 10:06');
82
test_cron_assert('get_event_subscriptions', '2007-04-30 12:01');
83
test_cron_assert('menu_items', '2007-05-10 13:12');
86
test_cron_start('Job to run on a range ... ');
87
test_cron_set('get_event_subscriptions', '30-40', '*', '*', '*', '*', null);
88
test_cron_set('menu_items', '10-30/3', '*', '*', '*', '*', null);
89
test_cron_run('2006-06-01 10:18');
90
test_cron_assert('get_event_subscriptions', '2006-06-01 10:30');
91
test_cron_assert('menu_items', '2006-06-01 10:19');
94
test_cron_start('Job to run on on sundays (using 0 and 7) ... ');
95
test_cron_set('get_event_subscriptions', '*', '*', '*', '*', '0', null);
96
test_cron_set('menu_items', '*', '*', '*', '*', '7', null);
97
test_cron_run('2006-06-01 10:18');
98
test_cron_assert('get_event_subscriptions', '2006-06-04 00:00');
99
test_cron_assert('menu_items', '2006-06-04 00:00');
102
test_cron_start('Job to run on a day of the week ... ');
103
test_cron_set('get_event_subscriptions', '1', '12', '4', '8', '3', null);
104
test_cron_set('menu_items', '12', '13', '4', '7', '3', null);
105
test_cron_run('2006-06-01 10:06');
106
test_cron_assert('get_event_subscriptions', '2006-08-02 12:01');
107
test_cron_assert('menu_items', '2006-07-04 13:12');
111
test_cron_start('Pretend to do nigel\'s squash run... ');
112
test_cron_set('get_event_subscriptions', '0', '12', '*', '*', '3', null);
113
test_cron_run('2006-10-30 16:56');
114
test_cron_assert('get_event_subscriptions', '2006-11-01 12:00');
118
test_cron_start('Update mochikit documentation... ');
119
test_cron_set('get_event_subscriptions', '0', '7', '*', '*', '*', null);
120
test_cron_run('2006-10-30 16:56');
121
test_cron_assert('get_event_subscriptions', '2006-10-31 7:00');
125
test_cron_start('Run moodle cron.php every 5 minutes... ');
126
test_cron_set('get_event_subscriptions', '*/5', '*', '*', '*', '*', null);
127
test_cron_run('2006-10-30 16:56');
128
test_cron_assert('get_event_subscriptions', '2006-10-30 17:00');
132
test_cron_start('Test yearly boundary (run on 1st jan)... ');
133
test_cron_set('get_event_subscriptions', '*', '*', '1', '1', '*', null);
134
test_cron_run('2006-10-30 16:56');
135
test_cron_assert('get_event_subscriptions', '2007-01-01 0:00');
138
function test_cron_set($callfunction, $minute, $hour, $day, $month, $dayofweek, $nextrun = null) {
139
delete_records('artefact_cron', 'plugin', 'internal', 'callfunction', $callfunction);
144
'plugin' => 'internal',
145
'callfunction' => $callfunction,
146
'nextrun' => $nextrun,
151
'dayofweek' => $dayofweek
156
function test_cron_assert($callfunction, $nextrun) {
159
$record = get_record_select(
162
array($callfunction),
166
$nextrun = strtotime($nextrun);
167
$realrun = strtotime($record->nextrun);
169
if ($nextrun != $realrun) {
170
echo("\n\tAssertion failed in '$callfunction': expected " . test_cron_prettydate($nextrun) . " != actual " . test_cron_prettydate($realrun) . "\n");
175
function test_cron_start($text) {
183
function test_cron_run($stamp) {
185
$config_file = get_cfg_var('cfg_file_path');
187
exec("php -c $config_file $cronscript " . escapeshellarg($stamp));
190
function test_cron_finish() {
201
function test_cron_prettydate($timestamp) {
202
return db_format_timestamp($timestamp);