diff --git a/reportbuilder/classes/local/helpers/schedule.php b/reportbuilder/classes/local/helpers/schedule.php index 38d2df19c27..3d6942619ad 100644 --- a/reportbuilder/classes/local/helpers/schedule.php +++ b/reportbuilder/classes/local/helpers/schedule.php @@ -43,13 +43,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(); } @@ -228,7 +229,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 c31228682fc..dc7cbdf049c 100644 --- a/reportbuilder/tests/generator/lib.php +++ b/reportbuilder/tests/generator/lib.php @@ -205,6 +205,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 7b822ac12bd..da502fc9e4d 100644 --- a/reportbuilder/upgrade.txt +++ b/reportbuilder/upgrade.txt @@ -10,6 +10,8 @@ Information provided here is intended especially for developers. - `data-force-table` to force table view * New optional parameter `pagesize` in external method `core_reportbuilder_reports_get` to set the displayed rows per page. * Javascript reports repository module method `getReport` updated to accept new pagesize parameter. +* The schedule helper `create_schedule` method accepts a `$timenow` parameter to use for comparisons against current date + during tests * The `datasource_stress_test_columns` test helper now enables sorting on those columns that support it * The `create_[column|filter|condition]` test generator methods now allow for setting all persistent properties