2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Unit tests for /lib/formslib.php.
22 * @copyright 2011 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 use HTML_QuickForm_Rule_Range
;
30 use MoodleQuickForm_radio
;
31 use MoodleQuickForm_Rule_Required
;
32 use MoodleQuickForm_select
;
33 use MoodleQuickForm_text
;
35 defined('MOODLE_INTERNAL') ||
die();
38 require_once($CFG->libdir
. '/formslib.php');
39 require_once($CFG->libdir
. '/form/radio.php');
40 require_once($CFG->libdir
. '/form/select.php');
41 require_once($CFG->libdir
. '/form/text.php');
44 * Unit tests for /lib/formslib.php.
48 * @copyright 2011 Sam Hemelryk
49 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
51 class formslib_test
extends \advanced_testcase
{
53 public function test_require_rule() {
56 $strictformsrequired = null;
57 if (isset($CFG->strictformsrequired
)) {
58 $strictformsrequired = $CFG->strictformsrequired
;
61 $rule = new MoodleQuickForm_Rule_Required();
63 // First run the tests with strictformsrequired off.
64 $CFG->strictformsrequired
= false;
66 $this->assertTrue($rule->validate('Something'));
67 $this->assertTrue($rule->validate("Something\nmore"));
68 $this->assertTrue($rule->validate("\nmore"));
69 $this->assertTrue($rule->validate(" more "));
70 $this->assertTrue($rule->validate('ш'));
71 $this->assertTrue($rule->validate("の"));
72 $this->assertTrue($rule->validate("0"));
73 $this->assertTrue($rule->validate(0));
74 $this->assertTrue($rule->validate(true));
75 $this->assertTrue($rule->validate(' '));
76 $this->assertTrue($rule->validate(' '));
77 $this->assertTrue($rule->validate("\t"));
78 $this->assertTrue($rule->validate("\n"));
79 $this->assertTrue($rule->validate("\r"));
80 $this->assertTrue($rule->validate("\r\n"));
81 $this->assertTrue($rule->validate(" \t \n \r "));
82 $this->assertTrue($rule->validate('<p></p>'));
83 $this->assertTrue($rule->validate('<p> </p>'));
84 $this->assertTrue($rule->validate('<p>x</p>'));
85 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
86 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
87 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
88 $this->assertTrue($rule->validate('<hr />'));
89 $this->assertTrue($rule->validate('<hr/>'));
90 $this->assertTrue($rule->validate('<hr>'));
91 $this->assertTrue($rule->validate('<hr></hr>'));
92 $this->assertTrue($rule->validate('<br />'));
93 $this->assertTrue($rule->validate('<br/>'));
94 $this->assertTrue($rule->validate('<br>'));
95 $this->assertTrue($rule->validate(' '));
97 $this->assertFalse($rule->validate(''));
98 $this->assertFalse($rule->validate(false));
99 $this->assertFalse($rule->validate(null));
101 // Now run the same tests with it on to make sure things work as expected.
102 $CFG->strictformsrequired
= true;
104 $this->assertTrue($rule->validate('Something'));
105 $this->assertTrue($rule->validate("Something\nmore"));
106 $this->assertTrue($rule->validate("\nmore"));
107 $this->assertTrue($rule->validate(" more "));
108 $this->assertTrue($rule->validate("0"));
109 $this->assertTrue($rule->validate('ш'));
110 $this->assertTrue($rule->validate("の"));
111 $this->assertTrue($rule->validate(0));
112 $this->assertTrue($rule->validate(true));
113 $this->assertTrue($rule->validate('<p>x</p>'));
114 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile" />'));
115 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"/>'));
116 $this->assertTrue($rule->validate('<img src="smile.jpg" alt="smile"></img>'));
117 $this->assertTrue($rule->validate('<hr />'));
118 $this->assertTrue($rule->validate('<hr/>'));
119 $this->assertTrue($rule->validate('<hr>'));
120 $this->assertTrue($rule->validate('<hr></hr>'));
122 $this->assertFalse($rule->validate(' '));
123 $this->assertFalse($rule->validate(' '));
124 $this->assertFalse($rule->validate("\t"));
125 $this->assertFalse($rule->validate("\n"));
126 $this->assertFalse($rule->validate("\r"));
127 $this->assertFalse($rule->validate("\r\n"));
128 $this->assertFalse($rule->validate(" \t \n \r "));
129 $this->assertFalse($rule->validate('<p></p>'));
130 $this->assertFalse($rule->validate('<p> </p>'));
131 $this->assertFalse($rule->validate('<br />'));
132 $this->assertFalse($rule->validate('<br/>'));
133 $this->assertFalse($rule->validate('<br>'));
134 $this->assertFalse($rule->validate(' '));
135 $this->assertFalse($rule->validate(''));
136 $this->assertFalse($rule->validate(false));
137 $this->assertFalse($rule->validate(null));
139 if (isset($strictformsrequired)) {
140 $CFG->strictformsrequired
= $strictformsrequired;
144 public function test_range_rule() {
147 require_once('HTML/QuickForm/Rule/Range.php'); // Requires this pear stuff.
149 $strictformsrequired = null;
150 if (isset($CFG->strictformsrequired
)) {
151 $strictformsrequired = $CFG->strictformsrequired
;
154 $rule = new HTML_QuickForm_Rule_Range();
156 // First run the tests with strictformsrequired off.
157 $CFG->strictformsrequired
= false;
159 $rule->setName('minlength'); // Let's verify some min lengths.
160 $this->assertTrue($rule->validate('12', 2));
161 $this->assertTrue($rule->validate('123', 2));
162 $this->assertTrue($rule->validate('áé', 2));
163 $this->assertTrue($rule->validate('áéí', 2));
164 $rule->setName('maxlength'); // Let's verify some max lengths.
165 $this->assertTrue($rule->validate('1', 2));
166 $this->assertTrue($rule->validate('12', 2));
167 $this->assertTrue($rule->validate('á', 2));
168 $this->assertTrue($rule->validate('áé', 2));
169 $rule->setName('----'); // Let's verify some ranges.
170 $this->assertTrue($rule->validate('', array(0, 2)));
171 $this->assertTrue($rule->validate('1', array(0, 2)));
172 $this->assertTrue($rule->validate('12', array(0, 2)));
173 $this->assertTrue($rule->validate('á', array(0, 2)));
174 $this->assertTrue($rule->validate('áé', array(0, 2)));
177 $rule->setName('minlength'); // Let's verify some min lengths.
178 $this->assertFalse($rule->validate('', 2));
179 $this->assertFalse($rule->validate('1', 2));
180 $this->assertFalse($rule->validate('á', 2));
181 $rule->setName('maxlength'); // Let's verify some max lengths.
182 $this->assertFalse($rule->validate('123', 2));
183 $this->assertFalse($rule->validate('áéí', 2));
184 $rule->setName('----'); // Let's verify some ranges.
185 $this->assertFalse($rule->validate('', array(1, 2)));
186 $this->assertFalse($rule->validate('123', array(1, 2)));
187 $this->assertFalse($rule->validate('áéí', array(1, 2)));
189 // Now run the same tests with it on to make sure things work as expected.
190 $CFG->strictformsrequired
= true;
192 $rule->setName('minlength'); // Let's verify some min lengths.
193 $this->assertTrue($rule->validate('12', 2));
194 $this->assertTrue($rule->validate('123', 2));
195 $this->assertTrue($rule->validate('áé', 2));
196 $this->assertTrue($rule->validate('áéí', 2));
197 $rule->setName('maxlength'); // Let's verify some min lengths.
198 $this->assertTrue($rule->validate('1', 2));
199 $this->assertTrue($rule->validate('12', 2));
200 $this->assertTrue($rule->validate('á', 2));
201 $this->assertTrue($rule->validate('áé', 2));
202 $rule->setName('----'); // Let's verify some ranges.
203 $this->assertTrue($rule->validate('', array(0, 2)));
204 $this->assertTrue($rule->validate('1', array(0, 2)));
205 $this->assertTrue($rule->validate('12', array(0, 2)));
206 $this->assertTrue($rule->validate('á', array(0, 2)));
207 $this->assertTrue($rule->validate('áé', array(0, 2)));
210 $rule->setName('minlength'); // Let's verify some min lengths.
211 $this->assertFalse($rule->validate('', 2));
212 $this->assertFalse($rule->validate('1', 2));
213 $this->assertFalse($rule->validate('á', 2));
214 $rule->setName('maxlength'); // Let's verify some min lengths.
215 $this->assertFalse($rule->validate('123', 2));
216 $this->assertFalse($rule->validate('áéí', 2));
217 $rule->setName('----'); // Let's verify some ranges.
218 $this->assertFalse($rule->validate('', array(1, 2)));
219 $this->assertFalse($rule->validate('123', array(1, 2)));
220 $this->assertFalse($rule->validate('áéí', array(1, 2)));
222 if (isset($strictformsrequired)) {
223 $CFG->strictformsrequired
= $strictformsrequired;
227 public function test_generate_id_select() {
228 $el = new MoodleQuickForm_select('choose_one', 'Choose one',
229 array(1 => 'One', '2' => 'Two'));
231 $this->assertSame('id_choose_one', $el->getAttribute('id'));
234 public function test_generate_id_like_repeat() {
235 $el = new MoodleQuickForm_text('text[7]', 'Type something');
237 $this->assertSame('id_text_7', $el->getAttribute('id'));
240 public function test_can_manually_set_id() {
241 $el = new MoodleQuickForm_text('elementname', 'Type something',
242 array('id' => 'customelementid'));
244 $this->assertSame('customelementid', $el->getAttribute('id'));
247 public function test_generate_id_radio() {
248 $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
250 $this->assertSame('id_radio_choice_value', $el->getAttribute('id'));
253 public function test_radio_can_manually_set_id() {
254 $el = new MoodleQuickForm_radio('radio2', 'Label', 'Choice label', 'choice_value',
255 array('id' => 'customelementid2'));
257 $this->assertSame('customelementid2', $el->getAttribute('id'));
260 public function test_generate_id_radio_like_repeat() {
261 $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
263 $this->assertSame('id_repeatradio_2_val', $el->getAttribute('id'));
266 public function test_rendering() {
267 $form = new formslib_test_form();
270 $html = ob_get_clean();
272 $this->assertTag(array('tag'=>'select', 'id'=>'id_choose_one',
273 'attributes'=>array('name'=>'choose_one')), $html);
275 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_0',
276 'attributes'=>array('type'=>'text', 'name'=>'text[0]')), $html);
278 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_1',
279 'attributes'=>array('type'=>'text', 'name'=>'text[1]')), $html);
281 $this->assertTag(array('tag'=>'input', 'id'=>'id_radio_choice_value',
282 'attributes'=>array('type'=>'radio', 'name'=>'radio', 'value'=>'choice_value')), $html);
284 $this->assertTag(array('tag'=>'input', 'id'=>'customelementid2',
285 'attributes'=>array('type'=>'radio', 'name'=>'radio2')), $html);
287 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_0_2',
288 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[0]', 'value'=>'2')), $html);
290 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_1',
291 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'1')), $html);
293 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
294 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
297 public function test_settype_debugging_text() {
298 $mform = new formslib_settype_debugging_text();
299 $this->assertDebuggingCalled("Did you remember to call setType() for 'texttest'? Defaulting to PARAM_RAW cleaning.");
301 // Check form still there though.
302 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="texttest/');
306 public function test_settype_debugging_hidden() {
307 $mform = new formslib_settype_debugging_hidden();
308 $this->assertDebuggingCalled("Did you remember to call setType() for 'hiddentest'? Defaulting to PARAM_RAW cleaning.");
310 // Check form still there though.
311 $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
315 public function test_settype_debugging_url() {
316 $this->resetAfterTest(true);
317 $this->setAdminUser();
318 $mform = new formslib_settype_debugging_url();
319 $this->assertDebuggingCalled("Did you remember to call setType() for 'urltest'? Defaulting to PARAM_RAW cleaning.");
321 // Check form still there though.
322 $this->expectOutputRegex('/<input[^>]*type="url[^>]*name="urltest"/');
326 public function test_settype_debugging_repeat() {
327 $mform = new formslib_settype_debugging_repeat();
328 $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest[0]'? Defaulting to PARAM_RAW cleaning.");
330 // Check form still there though.
331 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="repeattest/');
335 public function test_settype_debugging_repeat_ok() {
336 $mform = new formslib_settype_debugging_repeat_ok();
337 // No debugging expected here.
339 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="repeattest/');
343 public function test_settype_debugging_group() {
344 $mform = new formslib_settype_debugging_group();
345 $this->assertDebuggingCalled("Did you remember to call setType() for 'groupel1'? Defaulting to PARAM_RAW cleaning.");
346 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="groupel1"/');
347 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="groupel2"/');
351 public function test_settype_debugging_namedgroup() {
352 $mform = new formslib_settype_debugging_namedgroup();
353 $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[groupel1]'? Defaulting to PARAM_RAW cleaning.");
354 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[groupel1\]"/');
355 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[groupel2\]"/');
359 public function test_settype_debugging_funky_name() {
360 $mform = new formslib_settype_debugging_funky_name();
361 $this->assertDebuggingCalled("Did you remember to call setType() for 'blah[foo][bar][1]'? Defaulting to PARAM_RAW cleaning.");
362 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[0\]"/');
363 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[1\]"/');
367 public function test_settype_debugging_type_inheritance() {
368 $mform = new formslib_settype_debugging_type_inheritance();
369 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[foo\]\[bar\]\[0\]"/');
370 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[bar\]\[foo\]\[1\]"/');
371 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="blah\[any\]\[other\]\[2\]"/');
375 public function test_settype_debugging_type_group_in_repeat() {
376 $mform = new formslib_settype_debugging_type_group_in_repeat();
377 $this->assertDebuggingCalled("Did you remember to call setType() for 'test2[0]'? Defaulting to PARAM_RAW cleaning.");
378 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="test1\[0\]"/');
379 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="test2\[0\]"/');
383 public function test_settype_debugging_type_namedgroup_in_repeat() {
384 $mform = new formslib_settype_debugging_type_namedgroup_in_repeat();
385 $this->assertDebuggingCalled("Did you remember to call setType() for 'namedgroup[0][test2]'? Defaulting to PARAM_RAW cleaning.");
386 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[0\]\[test1\]"/');
387 $this->expectOutputRegex('/<input[^>]*type="text[^>]*name="namedgroup\[0\]\[test2\]"/');
391 public function test_type_cleaning() {
392 $expectedtypes = array(
393 'simpleel' => PARAM_INT
,
394 'groupel1' => PARAM_INT
,
395 'groupel2' => PARAM_FLOAT
,
396 'groupel3' => PARAM_INT
,
397 'namedgroup' => array(
398 'sndgroupel1' => PARAM_INT
,
399 'sndgroupel2' => PARAM_FLOAT
,
400 'sndgroupel3' => PARAM_INT
402 'namedgroupinherit' => array(
403 'thdgroupel1' => PARAM_INT
,
404 'thdgroupel2' => PARAM_INT
406 'repeatedel' => array(
410 'repeatedelinherit' => array(
414 'squaretest' => array(
427 'repeatgroupel1' => array(
431 'repeatgroupel2' => array(
435 'repeatnamedgroup' => array(
437 'repeatnamedgroupel1' => PARAM_INT
,
438 'repeatnamedgroupel2' => PARAM_INT
441 'repeatnamedgroupel1' => PARAM_INT
,
442 'repeatnamedgroupel2' => PARAM_INT
446 $valuessubmitted = array(
447 'simpleel' => '11.01',
448 'groupel1' => '11.01',
449 'groupel2' => '11.01',
450 'groupel3' => '11.01',
451 'namedgroup' => array(
452 'sndgroupel1' => '11.01',
453 'sndgroupel2' => '11.01',
454 'sndgroupel3' => '11.01'
456 'namedgroupinherit' => array(
457 'thdgroupel1' => '11.01',
458 'thdgroupel2' => '11.01'
460 'repeatedel' => array(
464 'repeatedelinherit' => array(
468 'squaretest' => array(
481 'repeatgroupel1' => array(
485 'repeatgroupel2' => array(
489 'repeatnamedgroup' => array(
491 'repeatnamedgroupel1' => '11.01',
492 'repeatnamedgroupel2' => '11.01'
495 'repeatnamedgroupel1' => '11.01',
496 'repeatnamedgroupel2' => '11.01'
500 $expectedvalues = array(
505 'namedgroup' => array(
507 'sndgroupel2' => 11.01,
510 'namedgroupinherit' => array(
515 'repeatedel' => array(
519 'repeatableinherit' => 2,
520 'repeatedelinherit' => array(
524 'squaretest' => array(
537 'repeatablegroup' => 2,
538 'repeatgroupel1' => array(
542 'repeatgroupel2' => array(
546 'repeatablenamedgroup' => 2,
547 'repeatnamedgroup' => array(
549 'repeatnamedgroupel1' => 11,
550 'repeatnamedgroupel2' => 11
553 'repeatnamedgroupel1' => 11,
554 'repeatnamedgroupel2' => 11
559 $mform = new formslib_clean_value();
560 $mform->get_form()->updateSubmission($valuessubmitted, null);
561 foreach ($expectedtypes as $elementname => $expected) {
562 $actual = $mform->get_form()->getCleanType($elementname, $valuessubmitted[$elementname]);
563 $this->assertSame($expected, $actual, "Failed validating clean type of '$elementname'");
566 $data = $mform->get_data();
567 $this->assertSame($expectedvalues, (array) $data);
573 public function test_multiple_modgrade_fields() {
575 $this->resetAfterTest(true);
577 $CFG->theme
= 'classic';
578 $form = new formslib_multiple_modgrade_form();
581 $html = ob_get_clean();
583 $this->assertTag(array('id' => 'fitem_fgroup_id_grade1'), $html);
584 $this->assertTag(array('id' => 'id_grade1_modgrade_type'), $html);
585 $this->assertTag(array('id' => 'id_grade1_modgrade_point'), $html);
586 $this->assertTag(array('id' => 'id_grade1_modgrade_scale'), $html);
588 $this->assertTag(array('id' => 'fitem_fgroup_id_grade2'), $html);
589 $this->assertTag(array('id' => 'id_grade2_modgrade_type'), $html);
590 $this->assertTag(array('id' => 'id_grade2_modgrade_point'), $html);
591 $this->assertTag(array('id' => 'id_grade2_modgrade_scale'), $html);
593 $this->assertTag(array('id' => 'fitem_fgroup_id_grade_3'), $html);
594 $this->assertTag(array('id' => 'id_grade_3_modgrade_type'), $html);
595 $this->assertTag(array('id' => 'id_grade_3_modgrade_point'), $html);
596 $this->assertTag(array('id' => 'id_grade_3_modgrade_scale'), $html);
600 * Test persistant freeze elements have different id's.
602 public function test_persistantrreeze_element() {
604 $this->resetAfterTest(true);
605 $CFG->theme
= 'classic';
607 $form = new formslib_persistantrreeze_element();
610 $html = ob_get_clean();
612 // Test advcheckbox id's.
613 $this->assertTag(array('id' => 'id_advcheckboxpersistant'), $html);
614 $this->assertTag(array('id' => 'id_advcheckboxnotpersistant'), $html);
615 $this->assertNotTag(array('id' => 'id_advcheckboxnotpersistant_persistant'), $html);
616 $this->assertTag(array('id' => 'id_advcheckboxfrozen'), $html);
618 // Check text element id's.
619 $this->assertTag(array('id' => 'id_textpersistant'), $html);
620 $this->assertTag(array('id' => 'id_textnotpersistant'), $html);
621 $this->assertNotTag(array('id' => 'id_textnotpersistant_persistant'), $html);
622 $this->assertTag(array('id' => 'id_textfrozen'), $html);
623 $this->assertNotTag(array('id' => 'id_textfrozen_persistant'), $html);
628 * Ensure a validation can run at least once per object. See MDL-56259.
630 public function test_multiple_validation() {
631 $this->resetAfterTest(true);
633 // It should be valid.
634 formslib_multiple_validation_form
::mock_submit(['somenumber' => '10']);
635 $form = new formslib_multiple_validation_form();
636 $this->assertTrue($form->is_validated());
637 $this->assertEquals(10, $form->get_data()->somenumber
);
639 // It should not validate.
640 formslib_multiple_validation_form
::mock_submit(['somenumber' => '-5']);
641 $form = new formslib_multiple_validation_form();
642 $this->assertFalse($form->is_validated());
643 $this->assertNull($form->get_data());
647 * MDL-56233 - Tests mocking a form inside a namespace.
649 public function test_mock_submit() {
650 require_once(__DIR__
.'/fixtures/namespaced_form.php');
651 \local_unittests\namespaced_form\exampleform
::mock_submit(['title' => 'Mocked Value']);
652 $form = new \local_unittests\namespaced_form\
exampleform();
654 // Here is the problem, this is the expected hidden field name.
655 $expected = '_qf__local_unittests_namespaced_form_exampleform';
656 self
::assertArrayHasKey($expected, $_POST);
658 // This should work now, before it would fail.
659 self
::assertTrue($form->is_submitted());
660 self
::assertSame('Mocked Value', $form->get_data()->title
);
666 * Test form to be used by {@link formslib_test::test_rendering()}.
668 class formslib_test_form
extends moodleform
{
669 public function definition() {
670 $this->_form
->addElement('select', 'choose_one', 'Choose one',
671 array(1 => 'One', '2' => 'Two'));
674 $this->_form
->createElement('text', 'text', 'Type something')
676 // TODO: The repeat_elements() is far from perfect. Everything should be
677 // repeated auto-magically by default with options only defining exceptions.
678 // Surely this is caused because we are storing some element information OUT
679 // from the element (type...) at form level. Anyway, the method should do its
680 // work better, no matter of that.
681 $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW
)), 'numtexts', 'addtexts');
683 $this->_form
->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
685 $this->_form
->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
686 array('id' => 'customelementid2'));
689 $this->_form
->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
690 $this->_form
->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
692 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
697 * Used to test debugging is called when text added without setType.
699 class formslib_settype_debugging_text
extends moodleform
{
700 public function definition() {
701 $mform = $this->_form
;
703 $mform->addElement('text', 'texttest', 'test123', 'testing123');
708 * Used to test debugging is called when hidden added without setType.
710 class formslib_settype_debugging_hidden
extends moodleform
{
711 public function definition() {
712 $mform = $this->_form
;
714 $mform->addElement('hidden', 'hiddentest', '1');
719 * Used to test debugging is called when hidden added without setType.
721 class formslib_settype_debugging_url
extends moodleform
{
722 public function definition() {
723 $mform = $this->_form
;
725 $mform->addElement('url', 'urltest', 'urltest');
730 * Used to test debugging is called when repeated text added without setType.
732 class formslib_settype_debugging_repeat
extends moodleform
{
733 public function definition() {
734 $mform = $this->_form
;
737 $mform->createElement('text', 'repeattest', 'Type something')
740 $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
745 * Used to no debugging is called when correctly test.
747 class formslib_settype_debugging_repeat_ok
extends moodleform
{
748 public function definition() {
749 $mform = $this->_form
;
752 $mform->createElement('text', 'repeattest', 'Type something')
755 $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW
)), 'numtexts', 'addtexts');
760 * Used to test if debugging is called when a group contains elements without type.
762 class formslib_settype_debugging_group
extends moodleform
{
763 public function definition() {
764 $mform = $this->_form
;
766 $mform->createElement('text', 'groupel1', 'groupel1'),
767 $mform->createElement('text', 'groupel2', 'groupel2')
769 $mform->addGroup($group);
770 $mform->setType('groupel2', PARAM_INT
);
775 * Used to test if debugging is called when a named group contains elements without type.
777 class formslib_settype_debugging_namedgroup
extends moodleform
{
778 public function definition() {
779 $mform = $this->_form
;
781 $mform->createElement('text', 'groupel1', 'groupel1'),
782 $mform->createElement('text', 'groupel2', 'groupel2')
784 $mform->addGroup($group, 'namedgroup');
785 $mform->setType('namedgroup[groupel2]', PARAM_INT
);
790 * Used to test if debugging is called when has a funky name.
792 class formslib_settype_debugging_funky_name
extends moodleform
{
793 public function definition() {
794 $mform = $this->_form
;
795 $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
796 $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
797 $mform->setType('blah[foo][bar][0]', PARAM_INT
);
802 * Used to test that debugging is not called with type inheritance.
804 class formslib_settype_debugging_type_inheritance
extends moodleform
{
805 public function definition() {
806 $mform = $this->_form
;
807 $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
808 $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
809 $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
810 $mform->setType('blah[foo][bar]', PARAM_INT
);
811 $mform->setType('blah[bar]', PARAM_FLOAT
);
812 $mform->setType('blah', PARAM_TEXT
);
817 * Used to test the debugging when using groups in repeated elements.
819 class formslib_settype_debugging_type_group_in_repeat
extends moodleform
{
820 public function definition() {
821 $mform = $this->_form
;
822 $groupelements = array(
823 $mform->createElement('text', 'test1', 'test1', 'test'),
824 $mform->createElement('text', 'test2', 'test2', 'test')
826 $group = $mform->createElement('group', null, 'group1', $groupelements, null, false);
827 $this->repeat_elements(array($group), 1, array('test1' => array('type' => PARAM_INT
)), 'hidden', 'button');
832 * Used to test the debugging when using named groups in repeated elements.
834 class formslib_settype_debugging_type_namedgroup_in_repeat
extends moodleform
{
835 public function definition() {
836 $mform = $this->_form
;
837 $groupelements = array(
838 $mform->createElement('text', 'test1', 'test1', 'test'),
839 $mform->createElement('text', 'test2', 'test2', 'test')
841 $group = $mform->createElement('group', 'namedgroup', 'group1', $groupelements, null, true);
842 $this->repeat_elements(array($group), 1, array('namedgroup[test1]' => array('type' => PARAM_INT
)), 'hidden', 'button');
847 * Used to check value cleaning.
849 class formslib_clean_value
extends moodleform
{
850 public function get_form() {
853 public function definition() {
854 $mform = $this->_form
;
857 $mform->addElement('text', 'simpleel', 'simpleel');
858 $mform->setType('simpleel', PARAM_INT
);
860 // Add a non-named group.
862 $mform->createElement('text', 'groupel1', 'groupel1'),
863 $mform->createElement('text', 'groupel2', 'groupel2'),
864 $mform->createElement('text', 'groupel3', 'groupel3')
866 $mform->setType('groupel1', PARAM_INT
);
867 $mform->setType('groupel2', PARAM_FLOAT
);
868 $mform->setType('groupel3', PARAM_INT
);
869 $mform->addGroup($group);
871 // Add a named group.
873 $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'),
874 $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'),
875 $mform->createElement('text', 'sndgroupel3', 'sndgroupel3')
877 $mform->addGroup($group, 'namedgroup');
878 $mform->setType('namedgroup[sndgroupel1]', PARAM_INT
);
879 $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT
);
880 $mform->setType('namedgroup[sndgroupel3]', PARAM_INT
);
882 // Add a named group, with inheritance.
884 $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'),
885 $mform->createElement('text', 'thdgroupel2', 'thdgroupel2')
887 $mform->addGroup($group, 'namedgroupinherit');
888 $mform->setType('namedgroupinherit', PARAM_INT
);
891 $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel');
892 $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT
)), 'repeatable', 'add', 0);
894 // Add a repetition, with inheritance.
895 $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit');
896 $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0);
897 $mform->setType('repeatedelinherit', PARAM_INT
);
899 // Add an arbitrary named element.
900 $mform->addElement('text', 'squaretest[0]', 'squaretest[0]');
901 $mform->setType('squaretest[0]', PARAM_INT
);
903 // Add an arbitrary nested array named element.
904 $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]');
905 $mform->setType('nested[0][bob][123]', PARAM_INT
);
907 // Add inheritance test cases.
908 $mform->setType('nested', PARAM_INT
);
909 $mform->setType('nested[0]', PARAM_RAW
);
910 $mform->setType('nested[0][bob]', PARAM_FLOAT
);
911 $mform->addElement('text', 'nested[1]', 'nested[1]');
912 $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
913 $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
915 // Add group in repeated element (with extra inheritance).
916 $groupelements = array(
917 $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
918 $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
920 $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
921 $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT
),
922 'repeatgroupel2' => array('type' => PARAM_INT
)), 'repeatablegroup', 'add', 0);
924 // Add named group in repeated element.
925 $groupelements = array(
926 $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
927 $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
929 $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
930 $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT
),
931 'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT
)), 'repeatablenamedgroup', 'add', 0);
936 * Used to test that modgrade fields get unique id attributes.
938 class formslib_multiple_modgrade_form
extends moodleform
{
939 public function definition() {
940 $mform = $this->_form
;
941 $mform->addElement('modgrade', 'grade1', 'Grade 1');
942 $mform->addElement('modgrade', 'grade2', 'Grade 2');
943 $mform->addElement('modgrade', 'grade[3]', 'Grade 3');
948 * Used to test frozen elements get unique id attributes.
950 class formslib_persistantrreeze_element
extends moodleform
{
951 public function definition() {
952 $mform = $this->_form
;
954 // Create advanced checkbox.
956 $advcheckboxpersistant = $mform->addElement('advcheckbox', 'advcheckboxpersistant', 'advcheckbox');
957 $mform->setType('advcheckboxpersistant', PARAM_BOOL
);
958 $advcheckboxpersistant->setChecked(true);
959 $advcheckboxpersistant->freeze();
960 $advcheckboxpersistant->setPersistantFreeze(true);
962 $advcheckboxfrozen = $mform->addElement('advcheckbox', 'advcheckboxfrozen', 'advcheckbox');
963 $mform->setType('advcheckboxfrozen', PARAM_BOOL
);
964 $advcheckboxfrozen->setChecked(true);
965 $advcheckboxfrozen->freeze();
966 // Neither persistant nor Frozen.
967 $mform->addElement('advcheckbox', 'advcheckboxnotpersistant', 'advcheckbox');
968 $mform->setType('advcheckboxnotpersistant', PARAM_BOOL
);
970 // Create text fields.
972 $elpersistant = $mform->addElement('text', 'textpersistant', 'test', 'test');
973 $mform->setType('textpersistant', PARAM_TEXT
);
974 $elpersistant->freeze();
975 $elpersistant->setPersistantFreeze(true);
977 $elfrozen = $mform->addElement('text', 'textfrozen', 'test', 'test');
978 $mform->setType('textfrozen', PARAM_TEXT
);
980 // Neither persistant nor Frozen.
981 $mform->addElement('text', 'textnotpersistant', 'test', 'test');
982 $mform->setType('textnotpersistant', PARAM_TEXT
);
987 * Used to test that you can validate a form more than once. See MDL-56250.
989 * @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
990 * @copyright 2016 Catalyst IT
991 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
993 class formslib_multiple_validation_form
extends moodleform
{
995 * Simple definition, one text field which can have a number.
997 public function definition() {
998 $mform = $this->_form
;
999 $mform->addElement('text', 'somenumber');
1000 $mform->setType('somenumber', PARAM_INT
);
1004 * The number cannot be negative.
1005 * @param array $data An array of form data
1006 * @param array $files An array of form files
1007 * @return array Error messages
1009 public function validation($data, $files) {
1010 $errors = parent
::validation($data, $files);
1011 if ($data['somenumber'] < 0) {
1012 $errors['somenumber'] = 'The number cannot be negative.';