blob: b5fe99b9672368af76f2639cf493da59d0aa767a [file] [log] [blame]
tiyowana83f0632012-03-16 23:52:43 +04001<?php
2
Andrey Andreev99b782d2012-06-09 22:24:46 +03003class Form_helper_test extends CI_TestCase
tiyowana83f0632012-03-16 23:52:43 +04004{
dchill427ecc5cd2012-10-12 16:25:51 -04005 public function set_up()
6 {
7 $this->helper('form');
8 }
9
Andrey Andreev74ffd172012-10-27 00:41:03 +030010 // ------------------------------------------------------------------------
11
tiyowana83f0632012-03-16 23:52:43 +040012 public function test_form_hidden()
Andrey Andreev99b782d2012-06-09 22:24:46 +030013 {
tiyowana83f0632012-03-16 23:52:43 +040014 $expected = <<<EOH
15
16<input type="hidden" name="username" value="johndoe" />
17
18EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +030019
tiyowana83f0632012-03-16 23:52:43 +040020 $this->assertEquals($expected, form_hidden('username', 'johndoe'));
21 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030022
Andrey Andreev74ffd172012-10-27 00:41:03 +030023 // ------------------------------------------------------------------------
24
tiyowana83f0632012-03-16 23:52:43 +040025 public function test_form_input()
26 {
27 $expected = <<<EOH
28<input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%" />
29
30EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +030031
tiyowana83f0632012-03-16 23:52:43 +040032 $data = array(
33 'name' => 'username',
34 'id' => 'username',
35 'value' => 'johndoe',
36 'maxlength' => '100',
37 'size' => '50',
38 'style' => 'width:50%',
39 );
40
41 $this->assertEquals($expected, form_input($data));
42 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030043
Andrey Andreev74ffd172012-10-27 00:41:03 +030044 // ------------------------------------------------------------------------
45
tiyowana83f0632012-03-16 23:52:43 +040046 public function test_form_password()
Andrey Andreev99b782d2012-06-09 22:24:46 +030047 {
tiyowana83f0632012-03-16 23:52:43 +040048 $expected = <<<EOH
49<input type="password" name="password" value="" />
50
51EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +030052
tiyowana83f0632012-03-16 23:52:43 +040053 $this->assertEquals($expected, form_password('password'));
54 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030055
Andrey Andreev74ffd172012-10-27 00:41:03 +030056 // ------------------------------------------------------------------------
57
tiyowana83f0632012-03-16 23:52:43 +040058 public function test_form_upload()
Andrey Andreev99b782d2012-06-09 22:24:46 +030059 {
tiyowana83f0632012-03-16 23:52:43 +040060 $expected = <<<EOH
Andrey Andreeve1d5a7a2013-02-19 13:38:43 +020061<input type="file" name="attachment" />
tiyowana83f0632012-03-16 23:52:43 +040062
63EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +030064
tiyowana83f0632012-03-16 23:52:43 +040065 $this->assertEquals($expected, form_upload('attachment'));
66 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030067
Andrey Andreev74ffd172012-10-27 00:41:03 +030068 // ------------------------------------------------------------------------
69
tiyowana83f0632012-03-16 23:52:43 +040070 public function test_form_textarea()
Andrey Andreev99b782d2012-06-09 22:24:46 +030071 {
tiyowana83f0632012-03-16 23:52:43 +040072 $expected = <<<EOH
73<textarea name="notes" cols="40" rows="10" >Notes</textarea>
74
75EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +030076
tiyowana83f0632012-03-16 23:52:43 +040077 $this->assertEquals($expected, form_textarea('notes', 'Notes'));
78 }
Andrey Andreev99b782d2012-06-09 22:24:46 +030079
Andrey Andreev74ffd172012-10-27 00:41:03 +030080 // ------------------------------------------------------------------------
81
tiyowana83f0632012-03-16 23:52:43 +040082 public function test_form_dropdown()
83 {
84 $expected = <<<EOH
85<select name="shirts">
86<option value="small">Small Shirt</option>
87<option value="med">Medium Shirt</option>
88<option value="large" selected="selected">Large Shirt</option>
89<option value="xlarge">Extra Large Shirt</option>
90</select>
91
92EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +030093
tiyowana83f0632012-03-16 23:52:43 +040094 $options = array(
Andrey Andreev99b782d2012-06-09 22:24:46 +030095 'small' => 'Small Shirt',
96 'med' => 'Medium Shirt',
97 'large' => 'Large Shirt',
98 'xlarge' => 'Extra Large Shirt',
tiyowana83f0632012-03-16 23:52:43 +040099 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300100
tiyowana83f0632012-03-16 23:52:43 +0400101 $this->assertEquals($expected, form_dropdown('shirts', $options, 'large'));
Andrey Andreev99b782d2012-06-09 22:24:46 +0300102
tiyowana83f0632012-03-16 23:52:43 +0400103 $expected = <<<EOH
104<select name="shirts" multiple="multiple">
105<option value="small" selected="selected">Small Shirt</option>
106<option value="med">Medium Shirt</option>
107<option value="large" selected="selected">Large Shirt</option>
108<option value="xlarge">Extra Large Shirt</option>
109</select>
110
111EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +0300112
tiyowana83f0632012-03-16 23:52:43 +0400113 $shirts_on_sale = array('small', 'large');
Andrey Andreev99b782d2012-06-09 22:24:46 +0300114
tiyowana83f0632012-03-16 23:52:43 +0400115 $this->assertEquals($expected, form_dropdown('shirts', $options, $shirts_on_sale));
Andrey Andreev99b782d2012-06-09 22:24:46 +0300116
tiyowana83f0632012-03-16 23:52:43 +0400117 $options = array(
118 'Swedish Cars' => array(
Andrey Andreev99b782d2012-06-09 22:24:46 +0300119 'volvo' => 'Volvo',
120 'saab' => 'Saab'
tiyowana83f0632012-03-16 23:52:43 +0400121 ),
122 'German Cars' => array(
Andrey Andreev99b782d2012-06-09 22:24:46 +0300123 'mercedes' => 'Mercedes',
124 'audi' => 'Audi'
tiyowana83f0632012-03-16 23:52:43 +0400125 )
126 );
Andrey Andreev99b782d2012-06-09 22:24:46 +0300127
tiyowana83f0632012-03-16 23:52:43 +0400128 $expected = <<<EOH
129<select name="cars" multiple="multiple">
130<optgroup label="Swedish Cars">
131<option value="volvo" selected="selected">Volvo</option>
132<option value="saab">Saab</option>
133</optgroup>
134<optgroup label="German Cars">
135<option value="mercedes">Mercedes</option>
136<option value="audi" selected="selected">Audi</option>
137</optgroup>
138</select>
139
140EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +0300141
142 $this->assertEquals($expected, form_dropdown('cars', $options, array('volvo', 'audi')));
tiyowana83f0632012-03-16 23:52:43 +0400143 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300144
Andrey Andreev74ffd172012-10-27 00:41:03 +0300145 // ------------------------------------------------------------------------
146
tiyowana83f0632012-03-16 23:52:43 +0400147 public function test_form_multiselect()
148 {
149 $expected = <<<EOH
150<select name="shirts[]" multiple="multiple">
151<option value="small">Small Shirt</option>
152<option value="med" selected="selected">Medium Shirt</option>
153<option value="large" selected="selected">Large Shirt</option>
154<option value="xlarge">Extra Large Shirt</option>
155</select>
156
157EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +0300158
tiyowana83f0632012-03-16 23:52:43 +0400159 $options = array(
Andrey Andreev99b782d2012-06-09 22:24:46 +0300160 'small' => 'Small Shirt',
161 'med' => 'Medium Shirt',
162 'large' => 'Large Shirt',
163 'xlarge' => 'Extra Large Shirt',
164 );
165
tiyowana83f0632012-03-16 23:52:43 +0400166 $this->assertEquals($expected, form_multiselect('shirts[]', $options, array('med', 'large')));
167 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300168
Andrey Andreev74ffd172012-10-27 00:41:03 +0300169 // ------------------------------------------------------------------------
170
tiyowana83f0632012-03-16 23:52:43 +0400171 public function test_form_fieldset()
172 {
173 $expected = <<<EOH
174<fieldset>
175<legend>Address Information</legend>
176
177EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +0300178
tiyowana83f0632012-03-16 23:52:43 +0400179 $this->assertEquals($expected, form_fieldset('Address Information'));
180 }
181
Andrey Andreev74ffd172012-10-27 00:41:03 +0300182 // ------------------------------------------------------------------------
183
tiyowana83f0632012-03-16 23:52:43 +0400184 public function test_form_fieldset_close()
185 {
186 $expected = <<<EOH
187</fieldset></div></div>
188EOH;
Andrey Andreev99b782d2012-06-09 22:24:46 +0300189
tiyowana83f0632012-03-16 23:52:43 +0400190 $this->assertEquals($expected, form_fieldset_close('</div></div>'));
191 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300192
Andrey Andreev74ffd172012-10-27 00:41:03 +0300193 // ------------------------------------------------------------------------
194
tiyowana83f0632012-03-16 23:52:43 +0400195 public function test_form_checkbox()
196 {
197 $expected = <<<EOH
198<input type="checkbox" name="newsletter" value="accept" checked="checked" />
199
200EOH;
201
202 $this->assertEquals($expected, form_checkbox('newsletter', 'accept', TRUE));
203 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300204
Andrey Andreev74ffd172012-10-27 00:41:03 +0300205 // ------------------------------------------------------------------------
206
tiyowana83f0632012-03-16 23:52:43 +0400207 public function test_form_radio()
208 {
209 $expected = <<<EOH
210<input type="radio" name="newsletter" value="accept" checked="checked" />
211
212EOH;
213
214 $this->assertEquals($expected, form_radio('newsletter', 'accept', TRUE));
215 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300216
Andrey Andreev74ffd172012-10-27 00:41:03 +0300217 // ------------------------------------------------------------------------
218
tiyowana83f0632012-03-16 23:52:43 +0400219 public function test_form_submit()
220 {
221 $expected = <<<EOH
222<input type="submit" name="mysubmit" value="Submit Post!" />
223
224EOH;
225
226 $this->assertEquals($expected, form_submit('mysubmit', 'Submit Post!'));
227 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300228
Andrey Andreev74ffd172012-10-27 00:41:03 +0300229 // ------------------------------------------------------------------------
230
tiyowana83f0632012-03-16 23:52:43 +0400231 public function test_form_label()
232 {
233 $expected = <<<EOH
234<label for="username">What is your Name</label>
235EOH;
236
237 $this->assertEquals($expected, form_label('What is your Name', 'username'));
238 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300239
Andrey Andreev74ffd172012-10-27 00:41:03 +0300240 // ------------------------------------------------------------------------
241
tiyowana83f0632012-03-16 23:52:43 +0400242 public function test_form_reset()
243 {
244 $expected = <<<EOH
245<input type="reset" name="myreset" value="Reset" />
246
247EOH;
248
249 $this->assertEquals($expected, form_reset('myreset', 'Reset'));
250 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300251
Andrey Andreev74ffd172012-10-27 00:41:03 +0300252 // ------------------------------------------------------------------------
253
tiyowana83f0632012-03-16 23:52:43 +0400254 public function test_form_button()
255 {
256 $expected = <<<EOH
257<button name="name" type="button" >content</button>
258
259EOH;
260
Andrey Andreev99b782d2012-06-09 22:24:46 +0300261 $this->assertEquals($expected, form_button('name', 'content'));
tiyowana83f0632012-03-16 23:52:43 +0400262 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300263
Andrey Andreev74ffd172012-10-27 00:41:03 +0300264 // ------------------------------------------------------------------------
265
tiyowana83f0632012-03-16 23:52:43 +0400266 public function test_form_close()
267 {
268 $expected = <<<EOH
269</form></div></div>
270EOH;
271
272 $this->assertEquals($expected, form_close('</div></div>'));
273 }
Andrey Andreev99b782d2012-06-09 22:24:46 +0300274
Andrey Andreev7c4d1062012-11-01 15:14:34 +0200275 // ------------------------------------------------------------------------
276
277 public function test_form_prep()
278 {
279 $this->assertEquals(
280 'Here is a string containing &quot;quoted&quot; text.',
281 form_prep('Here is a string containing "quoted" text.')
282 );
283
284 $this->assertEquals(
285 'Here is a string containing a &lt;tag&gt;.',
286 form_prep('Here is a string containing a <tag>.', TRUE)
287 );
288 }
289
tiyowana83f0632012-03-16 23:52:43 +0400290}