Commit | Line | Data |
---|---|---|
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 | ||
26 | defined('MOODLE_INTERNAL') || die(); | |
27 | ||
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'); | |
33 | ||
34 | ||
b7d20148 | 35 | class 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 | ||
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(' ')); | |
78 | // Fails | |
79 | $this->assertFalse($rule->validate('')); | |
80 | $this->assertFalse($rule->validate(false)); | |
81 | $this->assertFalse($rule->validate(null)); | |
82 | ||
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(' ')); | |
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(); | |
128 | $this->assertEquals('id_choose_one', $el->getAttribute('id')); | |
129 | } | |
130 | ||
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')); | |
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(); | |
141 | $this->assertEquals('customelementid', $el->getAttribute('id')); | |
142 | } | |
143 | ||
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')); | |
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(); | |
154 | $this->assertEquals('customelementid2', $el->getAttribute('id')); | |
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(); | |
160 | $this->assertEquals('id_repeatradio_2_val', $el->getAttribute('id')); | |
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 | ||
198 | // Check form still there though | |
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 | ||
207 | // Check form still there though | |
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 | ||
218 | // Check form still there though | |
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 DP |
226 | |
227 | // Check form still there though | |
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 FM |
271 | |
272 | public function test_type_cleaning() { | |
273 | $expectedtypes = array( | |
274 | 'simpleel' => PARAM_INT, | |
275 | 'groupel1' => PARAM_INT, | |
276 | 'groupel2' => PARAM_FLOAT, | |
277 | 'groupel3' => PARAM_INT, | |
278 | 'namedgroup' => array( | |
279 | 'sndgroupel1' => PARAM_INT, | |
280 | 'sndgroupel2' => PARAM_FLOAT, | |
281 | 'sndgroupel3' => PARAM_INT | |
282 | ), | |
283 | 'namedgroupinherit' => array( | |
284 | 'thdgroupel1' => PARAM_INT, | |
285 | 'thdgroupel2' => PARAM_INT | |
286 | ), | |
287 | 'repeatedel' => array( | |
288 | 0 => PARAM_INT, | |
289 | 1 => PARAM_INT | |
290 | ), | |
291 | 'repeatedelinherit' => array( | |
292 | 0 => PARAM_INT, | |
293 | 1 => PARAM_INT | |
294 | ), | |
295 | 'squaretest' => array( | |
296 | 0 => PARAM_INT | |
297 | ), | |
298 | 'nested' => array( | |
299 | 0 => array( | |
300 | 'bob' => array( | |
301 | 123 => PARAM_INT, | |
302 | 'foo' => PARAM_FLOAT | |
303 | ), | |
304 | 'xyz' => PARAM_RAW | |
305 | ), | |
306 | 1 => PARAM_INT | |
307 | ) | |
308 | ); | |
309 | $valuessubmitted = array( | |
310 | 'simpleel' => '11.01', | |
311 | 'groupel1' => '11.01', | |
312 | 'groupel2' => '11.01', | |
313 | 'groupel3' => '11.01', | |
314 | 'namedgroup' => array( | |
315 | 'sndgroupel1' => '11.01', | |
316 | 'sndgroupel2' => '11.01', | |
317 | 'sndgroupel3' => '11.01' | |
318 | ), | |
319 | 'namedgroupinherit' => array( | |
320 | 'thdgroupel1' => '11.01', | |
321 | 'thdgroupel2' => '11.01' | |
322 | ), | |
323 | 'repeatedel' => array( | |
324 | 0 => '11.01', | |
325 | 1 => '11.01' | |
326 | ), | |
327 | 'repeatedelinherit' => array( | |
328 | 0 => '11.01', | |
329 | 1 => '11.01' | |
330 | ), | |
331 | 'squaretest' => array( | |
332 | 0 => '11.01' | |
333 | ), | |
334 | 'nested' => array( | |
335 | 0 => array( | |
336 | 'bob' => array( | |
337 | 123 => '11.01', | |
338 | 'foo' => '11.01' | |
339 | ), | |
340 | 'xyz' => '11.01' | |
341 | ), | |
342 | 1 => '11.01' | |
343 | ) | |
344 | ); | |
345 | $expectedvalues = array( | |
346 | 'simpleel' => 11, | |
347 | 'groupel1' => 11, | |
348 | 'groupel2' => 11.01, | |
349 | 'groupel3' => 11, | |
350 | 'namedgroup' => array( | |
351 | 'sndgroupel1' => 11, | |
352 | 'sndgroupel2' => 11.01, | |
353 | 'sndgroupel3' => 11 | |
354 | ), | |
355 | 'namedgroupinherit' => array( | |
356 | 'thdgroupel1' => 11, | |
357 | 'thdgroupel2' => 11 | |
358 | ), | |
d5909fd1 | 359 | 'repeatable' => 2, |
a85f745d FM |
360 | 'repeatedel' => array( |
361 | 0 => 11, | |
362 | 1 => 11 | |
363 | ), | |
d5909fd1 | 364 | 'repeatableinherit' => 2, |
a85f745d FM |
365 | 'repeatedelinherit' => array( |
366 | 0 => 11, | |
367 | 1 => 11 | |
368 | ), | |
a85f745d FM |
369 | 'squaretest' => array( |
370 | 0 => 11 | |
371 | ), | |
372 | 'nested' => array( | |
373 | 0 => array( | |
374 | 'bob' => array( | |
375 | 123 => 11, | |
376 | 'foo' => 11.01 | |
377 | ), | |
378 | 'xyz' => '11.01' | |
379 | ), | |
380 | 1 => 11 | |
381 | ) | |
382 | ); | |
383 | ||
384 | $mform = new formslib_clean_value(); | |
385 | $mform->get_form()->updateSubmission($valuessubmitted, null); | |
d5909fd1 FM |
386 | foreach ($expectedtypes as $elementname => $expected) { |
387 | $actual = $mform->get_form()->getCleanType($elementname, $valuessubmitted[$elementname]); | |
388 | $this->assertSame($expected, $actual, "Failed validating clean type of '$elementname'"); | |
a85f745d FM |
389 | } |
390 | ||
391 | $data = $mform->get_data(); | |
d5909fd1 | 392 | $this->assertSame($expectedvalues, (array) $data); |
a85f745d | 393 | } |
a3d5830a PS |
394 | } |
395 | ||
396 | ||
397 | /** | |
398 | * Test form to be used by {@link formslib_test::test_rendering()}. | |
399 | */ | |
400 | class formslib_test_form extends moodleform { | |
401 | public function definition() { | |
402 | $this->_form->addElement('select', 'choose_one', 'Choose one', | |
403 | array(1 => 'One', '2' => 'Two')); | |
404 | ||
405 | $repeatels = array( | |
406 | $this->_form->createElement('text', 'text', 'Type something') | |
407 | ); | |
f35f07e9 EL |
408 | // TODO: The repeat_elements() is far from perfect. Everything should be |
409 | // repeated auto-magically by default with options only defining exceptions. | |
410 | // Surely this is caused because we are storing some element information OUT | |
411 | // from the element (type...) at form level. Anyway, the method should do its | |
412 | // work better, no matter of that. | |
413 | $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts'); | |
a3d5830a PS |
414 | |
415 | $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value'); | |
416 | ||
417 | $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value', | |
418 | array('id' => 'customelementid2')); | |
419 | ||
420 | $repeatels = array( | |
421 | $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1), | |
422 | $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2), | |
423 | ); | |
424 | $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios'); | |
425 | } | |
f35f07e9 | 426 | } |
b7d20148 DP |
427 | |
428 | // Used to test debugging is called when text added without setType. | |
429 | class formslib_settype_debugging_text extends moodleform { | |
430 | public function definition() { | |
431 | $mform = $this->_form; | |
432 | ||
433 | $mform->addElement('text', 'texttest', 'test123', 'testing123'); | |
434 | } | |
435 | } | |
436 | ||
437 | // Used to test debugging is called when hidden added without setType. | |
438 | class formslib_settype_debugging_hidden extends moodleform { | |
439 | public function definition() { | |
440 | $mform = $this->_form; | |
441 | ||
442 | $mform->addElement('hidden', 'hiddentest', '1'); | |
443 | } | |
444 | } | |
445 | ||
9654f68a FM |
446 | // Used to test debugging is called when hidden added without setType. |
447 | class formslib_settype_debugging_url extends moodleform { | |
448 | public function definition() { | |
449 | $mform = $this->_form; | |
450 | ||
451 | $mform->addElement('url', 'urltest', 'urltest'); | |
452 | } | |
453 | } | |
454 | ||
b7d20148 DP |
455 | // Used to test debugging is called when repeated text added without setType. |
456 | class formslib_settype_debugging_repeat extends moodleform { | |
457 | public function definition() { | |
458 | $mform = $this->_form; | |
459 | ||
460 | $repeatels = array( | |
461 | $mform->createElement('text', 'repeattest', 'Type something') | |
462 | ); | |
463 | ||
464 | $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts'); | |
465 | } | |
466 | } | |
467 | ||
468 | // Used to no debugging is called when correctly tset | |
469 | class formslib_settype_debugging_repeat_ok extends moodleform { | |
470 | public function definition() { | |
471 | $mform = $this->_form; | |
472 | ||
473 | $repeatels = array( | |
474 | $mform->createElement('text', 'repeattest', 'Type something') | |
475 | ); | |
476 | ||
477 | $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts'); | |
478 | } | |
479 | } | |
9654f68a FM |
480 | |
481 | // Used to test if debugging is called when a group contains elements without type. | |
482 | class formslib_settype_debugging_group extends moodleform { | |
483 | public function definition() { | |
484 | $mform = $this->_form; | |
485 | $group = array( | |
486 | $mform->createElement('text', 'groupel1', 'groupel1'), | |
487 | $mform->createElement('text', 'groupel2', 'groupel2') | |
488 | ); | |
489 | $mform->addGroup($group); | |
490 | $mform->setType('groupel2', PARAM_INT); | |
491 | } | |
492 | } | |
493 | ||
494 | // Used to test if debugging is called when a named group contains elements without type. | |
495 | class formslib_settype_debugging_namedgroup extends moodleform { | |
496 | public function definition() { | |
497 | $mform = $this->_form; | |
498 | $group = array( | |
499 | $mform->createElement('text', 'groupel1', 'groupel1'), | |
500 | $mform->createElement('text', 'groupel2', 'groupel2') | |
501 | ); | |
502 | $mform->addGroup($group, 'namedgroup'); | |
503 | $mform->setType('namedgroup[groupel2]', PARAM_INT); | |
504 | } | |
505 | } | |
506 | ||
507 | // Used to test if debugging is called when has a funky name. | |
508 | class formslib_settype_debugging_funky_name extends moodleform { | |
509 | public function definition() { | |
510 | $mform = $this->_form; | |
511 | $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test'); | |
512 | $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test'); | |
513 | $mform->setType('blah[foo][bar][0]', PARAM_INT); | |
514 | } | |
515 | } | |
516 | ||
a85f745d | 517 | // Used to test that debugging is not called with type inheritance. |
9654f68a FM |
518 | class formslib_settype_debugging_type_inheritance extends moodleform { |
519 | public function definition() { | |
520 | $mform = $this->_form; | |
521 | $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test'); | |
522 | $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test'); | |
523 | $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test'); | |
524 | $mform->setType('blah[foo][bar]', PARAM_INT); | |
525 | $mform->setType('blah[bar]', PARAM_FLOAT); | |
526 | $mform->setType('blah', PARAM_TEXT); | |
527 | } | |
528 | } | |
a85f745d FM |
529 | |
530 | class formslib_clean_value extends moodleform { | |
531 | public function get_form() { | |
532 | return $this->_form; | |
533 | } | |
534 | public function definition() { | |
535 | $mform = $this->_form; | |
536 | ||
537 | // Add a simple int. | |
538 | $mform->addElement('text', 'simpleel', 'simpleel'); | |
539 | $mform->setType('simpleel', PARAM_INT); | |
540 | ||
541 | // Add a non-named group. | |
542 | $group = array( | |
543 | $mform->createElement('text', 'groupel1', 'groupel1'), | |
544 | $mform->createElement('text', 'groupel2', 'groupel2'), | |
545 | $mform->createElement('text', 'groupel3', 'groupel3') | |
546 | ); | |
547 | $mform->setType('groupel1', PARAM_INT); | |
548 | $mform->setType('groupel2', PARAM_FLOAT); | |
549 | $mform->setType('groupel3', PARAM_INT); | |
550 | $mform->addGroup($group); | |
551 | ||
552 | // Add a named group. | |
553 | $group = array( | |
554 | $mform->createElement('text', 'sndgroupel1', 'sndgroupel1'), | |
555 | $mform->createElement('text', 'sndgroupel2', 'sndgroupel2'), | |
556 | $mform->createElement('text', 'sndgroupel3', 'sndgroupel3') | |
557 | ); | |
558 | $mform->addGroup($group, 'namedgroup'); | |
559 | $mform->setType('namedgroup[sndgroupel1]', PARAM_INT); | |
560 | $mform->setType('namedgroup[sndgroupel2]', PARAM_FLOAT); | |
561 | $mform->setType('namedgroup[sndgroupel3]', PARAM_INT); | |
562 | ||
563 | // Add a named group, with inheritance. | |
564 | $group = array( | |
565 | $mform->createElement('text', 'thdgroupel1', 'thdgroupel1'), | |
566 | $mform->createElement('text', 'thdgroupel2', 'thdgroupel2') | |
567 | ); | |
568 | $mform->addGroup($group, 'namedgroupinherit'); | |
569 | $mform->setType('namedgroupinherit', PARAM_INT); | |
570 | ||
571 | // Add a repetition. | |
572 | $repeat = $mform->createElement('text', 'repeatedel', 'repeatedel'); | |
573 | $this->repeat_elements(array($repeat), 2, array('repeatedel' => array('type' => PARAM_INT)), 'repeatable', 'add', 0); | |
574 | ||
575 | // Add a repetition, with inheritance. | |
576 | $repeat = $mform->createElement('text', 'repeatedelinherit', 'repeatedelinherit'); | |
577 | $this->repeat_elements(array($repeat), 2, array(), 'repeatableinherit', 'add', 0); | |
578 | $mform->setType('repeatedelinherit', PARAM_INT); | |
579 | ||
580 | // Add an arbitrary named element. | |
581 | $mform->addElement('text', 'squaretest[0]', 'squaretest[0]'); | |
582 | $mform->setType('squaretest[0]', PARAM_INT); | |
583 | ||
584 | // Add an arbitrary nested array named element. | |
585 | $mform->addElement('text', 'nested[0][bob][123]', 'nested[0][bob][123]'); | |
586 | $mform->setType('nested[0][bob][123]', PARAM_INT); | |
587 | ||
588 | // Add inheritance test cases. | |
589 | $mform->setType('nested', PARAM_INT); | |
590 | $mform->setType('nested[0]', PARAM_RAW); | |
591 | $mform->setType('nested[0][bob]', PARAM_FLOAT); | |
592 | $mform->addElement('text', 'nested[1]', 'nested[1]'); | |
593 | $mform->addElement('text', 'nested[0][xyz]', 'nested[0][xyz]'); | |
594 | $mform->addElement('text', 'nested[0][bob][foo]', 'nested[0][bob][foo]'); | |
595 | } | |
596 | } |