320daacfbfa979889f7605682f2ec4cf13af3f0d
[moodle.git] / backup / util / helper / tests / cronhelper_test.php
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
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.
8 //
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.
13 //
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/>.
17 /**
18  * Unit tests for backups cron helper.
19  *
20  * @package   core_backup
21  * @category  phpunit
22  * @copyright 2012 Frédéric Massart <fred@moodle.com>
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
31 /**
32  * Unit tests for backup cron helper
33  */
34 class backup_cron_helper_testcase extends advanced_testcase {
35     /**
36      * Test {@link backup_cron_automated_helper::calculate_next_automated_backup}.
37      */
38     public function test_next_automated_backup() {
39         global $CFG;
41         $this->resetAfterTest();
42         set_config('backup_auto_active', '1', 'backup');
44         $this->setTimezone('Australia/Perth');
46         // Notes
47         // - backup_auto_weekdays starts on Sunday
48         // - Tests cannot be done in the past
49         // - Only the DST on the server side is handled.
51         // Every Tue and Fri at 11pm.
52         set_config('backup_auto_weekdays', '0010010', 'backup');
53         set_config('backup_auto_hour', '23', 'backup');
54         set_config('backup_auto_minute', '0', 'backup');
56         $timezone = 99; // Ignored, everything is calculated in server timezone!!!
58         $now = strtotime('next Monday 17: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 Tuesday 18:00:00');
63         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
64         $this->assertEquals('2-23:00', date('w-H:i', $next));
66         $now = strtotime('next Wednesday 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 Thursday 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 Friday 17:00:00');
75         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
76         $this->assertEquals('5-23:00', date('w-H:i', $next));
78         $now = strtotime('next Saturday 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         $now = strtotime('next Sunday 17:00:00');
83         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
84         $this->assertEquals('2-23:00', date('w-H:i', $next));
86         // Every Sun and Sat at 12pm.
87         set_config('backup_auto_weekdays', '1000001', 'backup');
88         set_config('backup_auto_hour', '0', 'backup');
89         set_config('backup_auto_minute', '0', 'backup');
91         $now = strtotime('next Monday 17:00:00');
92         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
93         $this->assertEquals('6-00:00', date('w-H:i', $next));
95         $now = strtotime('next Tuesday 17:00:00');
96         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
97         $this->assertEquals('6-00:00', date('w-H:i', $next));
99         $now = strtotime('next Wednesday 17:00:00');
100         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
101         $this->assertEquals('6-00:00', date('w-H:i', $next));
103         $now = strtotime('next Thursday 17:00:00');
104         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
105         $this->assertEquals('6-00:00', date('w-H:i', $next));
107         $now = strtotime('next Friday 17:00:00');
108         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
109         $this->assertEquals('6-00:00', date('w-H:i', $next));
111         $now = strtotime('next Saturday 17:00:00');
112         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
113         $this->assertEquals('0-00:00', date('w-H:i', $next));
115         $now = strtotime('next Sunday 17:00:00');
116         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
117         $this->assertEquals('6-00:00', date('w-H:i', $next));
119         // Every Sun at 4am.
120         set_config('backup_auto_weekdays', '1000000', 'backup');
121         set_config('backup_auto_hour', '4', 'backup');
122         set_config('backup_auto_minute', '0', 'backup');
124         $now = strtotime('next Monday 17:00:00');
125         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
126         $this->assertEquals('0-04:00', date('w-H:i', $next));
128         $now = strtotime('next Tuesday 17:00:00');
129         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
130         $this->assertEquals('0-04:00', date('w-H:i', $next));
132         $now = strtotime('next Wednesday 17:00:00');
133         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
134         $this->assertEquals('0-04:00', date('w-H:i', $next));
136         $now = strtotime('next Thursday 17:00:00');
137         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
138         $this->assertEquals('0-04:00', date('w-H:i', $next));
140         $now = strtotime('next Friday 17:00:00');
141         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
142         $this->assertEquals('0-04:00', date('w-H:i', $next));
144         $now = strtotime('next Saturday 17:00:00');
145         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
146         $this->assertEquals('0-04:00', date('w-H:i', $next));
148         $now = strtotime('next Sunday 17:00:00');
149         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
150         $this->assertEquals('0-04:00', date('w-H:i', $next));
152         // Every day but Wed at 8:30pm.
153         set_config('backup_auto_weekdays', '1110111', 'backup');
154         set_config('backup_auto_hour', '20', 'backup');
155         set_config('backup_auto_minute', '30', 'backup');
157         $now = strtotime('next Monday 17:00:00');
158         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
159         $this->assertEquals('1-20:30', date('w-H:i', $next));
161         $now = strtotime('next Tuesday 17:00:00');
162         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
163         $this->assertEquals('2-20:30', date('w-H:i', $next));
165         $now = strtotime('next Wednesday 17:00:00');
166         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
167         $this->assertEquals('4-20:30', date('w-H:i', $next));
169         $now = strtotime('next Thursday 17:00:00');
170         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
171         $this->assertEquals('4-20:30', date('w-H:i', $next));
173         $now = strtotime('next Friday 17:00:00');
174         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
175         $this->assertEquals('5-20:30', date('w-H:i', $next));
177         $now = strtotime('next Saturday 17:00:00');
178         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
179         $this->assertEquals('6-20:30', date('w-H:i', $next));
181         $now = strtotime('next Sunday 17:00:00');
182         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
183         $this->assertEquals('0-20:30', date('w-H:i', $next));
185         // Sun, Tue, Thu, Sat at 12pm.
186         set_config('backup_auto_weekdays', '1010101', 'backup');
187         set_config('backup_auto_hour', '0', 'backup');
188         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));
218         // None.
219         set_config('backup_auto_weekdays', '0000000', 'backup');
220         set_config('backup_auto_hour', '15', 'backup');
221         set_config('backup_auto_minute', '30', 'backup');
223         $now = strtotime('next Sunday 13:00:00');
224         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
225         $this->assertEquals('0', $next);
227         // Playing with timezones.
228         set_config('backup_auto_weekdays', '1111111', 'backup');
229         set_config('backup_auto_hour', '20', 'backup');
230         set_config('backup_auto_minute', '00', 'backup');
232         $this->setTimezone('Australia/Perth');
233         $now = strtotime('18:00:00');
234         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
235         $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
237         $this->setTimezone('Europe/Brussels');
238         $now = strtotime('18:00:00');
239         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
240         $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
242         $this->setTimezone('America/New_York');
243         $now = strtotime('18:00:00');
244         $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
245         $this->assertEquals(date('w-20:00'), date('w-H:i', $next));
246     }
248     /**
249      * Test {@link backup_cron_automated_helper::get_backups_to_delete}.
250      */
251     public function test_get_backups_to_delete() {
252         $this->resetAfterTest();
253         // Active only backup_auto_max_kept config to 2 days.
254         set_config('backup_auto_max_kept', '2', 'backup');
255         set_config('backup_auto_delete_days', '0', 'backup');
256         set_config('backup_auto_min_kept', '0', 'backup');
258         // No backups to delete.
259         $backupfiles = array(
260             '1000000000' => 'file1.mbz',
261             '1000432000' => 'file3.mbz'
262         );
263         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000432000);
264         $this->assertFalse($deletedbackups);
266         // Older backup to delete.
267         $backupfiles['1000172800'] = 'file2.mbz';
268         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000432000);
269         $this->assertEquals(1, count($deletedbackups));
270         $this->assertArrayHasKey('1000000000', $backupfiles);
271         $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
273         // Activate backup_auto_max_kept to 5 days and backup_auto_delete_days to 10 days.
274         set_config('backup_auto_max_kept', '5', 'backup');
275         set_config('backup_auto_delete_days', '10', 'backup');
276         set_config('backup_auto_min_kept', '0', 'backup');
278         // No backups to delete. Timestamp is 1000000000 + 10 days.
279         $backupfiles['1000432001'] = 'file4.mbz';
280         $backupfiles['1000864000'] = 'file5.mbz';
281         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000864000);
282         $this->assertFalse($deletedbackups);
284         // One old backup to delete. Timestamp is 1000000000 + 10 days + 1 second.
285         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1000864001);
286         $this->assertEquals(1, count($deletedbackups));
287         $this->assertArrayHasKey('1000000000', $backupfiles);
288         $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
290         // Two old backups to delete. Timestamp is 1000000000 + 12 days + 1 second.
291         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1001036801);
292         $this->assertEquals(2, count($deletedbackups));
293         $this->assertArrayHasKey('1000000000', $backupfiles);
294         $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
295         $this->assertArrayHasKey('1000172800', $backupfiles);
296         $this->assertEquals('file2.mbz', $backupfiles['1000172800']);
298         // Activate backup_auto_max_kept to 5 days, backup_auto_delete_days to 10 days and backup_auto_min_kept to 2.
299         set_config('backup_auto_max_kept', '5', 'backup');
300         set_config('backup_auto_delete_days', '10', 'backup');
301         set_config('backup_auto_min_kept', '2', 'backup');
303         // Three instead of four old backups are deleted. Timestamp is 1000000000 + 16 days.
304         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1001382400);
305         $this->assertEquals(3, count($deletedbackups));
306         $this->assertArrayHasKey('1000000000', $backupfiles);
307         $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
308         $this->assertArrayHasKey('1000172800', $backupfiles);
309         $this->assertEquals('file2.mbz', $backupfiles['1000172800']);
310         $this->assertArrayHasKey('1000432000', $backupfiles);
311         $this->assertEquals('file3.mbz', $backupfiles['1000432000']);
313         // Three instead of all five backups are deleted. Timestamp is 1000000000 + 60 days.
314         $deletedbackups = testable_backup_cron_automated_helper::testable_get_backups_to_delete($backupfiles, 1005184000);
315         $this->assertEquals(3, count($deletedbackups));
316         $this->assertArrayHasKey('1000000000', $backupfiles);
317         $this->assertEquals('file1.mbz', $backupfiles['1000000000']);
318         $this->assertArrayHasKey('1000172800', $backupfiles);
319         $this->assertEquals('file2.mbz', $backupfiles['1000172800']);
320         $this->assertArrayHasKey('1000432000', $backupfiles);
321         $this->assertEquals('file3.mbz', $backupfiles['1000432000']);
322     }
325 /**
326  * Provides access to protected methods we want to explicitly test
327  *
328  * @copyright 2015 Jean-Philippe Gaudreau <jp.gaudreau@umontreal.ca>
329  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
330  */
331 class testable_backup_cron_automated_helper extends backup_cron_automated_helper {
333     /**
334      * Provides access to protected method get_backups_to_remove.
335      *
336      * @param array $backupfiles Existing backup files
337      * @param int $now Starting time of the process
338      * @return array Backup files to remove
339      */
340     public static function testable_get_backups_to_delete($backupfiles, $now) {
341         return parent::get_backups_to_delete($backupfiles, $now);
342     }