MDL-76792 webservice: Avoid exception when missing component

This commit is contained in:
Juan Leyva
2023-03-29 17:28:05 +02:00
parent f7a8df253b
commit b540a59e55
5 changed files with 71 additions and 4 deletions
+1
View File
@@ -88,3 +88,4 @@ showcalculations,core_grades
showcalculations_help,core_grades
studentsperpage,core_grades
studentsperpage_help,core_grades
missingversionfile,core_webservice
+2 -1
View File
@@ -126,7 +126,6 @@ $string['missingcaps_help'] = 'List of capabilities declared by the service whic
$string['missingpassword'] = 'Missing password';
$string['missingrequiredcapability'] = 'The capability {$a} is required.';
$string['missingusername'] = 'Missing username';
$string['missingversionfile'] = 'Coding error: version.php file is missing for the component {$a}';
$string['nameexists'] = 'This name is already in use by another service';
$string['nocapabilitytouseparameter'] = 'The user does not have the required capability to use the parameter {$a}';
$string['nofunctions'] = 'This service has no functions.';
@@ -235,3 +234,5 @@ $string['wsusername'] = 'Web service username';
// Deprecated since Moodle 3.11.
$string['usernameoridnousererror'] = 'No users were found with this username/user id.';
$string['usernameoridoccurenceerror'] = 'More than one user was found with this username. Please enter the user id.';
// Deprecated since Moodle 4.2.
$string['missingversionfile'] = 'Coding error: version.php file is missing for the component {$a}';
+2 -3
View File
@@ -142,9 +142,8 @@ class core_webservice_external extends \core_external\external_api {
$version = $componentversions[$function->component];
}
} else {
// Function component should always have a version.php,
// otherwise the function should have been described with component => 'moodle'.
throw new moodle_exception('missingversionfile', 'webservice', '', $function->component);
// Ignore this component or plugin, it was probably incorrectly uninstalled.
continue;
}
}
$functioninfo['version'] = $version;
+62
View File
@@ -208,4 +208,66 @@ class externallib_test extends externallib_advanced_testcase {
$this->assertEquals(PHP_INT_MAX, $result['userquota']);
}
/**
* Test get_site_info with missing components.
*/
public function test_get_site_missing_components() {
global $USER, $DB;
$this->resetAfterTest(true);
$this->setAdminUser();
// Add a web service and token.
$webservice = new \stdClass();
$webservice->name = 'Test web service';
$webservice->enabled = true;
$webservice->restrictedusers = false;
$webservice->component = 'moodle';
$webservice->timecreated = time();
$webservice->downloadfiles = true;
$webservice->uploadfiles = true;
$externalserviceid = $DB->insert_record('external_services', $webservice);
// Add a function to the service (missing plugin).
$DB->insert_record('external_functions',
[
'component' => 'mod_random',
'name' => 'mod_random_get_info'
]
);
// Insert one from missing component.
$DB->insert_record('external_services_functions',
[
'externalserviceid' => $externalserviceid,
'functionname' => 'mod_random_get_info'
]
);
// Insert a core one.
$DB->insert_record('external_services_functions',
[
'externalserviceid' => $externalserviceid,
'functionname' => 'core_user_get_users'
]
);
$_POST['wstoken'] = 'testtoken';
$externaltoken = new \stdClass();
$externaltoken->token = 'testtoken';
$externaltoken->tokentype = 0;
$externaltoken->userid = $USER->id;
$externaltoken->externalserviceid = $externalserviceid;
$externaltoken->contextid = 1;
$externaltoken->creatorid = $USER->id;
$externaltoken->timecreated = time();
$DB->insert_record('external_tokens', $externaltoken);
// Execution should complete.
$result = \core_webservice_external::get_site_info();
$result = external_api::clean_returnvalue(\core_webservice_external::get_site_info_returns(), $result);
// Check we ignore the missing component function.
$this->assertCount(1, $result['functions']);
$this->assertEquals('core_user_get_users', $result['functions'][0]['name']);
}
}
+4
View File
@@ -3,6 +3,10 @@ information provided here is intended especially for developers.
This information is intended for authors of webservices, not people writing webservice clients.
=== 4.2 ===
* External function core_webservice_external::get_site_info() does not throw exceptions for missing components anymore.
=== 4.1 ===
* The XMLRPC webservice (protocol) has been completely removed. It's now available in the plugins directory.