diff --git a/lib/classes/task/task_base.php b/lib/classes/task/task_base.php index 2b877556e2a..cfe202db99f 100644 --- a/lib/classes/task/task_base.php +++ b/lib/classes/task/task_base.php @@ -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(); + } } } diff --git a/lib/tests/scheduled_task_test.php b/lib/tests/scheduled_task_test.php index b15a7a01edf..4a54d61cd24 100644 --- a/lib/tests/scheduled_task_test.php +++ b/lib/tests/scheduled_task_test.php @@ -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()); + } }