mirror of
https://github.com/moodle/moodle.git
synced 2025-01-18 22:08:20 +01:00
MDL-69764 tool_task: unify crontab verification
Use scheduled_task crontab field verification in admin/tool/task to unify how Moodle deals with crontab definition and its verification. This helps remove duplicated code and fix crontab definition not allowed in the web form, but actually was valid. Updated crontab fields precision on task_scheduled table to have enough room for the worst case: all possible different values separated by comma.
This commit is contained in:
parent
d24a4ab56f
commit
21f73b29c1
@ -121,52 +121,33 @@ class tool_task_edit_scheduled_task_form extends moodleform {
|
||||
*/
|
||||
public function validation($data, $files) {
|
||||
$error = parent::validation($data, $files);
|
||||
$fields = array('minute', 'hour', 'day', 'month', 'dayofweek');
|
||||
foreach ($fields as $field) {
|
||||
if (!self::validate_fields($field, $data[$field])) {
|
||||
$error[$field . 'group'] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
}
|
||||
return $error;
|
||||
}
|
||||
// Use a checker class.
|
||||
$checker = new \tool_task\scheduled_checker_task();
|
||||
$checker->set_minute($data['minute']);
|
||||
$checker->set_hour($data['hour']);
|
||||
$checker->set_month($data['month']);
|
||||
$checker->set_day_of_week($data['dayofweek']);
|
||||
$checker->set_day($data['day']);
|
||||
$checker->set_disabled(false);
|
||||
$checker->set_customised(false);
|
||||
|
||||
/**
|
||||
* Helper function that validates the submitted data.
|
||||
*
|
||||
* Explanation of the regex:-
|
||||
*
|
||||
* \A\*\z - matches *
|
||||
* \A[0-5]?[0-9]\z - matches entries like 23
|
||||
* \A\*\/[0-5]?[0-9]\z - matches entries like * / 5
|
||||
* \A[0-5]?[0-9](,[0-5]?[0-9])*\z - matches entries like 1,2,3
|
||||
* \A[0-5]?[0-9]-[0-5]?[0-9]\z - matches entries like 2-10
|
||||
*
|
||||
* @param string $field field to validate
|
||||
* @param string $value value
|
||||
*
|
||||
* @return bool true if validation passes, false other wise.
|
||||
*/
|
||||
public static function validate_fields($field, $value) {
|
||||
switch ($field) {
|
||||
case 'minute' :
|
||||
case 'hour' :
|
||||
$regex = "/\A\*\z|\A[0-5]?[0-9]\z|\A\*\/[0-5]?[0-9]\z|\A[0-5]?[0-9](,[0-5]?[0-9])*\z|\A[0-5]?[0-9]-[0-5]?[0-9]\z/";
|
||||
break;
|
||||
case 'day':
|
||||
$regex = "/\A\*\z|\A([1-2]?[0-9]|3[0-1])\z|\A\*\/([1-2]?[0-9]|3[0-1])\z|";
|
||||
$regex .= "\A([1-2]?[0-9]|3[0-1])(,([1-2]?[0-9]|3[0-1]))*\z|\A([1-2]?[0-9]|3[0-1])-([1-2]?[0-9]|3[0-1])\z/";
|
||||
break;
|
||||
case 'month':
|
||||
$regex = "/\A\*\z|\A([0-9]|1[0-2])\z|\A\*\/([0-9]|1[0-2])\z|\A([0-9]|1[0-2])(,([0-9]|1[0-2]))*\z|";
|
||||
$regex .= "\A([0-9]|1[0-2])-([0-9]|1[0-2])\z/";
|
||||
break;
|
||||
case 'dayofweek':
|
||||
$regex = "/\A\*\z|\A[0-6]\z|\A\*\/[0-6]\z|\A[0-6](,[0-6])*\z|\A[0-6]-[0-6]\z/";
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
if (!$checker->is_valid($checker::FIELD_MINUTE)) {
|
||||
$error['minutegroup'] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
return (bool)preg_match($regex, $value);
|
||||
if (!$checker->is_valid($checker::FIELD_HOUR)) {
|
||||
$error['hourgroup'] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
if (!$checker->is_valid($checker::FIELD_DAY)) {
|
||||
$error['daygroup'] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
if (!$checker->is_valid($checker::FIELD_MONTH)) {
|
||||
$error['monthgroup'] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
if (!$checker->is_valid($checker::FIELD_DAYOFWEEK)) {
|
||||
$error['dayofweekgroup'] = get_string('invaliddata', 'core_error');
|
||||
}
|
||||
|
||||
return $error;
|
||||
}
|
||||
}
|
||||
|
||||
|
40
admin/tool/task/classes/scheduled_checker_task.php
Normal file
40
admin/tool/task/classes/scheduled_checker_task.php
Normal file
@ -0,0 +1,40 @@
|
||||
<?php
|
||||
// This file is part of Moodle - http://moodle.org/
|
||||
//
|
||||
// Moodle is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// Moodle is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
namespace tool_task;
|
||||
|
||||
/**
|
||||
* Checker class. Fake scheduled task used only to check that crontab settings are valid.
|
||||
*
|
||||
* @package tool_task
|
||||
* @copyright 2021 Jordi Pujol-Ahulló <jpahullo@gmail.com>
|
||||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
||||
*/
|
||||
class scheduled_checker_task extends \core\task\scheduled_task {
|
||||
|
||||
/**
|
||||
* Gets the checker task name.
|
||||
*/
|
||||
public function get_name() {
|
||||
return "Checker task";
|
||||
}
|
||||
|
||||
/**
|
||||
* Does nothing.
|
||||
*/
|
||||
public function execute() {
|
||||
}
|
||||
}
|
@ -39,237 +39,248 @@ class tool_task_form_testcase extends advanced_testcase {
|
||||
* Test validations for minute field.
|
||||
*/
|
||||
public function test_validate_fields_minute() {
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '*');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '65');
|
||||
$this->assertFalse($valid);
|
||||
$checker = new \tool_task\scheduled_checker_task();
|
||||
$checker->set_minute('*');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('65');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '*/');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '*/1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '*/20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '*/65');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_minute('*/');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('*/1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('*/20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('*/60');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '1,2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '2,20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '20,30,45');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '65,20,30');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '25,75');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_minute('1,2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('2,20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('20,30,45');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('65,20,30');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('25,75');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '1-2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '2-20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '20-30');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '65-20');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('minute', '25-75');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_minute('1-2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('2-20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('20-30');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('65-20');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
$checker->set_minute('25-75');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MINUTE));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validations for minute hour.
|
||||
*/
|
||||
public function test_validate_fields_hour() {
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '*');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '65');
|
||||
$this->assertFalse($valid);
|
||||
$checker = new \tool_task\scheduled_checker_task();
|
||||
$checker->set_hour('*');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('60');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('65');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '*/');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '*/1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '*/20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '*/65');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_hour('*/');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('*/1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('*/20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('*/60');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('*/65');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '1,2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '2,20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '20,30,45');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '65,20,30');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '25,75');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_hour('1,2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('2,20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('20,30,45');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('65,20,30');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('25,75');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '1-2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '2-20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '20-30');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '65-20');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('hour', '25-75');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_hour('1-2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('2-20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('20-30');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('65-20');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
$checker->set_hour('24-75');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_HOUR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validations for day field.
|
||||
*/
|
||||
public function test_validate_fields_day() {
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '*');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '65');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '35');
|
||||
$this->assertFalse($valid);
|
||||
$checker = new \tool_task\scheduled_checker_task();
|
||||
$checker->set_day('*');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('65');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '*/');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '*/1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '*/20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '*/65');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '*/35');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_day('*/');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('*/1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('*/20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('*/65');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('*/35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '1,2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '2,20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '20,30,25');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '65,20,30');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '25,35');
|
||||
$this->assertFalse($valid);
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '1-2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '2-20');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '20-30');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '65-20');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('day', '25-35');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_day('1,2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('2,20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('20,30,25');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('65,20,30');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('25,35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
|
||||
$checker->set_day('1-2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('2-20');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('20-30');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('65-20');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
$checker->set_day('25-35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAY));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validations for month field.
|
||||
*/
|
||||
public function test_validate_fields_month() {
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '*');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '10');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '13');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '35');
|
||||
$this->assertFalse($valid);
|
||||
$checker = new \tool_task\scheduled_checker_task();
|
||||
$checker->set_month('*');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('10');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('13');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '*/');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '*/1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '*/12');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '*/13');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '*/35');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_month('*/');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('*/1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('*/12');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('*/13');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('*/35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '1,2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '2,11');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '2,10,12');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '65,2,13');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '25,35');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_month('1,2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('2,11');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('2,10,12');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('65,2,13');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('25,35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '1-2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '2-12');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '3-6');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '65-2');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('month', '25-26');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_month('1-2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('2-12');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('3-6');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('65-2');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
$checker->set_month('25-26');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_MONTH));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test validations for dayofweek field.
|
||||
*/
|
||||
public function test_validate_fields_dayofweek() {
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '*');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '0');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '6');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '7');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '20');
|
||||
$this->assertFalse($valid);
|
||||
$checker = new \tool_task\scheduled_checker_task();
|
||||
$checker->set_day_of_week('*');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('0');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('6');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('7');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('20');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '*/');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '*/1');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '*/6');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '*/13');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '*/35');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_day_of_week('*/');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('*/1');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('*/6');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('*/7');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('*/13');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('*/35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '1,2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '2,6');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '2,6,3');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '65,2,13');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '25,35');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_day_of_week('1,2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('2,6');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('2,6,3');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('65,2,13');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('25,35');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '1-2');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '2-6');
|
||||
$this->assertTrue($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '65-2');
|
||||
$this->assertFalse($valid);
|
||||
$valid = \tool_task_edit_scheduled_task_form::validate_fields('dayofweek', '3-7');
|
||||
$this->assertFalse($valid);
|
||||
$checker->set_day_of_week('1-2');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('2-6');
|
||||
$this->assertTrue($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('65-2');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
$checker->set_day_of_week('3-7');
|
||||
$this->assertFalse($checker->is_valid($checker::FIELD_DAYOFWEEK));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,11 +41,42 @@ abstract class scheduled_task extends task_base {
|
||||
/** Maximum hour value. */
|
||||
const HOURMAX = 23;
|
||||
|
||||
/** Minimum day of month value. */
|
||||
const DAYMIN = 1;
|
||||
/** Maximum day of month value. */
|
||||
const DAYMAX = 31;
|
||||
|
||||
/** Minimum month value. */
|
||||
const MONTHMIN = 1;
|
||||
/** Maximum month value. */
|
||||
const MONTHMAX = 12;
|
||||
|
||||
/** Minimum dayofweek value. */
|
||||
const DAYOFWEEKMIN = 0;
|
||||
/** Maximum dayofweek value. */
|
||||
const DAYOFWEEKMAX = 6;
|
||||
|
||||
/**
|
||||
* Minute field identifier.
|
||||
*/
|
||||
const FIELD_MINUTE = 'minute';
|
||||
/**
|
||||
* Hour field identifier.
|
||||
*/
|
||||
const FIELD_HOUR = 'hour';
|
||||
/**
|
||||
* Day-of-month field identifier.
|
||||
*/
|
||||
const FIELD_DAY = 'day';
|
||||
/**
|
||||
* Month field identifier.
|
||||
*/
|
||||
const FIELD_MONTH = 'month';
|
||||
/**
|
||||
* Day-of-week field identifier.
|
||||
*/
|
||||
const FIELD_DAYOFWEEK = 'dayofweek';
|
||||
|
||||
/** @var string $hour - Pattern to work out the valid hours */
|
||||
private $hour = '*';
|
||||
|
||||
@ -75,6 +106,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Get the last run time for this scheduled task.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_last_run_time() {
|
||||
@ -83,6 +115,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Set the last run time for this scheduled task.
|
||||
*
|
||||
* @param int $lastruntime
|
||||
*/
|
||||
public function set_last_run_time($lastruntime) {
|
||||
@ -91,6 +124,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Has this task been changed from it's default config?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_customised() {
|
||||
@ -99,6 +133,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Has this task been changed from it's default config?
|
||||
*
|
||||
* @param bool
|
||||
*/
|
||||
public function set_customised($customised) {
|
||||
@ -107,6 +142,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Has this task been changed from it's default config?
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is_overridden(): bool {
|
||||
@ -115,6 +151,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Set the overridden value.
|
||||
*
|
||||
* @param bool $overridden
|
||||
*/
|
||||
public function set_overridden(bool $overridden): void {
|
||||
@ -124,6 +161,7 @@ abstract class scheduled_task extends task_base {
|
||||
/**
|
||||
* Setter for $minute. Accepts a special 'R' value
|
||||
* which will be translated to a random minute.
|
||||
*
|
||||
* @param string $minute
|
||||
* @param bool $expandr - if true (default) an 'R' value in a time is expanded to an appropriate int.
|
||||
* If false, they are left as 'R'
|
||||
@ -137,6 +175,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Getter for $minute.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_minute() {
|
||||
@ -146,6 +185,7 @@ abstract class scheduled_task extends task_base {
|
||||
/**
|
||||
* Setter for $hour. Accepts a special 'R' value
|
||||
* which will be translated to a random hour.
|
||||
*
|
||||
* @param string $hour
|
||||
* @param bool $expandr - if true (default) an 'R' value in a time is expanded to an appropriate int.
|
||||
* If false, they are left as 'R'
|
||||
@ -159,6 +199,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Getter for $hour.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_hour() {
|
||||
@ -167,6 +208,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Setter for $month.
|
||||
*
|
||||
* @param string $month
|
||||
*/
|
||||
public function set_month($month) {
|
||||
@ -175,6 +217,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Getter for $month.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_month() {
|
||||
@ -183,6 +226,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Setter for $day.
|
||||
*
|
||||
* @param string $day
|
||||
*/
|
||||
public function set_day($day) {
|
||||
@ -191,6 +235,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Getter for $day.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_day() {
|
||||
@ -199,6 +244,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Setter for $dayofweek.
|
||||
*
|
||||
* @param string $dayofweek
|
||||
* @param bool $expandr - if true (default) an 'R' value in a time is expanded to an appropriate int.
|
||||
* If false, they are left as 'R'
|
||||
@ -212,6 +258,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Getter for $dayofweek.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_day_of_week() {
|
||||
@ -220,6 +267,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Setter for $disabled.
|
||||
*
|
||||
* @param bool $disabled
|
||||
*/
|
||||
public function set_disabled($disabled) {
|
||||
@ -243,6 +291,57 @@ abstract class scheduled_task extends task_base {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Informs whether the given field is valid.
|
||||
* Use the constants FIELD_* to identify the field.
|
||||
* Have to be called after the method set_{field}(string).
|
||||
*
|
||||
* @param string $field field identifier; expected values from constants FIELD_*.
|
||||
*
|
||||
* @return bool true if given field is valid. false otherwise.
|
||||
*/
|
||||
public function is_valid(string $field): bool {
|
||||
return !empty($this->get_valid($field));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates the list of valid values according to the given field and stored expression.
|
||||
*
|
||||
* @param string $field field identifier. Must be one of those FIELD_*.
|
||||
*
|
||||
* @return array(int) list of matching values.
|
||||
*
|
||||
* @throws \coding_exception when passed an invalid field identifier.
|
||||
*/
|
||||
private function get_valid(string $field): array {
|
||||
switch($field) {
|
||||
case self::FIELD_MINUTE:
|
||||
$min = self::MINUTEMIN;
|
||||
$max = self::MINUTEMAX;
|
||||
break;
|
||||
case self::FIELD_HOUR:
|
||||
$min = self::HOURMIN;
|
||||
$max = self::HOURMAX;
|
||||
break;
|
||||
case self::FIELD_DAY:
|
||||
$min = self::DAYMIN;
|
||||
$max = self::DAYMAX;
|
||||
break;
|
||||
case self::FIELD_MONTH:
|
||||
$min = self::MONTHMIN;
|
||||
$max = self::MONTHMAX;
|
||||
break;
|
||||
case self::FIELD_DAYOFWEEK:
|
||||
$min = self::DAYOFWEEKMIN;
|
||||
$max = self::DAYOFWEEKMAX;
|
||||
break;
|
||||
default:
|
||||
throw new \coding_exception("Field '$field' is not a valid crontab identifier.");
|
||||
}
|
||||
|
||||
return $this->eval_cron_field($this->{$field}, $min, $max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Take a cron field definition and return an array of valid numbers with the range min-max.
|
||||
*
|
||||
@ -272,7 +371,6 @@ abstract class scheduled_task extends task_base {
|
||||
$last = 0;
|
||||
$inrange = false;
|
||||
$instep = false;
|
||||
|
||||
foreach ($matches[0] as $match) {
|
||||
if ($match == '*') {
|
||||
array_push($range, range($min, $max));
|
||||
@ -281,28 +379,35 @@ abstract class scheduled_task extends task_base {
|
||||
} else if ($match == '-') {
|
||||
$inrange = true;
|
||||
} else if (is_numeric($match)) {
|
||||
if ($min > $match || $match > $max) {
|
||||
// This is a value error: The value lays out of the expected range of values.
|
||||
return [];
|
||||
}
|
||||
if ($instep) {
|
||||
$i = 0;
|
||||
for ($i = 0; $i < count($range[count($range) - 1]); $i++) {
|
||||
if (($i) % $match != 0) {
|
||||
$range[count($range) - 1][$i] = -1;
|
||||
}
|
||||
}
|
||||
$inrange = false;
|
||||
$instep = false;
|
||||
} else if ($inrange) {
|
||||
if (count($range)) {
|
||||
$range[count($range) - 1] = range($last, $match);
|
||||
}
|
||||
$inrange = false;
|
||||
} else {
|
||||
if ($match >= $min && $match <= $max) {
|
||||
array_push($range, $match);
|
||||
}
|
||||
array_push($range, $match);
|
||||
$last = $match;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If inrange or instep were not processed, there is a syntax error.
|
||||
// Cleanup any existing values to show up the error.
|
||||
if ($inrange || $instep) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Flatten the result.
|
||||
$result = array();
|
||||
foreach ($range as $r) {
|
||||
@ -348,21 +453,19 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Calculate when this task should next be run based on the schedule.
|
||||
*
|
||||
* @return int $nextruntime.
|
||||
*/
|
||||
public function get_next_scheduled_time() {
|
||||
global $CFG;
|
||||
|
||||
$validminutes = $this->eval_cron_field($this->minute, self::MINUTEMIN, self::MINUTEMAX);
|
||||
$validhours = $this->eval_cron_field($this->hour, self::HOURMIN, self::HOURMAX);
|
||||
|
||||
// We need to change to the server timezone before using php date() functions.
|
||||
\core_date::set_default_server_timezone();
|
||||
|
||||
$daysinmonth = date("t");
|
||||
$validdays = $this->eval_cron_field($this->day, 1, $daysinmonth);
|
||||
$validdaysofweek = $this->eval_cron_field($this->dayofweek, 0, 7);
|
||||
$validmonths = $this->eval_cron_field($this->month, 1, 12);
|
||||
$validminutes = $this->get_valid(self::FIELD_MINUTE);
|
||||
$validhours = $this->get_valid(self::FIELD_HOUR);
|
||||
$validdays = $this->get_valid(self::FIELD_DAY);
|
||||
$validdaysofweek = $this->get_valid(self::FIELD_DAYOFWEEK);
|
||||
$validmonths = $this->get_valid(self::FIELD_MONTH);
|
||||
|
||||
$nextvalidyear = date('Y');
|
||||
|
||||
$currentminute = date("i") + 1;
|
||||
@ -383,6 +486,7 @@ abstract class scheduled_task extends task_base {
|
||||
$nextvaliddayofmonth = $this->next_in_list($currentday, $validdays);
|
||||
$nextvaliddayofweek = $this->next_in_list($currentdayofweek, $validdaysofweek);
|
||||
$daysincrementbymonth = $nextvaliddayofmonth - $currentday;
|
||||
$daysinmonth = date('t');
|
||||
if ($nextvaliddayofmonth < $currentday) {
|
||||
$daysincrementbymonth += $daysinmonth;
|
||||
}
|
||||
@ -431,6 +535,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Informs whether this task can be run.
|
||||
*
|
||||
* @return bool true when this task can be run. false otherwise.
|
||||
*/
|
||||
public function can_run(): bool {
|
||||
@ -441,6 +546,7 @@ abstract class scheduled_task extends task_base {
|
||||
* Checks whether the component and the task disabled flag enables to run this task.
|
||||
* This do not checks whether the task manager allows running them or if the
|
||||
* site allows tasks to "run now".
|
||||
*
|
||||
* @return bool true if task is enabled. false otherwise.
|
||||
*/
|
||||
public function is_enabled(): bool {
|
||||
@ -449,6 +555,7 @@ abstract class scheduled_task extends task_base {
|
||||
|
||||
/**
|
||||
* Produces a valid id string to use as id attribute based on the given FQCN class name.
|
||||
*
|
||||
* @param string $classname FQCN of a task.
|
||||
* @return string valid string to be used as id attribute.
|
||||
*/
|
||||
@ -461,6 +568,6 @@ abstract class scheduled_task extends task_base {
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public abstract function get_name();
|
||||
abstract public function get_name();
|
||||
|
||||
}
|
||||
|
@ -3401,10 +3401,10 @@
|
||||
<FIELD NAME="lastruntime" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
|
||||
<FIELD NAME="nextruntime" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
|
||||
<FIELD NAME="blocking" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Block the entire cron when this task is running."/>
|
||||
<FIELD NAME="minute" TYPE="char" LENGTH="25" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="hour" TYPE="char" LENGTH="25" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="day" TYPE="char" LENGTH="25" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="month" TYPE="char" LENGTH="25" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="minute" TYPE="char" LENGTH="200" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="hour" TYPE="char" LENGTH="70" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="day" TYPE="char" LENGTH="90" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="month" TYPE="char" LENGTH="30" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="dayofweek" TYPE="char" LENGTH="25" NOTNULL="true" SEQUENCE="false"/>
|
||||
<FIELD NAME="faildelay" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
|
||||
<FIELD NAME="customised" TYPE="int" LENGTH="2" NOTNULL="true" DEFAULT="0" SEQUENCE="false" COMMENT="Used on upgrades to prevent overwriting custom schedules."/>
|
||||
|
@ -3961,5 +3961,26 @@ privatefiles,moodle|/user/files.php';
|
||||
upgrade_main_savepoint(true, 2022020200.03);
|
||||
}
|
||||
|
||||
if ($oldversion < 2022021100.01) {
|
||||
|
||||
$table = new xmldb_table('task_scheduled');
|
||||
|
||||
// Changing precision of field minute on table task_scheduled to (200).
|
||||
$field = new xmldb_field('minute', XMLDB_TYPE_CHAR, '200', null, XMLDB_NOTNULL, null, null, 'blocking');
|
||||
$dbman->change_field_precision($table, $field);
|
||||
// Changing precision of field hour on table task_scheduled to (70).
|
||||
$field = new xmldb_field('hour', XMLDB_TYPE_CHAR, '70', null, XMLDB_NOTNULL, null, null, 'minute');
|
||||
$dbman->change_field_precision($table, $field);
|
||||
// Changing precision of field day on table task_scheduled to (90).
|
||||
$field = new xmldb_field('day', XMLDB_TYPE_CHAR, '90', null, XMLDB_NOTNULL, null, null, 'hour');
|
||||
$dbman->change_field_precision($table, $field);
|
||||
// Changing precision of field month on table task_scheduled to (30).
|
||||
$field = new xmldb_field('month', XMLDB_TYPE_CHAR, '30', null, XMLDB_NOTNULL, null, null, 'day');
|
||||
$dbman->change_field_precision($table, $field);
|
||||
|
||||
// Main savepoint reached.
|
||||
upgrade_main_savepoint(true, 2022021100.01);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -47,7 +47,8 @@ class core_scheduled_task_testcase extends advanced_testcase {
|
||||
$this->assertEquals(15, count($testclass->eval_cron_field('1-10,5-15', 0, 59)));
|
||||
$this->assertEquals(13, count($testclass->eval_cron_field('1-10,5-15/2', 0, 59)));
|
||||
$this->assertEquals(3, count($testclass->eval_cron_field('1,2,3,1,2,3', 0, 59)));
|
||||
$this->assertEquals(1, count($testclass->eval_cron_field('-1,10,80', 0, 59)));
|
||||
$this->assertEquals(0, count($testclass->eval_cron_field('-1,10,80', 0, 59)));
|
||||
$this->assertEquals(0, count($testclass->eval_cron_field('-1', 0, 59)));
|
||||
}
|
||||
|
||||
public function test_get_next_scheduled_time() {
|
||||
@ -108,7 +109,7 @@ class core_scheduled_task_testcase extends advanced_testcase {
|
||||
// Next valid time should be next 10 minute boundary.
|
||||
$nexttime = $testclass->get_next_scheduled_time();
|
||||
|
||||
$minutes = ((intval(date('i') / 10))+1) * 10;
|
||||
$minutes = ((intval(date('i') / 10)) + 1) * 10;
|
||||
$nexttenminutes = mktime(date('H'), $minutes, 0);
|
||||
|
||||
$this->assertEquals($nexttenminutes, $nexttime, 'Next scheduled time is in 10 minutes.');
|
||||
@ -128,7 +129,7 @@ class core_scheduled_task_testcase extends advanced_testcase {
|
||||
$this->assertEquals(7, date('N', $nexttime));
|
||||
$this->assertEquals(0, date('i', $nexttime));
|
||||
|
||||
// Test monthly job
|
||||
// Test monthly job.
|
||||
$testclass = new \core\task\scheduled_test_task();
|
||||
$testclass->set_minute('32');
|
||||
$testclass->set_hour('0');
|
||||
|
@ -29,7 +29,7 @@
|
||||
|
||||
defined('MOODLE_INTERNAL') || die();
|
||||
|
||||
$version = 2022021100.00; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
$version = 2022021100.01; // YYYYMMDD = weekly release date of this DEV branch.
|
||||
// RR = release increments - 00 in DEV branches.
|
||||
// .XX = incremental changes.
|
||||
$release = '4.0dev+ (Build: 20220211)'; // Human-friendly version name
|
||||
|
Loading…
x
Reference in New Issue
Block a user