From 639bc8b2f33d50f9c3f1a67fadb99f01a97b7216 Mon Sep 17 00:00:00 2001 From: Paul Charsley Date: Wed, 5 Sep 2012 13:05:43 +1200 Subject: [PATCH] MDL-34962 added release number to returned values --- webservice/externallib.php | 69 +++++++++++++-------- webservice/tests/externallib_test.php | 88 +++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 27 deletions(-) create mode 100644 webservice/tests/externallib_test.php diff --git a/webservice/externallib.php b/webservice/externallib.php index 4effcde1d49..2a3c62ab191 100644 --- a/webservice/externallib.php +++ b/webservice/externallib.php @@ -24,6 +24,10 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ +defined('MOODLE_INTERNAL') || die; + +require_once("$CFG->libdir/externallib.php"); + /** * Web service related functions * @@ -47,7 +51,8 @@ class core_webservice_external extends external_api { new external_value( PARAM_ALPHANUMEXT, 'service shortname'), - 'DEPRECATED PARAMETER - it was a design error in the original implementation. It is ignored now. (parameter kept for backward compatibility)', + 'DEPRECATED PARAMETER - it was a design error in the original implementation. \ + It is ignored now. (parameter kept for backward compatibility)', VALUE_DEFAULT, array() ), @@ -60,20 +65,21 @@ class core_webservice_external extends external_api { * Note: * - no capability checking because we return only known information about logged user * - * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored - it was an original design error, we keep for backward compatibility. + * @param array $serviceshortnames - DEPRECATED PARAMETER - values will be ignored - + * it was an original design error, we keep for backward compatibility. * @return array site info * @since Moodle 2.2 */ - public function get_site_info($serviceshortnames = array()) { + public static function get_site_info($serviceshortnames = array()) { global $USER, $SITE, $CFG, $DB; $params = self::validate_parameters(self::get_site_info_parameters(), array('serviceshortnames'=>$serviceshortnames)); $profileimageurl = moodle_url::make_pluginfile_url( - context_user::instance($USER->id)->id, 'user', 'icon', NULL, '/', 'f1'); + context_user::instance($USER->id)->id, 'user', 'icon', null, '/', 'f1'); - //site information + // Site information. $siteinfo = array( 'sitename' => $SITE->fullname, 'siteurl' => $CFG->wwwroot, @@ -85,16 +91,16 @@ class core_webservice_external extends external_api { 'userpictureurl' => $profileimageurl->out(false) ); - //Retrieve the service and functions from the web service linked to the token - //If you call this function directly from external (not a web service call), - //then it will still return site info without information about a service - //Note: wsusername/wspassword ws authentication is not supported. + // Retrieve the service and functions from the web service linked to the token + // If you call this function directly from external (not a web service call), + // then it will still return site info without information about a service + // Note: wsusername/wspassword ws authentication is not supported. $functions = array(); - if ($CFG->enablewebservices) { //no need to check token if web service are disabled and not a ws call + if ($CFG->enablewebservices) { // No need to check token if web service are disabled and not a ws call. $token = optional_param('wstoken', '', PARAM_ALPHANUM); - if (!empty($token)) { //no need to run if not a ws call - //retrieve service shortname + if (!empty($token)) { // No need to run if not a ws call. + // Retrieve service shortname. $servicesql = 'SELECT s.* FROM {external_services} s, {external_tokens} t WHERE t.externalserviceid = s.id AND token = ? AND t.userid = ? AND s.enabled = 1'; @@ -103,29 +109,33 @@ class core_webservice_external extends external_api { $siteinfo['downloadfiles'] = $service->downloadfiles; if (!empty($service)) { - //retrieve the functions + // Return the release and version number for web service users only. + $siteinfo['release'] = $CFG->release; + $siteinfo['version'] = $CFG->version; + // Retrieve the functions. $functionssql = "SELECT f.* FROM {external_functions} f, {external_services_functions} sf WHERE f.name = sf.functionname AND sf.externalserviceid = ?"; $functions = $DB->get_records_sql($functionssql, array($service->id)); } else { - throw new coding_exception('No service found in get_site_info: something is buggy, it should have fail at the ws server authentication layer.'); + throw new coding_exception('No service found in get_site_info: something is buggy, \ + it should have fail at the ws server authentication layer.'); } } } - //built up the returned values of the list of functions + // Build up the returned values of the list of functions. $componentversions = array(); - $avalaiblefunctions = array(); + $availablefunctions = array(); foreach ($functions as $function) { $functioninfo = array(); $functioninfo['name'] = $function->name; - if ($function->component == 'moodle') { - $version = $CFG->version; //moodle version + if ($function->component == 'moodle' || $function->component == 'core') { + $version = $CFG->version; // Moodle version. } else { $versionpath = get_component_directory($function->component).'/version.php'; if (is_readable($versionpath)) { - //we store the component version once retrieved (so we don't load twice the version.php) + // We store the component version once retrieved (so we don't load twice the version.php). if (!isset($componentversions[$function->component])) { include($versionpath); $componentversions[$function->component] = $plugin->version; @@ -134,16 +144,16 @@ class core_webservice_external extends 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' + // 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); } } $functioninfo['version'] = $version; - $avalaiblefunctions[] = $functioninfo; + $availablefunctions[] = $functioninfo; } - $siteinfo['functions'] = $avalaiblefunctions; + $siteinfo['functions'] = $availablefunctions; return $siteinfo; } @@ -167,16 +177,21 @@ class core_webservice_external extends external_api { 'userpictureurl' => new external_value(PARAM_URL, 'the user profile picture. Warning: this url is the public URL that only works when forcelogin is set to NO and guestaccess is set to YES. In order to retrieve user profile pictures independently of the Moodle config, replace "pluginfile.php" by - "webservice/pluginfile.php?token=WSTOKEN&file=". Of course the user can only see profile picture depending on his/her permissions. - Moreover it is recommended to use HTTPS too.'), + "webservice/pluginfile.php?token=WSTOKEN&file=" + Of course the user can only see profile picture depending + on his/her permissions. Moreover it is recommended to use HTTPS too.'), 'functions' => new external_multiple_structure( new external_single_structure( array( 'name' => new external_value(PARAM_RAW, 'function name'), - 'version' => new external_value(PARAM_FLOAT, 'The version number of moodle site/local plugin linked to the function') + 'version' => new external_value(PARAM_FLOAT, + 'The version number of the component to which the function belongs') ), 'functions that are available') ), - 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not', VALUE_OPTIONAL), + 'downloadfiles' => new external_value(PARAM_INT, '1 if users are allowed to download files, 0 if not', + VALUE_OPTIONAL), + 'release' => new external_value(PARAM_TEXT, 'Moodle release number', VALUE_OPTIONAL), + 'version' => new external_value(PARAM_TEXT, 'Moodle version number', VALUE_OPTIONAL) ) ); } diff --git a/webservice/tests/externallib_test.php b/webservice/tests/externallib_test.php new file mode 100644 index 00000000000..2ed14ddb095 --- /dev/null +++ b/webservice/tests/externallib_test.php @@ -0,0 +1,88 @@ +. + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; + +require_once($CFG->dirroot . '/webservice/tests/helpers.php'); + +/** + * External course functions unit tests + * + * @package core_webservice + * @category external + * @copyright 2012 Paul Charsley + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class core_webservice_external_testcase extends externallib_advanced_testcase { + + /** + * Tests set up + */ + protected function setUp() { + global $CFG; + require_once($CFG->dirroot . '/webservice/externallib.php'); + $_POST = array(); + // Enable the webservices and set a release number. + set_config('enablewebservices', '1'); + set_config('release', '2.4dev (Build: 20120823)'); + set_config('version', '2012083100.00'); + } + + public function test_get_site_info() { + global $DB, $USER, $CFG; + + $this->resetAfterTest(true); + $user = array(); + $user['username'] = 'johnd'; + $user['firstname'] = 'John'; + $user['lastname'] = 'Doe'; + self::setUser(self::getDataGenerator()->create_user($user)); + + // 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; + $externalserviceid = $DB->insert_record('external_services', $webservice); + + $_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); + + $siteinfo = core_webservice_external::get_site_info(); + + $this->assertEquals('johnd', $siteinfo['username']); + $this->assertEquals('John', $siteinfo['firstname']); + $this->assertEquals('Doe', $siteinfo['lastname']); + $this->assertEquals($USER->id, $siteinfo['userid']); + $this->assertEquals(true, $siteinfo['downloadfiles']); + $this->assertEquals($CFG->release, $siteinfo['release']); + $this->assertEquals($CFG->version, $siteinfo['version']); + } + +}