From 928323a779c3bed1729fa84abdc03448c016f610 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Fri, 23 Feb 2024 17:03:41 +0800 Subject: [PATCH] Revert to stored apis.json when Moodle version not available --- moodle/Tests/Util/MoodleUtilTest.php | 13 +- moodle/Util/MoodleUtil.php | 31 +-- moodle/Util/apis.json | 272 +++++++++++++++++++++++++++ 3 files changed, 301 insertions(+), 15 deletions(-) create mode 100644 moodle/Util/apis.json diff --git a/moodle/Tests/Util/MoodleUtilTest.php b/moodle/Tests/Util/MoodleUtilTest.php index fe41b3b..1cb19ac 100644 --- a/moodle/Tests/Util/MoodleUtilTest.php +++ b/moodle/Tests/Util/MoodleUtilTest.php @@ -839,7 +839,9 @@ public function testGetMoodleApisNoApis(): void { $phpcsRuleset = new Ruleset($phpcsConfig); $file = new File($vfs->url() . '/lib/lib.php', $phpcsRuleset, $phpcsConfig); - $this->assertNull( + $apis = json_decode(file_get_contents(__DIR__ . '/../../Util/apis.json')); + $this->assertEquals( + array_keys((array) $apis), MoodleUtil::getMoodleApis($file) ); } @@ -865,7 +867,10 @@ public function testGetMoodleApisInvalidJson(): void { $phpcsRuleset = new Ruleset($phpcsConfig); $file = new File($vfs->url() . '/lib/lib.php', $phpcsRuleset, $phpcsConfig); - $this->assertNull( + // Revert to the stored version if the file is not readable. + $apis = json_decode(file_get_contents(__DIR__ . '/../../Util/apis.json')); + $this->assertEquals( + array_keys((array) $apis), MoodleUtil::getMoodleApis($file) ); } @@ -907,7 +912,9 @@ public function testGetMoodleApisNotAMoodle(): void { $phpcsRuleset = new Ruleset($phpcsConfig); $file = new File($vfs->url() . '/lib/lib.php', $phpcsRuleset, $phpcsConfig); - $this->assertNull( + $apis = json_decode(file_get_contents(__DIR__ . '/../../Util/apis.json')); + $this->assertEquals( + array_keys((array) $apis), MoodleUtil::getMoodleApis($file) ); } diff --git a/moodle/Util/MoodleUtil.php b/moodle/Util/MoodleUtil.php index 55bad11..e943c45 100644 --- a/moodle/Util/MoodleUtil.php +++ b/moodle/Util/MoodleUtil.php @@ -274,23 +274,30 @@ public static function getMoodleApis(File $file, bool $selfPath = true): ?array if (empty(self::$apis)) { // Verify that we are able to find a valid moodle root. - if (!$moodleRoot = self::getMoodleRoot($file, $selfPath)) { - return null; + if ($moodleRoot = self::getMoodleRoot($file, $selfPath)) { + // APIs are located in lib/apis.json. + $apisFile = $moodleRoot . '/lib/apis.json'; + + if (is_readable($apisFile)) { + $data = json_decode(file_get_contents($apisFile), true); + if (json_last_error() === JSON_ERROR_NONE) { + self::$apis = $data; + } + } } - // APIs are located in lib/apis.json. - $apisFile = $moodleRoot . '/lib/apis.json'; + if (empty(self::$apis)) { + // If there is no apis.json file, we can't load the current APIs. + // Load the version from the release of 4.2 when the file was introduced. + $apisFile = __DIR__ . '/apis.json'; - if (!is_readable($apisFile)) { - return null; - } + $data = json_decode(file_get_contents($apisFile), true); + if (json_last_error() !== JSON_ERROR_NONE) { + return null; // @codeCoverageIgnore + } - $data = json_decode(file_get_contents($apisFile), true); - if (json_last_error() !== JSON_ERROR_NONE) { - return null; + self::$apis = $data; } - - self::$apis = $data; } return array_keys(self::$apis); diff --git a/moodle/Util/apis.json b/moodle/Util/apis.json new file mode 100644 index 0000000..8d91b22 --- /dev/null +++ b/moodle/Util/apis.json @@ -0,0 +1,272 @@ +{ + "access": { + "component": "core_access", + "allowedlevel2": true, + "allowedspread": false + }, + "admin": { + "component": "core_admin", + "allowedlevel2": false, + "allowedspread": false + }, + "adminpresets": { + "component": "core_adminpresets", + "allowedlevel2": true, + "allowedspread": false + }, + "analytics": { + "component": "core_analytics", + "allowedlevel2": true, + "allowedspread": true + }, + "availability": { + "component": "core_availability", + "allowedlevel2": false, + "allowedspread": false + }, + "backup": { + "component": "core_backup", + "allowedlevel2": true, + "allowedspread": true + }, + "badges": { + "component": "core_badges", + "allowedlevel2": false, + "allowedspread": false + }, + "cache": { + "component": "core_cache", + "allowedlevel2": true, + "allowedspread": true + }, + "calendar": { + "component": "core_calendar", + "allowedlevel2": false, + "allowedspread": false + }, + "check": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "comment": { + "component": "core_comment", + "allowedlevel2": false, + "allowedspread": false + }, + "competency": { + "component": "core_competency", + "allowedlevel2": false, + "allowedspread": false + }, + "completion": { + "component": "core_completion", + "allowedlevel2": true, + "allowedspread": true + }, + "context": { + "component": "core", + "allowedlevel2": true, + "allowedspread": false + }, + "core": { + "component": null, + "allowedlevel2": false, + "allowedspread": false + }, + "customfield": { + "component": "core_customfield", + "allowedlevel2": true, + "allowedspread": true + }, + "ddl": { + "component": "core", + "allowedlevel2": true, + "allowedspread": false + }, + "dml": { + "component": "core", + "allowedlevel2": true, + "allowedspread": false + }, + "enrol": { + "component": "core_enrol", + "allowedlevel2": false, + "allowedspread": false + }, + "event": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "external": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "files": { + "component": "core_files", + "allowedlevel2": true, + "allowedspread": false + }, + "form": { + "component": "core_form", + "allowedlevel2": true, + "allowedspread": true + }, + "grade": { + "component": "core_grades", + "allowedlevel2": false, + "allowedspread": false + }, + "grading": { + "component": "core_grading", + "allowedlevel2": false, + "allowedspread": false + }, + "group": { + "component": "core_group", + "allowedlevel2": false, + "allowedspread": false + }, + "h5p": { + "component": "core_h5p", + "allowedlevel2": true, + "allowedspread": true + }, + "lock": { + "component": "core", + "allowedlevel2": true, + "allowedspread": false + }, + "log": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "media": { + "component": "core_media", + "allowedlevel2": false, + "allowedspread": false + }, + "message": { + "component": "core_message", + "allowedlevel2": true, + "allowedspread": true + }, + "moodlenet": { + "component": "core", + "allowedlevel2": false, + "allowedspread": false + }, + "navigation": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "oauth2": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "output": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "page": { + "component": "core", + "allowedlevel2": false, + "allowedspread": false + }, + "payment": { + "component": "core_payment", + "allowedlevel2": true, + "allowedspread": true + }, + "plagiarism": { + "component": "core_plagiarism", + "allowedlevel2": false, + "allowedspread": false + }, + "portfolio": { + "component": "core_portfolio", + "allowedlevel2": false, + "allowedspread": false + }, + "preference": { + "component": "core", + "allowedlevel2": false, + "allowedspread": false + }, + "privacy": { + "component": "core_privacy", + "allowedlevel2": true, + "allowedspread": true + }, + "question": { + "component": "core_question", + "allowedlevel2": true, + "allowedspread": true + }, + "rating": { + "component": "core_rating", + "allowedlevel2": false, + "allowedspread": false + }, + "reportbuilder": { + "component": "core_reportbuilder", + "allowedlevel2": true, + "allowedspread": true + }, + "rss": { + "component": "core_rss", + "allowedlevel2": false, + "allowedspread": false + }, + "search": { + "component": "core_search", + "allowedlevel2": true, + "allowedspread": true + }, + "string": { + "component": "core", + "allowedlevel2": false, + "allowedspread": false + }, + "tag": { + "component": "core_tag", + "allowedlevel2": false, + "allowedspread": false + }, + "task": { + "component": "core", + "allowedlevel2": true, + "allowedspread": true + }, + "test": { + "component": "core", + "allowedlevel2": false, + "allowedspread": false + }, + "time": { + "component": "core", + "allowedlevel2": false, + "allowedspread": false + }, + "upgrade": { + "component": "core", + "allowedlevel2": true, + "allowedspread": false + }, + "webservice": { + "component": "core_webservice", + "allowedlevel2": false, + "allowedspread": false + }, + "xapi": { + "component": "core_xapi", + "allowedlevel2": true, + "allowedspread": true + } +}