/** * Runs the automated backups if required * * @global moodle_database $DB */ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) { global $CFG, $DB; $status = true; $emailpending = false; $now = time(); $config = get_config('backup'); mtrace("Checking automated backup status", '...'); $state = backup_cron_automated_helper::get_automated_backup_state($rundirective); if ($state === backup_cron_automated_helper::STATE_DISABLED) { mtrace('INACTIVE'); return $state; } else { if ($state === backup_cron_automated_helper::STATE_RUNNING) { mtrace('RUNNING'); if ($rundirective == self::RUN_IMMEDIATELY) { mtrace('Automated backups are already running. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.'); } else { mtrace("automated backup are already running. Execution delayed"); } return $state; } else { mtrace('OK'); } } backup_cron_automated_helper::set_state_running(); mtrace("Getting admin info"); $admin = get_admin(); if (!$admin) { mtrace("Error: No admin account was found"); $state = false; } if ($status) { mtrace("Checking courses"); mtrace("Skipping deleted courses", '...'); mtrace(sprintf("%d courses", backup_cron_automated_helper::remove_deleted_courses_from_schedule())); } if ($status) { mtrace('Running required automated backups...'); cron_trace_time_and_memory(); // This could take a while! core_php_time_limit::raise(); raise_memory_limit(MEMORY_EXTRA); $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now); $showtime = "undefined"; if ($nextstarttime > 0) { $showtime = date('r', $nextstarttime); } $rs = $DB->get_recordset('course'); foreach ($rs as $course) { $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); if (!$backupcourse) { $backupcourse = new stdClass(); $backupcourse->courseid = $course->id; $backupcourse->laststatus = self::BACKUP_STATUS_NOTYETRUN; $DB->insert_record('backup_courses', $backupcourse); $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); } // The last backup is considered as successful when OK or SKIPPED. $lastbackupwassuccessful = ($backupcourse->laststatus == self::BACKUP_STATUS_SKIPPED || $backupcourse->laststatus == self::BACKUP_STATUS_OK) && ($backupcourse->laststarttime > 0 && $backupcourse->lastendtime > 0); // Assume that we are not skipping anything. $skipped = false; $skippedmessage = ''; // Check if we are going to be running the backup now. $shouldrunnow = $backupcourse->nextstarttime > 0 && $backupcourse->nextstarttime < $now || $rundirective == self::RUN_IMMEDIATELY; // If config backup_auto_skip_hidden is set to true, skip courses that are not visible. if ($shouldrunnow && $config->backup_auto_skip_hidden) { $skipped = $config->backup_auto_skip_hidden && !$course->visible; $skippedmessage = 'Not visible'; } // If config backup_auto_skip_modif_days is set to true, skip courses // that have not been modified since the number of days defined. if ($shouldrunnow && !$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_days) { $timenotmodifsincedays = $now - $config->backup_auto_skip_modif_days * DAYSECS; // Check log if there were any modifications to the course content. $logexists = self::is_course_modified($course->id, $timenotmodifsincedays); $skipped = $course->timemodified <= $timenotmodifsincedays && !$logexists; $skippedmessage = 'Not modified in the past ' . $config->backup_auto_skip_modif_days . ' days'; } // If config backup_auto_skip_modif_prev is set to true, skip courses // that have not been modified since previous backup. if ($shouldrunnow && !$skipped && $lastbackupwassuccessful && $config->backup_auto_skip_modif_prev) { // Check log if there were any modifications to the course content. $logexists = self::is_course_modified($course->id, $backupcourse->laststarttime); $skipped = $course->timemodified <= $backupcourse->laststarttime && !$logexists; $skippedmessage = 'Not modified since previous backup'; } // Check if the course is not scheduled to run right now. if (!$shouldrunnow) { $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); mtrace('Skipping ' . $course->fullname . ' (Not scheduled for backup until ' . $showtime . ')'); } else { if ($skipped) { // Must have been skipped for a reason. $backupcourse->laststatus = self::BACKUP_STATUS_SKIPPED; $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); mtrace('Skipping ' . $course->fullname . ' (' . $skippedmessage . ')'); mtrace('Backup of \'' . $course->fullname . '\' is scheduled on ' . $showtime); } else { // Backup every non-skipped courses. mtrace('Backing up ' . $course->fullname . '...'); // We have to send an email because we have included at least one backup. $emailpending = true; // Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error). if ($backupcourse->laststatus != self::BACKUP_STATUS_UNFINISHED) { // Set laststarttime. $starttime = time(); $backupcourse->laststarttime = time(); $backupcourse->laststatus = self::BACKUP_STATUS_UNFINISHED; $DB->update_record('backup_courses', $backupcourse); $backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id); $backupcourse->lastendtime = time(); $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); if ($backupcourse->laststatus === self::BACKUP_STATUS_OK) { // Clean up any excess course backups now that we have // taken a successful backup. $removedcount = backup_cron_automated_helper::remove_excess_backups($course); } } mtrace("complete - next execution: {$showtime}"); } } } $rs->close(); } //Send email to admin if necessary if ($emailpending) { mtrace("Sending email to admin"); $message = ""; $count = backup_cron_automated_helper::get_backup_status_array(); $haserrors = $count[self::BACKUP_STATUS_ERROR] != 0 || $count[self::BACKUP_STATUS_UNFINISHED] != 0; // Build the message text. // Summary. $message .= get_string('summary') . "\n"; $message .= "==================================================\n"; $message .= ' ' . get_string('courses') . '; ' . array_sum($count) . "\n"; $message .= ' ' . get_string('ok') . '; ' . $count[self::BACKUP_STATUS_OK] . "\n"; $message .= ' ' . get_string('skipped') . '; ' . $count[self::BACKUP_STATUS_SKIPPED] . "\n"; $message .= ' ' . get_string('error') . '; ' . $count[self::BACKUP_STATUS_ERROR] . "\n"; $message .= ' ' . get_string('unfinished') . '; ' . $count[self::BACKUP_STATUS_UNFINISHED] . "\n"; $message .= ' ' . get_string('warning') . '; ' . $count[self::BACKUP_STATUS_WARNING] . "\n"; $message .= ' ' . get_string('backupnotyetrun') . '; ' . $count[self::BACKUP_STATUS_NOTYETRUN] . "\n\n"; //Reference if ($haserrors) { $message .= " " . get_string('backupfailed') . "\n\n"; $dest_url = "{$CFG->wwwroot}/report/backups/index.php"; $message .= " " . get_string('backuptakealook', '', $dest_url) . "\n\n"; //Set message priority $admin->priority = 1; //Reset unfinished to error $DB->set_field('backup_courses', 'laststatus', '0', array('laststatus' => '2')); } else { $message .= " " . get_string('backupfinished') . "\n"; } //Build the message subject $site = get_site(); $prefix = format_string($site->shortname, true, array('context' => context_course::instance(SITEID))) . ": "; if ($haserrors) { $prefix .= "[" . strtoupper(get_string('error')) . "] "; } $subject = $prefix . get_string('automatedbackupstatus', 'backup'); //Send the message $eventdata = new stdClass(); $eventdata->modulename = 'moodle'; $eventdata->userfrom = $admin; $eventdata->userto = $admin; $eventdata->subject = $subject; $eventdata->fullmessage = $message; $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; $eventdata->component = 'moodle'; $eventdata->name = 'backup'; message_send($eventdata); } //Everything is finished stop backup_auto_running backup_cron_automated_helper::set_state_running(false); mtrace('Automated backups complete.'); return $status; }
/** * Runs the automated backups if required * * @global moodle_database $DB */ public static function run_automated_backup($rundirective = self::RUN_ON_SCHEDULE) { global $CFG, $DB; $status = true; $emailpending = false; $now = time(); mtrace("Checking automated backup status", '...'); $state = backup_cron_automated_helper::get_automated_backup_state($rundirective); if ($state === backup_cron_automated_helper::STATE_DISABLED) { mtrace('INACTIVE'); return $state; } else { if ($state === backup_cron_automated_helper::STATE_RUNNING) { mtrace('RUNNING'); if ($rundirective == self::RUN_IMMEDIATELY) { mtrace('Automated backups are already running. If this script is being run by cron this constitues an error. You will need to increase the time between executions within cron.'); } else { mtrace("automated backup are already running. Execution delayed"); } return $state; } else { mtrace('OK'); } } backup_cron_automated_helper::set_state_running(); mtrace("Getting admin info"); $admin = get_admin(); if (!$admin) { mtrace("Error: No admin account was found"); $state = false; } if ($status) { mtrace("Checking courses"); mtrace("Skipping deleted courses", '...'); mtrace(sprintf("%d courses", backup_cron_automated_helper::remove_deleted_courses_from_schedule())); } if ($status) { mtrace('Running required automated backups...'); // This could take a while! @set_time_limit(0); raise_memory_limit(MEMORY_EXTRA); $nextstarttime = backup_cron_automated_helper::calculate_next_automated_backup($admin->timezone, $now); $showtime = "undefined"; if ($nextstarttime > 0) { $showtime = userdate($nextstarttime, "", $admin->timezone); } $rs = $DB->get_recordset('course'); foreach ($rs as $course) { $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); if (!$backupcourse) { $backupcourse = new stdClass(); $backupcourse->courseid = $course->id; $DB->insert_record('backup_courses', $backupcourse); $backupcourse = $DB->get_record('backup_courses', array('courseid' => $course->id)); } // Skip courses that do not yet need backup $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 $sqlwhere = "course=:courseid AND time>:time AND " . $DB->sql_like('action', ':action', false, true, true); $params = array('courseid' => $course->id, 'time' => $now - 31 * 24 * 60 * 60, 'action' => '%view%'); $logexists = $DB->record_exists_select('log', $sqlwhere, $params); if (!$logexists) { $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_SKIPPED; $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); mtrace('Skipping unchanged course ' . $course->fullname); $skipped = true; } } //Now we backup every non-skipped course if (!$skipped) { mtrace('Backing up ' . $course->fullname, '...'); //We have to send a email because we have included at least one backup $emailpending = true; //Only make the backup if laststatus isn't 2-UNFINISHED (uncontrolled error) if ($backupcourse->laststatus != 2) { //Set laststarttime $starttime = time(); $backupcourse->laststarttime = time(); $backupcourse->laststatus = backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED; $DB->update_record('backup_courses', $backupcourse); $backupcourse->laststatus = backup_cron_automated_helper::launch_automated_backup($course, $backupcourse->laststarttime, $admin->id); $backupcourse->lastendtime = time(); $backupcourse->nextstarttime = $nextstarttime; $DB->update_record('backup_courses', $backupcourse); if ($backupcourse->laststatus) { // Clean up any excess course backups now that we have // taken a successful backup. $removedcount = backup_cron_automated_helper::remove_excess_backups($course); } } mtrace("complete - next execution: {$showtime}"); } } $rs->close(); } //Send email to admin if necessary if ($emailpending) { mtrace("Sending email to admin"); $message = ""; $count = backup_cron_automated_helper::get_backup_status_array(); $haserrors = $count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] != 0 || $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] != 0; //Build the message text //Summary $message .= get_string('summary') . "\n"; $message .= "==================================================\n"; $message .= " " . get_string('courses') . ": " . array_sum($count) . "\n"; $message .= " " . get_string('ok') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_OK] . "\n"; $message .= " " . get_string('skipped') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_SKIPPED] . "\n"; $message .= " " . get_string('error') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_ERROR] . "\n"; $message .= " " . get_string('unfinished') . ": " . $count[backup_cron_automated_helper::BACKUP_STATUS_UNFINISHED] . "\n\n"; //Reference if ($haserrors) { $message .= " " . get_string('backupfailed') . "\n\n"; $dest_url = "{$CFG->wwwroot}/report/backups/index.php"; $message .= " " . get_string('backuptakealook', '', $dest_url) . "\n\n"; //Set message priority $admin->priority = 1; //Reset unfinished to error $DB->set_field('backup_courses', 'laststatus', '0', array('laststatus' => '2')); } else { $message .= " " . get_string('backupfinished') . "\n"; } //Build the message subject $site = get_site(); $prefix = format_string($site->shortname, true, array('context' => get_context_instance(CONTEXT_COURSE, SITEID))) . ": "; if ($haserrors) { $prefix .= "[" . strtoupper(get_string('error')) . "] "; } $subject = $prefix . get_string('automatedbackupstatus', 'backup'); //Send the message $eventdata = new stdClass(); $eventdata->modulename = 'moodle'; $eventdata->userfrom = $admin; $eventdata->userto = $admin; $eventdata->subject = $subject; $eventdata->fullmessage = $message; $eventdata->fullmessageformat = FORMAT_PLAIN; $eventdata->fullmessagehtml = ''; $eventdata->smallmessage = ''; $eventdata->component = 'moodle'; $eventdata->name = 'backup'; message_send($eventdata); } //Everything is finished stop backup_auto_running backup_cron_automated_helper::set_state_running(false); mtrace('Automated backups complete.'); return $status; }
/** * Test {@link backup_cron_automated_helper::calculate_next_automated_backup}. */ public function test_next_automated_backup() { $this->resetAfterTest(); set_config('backup_auto_active', '1', 'backup'); // Notes // - backup_auto_weekdays starts on Sunday // - Tests cannot be done in the past // - Only the DST on the server side is handled. // 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 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 18: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 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'); $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'); $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_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'); $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'); $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'); $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'); $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'); $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'); $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'); $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_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'); $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'); $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'); $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'); $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'); $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'); $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'); $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_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'); $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'); $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'); $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'); $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'); $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'); $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'); $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). // Warning: Some of these tests will fail if executed "around" // 'Europe/Brussels' DST changes (last Sunday in March and // last Sunday in October right now - 2012). Once Moodle // moves to PHP TZ support this could be fixed properly. date_default_timezone_set('Europe/Brussels'); $now = strtotime('18:00:00'); $dst = date('I', $now); $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). // Warning: Some of these tests will fail if executed "around" // 'America/New_York' DST changes (2nd Sunday in March and // 1st Sunday in November right now - 2012). Once Moodle // moves to PHP TZ support this could be fixed properly. date_default_timezone_set('America/New_York'); $now = strtotime('18:00:00'); $dst = date('I', $now); $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'); // Note: These tests should not fail because they are "unnafected" // by DST changes, as far as execution always happens on Monday and // Saturday and those week days are not, right now, the ones rulez // to peform the DST changes (Sunday is). This may change if rules // are modified in the future. date_default_timezone_set('Europe/Brussels'); $now = strtotime('next Monday 18:00:00'); $dst = date('I', $now); $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'); // Note: These tests should not fail because they are "unnafected" // by DST changes, as far as execution always happens on Monday and // Saturday and those week days are not, right now, the ones rulez // to peform the DST changes (Sunday is). This may change if rules // are modified in the future. date_default_timezone_set('America/New_York'); $now = strtotime('next Monday 04:00:00'); $dst = date('I', $now); $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)); }
public function test_next_automated_backup() { $this->resetAfterTest(); $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. // 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'); $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'); $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 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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $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 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 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 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 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 Saturday'); $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'); $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now); $this->assertEquals('1-20:30', date('w-H:i', $next)); }
/** * Test {@link backup_cron_automated_helper::calculate_next_automated_backup}. */ public function test_next_automated_backup() { $this->resetAfterTest(); set_config('backup_auto_active', '1', 'backup'); // Notes // - backup_auto_weekdays starts on Sunday // - Tests cannot be done in the past // - Only the DST on the server side is handled. // 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 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 18: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 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'); $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'); $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_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'); $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'); $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'); $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'); $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'); $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'); $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'); $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_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'); $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'); $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'); $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'); $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'); $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'); $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'); $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_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'); $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'); $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'); $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'); $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'); $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'); $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'); $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)); }
/** * Test {@link backup_cron_automated_helper::calculate_next_automated_backup}. */ public function test_next_automated_backup() { global $CFG; $this->resetAfterTest(); set_config('backup_auto_active', '1', 'backup'); $this->setTimezone('Australia/Perth'); // Notes // - backup_auto_weekdays starts on Sunday // - Tests cannot be done in the past // - Only the DST on the server side is handled. // 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; // Ignored, everything is calculated in server timezone!!! $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 18: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 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'); $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'); $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_weekdays', '1000001', 'backup'); set_config('backup_auto_hour', '0', 'backup'); set_config('backup_auto_minute', '0', 'backup'); $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 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 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 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 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 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 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_weekdays', '1000000', 'backup'); set_config('backup_auto_hour', '4', 'backup'); set_config('backup_auto_minute', '0', 'backup'); $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 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 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 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 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 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 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_weekdays', '1110111', 'backup'); set_config('backup_auto_hour', '20', 'backup'); set_config('backup_auto_minute', '30', 'backup'); $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'); $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'); $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'); $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'); $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'); $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'); $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'); $now = strtotime('next Monday 13:00:00'); $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'); $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'); $this->setTimezone('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)); $this->setTimezone('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)); $this->setTimezone('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)); }