MDL-32149 PHPUnit test support - part 2
[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
35class formslib_testcase extends basic_testcase {
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 }
193}
194
195
196/**
197 * Test form to be used by {@link formslib_test::test_rendering()}.
198 */
199class formslib_test_form extends moodleform {
200 public function definition() {
201 $this->_form->addElement('select', 'choose_one', 'Choose one',
202 array(1 => 'One', '2' => 'Two'));
203
204 $repeatels = array(
205 $this->_form->createElement('text', 'text', 'Type something')
206 );
207 $this->repeat_elements($repeatels, 2, array(), 'numtexts', 'addtexts');
208
209 $this->_form->addElement('radio', 'radio', 'Label', 'Choice label', 'choice_value');
210
211 $this->_form->addElement('radio', 'radio2', 'Label', 'Choice label', 'choice_value',
212 array('id' => 'customelementid2'));
213
214 $repeatels = array(
215 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'One', 1),
216 $this->_form->createElement('radio', 'repeatradio', 'Choose {no}', 'Two', 2),
217 );
218 $this->repeat_elements($repeatels, 3, array(), 'numradios', 'addradios');
219 }
220}