2 // This file is part of Moodle - http://moodle.org/
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 * Unit tests for backups cron helper.
20 * @package core_backup
22 * @copyright 2012 Frédéric Massart <fred@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
32 * Unit tests for backup cron helper
34 class backup_cron_helper_testcase extends advanced_testcase {
37 * Test {@link backup_cron_automated_helper::calculate_next_automated_backup}.
39 public function test_next_automated_backup() {
40 $this->resetAfterTest();
41 set_config('backup_auto_active', '1', 'backup');
44 // - backup_auto_weekdays starts on Sunday
45 // - Tests cannot be done in the past
46 // - Only the DST on the server side is handled.
48 // Every Tue and Fri at 11pm.
49 set_config('backup_auto_weekdays', '0010010', 'backup');
50 set_config('backup_auto_hour', '23', 'backup');
51 set_config('backup_auto_minute', '0', 'backup');
54 $now = strtotime('next Monday 17:00:00');
55 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
56 $this->assertEquals('2-23:00', date('w-H:i', $next));
58 $now = strtotime('next Tuesday 18:00:00');
59 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
60 $this->assertEquals('2-23:00', date('w-H:i', $next));
62 $now = strtotime('next Wednesday 17:00:00');
63 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
64 $this->assertEquals('5-23:00', date('w-H:i', $next));
66 $now = strtotime('next Thursday 17:00:00');
67 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
68 $this->assertEquals('5-23:00', date('w-H:i', $next));
70 $now = strtotime('next Friday 17:00:00');
71 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
72 $this->assertEquals('5-23:00', date('w-H:i', $next));
74 $now = strtotime('next Saturday 17:00:00');
75 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
76 $this->assertEquals('2-23:00', date('w-H:i', $next));
78 $now = strtotime('next Sunday 17:00:00');
79 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
80 $this->assertEquals('2-23:00', date('w-H:i', $next));
82 // Every Sun and Sat at 12pm.
83 set_config('backup_auto_weekdays', '1000001', 'backup');
84 set_config('backup_auto_hour', '0', 'backup');
85 set_config('backup_auto_minute', '0', 'backup');
88 $now = strtotime('next Monday 17:00:00');
89 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
90 $this->assertEquals('6-00:00', date('w-H:i', $next));
92 $now = strtotime('next Tuesday 17:00:00');
93 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
94 $this->assertEquals('6-00:00', date('w-H:i', $next));
96 $now = strtotime('next Wednesday 17:00:00');
97 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
98 $this->assertEquals('6-00:00', date('w-H:i', $next));
100 $now = strtotime('next Thursday 17:00:00');
101 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
102 $this->assertEquals('6-00:00', date('w-H:i', $next));
104 $now = strtotime('next Friday 17:00:00');
105 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
106 $this->assertEquals('6-00:00', date('w-H:i', $next));
108 $now = strtotime('next Saturday 17:00:00');
109 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
110 $this->assertEquals('0-00:00', date('w-H:i', $next));
112 $now = strtotime('next Sunday 17:00:00');
113 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
114 $this->assertEquals('6-00:00', date('w-H:i', $next));
117 set_config('backup_auto_weekdays', '1000000', 'backup');
118 set_config('backup_auto_hour', '4', 'backup');
119 set_config('backup_auto_minute', '0', 'backup');
122 $now = strtotime('next Monday 17:00:00');
123 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
124 $this->assertEquals('0-04:00', date('w-H:i', $next));
126 $now = strtotime('next Tuesday 17:00:00');
127 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
128 $this->assertEquals('0-04:00', date('w-H:i', $next));
130 $now = strtotime('next Wednesday 17:00:00');
131 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
132 $this->assertEquals('0-04:00', date('w-H:i', $next));
134 $now = strtotime('next Thursday 17:00:00');
135 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
136 $this->assertEquals('0-04:00', date('w-H:i', $next));
138 $now = strtotime('next Friday 17:00:00');
139 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
140 $this->assertEquals('0-04:00', date('w-H:i', $next));
142 $now = strtotime('next Saturday 17:00:00');
143 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
144 $this->assertEquals('0-04:00', date('w-H:i', $next));
146 $now = strtotime('next Sunday 17:00:00');
147 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
148 $this->assertEquals('0-04:00', date('w-H:i', $next));
150 // Every day but Wed at 8:30pm.
151 set_config('backup_auto_weekdays', '1110111', 'backup');
152 set_config('backup_auto_hour', '20', 'backup');
153 set_config('backup_auto_minute', '30', 'backup');
156 $now = strtotime('next Monday 17:00:00');
157 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
158 $this->assertEquals('1-20:30', date('w-H:i', $next));
160 $now = strtotime('next Tuesday 17:00:00');
161 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
162 $this->assertEquals('2-20:30', date('w-H:i', $next));
164 $now = strtotime('next Wednesday 17:00:00');
165 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
166 $this->assertEquals('4-20:30', date('w-H:i', $next));
168 $now = strtotime('next Thursday 17:00:00');
169 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
170 $this->assertEquals('4-20:30', date('w-H:i', $next));
172 $now = strtotime('next Friday 17:00:00');
173 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
174 $this->assertEquals('5-20:30', date('w-H:i', $next));
176 $now = strtotime('next Saturday 17:00:00');
177 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
178 $this->assertEquals('6-20:30', date('w-H:i', $next));
180 $now = strtotime('next Sunday 17:00:00');
181 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
182 $this->assertEquals('0-20:30', date('w-H:i', $next));
184 // Sun, Tue, Thu, Sat at 12pm.
185 set_config('backup_auto_weekdays', '1010101', 'backup');
186 set_config('backup_auto_hour', '0', 'backup');
187 set_config('backup_auto_minute', '0', 'backup');
190 $now = strtotime('next Monday 13:00:00');
191 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
192 $this->assertEquals('2-00:00', date('w-H:i', $next));
194 $now = strtotime('next Tuesday 13:00:00');
195 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
196 $this->assertEquals('4-00:00', date('w-H:i', $next));
198 $now = strtotime('next Wednesday 13:00:00');
199 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
200 $this->assertEquals('4-00:00', date('w-H:i', $next));
202 $now = strtotime('next Thursday 13:00:00');
203 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
204 $this->assertEquals('6-00:00', date('w-H:i', $next));
206 $now = strtotime('next Friday 13:00:00');
207 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
208 $this->assertEquals('6-00:00', date('w-H:i', $next));
210 $now = strtotime('next Saturday 13:00:00');
211 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
212 $this->assertEquals('0-00:00', date('w-H:i', $next));
214 $now = strtotime('next Sunday 13:00:00');
215 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
216 $this->assertEquals('2-00:00', date('w-H:i', $next));
219 set_config('backup_auto_weekdays', '0000000', 'backup');
220 set_config('backup_auto_hour', '15', 'backup');
221 set_config('backup_auto_minute', '30', 'backup');
224 $now = strtotime('next Sunday 13:00:00');
225 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
226 $this->assertEquals('0', $next);
228 // Playing with timezones.
229 set_config('backup_auto_weekdays', '1111111', 'backup');
230 set_config('backup_auto_hour', '20', 'backup');
231 set_config('backup_auto_minute', '00', 'backup');
234 date_default_timezone_set('Australia/Perth');
235 $now = strtotime('18:00:00');
236 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
237 $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
240 date_default_timezone_set('Europe/Brussels');
241 $now = strtotime('18:00:00');
242 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
243 $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
246 date_default_timezone_set('America/New_York');
247 $now = strtotime('18:00:00');
248 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
249 $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
251 // Viva Australia! (UTC+8).
252 date_default_timezone_set('Australia/Perth');
253 $now = strtotime('18:00:00');
255 $timezone = -10.0; // 12am for the user.
256 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
257 $this->assertEquals(date('w-14:00', strtotime('tomorrow')), date('w-H:i', $next));
259 $timezone = -5.0; // 5am for the user.
260 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
261 $this->assertEquals(date('w-09:00', strtotime('tomorrow')), date('w-H:i', $next));
263 $timezone = 0.0; // 10am for the user.
264 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
265 $this->assertEquals(date('w-04:00', strtotime('tomorrow')), date('w-H:i', $next));
267 $timezone = 3.0; // 1pm for the user.
268 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
269 $this->assertEquals(date('w-01:00', strtotime('tomorrow')), date('w-H:i', $next));
271 $timezone = 8.0; // 6pm for the user (same than the server).
272 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
273 $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
275 $timezone = 9.0; // 7pm for the user.
276 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
277 $this->assertEquals(date('w-19:00'), date('w-H:i', $next));
279 $timezone = 13.0; // 12am for the user.
280 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
281 $this->assertEquals(date('w-15:00', strtotime('tomorrow')), date('w-H:i', $next));
283 // Let's have a Belgian beer! (UTC+1 / UTC+2 DST).
284 date_default_timezone_set('Europe/Brussels');
285 $now = strtotime('18:00:00');
288 $timezone = -10.0; // 7am for the user.
289 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
290 $expected = !$dst ? date('w-07:00', strtotime('tomorrow')) : date('w-08:00', strtotime('tomorrow'));
291 $this->assertEquals($expected, date('w-H:i', $next));
293 $timezone = -5.0; // 12pm for the user.
294 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
295 $expected = !$dst ? date('w-02:00', strtotime('tomorrow')) : date('w-03:00', strtotime('tomorrow'));
296 $this->assertEquals($expected, date('w-H:i', $next));
298 $timezone = 0.0; // 5pm for the user.
299 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
300 $expected = !$dst ? date('w-21:00') : date('w-22:00');
301 $this->assertEquals($expected, date('w-H:i', $next));
303 $timezone = 3.0; // 8pm for the user (note the expected time is today while in DST).
304 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
305 $expected = !$dst ? date('w-18:00', strtotime('tomorrow')) : date('w-19:00');
306 $this->assertEquals($expected, date('w-H:i', $next));
308 $timezone = 8.0; // 1am for the user.
309 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
310 $expected = !$dst ? date('w-13:00', strtotime('tomorrow')) : date('w-14:00', strtotime('tomorrow'));
311 $this->assertEquals($expected, date('w-H:i', $next));
313 $timezone = 9.0; // 2am for the user.
314 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
315 $expected = !$dst ? date('w-12:00', strtotime('tomorrow')) : date('w-13:00', strtotime('tomorrow'));
316 $this->assertEquals($expected, date('w-H:i', $next));
318 $timezone = 13.0; // 6am for the user.
319 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
320 $expected = !$dst ? date('w-08:00', strtotime('tomorrow')) : date('w-09:00', strtotime('tomorrow'));
321 $this->assertEquals($expected, date('w-H:i', $next));
323 // The big apple! (UTC-5 / UTC-4 DST).
324 date_default_timezone_set('America/New_York');
325 $now = strtotime('18:00:00');
328 $timezone = -10.0; // 1pm for the user.
329 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
330 $expected = !$dst ? date('w-01:00', strtotime('tomorrow')) : date('w-02:00', strtotime('tomorrow'));
331 $this->assertEquals($expected, date('w-H:i', $next));
333 $timezone = -5.0; // 6pm for the user (server time).
334 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
335 $expected = !$dst ? date('w-20:00') : date('w-21:00');
336 $this->assertEquals($expected, date('w-H:i', $next));
338 $timezone = 0.0; // 11pm for the user.
339 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
340 $expected = !$dst ? date('w-15:00', strtotime('tomorrow')) : date('w-16:00', strtotime('tomorrow'));
341 $this->assertEquals($expected, date('w-H:i', $next));
343 $timezone = 3.0; // 2am for the user.
344 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
345 $expected = !$dst ? date('w-12:00', strtotime('tomorrow')) : date('w-13:00', strtotime('tomorrow'));
346 $this->assertEquals($expected, date('w-H:i', $next));
348 $timezone = 8.0; // 7am for the user.
349 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
350 $expected = !$dst ? date('w-07:00', strtotime('tomorrow')) : date('w-08:00', strtotime('tomorrow'));
351 $this->assertEquals($expected, date('w-H:i', $next));
353 $timezone = 9.0; // 8am for the user.
354 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
355 $expected = !$dst ? date('w-06:00', strtotime('tomorrow')) : date('w-07:00', strtotime('tomorrow'));
356 $this->assertEquals($expected, date('w-H:i', $next));
358 $timezone = 13.0; // 6am for the user.
359 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
360 $expected = !$dst ? date('w-02:00', strtotime('tomorrow')) : date('w-03:00', strtotime('tomorrow'));
361 $this->assertEquals($expected, date('w-H:i', $next));
363 // Some more timezone tests
364 set_config('backup_auto_weekdays', '0100001', 'backup');
365 set_config('backup_auto_hour', '20', 'backup');
366 set_config('backup_auto_minute', '00', 'backup');
368 date_default_timezone_set('Europe/Brussels');
369 $now = strtotime('next Monday 18:00:00');
372 $timezone = -12.0; // 1pm for the user.
373 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
374 $expected = !$dst ? '2-09:00' : '2-10:00';
375 $this->assertEquals($expected, date('w-H:i', $next));
377 $timezone = -4.0; // 1pm for the user.
378 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
379 $expected = !$dst ? '2-01:00' : '2-02:00';
380 $this->assertEquals($expected, date('w-H:i', $next));
382 $timezone = 0.0; // 5pm for the user.
383 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
384 $expected = !$dst ? '1-21:00' : '1-22:00';
385 $this->assertEquals($expected, date('w-H:i', $next));
387 $timezone = 2.0; // 7pm for the user.
388 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
389 $expected = !$dst ? '1-19:00' : '1-20:00';
390 $this->assertEquals($expected, date('w-H:i', $next));
392 $timezone = 4.0; // 9pm for the user.
393 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
394 $expected = !$dst ? '6-17:00' : '6-18:00';
395 $this->assertEquals($expected, date('w-H:i', $next));
397 $timezone = 12.0; // 6am for the user.
398 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
399 $expected = !$dst ? '6-09:00' : '6-10:00';
400 $this->assertEquals($expected, date('w-H:i', $next));
402 // Some more timezone tests
403 set_config('backup_auto_weekdays', '0100001', 'backup');
404 set_config('backup_auto_hour', '02', 'backup');
405 set_config('backup_auto_minute', '00', 'backup');
407 date_default_timezone_set('America/New_York');
408 $now = strtotime('next Monday 04:00:00');
411 $timezone = -12.0; // 8pm for the user.
412 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
413 $expected = !$dst ? '1-09:00' : '1-10:00';
414 $this->assertEquals($expected, date('w-H:i', $next));
416 $timezone = -4.0; // 4am for the user.
417 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
418 $expected = !$dst ? '6-01:00' : '6-02:00';
419 $this->assertEquals($expected, date('w-H:i', $next));
421 $timezone = 0.0; // 8am for the user.
422 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
423 $expected = !$dst ? '5-21:00' : '5-22:00';
424 $this->assertEquals($expected, date('w-H:i', $next));
426 $timezone = 2.0; // 10am for the user.
427 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
428 $expected = !$dst ? '5-19:00' : '5-20:00';
429 $this->assertEquals($expected, date('w-H:i', $next));
431 $timezone = 4.0; // 12pm for the user.
432 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
433 $expected = !$dst ? '5-17:00' : '5-18:00';
434 $this->assertEquals($expected, date('w-H:i', $next));
436 $timezone = 12.0; // 8pm for the user.
437 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
438 $expected = !$dst ? '5-09:00' : '5-10:00';
439 $this->assertEquals($expected, date('w-H:i', $next));