MDL-38885 form: Advanced type checking on special elements
[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
212 public function test_settype_debugging_repeat() {
213 $mform = new formslib_settype_debugging_repeat();
214 $this->assertDebuggingCalled("Did you remember to call setType() for 'repeattest'? Defaulting to PARAM_RAW cleaning.");
215
216 // Check form still there though
217 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
218 $mform->display();
219 }
220
221 public function test_settype_debugging_repeat_ok() {
222 $mform = new formslib_settype_debugging_repeat_ok();
223 // No debugging expected here.
224
225 $this->expectOutputRegex('/<input[^>]*name="repeattest[^>]*type="text/');
226 $mform->display();
227 }
a3d5830a
PS
228}
229
230
231/**
232 * Test form to be used by {@link formslib_test::test_rendering()}.
233 */
234class formslib_test_form extends moodleform {
235 public function definition() {
236 $this->_form->addElement('select', 'choose_one', 'Choose one',
237 array(1 => 'One', '2' => 'Two'));
238
239 $repeatels = array(
240 $this->_form->createElement('text', 'text', 'Type something')
241 );
f35f07e9
EL
242 // TODO: The repeat_elements() is far from perfect. Everything should be
243 // repeated auto-magically by default with options only defining exceptions.
244 // Surely this is caused because we are storing some element information OUT
245 // from the element (type...) at form level. Anyway, the method should do its
246 // work better, no matter of that.
247 $this->repeat_elements($repeatels, 2, array('text' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
a3d5830a
PS
248
249 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
250
251 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
252 array('id' => 'customelementid2'));
253
254 $repeatels = array(
255 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
256 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
257 );
258 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
259 }
f35f07e9 260}
b7d20148
DP
261
262// Used to test debugging is called when text added without setType.
263class formslib_settype_debugging_text extends moodleform {
264 public function definition() {
265 $mform = $this->_form;
266
267 $mform->addElement('text', 'texttest', 'test123', 'testing123');
268 }
269}
270
271// Used to test debugging is called when hidden added without setType.
272class formslib_settype_debugging_hidden extends moodleform {
273 public function definition() {
274 $mform = $this->_form;
275
276 $mform->addElement('hidden', 'hiddentest', '1');
277 }
278}
279
280// Used to test debugging is called when repeated text added without setType.
281class formslib_settype_debugging_repeat extends moodleform {
282 public function definition() {
283 $mform = $this->_form;
284
285 $repeatels = array(
286 $mform->createElement('text', 'repeattest', 'Type something')
287 );
288
289 $this->repeat_elements($repeatels, 1, array(), 'numtexts', 'addtexts');
290 }
291}
292
293// Used to no debugging is called when correctly tset
294class formslib_settype_debugging_repeat_ok extends moodleform {
295 public function definition() {
296 $mform = $this->_form;
297
298 $repeatels = array(
299 $mform->createElement('text', 'repeattest', 'Type something')
300 );
301
302 $this->repeat_elements($repeatels, 2, array('repeattest' => array('type' => PARAM_RAW)), 'numtexts', 'addtexts');
303 }
304}