Skip to content

Commit

Permalink
MDL-12886 refactored ws test client support and other minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Oct 21, 2009
1 parent 06f0784 commit f0dafb3
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 216 deletions.
2 changes: 1 addition & 1 deletion lang/en_utf8/webservice.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@
$string['serviceusersmatching'] = 'Authorized users matching';
$string['serviceuserssettings'] = 'Change settings for the Authorized users';
$string['test'] = 'Test';
$string['testclient'] = 'Test client';
$string['testclient'] = 'Web service test client';
$string['validuntil'] = 'Valid until';
$string['webservices'] = 'Web services';
2 changes: 1 addition & 1 deletion lang/en_utf8/webservice_xmlrpc.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

$string['pluginname'] = 'XMLRPC protocol';
$string['pluginname'] = 'XML-RPC protocol';
21 changes: 20 additions & 1 deletion webservice/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ function __construct($debuginfo) {
}
}

/**
* Is protocol enabled?
* @param string $protocol name of WS protocol
* @return bool
*/
function webservice_protocol_is_enabled($protocol) {
global $CFG;

Expand All @@ -62,6 +67,20 @@ interface webservice_server {
public function run($simple);
}

/**
* Mandatory test client interface.
*/
interface webservice_test_client_interface {
/**
* Execute test client WS request
* @param string $serverurl
* @param string $function
* @param array $params
* @return mixed
*/
public function simpletest($serverurl, $function, $params);
}

/**
* Special abstraction of our srvices that allows
* interaction with stock Zend ws servers.
Expand Down Expand Up @@ -229,6 +248,7 @@ class '.$classname.' {
'.$methods.'
}
';

// load the virtual class definition into memory
eval($code);
$this->service_class = $classname;
Expand Down Expand Up @@ -506,7 +526,6 @@ abstract protected function send_response();
*/
abstract protected function send_error($ex=null);


/**
* Process request from client.
* @param bool $simple use simple authentication
Expand Down
23 changes: 21 additions & 2 deletions webservice/rest/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,11 @@ protected static function xmlize_result($returns, $desc) {
return '';

} else if ($desc instanceof external_value) {
//TODO: there should be some way to indicate the real NULL value
return '<VALUE>'.htmlentities($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
if (is_null($returns)) {
return '<VALUE null="null"/>'."\n";
} else {
return '<VALUE>'.htmlentities($returns, ENT_COMPAT, 'UTF-8').'</VALUE>'."\n";
}

} else if ($desc instanceof external_multiple_structure) {
$mult = '<MULTIPLE>'."\n";
Expand Down Expand Up @@ -153,3 +156,19 @@ protected static function xmlize_result($returns, $desc) {
}
}


/**
* REST test client class
*/
class webservice_rest_test_client implements webservice_test_client_interface {
/**
* Execute test client WS request
* @param string $serverurl
* @param string $function
* @param array $params
* @return mixed
*/
public function simpletest($serverurl, $function, $params) {
return download_file_content($serverurl.'&wsfunction='.$function, null, $params);
}
}
100 changes: 0 additions & 100 deletions webservice/rest/testclient/index.php

This file was deleted.

139 changes: 139 additions & 0 deletions webservice/testclient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Web service test client.
*
* @package webservice
* @copyright 2009 Moodle Pty Ltd (http://moodle.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require('../config.php');
require_once("$CFG->dirroot/webservice/testclient_forms.php");

$function = optional_param('function', '', PARAM_SAFEDIR);
$protocol = optional_param('protocol', '', PARAM_SAFEDIR);

$PAGE->set_url('webservice/testclient.php');

require_login();
require_capability('moodle/site:config', get_context_instance(CONTEXT_SYSTEM)); // TODO: do we need some new capability?
// list of all available functions for testing - please note there must be explicit
// support for testing of each functions, the parameter conversion and form is hardcoded
$functions = array('moodle_group_get_groups');
$functions = array_combine($functions, $functions);
if (!isset($functions[$function])) { // whitelisting security
$function = '';
}

// list all enabled webservices
$available_protocols = get_plugin_list('webservice');
$active_protocols = empty($CFG->webserviceprotocols) ? array() : explode(',', $CFG->webserviceprotocols);
$protocols = array();
foreach ($active_protocols as $p) {
if (empty($available_protocols[$p])) {
continue;
}
$protocols[$p] = get_string('pluginname', 'webservice_'.$p);
}
if (!isset($protocols[$protocol])) { // whitelisting security
$protocol = '';
}

if (!$function or !$protocol) {
$mform = new webservice_test_client_form(null, array($functions, $protocols));
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('testclient', 'webservice'));
$mform->display();
echo $OUTPUT->footer();
die;
}

$class = $function.'_form';

$mform = new $class();
$mform->set_data(array('function'=>$function, 'protocol'=>$protocol));

if ($mform->is_cancelled()) {
redirect('testclient.php');

} else if ($data = $mform->get_data()) {
// remove unused from form data
unset($data->submitbutton);
unset($data->protocol);
unset($data->function);

// first load lib of selected protocol
require_once("$CFG->dirroot/webservice/$protocol/locallib.php");

$testclientclass = "webservice_{$protocol}_test_client";
if (!class_exists($testclientclass)) {
throw new coding_exception('Missing WS test class in protocol '.$protocol);
}
$testclient = new $testclientclass();

$serverurl = "$CFG->wwwroot/webservice/$protocol/simpleserver.php";
$serverurl .= '?wsusername='.urlencode($data->wsusername);
unset($data->wsusername);
$serverurl .= '&wspassword='.urlencode($data->wspassword);
unset($data->wspassword);

// now get the function parameters - each functions processing must be hardcoded here
$params = array();
if ($function === 'moodle_group_get_groups') {
$params['groupids'] = array();
for ($i=0; $i<10; $i++) {
if (empty($data->groupids[$i])) {
continue;
}
$params['groupids'][] = $data->groupids[$i];
}

} else {
throw new coding_exception('Testing of function '.$function.' not implemented yet!');
}

echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);

echo 'URL: '.s($serverurl);
echo $OUTPUT->box_start();
echo '<code>';

try {
$response = $testclient->simpletest($serverurl, $function, $params);
echo str_replace("\n", '<br />', s(var_export($response, true)));
} catch (Exception $ex) {
//TODO: handle exceptions and faults without exposing of the sensitive information such as debug traces!
echo str_replace("\n", '<br />', s($ex));
}

echo '</code>';
echo $OUTPUT->box_end();
$mform->display();
echo $OUTPUT->footer();
die;

} else {
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('pluginname', 'webservice_'.$protocol).': '.$function);
$mform->display();
echo $OUTPUT->footer();
die;
}
8 changes: 6 additions & 2 deletions webservice/testclient_forms.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ public function definition() {
global $CFG;

$mform = $this->_form;
$functions = $this->_customdata;
list($functions, $protocols) = $this->_customdata;

$mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));

$mform->addElement('select', 'protocol', get_string('protocol', 'webservice'), $protocols);

$mform->addElement('select', 'function', get_string('function', 'webservice'), $functions);

$this->add_action_buttons(false, get_string('select'));
Expand All @@ -36,7 +38,9 @@ public function definition() {

$mform->addElement('hidden', 'function');
$mform->setType('function', PARAM_SAFEDIR);
$mform->setDefault('function', 'moodle_group_get_groups');

$mform->addElement('hidden', 'protocol');
$mform->setType('protocol', PARAM_SAFEDIR);

$this->add_action_buttons(true, get_string('test', 'webservice'));
}
Expand Down
21 changes: 21 additions & 0 deletions webservice/xmlrpc/locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,24 @@ public function __construct() {
}
}

/**
* XML-RPC test client class
*/
class webservice_xmlrpc_test_client implements webservice_test_client_interface {
/**
* Execute test client WS request
* @param string $serverurl
* @param string $function
* @param array $params
* @return mixed
*/
public function simpletest($serverurl, $function, $params) {
//zend expects 0 based array with numeric indexes
$params = array_values($params);

include "Zend/Loader.php";
Zend_Loader::registerAutoload();
$client = new Zend_XmlRpc_Client($serverurl);
return $client->call($function, $params);
}
}
Loading

0 comments on commit f0dafb3

Please sign in to comment.