~ubuntu-branches/ubuntu/maverick/mahara/maverick-updates

« back to all changes in this revision

Viewing changes to test/crontest.php

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-07-06 17:35:06 UTC
  • mfrom: (6.3.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100706173506-gxhn0lwqm0v2ikoo
Tags: 1.2.5-2
* Remove postgresql8.3 from recommends, add postgresql8.4
* Add mysql-server-5.1 to recommends

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/**
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
6
 
 *
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.
11
 
 *
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.
16
 
 *
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/>.
19
 
 *
20
 
 * @package    mahara
21
 
 * @subpackage tests
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
25
 
 *
26
 
 */
27
 
 
28
 
define('INTERNAL',1);
29
 
define('PUBLIC',1);
30
 
 
31
 
/**
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.
34
 
  *
35
 
  * For both these examples we will assume a value of 300 (seconds)
36
 
  *
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.
41
 
  *
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.
45
 
 */
46
 
define('MAXRUNAGE', 300);
47
 
 
48
 
require(dirname(dirname(__FILE__)).'/htdocs/init.php');
49
 
 
50
 
log_debug('********** RUNNING CRON TEST **********');
51
 
 
52
 
$cronscript = get_config('libroot') . 'cron.php';
53
 
 
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');
60
 
test_cron_finish();
61
 
 
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');
68
 
test_cron_finish();
69
 
 
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');
76
 
test_cron_finish();
77
 
 
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');
84
 
test_cron_finish();
85
 
 
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');
92
 
test_cron_finish();
93
 
 
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');
100
 
test_cron_finish();
101
 
 
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');
108
 
test_cron_finish();
109
 
 
110
 
 
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');
115
 
test_cron_finish();
116
 
 
117
 
 
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');
122
 
test_cron_finish();
123
 
 
124
 
 
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');
129
 
test_cron_finish();
130
 
 
131
 
 
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');
136
 
test_cron_finish();
137
 
 
138
 
function test_cron_set($callfunction, $minute, $hour, $day, $month, $dayofweek, $nextrun = null) {
139
 
    delete_records('artefact_cron', 'plugin', 'internal', 'callfunction', $callfunction);
140
 
 
141
 
    insert_record(
142
 
        'artefact_cron',
143
 
        (object) array (
144
 
            'plugin'          => 'internal',
145
 
            'callfunction'    => $callfunction,
146
 
            'nextrun'         => $nextrun,
147
 
            'minute'          => $minute,
148
 
            'hour'            => $hour,
149
 
            'day'             => $day,
150
 
            'month'           => $month,
151
 
            'dayofweek'       => $dayofweek
152
 
        )
153
 
    );
154
 
}
155
 
 
156
 
function test_cron_assert($callfunction, $nextrun) {
157
 
    global $errors;
158
 
 
159
 
    $record = get_record_select(
160
 
        'artefact_cron',
161
 
        'callfunction = ?',
162
 
        array($callfunction),
163
 
        'nextrun'
164
 
    );
165
 
 
166
 
    $nextrun = strtotime($nextrun);
167
 
    $realrun = strtotime($record->nextrun);
168
 
 
169
 
    if ($nextrun != $realrun) {
170
 
        echo("\n\tAssertion failed in '$callfunction': expected " . test_cron_prettydate($nextrun) . " != actual " . test_cron_prettydate($realrun) . "\n");
171
 
        $errors++;
172
 
    }
173
 
}
174
 
 
175
 
function test_cron_start($text) {
176
 
    global $errors;
177
 
 
178
 
    echo $text;
179
 
 
180
 
    $errors = 0;
181
 
}
182
 
 
183
 
function test_cron_run($stamp) {
184
 
    global $cronscript;
185
 
    $config_file = get_cfg_var('cfg_file_path');
186
 
 
187
 
    exec("php -c $config_file $cronscript " . escapeshellarg($stamp));
188
 
}
189
 
 
190
 
function test_cron_finish() {
191
 
    global $errors;
192
 
 
193
 
    if ($errors) {
194
 
        echo("FAILED\n");
195
 
    }
196
 
    else {
197
 
        echo("OK\n");
198
 
    }
199
 
}
200
 
 
201
 
function test_cron_prettydate($timestamp) {
202
 
    return db_format_timestamp($timestamp);
203
 
}
204
 
 
205
 
?>