Skip to content

Commit

Permalink
Merge branch 'MDL-50532-master' of git://github.com/FMCorz/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Aug 4, 2015
2 parents 45f8f3e + b2f6f88 commit ac41247
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/classes/task/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function load_default_scheduled_tasks_for_component($componentname
}

$tasks = null;
require_once($file);
include($file);

if (!isset($tasks)) {
return array();
Expand Down Expand Up @@ -86,13 +86,16 @@ public static function load_default_scheduled_tasks_for_component($componentname
public static function reset_scheduled_tasks_for_component($componentname) {
global $DB;
$tasks = self::load_default_scheduled_tasks_for_component($componentname);
$validtasks = array();

foreach ($tasks as $taskid => $task) {
$classname = get_class($task);
if (strpos($classname, '\\') !== 0) {
$classname = '\\' . $classname;
}

$validtasks[] = $classname;

if ($currenttask = self::get_scheduled_task($classname)) {
if ($currenttask->is_customised()) {
// If there is an existing task with a custom schedule, do not override it.
Expand All @@ -110,6 +113,16 @@ public static function reset_scheduled_tasks_for_component($componentname) {
$DB->insert_record('task_scheduled', $record);
}
}

// Delete any task that is not defined in the component any more.
$sql = "component = :component";
$params = array('component' => $componentname);
if (!empty($validtasks)) {
list($insql, $inparams) = $DB->get_in_or_equal($validtasks, SQL_PARAMS_NAMED, 'param', false);
$sql .= ' AND classname ' . $insql;
$params = array_merge($params, $inparams);
}
$DB->delete_records_select('task_scheduled', $sql, $params);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions lib/tests/scheduled_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,47 @@ public function test_reset_scheduled_tasks_for_component() {
$this->assertEquals($initcount, $finalcount);
}

/**
* Tests that the reset function deletes old tasks.
*/
public function test_reset_scheduled_tasks_for_component_delete() {
global $DB;
$this->resetAfterTest(true);

$count = $DB->count_records('task_scheduled', array('component' => 'moodle'));
$allcount = $DB->count_records('task_scheduled');

$task = new \core\task\scheduled_test_task();
$task->set_component('moodle');
$record = \core\task\manager::record_from_scheduled_task($task);
$DB->insert_record('task_scheduled', $record);
$this->assertTrue($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test_task',
'component' => 'moodle')));

$task = new \core\task\scheduled_test2_task();
$task->set_component('moodle');
$record = \core\task\manager::record_from_scheduled_task($task);
$DB->insert_record('task_scheduled', $record);
$this->assertTrue($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test2_task',
'component' => 'moodle')));

$aftercount = $DB->count_records('task_scheduled', array('component' => 'moodle'));
$afterallcount = $DB->count_records('task_scheduled');

$this->assertEquals($count + 2, $aftercount);
$this->assertEquals($allcount + 2, $afterallcount);

// Now check that the right things were deleted.
\core\task\manager::reset_scheduled_tasks_for_component('moodle');

$this->assertEquals($count, $DB->count_records('task_scheduled', array('component' => 'moodle')));
$this->assertEquals($allcount, $DB->count_records('task_scheduled'));
$this->assertFalse($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test2_task',
'component' => 'moodle')));
$this->assertFalse($DB->record_exists('task_scheduled', array('classname' => '\core\task\scheduled_test_task',
'component' => 'moodle')));
}

public function test_get_next_scheduled_task() {
global $DB;

Expand Down

0 comments on commit ac41247

Please sign in to comment.