MDL-72138 task: allow running core tasks from schedules UI.

Fixes regression from 9c4510a3 that didn't consider tasks belonging
to core, rather than just those belonging to plugins.
This commit is contained in:
Paul Holden 2021-07-12 12:03:16 +01:00
parent 0d0e66d37c
commit 58c6afb94d
2 changed files with 54 additions and 2 deletions

View File

@ -24,6 +24,9 @@
*/
namespace core\task;
use core_component;
use core_plugin_manager;
/**
* Abstract class for common properties of scheduled_task and adhoc_task.
*
@ -236,7 +239,15 @@ abstract class task_base {
* @return bool true when enabled. false otherwise.
*/
public function is_component_enabled(): bool {
$plugininfo = \core_plugin_manager::instance()->get_plugin_info($this->get_component());
return $plugininfo && $plugininfo->is_enabled();
$component = $this->get_component();
// An entire core component type cannot be explicitly disabled.
[$componenttype] = core_component::normalize_component($component);
if ($componenttype === 'core') {
return true;
} else {
$plugininfo = core_plugin_manager::instance()->get_plugin_info($component);
return $plugininfo && $plugininfo->is_enabled();
}
}
}

View File

@ -772,4 +772,45 @@ class core_scheduled_task_testcase extends advanced_testcase {
// Confirm, that lastruntime is still in place.
$this->assertEquals(123456789, $taskafterreset->get_last_run_time());
}
/**
* Data provider for {@see test_is_component_enabled}
*
* @return array[]
*/
public function is_component_enabled_provider(): array {
return [
'Enabled component' => ['auth_cas', true],
'Disabled component' => ['auth_ldap', false],
'Invalid component' => ['auth_invalid', false],
];
}
/**
* Tests whether tasks belonging to components consider the component to be enabled
*
* @param string $component
* @param bool $expected
*
* @dataProvider is_component_enabled_provider
*/
public function test_is_component_enabled(string $component, bool $expected): void {
$this->resetAfterTest();
// Set cas as the only enabled auth component.
set_config('auth', 'cas');
$task = new \core\task\scheduled_test_task();
$task->set_component($component);
$this->assertEquals($expected, $task->is_component_enabled());
}
/**
* Test whether tasks belonging to core components considers the component to be enabled
*/
public function test_is_component_enabled_core(): void {
$task = new \core\task\scheduled_test_task();
$this->assertTrue($task->is_component_enabled());
}
}