Skip to content

Commit

Permalink
Revert to stored apis.json when Moodle version not available
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Feb 23, 2024
1 parent 559d540 commit ddb2325
Show file tree
Hide file tree
Showing 4 changed files with 573 additions and 15 deletions.
272 changes: 272 additions & 0 deletions apis.json
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 10 additions & 3 deletions moodle/Tests/Util/MoodleUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand All @@ -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)
);
}
Expand Down Expand Up @@ -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)
);
}
Expand Down
31 changes: 19 additions & 12 deletions moodle/Util/MoodleUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit ddb2325

Please sign in to comment.