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 // Warning: Some of these tests will fail if executed "around"
285 // 'Europe/Brussels' DST changes (last Sunday in March and
286 // last Sunday in October right now - 2012). Once Moodle
287 // moves to PHP TZ support this could be fixed properly.
288 date_default_timezone_set('Europe/Brussels');
289 $now = strtotime('18:00:00');
290 $dst = date('I', $now);
292 $timezone = -10.0; // 7am for the user.
293 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
294 $expected = !$dst ? date('w-07:00', strtotime('tomorrow')) : date('w-08:00', strtotime('tomorrow'));
295 $this->assertEquals($expected, date('w-H:i', $next));
297 $timezone = -5.0; // 12pm for the user.
298 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
299 $expected = !$dst ? date('w-02:00', strtotime('tomorrow')) : date('w-03:00', strtotime('tomorrow'));
300 $this->assertEquals($expected, date('w-H:i', $next));
302 $timezone = 0.0; // 5pm for the user.
303 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
304 $expected = !$dst ? date('w-21:00') : date('w-22:00');
305 $this->assertEquals($expected, date('w-H:i', $next));
307 $timezone = 3.0; // 8pm for the user (note the expected time is today while in DST).
308 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
309 $expected = !$dst ? date('w-18:00', strtotime('tomorrow')) : date('w-19:00');
310 $this->assertEquals($expected, date('w-H:i', $next));
312 $timezone = 8.0; // 1am for the user.
313 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
314 $expected = !$dst ? date('w-13:00', strtotime('tomorrow')) : date('w-14:00', strtotime('tomorrow'));
315 $this->assertEquals($expected, date('w-H:i', $next));
317 $timezone = 9.0; // 2am for the user.
318 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
319 $expected = !$dst ? date('w-12:00', strtotime('tomorrow')) : date('w-13:00', strtotime('tomorrow'));
320 $this->assertEquals($expected, date('w-H:i', $next));
322 $timezone = 13.0; // 6am for the user.
323 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
324 $expected = !$dst ? date('w-08:00', strtotime('tomorrow')) : date('w-09:00', strtotime('tomorrow'));
325 $this->assertEquals($expected, date('w-H:i', $next));
327 // The big apple! (UTC-5 / UTC-4 DST).
328 // Warning: Some of these tests will fail if executed "around"
329 // 'America/New_York' DST changes (2nd Sunday in March and
330 // 1st Sunday in November right now - 2012). Once Moodle
331 // moves to PHP TZ support this could be fixed properly.
332 date_default_timezone_set('America/New_York');
333 $now = strtotime('18:00:00');
334 $dst = date('I', $now);
336 $timezone = -10.0; // 1pm for the user.
337 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
338 $expected = !$dst ? date('w-01:00', strtotime('tomorrow')) : date('w-02:00', strtotime('tomorrow'));
339 $this->assertEquals($expected, date('w-H:i', $next));
341 $timezone = -5.0; // 6pm for the user (server time).
342 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
343 $expected = !$dst ? date('w-20:00') : date('w-21:00');
344 $this->assertEquals($expected, date('w-H:i', $next));
346 $timezone = 0.0; // 11pm for the user.
347 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
348 $expected = !$dst ? date('w-15:00', strtotime('tomorrow')) : date('w-16:00', strtotime('tomorrow'));
349 $this->assertEquals($expected, date('w-H:i', $next));
351 $timezone = 3.0; // 2am for the user.
352 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
353 $expected = !$dst ? date('w-12:00', strtotime('tomorrow')) : date('w-13:00', strtotime('tomorrow'));
354 $this->assertEquals($expected, date('w-H:i', $next));
356 $timezone = 8.0; // 7am for the user.
357 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
358 $expected = !$dst ? date('w-07:00', strtotime('tomorrow')) : date('w-08:00', strtotime('tomorrow'));
359 $this->assertEquals($expected, date('w-H:i', $next));
361 $timezone = 9.0; // 8am for the user.
362 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
363 $expected = !$dst ? date('w-06:00', strtotime('tomorrow')) : date('w-07:00', strtotime('tomorrow'));
364 $this->assertEquals($expected, date('w-H:i', $next));
366 $timezone = 13.0; // 6am for the user.
367 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
368 $expected = !$dst ? date('w-02:00', strtotime('tomorrow')) : date('w-03:00', strtotime('tomorrow'));
369 $this->assertEquals($expected, date('w-H:i', $next));
371 // Some more timezone tests
372 set_config('backup_auto_weekdays', '0100001', 'backup');
373 set_config('backup_auto_hour', '20', 'backup');
374 set_config('backup_auto_minute', '00', 'backup');
376 // Note: These tests should not fail because they are "unnafected"
377 // by DST changes, as far as execution always happens on Monday and
378 // Saturday and those week days are not, right now, the ones rulez
379 // to peform the DST changes (Sunday is). This may change if rules
380 // are modified in the future.
381 date_default_timezone_set('Europe/Brussels');
382 $now = strtotime('next Monday 18:00:00');
383 $dst = date('I', $now);
385 $timezone = -12.0; // 1pm for the user.
386 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
387 $expected = !$dst ? '2-09:00' : '2-10:00';
388 $this->assertEquals($expected, date('w-H:i', $next));
390 $timezone = -4.0; // 1pm for the user.
391 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
392 $expected = !$dst ? '2-01:00' : '2-02:00';
393 $this->assertEquals($expected, date('w-H:i', $next));
395 $timezone = 0.0; // 5pm for the user.
396 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
397 $expected = !$dst ? '1-21:00' : '1-22:00';
398 $this->assertEquals($expected, date('w-H:i', $next));
400 $timezone = 2.0; // 7pm for the user.
401 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
402 $expected = !$dst ? '1-19:00' : '1-20:00';
403 $this->assertEquals($expected, date('w-H:i', $next));
405 $timezone = 4.0; // 9pm for the user.
406 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
407 $expected = !$dst ? '6-17:00' : '6-18:00';
408 $this->assertEquals($expected, date('w-H:i', $next));
410 $timezone = 12.0; // 6am for the user.
411 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
412 $expected = !$dst ? '6-09:00' : '6-10:00';
413 $this->assertEquals($expected, date('w-H:i', $next));
415 // Some more timezone tests
416 set_config('backup_auto_weekdays', '0100001', 'backup');
417 set_config('backup_auto_hour', '02', 'backup');
418 set_config('backup_auto_minute', '00', 'backup');
420 // Note: These tests should not fail because they are "unnafected"
421 // by DST changes, as far as execution always happens on Monday and
422 // Saturday and those week days are not, right now, the ones rulez
423 // to peform the DST changes (Sunday is). This may change if rules
424 // are modified in the future.
425 date_default_timezone_set('America/New_York');
426 $now = strtotime('next Monday 04:00:00');
427 $dst = date('I', $now);
429 $timezone = -12.0; // 8pm for the user.
430 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
431 $expected = !$dst ? '1-09:00' : '1-10:00';
432 $this->assertEquals($expected, date('w-H:i', $next));
434 $timezone = -4.0; // 4am for the user.
435 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
436 $expected = !$dst ? '6-01:00' : '6-02:00';
437 $this->assertEquals($expected, date('w-H:i', $next));
439 $timezone = 0.0; // 8am for the user.
440 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
441 $expected = !$dst ? '5-21:00' : '5-22:00';
442 $this->assertEquals($expected, date('w-H:i', $next));
444 $timezone = 2.0; // 10am for the user.
445 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
446 $expected = !$dst ? '5-19:00' : '5-20:00';
447 $this->assertEquals($expected, date('w-H:i', $next));
449 $timezone = 4.0; // 12pm for the user.
450 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
451 $expected = !$dst ? '5-17:00' : '5-18:00';
452 $this->assertEquals($expected, date('w-H:i', $next));
454 $timezone = 12.0; // 8pm for the user.
455 $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
456 $expected = !$dst ? '5-09:00' : '5-10:00';
457 $this->assertEquals($expected, date('w-H:i', $next));