Skip to content

Commit

Permalink
MDL-70520 mod_assign: Use task API to get scheduled task lastruntime
Browse files Browse the repository at this point in the history
  • Loading branch information
golenkovm committed Dec 22, 2020
1 parent 322afa9 commit 449c835
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mod/assign/locallib.php
Expand Up @@ -2539,7 +2539,8 @@ public static function cron() {
// Only ever send a max of one days worth of updates.
$yesterday = time() - (24 * 3600);
$timenow = time();
$lastruntime = $DB->get_field('task_scheduled', 'lastruntime', array('component' => 'mod_assign'));
$task = \core\task\manager::get_scheduled_task(mod_assign\task\cron_task::class);
$lastruntime = $task->get_last_run_time();

// Collect all submissions that require mailing.
// Submissions are included if all are true:
Expand Down
50 changes: 50 additions & 0 deletions mod/assign/tests/locallib_test.php
Expand Up @@ -4201,4 +4201,54 @@ public function test_view_group_override() {
$output3 .= $assign->get_renderer()->render($summary);
$this->assertContains('Friday, 7 June 2019, 5:37 PM', $output3, '', true);
}

/**
* Test that cron task uses task API to get its last run time.
*/
public function test_cron_use_task_api_to_get_lastruntime() {
global $DB;
$this->resetAfterTest();
$course = $this->getDataGenerator()->create_course();

// Create an assignment which allows submissions from 3 days ago.
$assign1 = $this->create_instance($course, [
'duedate' => time() + DAYSECS,
'alwaysshowdescription' => 0,
'allowsubmissionsfromdate' => time() - 3 * DAYSECS,
'intro' => 'This one should not be re-created',
]);

// Create an assignment which allows submissions from 1 day ago.
$assign2 = $this->create_instance($course, [
'duedate' => time() + DAYSECS,
'alwaysshowdescription' => 0,
'allowsubmissionsfromdate' => time() - DAYSECS,
'intro' => 'This one should be re-created',
]);

// Set last run time 2 days ago.
$DB->set_field('task_scheduled', 'lastruntime', time() - 2 * DAYSECS, ['classname' => '\mod_assign\task\cron_task']);

// Remove events to make sure cron will update calendar and re-create one of them.
$params = array('modulename' => 'assign', 'instance' => $assign1->get_instance()->id);
$DB->delete_records('event', $params);
$params = array('modulename' => 'assign', 'instance' => $assign2->get_instance()->id);
$DB->delete_records('event', $params);

// Run cron.
assign::cron();

// Assert that calendar hasn't been updated for the first assignment as it's supposed to be
// updated as part of previous cron runs (allowsubmissionsfromdate is less than lastruntime).
$params = array('modulename' => 'assign', 'instance' => $assign1->get_instance()->id);
$event1 = $DB->get_record('event', $params);
$this->assertEmpty($event1);

// Assert that calendar has been updated for the second assignment
// because its allowsubmissionsfromdate is greater than lastruntime.
$params = array('modulename' => 'assign', 'instance' => $assign2->get_instance()->id);
$event2 = $DB->get_record('event', $params);
$this->assertNotEmpty($event2);
$this->assertSame('This one should be re-created', $event2->description);
}
}

0 comments on commit 449c835

Please sign in to comment.