Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'MDL-29470-23' of git://github.com/FMCorz/moodle into MO…

…ODLE_23_STABLE
  • Loading branch information...
commit bd75b67885fa7e0317fed9961f36f9f71a6b7be3 2 parents 3936796 + 8fb4b73
Sam Hemelryk samhemelryk authored
20 backup/util/helper/backup_cron_helper.class.php
@@ -281,23 +281,25 @@ public static function calculate_next_automated_backup($timezone, $now) {
281 281 $midnight = usergetmidnight($now, $timezone);
282 282 $date = usergetdate($now, $timezone);
283 283
284   - //Get number of days (from today) to execute backups
285   - $automateddays = substr($config->backup_auto_weekdays,$date['wday']) . $config->backup_auto_weekdays;
286   - $daysfromtoday = strpos($automateddays, "1");
  284 + // Get number of days (from today) to execute backups
  285 + $automateddays = substr($config->backup_auto_weekdays, $date['wday']) . $config->backup_auto_weekdays;
  286 + $daysfromtoday = strpos($automateddays, "1", 1);
  287 +
  288 + // If we can't find the next day, we set it to tomorrow
287 289 if (empty($daysfromtoday)) {
288 290 $daysfromtoday = 1;
289 291 }
290 292
291   - //If some day has been found
  293 + // If some day has been found
292 294 if ($daysfromtoday !== false) {
293   - //Calculate distance
294   - $dist = ($daysfromtoday * 86400) + //Days distance
295   - ($config->backup_auto_hour * 3600) + //Hours distance
296   - ($config->backup_auto_minute * 60); //Minutes distance
  295 + // Calculate distance
  296 + $dist = ($daysfromtoday * 86400) + // Days distance
  297 + ($config->backup_auto_hour * 3600) + // Hours distance
  298 + ($config->backup_auto_minute * 60); // Minutes distance
297 299 $result = $midnight + $dist;
298 300 }
299 301
300   - //If that time is past, call the function recursively to obtain the next valid day
  302 + // If that time is past, call the function recursively to obtain the next valid day
301 303 if ($result > 0 && $result < time()) {
302 304 $result = self::calculate_next_automated_backup($timezone, $result);
303 305 }
184 lib/tests/backup_test.php
... ... @@ -0,0 +1,184 @@
  1 +<?php
  2 +// This file is part of Moodle - http://moodle.org/
  3 +//
  4 +// Moodle is free software: you can redistribute it and/or modify
  5 +// it under the terms of the GNU General Public License as published by
  6 +// the Free Software Foundation, either version 3 of the License, or
  7 +// (at your option) any later version.
  8 +//
  9 +// Moodle is distributed in the hope that it will be useful,
  10 +// but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12 +// GNU General Public License for more details.
  13 +//
  14 +// You should have received a copy of the GNU General Public License
  15 +// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16 +
  17 +/**
  18 + * Unit tests for backups.
  19 + *
  20 + * @package core
  21 + * @category phpunit
  22 + * @copyright 2012 Frédéric Massart <fred@moodle.com>
  23 + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24 + */
  25 +
  26 +defined('MOODLE_INTERNAL') || die();
  27 +
  28 +global $CFG;
  29 +require_once($CFG->dirroot . '/backup/util/helper/backup_cron_helper.class.php');
  30 +
  31 +/**
  32 + * Unit tests for backup system
  33 + */
  34 +class backup_testcase extends advanced_testcase {
  35 +
  36 + public function test_next_automated_backup() {
  37 +
  38 + $this->resetAfterTest();
  39 + $admin = get_admin();
  40 + $timezone = $admin->timezone;
  41 +
  42 + // Notes
  43 + // - The next automated backup will never be on the same date than $now
  44 + // - backup_auto_weekdays starts on Sunday
  45 + // - Tests cannot be done in the past.
  46 +
  47 + // Every Wed and Sat at 11pm.
  48 + set_config('backup_auto_active', '1', 'backup');
  49 + set_config('backup_auto_weekdays', '0010010', 'backup');
  50 + set_config('backup_auto_hour', '23', 'backup');
  51 + set_config('backup_auto_minute', '0', 'backup');
  52 +
  53 + $now = strtotime('next Monday');
  54 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  55 + $this->assertEquals('2-23:00', date('w-H:i', $next));
  56 +
  57 + $now = strtotime('next Tuesday');
  58 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  59 + $this->assertEquals('5-23:00', date('w-H:i', $next));
  60 +
  61 + $now = strtotime('next Wednesday');
  62 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  63 + $this->assertEquals('5-23:00', date('w-H:i', $next));
  64 +
  65 + $now = strtotime('next Thursday');
  66 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  67 + $this->assertEquals('5-23:00', date('w-H:i', $next));
  68 +
  69 + $now = strtotime('next Friday');
  70 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  71 + $this->assertEquals('2-23:00', date('w-H:i', $next));
  72 +
  73 + $now = strtotime('next Saturday');
  74 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  75 + $this->assertEquals('2-23:00', date('w-H:i', $next));
  76 +
  77 + $now = strtotime('next Sunday');
  78 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  79 + $this->assertEquals('2-23:00', date('w-H:i', $next));
  80 +
  81 + // Every Sun and Sat at 12pm.
  82 + set_config('backup_auto_active', '1', 'backup');
  83 + set_config('backup_auto_weekdays', '1000001', 'backup');
  84 + set_config('backup_auto_hour', '0', 'backup');
  85 + set_config('backup_auto_minute', '0', 'backup');
  86 +
  87 + $now = strtotime('next Monday');
  88 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  89 + $this->assertEquals('6-00:00', date('w-H:i', $next));
  90 +
  91 + $now = strtotime('next Tuesday');
  92 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  93 + $this->assertEquals('6-00:00', date('w-H:i', $next));
  94 +
  95 + $now = strtotime('next Wednesday');
  96 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  97 + $this->assertEquals('6-00:00', date('w-H:i', $next));
  98 +
  99 + $now = strtotime('next Thursday');
  100 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  101 + $this->assertEquals('6-00:00', date('w-H:i', $next));
  102 +
  103 + $now = strtotime('next Friday');
  104 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  105 + $this->assertEquals('6-00:00', date('w-H:i', $next));
  106 +
  107 + $now = strtotime('next Saturday');
  108 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  109 + $this->assertEquals('0-00:00', date('w-H:i', $next));
  110 +
  111 + $now = strtotime('next Sunday');
  112 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  113 + $this->assertEquals('6-00:00', date('w-H:i', $next));
  114 +
  115 + // Every Sun at 4am.
  116 + set_config('backup_auto_active', '1', 'backup');
  117 + set_config('backup_auto_weekdays', '1000000', 'backup');
  118 + set_config('backup_auto_hour', '4', 'backup');
  119 + set_config('backup_auto_minute', '0', 'backup');
  120 +
  121 + $now = strtotime('next Monday');
  122 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  123 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  124 +
  125 + $now = strtotime('next Tuesday');
  126 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  127 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  128 +
  129 + $now = strtotime('next Wednesday');
  130 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  131 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  132 +
  133 + $now = strtotime('next Thursday');
  134 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  135 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  136 +
  137 + $now = strtotime('next Friday');
  138 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  139 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  140 +
  141 + $now = strtotime('next Saturday');
  142 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  143 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  144 +
  145 + $now = strtotime('next Sunday');
  146 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  147 + $this->assertEquals('0-04:00', date('w-H:i', $next));
  148 +
  149 + // Every day but Wed at 8:30pm.
  150 + set_config('backup_auto_active', '1', 'backup');
  151 + set_config('backup_auto_weekdays', '1110111', 'backup');
  152 + set_config('backup_auto_hour', '20', 'backup');
  153 + set_config('backup_auto_minute', '30', 'backup');
  154 +
  155 + $now = strtotime('next Monday');
  156 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  157 + $this->assertEquals('2-20:30', date('w-H:i', $next));
  158 +
  159 + $now = strtotime('next Tuesday');
  160 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  161 + $this->assertEquals('4-20:30', date('w-H:i', $next));
  162 +
  163 + $now = strtotime('next Wednesday');
  164 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  165 + $this->assertEquals('4-20:30', date('w-H:i', $next));
  166 +
  167 + $now = strtotime('next Thursday');
  168 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  169 + $this->assertEquals('5-20:30', date('w-H:i', $next));
  170 +
  171 + $now = strtotime('next Friday');
  172 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  173 + $this->assertEquals('6-20:30', date('w-H:i', $next));
  174 +
  175 + $now = strtotime('next Saturday');
  176 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  177 + $this->assertEquals('0-20:30', date('w-H:i', $next));
  178 +
  179 + $now = strtotime('next Sunday');
  180 + $next = backup_cron_automated_helper::calculate_next_automated_backup($timezone, $now);
  181 + $this->assertEquals('1-20:30', date('w-H:i', $next));
  182 +
  183 + }
  184 +}

0 comments on commit bd75b67

Please sign in to comment.
Something went wrong with that request. Please try again.