From b56a5e0dfb2ab9b2de18ef8ac14296777ec61c85 Mon Sep 17 00:00:00 2001 From: Paul Holden Date: Tue, 31 Jan 2023 14:05:44 +0000 Subject: [PATCH] MDL-77091 reportbuilder: set base time in schedule test generator. The base time allows for the setting of the "current time" for the purposes of the schedule test against recurrence configuration. We should ensure the generator method allows this property so that it can be used when calculating the initial "next send time" of the schedule upon creation (and avoiding excessive recursion when the actual current time is used for the calculation). --- reportbuilder/classes/local/helpers/schedule.php | 7 ++++--- reportbuilder/tests/generator/lib.php | 5 ++++- reportbuilder/tests/local/helpers/schedule_test.php | 1 + reportbuilder/upgrade.txt | 5 +++++ 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/reportbuilder/classes/local/helpers/schedule.php b/reportbuilder/classes/local/helpers/schedule.php index 9809bc354c7..dd61418559d 100644 --- a/reportbuilder/classes/local/helpers/schedule.php +++ b/reportbuilder/classes/local/helpers/schedule.php @@ -44,13 +44,14 @@ class schedule { * Create report schedule, calculate when it should be next sent * * @param stdClass $data + * @param int|null $timenow Time to use as comparison against current date (defaults to current time) * @return model */ - public static function create_schedule(stdClass $data): model { + public static function create_schedule(stdClass $data, ?int $timenow = null): model { $data->name = trim($data->name); $schedule = (new model(0, $data)); - $schedule->set('timenextsend', self::calculate_next_send_time($schedule)); + $schedule->set('timenextsend', self::calculate_next_send_time($schedule, $timenow)); return $schedule->create(); } @@ -229,7 +230,7 @@ class schedule { * returned value is after the current date * * @param model $schedule - * @param int|null $timenow Time to use for calculation (defaults to current time) + * @param int|null $timenow Time to use as comparison against current date (defaults to current time) * @return int */ public static function calculate_next_send_time(model $schedule, ?int $timenow = null): int { diff --git a/reportbuilder/tests/generator/lib.php b/reportbuilder/tests/generator/lib.php index 79aae314a57..dd95d89a5e0 100644 --- a/reportbuilder/tests/generator/lib.php +++ b/reportbuilder/tests/generator/lib.php @@ -198,6 +198,9 @@ class core_reportbuilder_generator extends component_generator_base { $record['timescheduled'] = usergetmidnight(time() + DAYSECS); } - return schedule_helper::create_schedule((object) $record); + // Time to use as comparison against current date (null means current time). + $timenow = $record['timenow'] ?? null; + + return schedule_helper::create_schedule((object) $record, $timenow); } } diff --git a/reportbuilder/tests/local/helpers/schedule_test.php b/reportbuilder/tests/local/helpers/schedule_test.php index c45c942e374..c1d9451e6ef 100644 --- a/reportbuilder/tests/local/helpers/schedule_test.php +++ b/reportbuilder/tests/local/helpers/schedule_test.php @@ -404,6 +404,7 @@ class schedule_test extends advanced_testcase { 'name' => 'My schedule', 'recurrence' => $recurrence, 'timescheduled' => $timescheduled, + 'timenow' => $timenow, ]); $this->assertEquals($expected, schedule::calculate_next_send_time($schedule, $timenow)); diff --git a/reportbuilder/upgrade.txt b/reportbuilder/upgrade.txt index fa85716772e..58d71093128 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -1,6 +1,11 @@ This file describes API changes in /reportbuilder/* Information provided here is intended especially for developers. +=== 4.0.7 === + +* The schedule helper `create_schedule` method accepts a `$timenow` parameter to use for comparisons against current date + during tests + === 4.0.6 === * The `datasource_stress_test_columns` test helper now enables sorting on those columns that support it