on-demand release 2.6beta+
[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 "));
54 $this->assertTrue($rule->validate("0"));
55 $this->assertTrue($rule->validate(0));
56 $this->assertTrue($rule->validate(true));
57 $this->assertTrue($rule->validate(' '));
58 $this->assertTrue($rule->validate(' '));
59 $this->assertTrue($rule->validate("\t"));
60 $this->assertTrue($rule->validate("\n"));
61 $this->assertTrue($rule->validate("\r"));
62 $this->assertTrue($rule->validate("\r\n"));
63 $this->assertTrue($rule->validate(" \t \n \r "));
64 $this->assertTrue($rule->validate('<p></p>'));
65 $this->assertTrue($rule->validate('<p> </p>'));
66 $this->assertTrue($rule->validate('<p>x</p>'));
67 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
68 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
69 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
70 $this->assertTrue($rule->validate('<hr />'));
71 $this->assertTrue($rule->validate('<hr/>'));
72 $this->assertTrue($rule->validate('<hr>'));
73 $this->assertTrue($rule->validate('<hr></hr>'));
74 $this->assertTrue($rule->validate('<br />'));
75 $this->assertTrue($rule->validate('<br/>'));
76 $this->assertTrue($rule->validate('<br>'));
77 $this->assertTrue($rule->validate('&nbsp;'));
78d4a260 78 // Fails.
a3d5830a
PS
79 $this->assertFalse($rule->validate(''));
80 $this->assertFalse($rule->validate(false));
81 $this->assertFalse($rule->validate(null));
82
78d4a260 83 // Now run the same tests with it on to make sure things work as expected.
a3d5830a 84 $CFG->strictformsrequired = true;
78d4a260 85 // Passes.
a3d5830a
PS
86 $this->assertTrue($rule->validate('Something'));
87 $this->assertTrue($rule->validate("Something\nmore"));
88 $this->assertTrue($rule->validate("\nmore"));
89 $this->assertTrue($rule->validate(" more "));
90 $this->assertTrue($rule->validate("0"));
91 $this->assertTrue($rule->validate(0));
92 $this->assertTrue($rule->validate(true));
93 $this->assertTrue($rule->validate('<p>x</p>'));
94 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
95 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
96 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
97 $this->assertTrue($rule->validate('<hr />'));
98 $this->assertTrue($rule->validate('<hr/>'));
99 $this->assertTrue($rule->validate('<hr>'));
100 $this->assertTrue($rule->validate('<hr></hr>'));
78d4a260 101 // Fails.
a3d5830a
PS
102 $this->assertFalse($rule->validate(' '));
103 $this->assertFalse($rule->validate(' '));
104 $this->assertFalse($rule->validate("\t"));
105 $this->assertFalse($rule->validate("\n"));
106 $this->assertFalse($rule->validate("\r"));
107 $this->assertFalse($rule->validate("\r\n"));
108 $this->assertFalse($rule->validate(" \t \n \r "));
109 $this->assertFalse($rule->validate('<p></p>'));
110 $this->assertFalse($rule->validate('<p> </p>'));
111 $this->assertFalse($rule->validate('<br />'));
112 $this->assertFalse($rule->validate('<br/>'));
113 $this->assertFalse($rule->validate('<br>'));
114 $this->assertFalse($rule->validate('&nbsp;'));
115 $this->assertFalse($rule->validate(''));
116 $this->assertFalse($rule->validate(false));
117 $this->assertFalse($rule->validate(null));
118
119 if (isset($strictformsrequired)) {
120 $CFG->strictformsrequired = $strictformsrequired;
121 }
122 }
123
124 public function test_generate_id_select() {
125 $el = new MoodleQuickForm_select('choose_one', 'Choose one',
126 array(1 => 'One', '2' => 'Two'));
127 $el->_generateId();
78d4a260 128 $this->assertSame('id_choose_one', $el->getAttribute('id'));
a3d5830a
PS
129 }
130
131 public function test_generate_id_like_repeat() {
132 $el = new MoodleQuickForm_text('text[7]', 'Type something');
133 $el->_generateId();
78d4a260 134 $this->assertSame('id_text_7', $el->getAttribute('id'));
a3d5830a
PS
135 }
136
137 public function test_can_manually_set_id() {
138 $el = new MoodleQuickForm_text('elementname', 'Type something',
139 array('id' => 'customelementid'));
140 $el->_generateId();
78d4a260 141 $this->assertSame('customelementid', $el->getAttribute('id'));
a3d5830a
PS
142 }
143
144 public function test_generate_id_radio() {
145 $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
146 $el->_generateId();
78d4a260 147 $this->assertSame('id_radio_choice_value', $el->getAttribute('id'));
a3d5830a
PS
148 }
149
150 public function test_radio_can_manually_set_id() {
151 $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value',
152 array('id' => 'customelementid2'));
153 $el->_generateId();
78d4a260 154 $this->assertSame('customelementid2', $el->getAttribute('id'));
a3d5830a
PS
155 }
156
157 public function test_generate_id_radio_like_repeat() {
158 $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
159 $el->_generateId();
78d4a260 160 $this->assertSame('id_repeatradio_2_val', $el->getAttribute('id'));
a3d5830a
PS
161 }
162
163 public function test_rendering() {
164 $form = new formslib_test_form();
165 ob_start();
166 $form->display();
167 $html = ob_get_clean();
168
169 $this->assertTag(array('tag'=>'select', 'id'=>'id_choose_one',
170 'attributes'=>array('name'=>'choose_one')), $html);
171
172 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_0',
173 'attributes'=>array('type'=>'text', 'name'=>'text[0]')), $html);
174
175 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_1',
176 'attributes'=>array('type'=>'text', 'name'=>'text[1]')), $html);
177
178 $this->assertTag(array('tag'=>'input', 'id'=>'id_radio_choice_value',
179 'attributes'=>array('type'=>'radio', 'name'=>'radio', 'value'=>'choice_value')), $html);
180
181 $this->assertTag(array('tag'=>'input', 'id'=>'customelementid2',
182 'attributes'=>array('type'=>'radio', 'name'=>'radio2')), $html);
183
184 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_0_2',
185 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[0]', 'value'=>'2')), $html);
186
187 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_1',
188 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'1')), $html);
189
190 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
191 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
192 }
b7d20148
DP
193
194 public function test_settype_debugging_text() {
195 $mform = new formslib_settype_debugging_text();
196 $this->assertDebuggingCalled("Did you remember to call setType() for 'texttest'? Defaulting to PARAM_RAW cleaning.");
197
78d4a260 198 // Check form still there though.
b7d20148
DP
199 $this->expectOutputRegex('/<input[^>]*name="texttest[^>]*type="text/');
200 $mform->display();
201 }
202
203 public function test_settype_debugging_hidden() {
204 $mform = new formslib_settype_debugging_hidden();
205 $this->assertDebuggingCalled("Did you remember to call setType() for 'hiddentest'? Defaulting to PARAM_RAW cleaning.");
206
78d4a260 207 // Check form still there though.
b7d20148
DP
208 $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
209 $mform->display();
210 }
211
9654f68a
FM
212 public function test_settype_debugging_url() {
213 $this->resetAfterTest(true);
214 $this->setAdminUser();
215 $mform = new formslib_settype_debugging_url();
216 $this->assertDebuggingCalled("Did you remember to call setType() for 'urltest'? Defaulting to PARAM_RAW cleaning.");
217
78d4a260 218 // Check form still there though.
9654f68a
FM
219 $this->expectOutputRegex('/<input[^>]*name="urltest"[^>]*type="text/');
220 $mform->display();
221 }
222
b7d20148
DP
223 public function test_settype_debugging_repeat() {
224 $mform = new formslib_settype_debugging_repeat();
9654f68a 225 $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest[0]'? Defaulting to PARAM_RAW cleaning.");
b7d20148 226
78d4a260 227 // Check form still there though.
b7d20148
DP
228 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
229 $mform->display();
230 }
231
232 public function test_settype_debugging_repeat_ok() {
233 $mform = new formslib_settype_debugging_repeat_ok();
234 // No debugging expected here.
235
236 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
237 $mform->display();
238 }
9654f68a
FM
239
240 public function test_settype_debugging_group() {
241 $mform = new formslib_settype_debugging_group();
242 $this->assertDebuggingCalled("Did you remember to call setType() for 'groupel1'? Defaulting to PARAM_RAW cleaning.");
243 $this->expectOutputRegex('/<input[^>]*name="groupel1"[^>]*type="text/');
244 $this->expectOutputRegex('/<input[^>]*name="groupel2"[^>]*type="text/');
245 $mform->display();
246 }
247
248 public function test_settype_debugging_namedgroup() {
249 $mform = new formslib_settype_debugging_namedgroup();
250 $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[groupel1]'? Defaulting to PARAM_RAW cleaning.");
251 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[groupel1\]"[^>]*type="text/');
252 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[groupel2\]"[^>]*type="text/');
253 $mform->display();
254 }
255
256 public function test_settype_debugging_funky_name() {
257 $mform = new formslib_settype_debugging_funky_name();
258 $this->assertDebuggingCalled("Did you remember to call setType() for 'blah[foo][bar][1]'? Defaulting to PARAM_RAW cleaning.");
259 $this->expectOutputRegex('/<input[^>]*name="blah\[foo\]\[bar\]\[0\]"[^>]*type="text/');
260 $this->expectOutputRegex('/<input[^>]*name="blah\[foo\]\[bar\]\[1\]"[^>]*type="text/');
261 $mform->display();
262 }
263
264 public function test_settype_debugging_type_inheritance() {
265 $mform = new formslib_settype_debugging_type_inheritance();
266 $this->expectOutputRegex('/<input[^>]*name="blah\[foo\]\[bar\]\[0\]"[^>]*type="text/');
267 $this->expectOutputRegex('/<input[^>]*name="blah\[bar\]\[foo\]\[1\]"[^>]*type="text/');
268 $this->expectOutputRegex('/<input[^>]*name="blah\[any\]\[other\]\[2\]"[^>]*type="text/');
269 $mform->display();
270 }
a85f745d 271
188c04b0
FM
272 public function test_settype_debugging_type_group_in_repeat() {
273 $mform = new formslib_settype_debugging_type_group_in_repeat();
274 $this->assertDebuggingCalled("Did you remember to call setType() for 'test2[0]'? Defaulting to PARAM_RAW cleaning.");
275 $this->expectOutputRegex('/<input[^>]*name="test1\[0\]"[^>]*type="text/');
276 $this->expectOutputRegex('/<input[^>]*name="test2\[0\]"[^>]*type="text/');
277 $mform->display();
278 }
279
280 public function test_settype_debugging_type_namedgroup_in_repeat() {
281 $mform = new formslib_settype_debugging_type_namedgroup_in_repeat();
282 $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[0][test2]'? Defaulting to PARAM_RAW cleaning.");
283 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[0\]\[test1\]"[^>]*type="text/');
284 $this->expectOutputRegex('/<input[^>]*name="namedgroup\[0\]\[test2\]"[^>]*type="text/');
285 $mform->display();
286 }
287
a85f745d
FM
288 public function test_type_cleaning() {
289 $expectedtypes = array(
290 'simpleel' => PARAM_INT,
291 'groupel1' => PARAM_INT,
292 'groupel2' => PARAM_FLOAT,
293 'groupel3' => PARAM_INT,
294 'namedgroup' => array(
295 'sndgroupel1' => PARAM_INT,
296 'sndgroupel2' => PARAM_FLOAT,
297 'sndgroupel3' => PARAM_INT
298 ),
299 'namedgroupinherit' => array(
300 'thdgroupel1' => PARAM_INT,
301 'thdgroupel2' => PARAM_INT
302 ),
303 'repeatedel' => array(
304 0 => PARAM_INT,
305 1 => PARAM_INT
306 ),
307 'repeatedelinherit' => array(
308 0 => PARAM_INT,
309 1 => PARAM_INT
310 ),
311 'squaretest' => array(
312 0 => PARAM_INT
313 ),
314 'nested' => array(
315 0 => array(
316 'bob' => array(
317 123 => PARAM_INT,
318 'foo' => PARAM_FLOAT
319 ),
320 'xyz' => PARAM_RAW
321 ),
322 1 => PARAM_INT
188c04b0
FM
323 ),
324 'repeatgroupel1' => array(
325 0 => PARAM_INT,
326 1 => PARAM_INT
327 ),
328 'repeatgroupel2' => array(
329 0 => PARAM_INT,
330 1 => PARAM_INT
331 ),
332 'repeatnamedgroup' => array(
333 0 => array(
334 'repeatnamedgroupel1' => PARAM_INT,
335 'repeatnamedgroupel2' => PARAM_INT
336 ),
337 1 => array(
338 'repeatnamedgroupel1' => PARAM_INT,
339 'repeatnamedgroupel2' => PARAM_INT
340 )
a85f745d
FM
341 )
342 );
343 $valuessubmitted = array(
344 'simpleel' => '11.01',
345 'groupel1' => '11.01',
346 'groupel2' => '11.01',
347 'groupel3' => '11.01',
348 'namedgroup' => array(
349 'sndgroupel1' => '11.01',
350 'sndgroupel2' => '11.01',
351 'sndgroupel3' => '11.01'
352 ),
353 'namedgroupinherit' => array(
354 'thdgroupel1' => '11.01',
355 'thdgroupel2' => '11.01'
356 ),
357 'repeatedel' => array(
358 0 => '11.01',
359 1 => '11.01'
360 ),
361 'repeatedelinherit' => array(
362 0 => '11.01',
363 1 => '11.01'
364 ),
365 'squaretest' => array(
366 0 => '11.01'
367 ),
368 'nested' => array(
369 0 => array(
370 'bob' => array(
371 123 => '11.01',
372 'foo' => '11.01'
373 ),
374 'xyz' => '11.01'
375 ),
376 1 => '11.01'
188c04b0
FM
377 ),
378 'repeatgroupel1' => array(
379 0 => '11.01',
380 1 => '11.01'
381 ),
382 'repeatgroupel2' => array(
383 0 => '11.01',
384 1 => '11.01'
385 ),
386 'repeatnamedgroup' => array(
387 0 => array(
388 'repeatnamedgroupel1' => '11.01',
389 'repeatnamedgroupel2' => '11.01'
390 ),
391 1 => array(
392 'repeatnamedgroupel1' => '11.01',
393 'repeatnamedgroupel2' => '11.01'
394 )
a85f745d
FM
395 )
396 );
397 $expectedvalues = array(
398 'simpleel' => 11,
399 'groupel1' => 11,
400 'groupel2' => 11.01,
401 'groupel3' => 11,
402 'namedgroup' => array(
403 'sndgroupel1' => 11,
404 'sndgroupel2' => 11.01,
405 'sndgroupel3' => 11
406 ),
407 'namedgroupinherit' => array(
408 'thdgroupel1' => 11,
409 'thdgroupel2' => 11
410 ),
d5909fd1 411 'repeatable' => 2,
a85f745d
FM
412 'repeatedel' => array(
413 0 => 11,
414 1 => 11
415 ),
d5909fd1 416 'repeatableinherit' => 2,
a85f745d
FM
417 'repeatedelinherit' => array(
418 0 => 11,
419 1 => 11
420 ),
a85f745d
FM
421 'squaretest' => array(
422 0 => 11
423 ),
424 'nested' => array(
425 0 => array(
426 'bob' => array(
427 123 => 11,
428 'foo' => 11.01
429 ),
430 'xyz' => '11.01'
431 ),
432 1 => 11
188c04b0
FM
433 ),
434 'repeatablegroup' => 2,
435 'repeatgroupel1' => array(
436 0 => 11,
437 1 => 11
438 ),
439 'repeatgroupel2' => array(
440 0 => 11,
441 1 => 11
442 ),
443 'repeatablenamedgroup' => 2,
444 'repeatnamedgroup' => array(
445 0 => array(
446 'repeatnamedgroupel1' => 11,
447 'repeatnamedgroupel2' => 11
448 ),
449 1 => array(
450 'repeatnamedgroupel1' => 11,
451 'repeatnamedgroupel2' => 11
452 )
a85f745d
FM
453 )
454 );
455
456 $mform = new formslib_clean_value();
457 $mform->get_form()->updateSubmission($valuessubmitted, null);
d5909fd1
FM
458 foreach ($expectedtypes as $elementname => $expected) {
459 $actual = $mform->get_form()->getCleanType($elementname, $valuessubmitted[$elementname]);
460 $this->assertSame($expected, $actual, "Failed validating clean type of '$elementname'");
a85f745d
FM
461 }
462
463 $data = $mform->get_data();
d5909fd1 464 $this->assertSame($expectedvalues, (array) $data);
a85f745d 465 }
a3d5830a
PS
466}
467
468
469/**
470 * Test form to be used by {@link formslib_test::test_rendering()}.
471 */
472class formslib_test_form extends moodleform {
473 public function definition() {
474 $this->_form->addElement('select', 'choose_one', 'Choose one',
475 array(1 => 'One', '2' => 'Two'));
476
477 $repeatels = array(
478 $this->_form->createElement('text', 'text', 'Type something')
479 );
f35f07e9
EL
480 // TODO: The repeat_elements() is far from perfect. Everything should be
481 // repeated auto-magically by default with options only defining exceptions.
482 // Surely this is caused because we are storing some element information OUT
483 // from the element (type...) at form level. Anyway, the method should do its
484 // work better, no matter of that.
485 $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
a3d5830a
PS
486
487 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
488
489 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
490 array('id' => 'customelementid2'));
491
492 $repeatels = array(
493 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
494 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
495 );
496 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
497 }
f35f07e9 498}
b7d20148 499
78d4a260
PS
500/**
501 * Used to test debugging is called when text added without setType.
502 */
b7d20148
DP
503class formslib_settype_debugging_text extends moodleform {
504 public function definition() {
505 $mform = $this->_form;
506
507 $mform->addElement('text', 'texttest', 'test123', 'testing123');
508 }
509}
510
78d4a260
PS
511/**
512 * Used to test debugging is called when hidden added without setType.
513 */
b7d20148
DP
514class formslib_settype_debugging_hidden extends moodleform {
515 public function definition() {
516 $mform = $this->_form;
517
518 $mform->addElement('hidden', 'hiddentest', '1');
519 }
520}
521
78d4a260
PS
522/**
523 * Used to test debugging is called when hidden added without setType.
524 */
9654f68a
FM
525class formslib_settype_debugging_url extends moodleform {
526 public function definition() {
527 $mform = $this->_form;
528
529 $mform->addElement('url', 'urltest', 'urltest');
530 }
531}
532
78d4a260
PS
533/**
534 * Used to test debugging is called when repeated text added without setType.
535 */
b7d20148
DP
536class formslib_settype_debugging_repeat extends moodleform {
537 public function definition() {
538 $mform = $this->_form;
539
540 $repeatels = array(
541 $mform->createElement('text', 'repeattest', 'Type something')
542 );
543
544 $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
545 }
546}
547
78d4a260
PS
548/**
549 * Used to no debugging is called when correctly test.
550 */
b7d20148
DP
551class formslib_settype_debugging_repeat_ok extends moodleform {
552 public function definition() {
553 $mform = $this->_form;
554
555 $repeatels = array(
556 $mform->createElement('text', 'repeattest', 'Type something')
557 );
558
78d4a260 559 $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
b7d20148
DP
560 }
561}
9654f68a 562
78d4a260
PS
563/**
564 * Used to test if debugging is called when a group contains elements without type.
565 */
9654f68a
FM
566class formslib_settype_debugging_group extends moodleform {
567 public function definition() {
568 $mform = $this->_form;
569 $group = array(
570 $mform->createElement('text', 'groupel1', 'groupel1'),
571 $mform->createElement('text', 'groupel2', 'groupel2')
572 );
573 $mform->addGroup($group);
574 $mform->setType('groupel2', PARAM_INT);
575 }
576}
577
78d4a260
PS
578/**
579 * Used to test if debugging is called when a named group contains elements without type.
580 */
9654f68a
FM
581class formslib_settype_debugging_namedgroup extends moodleform {
582 public function definition() {
583 $mform = $this->_form;
584 $group = array(
585 $mform->createElement('text', 'groupel1', 'groupel1'),
586 $mform->createElement('text', 'groupel2', 'groupel2')
587 );
588 $mform->addGroup($group, 'namedgroup');
589 $mform->setType('namedgroup[groupel2]', PARAM_INT);
590 }
591}
592
78d4a260
PS
593/**
594 * Used to test if debugging is called when has a funky name.
595 */
9654f68a
FM
596class formslib_settype_debugging_funky_name extends moodleform {
597 public function definition() {
598 $mform = $this->_form;
599 $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
600 $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
601 $mform->setType('blah[foo][bar][0]', PARAM_INT);
602 }
603}
604
78d4a260
PS
605/**
606 * Used to test that debugging is not called with type inheritance.
607 */
9654f68a
FM
608class formslib_settype_debugging_type_inheritance extends moodleform {
609 public function definition() {
610 $mform = $this->_form;
611 $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
612 $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
613 $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
614 $mform->setType('blah[foo][bar]', PARAM_INT);
615 $mform->setType('blah[bar]', PARAM_FLOAT);
616 $mform->setType('blah', PARAM_TEXT);
617 }
618}
a85f745d 619
78d4a260
PS
620/**
621 * Used to test the debugging when using groups in repeated elements.
622 */
188c04b0
FM
623class formslib_settype_debugging_type_group_in_repeat extends moodleform {
624 public function definition() {
625 $mform = $this->_form;
626 $groupelements = array(
627 $mform->createElement('text', 'test1', 'test1', 'test'),
628 $mform->createElement('text', 'test2', 'test2', 'test')
629 );
630 $group = $mform->createElement('group', null, 'group1', $groupelements, null, false);
631 $this->repeat_elements(array($group), 1, array('test1' => array('type' => PARAM_INT)), 'hidden', 'button');
632 }
633}
634
78d4a260
PS
635/**
636 * Used to test the debugging when using named groups in repeated elements.
637 */
188c04b0
FM
638class formslib_settype_debugging_type_namedgroup_in_repeat extends moodleform {
639 public function definition() {
640 $mform = $this->_form;
641 $groupelements = array(
642 $mform->createElement('text', 'test1', 'test1', 'test'),
643 $mform->createElement('text', 'test2', 'test2', 'test')
644 );
645 $group = $mform->createElement('group', 'namedgroup', 'group1', $groupelements, null, true);
646 $this->repeat_elements(array($group), 1, array('namedgroup[test1]' => array('type' => PARAM_INT)), 'hidden', 'button');
647 }
648}
649
78d4a260
PS
650/**
651 * Used to check value cleaning.
652 */
a85f745d
FM
653class formslib_clean_value extends moodleform {
654 public function get_form() {
655 return $this->_form;
656 }
657 public function definition() {
658 $mform = $this->_form;
659
660 // Add a simple int.
661 $mform->addElement('text', 'simpleel', 'simpleel');
662 $mform->setType('simpleel', PARAM_INT);
663
664 // Add a non-named group.
665 $group = array(
666 $mform->createElement('text', 'groupel1', 'groupel1'),
667 $mform->createElement('text', 'groupel2', 'groupel2'),
668 $mform->createElement('text', 'groupel3', 'groupel3')
669 );
670 $mform->setType('groupel1', PARAM_INT);
671 $mform->setType('groupel2', PARAM_FLOAT);
672 $mform->setType('groupel3', PARAM_INT);
673 $mform->addGroup($group);
674
675 // Add a named group.
676 $group = array(
677 $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'),
678 $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'),
679 $mform->createElement('text', 'sndgroupel3', 'sndgroupel3')
680 );
681 $mform->addGroup($group, 'namedgroup');
682 $mform->setType('namedgroup[sndgroupel1]', PARAM_INT);
683 $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT);
684 $mform->setType('namedgroup[sndgroupel3]', PARAM_INT);
685
686 // Add a named group, with inheritance.
687 $group = array(
688 $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'),
689 $mform->createElement('text', 'thdgroupel2', 'thdgroupel2')
690 );
691 $mform->addGroup($group, 'namedgroupinherit');
692 $mform->setType('namedgroupinherit', PARAM_INT);
693
694 // Add a repetition.
695 $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel');
696 $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT)), 'repeatable', 'add', 0);
697
698 // Add a repetition, with inheritance.
699 $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit');
700 $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0);
701 $mform->setType('repeatedelinherit', PARAM_INT);
702
703 // Add an arbitrary named element.
704 $mform->addElement('text', 'squaretest[0]', 'squaretest[0]');
705 $mform->setType('squaretest[0]', PARAM_INT);
706
707 // Add an arbitrary nested array named element.
708 $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]');
709 $mform->setType('nested[0][bob][123]', PARAM_INT);
710
711 // Add inheritance test cases.
712 $mform->setType('nested', PARAM_INT);
713 $mform->setType('nested[0]', PARAM_RAW);
714 $mform->setType('nested[0][bob]', PARAM_FLOAT);
715 $mform->addElement('text', 'nested[1]', 'nested[1]');
716 $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
717 $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
188c04b0
FM
718
719 // Add group in repeated element (with extra inheritance).
720 $groupelements = array(
721 $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
722 $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
723 );
724 $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
725 $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT),
726 'repeatgroupel2' => array('type' => PARAM_INT)), 'repeatablegroup', 'add', 0);
727
728 // Add named group in repeated element.
729 $groupelements = array(
730 $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
731 $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
732 );
733 $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
734 $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT),
735 'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);
a85f745d
FM
736 }
737}