From 68576ef61d23d622a3fa79bc3efcda05dc2f399a Mon Sep 17 00:00:00 2001 From: Frederic Massart Date: Thu, 12 Jul 2012 09:39:52 +0800 Subject: [PATCH] MDL-28531 Backup: Automated backups run when they are scheduled --- .../util/helper/backup_cron_helper.class.php | 70 ++-- lib/moodlelib.php | 4 +- lib/tests/backup_test.php | 336 +++++++++++++++--- 3 files changed, 344 insertions(+), 66 deletions(-) diff --git a/backup/util/helper/backup_cron_helper.class.php b/backup/util/helper/backup_cron_helper.class.php index 9715a5d06ada4..e88805b1d5e24 100644 --- a/backup/util/helper/backup_cron_helper.class.php +++ b/backup/util/helper/backup_cron_helper.class.php @@ -110,7 +110,7 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now); $showtime = "undefined"; if ($nextstarttime > 0) { - $showtime = userdate($nextstarttime,"",$admin->timezone); + $showtime = date('r', $nextstarttime); } $rs = $DB->get_recordset('course'); @@ -124,7 +124,14 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL } // Skip courses that do not yet need backup - $skipped = !(($backupcourse->nextstarttime >= 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATELY); + $skipped = !(($backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATELY); + if ($skipped && $backupcourse->nextstarttime != $nextstarttime) { + $backupcourse->nextstarttime = $nextstarttime; + $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED; + $DB->update_record('backup_courses', $backupcourse); + mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime); + } + // Skip backup of unavailable courses that have remained unmodified in a month if (!$skipped && empty($course->visible) && ($now - $course->timemodified) > 31*24*60*60) { //Hidden + settings were unmodified last month //Check log if there were any modifications to the course content @@ -139,9 +146,10 @@ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDUL $skipped = true; } } + //Now we backup every non-skipped course if (!$skipped) { - mtrace('Backing up '.$course->fullname, '...'); + mtrace('Backing up '.$course->fullname.'...'); //We have to send a email because we have included at least one backup $emailpending = true; @@ -270,38 +278,47 @@ public static function get_backup_status_array() { /** * Works out the next time the automated backup should be run. * - * @param mixed $timezone - * @param int $now - * @return int + * @param mixed $timezone user timezone + * @param int $now timestamp, should not be in the past, most likely time() + * @return int timestamp of the next execution at server time */ public static function calculate_next_automated_backup($timezone, $now) { - $result = -1; + $result = 0; $config = get_config('backup'); - $midnight = usergetmidnight($now, $timezone); + $autohour = $config->backup_auto_hour; + $automin = $config->backup_auto_minute; + + // Gets the user time relatively to the server time. $date = usergetdate($now, $timezone); + $usertime = mktime($date['hours'], $date['minutes'], $date['seconds'], $date['mon'], $date['mday'], $date['year']); + $diff = $now - $usertime; - // Get number of days (from today) to execute backups + // Get number of days (from user's today) to execute backups. $automateddays = substr($config->backup_auto_weekdays, $date['wday']) . $config->backup_auto_weekdays; - $daysfromtoday = strpos($automateddays, "1", 1); + $daysfromnow = strpos($automateddays, "1"); - // If we can't find the next day, we set it to tomorrow - if (empty($daysfromtoday)) { - $daysfromtoday = 1; + // Error, there are no days to schedule the backup for. + if ($daysfromnow === false) { + return 0; } - // If some day has been found - if ($daysfromtoday !== false) { - // Calculate distance - $dist = ($daysfromtoday * 86400) + // Days distance - ($config->backup_auto_hour * 3600) + // Hours distance - ($config->backup_auto_minute * 60); // Minutes distance - $result = $midnight + $dist; + // Checks if the date would happen in the future (of the user). + $userresult = mktime($autohour, $automin, 0, $date['mon'], $date['mday'] + $daysfromnow, $date['year']); + if ($userresult <= $usertime) { + // If not, we skip the first scheduled day, that should fix it. + $daysfromnow = strpos($automateddays, "1", 1); + $userresult = mktime($autohour, $automin, 0, $date['mon'], $date['mday'] + $daysfromnow, $date['year']); } - // If that time is past, call the function recursively to obtain the next valid day - if ($result > 0 && $result < time()) { - $result = self::calculate_next_automated_backup($timezone, $result); + // Now we generate the time relative to the server. + $result = $userresult + $diff; + + // If that time is past, call the function recursively to obtain the next valid day. + if ($result <= $now) { + // Checking time() in here works, but makes PHPUnit Tests extremely hard to predict. + // $now should never be earlier than time() anyway... + $result = self::calculate_next_automated_backup($timezone, $now + DAYSECS); } return $result; @@ -411,7 +428,12 @@ public static function get_automated_backup_state($rundirective = self::RUN_ON_S $config = get_config('backup'); $active = (int)$config->backup_auto_active; - if ($active === self::AUTO_BACKUP_DISABLED || ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL)) { + $weekdays = (string)$config->backup_auto_weekdays; + + // In case of automated backup also check that it is scheduled for at least one weekday. + if ($active === self::AUTO_BACKUP_DISABLED || + ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL) || + ($rundirective == self::RUN_ON_SCHEDULE && strpos($weekdays, '1') === false)) { return self::STATE_DISABLED; } else if (!empty($config->backup_auto_running)) { // Detect if the backup_auto_running semaphore is a valid one diff --git a/lib/moodlelib.php b/lib/moodlelib.php index f229b6b3ef246..096d4675b6812 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2324,10 +2324,10 @@ function get_user_timezone($tz = 99) { $tz = 99; - while(($tz == '' || $tz == 99 || $tz == NULL) && $next = each($timezones)) { + // Loop while $tz is, empty but not zero, or 99, and there is another timezone is the array + while(((empty($tz) && !is_numeric($tz)) || $tz == 99) && $next = each($timezones)) { $tz = $next['value']; } - return is_numeric($tz) ? (float) $tz : $tz; } diff --git a/lib/tests/backup_test.php b/lib/tests/backup_test.php index 1830f56dce0ba..5b44435310f41 100644 --- a/lib/tests/backup_test.php +++ b/lib/tests/backup_test.php @@ -36,149 +36,405 @@ class backup_testcase extends advanced_testcase { public function test_next_automated_backup() { $this->resetAfterTest(); - $admin = get_admin(); - $timezone = $admin->timezone; + set_config('backup_auto_active', '1', 'backup'); // Notes - // - The next automated backup will never be on the same date than $now // - backup_auto_weekdays starts on Sunday - // - Tests cannot be done in the past. + // - Tests cannot be done in the past + // - Only the DST on the server side is handled. - // Every Wed and Sat at 11pm. - set_config('backup_auto_active', '1', 'backup'); + // Every Tue and Fri at 11pm. set_config('backup_auto_weekdays', '0010010', 'backup'); set_config('backup_auto_hour', '23', 'backup'); set_config('backup_auto_minute', '0', 'backup'); + $timezone = 99; - $now = strtotime('next Monday'); + $now = strtotime('next Monday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('2-23:00', date('w-H:i', $next)); - $now = strtotime('next Tuesday'); + $now = strtotime('next Tuesday 18:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); - $this->assertEquals('5-23:00', date('w-H:i', $next)); + $this->assertEquals('2-23:00', date('w-H:i', $next)); - $now = strtotime('next Wednesday'); + $now = strtotime('next Wednesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('5-23:00', date('w-H:i', $next)); - $now = strtotime('next Thursday'); + $now = strtotime('next Thursday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('5-23:00', date('w-H:i', $next)); - $now = strtotime('next Friday'); + $now = strtotime('next Friday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); - $this->assertEquals('2-23:00', date('w-H:i', $next)); + $this->assertEquals('5-23:00', date('w-H:i', $next)); - $now = strtotime('next Saturday'); + $now = strtotime('next Saturday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('2-23:00', date('w-H:i', $next)); - $now = strtotime('next Sunday'); + $now = strtotime('next Sunday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('2-23:00', date('w-H:i', $next)); // Every Sun and Sat at 12pm. - set_config('backup_auto_active', '1', 'backup'); set_config('backup_auto_weekdays', '1000001', 'backup'); set_config('backup_auto_hour', '0', 'backup'); set_config('backup_auto_minute', '0', 'backup'); + $timezone = 99; - $now = strtotime('next Monday'); + $now = strtotime('next Monday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-00:00', date('w-H:i', $next)); - $now = strtotime('next Tuesday'); + $now = strtotime('next Tuesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-00:00', date('w-H:i', $next)); - $now = strtotime('next Wednesday'); + $now = strtotime('next Wednesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-00:00', date('w-H:i', $next)); - $now = strtotime('next Thursday'); + $now = strtotime('next Thursday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-00:00', date('w-H:i', $next)); - $now = strtotime('next Friday'); + $now = strtotime('next Friday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-00:00', date('w-H:i', $next)); - $now = strtotime('next Saturday'); + $now = strtotime('next Saturday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-00:00', date('w-H:i', $next)); - $now = strtotime('next Sunday'); + $now = strtotime('next Sunday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-00:00', date('w-H:i', $next)); // Every Sun at 4am. - set_config('backup_auto_active', '1', 'backup'); set_config('backup_auto_weekdays', '1000000', 'backup'); set_config('backup_auto_hour', '4', 'backup'); set_config('backup_auto_minute', '0', 'backup'); + $timezone = 99; - $now = strtotime('next Monday'); + $now = strtotime('next Monday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); - $now = strtotime('next Tuesday'); + $now = strtotime('next Tuesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); - $now = strtotime('next Wednesday'); + $now = strtotime('next Wednesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); - $now = strtotime('next Thursday'); + $now = strtotime('next Thursday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); - $now = strtotime('next Friday'); + $now = strtotime('next Friday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); - $now = strtotime('next Saturday'); + $now = strtotime('next Saturday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); - $now = strtotime('next Sunday'); + $now = strtotime('next Sunday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-04:00', date('w-H:i', $next)); // Every day but Wed at 8:30pm. - set_config('backup_auto_active', '1', 'backup'); set_config('backup_auto_weekdays', '1110111', 'backup'); set_config('backup_auto_hour', '20', 'backup'); set_config('backup_auto_minute', '30', 'backup'); + $timezone = 99; + + $now = strtotime('next Monday 17:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('1-20:30', date('w-H:i', $next)); - $now = strtotime('next Monday'); + $now = strtotime('next Tuesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('2-20:30', date('w-H:i', $next)); - $now = strtotime('next Tuesday'); + $now = strtotime('next Wednesday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('4-20:30', date('w-H:i', $next)); - $now = strtotime('next Wednesday'); + $now = strtotime('next Thursday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('4-20:30', date('w-H:i', $next)); - $now = strtotime('next Thursday'); + $now = strtotime('next Friday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('5-20:30', date('w-H:i', $next)); - $now = strtotime('next Friday'); + $now = strtotime('next Saturday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-20:30', date('w-H:i', $next)); - $now = strtotime('next Saturday'); + $now = strtotime('next Sunday 17:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-20:30', date('w-H:i', $next)); - $now = strtotime('next Sunday'); + // Sun, Tue, Thu, Sat at 12pm. + set_config('backup_auto_weekdays', '1010101', 'backup'); + set_config('backup_auto_hour', '0', 'backup'); + set_config('backup_auto_minute', '0', 'backup'); + $timezone = 99; + + $now = strtotime('next Monday 13:00:00'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); - $this->assertEquals('1-20:30', date('w-H:i', $next)); + $this->assertEquals('2-00:00', date('w-H:i', $next)); + + $now = strtotime('next Tuesday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('4-00:00', date('w-H:i', $next)); + + $now = strtotime('next Wednesday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('4-00:00', date('w-H:i', $next)); + + $now = strtotime('next Thursday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('6-00:00', date('w-H:i', $next)); + + $now = strtotime('next Friday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('6-00:00', date('w-H:i', $next)); + + $now = strtotime('next Saturday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('0-00:00', date('w-H:i', $next)); + + $now = strtotime('next Sunday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('2-00:00', date('w-H:i', $next)); + + // None. + set_config('backup_auto_weekdays', '0000000', 'backup'); + set_config('backup_auto_hour', '15', 'backup'); + set_config('backup_auto_minute', '30', 'backup'); + $timezone = 99; + + $now = strtotime('next Sunday 13:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('0', $next); + + // Playing with timezones. + set_config('backup_auto_weekdays', '1111111', 'backup'); + set_config('backup_auto_hour', '20', 'backup'); + set_config('backup_auto_minute', '00', 'backup'); + + $timezone = 99; + date_default_timezone_set('Australia/Perth'); + $now = strtotime('18:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-20:00'), date('w-H:i', $next)); + + $timezone = 99; + date_default_timezone_set('Europe/Brussels'); + $now = strtotime('18:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-20:00'), date('w-H:i', $next)); + + $timezone = 99; + date_default_timezone_set('America/New_York'); + $now = strtotime('18:00:00'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-20:00'), date('w-H:i', $next)); + + // Viva Australia! (UTC+8). + date_default_timezone_set('Australia/Perth'); + $now = strtotime('18:00:00'); + + $timezone = -10.0; // 12am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-14:00', strtotime('tomorrow')), date('w-H:i', $next)); + + $timezone = -5.0; // 5am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-09:00', strtotime('tomorrow')), date('w-H:i', $next)); + + $timezone = 0.0; // 10am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-04:00', strtotime('tomorrow')), date('w-H:i', $next)); + + $timezone = 3.0; // 1pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-01:00', strtotime('tomorrow')), date('w-H:i', $next)); + + $timezone = 8.0; // 6pm for the user (same than the server). + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-20:00'), date('w-H:i', $next)); + + $timezone = 9.0; // 7pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-19:00'), date('w-H:i', $next)); + + $timezone = 13.0; // 12am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals(date('w-15:00', strtotime('tomorrow')), date('w-H:i', $next)); + + // Let's have a Belgian beer! (UTC+1 / UTC+2 DST). + date_default_timezone_set('Europe/Brussels'); + $now = strtotime('18:00:00'); + $dst = date('I'); + + $timezone = -10.0; // 7am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-07:00', strtotime('tomorrow')) : date('w-08:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = -5.0; // 12pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-02:00', strtotime('tomorrow')) : date('w-03:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 0.0; // 5pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-21:00') : date('w-22:00'); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 3.0; // 8pm for the user (note the expected time is today while in DST). + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-18:00', strtotime('tomorrow')) : date('w-19:00'); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 8.0; // 1am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-13:00', strtotime('tomorrow')) : date('w-14:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 9.0; // 2am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-12:00', strtotime('tomorrow')) : date('w-13:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 13.0; // 6am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-08:00', strtotime('tomorrow')) : date('w-09:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + // The big apple! (UTC-5 / UTC-4 DST). + date_default_timezone_set('America/New_York'); + $now = strtotime('18:00:00'); + $dst = date('I'); + + $timezone = -10.0; // 1pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-01:00', strtotime('tomorrow')) : date('w-02:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = -5.0; // 6pm for the user (server time). + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-20:00') : date('w-21:00'); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 0.0; // 11pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-15:00', strtotime('tomorrow')) : date('w-16:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 3.0; // 2am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-12:00', strtotime('tomorrow')) : date('w-13:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 8.0; // 7am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-07:00', strtotime('tomorrow')) : date('w-08:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 9.0; // 8am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-06:00', strtotime('tomorrow')) : date('w-07:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 13.0; // 6am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? date('w-02:00', strtotime('tomorrow')) : date('w-03:00', strtotime('tomorrow')); + $this->assertEquals($expected, date('w-H:i', $next)); + + // Some more timezone tests + set_config('backup_auto_weekdays', '0100001', 'backup'); + set_config('backup_auto_hour', '20', 'backup'); + set_config('backup_auto_minute', '00', 'backup'); + + date_default_timezone_set('Europe/Brussels'); + $now = strtotime('next Monday 18:00:00'); + $dst = date('I'); + + $timezone = -12.0; // 1pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '2-09:00' : '2-10:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = -4.0; // 1pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '2-01:00' : '2-02:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 0.0; // 5pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '1-21:00' : '1-22:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 2.0; // 7pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '1-19:00' : '1-20:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 4.0; // 9pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '6-17:00' : '6-18:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 12.0; // 6am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '6-09:00' : '6-10:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + // Some more timezone tests + set_config('backup_auto_weekdays', '0100001', 'backup'); + set_config('backup_auto_hour', '02', 'backup'); + set_config('backup_auto_minute', '00', 'backup'); + + date_default_timezone_set('America/New_York'); + $now = strtotime('next Monday 04:00:00'); + $dst = date('I'); + + $timezone = -12.0; // 8pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '1-09:00' : '1-10:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = -4.0; // 4am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '6-01:00' : '6-02:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 0.0; // 8am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '5-21:00' : '5-22:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 2.0; // 10am for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '5-19:00' : '5-20:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 4.0; // 12pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '5-17:00' : '5-18:00'; + $this->assertEquals($expected, date('w-H:i', $next)); + + $timezone = 12.0; // 8pm for the user. + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $expected = !$dst ? '5-09:00' : '5-10:00'; + $this->assertEquals($expected, date('w-H:i', $next)); } }