From 84d831318fc65182cfc706c1c364196a9d59e7c3 Mon Sep 17 00:00:00 2001 From: sam marshall Date: Mon, 16 Sep 2024 10:26:54 +0100 Subject: [PATCH] MDL-83163 core_course: Fresh install fails if dependency on format If any plugin had dependency on a course format, Moodle fresh install showed dependency errors (even though course format exists). --- course/lib.php | 6 +++++- course/tests/courselib_test.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/course/lib.php b/course/lib.php index 7c476717bc6..0e9a4f35835 100644 --- a/course/lib.php +++ b/course/lib.php @@ -3352,7 +3352,11 @@ function include_course_editor(course_format $format) { function get_sorted_course_formats($enabledonly = false) { global $CFG; - $formats = core_plugin_manager::instance()->get_installed_plugins('format'); + // Include both formats that exist on disk (but might not have been installed yet), and those + // which were installed but no longer exist on disk. + $installedformats = core_plugin_manager::instance()->get_installed_plugins('format'); + $existingformats = core_component::get_plugin_list('format'); + $formats = array_merge($installedformats, $existingformats); if (!empty($CFG->format_plugins_sortorder)) { $order = explode(',', $CFG->format_plugins_sortorder); diff --git a/course/tests/courselib_test.php b/course/tests/courselib_test.php index 15e74c1a46d..095e93e7a91 100644 --- a/course/tests/courselib_test.php +++ b/course/tests/courselib_test.php @@ -7488,4 +7488,36 @@ class courselib_test extends advanced_testcase { $course = get_course($course->id); $this->assertEquals($course->fullname, $data->fullname); } + + /** + * Tests get_sorted_course_formats returns all plugins in cases where plugins are installed now, + * installed previously but no longer exist, or not installed yet. + * + * @covers ::get_sorted_course_formats() + */ + public function test_get_sorted_course_formats_installed_or_not(): void { + global $DB; + + $this->resetAfterTest(); + + // By default returns all course formats. + $formats = get_sorted_course_formats(); + $this->assertEquals(['singleactivity', 'social', 'topics', 'weeks'], $formats); + + // If there is an extra format installed that no longer exists, include in list (at start). + $DB->insert_record('config_plugins', [ + 'plugin' => 'format_frogs', + 'name' => 'version', + 'value' => '20240916', + ]); + \core_plugin_manager::reset_caches(); + $formats = get_sorted_course_formats(); + $this->assertEquals(['frogs', 'singleactivity', 'social', 'topics', 'weeks'], $formats); + + // If one of the formats is not installed yet, we still return it. + $DB->delete_records('config_plugins', ['plugin' => 'format_weeks']); + \core_plugin_manager::reset_caches(); + $formats = get_sorted_course_formats(); + $this->assertEquals(['frogs', 'singleactivity', 'social', 'topics', 'weeks'], $formats); + } }