PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/webservice/tests/lib_test.php

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