MDL-41623 ensure all rss links are valid urls.
[moodle.git] / lib / tests / formslib_test.php
blob2a819aed64439bb1abbe990cd7f4a847f8de53cc
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.
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/>.
17 /**
18 * Unit tests for /lib/formslib.php.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/formslib.php');
30 require_once($CFG->libdir . '/form/radio.php');
31 require_once($CFG->libdir . '/form/select.php');
32 require_once($CFG->libdir . '/form/text.php');
35 class formslib_testcase extends advanced_testcase {
37 public function test_require_rule() {
38 global $CFG;
40 $strictformsrequired = null;
41 if (isset($CFG->strictformsrequired)) {
42 $strictformsrequired = $CFG->strictformsrequired;
45 $rule = new MoodleQuickForm_Rule_Required();
47 // First run the tests with strictformsrequired off
48 $CFG->strictformsrequired = false;
49 // Passes
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;'));
78 // Fails
79 $this->assertFalse($rule->validate(''));
80 $this->assertFalse($rule->validate(false));
81 $this->assertFalse($rule->validate(null));
83 // Now run the same tests with it on to make sure things work as expected
84 $CFG->strictformsrequired = true;
85 // Passes
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>'));
101 // Fails
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));
119 if (isset($strictformsrequired)) {
120 $CFG->strictformsrequired = $strictformsrequired;
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();
128 $this->assertEquals('id_choose_one', $el->getAttribute('id'));
131 public function test_generate_id_like_repeat() {
132 $el = new MoodleQuickForm_text('text[7]', 'Type something');
133 $el->_generateId();
134 $this->assertEquals('id_text_7', $el->getAttribute('id'));
137 public function test_can_manually_set_id() {
138 $el = new MoodleQuickForm_text('elementname', 'Type something',
139 array('id' => 'customelementid'));
140 $el->_generateId();
141 $this->assertEquals('customelementid', $el->getAttribute('id'));
144 public function test_generate_id_radio() {
145 $el = new MoodleQuickForm_radio('radio', 'Label', 'Choice label', 'choice_value');
146 $el->_generateId();
147 $this->assertEquals('id_radio_choice_value', $el->getAttribute('id'));
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();
154 $this->assertEquals('customelementid2', $el->getAttribute('id'));
157 public function test_generate_id_radio_like_repeat() {
158 $el = new MoodleQuickForm_radio('repeatradio[2]', 'Label', 'Choice label', 'val');
159 $el->_generateId();
160 $this->assertEquals('id_repeatradio_2_val', $el->getAttribute('id'));
163 public function test_rendering() {
164 $form = new formslib_test_form();
165 ob_start();
166 $form->display();
167 $html = ob_get_clean();
169 $this->assertTag(array('tag'=>'select', 'id'=>'id_choose_one',
170 'attributes'=>array('name'=>'choose_one')), $html);
172 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_0',
173 'attributes'=>array('type'=>'text', 'name'=>'text[0]')), $html);
175 $this->assertTag(array('tag'=>'input', 'id'=>'id_text_1',
176 'attributes'=>array('type'=>'text', 'name'=>'text[1]')), $html);
178 $this->assertTag(array('tag'=>'input', 'id'=>'id_radio_choice_value',
179 'attributes'=>array('type'=>'radio', 'name'=>'radio', 'value'=>'choice_value')), $html);
181 $this->assertTag(array('tag'=>'input', 'id'=>'customelementid2',
182 'attributes'=>array('type'=>'radio', 'name'=>'radio2')), $html);
184 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_0_2',
185 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[0]', 'value'=>'2')), $html);
187 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_1',
188 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'1')), $html);
190 $this->assertTag(array('tag'=>'input', 'id'=>'id_repeatradio_2_2',
191 'attributes'=>array('type'=>'radio', 'name'=>'repeatradio[2]', 'value'=>'2')), $html);
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.");
198 // Check form still there though
199 $this->expectOutputRegex('/<input[^>]*name="texttest[^>]*type="text/');
200 $mform->display();
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.");
207 // Check form still there though
208 $this->expectOutputRegex('/<input[^>]*name="hiddentest[^>]*type="hidden/');
209 $mform->display();
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.");
218 // Check form still there though
219 $this->expectOutputRegex('/<input[^>]*name="urltest"[^>]*type="text/');
220 $mform->display();
223 public function test_settype_debugging_repeat() {
224 $mform = new formslib_settype_debugging_repeat();
225 $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest[0]'? Defaulting to PARAM_RAW cleaning.");
227 // Check form still there though
228 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
229 $mform->display();
232 public function test_settype_debugging_repeat_ok() {
233 $mform = new formslib_settype_debugging_repeat_ok();
234 // No debugging expected here.
236 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
237 $mform->display();
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();
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();
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();
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();
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();
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();
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
299 'namedgroupinherit' => array(
300 'thdgroupel1' => PARAM_INT,
301 'thdgroupel2' => PARAM_INT
303 'repeatedel' => array(
304 0 => PARAM_INT,
305 1 => PARAM_INT
307 'repeatedelinherit' => array(
308 0 => PARAM_INT,
309 1 => PARAM_INT
311 'squaretest' => array(
312 0 => PARAM_INT
314 'nested' => array(
315 0 => array(
316 'bob' => array(
317 123 => PARAM_INT,
318 'foo' => PARAM_FLOAT
320 'xyz' => PARAM_RAW
322 1 => PARAM_INT
324 'repeatgroupel1' => array(
325 0 => PARAM_INT,
326 1 => PARAM_INT
328 'repeatgroupel2' => array(
329 0 => PARAM_INT,
330 1 => PARAM_INT
332 'repeatnamedgroup' => array(
333 0 => array(
334 'repeatnamedgroupel1' => PARAM_INT,
335 'repeatnamedgroupel2' => PARAM_INT
337 1 => array(
338 'repeatnamedgroupel1' => PARAM_INT,
339 'repeatnamedgroupel2' => PARAM_INT
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'
353 'namedgroupinherit' => array(
354 'thdgroupel1' => '11.01',
355 'thdgroupel2' => '11.01'
357 'repeatedel' => array(
358 0 => '11.01',
359 1 => '11.01'
361 'repeatedelinherit' => array(
362 0 => '11.01',
363 1 => '11.01'
365 'squaretest' => array(
366 0 => '11.01'
368 'nested' => array(
369 0 => array(
370 'bob' => array(
371 123 => '11.01',
372 'foo' => '11.01'
374 'xyz' => '11.01'
376 1 => '11.01'
378 'repeatgroupel1' => array(
379 0 => '11.01',
380 1 => '11.01'
382 'repeatgroupel2' => array(
383 0 => '11.01',
384 1 => '11.01'
386 'repeatnamedgroup' => array(
387 0 => array(
388 'repeatnamedgroupel1' => '11.01',
389 'repeatnamedgroupel2' => '11.01'
391 1 => array(
392 'repeatnamedgroupel1' => '11.01',
393 'repeatnamedgroupel2' => '11.01'
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
407 'namedgroupinherit' => array(
408 'thdgroupel1' => 11,
409 'thdgroupel2' => 11
411 'repeatable' => 2,
412 'repeatedel' => array(
413 0 => 11,
414 1 => 11
416 'repeatableinherit' => 2,
417 'repeatedelinherit' => array(
418 0 => 11,
419 1 => 11
421 'squaretest' => array(
422 0 => 11
424 'nested' => array(
425 0 => array(
426 'bob' => array(
427 123 => 11,
428 'foo' => 11.01
430 'xyz' => '11.01'
432 1 => 11
434 'repeatablegroup' => 2,
435 'repeatgroupel1' => array(
436 0 => 11,
437 1 => 11
439 'repeatgroupel2' => array(
440 0 => 11,
441 1 => 11
443 'repeatablenamedgroup' => 2,
444 'repeatnamedgroup' => array(
445 0 => array(
446 'repeatnamedgroupel1' => 11,
447 'repeatnamedgroupel2' => 11
449 1 => array(
450 'repeatnamedgroupel1' => 11,
451 'repeatnamedgroupel2' => 11
456 $mform = new formslib_clean_value();
457 $mform->get_form()->updateSubmission($valuessubmitted, null);
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'");
463 $data = $mform->get_data();
464 $this->assertSame($expectedvalues, (array) $data);
470 * Test form to be used by {@link formslib_test::test_rendering()}.
472 class formslib_test_form extends moodleform {
473 public function definition() {
474 $this->_form->addElement('select', 'choose_one', 'Choose one',
475 array(1 => 'One', '2' => 'Two'));
477 $repeatels = array(
478 $this->_form->createElement('text', 'text', 'Type something')
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');
487 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
489 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
490 array('id' => 'customelementid2'));
492 $repeatels = array(
493 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
494 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
496 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
500 // Used to test debugging is called when text added without setType.
501 class formslib_settype_debugging_text extends moodleform {
502 public function definition() {
503 $mform = $this->_form;
505 $mform->addElement('text', 'texttest', 'test123', 'testing123');
509 // Used to test debugging is called when hidden added without setType.
510 class formslib_settype_debugging_hidden extends moodleform {
511 public function definition() {
512 $mform = $this->_form;
514 $mform->addElement('hidden', 'hiddentest', '1');
518 // Used to test debugging is called when hidden added without setType.
519 class formslib_settype_debugging_url extends moodleform {
520 public function definition() {
521 $mform = $this->_form;
523 $mform->addElement('url', 'urltest', 'urltest');
527 // Used to test debugging is called when repeated text added without setType.
528 class formslib_settype_debugging_repeat extends moodleform {
529 public function definition() {
530 $mform = $this->_form;
532 $repeatels = array(
533 $mform->createElement('text', 'repeattest', 'Type something')
536 $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
540 // Used to no debugging is called when correctly tset
541 class formslib_settype_debugging_repeat_ok extends moodleform {
542 public function definition() {
543 $mform = $this->_form;
545 $repeatels = array(
546 $mform->createElement('text', 'repeattest', 'Type something')
549 $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
553 // Used to test if debugging is called when a group contains elements without type.
554 class formslib_settype_debugging_group extends moodleform {
555 public function definition() {
556 $mform = $this->_form;
557 $group = array(
558 $mform->createElement('text', 'groupel1', 'groupel1'),
559 $mform->createElement('text', 'groupel2', 'groupel2')
561 $mform->addGroup($group);
562 $mform->setType('groupel2', PARAM_INT);
566 // Used to test if debugging is called when a named group contains elements without type.
567 class formslib_settype_debugging_namedgroup extends moodleform {
568 public function definition() {
569 $mform = $this->_form;
570 $group = array(
571 $mform->createElement('text', 'groupel1', 'groupel1'),
572 $mform->createElement('text', 'groupel2', 'groupel2')
574 $mform->addGroup($group, 'namedgroup');
575 $mform->setType('namedgroup[groupel2]', PARAM_INT);
579 // Used to test if debugging is called when has a funky name.
580 class formslib_settype_debugging_funky_name extends moodleform {
581 public function definition() {
582 $mform = $this->_form;
583 $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
584 $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
585 $mform->setType('blah[foo][bar][0]', PARAM_INT);
589 // Used to test that debugging is not called with type inheritance.
590 class formslib_settype_debugging_type_inheritance extends moodleform {
591 public function definition() {
592 $mform = $this->_form;
593 $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
594 $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
595 $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
596 $mform->setType('blah[foo][bar]', PARAM_INT);
597 $mform->setType('blah[bar]', PARAM_FLOAT);
598 $mform->setType('blah', PARAM_TEXT);
602 // Used to test the debugging when using groups in repeated elements.
603 class formslib_settype_debugging_type_group_in_repeat extends moodleform {
604 public function definition() {
605 $mform = $this->_form;
606 $groupelements = array(
607 $mform->createElement('text', 'test1', 'test1', 'test'),
608 $mform->createElement('text', 'test2', 'test2', 'test')
610 $group = $mform->createElement('group', null, 'group1', $groupelements, null, false);
611 $this->repeat_elements(array($group), 1, array('test1' => array('type' => PARAM_INT)), 'hidden', 'button');
615 // Used to test the debugging when using named groups in repeated elements.
616 class formslib_settype_debugging_type_namedgroup_in_repeat extends moodleform {
617 public function definition() {
618 $mform = $this->_form;
619 $groupelements = array(
620 $mform->createElement('text', 'test1', 'test1', 'test'),
621 $mform->createElement('text', 'test2', 'test2', 'test')
623 $group = $mform->createElement('group', 'namedgroup', 'group1', $groupelements, null, true);
624 $this->repeat_elements(array($group), 1, array('namedgroup[test1]' => array('type' => PARAM_INT)), 'hidden', 'button');
628 // Used to check value cleaning.
629 class formslib_clean_value extends moodleform {
630 public function get_form() {
631 return $this->_form;
633 public function definition() {
634 $mform = $this->_form;
636 // Add a simple int.
637 $mform->addElement('text', 'simpleel', 'simpleel');
638 $mform->setType('simpleel', PARAM_INT);
640 // Add a non-named group.
641 $group = array(
642 $mform->createElement('text', 'groupel1', 'groupel1'),
643 $mform->createElement('text', 'groupel2', 'groupel2'),
644 $mform->createElement('text', 'groupel3', 'groupel3')
646 $mform->setType('groupel1', PARAM_INT);
647 $mform->setType('groupel2', PARAM_FLOAT);
648 $mform->setType('groupel3', PARAM_INT);
649 $mform->addGroup($group);
651 // Add a named group.
652 $group = array(
653 $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'),
654 $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'),
655 $mform->createElement('text', 'sndgroupel3', 'sndgroupel3')
657 $mform->addGroup($group, 'namedgroup');
658 $mform->setType('namedgroup[sndgroupel1]', PARAM_INT);
659 $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT);
660 $mform->setType('namedgroup[sndgroupel3]', PARAM_INT);
662 // Add a named group, with inheritance.
663 $group = array(
664 $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'),
665 $mform->createElement('text', 'thdgroupel2', 'thdgroupel2')
667 $mform->addGroup($group, 'namedgroupinherit');
668 $mform->setType('namedgroupinherit', PARAM_INT);
670 // Add a repetition.
671 $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel');
672 $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT)), 'repeatable', 'add', 0);
674 // Add a repetition, with inheritance.
675 $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit');
676 $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0);
677 $mform->setType('repeatedelinherit', PARAM_INT);
679 // Add an arbitrary named element.
680 $mform->addElement('text', 'squaretest[0]', 'squaretest[0]');
681 $mform->setType('squaretest[0]', PARAM_INT);
683 // Add an arbitrary nested array named element.
684 $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]');
685 $mform->setType('nested[0][bob][123]', PARAM_INT);
687 // Add inheritance test cases.
688 $mform->setType('nested', PARAM_INT);
689 $mform->setType('nested[0]', PARAM_RAW);
690 $mform->setType('nested[0][bob]', PARAM_FLOAT);
691 $mform->addElement('text', 'nested[1]', 'nested[1]');
692 $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]');
693 $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]');
695 // Add group in repeated element (with extra inheritance).
696 $groupelements = array(
697 $mform->createElement('text', 'repeatgroupel1', 'repeatgroupel1'),
698 $mform->createElement('text', 'repeatgroupel2', 'repeatgroupel2')
700 $group = $mform->createElement('group', 'repeatgroup', 'repeatgroup', $groupelements, null, false);
701 $this->repeat_elements(array($group), 2, array('repeatgroupel1' => array('type' => PARAM_INT),
702 'repeatgroupel2' => array('type' => PARAM_INT)), 'repeatablegroup', 'add', 0);
704 // Add named group in repeated element.
705 $groupelements = array(
706 $mform->createElement('text', 'repeatnamedgroupel1', 'repeatnamedgroupel1'),
707 $mform->createElement('text', 'repeatnamedgroupel2', 'repeatnamedgroupel2')
709 $group = $mform->createElement('group', 'repeatnamedgroup', 'repeatnamedgroup', $groupelements, null, true);
710 $this->repeat_elements(array($group), 2, array('repeatnamedgroup[repeatnamedgroupel1]' => array('type' => PARAM_INT),
711 'repeatnamedgroup[repeatnamedgroupel2]' => array('type' => PARAM_INT)), 'repeatablenamedgroup', 'add', 0);