Skip to content

Commit

Permalink
Merge branch 'MDL-70427' of git://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jul 22, 2021
2 parents 05db683 + 42281e4 commit 4c9f590
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/adminlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ function uninstall_plugin($type, $name) {
$DB->delete_records('event', ['component' => $component]);

// Delete scheduled tasks.
$DB->delete_records('task_adhoc', ['component' => $component]);
$DB->delete_records('task_scheduled', array('component' => $component));

// Delete Inbound Message datakeys.
Expand Down
22 changes: 22 additions & 0 deletions lib/classes/task/task_base.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ public function set_component($component) {
* @return string
*/
public function get_component() {
if (empty($this->component)) {
// The component should be the root of the class namespace.
$classname = get_class($this);
$parts = explode('\\', $classname);

if (count($parts) === 1) {
$component = substr($classname, 0, strpos($classname, '_task'));
} else {
[$component] = $parts;
}

// Load component information from plugin manager.
if ($component !== 'core' && strpos($component, 'core_') !== 0) {
$plugininfo = \core_plugin_manager::instance()->get_plugin_info($component);
if ($plugininfo && $plugininfo->component) {
$this->set_component($plugininfo->component);
} else {
debugging("Component not set and the class namespace does not match a valid component ({$component}).");
}
}
}

return $this->component;
}

Expand Down
40 changes: 40 additions & 0 deletions lib/tests/adhoc_task_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,46 @@ public function test_get_next_adhoc_task_future() {
\core\task\manager::adhoc_task_complete($task);
}

/**
* Test queueing an adhoc task belonging to a component, where we set the task component accordingly
*/
public function test_queue_adhoc_task_for_component(): void {
$this->resetAfterTest();

$task = new \mod_forum\task\refresh_forum_post_counts();
$task->set_component('mod_test');

\core\task\manager::queue_adhoc_task($task);
$this->assertDebuggingNotCalled();
}

/**
* Test queueing an adhoc task belonging to a component, where we do not set the task component
*/
public function test_queue_task_for_component_without_set_component(): void {
$this->resetAfterTest();

$task = new \mod_forum\task\refresh_forum_post_counts();

\core\task\manager::queue_adhoc_task($task);
$this->assertDebuggingNotCalled();

// Assert the missing component was set.
$this->assertEquals('mod_forum', $task->get_component());
}

/**
* Test queueing an adhoc task belonging to an invalid component, where we do not set the task component
*/
public function test_queue_task_for_invalid_component_without_set_component(): void {
$this->resetAfterTest();

$task = new \mod_fake\task\adhoc_component_task();

\core\task\manager::queue_adhoc_task($task);
$this->assertDebuggingCalled('Component not set and the class namespace does not match a valid component (mod_fake).');
}

/**
* Test empty set of adhoc tasks
*/
Expand Down
8 changes: 8 additions & 0 deletions lib/tests/fixtures/task_fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,11 @@ public function get_name() {
public function execute() {
}
}

namespace mod_fake\task;

class adhoc_component_task extends \core\task\adhoc_task {
public function execute() {

}
}
1 change: 1 addition & 0 deletions lib/tests/task_logging_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,7 @@ public function test_inner_flushed_buffers_are_logged() {
*/
protected function get_test_adhoc_task() : \core\task\adhoc_task {
$task = $this->getMockForAbstractClass(\core\task\adhoc_task::class);
$task->set_component('core');

// Mock a lock on the task.
$lock = $this->getMockBuilder(\core\lock\lock::class)
Expand Down

0 comments on commit 4c9f590

Please sign in to comment.