PageRenderTime 305ms CodeModel.GetById 30ms RepoModel.GetById 6ms app.codeStats 0ms

/admin/webservice/testclient_forms.php

http://github.com/moodle/moodle
PHP | 352 lines | 223 code | 34 blank | 95 comment | 27 complexity | febe7545f613efddd0025d9af65f641a MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. require_once($CFG->libdir.'/formslib.php');
  3. class webservice_test_client_form extends moodleform {
  4. public function definition() {
  5. global $CFG;
  6. $mform = $this->_form;
  7. list($functions, $protocols) = $this->_customdata;
  8. $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
  9. $authmethod = array('simple' => 'simple', 'token' => 'token');
  10. $mform->addElement('select', 'authmethod', get_string('authmethod', 'webservice'), $authmethod);
  11. $mform->setType('simple', PARAM_ALPHA);
  12. $mform->addElement('select', 'protocol', get_string('protocol', 'webservice'), $protocols);
  13. $mform->setType('protocol', PARAM_ALPHA);
  14. $mform->addElement('select', 'function', get_string('function', 'webservice'), $functions);
  15. $mform->setType('function', PARAM_PLUGIN);
  16. $this->add_action_buttons(false, get_string('select'));
  17. }
  18. }
  19. // === Test client forms ===
  20. /**
  21. * Base class for implementations of WS test client forms.
  22. *
  23. * @package core_webservice
  24. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25. * @copyright 2017 Marina Glancy
  26. */
  27. abstract class webservice_test_client_base_form extends moodleform {
  28. /**
  29. * Definition of the parameters used by this WS function
  30. */
  31. protected abstract function test_client_definition();
  32. /**
  33. * The form definition.
  34. */
  35. public function definition() {
  36. $mform = $this->_form;
  37. $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
  38. // Note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters.
  39. $data = $this->_customdata;
  40. if ($data['authmethod'] == 'simple') {
  41. $mform->addElement('text', 'wsusername', 'wsusername');
  42. $mform->setType('wsusername', core_user::get_property_type('username'));
  43. $mform->addElement('text', 'wspassword', 'wspassword');
  44. $mform->setType('wspassword', core_user::get_property_type('password'));
  45. } else if ($data['authmethod'] == 'token') {
  46. $mform->addElement('text', 'token', 'token');
  47. $mform->setType('token', PARAM_RAW_TRIMMED);
  48. }
  49. $mform->addElement('hidden', 'authmethod', $data['authmethod']);
  50. $mform->setType('authmethod', PARAM_ALPHA);
  51. $mform->addElement('hidden', 'function');
  52. $mform->setType('function', PARAM_PLUGIN);
  53. $mform->addElement('hidden', 'protocol');
  54. $mform->setType('protocol', PARAM_ALPHA);
  55. $this->test_client_definition();
  56. $this->add_action_buttons(true, get_string('execute', 'webservice'));
  57. }
  58. /**
  59. * Get the parameters that the user submitted using the form.
  60. * @return array|null
  61. */
  62. public function get_params() {
  63. if (!$data = $this->get_data()) {
  64. return null;
  65. }
  66. return array_diff_key((array)$data, ['submitbutton' => 1, 'protocol' => 1, 'function' => 1,
  67. 'wsusername' => 1, 'wspassword' => 1, 'token' => 1, 'authmethod' => 1]);
  68. }
  69. }
  70. /**
  71. * Form class for create_categories() web service function test.
  72. *
  73. * @package core_webservice
  74. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  75. * @copyright 2012 Fabio Souto
  76. */
  77. class core_course_create_categories_testclient_form extends webservice_test_client_base_form {
  78. /**
  79. * The form definition.
  80. */
  81. protected function test_client_definition() {
  82. $mform = $this->_form;
  83. $mform->addElement('text', 'name[0]', 'name[0]');
  84. $mform->addElement('text', 'parent[0]', 'parent[0]');
  85. $mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
  86. $mform->addElement('text', 'description[0]', 'description[0]');
  87. $mform->addElement('text', 'name[1]', 'name[1]');
  88. $mform->addElement('text', 'parent[1]', 'parent[1]');
  89. $mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
  90. $mform->addElement('text', 'description[1]', 'description[1]');
  91. $mform->setType('name', PARAM_TEXT);
  92. $mform->setType('parent', PARAM_INT);
  93. $mform->setType('idnumber', PARAM_RAW);
  94. $mform->setType('description', PARAM_RAW);
  95. }
  96. /**
  97. * Get the parameters that the user submitted using the form.
  98. * @return array|null
  99. */
  100. public function get_params() {
  101. if (!$data = $this->get_data()) {
  102. return null;
  103. }
  104. $params = array();
  105. $params['categories'] = array();
  106. for ($i=0; $i<10; $i++) {
  107. if (empty($data->name[$i])) {
  108. continue;
  109. }
  110. $params['categories'][] = array('name'=>$data->name[$i], 'parent'=>$data->parent[$i],
  111. 'idnumber'=>$data->idnumber[$i], 'description'=>$data->description[$i]);
  112. }
  113. return $params;
  114. }
  115. }
  116. /**
  117. * Form class for delete_categories() web service function test.
  118. *
  119. * @package core_webservice
  120. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  121. * @copyright 2012 Fabio Souto
  122. */
  123. class core_course_delete_categories_testclient_form extends webservice_test_client_base_form {
  124. /**
  125. * The form definition.
  126. */
  127. protected function test_client_definition() {
  128. $mform = $this->_form;
  129. $mform->addElement('text', 'id[0]', 'id[0]');
  130. $mform->addElement('text', 'newparent[0]', 'newparent[0]');
  131. $mform->addElement('text', 'recursive[0]', 'recursive[0]');
  132. $mform->addElement('text', 'id[1]', 'id[1]');
  133. $mform->addElement('text', 'newparent[1]', 'newparent[1]');
  134. $mform->addElement('text', 'recursive[1]', 'recursive[1]');
  135. $mform->setType('id', PARAM_INT);
  136. $mform->setType('newparent', PARAM_INT);
  137. $mform->setType('recursive', PARAM_BOOL);
  138. }
  139. /**
  140. * Get the parameters that the user submitted using the form.
  141. * @return array|null
  142. */
  143. public function get_params() {
  144. if (!$data = $this->get_data()) {
  145. return null;
  146. }
  147. $params = array();
  148. $params['categories'] = array();
  149. for ($i=0; $i<10; $i++) {
  150. if (empty($data->id[$i])) {
  151. continue;
  152. }
  153. $attrs = array();
  154. $attrs['id'] = $data->id[$i];
  155. if (!empty($data->newparent[$i])) {
  156. $attrs['newparent'] = $data->newparent[$i];
  157. }
  158. if (!empty($data->recursive[$i])) {
  159. $attrs['recursive'] = $data->recursive[$i];
  160. }
  161. $params['categories'][] = $attrs;
  162. }
  163. return $params;
  164. }
  165. }
  166. /**
  167. * Form class for create_categories() web service function test.
  168. *
  169. * @package core_webservice
  170. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  171. * @copyright 2012 Fabio Souto
  172. */
  173. class core_course_update_categories_testclient_form extends webservice_test_client_base_form {
  174. /**
  175. * The form definition.
  176. */
  177. protected function test_client_definition() {
  178. $mform = $this->_form;
  179. $mform->addElement('text', 'id[0]', 'id[0]');
  180. $mform->addElement('text', 'name[0]', 'name[0]');
  181. $mform->addElement('text', 'parent[0]', 'parent[0]');
  182. $mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
  183. $mform->addElement('text', 'description[0]', 'description[0]');
  184. $mform->addElement('text', 'id[1]', 'id[1]');
  185. $mform->addElement('text', 'name[1]', 'name[1]');
  186. $mform->addElement('text', 'parent[1]', 'parent[1]');
  187. $mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
  188. $mform->addElement('text', 'description[1]', 'description[1]');
  189. $mform->setType('id', PARAM_INT);
  190. $mform->setType('name', PARAM_TEXT);
  191. $mform->setType('parent', PARAM_INT);
  192. $mform->setType('idnumber', PARAM_RAW);
  193. $mform->setType('description', PARAM_RAW);
  194. }
  195. /**
  196. * Get the parameters that the user submitted using the form.
  197. * @return array|null
  198. */
  199. public function get_params() {
  200. if (!$data = $this->get_data()) {
  201. return null;
  202. }
  203. $params = array();
  204. $params['categories'] = array();
  205. for ($i=0; $i<10; $i++) {
  206. if (empty($data->id[$i])) {
  207. continue;
  208. }
  209. $attrs = array();
  210. $attrs['id'] = $data->id[$i];
  211. if (!empty($data->name[$i])) {
  212. $attrs['name'] = $data->name[$i];
  213. }
  214. if (!empty($data->parent[$i])) {
  215. $attrs['parent'] = $data->parent[$i];
  216. }
  217. if (!empty($data->idnumber[$i])) {
  218. $attrs['idnumber'] = $data->idnumber[$i];
  219. }
  220. if (!empty($data->description[$i])) {
  221. $attrs['description'] = $data->description[$i];
  222. }
  223. $params['categories'][] = $attrs;
  224. }
  225. return $params;
  226. }
  227. }
  228. /**
  229. * Test class for WS function core_fetch_notifications
  230. *
  231. * @package core_webservice
  232. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  233. * @copyright 2017 Marina Glancy
  234. */
  235. class core_fetch_notifications_testclient_form extends webservice_test_client_base_form {
  236. /**
  237. * The form definition.
  238. */
  239. protected function test_client_definition() {
  240. $mform = $this->_form;
  241. $mform->addElement('text', 'contextid', 'contextid');
  242. $mform->setType('contextid', PARAM_INT);
  243. $mform->setDefault('contextid', context_system::instance()->id);
  244. }
  245. }
  246. /**
  247. * Test class for WS function get_site_info
  248. *
  249. * @package core_webservice
  250. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  251. * @copyright 2017 Marina Glancy
  252. */
  253. class core_webservice_get_site_info_testclient_form extends webservice_test_client_base_form {
  254. /**
  255. * The form definition.
  256. */
  257. protected function test_client_definition() {
  258. }
  259. }
  260. /**
  261. * Test class for WS function core_get_string
  262. *
  263. * @package core_webservice
  264. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  265. * @copyright 2017 Marina Glancy
  266. */
  267. class core_get_string_testclient_form extends webservice_test_client_base_form {
  268. /**
  269. * The form definition.
  270. */
  271. protected function test_client_definition() {
  272. $mform = $this->_form;
  273. $mform->addElement('text', 'stringid', 'stringid');
  274. $mform->setType('stringid', PARAM_STRINGID);
  275. $mform->addElement('text', 'component', 'component');
  276. $mform->setType('component', PARAM_COMPONENT);
  277. $mform->addElement('text', 'lang', 'lang');
  278. $mform->setType('lang', PARAM_LANG);
  279. $mform->addElement('text', 'stringparams_name[1]', 'Parameter 1 name');
  280. $mform->setType('stringparams_name[1]', PARAM_ALPHANUMEXT);
  281. $mform->addElement('text', 'stringparams_value[1]', 'Parameter 1 value');
  282. $mform->setType('stringparams_value[1]', PARAM_RAW);
  283. $mform->addElement('text', 'stringparams_name[2]', 'Parameter 2 name');
  284. $mform->setType('stringparams_name[2]', PARAM_ALPHANUMEXT);
  285. $mform->addElement('text', 'stringparams_value[2]', 'Parameter 2 value');
  286. $mform->setType('stringparams_value[2]', PARAM_RAW);
  287. $mform->addElement('text', 'stringparams_name[3]', 'Parameter 3 name');
  288. $mform->setType('stringparams_name[3]', PARAM_ALPHANUMEXT);
  289. $mform->addElement('text', 'stringparams_value[3]', 'Parameter 3 value');
  290. $mform->setType('stringparams_value[3]', PARAM_RAW);
  291. $mform->addElement('static', 'paramnote', '', 'If a parameter is not an object, only specify "Parameter 1 value"');
  292. }
  293. /**
  294. * Get the parameters that the user submitted using the form.
  295. * @return array|null
  296. */
  297. public function get_params() {
  298. $params = parent::get_params();
  299. if ($params === null) {
  300. return null;
  301. }
  302. $params['stringparams'] = [];
  303. for ($idx = 1; $idx <= 3; $idx++) {
  304. $name = isset($params['stringparams_name'][$idx]) ? strval($params['stringparams_name'][$idx]) : '';
  305. $value = isset($params['stringparams_value'][$idx]) ? strval($params['stringparams_value'][$idx]) : '';
  306. if ($name !== '' || $value !== '') {
  307. if ($name === '') {
  308. $params['stringparams'][] = ['value' => $value];
  309. } else {
  310. $params['stringparams'][] = ['name' => $name, 'value' => $value];
  311. }
  312. }
  313. }
  314. unset($params['stringparams_name']);
  315. unset($params['stringparams_value']);
  316. return $params;
  317. }
  318. }