Froze rails gems
[depot.git] / vendor / rails / actionpack / test / template / form_tag_helper_test.rb
1 require 'abstract_unit'
2
3 class FormTagHelperTest < ActionView::TestCase
4 tests ActionView::Helpers::FormTagHelper
5
6 def setup
7 @controller = Class.new do
8 def url_for(options)
9 "http://www.example.com"
10 end
11 end
12 @controller = @controller.new
13 end
14
15 VALID_HTML_ID = /^[A-Za-z][-_:.A-Za-z0-9]*$/ # see http://www.w3.org/TR/html4/types.html#type-name
16
17 def test_check_box_tag
18 actual = check_box_tag "admin"
19 expected = %(<input id="admin" name="admin" type="checkbox" value="1" />)
20 assert_dom_equal expected, actual
21 end
22
23 def test_check_box_tag_id_sanitized
24 label_elem = root_elem(check_box_tag("project[2][admin]"))
25 assert_match VALID_HTML_ID, label_elem['id']
26 end
27
28 def test_form_tag
29 actual = form_tag
30 expected = %(<form action="http://www.example.com" method="post">)
31 assert_dom_equal expected, actual
32 end
33
34 def test_form_tag_multipart
35 actual = form_tag({}, { 'multipart' => true })
36 expected = %(<form action="http://www.example.com" enctype="multipart/form-data" method="post">)
37 assert_dom_equal expected, actual
38 end
39
40 def test_form_tag_with_method_put
41 actual = form_tag({}, { :method => :put })
42 expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>)
43 assert_dom_equal expected, actual
44 end
45
46 def test_form_tag_with_method_delete
47 actual = form_tag({}, { :method => :delete })
48 expected = %(<form action="http://www.example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="delete" /></div>)
49 assert_dom_equal expected, actual
50 end
51
52 def test_form_tag_with_block_in_erb
53 __in_erb_template = ''
54 form_tag("http://example.com") { concat "Hello world!" }
55
56 expected = %(<form action="http://example.com" method="post">Hello world!</form>)
57 assert_dom_equal expected, output_buffer
58 end
59
60 def test_form_tag_with_block_and_method_in_erb
61 __in_erb_template = ''
62 form_tag("http://example.com", :method => :put) { concat "Hello world!" }
63
64 expected = %(<form action="http://example.com" method="post"><div style='margin:0;padding:0'><input type="hidden" name="_method" value="put" /></div>Hello world!</form>)
65 assert_dom_equal expected, output_buffer
66 end
67
68 def test_hidden_field_tag
69 actual = hidden_field_tag "id", 3
70 expected = %(<input id="id" name="id" type="hidden" value="3" />)
71 assert_dom_equal expected, actual
72 end
73
74 def test_hidden_field_tag_id_sanitized
75 input_elem = root_elem(hidden_field_tag("item[][title]"))
76 assert_match VALID_HTML_ID, input_elem['id']
77 end
78
79 def test_file_field_tag
80 assert_dom_equal "<input name=\"picsplz\" type=\"file\" id=\"picsplz\" />", file_field_tag("picsplz")
81 end
82
83 def test_file_field_tag_with_options
84 assert_dom_equal "<input name=\"picsplz\" type=\"file\" id=\"picsplz\" class=\"pix\"/>", file_field_tag("picsplz", :class => "pix")
85 end
86
87 def test_password_field_tag
88 actual = password_field_tag
89 expected = %(<input id="password" name="password" type="password" />)
90 assert_dom_equal expected, actual
91 end
92
93 def test_radio_button_tag
94 actual = radio_button_tag "people", "david"
95 expected = %(<input id="people_david" name="people" type="radio" value="david" />)
96 assert_dom_equal expected, actual
97
98 actual = radio_button_tag("num_people", 5)
99 expected = %(<input id="num_people_5" name="num_people" type="radio" value="5" />)
100 assert_dom_equal expected, actual
101
102 actual = radio_button_tag("gender", "m") + radio_button_tag("gender", "f")
103 expected = %(<input id="gender_m" name="gender" type="radio" value="m" /><input id="gender_f" name="gender" type="radio" value="f" />)
104 assert_dom_equal expected, actual
105
106 actual = radio_button_tag("opinion", "-1") + radio_button_tag("opinion", "1")
107 expected = %(<input id="opinion_-1" name="opinion" type="radio" value="-1" /><input id="opinion_1" name="opinion" type="radio" value="1" />)
108 assert_dom_equal expected, actual
109
110 actual = radio_button_tag("person[gender]", "m")
111 expected = %(<input id="person_gender_m" name="person[gender]" type="radio" value="m" />)
112 assert_dom_equal expected, actual
113 end
114
115 def test_select_tag
116 actual = select_tag "people", "<option>david</option>"
117 expected = %(<select id="people" name="people"><option>david</option></select>)
118 assert_dom_equal expected, actual
119 end
120
121 def test_select_tag_with_multiple
122 actual = select_tag "colors", "<option>Red</option><option>Blue</option><option>Green</option>", :multiple => :true
123 expected = %(<select id="colors" multiple="multiple" name="colors"><option>Red</option><option>Blue</option><option>Green</option></select>)
124 assert_dom_equal expected, actual
125 end
126
127 def test_select_tag_disabled
128 actual = select_tag "places", "<option>Home</option><option>Work</option><option>Pub</option>", :disabled => :true
129 expected = %(<select id="places" disabled="disabled" name="places"><option>Home</option><option>Work</option><option>Pub</option></select>)
130 assert_dom_equal expected, actual
131 end
132
133 def test_select_tag_id_sanitized
134 input_elem = root_elem(select_tag("project[1]people", "<option>david</option>"))
135 assert_match VALID_HTML_ID, input_elem['id']
136 end
137
138 def test_text_area_tag_size_string
139 actual = text_area_tag "body", "hello world", "size" => "20x40"
140 expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
141 assert_dom_equal expected, actual
142 end
143
144 def test_text_area_tag_size_symbol
145 actual = text_area_tag "body", "hello world", :size => "20x40"
146 expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>)
147 assert_dom_equal expected, actual
148 end
149
150 def test_text_area_tag_should_disregard_size_if_its_given_as_an_integer
151 actual = text_area_tag "body", "hello world", :size => 20
152 expected = %(<textarea id="body" name="body">hello world</textarea>)
153 assert_dom_equal expected, actual
154 end
155
156 def test_text_field_tag
157 actual = text_field_tag "title", "Hello!"
158 expected = %(<input id="title" name="title" type="text" value="Hello!" />)
159 assert_dom_equal expected, actual
160 end
161
162 def test_text_field_tag_class_string
163 actual = text_field_tag "title", "Hello!", "class" => "admin"
164 expected = %(<input class="admin" id="title" name="title" type="text" value="Hello!" />)
165 assert_dom_equal expected, actual
166 end
167
168 def test_text_field_tag_size_symbol
169 actual = text_field_tag "title", "Hello!", :size => 75
170 expected = %(<input id="title" name="title" size="75" type="text" value="Hello!" />)
171 assert_dom_equal expected, actual
172 end
173
174 def test_text_field_tag_size_string
175 actual = text_field_tag "title", "Hello!", "size" => "75"
176 expected = %(<input id="title" name="title" size="75" type="text" value="Hello!" />)
177 assert_dom_equal expected, actual
178 end
179
180 def test_text_field_tag_maxlength_symbol
181 actual = text_field_tag "title", "Hello!", :maxlength => 75
182 expected = %(<input id="title" name="title" maxlength="75" type="text" value="Hello!" />)
183 assert_dom_equal expected, actual
184 end
185
186 def test_text_field_tag_maxlength_string
187 actual = text_field_tag "title", "Hello!", "maxlength" => "75"
188 expected = %(<input id="title" name="title" maxlength="75" type="text" value="Hello!" />)
189 assert_dom_equal expected, actual
190 end
191
192 def test_text_field_disabled
193 actual = text_field_tag "title", "Hello!", :disabled => :true
194 expected = %(<input id="title" name="title" disabled="disabled" type="text" value="Hello!" />)
195 assert_dom_equal expected, actual
196 end
197
198 def test_text_field_tag_with_multiple_options
199 actual = text_field_tag "title", "Hello!", :size => 70, :maxlength => 80
200 expected = %(<input id="title" name="title" size="70" maxlength="80" type="text" value="Hello!" />)
201 assert_dom_equal expected, actual
202 end
203
204 def test_text_field_tag_id_sanitized
205 input_elem = root_elem(text_field_tag("item[][title]"))
206 assert_match VALID_HTML_ID, input_elem['id']
207 end
208
209 def test_label_tag_without_text
210 actual = label_tag "title"
211 expected = %(<label for="title">Title</label>)
212 assert_dom_equal expected, actual
213 end
214
215 def test_label_tag_with_symbol
216 actual = label_tag :title
217 expected = %(<label for="title">Title</label>)
218 assert_dom_equal expected, actual
219 end
220
221 def test_label_tag_with_text
222 actual = label_tag "title", "My Title"
223 expected = %(<label for="title">My Title</label>)
224 assert_dom_equal expected, actual
225 end
226
227 def test_label_tag_class_string
228 actual = label_tag "title", "My Title", "class" => "small_label"
229 expected = %(<label for="title" class="small_label">My Title</label>)
230 assert_dom_equal expected, actual
231 end
232
233 def test_label_tag_id_sanitized
234 label_elem = root_elem(label_tag("item[title]"))
235 assert_match VALID_HTML_ID, label_elem['for']
236 end
237
238 def test_boolean_options
239 assert_dom_equal %(<input checked="checked" disabled="disabled" id="admin" name="admin" readonly="readonly" type="checkbox" value="1" />), check_box_tag("admin", 1, true, 'disabled' => true, :readonly => "yes")
240 assert_dom_equal %(<input checked="checked" id="admin" name="admin" type="checkbox" value="1" />), check_box_tag("admin", 1, true, :disabled => false, :readonly => nil)
241 assert_dom_equal %(<input type="checkbox" />), tag(:input, :type => "checkbox", :checked => false)
242 assert_dom_equal %(<select id="people" multiple="multiple" name="people[]"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => true)
243 assert_dom_equal %(<select id="people_" multiple="multiple" name="people[]"><option>david</option></select>), select_tag("people[]", "<option>david</option>", :multiple => true)
244 assert_dom_equal %(<select id="people" name="people"><option>david</option></select>), select_tag("people", "<option>david</option>", :multiple => nil)
245 end
246
247 def test_stringify_symbol_keys
248 actual = text_field_tag "title", "Hello!", :id => "admin"
249 expected = %(<input id="admin" name="title" type="text" value="Hello!" />)
250 assert_dom_equal expected, actual
251 end
252
253 def test_submit_tag
254 assert_dom_equal(
255 %(<input name='commit' type='submit' value='Save' onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';alert('hello!');result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" />),
256 submit_tag("Save", :disable_with => "Saving...", :onclick => "alert('hello!')")
257 )
258 end
259
260 def test_submit_tag_with_no_onclick_options
261 assert_dom_equal(
262 %(<input name='commit' type='submit' value='Save' onclick="if (window.hiddenCommit) { window.hiddenCommit.setAttribute('value', this.value); }else { hiddenCommit = this.cloneNode(false);hiddenCommit.setAttribute('type', 'hidden');this.form.appendChild(hiddenCommit); }this.setAttribute('originalValue', this.value);this.disabled = true;this.value='Saving...';result = (this.form.onsubmit ? (this.form.onsubmit() ? this.form.submit() : false) : this.form.submit());if (result == false) { this.value = this.getAttribute('originalValue');this.disabled = false; }return result;" />),
263 submit_tag("Save", :disable_with => "Saving...")
264 )
265 end
266
267 def test_submit_tag_with_confirmation
268 assert_dom_equal(
269 %(<input name='commit' type='submit' value='Save' onclick="return confirm('Are you sure?');"/>),
270 submit_tag("Save", :confirm => "Are you sure?")
271 )
272 end
273
274 def test_image_submit_tag_with_confirmation
275 assert_dom_equal(
276 %(<input type="image" src="/images/save.gif" onclick="return confirm('Are you sure?');"/>),
277 image_submit_tag("save.gif", :confirm => "Are you sure?")
278 )
279 end
280
281 def test_pass
282 assert_equal 1, 1
283 end
284
285 def test_field_set_tag_in_erb
286 __in_erb_template = ''
287 field_set_tag("Your details") { concat "Hello world!" }
288
289 expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
290 assert_dom_equal expected, output_buffer
291
292 self.output_buffer = ''
293 field_set_tag { concat "Hello world!" }
294
295 expected = %(<fieldset>Hello world!</fieldset>)
296 assert_dom_equal expected, output_buffer
297
298 self.output_buffer = ''
299 field_set_tag('') { concat "Hello world!" }
300
301 expected = %(<fieldset>Hello world!</fieldset>)
302 assert_dom_equal expected, output_buffer
303
304 self.output_buffer = ''
305 field_set_tag('', :class => 'format') { concat "Hello world!" }
306
307 expected = %(<fieldset class="format">Hello world!</fieldset>)
308 assert_dom_equal expected, output_buffer
309 end
310
311 def protect_against_forgery?
312 false
313 end
314
315 private
316
317 def root_elem(rendered_content)
318 HTML::Document.new(rendered_content).root.children[0]
319 end
320 end