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