From: jerome Date: Wed, 4 Mar 2009 03:18:14 +0000 (+0000) Subject: web service MDL-12886 better looking documentation, use a lower integer for the mock... X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=fa0797ecf4755a8c76fbe1e3d7ed944efacd0ffb;p=moodle.git web service MDL-12886 better looking documentation, use a lower integer for the mock token --- diff --git a/webservice/documentation.php b/webservice/documentation.php index d2388e6533..2b693a8509 100644 --- a/webservice/documentation.php +++ b/webservice/documentation.php @@ -32,47 +32,12 @@ require_once('lib.php'); $protocol = optional_param('protocol',"soap",PARAM_ALPHA); print_header(get_string('wspagetitle','webservice'), get_string('wspagetitle','webservice').":", true); -check_webservices($protocol); +webservice_lib::display_webservices_availability($protocol); generate_documentation($protocol); generate_functionlist(); print_footer(); -/** - * Check if the Moodle site has the web service protocol enable - * @global object $CFG - * @param string $protocol - */ -function check_webservices($protocol){ - global $CFG; - - echo get_string('webservicesenable','webservice').": "; - if (empty($CFG->enablewebservices)) { - echo "".get_string('fail','webservice').""; - } else { - echo "".get_string('ok','webservice').""; - } - echo "
"; - - foreach(webservice_lib::get_list_protocols() as $wsprotocol) { - if (strtolower($wsprotocol->get_protocolname()) == strtolower($protocol)) { - echo get_string('protocolenable','webservice',array($wsprotocol->get_protocolname())).": "; - if ( get_config($wsprotocol-> get_protocolname(), "enable")) { - echo "".get_string('ok','webservice').""; - } else { - echo "".get_string('fail','webservice').""; - } - echo "
"; - continue; - } - } - //check debugging - if ($CFG->debugdisplay) { - echo "".get_string('debugdisplayon','webservice').""; - } - - -} /** * Generate documentation specific to a protocol diff --git a/webservice/lib.php b/webservice/lib.php index 7e7a8da1ed..a751a861b5 100644 --- a/webservice/lib.php +++ b/webservice/lib.php @@ -70,7 +70,7 @@ final class webservice_lib { */ public static function mock_check_token($token) { //fake test - if ($token == 465465465468468464) { + if ($token == 456) { ///retrieve the user global $DB; $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1)); @@ -134,7 +134,7 @@ final class webservice_lib { ------- * Docnlock: @ subparam string $params:searches->search - the string to search * $params is considered as the first element, searches the second, and search the terminal - * Except the terminal element, all other will be generated as an array + * Except the terminal element, all other will be generated as an array * => left element are generated as an associative array. * If the following character is ':' so the right element is a key named 'multiple:element_name' * If the following character is '->' so the right element will be named 'element_name' @@ -150,7 +150,7 @@ final class webservice_lib { @ return array users @ subreturn integer $users:user->id @ subreturn integer $users:user->auth - + Generated description array --------------------------- description["mock_function"]=> @@ -221,10 +221,10 @@ final class webservice_lib { //retrieve the subparam and subreturn preg_match_all('/\s*\*\s*@(subparam|subreturn)\s+(\w+)\s+(\$\w+(?::\w+|->\w+)+)((?:\s+(?:optional|required|multiple))*)/', $docBlock, $matches); - /// process every @subparam and @subreturn line of the docblock + /// process every @subparam and @subreturn line of the docblock for($i=0;$igetName()])) { $description[$method->getName()] = array(); } - /// directly set description[method][return] if the return value is a primary type + /// directly set description[method][return] if the return value is a primary type if (strpos($returnmatches[1][0] ,"object")===false && strpos($returnmatches[1][0],"array")===false) { $description[$method->getName()]['return'] = array($returnmatches[2][0] => $returnmatches[1][0]); } @@ -258,8 +258,8 @@ final class webservice_lib { ///Part 1. - /// extract the first part into $param (has to be $params in the case of @subparam, or anything in the case of $subreturn) - /// extract the second part + /// extract the first part into $param (has to be $params in the case of @subparam, or anything in the case of $subreturn) + /// extract the second part if (strpos($matches[3][$i], "->")===false || (strpos($matches[3][$i], ":")!==false && (strpos($matches[3][$i], ":") < strpos($matches[3][$i], "->")))) { $separator = ":"; $separatorsize=1; @@ -270,27 +270,27 @@ final class webservice_lib { } $param = substr($matches[3][$i],1,strpos($matches[3][$i], $separator)-1); //first element/part/array - //for example for the line @subparam string $params:students->student->name - // @params is the first element/part/array of this docnlock line - // students is the second element/part/array - // ... - // name is the terminal element, this element will be generated as String here + //for example for the line @subparam string $params:students->student->name + // @params is the first element/part/array of this docnlock line + // students is the second element/part/array + // ... + // name is the terminal element, this element will be generated as String here $otherparam = substr($matches[3][$i],strpos($matches[3][$i], $separator)+$separatorsize); //rest of the line $parsingdesc = $description[$method->getName()]; //$pasingdesc is the current position of the algorythm into the description array - //it is used to check if a element already exist into the description array + //it is used to check if a element already exist into the description array if (!empty($parsingdesc) && array_key_exists($descriptiontype, $parsingdesc)){ $parsingdesc = $parsingdesc[$descriptiontype]; } $descriptionpath=array(); //we save in this variable the description path (e.g all keys to go deep into the description array) - //it will be used to know where to add a new part the description array + //it will be used to know where to add a new part the description array $creationfinished = false; //it's used to stop the algorythm when we find a new element that we can add to the descripitoin unset($type); - /// try to extract the other elements and add them to the descripition id there are not already in the description + /// try to extract the other elements and add them to the descripition id there are not already in the description while(!$creationfinished && (strpos($otherparam, ":") || strpos($otherparam, "->"))) { if (strpos($otherparam, "->")===false || (strpos($otherparam, ":")!==false && (strpos($otherparam, ":") < strpos($otherparam, "->")))) { $type = $separator; @@ -325,7 +325,7 @@ final class webservice_lib { } webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); $creationfinished = true; // we do not want to keep going to parse this line, - // neither add again the terminal element of the line to the descripiton + // neither add again the terminal element of the line to the descripiton } else { if(empty($descriptionpath)) { $descriptionpath[] = $descriptiontype; @@ -353,7 +353,7 @@ final class webservice_lib { webservice_lib::add_end_of_description($paramtoadd, $desctoadd, $description[$method->getName()], $descriptionpath); $creationfinished = true; // we do not want to keep going to parse this line, - // neither add again the terminal element of the line to the descripiton + // neither add again the terminal element of the line to the descripiton } else { if(empty($descriptionpath)) { $descriptionpath[] = $descriptiontype; @@ -364,7 +364,7 @@ final class webservice_lib { } } - /// Add the "terminal" element of the line to the description array + /// Add the "terminal" element of the line to the description array if (!$creationfinished) { if (!empty($type) && $type==":") { @@ -400,9 +400,9 @@ final class webservice_lib { } } -// echo "
";
-//        var_dump($description);
-//        echo "
"; + // echo "
";
+        //        var_dump($description);
+        //        echo "
"; return $description; } @@ -481,6 +481,48 @@ final class webservice_lib { } + /** + * Check if the Moodle site has the web service protocol enable + * @global object $CFG + * @param string $protocol + */ + function display_webservices_availability($protocol){ + global $CFG; + + $available = true; + + echo get_string('webservicesenable','webservice').": "; + if (empty($CFG->enablewebservices)) { + echo "".get_string('fail','webservice').""; + $available = false; + } else { + echo "".get_string('ok','webservice').""; + } + echo "
"; + + foreach(webservice_lib::get_list_protocols() as $wsprotocol) { + if (strtolower($wsprotocol->get_protocolname()) == strtolower($protocol)) { + echo get_string('protocolenable','webservice',array($wsprotocol->get_protocolname())).": "; + if ( get_config($wsprotocol-> get_protocolname(), "enable")) { + echo "".get_string('ok','webservice').""; + } else { + echo "".get_string('fail','webservice').""; + $available = false; + } + echo "
"; + continue; + } + } + + //check debugging + if ($CFG->debugdisplay) { + echo "".get_string('debugdisplayon','webservice').""; + $available = false; + } + + return $available; + } + } /** @@ -528,7 +570,7 @@ class ws_authentication { */ function tmp_get_token($params) { if ($params['username'] == 'wsuser' && $params['password'] == 'wspassword') { - return '465465465468468464'; + return '456'; } else { throw new moodle_exception('wrongusernamepassword'); } diff --git a/webservice/rest/locallib.php b/webservice/rest/locallib.php index 8c17ffeb24..39b1f1d0ec 100644 --- a/webservice/rest/locallib.php +++ b/webservice/rest/locallib.php @@ -57,7 +57,7 @@ function call_moodle_function ($rest_arguments) { } else { /// TODO: authentication + token generation need to be implemented if (optional_param('username',null,PARAM_ALPHANUM) == 'wsuser' && optional_param('password',null,PARAM_ALPHANUM) == 'wspassword') { - return '465465465468468464'; + return '456'; } else { throw new moodle_exception('wrongusernamepassword'); } @@ -113,7 +113,7 @@ function call_moodle_function ($rest_arguments) { */ function mock_check_token($token) { //fake test - if ($token == 465465465468468464) { + if ($token == 456) { ///retrieve the user global $DB; $user = $DB->get_record('user', array('username'=>'wsuser', 'mnethostid'=>1)); diff --git a/webservice/soap/testclient/zend_soap_client.php b/webservice/soap/testclient/zend_soap_client.php index 17536c84df..ece750cb13 100644 --- a/webservice/soap/testclient/zend_soap_client.php +++ b/webservice/soap/testclient/zend_soap_client.php @@ -29,31 +29,86 @@ */ require_once('../../../config.php'); +require_once('../lib.php'); include "Zend/Loader.php"; Zend_Loader::registerAutoload(); +print_header('Soap test client', 'Soap test client'.":", true); +if (!webservice_lib::display_webservices_availability("soap")) { + echo "

"; + echo "Please fix the previous problem(s), the testing session has been interupted."; + print_footer(); + exit(); +} //1. authentication $client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?wsdl"); try { $token = $client->tmp_get_token(array('username' => "wsuser", 'password' => "wspassword")); - printLastRequestResponse($client); + print "
\n";
+    var_dump($token);
+    print "
"; } catch (moodle_exception $exception) { echo $exception; } //2. test functions +//$client = new Zend_Http_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl", array( +// 'maxredirects' => 0, +// 'timeout' => 30)); +//$response = $client->request(); +//$wsdl = $response->getBody(); +//varlog($wsdl,"user.wsdl", "w"); + + $client = new Zend_Soap_Client($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl"); +var_dump($CFG->wwwroot."/webservice/soap/server.php?token=".$token."&classpath=user&wsdl"); +print "
\n";
 var_dump($client->tmp_get_users(array('search' => "admin")));
-printLastRequestResponse($client);
+print "
"; + +//$param = array('search' => "admin"); +//$expectedresult = array(array( 'id' => 2, +// 'auth' => 'manual', +// 'confirmed' => '1', +// 'username' => 'admin', +// 'idnumber' => '', +// 'firstname' => 'Admin', +// 'lastname' => 'HEAD', +// 'email' => 'jerome@moodle.com', +// 'emailstop' => '0', +// 'lang' => 'en_utf8', +// 'theme' => '', +// 'timezone' => '99', +// 'mailformat' => '1')); +//$functionname = tmp_get_users; +//call_soap_function($client,$functionname,$param,$expectedresult); + +print "
\n";
 var_dump($client->tmp_create_user(array('username' => "mockuser66",'firstname' => "firstname6",'lastname' => "lastname6",'email' => "mockuser6@mockuser6.com",'password' => "password6")));
-printLastRequestResponse($client);
-var_dump($client->tmp_update_user(array('username' => "mockuser66",'mnethostid' => 1,'newusername' => "mockuser6b",'firstname' => "firstname6b")));
-printLastRequestResponse($client);
+print "
"; + +print "
\n";
+var_dump($client->tmp_update_user(array('mnethostid' => 1,'username' => "mockuser66",'newusername' => "mockuser6b",'firstname' => "firstname6b")));
+print "
"; + +print "
\n";
 var_dump($client->tmp_delete_user(array('username' => "mockuser6b",'mnethostid' => 1)));
-printLastRequestResponse($client);
+print "
"; + +print "
\n";
 var_dump($client->tmp_do_multiple_user_searches(array(array('search' => "jerome"),array('search' => "mock"))));
-printLastRequestResponse($client);
+print "
"; + + +print_footer(); + +//function call_soap_function($client,$functionname,$param,$expectedresult) { +// print "
\n";
+//    var_dump($client->$functionname($param));
+//    print "
"; +//} + function printLastRequestResponse($client) { print "
\n";