diff --git a/classes/cachemanager.php b/classes/cachemanager.php index dbb4972..1336375 100644 --- a/classes/cachemanager.php +++ b/classes/cachemanager.php @@ -16,6 +16,8 @@ namespace mod_learningmap; +use core\clock; + /** * Cache manager class for mod_learningmap * @@ -104,7 +106,8 @@ public static function build_backlink_cache(int $courseid = 0) { if (empty($courseid)) { // Finally set the flag to indicate that the cache is properly built. - $cache->set('fillstate', time()); + $clock = \core\di::get(clock::class); + $cache->set('fillstate', $clock->time()); } } diff --git a/classes/task/fill_backlink_cache.php b/classes/task/fill_backlink_cache.php index 3274bbb..bcf3307 100644 --- a/classes/task/fill_backlink_cache.php +++ b/classes/task/fill_backlink_cache.php @@ -16,6 +16,7 @@ namespace mod_learningmap\task; +use core\clock; use mod_learningmap\cachemanager; /** @@ -48,8 +49,9 @@ public function execute() { $fillstate = $cache->get('fillstate'); + $clock = \core\di::get(clock::class); // If the cache is filled within the last 24 hours, do nothing. - if (!empty($fillstate) && $fillstate > time() - 60 * 60 * 24) { + if (!empty($fillstate) && $fillstate > $clock->time() - 60 * 60 * 24) { mtrace('Backlink cache is already filled within the last 24 hours. Exiting.'); return; } diff --git a/tests/mod_learningmap_backlink_cache_test.php b/tests/mod_learningmap_backlink_cache_test.php index 07f2576..a2353e4 100644 --- a/tests/mod_learningmap_backlink_cache_test.php +++ b/tests/mod_learningmap_backlink_cache_test.php @@ -16,6 +16,8 @@ namespace mod_learningmap; +use core\clock; +use mod_learningmap\task\fill_backlink_cache; use stdClass; /** @@ -225,4 +227,40 @@ public function test_backlink_generation(): void { $descriptionafter = $PAGE->activityheader->export_for_template($OUTPUT)['description']; $this->assertEquals($descriptionbefore, $descriptionafter); } + + /** + * Tests if the nightly task only runs the whole rebuild every 24h. + * + * @covers \mod_learningmap\task\fill_backlink_cache + */ + public function test_backlink_cache_rebuild_task(): void { + $this->resetAfterTest(); + $cache = \cache::make('mod_learningmap', 'backlinks'); + // Set clock to current time. + $clock = $this->mock_clock_with_frozen(time()); + $task = new fill_backlink_cache(); + ob_start(); + $task->execute(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertStringContainsString('Building backlink cache started', $output); + + // We now have built the backlink cache. + // We reset the clock to a time which is less than 24h hours in the future. + $clock->set_to($clock->time() + 23 * 60 * 60); + + ob_start(); + $task->execute(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertStringContainsString('Backlink cache is already filled within the last 24 hours', $output); + + // We reset the clock to a time which is more than 24h hours in the future. + $clock->set_to($clock->time() + 25 * 60 * 60); + ob_start(); + $task->execute(); + $output = ob_get_contents(); + ob_end_clean(); + $this->assertStringContainsString('Building backlink cache started', $output); + } }