Merge branch 'MDL-80014' of https://github.com/paulholden/moodle
[moodle.git] / admin / webservice / testclient_forms.php
blob9f89f30907082e964a5fb7659e48671dee8f5b1f
1 <?php
3 require_once($CFG->libdir.'/formslib.php');
6 class webservice_test_client_form extends moodleform {
7 public function definition() {
8 global $CFG;
10 $mform = $this->_form;
11 list($functions, $protocols) = $this->_customdata;
13 $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
15 $authmethod = array('simple' => 'simple', 'token' => 'token');
16 $mform->addElement('select', 'authmethod', get_string('authmethod', 'webservice'), $authmethod);
17 $mform->setType('simple', PARAM_ALPHA);
19 $mform->addElement('select', 'protocol', get_string('protocol', 'webservice'), $protocols);
20 $mform->setType('protocol', PARAM_ALPHA);
22 $mform->addElement('select', 'function', get_string('function', 'webservice'), $functions);
23 $mform->setType('function', PARAM_PLUGIN);
25 $this->add_action_buttons(false, get_string('select'));
29 // === Test client forms ===
31 /**
32 * Base class for implementations of WS test client forms.
34 * @package core_webservice
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 * @copyright 2017 Marina Glancy
38 abstract class webservice_test_client_base_form extends moodleform {
40 /**
41 * Definition of the parameters used by this WS function
43 protected abstract function test_client_definition();
45 /**
46 * The form definition.
48 public function definition() {
49 $mform = $this->_form;
51 $mform->addElement('header', 'wstestclienthdr', get_string('testclient', 'webservice'));
53 // Note: these values are intentionally PARAM_RAW - we want users to test any rubbish as parameters.
54 $data = $this->_customdata;
55 if ($data['authmethod'] == 'simple') {
56 $mform->addElement('text', 'wsusername', 'wsusername');
57 $mform->setType('wsusername', core_user::get_property_type('username'));
58 $mform->addElement('text', 'wspassword', 'wspassword');
59 $mform->setType('wspassword', core_user::get_property_type('password'));
60 } else if ($data['authmethod'] == 'token') {
61 $mform->addElement('text', 'token', 'token');
62 $mform->setType('token', PARAM_RAW_TRIMMED);
65 $mform->addElement('hidden', 'authmethod', $data['authmethod']);
66 $mform->setType('authmethod', PARAM_ALPHA);
68 $mform->addElement('hidden', 'function');
69 $mform->setType('function', PARAM_PLUGIN);
71 $mform->addElement('hidden', 'protocol');
72 $mform->setType('protocol', PARAM_ALPHA);
74 $this->test_client_definition();
76 $this->add_action_buttons(true, get_string('execute', 'webservice'));
79 /**
80 * Get the parameters that the user submitted using the form.
81 * @return array|null
83 public function get_params() {
84 if (!$data = $this->get_data()) {
85 return null;
87 return array_diff_key((array)$data, ['submitbutton' => 1, 'protocol' => 1, 'function' => 1,
88 'wsusername' => 1, 'wspassword' => 1, 'token' => 1, 'authmethod' => 1]);
92 /**
93 * Form class for create_categories() web service function test.
95 * @package core_webservice
96 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
97 * @copyright 2012 Fabio Souto
99 class core_course_create_categories_testclient_form extends webservice_test_client_base_form {
101 * The form definition.
103 protected function test_client_definition() {
104 $mform = $this->_form;
105 $mform->addElement('text', 'name[0]', 'name[0]');
106 $mform->addElement('text', 'parent[0]', 'parent[0]');
107 $mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
108 $mform->addElement('text', 'description[0]', 'description[0]');
109 $mform->addElement('text', 'name[1]', 'name[1]');
110 $mform->addElement('text', 'parent[1]', 'parent[1]');
111 $mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
112 $mform->addElement('text', 'description[1]', 'description[1]');
113 $mform->setType('name', PARAM_TEXT);
114 $mform->setType('parent', PARAM_INT);
115 $mform->setType('idnumber', PARAM_RAW);
116 $mform->setType('description', PARAM_RAW);
120 * Get the parameters that the user submitted using the form.
121 * @return array|null
123 public function get_params() {
124 if (!$data = $this->get_data()) {
125 return null;
128 $params = array();
129 $params['categories'] = array();
130 for ($i=0; $i<10; $i++) {
131 if (empty($data->name[$i])) {
132 continue;
134 $params['categories'][] = array('name'=>$data->name[$i], 'parent'=>$data->parent[$i],
135 'idnumber'=>$data->idnumber[$i], 'description'=>$data->description[$i]);
137 return $params;
142 * Form class for delete_categories() web service function test.
144 * @package core_webservice
145 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
146 * @copyright 2012 Fabio Souto
148 class core_course_delete_categories_testclient_form extends webservice_test_client_base_form {
150 * The form definition.
152 protected function test_client_definition() {
153 $mform = $this->_form;
154 $mform->addElement('text', 'id[0]', 'id[0]');
155 $mform->addElement('text', 'newparent[0]', 'newparent[0]');
156 $mform->addElement('text', 'recursive[0]', 'recursive[0]');
157 $mform->addElement('text', 'id[1]', 'id[1]');
158 $mform->addElement('text', 'newparent[1]', 'newparent[1]');
159 $mform->addElement('text', 'recursive[1]', 'recursive[1]');
160 $mform->setType('id', PARAM_INT);
161 $mform->setType('newparent', PARAM_INT);
162 $mform->setType('recursive', PARAM_BOOL);
166 * Get the parameters that the user submitted using the form.
167 * @return array|null
169 public function get_params() {
170 if (!$data = $this->get_data()) {
171 return null;
173 $params = array();
174 $params['categories'] = array();
175 for ($i=0; $i<10; $i++) {
176 if (empty($data->id[$i])) {
177 continue;
179 $attrs = array();
180 $attrs['id'] = $data->id[$i];
181 if (!empty($data->newparent[$i])) {
182 $attrs['newparent'] = $data->newparent[$i];
184 if (!empty($data->recursive[$i])) {
185 $attrs['recursive'] = $data->recursive[$i];
187 $params['categories'][] = $attrs;
189 return $params;
194 * Form class for create_categories() web service function test.
196 * @package core_webservice
197 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
198 * @copyright 2012 Fabio Souto
200 class core_course_update_categories_testclient_form extends webservice_test_client_base_form {
202 * The form definition.
204 protected function test_client_definition() {
205 $mform = $this->_form;
206 $mform->addElement('text', 'id[0]', 'id[0]');
207 $mform->addElement('text', 'name[0]', 'name[0]');
208 $mform->addElement('text', 'parent[0]', 'parent[0]');
209 $mform->addElement('text', 'idnumber[0]', 'idnumber[0]');
210 $mform->addElement('text', 'description[0]', 'description[0]');
211 $mform->addElement('text', 'id[1]', 'id[1]');
212 $mform->addElement('text', 'name[1]', 'name[1]');
213 $mform->addElement('text', 'parent[1]', 'parent[1]');
214 $mform->addElement('text', 'idnumber[1]', 'idnumber[1]');
215 $mform->addElement('text', 'description[1]', 'description[1]');
216 $mform->setType('id', PARAM_INT);
217 $mform->setType('name', PARAM_TEXT);
218 $mform->setType('parent', PARAM_INT);
219 $mform->setType('idnumber', PARAM_RAW);
220 $mform->setType('description', PARAM_RAW);
224 * Get the parameters that the user submitted using the form.
225 * @return array|null
227 public function get_params() {
228 if (!$data = $this->get_data()) {
229 return null;
231 $params = array();
232 $params['categories'] = array();
233 for ($i=0; $i<10; $i++) {
235 if (empty($data->id[$i])) {
236 continue;
238 $attrs = array();
239 $attrs['id'] = $data->id[$i];
240 if (!empty($data->name[$i])) {
241 $attrs['name'] = $data->name[$i];
243 if (!empty($data->parent[$i])) {
244 $attrs['parent'] = $data->parent[$i];
246 if (!empty($data->idnumber[$i])) {
247 $attrs['idnumber'] = $data->idnumber[$i];
249 if (!empty($data->description[$i])) {
250 $attrs['description'] = $data->description[$i];
252 $params['categories'][] = $attrs;
254 return $params;
259 * Test class for WS function core_fetch_notifications
261 * @package core_webservice
262 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
263 * @copyright 2017 Marina Glancy
265 class core_fetch_notifications_testclient_form extends webservice_test_client_base_form {
267 * The form definition.
269 protected function test_client_definition() {
270 $mform = $this->_form;
271 $mform->addElement('text', 'contextid', 'contextid');
272 $mform->setType('contextid', PARAM_INT);
273 $mform->setDefault('contextid', context_system::instance()->id);
278 * Test class for WS function get_site_info
280 * @package core_webservice
281 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
282 * @copyright 2017 Marina Glancy
284 class core_webservice_get_site_info_testclient_form extends webservice_test_client_base_form {
286 * The form definition.
288 protected function test_client_definition() {
293 * Test class for WS function core_get_string
295 * @package core_webservice
296 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
297 * @copyright 2017 Marina Glancy
299 class core_get_string_testclient_form extends webservice_test_client_base_form {
301 * The form definition.
303 protected function test_client_definition() {
304 $mform = $this->_form;
305 $mform->addElement('text', 'stringid', 'stringid');
306 $mform->setType('stringid', PARAM_STRINGID);
307 $mform->addElement('text', 'component', 'component');
308 $mform->setType('component', PARAM_COMPONENT);
309 $mform->addElement('text', 'lang', 'lang');
310 $mform->setType('lang', PARAM_LANG);
311 $mform->addElement('text', 'stringparams_name[1]', 'Parameter 1 name');
312 $mform->setType('stringparams_name[1]', PARAM_ALPHANUMEXT);
313 $mform->addElement('text', 'stringparams_value[1]', 'Parameter 1 value');
314 $mform->setType('stringparams_value[1]', PARAM_RAW);
315 $mform->addElement('text', 'stringparams_name[2]', 'Parameter 2 name');
316 $mform->setType('stringparams_name[2]', PARAM_ALPHANUMEXT);
317 $mform->addElement('text', 'stringparams_value[2]', 'Parameter 2 value');
318 $mform->setType('stringparams_value[2]', PARAM_RAW);
319 $mform->addElement('text', 'stringparams_name[3]', 'Parameter 3 name');
320 $mform->setType('stringparams_name[3]', PARAM_ALPHANUMEXT);
321 $mform->addElement('text', 'stringparams_value[3]', 'Parameter 3 value');
322 $mform->setType('stringparams_value[3]', PARAM_RAW);
323 $mform->addElement('static', 'paramnote', '', 'If a parameter is not an object, only specify "Parameter 1 value"');
327 * Get the parameters that the user submitted using the form.
328 * @return array|null
330 public function get_params() {
331 $params = parent::get_params();
332 if ($params === null) {
333 return null;
336 $params['stringparams'] = [];
337 for ($idx = 1; $idx <= 3; $idx++) {
338 $name = isset($params['stringparams_name'][$idx]) ? strval($params['stringparams_name'][$idx]) : '';
339 $value = isset($params['stringparams_value'][$idx]) ? strval($params['stringparams_value'][$idx]) : '';
340 if ($name !== '' || $value !== '') {
341 if ($name === '') {
342 $params['stringparams'][] = ['value' => $value];
343 } else {
344 $params['stringparams'][] = ['name' => $name, 'value' => $value];
348 unset($params['stringparams_name']);
349 unset($params['stringparams_value']);
350 return $params;