MDL-38885 form: More unit testing for missing type detection
[moodle.git] / lib / tests / formslib_test.php
CommitLineData
a3d5830a
PS
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for /lib/formslib.php.
19 *
20 * @package core_form
21 * @category phpunit
22 * @copyright 2011 Sam Hemelryk
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/formslib.php');
30require_once($CFG->libdir . '/form/radio.php');
31require_once($CFG->libdir . '/form/select.php');
32require_once($CFG->libdir . '/form/text.php');
33
34
b7d20148 35class 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('&nbsp;'));
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('&nbsp;'));
115 $this->assertFalse($rule->validate(''));
116 $this->assertFalse($rule->validate(false));
117 $this->assertFalse($rule->validate(null));
118
119 if (isset($strictformsrequired)) {
120 $CFG->strictformsrequired = $strictformsrequired;
121 }
122 }
123
124 public function test_generate_id_select() {
125 $el = new MoodleQuickForm_select('choose_one', 'Choose one',
126 array(1 => 'One', '2' => 'Two'));
127 $el->_generateId();
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 }
a3d5830a
PS
271}
272
273
274/**
275 * Test form to be used by {@link formslib_test::test_rendering()}.
276 */
277class formslib_test_form extends moodleform {
278 public function definition() {
279 $this->_form->addElement('select', 'choose_one', 'Choose one',
280 array(1 => 'One', '2' => 'Two'));
281
282 $repeatels = array(
283 $this->_form->createElement('text', 'text', 'Type something')
284 );
f35f07e9
EL
285 // TODO: The repeat_elements() is far from perfect. Everything should be
286 // repeated auto-magically by default with options only defining exceptions.
287 // Surely this is caused because we are storing some element information OUT
288 // from the element (type...) at form level. Anyway, the method should do its
289 // work better, no matter of that.
290 $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
a3d5830a
PS
291
292 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
293
294 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
295 array('id' => 'customelementid2'));
296
297 $repeatels = array(
298 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
299 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
300 );
301 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
302 }
f35f07e9 303}
b7d20148
DP
304
305// Used to test debugging is called when text added without setType.
306class formslib_settype_debugging_text extends moodleform {
307 public function definition() {
308 $mform = $this->_form;
309
310 $mform->addElement('text', 'texttest', 'test123', 'testing123');
311 }
312}
313
314// Used to test debugging is called when hidden added without setType.
315class formslib_settype_debugging_hidden extends moodleform {
316 public function definition() {
317 $mform = $this->_form;
318
319 $mform->addElement('hidden', 'hiddentest', '1');
320 }
321}
322
9654f68a
FM
323// Used to test debugging is called when hidden added without setType.
324class formslib_settype_debugging_url extends moodleform {
325 public function definition() {
326 $mform = $this->_form;
327
328 $mform->addElement('url', 'urltest', 'urltest');
329 }
330}
331
b7d20148
DP
332// Used to test debugging is called when repeated text added without setType.
333class formslib_settype_debugging_repeat extends moodleform {
334 public function definition() {
335 $mform = $this->_form;
336
337 $repeatels = array(
338 $mform->createElement('text', 'repeattest', 'Type something')
339 );
340
341 $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
342 }
343}
344
345// Used to no debugging is called when correctly tset
346class formslib_settype_debugging_repeat_ok extends moodleform {
347 public function definition() {
348 $mform = $this->_form;
349
350 $repeatels = array(
351 $mform->createElement('text', 'repeattest', 'Type something')
352 );
353
354 $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
355 }
356}
9654f68a
FM
357
358// Used to test if debugging is called when a group contains elements without type.
359class formslib_settype_debugging_group extends moodleform {
360 public function definition() {
361 $mform = $this->_form;
362 $group = array(
363 $mform->createElement('text', 'groupel1', 'groupel1'),
364 $mform->createElement('text', 'groupel2', 'groupel2')
365 );
366 $mform->addGroup($group);
367 $mform->setType('groupel2', PARAM_INT);
368 }
369}
370
371// Used to test if debugging is called when a named group contains elements without type.
372class formslib_settype_debugging_namedgroup extends moodleform {
373 public function definition() {
374 $mform = $this->_form;
375 $group = array(
376 $mform->createElement('text', 'groupel1', 'groupel1'),
377 $mform->createElement('text', 'groupel2', 'groupel2')
378 );
379 $mform->addGroup($group, 'namedgroup');
380 $mform->setType('namedgroup[groupel2]', PARAM_INT);
381 }
382}
383
384// Used to test if debugging is called when has a funky name.
385class formslib_settype_debugging_funky_name extends moodleform {
386 public function definition() {
387 $mform = $this->_form;
388 $mform->addElement('text', 'blah[foo][bar][0]', 'test', 'test');
389 $mform->addElement('text', 'blah[foo][bar][1]', 'test', 'test');
390 $mform->setType('blah[foo][bar][0]', PARAM_INT);
391 }
392}
393
394// Used to test if debugging is not called with type inheritance.
395class formslib_settype_debugging_type_inheritance extends moodleform {
396 public function definition() {
397 $mform = $this->_form;
398 $mform->addElement('text', 'blah[foo][bar][0]', 'test1', 'test');
399 $mform->addElement('text', 'blah[bar][foo][1]', 'test2', 'test');
400 $mform->addElement('text', 'blah[any][other][2]', 'test3', 'test');
401 $mform->setType('blah[foo][bar]', PARAM_INT);
402 $mform->setType('blah[bar]', PARAM_FLOAT);
403 $mform->setType('blah', PARAM_TEXT);
404 }
405}