[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Unit tests for the webservice component. 19 * 20 * @package core_webservice 21 * @category test 22 * @copyright 2016 Jun Pataleta <jun@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 defined('MOODLE_INTERNAL') || die(); 26 27 global $CFG; 28 require_once($CFG->dirroot . '/webservice/lib.php'); 29 30 /** 31 * Unit tests for the webservice component. 32 * 33 * @package core_webservice 34 * @category test 35 * @copyright 2016 Jun Pataleta <jun@moodle.com> 36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 37 */ 38 class webservice_test extends advanced_testcase { 39 40 /** 41 * Setup. 42 */ 43 public function setUp() { 44 // Calling parent is good, always. 45 parent::setUp(); 46 47 // We always need enabled WS for this testcase. 48 set_config('enablewebservices', '1'); 49 } 50 51 /** 52 * Test init_service_class(). 53 */ 54 public function test_init_service_class() { 55 global $DB, $USER; 56 57 $this->resetAfterTest(true); 58 59 // Set current user. 60 $this->setAdminUser(); 61 62 // Add a web service. 63 $webservice = new stdClass(); 64 $webservice->name = 'Test web service'; 65 $webservice->enabled = true; 66 $webservice->restrictedusers = false; 67 $webservice->component = 'moodle'; 68 $webservice->timecreated = time(); 69 $webservice->downloadfiles = true; 70 $webservice->uploadfiles = true; 71 $externalserviceid = $DB->insert_record('external_services', $webservice); 72 73 // Add token. 74 $externaltoken = new stdClass(); 75 $externaltoken->token = 'testtoken'; 76 $externaltoken->tokentype = 0; 77 $externaltoken->userid = $USER->id; 78 $externaltoken->externalserviceid = $externalserviceid; 79 $externaltoken->contextid = 1; 80 $externaltoken->creatorid = $USER->id; 81 $externaltoken->timecreated = time(); 82 $DB->insert_record('external_tokens', $externaltoken); 83 84 // Add a function to the service. 85 $wsmethod = new stdClass(); 86 $wsmethod->externalserviceid = $externalserviceid; 87 $wsmethod->functionname = 'core_course_get_contents'; 88 $DB->insert_record('external_services_functions', $wsmethod); 89 90 // Initialise the dummy web service. 91 $dummy = new webservice_dummy(WEBSERVICE_AUTHMETHOD_PERMANENT_TOKEN); 92 // Set the token. 93 $dummy->set_token($externaltoken->token); 94 // Run the web service. 95 $dummy->run(); 96 // Get service methods and structs. 97 $servicemethods = $dummy->get_service_methods(); 98 $servicestructs = $dummy->get_service_structs(); 99 $this->assertNotEmpty($servicemethods); 100 // The function core_course_get_contents should be only the only web service function in the moment. 101 $this->assertEquals(1, count($servicemethods)); 102 // The function core_course_get_contents doesn't have a struct class, so the list of service structs should be empty. 103 $this->assertEmpty($servicestructs); 104 105 // Add other functions to the service. 106 // The function core_comment_get_comments has one struct class in its output. 107 $wsmethod->functionname = 'core_comment_get_comments'; 108 $DB->insert_record('external_services_functions', $wsmethod); 109 // The function core_grades_update_grades has one struct class in its input. 110 $wsmethod->functionname = 'core_grades_update_grades'; 111 $DB->insert_record('external_services_functions', $wsmethod); 112 113 // Run the web service again. 114 $dummy->run(); 115 // Get service methods and structs. 116 $servicemethods = $dummy->get_service_methods(); 117 $servicestructs = $dummy->get_service_structs(); 118 $this->assertEquals(3, count($servicemethods)); 119 $this->assertEquals(2, count($servicestructs)); 120 121 // Check the contents of service methods. 122 foreach ($servicemethods as $method) { 123 // Get the external function info. 124 $function = external_api::external_function_info($method->name); 125 126 // Check input params. 127 foreach ($function->parameters_desc->keys as $name => $keydesc) { 128 $this->check_params($method->inputparams[$name]['type'], $keydesc, $servicestructs); 129 } 130 131 // Check output params. 132 $this->check_params($method->outputparams['return']['type'], $function->returns_desc, $servicestructs); 133 134 // Check description. 135 $this->assertEquals($function->description, $method->description); 136 } 137 } 138 139 /** 140 * Utility method that tests the parameter type of a method info's input/output parameter. 141 * 142 * @param string $type The parameter type that is being evaluated. 143 * @param mixed $methoddesc The method description of the WS function. 144 * @param array $servicestructs The list of generated service struct classes. 145 */ 146 private function check_params($type, $methoddesc, $servicestructs) { 147 if ($methoddesc instanceof external_value) { 148 // Test for simple types. 149 if (in_array($methoddesc->type, [PARAM_INT, PARAM_FLOAT, PARAM_BOOL])) { 150 $this->assertEquals($methoddesc->type, $type); 151 } else { 152 $this->assertEquals('string', $type); 153 } 154 } else if ($methoddesc instanceof external_single_structure) { 155 // Test that the class name of the struct class is in the array of service structs. 156 $structinfo = $this->get_struct_info($servicestructs, $type); 157 $this->assertNotNull($structinfo); 158 // Test that the properties of the struct info exist in the method description. 159 foreach ($structinfo->properties as $propname => $proptype) { 160 $this->assertTrue($this->in_keydesc($methoddesc, $propname)); 161 } 162 } else if ($methoddesc instanceof external_multiple_structure) { 163 // Test for array types. 164 $this->assertEquals('array', $type); 165 } 166 } 167 168 /** 169 * Gets the struct information from the list of struct classes based on the given struct class name. 170 * 171 * @param array $structarray The list of generated struct classes. 172 * @param string $structclass The name of the struct class. 173 * @return object|null The struct class info, or null if it's not found. 174 */ 175 private function get_struct_info($structarray, $structclass) { 176 foreach ($structarray as $struct) { 177 if ($struct->classname === $structclass) { 178 return $struct; 179 } 180 } 181 return null; 182 } 183 184 /** 185 * Searches the keys of the given external_single_structure object if it contains a certain property name. 186 * 187 * @param external_single_structure $keydesc 188 * @param string $propertyname The property name to be searched for. 189 * @return bool True if the property name is found in $keydesc. False, otherwise. 190 */ 191 private function in_keydesc(external_single_structure $keydesc, $propertyname) { 192 foreach ($keydesc->keys as $key => $desc) { 193 if ($key === $propertyname) { 194 return true; 195 } 196 } 197 return false; 198 } 199 } 200 201 /** 202 * Class webservice_dummy. 203 * 204 * Dummy webservice class for testing the webservice_base_server class and enable us to expose variables we want to test. 205 * 206 * @package core_webservice 207 * @category test 208 * @copyright 2016 Jun Pataleta <jun@moodle.com> 209 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 210 */ 211 class webservice_dummy extends webservice_base_server { 212 213 /** 214 * webservice_dummy constructor. 215 * 216 * @param int $authmethod The authentication method. 217 */ 218 public function __construct($authmethod) { 219 parent::__construct($authmethod); 220 221 // Arbitrarily naming this as REST in order not to have to register another WS protocol and set capabilities. 222 $this->wsname = 'rest'; 223 } 224 225 /** 226 * Token setter method. 227 * 228 * @param string $token The web service token. 229 */ 230 public function set_token($token) { 231 $this->token = $token; 232 } 233 234 /** 235 * This method parses the request input, it needs to get: 236 * 1/ user authentication - username+password or token 237 * 2/ function name 238 * 3/ function parameters 239 */ 240 protected function parse_request() { 241 // Just a method stub. No need to implement at the moment since it's not really being used for this test case for now. 242 } 243 244 /** 245 * Send the result of function call to the WS client. 246 */ 247 protected function send_response() { 248 // Just a method stub. No need to implement at the moment since it's not really being used for this test case for now. 249 } 250 251 /** 252 * Send the error information to the WS client. 253 * 254 * @param exception $ex 255 */ 256 protected function send_error($ex = null) { 257 // Just a method stub. No need to implement at the moment since it's not really being used for this test case for now. 258 } 259 260 /** 261 * run() method implementation. 262 */ 263 public function run() { 264 $this->authenticate_user(); 265 $this->init_service_class(); 266 } 267 268 /** 269 * Getter method of servicemethods array. 270 * 271 * @return array 272 */ 273 public function get_service_methods() { 274 return $this->servicemethods; 275 } 276 277 /** 278 * Getter method of servicestructs array. 279 * 280 * @return array 281 */ 282 public function get_service_structs() { 283 return $this->servicestructs; 284 } 285 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |