summaryrefslogtreecommitdiffstats
path: root/tests/codeigniter/helpers/form_helper_test.php
blob: b5fe99b9672368af76f2639cf493da59d0aa767a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
<?php

class Form_helper_test extends CI_TestCase
{
	public function set_up()
	{
		$this->helper('form');
	}

	// ------------------------------------------------------------------------

	public function test_form_hidden()
	{
		$expected = <<<EOH

<input type="hidden" name="username" value="johndoe" />

EOH;

		$this->assertEquals($expected, form_hidden('username', 'johndoe'));
	}

	// ------------------------------------------------------------------------

	public function test_form_input()
	{
		$expected = <<<EOH
<input type="text" name="username" value="johndoe" id="username" maxlength="100" size="50" style="width:50%"  />

EOH;

		$data = array(
			'name'        => 'username',
			'id'          => 'username',
			'value'       => 'johndoe',
			'maxlength'   => '100',
			'size'        => '50',
			'style'       => 'width:50%',
		);

		$this->assertEquals($expected, form_input($data));
	}

	// ------------------------------------------------------------------------

	public function test_form_password()
	{
		$expected = <<<EOH
<input type="password" name="password" value=""  />

EOH;

		$this->assertEquals($expected, form_password('password'));
	}

	// ------------------------------------------------------------------------

	public function test_form_upload()
	{
		$expected = <<<EOH
<input type="file" name="attachment"  />

EOH;

		$this->assertEquals($expected, form_upload('attachment'));
	}

	// ------------------------------------------------------------------------

	public function test_form_textarea()
	{
		$expected = <<<EOH
<textarea name="notes" cols="40" rows="10" >Notes</textarea>

EOH;

		$this->assertEquals($expected, form_textarea('notes', 'Notes'));
	}

	// ------------------------------------------------------------------------

	public function test_form_dropdown()
	{
		$expected = <<<EOH
<select name="shirts">
<option value="small">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

EOH;

		$options = array(
			'small'		=> 'Small Shirt',
			'med'		=> 'Medium Shirt',
			'large'		=> 'Large Shirt',
			'xlarge'	=> 'Extra Large Shirt',
		);

		$this->assertEquals($expected, form_dropdown('shirts', $options, 'large'));

		$expected = <<<EOH
<select name="shirts" multiple="multiple">
<option value="small" selected="selected">Small Shirt</option>
<option value="med">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

EOH;

		$shirts_on_sale = array('small', 'large');

		$this->assertEquals($expected, form_dropdown('shirts', $options, $shirts_on_sale));

		$options = array(
			'Swedish Cars' => array(
				'volvo'	=> 'Volvo',
				'saab'	=> 'Saab'
			),
			'German Cars' => array(
				'mercedes'	=> 'Mercedes',
				'audi'		=> 'Audi'
			)
		);

		$expected = <<<EOH
<select name="cars" multiple="multiple">
<optgroup label="Swedish Cars">
<option value="volvo" selected="selected">Volvo</option>
<option value="saab">Saab</option>
</optgroup>
<optgroup label="German Cars">
<option value="mercedes">Mercedes</option>
<option value="audi" selected="selected">Audi</option>
</optgroup>
</select>

EOH;

		$this->assertEquals($expected, form_dropdown('cars', $options, array('volvo', 'audi')));
	}

	// ------------------------------------------------------------------------

	public function test_form_multiselect()
	{
		$expected = <<<EOH
<select name="shirts[]"  multiple="multiple">
<option value="small">Small Shirt</option>
<option value="med" selected="selected">Medium Shirt</option>
<option value="large" selected="selected">Large Shirt</option>
<option value="xlarge">Extra Large Shirt</option>
</select>

EOH;

		$options = array(
			'small'		=> 'Small Shirt',
			'med'		=> 'Medium Shirt',
			'large'		=> 'Large Shirt',
			'xlarge'	=> 'Extra Large Shirt',
		);

		$this->assertEquals($expected, form_multiselect('shirts[]', $options, array('med', 'large')));
	}

	// ------------------------------------------------------------------------

	public function test_form_fieldset()
	{
		$expected = <<<EOH
<fieldset>
<legend>Address Information</legend>

EOH;

		$this->assertEquals($expected, form_fieldset('Address Information'));
	}

	// ------------------------------------------------------------------------

	public function test_form_fieldset_close()
	{
		$expected = <<<EOH
</fieldset></div></div>
EOH;

		$this->assertEquals($expected, form_fieldset_close('</div></div>'));
	}

	// ------------------------------------------------------------------------

	public function test_form_checkbox()
	{
		$expected = <<<EOH
<input type="checkbox" name="newsletter" value="accept" checked="checked"  />

EOH;

		$this->assertEquals($expected, form_checkbox('newsletter', 'accept', TRUE));
	}

	// ------------------------------------------------------------------------

	public function test_form_radio()
	{
		$expected = <<<EOH
<input type="radio" name="newsletter" value="accept" checked="checked"  />

EOH;

		$this->assertEquals($expected, form_radio('newsletter', 'accept', TRUE));
	}

	// ------------------------------------------------------------------------

	public function test_form_submit()
	{
		$expected = <<<EOH
<input type="submit" name="mysubmit" value="Submit Post!"  />

EOH;

		$this->assertEquals($expected, form_submit('mysubmit', 'Submit Post!'));
	}

	// ------------------------------------------------------------------------

	public function test_form_label()
	{
		$expected = <<<EOH
<label for="username">What is your Name</label>
EOH;

		$this->assertEquals($expected, form_label('What is your Name', 'username'));
	}

	// ------------------------------------------------------------------------

	public function test_form_reset()
	{
		$expected = <<<EOH
<input type="reset" name="myreset" value="Reset"  />

EOH;

		$this->assertEquals($expected, form_reset('myreset', 'Reset'));
	}

	// ------------------------------------------------------------------------

	public function test_form_button()
	{
		$expected = <<<EOH
<button name="name" type="button" >content</button>

EOH;

		$this->assertEquals($expected, form_button('name', 'content'));
	}

	// ------------------------------------------------------------------------

	public function test_form_close()
	{
		$expected = <<<EOH
</form></div></div>
EOH;

		$this->assertEquals($expected, form_close('</div></div>'));
	}

	// ------------------------------------------------------------------------

	public function test_form_prep()
	{
		$this->assertEquals(
			'Here is a string containing &quot;quoted&quot; text.',
			form_prep('Here is a string containing "quoted" text.')
		);

		$this->assertEquals(
			'Here is a string containing a &lt;tag&gt;.',
			form_prep('Here is a string containing a <tag>.', TRUE)
		);
	}

}