MDL-52873 forms: Generate unique id attributes for modgrade elements
[moodle.git] / lib / tests / formslib_test.php
CommitLineData
a3d5830a
PS
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 /lib/formslib.php.
19 *
20 * @package core_form
21 * @category phpunit
22 * @copyright 2011 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/formslib.php');
30require_once($CFG->libdir . '/form/radio.php');
31require_once($CFG->libdir . '/form/select.php');
32require_once($CFG->libdir . '/form/text.php');
33
34
78d4a260 35class core_formslib_testcase extends advanced_testcase {
a3d5830a
PS
36
37 public function test_require_rule() {
38 global $CFG;
39
40 $strictformsrequired = null;
41 if (isset($CFG->strictformsrequired)) {
42 $strictformsrequired = $CFG->strictformsrequired;
43 }
44
45 $rule = new MoodleQuickForm_Rule_Required();
46
78d4a260 47 // First run the tests with strictformsrequired off.
a3d5830a 48 $CFG->strictformsrequired = false;
78d4a260 49 // Passes.
a3d5830a
PS
50 $this->assertTrue($rule->validate('Something'));
51 $this->assertTrue($rule->validate("Something\nmore"));
52 $this->assertTrue($rule->validate("\nmore"));
53 $this->assertTrue($rule->validate(" more "));
bf8542ce
MG
54 $this->assertTrue($rule->validate('ш'));
55 $this->assertTrue($rule->validate("の"));
a3d5830a
PS
56 $this->assertTrue($rule->validate("0"));
57 $this->assertTrue($rule->validate(0));
58 $this->assertTrue($rule->validate(true));
59 $this->assertTrue($rule->validate(' '));
60 $this->assertTrue($rule->validate(' '));
61 $this->assertTrue($rule->validate("\t"));
62 $this->assertTrue($rule->validate("\n"));
63 $this->assertTrue($rule->validate("\r"));
64 $this->assertTrue($rule->validate("\r\n"));
65 $this->assertTrue($rule->validate(" \t \n \r "));
66 $this->assertTrue($rule->validate('<p></p>'));
67 $this->assertTrue($rule->validate('<p> </p>'));
68 $this->assertTrue($rule->validate('<p>x</p>'));
69 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
70 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
71 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
72 $this->assertTrue($rule->validate('<hr />'));
73 $this->assertTrue($rule->validate('<hr/>'));
74 $this->assertTrue($rule->validate('<hr>'));
75 $this->assertTrue($rule->validate('<hr></hr>'));
76 $this->assertTrue($rule->validate('<br />'));
77 $this->assertTrue($rule->validate('<br/>'));
78 $this->assertTrue($rule->validate('<br>'));
79 $this->assertTrue($rule->validate('&nbsp;'));
78d4a260 80 // Fails.
a3d5830a
PS
81 $this->assertFalse($rule->validate(''));
82 $this->assertFalse($rule->validate(false));
83 $this->assertFalse($rule->validate(null));
84
78d4a260 85 // Now run the same tests with it on to make sure things work as expected.
a3d5830a 86 $CFG->strictformsrequired = true;
78d4a260 87 // Passes.
a3d5830a
PS
88 $this->assertTrue($rule->validate('Something'));
89 $this->assertTrue($rule->validate("Something\nmore"));
90 $this->assertTrue($rule->validate("\nmore"));
91 $this->assertTrue($rule->validate(" more "));
92 $this->assertTrue($rule->validate("0"));
bf8542ce
MG
93 $this->assertTrue($rule->validate('ш'));
94 $this->assertTrue($rule->validate("の"));
a3d5830a
PS
95 $this->assertTrue($rule->validate(0));
96 $this->assertTrue($rule->validate(true));
97 $this->assertTrue($rule->validate('<p>x</p>'));
98 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
99 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
100 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
101 $this->assertTrue($rule->validate('<hr />'));
102 $this->assertTrue($rule->validate('<hr/>'));
103 $this->assertTrue($rule->validate('<hr>'));
104 $this->assertTrue($rule->validate('<hr></hr>'));
78d4a260 105 // Fails.
a3d5830a
PS
106 $this->assertFalse($rule->validate(' '));
107 $this->assertFalse($rule->validate(' '));
108 $this->assertFalse($rule->validate("\t"));
109 $this->assertFalse($rule->validate("\n"));
110 $this->assertFalse($rule->validate("\r"));
111 $this->assertFalse($rule->validate("\r\n"));
112 $this->assertFalse($rule->validate(" \t \n \r "));
113 $this->assertFalse($rule->validate('<p></p>'));
114 $this->assertFalse($rule->validate('<p> </p>'));
115 $this->assertFalse($rule->validate('<br />'));
116 $this->assertFalse($rule->validate('<br/>'));
117 $this->assertFalse($rule->validate('<br>'));
118 $this->assertFalse($rule->validate('&nbsp;'));
119 $this->assertFalse($rule->validate(''));
120 $this->assertFalse($rule->validate(false));
121 $this->assertFalse($rule->validate(null));
122
123 if (isset($strictformsrequired)) {
124 $CFG->strictformsrequired = $strictformsrequired;
125 }
126 }
127
61dc1d26
EL
128 public function test_range_rule() {
129 global $CFG;
130
131 require_once('HTML/QuickForm/Rule/Range.php'); // Requires this pear stuff.
132
133 $strictformsrequired = null;
134 if (isset($CFG->strictformsrequired)) {
135 $strictformsrequired = $CFG->strictformsrequired;
136 }
137
138 $rule = new HTML_QuickForm_Rule_Range();
139
140 // First run the tests with strictformsrequired off.
141 $CFG->strictformsrequired = false;
142 // Passes.
143 $rule->setName('minlength'); // Let's verify some min lengths.
144 $this->assertTrue($rule->validate('12', 2));
145 $this->assertTrue($rule->validate('123', 2));
146 $this->assertTrue($rule->validate('áé', 2));
147 $this->assertTrue($rule->validate('áéí', 2));
148 $rule->setName('maxlength'); // Let's verify some max lengths.
149 $this->assertTrue($rule->validate('1', 2));
150 $this->assertTrue($rule->validate('12', 2));
151 $this->assertTrue($rule->validate('á', 2));
152 $this->assertTrue($rule->validate('áé', 2));
153 $rule->setName('----'); // Let's verify some ranges.
154 $this->assertTrue($rule->validate('', array(0, 2)));
155 $this->assertTrue($rule->validate('1', array(0, 2)));
156 $this->assertTrue($rule->validate('12', array(0, 2)));
157 $this->assertTrue($rule->validate('á', array(0, 2)));
158 $this->assertTrue($rule->validate('áé', array(0, 2)));
159
160 // Fail.
161 $rule->setName('minlength'); // Let's verify some min lengths.
162 $this->assertFalse($rule->validate('', 2));
163 $this->assertFalse($rule->validate('1', 2));
164 $this->assertFalse($rule->validate('á', 2));
165 $rule->setName('maxlength'); // Let's verify some max lengths.
166 $this->assertFalse($rule->validate('123', 2));
167 $this->assertFalse($rule->validate('áéí', 2));
168 $rule->setName('----'); // Let's verify some ranges.
169 $this->assertFalse($rule->validate('', array(1, 2)));
170 $this->assertFalse($rule->validate('123', array(1, 2)));
171 $this->assertFalse($rule->validate('áéí', array(1, 2)));
172
173 // Now run the same tests with it on to make sure things work as expected.
174 $CFG->strictformsrequired = true;
175 // Passes.
176 $rule->setName('minlength'); // Let's verify some min lengths.
177 $this->assertTrue($rule->validate('12', 2));
178 $this->assertTrue($rule->validate('123', 2));
179 $this->assertTrue($rule->validate('áé', 2));
180 $this->assertTrue($rule->validate('áéí', 2));
181 $rule->setName('maxlength'); // Let's verify some min lengths.
182 $this->assertTrue($rule->validate('1', 2));
183 $this->assertTrue($rule->validate('12', 2));
184 $this->assertTrue($rule->validate('á', 2));
185 $this->assertTrue($rule->validate('áé', 2));
186 $rule->setName('----'); // Let's verify some ranges.
187 $this->assertTrue($rule->validate('', array(0, 2)));
188 $this->assertTrue($rule->validate('1', array(0, 2)));
189 $this->assertTrue($rule->validate('12', array(0, 2)));
190 $this->assertTrue($rule->validate('á', array(0, 2)));
191 $this->assertTrue($rule->validate('áé', array(0, 2)));
192
193 // Fail.
194 $rule->setName('minlength'); // Let's verify some min lengths.
195 $this->assertFalse($rule->validate('', 2));
196 $this->assertFalse($rule->validate('1', 2));
197 $this->assertFalse($rule->validate('á', 2));
198 $rule->setName('maxlength'); // Let's verify some min lengths.
199 $this->assertFalse($rule->validate('123', 2));
200 $this->assertFalse($rule->validate('áéí', 2));
201 $rule->setName('----'); // Let's verify some ranges.
202 $this->assertFalse($rule->validate('', array(1, 2)));
203 $this->assertFalse($rule->validate('123', array(1, 2)));
204 $this->assertFalse($rule->validate('áéí', array(1, 2)));
205
206 if (isset($strictformsrequired)) {
207 $CFG->strictformsrequired = $strictformsrequired;
208 }
209 }
210
a3d5830a
PS
211 public function test_generate_id_select() {
212 $el = new MoodleQuickForm_select('choose_one', 'Choose one',
213 array(1 => 'One', '2' => 'Two'));
214 $el->_generateId();
78d4a260 215 $this->assertSame('id_choose_one', $el->getAttribute('id'));
a3d5830a
PS
216 }
217
218 public function test_generate_id_like_repeat() {
219 $el = new MoodleQuickForm_text('text[7]', 'Type something');
220 $el->_generateId();
78d4a260 221 $this->assertSame('id_text_7', $el->getAttribute('id'));
a3d5830a
PS
222 }
223
224 public function test_can_manually_set_id() {
225 $el = new MoodleQuickForm_text('elementname', 'Type something',
226 array('id' => 'customelementid'));
227 $el->_generateId();
78d4a260 228 $this->assertSame('customelementid', $el->getAttribute('id'));
a3d5830a
PS
229 }
230
231 public function test_generate_id_radio() {
232 $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
233 $el->_generateId();
78d4a260 234 $this->assertSame('id_radio_choice_value', $el->getAttribute('id'));
a3d5830a
PS
235 }
236
237 public function test_radio_can_manually_set_id() {
238 $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value',
239 array('id' => 'customelementid2'));
240 $el->_generateId();
78d4a260 241 $this->assertSame('customelementid2', $el->getAttribute('id'));
a3d5830a
PS
242 }
243
244 public function test_generate_id_radio_like_repeat() {
245 $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
246 $el->_generateId();
78d4a260 247 $this->assertSame('id_repeatradio_2_val', $el->getAttribute('id'));
a3d5830a
PS
248 }
249
250 public function test_rendering() {
251 $form = new formslib_test_form();
252 ob_start();
253 $form->display();
254 $html = ob_get_clean();
255
256 $this->assertTag(array('tag'=>'select', 'id'=>'id_choose_one',
257 'attributes'=>array('name'=>'choose_one')), $html);
258
259 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_0',
260 'attributes'=>array('type'=>'text', 'name'=>'text[0]')), $html);
261
262 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_1',
263 'attributes'=>array('type'=>'text', 'name'=>'text[1]')), $html);
264
265 $this->assertTag(array('tag'=>'input', 'id'=>'id_radio_choice_value',
266 'attributes'=>array('type'=>'radio', 'name'=>'radio', 'value'=>'choice_value')), $html);
267
268 $this->assertTag(array('tag'=>'input', 'id'=>'customelementid2',
269 'attributes'=>array('type'=>'radio', 'name'=>'radio2')), $html);
270
271 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_0_2',
272 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[0]', 'value'=>'2')), $html);
273
274 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_1',
275 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'1')), $html);
276
277 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
278 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
279 }
b7d20148
DP
280
281 public function test_settype_debugging_text() {
282 $mform = new formslib_settype_debugging_text();
283 $this->assertDebuggingCalled("Did you remember to call setType() for 'texttest'? Defaulting to PARAM_RAW cleaning.");
284
78d4a260 285 // Check form still there though.
b7d20148
DP
286 $this->expectOutputRegex('/<input[^>]*name="texttest[^>]*type="text/');
287 $mform->display();
288 }
289
290 public function test_settype_debugging_hidden() {
291 $mform = new formslib_settype_debugging_hidden();
292 $this->assertDebuggingCalled("Did you remember to call setType() for 'hiddentest'? Defaulting to PARAM_RAW cleaning.");
293
78d4a260 294 // Check form still there though.
b7d20148
DP
295 $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
296 $mform->display();
297 }
298
9654f68a
FM
299 public function test_settype_debugging_url() {
300 $this->resetAfterTest(true);
301 $this->setAdminUser();
302 $mform = new formslib_settype_debugging_url();
303 $this->assertDebuggingCalled("Did you remember to call setType() for 'urltest'? Defaulting to PARAM_RAW cleaning.");
304
78d4a260 305 // Check form still there though.
9654f68a
FM
306 $this->expectOutputRegex('/<input[^>]*name="urltest"[^>]*type="text/');
307 $mform->display();
308 }
309
b7d20148
DP
310 public function test_settype_debugging_repeat() {
311 $mform = new formslib_settype_debugging_repeat();
9654f68a 312 $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest[0]'? Defaulting to PARAM_RAW cleaning.");
b7d20148 313
78d4a260 314 // Check form still there though.
b7d20148
DP
315 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
316 $mform->display();
317 }
318
319 public function test_settype_debugging_repeat_ok() {
320 $mform = new formslib_settype_debugging_repeat_ok();
321 // No debugging expected here.
322
323 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
324 $mform->display();
325 }
9654f68a
FM
326
327 public function test_settype_debugging_group() {
328 $mform = new formslib_settype_debugging_group();
329 $this->assertDebuggingCalled("Did you remember to call setType() for 'groupel1'? Defaulting to PARAM_RAW cleaning.");
330 $this->expectOutputRegex('/<input[^>]*name="groupel1"[^>]*type="text/');
331 $this->expectOutputRegex('/<input[^>]*name="groupel2"[^>]*type="text/');
332 $mform->display();
333 }
334
335 public function test_settype_debugging_namedgroup() {
336 $mform = new formslib_settype_debugging_namedgroup();
337 $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[groupel1]'? Defaulting to PARAM_RAW cleaning.");
338 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[groupel1\]"[^>]*type="text/');
339 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[groupel2\]"[^>]*type="text/');
340 $mform->display();
341 }
342
343 public function test_settype_debugging_funky_name() {
344 $mform = new formslib_settype_debugging_funky_name();
345 $this->assertDebuggingCalled("Did you remember to call setType() for 'blah[foo][bar][1]'? Defaulting to PARAM_RAW cleaning.");
346 $this->expectOutputRegex('/<input[^>]*name="blah\[foo\]\[bar\]\[0\]"[^>]*type="text/');
347 $this->expectOutputRegex('/<input[^>]*name="blah\[foo\]\[bar\]\[1\]"[^>]*type="text/');
348 $mform->display();
349 }
350
351 public function test_settype_debugging_type_inheritance() {
352 $mform = new formslib_settype_debugging_type_inheritance();
353 $this->expectOutputRegex('/<input[^>]*name="blah\[foo\]\[bar\]\[0\]"[^>]*type="text/');
354 $this->expectOutputRegex('/<input[^>]*name="blah\[bar\]\[foo\]\[1\]"[^>]*type="text/');
355 $this->expectOutputRegex('/<input[^>]*name="blah\[any\]\[other\]\[2\]"[^>]*type="text/');
356 $mform->display();
357 }
a85f745d 358
188c04b0
FM
359 public function test_settype_debugging_type_group_in_repeat() {
360 $mform = new formslib_settype_debugging_type_group_in_repeat();
361 $this->assertDebuggingCalled("Did you remember to call setType() for 'test2[0]'? Defaulting to PARAM_RAW cleaning.");
362 $this->expectOutputRegex('/<input[^>]*name="test1\[0\]"[^>]*type="text/');
363 $this->expectOutputRegex('/<input[^>]*name="test2\[0\]"[^>]*type="text/');
364 $mform->display();
365 }
366
367 public function test_settype_debugging_type_namedgroup_in_repeat() {
368 $mform = new formslib_settype_debugging_type_namedgroup_in_repeat();
369 $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[0][test2]'? Defaulting to PARAM_RAW cleaning.");
370 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[0\]\[test1\]"[^>]*type="text/');
371 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[0\]\[test2\]"[^>]*type="text/');
372 $mform->display();
373 }
374
a85f745d
FM
375 public function test_type_cleaning() {
376 $expectedtypes = array(
377 'simpleel' => PARAM_INT,
378 'groupel1' => PARAM_INT,
379 'groupel2' => PARAM_FLOAT,
380 'groupel3' => PARAM_INT,
381 'namedgroup' => array(
382 'sndgroupel1' => PARAM_INT,
383 'sndgroupel2' => PARAM_FLOAT,
384 'sndgroupel3' => PARAM_INT
385 ),
386 'namedgroupinherit' => array(
387 'thdgroupel1' => PARAM_INT,
388 'thdgroupel2' => PARAM_INT
389 ),
390 'repeatedel' => array(
391 0 => PARAM_INT,
392 1 => PARAM_INT
393 ),
394 'repeatedelinherit' => array(
395 0 => PARAM_INT,
396 1 => PARAM_INT
397 ),
398 'squaretest' => array(
399 0 => PARAM_INT
400 ),
401 'nested' => array(
402 0 => array(
403 'bob' => array(
404 123 => PARAM_INT,
405 'foo' => PARAM_FLOAT
406 ),
407 'xyz' => PARAM_RAW
408 ),
409 1 => PARAM_INT
188c04b0
FM
410 ),
411 'repeatgroupel1' => array(
412 0 => PARAM_INT,
413 1 => PARAM_INT
414 ),
415 'repeatgroupel2' => array(
416 0 => PARAM_INT,
417 1 => PARAM_INT
418 ),
419 'repeatnamedgroup' => array(
420 0 => array(
421 'repeatnamedgroupel1' => PARAM_INT,
422 'repeatnamedgroupel2' => PARAM_INT
423 ),
424 1 => array(
425 'repeatnamedgroupel1' => PARAM_INT,
426 'repeatnamedgroupel2' => PARAM_INT
427 )
a85f745d
FM
428 )
429 );
430 $valuessubmitted = array(
431 'simpleel' => '11.01',
432 'groupel1' => '11.01',
433 'groupel2' => '11.01',
434 'groupel3' => '11.01',
435 'namedgroup' => array(
436 'sndgroupel1' => '11.01',
437 'sndgroupel2' => '11.01',
438 'sndgroupel3' => '11.01'
439 ),
440 'namedgroupinherit' => array(
441 'thdgroupel1' => '11.01',
442 'thdgroupel2' => '11.01'
443 ),
444 'repeatedel' => array(
445 0 => '11.01',
446 1 => '11.01'
447 ),
448 'repeatedelinherit' => array(
449 0 => '11.01',
450 1 => '11.01'
451 ),
452 'squaretest' => array(
453 0 => '11.01'
454 ),
455 'nested' => array(
456 0 => array(
457 'bob' => array(
458 123 => '11.01',
459 'foo' => '11.01'
460 ),
461 'xyz' => '11.01'
462 ),
463 1 => '11.01'
188c04b0
FM
464 ),
465 'repeatgroupel1' => array(
466 0 => '11.01',
467 1 => '11.01'
468 ),
469 'repeatgroupel2' => array(
470 0 => '11.01',
471 1 => '11.01'
472 ),
473 'repeatnamedgroup' => array(
474 0 => array(
475 'repeatnamedgroupel1' => '11.01',
476 'repeatnamedgroupel2' => '11.01'
477 ),
478 1 => array(
479 'repeatnamedgroupel1' => '11.01',
480 'repeatnamedgroupel2' => '11.01'
481 )
a85f745d
FM
482 )
483 );
484 $expectedvalues = array(
485 'simpleel' => 11,
486 'groupel1' => 11,
487 'groupel2' => 11.01,
488 'groupel3' => 11,
489 'namedgroup' => array(
490 'sndgroupel1' => 11,
491 'sndgroupel2' => 11.01,
492 'sndgroupel3' => 11
493 ),
494 'namedgroupinherit' => array(
495 'thdgroupel1' => 11,
496 'thdgroupel2' => 11
497 ),
d5909fd1 498 'repeatable' => 2,
a85f745d
FM
499 'repeatedel' => array(
500 0 => 11,
501 1 => 11
502 ),
d5909fd1 503 'repeatableinherit' => 2,
a85f745d
FM
504 'repeatedelinherit' => array(
505 0 => 11,
506 1 => 11
507 ),
a85f745d
FM
508 'squaretest' => array(
509 0 => 11
510 ),
511 'nested' => array(
512 0 => array(
513 'bob' => array(
514 123 => 11,
515 'foo' => 11.01
516 ),
517 'xyz' => '11.01'
518 ),
519 1 => 11
188c04b0
FM
520 ),
521 'repeatablegroup' => 2,
522 'repeatgroupel1' => array(
523 0 => 11,
524 1 => 11
525 ),
526 'repeatgroupel2' => array(
527 0 => 11,
528 1 => 11
529 ),
530 'repeatablenamedgroup' => 2,
531 'repeatnamedgroup' => array(
532 0 => array(
533 'repeatnamedgroupel1' => 11,
534 'repeatnamedgroupel2' => 11
535 ),
536 1 => array(
537 'repeatnamedgroupel1' => 11,
538 'repeatnamedgroupel2' => 11
539 )
a85f745d
FM
540 )
541 );
542
543 $mform = new formslib_clean_value();
544 $mform->get_form()->updateSubmission($valuessubmitted, null);
d5909fd1
FM
545 foreach ($expectedtypes as $elementname => $expected) {
546 $actual = $mform->get_form()->getCleanType($elementname, $valuessubmitted[$elementname]);
547 $this->assertSame($expected, $actual, "Failed validating clean type of '$elementname'");
a85f745d
FM
548 }
549
550 $data = $mform->get_data();
d5909fd1 551 $this->assertSame($expectedvalues, (array) $data);
a85f745d 552 }
d84c64b7
DM
553
554 /**
555 * MDL-52873
556 */
557 public function test_multiple_modgrade_fields() {
558 $this->resetAfterTest(true);
559
560 $form = new formslib_multiple_modgrade_form();
561 ob_start();
562 $form->display();
563 $html = ob_get_clean();
564
565 $this->assertTag(array('id' => 'fgroup_id_grade1'), $html);
566 $this->assertTag(array('id' => 'id_grade1_modgrade_type'), $html);
567 $this->assertTag(array('id' => 'id_grade1_modgrade_point'), $html);
568 $this->assertTag(array('id' => 'id_grade1_modgrade_scale'), $html);
569
570 $this->assertTag(array('id' => 'fgroup_id_grade2'), $html);
571 $this->assertTag(array('id' => 'id_grade2_modgrade_type'), $html);
572 $this->assertTag(array('id' => 'id_grade2_modgrade_point'), $html);
573 $this->assertTag(array('id' => 'id_grade2_modgrade_scale'), $html);
574
575 $this->assertTag(array('id' => 'fgroup_id_grade_3'), $html);
576 $this->assertTag(array('id' => 'id_grade_3_modgrade_type'), $html);
577 $this->assertTag(array('id' => 'id_grade_3_modgrade_point'), $html);
578 $this->assertTag(array('id' => 'id_grade_3_modgrade_scale'), $html);
579 }
a3d5830a
PS
580}
581
582
583/**
584 * Test form to be used by {@link formslib_test::test_rendering()}.
585 */
586class formslib_test_form extends moodleform {
587 public function definition() {
588 $this->_form->addElement('select', 'choose_one', 'Choose one',
589 array(1 => 'One', '2' => 'Two'));
590
591 $repeatels = array(
592 $this->_form->createElement('text', 'text', 'Type something')
593 );
f35f07e9
EL
594 // TODO: The repeat_elements() is far from perfect. Everything should be
595 // repeated auto-magically by default with options only defining exceptions.
596 // Surely this is caused because we are storing some element information OUT
597 // from the element (type...) at form level. Anyway, the method should do its
598 // work better, no matter of that.
599 $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
a3d5830a
PS
600
601 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
602
603 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
604 array('id' => 'customelementid2'));
605
606 $repeatels = array(
607 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
608 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
609 );
610 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
611 }
f35f07e9 612}
b7d20148 613
78d4a260
PS
614/**
615 * Used to test debugging is called when text added without setType.
616 */
b7d20148
DP
617class formslib_settype_debugging_text extends moodleform {
618 public function definition() {
619 $mform = $this->_form;
620
621 $mform->addElement('text', 'texttest', 'test123', 'testing123');
622 }
623}
624
78d4a260
PS
625/**
626 * Used to test debugging is called when hidden added without setType.
627 */
b7d20148
DP
628class formslib_settype_debugging_hidden extends moodleform {
629 public function definition() {
630 $mform = $this->_form;
631
632 $mform->addElement('hidden', 'hiddentest', '1');
633 }
634}
635
78d4a260
PS
636/**
637 * Used to test debugging is called when hidden added without setType.
638 */
9654f68a
FM
639class formslib_settype_debugging_url extends moodleform {
640 public function definition() {
641 $mform = $this->_form;
642
643 $mform->addElement('url', 'urltest', 'urltest');
644 }
645}
646
78d4a260
PS
647/**
648 * Used to test debugging is called when repeated text added without setType.
649 */
b7d20148
DP
650class formslib_settype_debugging_repeat extends moodleform {
651 public function definition() {
652 $mform = $this->_form;
653
654 $repeatels = array(
655 $mform->createElement('text', 'repeattest', 'Type something')
656 );
657
658 $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
659 }
660}
661
78d4a260
PS
662/**
663 * Used to no debugging is called when correctly test.
664 */
b7d20148
DP
665class formslib_settype_debugging_repeat_ok extends moodleform {
666 public function definition() {
667 $mform = $this->_form;
668
669 $repeatels = array(
670 $mform->createElement('text', 'repeattest', 'Type something')
671 );
672
78d4a260 673 $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
b7d20148
DP
674 }
675}
9654f68a 676
78d4a260
PS
677/**
678 * Used to test if debugging is called when a group contains elements without type.
679 */
9654f68a
FM
680class formslib_settype_debugging_group extends moodleform {
681 public function definition() {
682 $mform = $this->_form;
683 $group = array(
684 $mform->createElement('text', 'groupel1', 'groupel1'),
685 $mform->createElement('text', 'groupel2', 'groupel2')
686 );
687 $mform->addGroup($group);
688 $mform->setType('groupel2', PARAM_INT);
689 }
690}
691
78d4a260
PS
692/**
693 * Used to test if debugging is called when a named group contains elements without type.
694 */
9654f68a
FM
695class formslib_settype_debugging_namedgroup extends moodleform {
696 public function definition() {
697 $mform = $this->_form;
698 $group = array(
699 $mform->createElement('text', 'groupel1', 'groupel1'),
700 $mform->createElement('text', 'groupel2', 'groupel2')
701 );
702 $mform->addGroup($group, 'namedgroup');
703 $mform->setType('namedgroup[groupel2]', PARAM_INT);
704 }
705}
706
78d4a260
PS
707/**
708 * Used to test if debugging is called when has a funky name.
709 */
9654f68a
FM
710class formslib_settype_debugging_funky_name extends moodleform {
711 public function definition() {
712 $mform = $this->_form;
713 $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
714 $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
715 $mform->setType('blah[foo][bar][0]', PARAM_INT);
716 }
717}
718
78d4a260
PS
719/**
720 * Used to test that debugging is not called with type inheritance.
721 */
9654f68a
FM
722class formslib_settype_debugging_type_inheritance extends moodleform {
723 public function definition() {
724 $mform = $this->_form;
725 $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
726 $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
727 $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
728 $mform->setType('blah[foo][bar]', PARAM_INT);
729 $mform->setType('blah[bar]', PARAM_FLOAT);
730 $mform->setType('blah', PARAM_TEXT);
731 }
732}
a85f745d 733
78d4a260
PS
734/**
735 * Used to test the debugging when using groups in repeated elements.
736 */
188c04b0
FM
737class formslib_settype_debugging_type_group_in_repeat extends moodleform {
738 public function definition() {
739 $mform = $this->_form;
740 $groupelements = array(
741 $mform->createElement('text', 'test1', 'test1', 'test'),
742 $mform->createElement('text', 'test2', 'test2', 'test')
743 );
744 $group = $mform->createElement('group', null, 'group1', $groupelements, null, false);
745 $this->repeat_elements(array($group), 1, array('test1' => array('type' => PARAM_INT)), 'hidden', 'button');
746 }
747}
748
78d4a260
PS
749/**
750 * Used to test the debugging when using named groups in repeated elements.
751 */
188c04b0
FM
752class formslib_settype_debugging_type_namedgroup_in_repeat extends moodleform {
753 public function definition() {
754 $mform = $this->_form;
755 $groupelements = array(
756 $mform->createElement('text', 'test1', 'test1', 'test'),
757 $mform->createElement('text', 'test2', 'test2', 'test')
758 );
759 $group = $mform->createElement('group', 'namedgroup', 'group1', $groupelements, null, true);
760 $this->repeat_elements(array($group), 1, array('namedgroup[test1]' => array('type' => PARAM_INT)), 'hidden', 'button');
761 }
762}
763
78d4a260
PS
764/**
765 * Used to check value cleaning.
766 */
a85f745d
FM
767class formslib_clean_value extends moodleform {
768 public function get_form() {
769 return $this->_form;
770 }
771 public function definition() {
772 $mform = $this->_form;
773
774 // Add a simple int.
775 $mform->addElement('text', 'simpleel', 'simpleel');
776 $mform->setType('simpleel', PARAM_INT);
777
778 // Add a non-named group.
779 $group = array(
780 $mform->createElement('text', 'groupel1', 'groupel1'),
781 $mform->createElement('text', 'groupel2', 'groupel2'),
782 $mform->createElement('text', 'groupel3', 'groupel3')
783 );
784 $mform->setType('groupel1', PARAM_INT);
785 $mform->setType('groupel2', PARAM_FLOAT);
786 $mform->setType('groupel3', PARAM_INT);
787 $mform->addGroup($group);
788
789 // Add a named group.
790 $group = array(
791 $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'),
792 $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'),
793 $mform->createElement('text', 'sndgroupel3', 'sndgroupel3')
794 );
795 $mform->addGroup($group, 'namedgroup');
796 $mform->setType('namedgroup[sndgroupel1]', PARAM_INT);
797 $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT);
798 $mform->setType('namedgroup[sndgroupel3]', PARAM_INT);
799
800 // Add a named group, with inheritance.
801 $group = array(
802 $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'),
803 $mform->createElement('text', 'thdgroupel2', 'thdgroupel2')
804 );
805 $mform->addGroup($group, 'namedgroupinherit');
806 $mform->setType('namedgroupinherit', PARAM_INT);
807
808 // Add a repetition.
809 $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel');
810 $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT)), 'repeatable', 'add', 0);
811
812 // Add a repetition, with inheritance.
813 $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit');
814 $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0);
815 $mform->setType('repeatedelinherit', PARAM_INT);
816
817 // Add an arbitrary named element.
818 $mform->addElement('text', 'squaretest[0]', 'squaretest[0]');
819 $mform->setType('squaretest[0]', PARAM_INT);
820
821 // Add an arbitrary nested array named element.
822 $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]');
823 $mform->setType('nested[0][bob][123]', PARAM_INT);
824
825 // Add inheritance test cases.
826 $mform->setType('nested', PARAM_INT);
827 $mform->setType('nested[0]', PARAM_RAW);
828 $mform->setType('nested[0][bob]', PARAM_FLOAT);
829 $mform->addElement('text', 'nested[1]', 'nested[1]');
830 $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
831 $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
188c04b0
FM
832
833 // Add group in repeated element (with extra inheritance).
834 $groupelements = array(
835 $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
836 $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
837 );
838 $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
839 $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT),
840 'repeatgroupel2' => array('type' => PARAM_INT)), 'repeatablegroup', 'add', 0);
841
842 // Add named group in repeated element.
843 $groupelements = array(
844 $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
845 $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
846 );
847 $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
848 $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT),
849 'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);
a85f745d
FM
850 }
851}
d84c64b7
DM
852
853/**
854 * Used to test that modgrade fields get unique id attributes.
855 */
856class formslib_multiple_modgrade_form extends moodleform {
857 public function definition() {
858 $mform = $this->_form;
859 $mform->addElement('modgrade', 'grade1', 'Grade 1');
860 $mform->addElement('modgrade', 'grade2', 'Grade 2');
861 $mform->addElement('modgrade', 'grade[3]', 'Grade 3');
862 }
863}