Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MBS-9327: Add unit test for backlink cache task #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion classes/cachemanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace mod_learningmap;

use core\clock;

/**
* Cache manager class for mod_learningmap
*
Expand Down Expand Up @@ -99,7 +101,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());
}
}

Expand Down
4 changes: 3 additions & 1 deletion classes/task/fill_backlink_cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

namespace mod_learningmap\task;

use core\clock;
use mod_learningmap\cachemanager;

/**
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/mod_learningmap_backlink_cache_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

namespace mod_learningmap;

use core\clock;
use mod_learningmap\task\fill_backlink_cache;
use stdClass;

/**
Expand Down Expand Up @@ -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);
}
}
Loading