From 9e71503ab35bf95752bb60e1011e8722f1ff7cc8 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Tue, 19 Dec 2023 22:13:32 +0800 Subject: [PATCH] MDL-66903 core: Add option to get core in list of components Note: This change is backported from MDL-81063. --- lib/classes/component.php | 23 ++++++++++++---------- lib/tests/component_test.php | 38 ++++++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/lib/classes/component.php b/lib/classes/component.php index 3a2f323c325..bb85492c6f4 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -1329,18 +1329,16 @@ $cache = '.var_export($cache, true).'; } /** - * Returns a list of frankenstyle component names. + * Returns a list of frankenstyle component names, including all plugins, subplugins, and subsystems. * - * E.g. - * [ - * 'core_course', - * 'core_message', - * 'mod_assign', - * ... - * ] - * @return array the list of frankenstyle component names. + * Note: By default the 'core' subsystem is not included. + * + * @param bool $includecore Whether to include the 'core' subsystem + * @return string[] the list of frankenstyle component names. */ - public static function get_component_names() : array { + public static function get_component_names( + bool $includecore = false + ): array { $componentnames = []; // Get all plugins. foreach (self::get_plugin_types() as $plugintype => $typedir) { @@ -1352,6 +1350,11 @@ $cache = '.var_export($cache, true).'; foreach (self::get_core_subsystems() as $subsystemname => $subsystempath) { $componentnames[] = 'core_' . $subsystemname; } + + if ($includecore) { + $componentnames[] = 'core'; + } + return $componentnames; } diff --git a/lib/tests/component_test.php b/lib/tests/component_test.php index e55f40a86ef..f11168185e8 100644 --- a/lib/tests/component_test.php +++ b/lib/tests/component_test.php @@ -1052,22 +1052,31 @@ final class component_test extends advanced_testcase { /** * Test the get_component_names() method. + * + * @dataProvider get_component_names_provider + * @param bool $includecore Whether to include core in the list. + * @param bool $coreexpected Whether core is expected to be in the list. */ - public function test_get_component_names() { + public function test_get_component_names( + bool $includecore, + bool $coreexpected + ): void { global $CFG; - $componentnames = \core_component::get_component_names(); + $componentnames = \core_component::get_component_names($includecore); // We should have an entry for each plugin type. $plugintypes = \core_component::get_plugin_types(); $numplugintypes = 0; - foreach ($plugintypes as $type => $typedir) { - foreach (\core_component::get_plugin_list($type) as $plugin) { - $numplugintypes++; - } + foreach (array_keys($plugintypes) as $type) { + $numplugintypes += count(\core_component::get_plugin_list($type)); } // And an entry for each core subsystem. $numcomponents = $numplugintypes + count(\core_component::get_core_subsystems()); + if ($coreexpected) { + // Add one for core. + $numcomponents++; + } $this->assertEquals($numcomponents, count($componentnames)); // Check a few of the known plugin types to confirm their presence at their respective type index. @@ -1075,6 +1084,23 @@ final class component_test extends advanced_testcase { $this->assertContains('mod_forum', $componentnames); $this->assertContains('tool_usertours', $componentnames); $this->assertContains('core_favourites', $componentnames); + if ($coreexpected) { + $this->assertContains('core', $componentnames); + } else { + $this->assertNotContains('core', $componentnames); + } + } + + /** + * Data provider for get_component_names() test. + * + * @return array + */ + public static function get_component_names_provider(): array { + return [ + [false, false], + [true, true], + ]; } /**