diff --git a/backup/util/helper/backup_cron_helper.class.php b/backup/util/helper/backup_cron_helper.class.php index 353d451ee91..00db13dc390 100644 --- a/backup/util/helper/backup_cron_helper.class.php +++ b/backup/util/helper/backup_cron_helper.class.php @@ -110,7 +110,7 @@ abstract class backup_cron_automated_helper { $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now); $showtime = "undefined"; if ($nextstarttime > 0) { - $showtime = date('r', $nextstarttime); + $showtime = userdate($nextstarttime,"",$admin->timezone); } $rs = $DB->get_recordset('course'); @@ -124,14 +124,7 @@ abstract class backup_cron_automated_helper { } // Skip courses that do not yet need backup - $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); - } - + $skipped = !(($backupcourse->nextstarttime >= 0 && $backupcourse->nextstarttime < $now) || $rundirective == self::RUN_IMMEDIATELY); // 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 @@ -146,10 +139,9 @@ abstract class backup_cron_automated_helper { $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; @@ -278,47 +270,38 @@ abstract class backup_cron_automated_helper { /** * Works out the next time the automated backup should be run. * - * @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 + * @param mixed $timezone + * @param int $now + * @return int */ public static function calculate_next_automated_backup($timezone, $now) { - $result = 0; + $result = -1; $config = get_config('backup'); - $autohour = $config->backup_auto_hour; - $automin = $config->backup_auto_minute; - - // Gets the user time relatively to the server time. + $midnight = usergetmidnight($now, $timezone); $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 user's today) to execute backups. + // Get number of days (from today) to execute backups $automateddays = substr($config->backup_auto_weekdays, $date['wday']) . $config->backup_auto_weekdays; - $daysfromnow = strpos($automateddays, "1"); + $daysfromtoday = strpos($automateddays, "1", 1); - // Error, there are no days to schedule the backup for. - if ($daysfromnow === false) { - return 0; + // If we can't find the next day, we set it to tomorrow + if (empty($daysfromtoday)) { + $daysfromtoday = 1; } - // 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 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; } - // 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); + // 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); } return $result; @@ -428,12 +411,7 @@ abstract class backup_cron_automated_helper { $config = get_config('backup'); $active = (int)$config->backup_auto_active; - $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)) { + if ($active === self::AUTO_BACKUP_DISABLED || ($rundirective == self::RUN_ON_SCHEDULE && $active === self::AUTO_BACKUP_MANUAL)) { 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 5cf0b7ead70..6591795fcca 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -2324,10 +2324,10 @@ function get_user_timezone($tz = 99) { $tz = 99; - // 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)) { + while(($tz == '' || $tz == 99 || $tz == NULL) && $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 5b44435310f..1830f56dce0 100644 --- a/lib/tests/backup_test.php +++ b/lib/tests/backup_test.php @@ -36,405 +36,149 @@ class backup_testcase extends advanced_testcase { public function test_next_automated_backup() { $this->resetAfterTest(); - set_config('backup_auto_active', '1', 'backup'); + $admin = get_admin(); + $timezone = $admin->timezone; // 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 - // - Only the DST on the server side is handled. + // - Tests cannot be done in the past. - // Every Tue and Fri at 11pm. + // Every Wed and Sat at 11pm. + set_config('backup_auto_active', '1', 'backup'); 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 17:00:00'); + $now = strtotime('next Monday'); $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 18:00:00'); + $now = strtotime('next Tuesday'); + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); + $this->assertEquals('5-23:00', date('w-H:i', $next)); + + $now = strtotime('next Wednesday'); + $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'); + $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'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('2-23:00', date('w-H:i', $next)); - $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 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 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 Saturday 17:00:00'); + $now = strtotime('next Saturday'); $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 17:00:00'); + $now = strtotime('next Sunday'); $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 17:00:00'); + $now = strtotime('next Monday'); $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 17:00:00'); + $now = strtotime('next Tuesday'); $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 17:00:00'); + $now = strtotime('next Wednesday'); $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 17:00:00'); + $now = strtotime('next Thursday'); $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 17:00:00'); + $now = strtotime('next Friday'); $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 17:00:00'); + $now = strtotime('next Saturday'); $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 17:00:00'); + $now = strtotime('next Sunday'); $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 17:00:00'); + $now = strtotime('next Monday'); $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 17:00:00'); + $now = strtotime('next Tuesday'); $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 17:00:00'); + $now = strtotime('next Wednesday'); $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 17:00:00'); + $now = strtotime('next Thursday'); $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 17:00:00'); + $now = strtotime('next Friday'); $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 17:00:00'); + $now = strtotime('next Saturday'); $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 17:00:00'); + $now = strtotime('next Sunday'); $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 Tuesday 17:00:00'); + $now = strtotime('next Monday'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('2-20:30', date('w-H:i', $next)); - $now = strtotime('next Wednesday 17:00:00'); + $now = strtotime('next Tuesday'); $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 17:00:00'); + $now = strtotime('next Wednesday'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('4-20:30', date('w-H:i', $next)); - $now = strtotime('next Friday 17:00:00'); + $now = strtotime('next Thursday'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('5-20:30', date('w-H:i', $next)); - $now = strtotime('next Saturday 17:00:00'); + $now = strtotime('next Friday'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('6-20:30', date('w-H:i', $next)); - $now = strtotime('next Sunday 17:00:00'); + $now = strtotime('next Saturday'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('0-20:30', date('w-H:i', $next)); - // 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'); + $now = strtotime('next Sunday'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); - $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)); + $this->assertEquals('1-20:30', date('w-H:i', $next)); } }