1 require 'abstract_unit'
3 class FormTagHelperTest
< ActionView
::TestCase
4 tests ActionView
::Helpers::FormTagHelper
7 @controller = Class
.new
do
9 "http://www.example.com"
12 @controller = @controller.new
15 VALID_HTML_ID
= /^[A-Za-z][-_:.A-Za-z0-9]*$/ # see http://www.w3.org/TR/html4/types.html#type-name
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
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']
30 expected = %(<form action="http://www.example.com" method="post">)
31 assert_dom_equal expected, actual
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
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
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
52 def test_form_tag_with_block_in_erb
53 __in_erb_template = ''
54 form_tag("http://example.com") { concat "Hello world!" }
56 expected = %(<form action="http://example.com" method="post">Hello world!</form>)
57 assert_dom_equal expected, output_buffer
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!" }
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
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
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']
79 def test_file_field_tag
80 assert_dom_equal "<input name=\"picsplz\" type=\"file\" id=\"picsplz\" />", file_field_tag("picsplz")
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")
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
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
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
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
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
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
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
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
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
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']
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
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
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
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
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
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
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
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
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
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
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
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']
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
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
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
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
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']
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)
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
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!')")
260 def test_submit_tag_with_no_onclick_options
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...")
267 def test_submit_tag_with_confirmation
269 %(<input name='commit' type='submit' value='Save' onclick="return confirm('Are you sure?');"/>),
270 submit_tag("Save", :confirm => "Are you sure?")
274 def test_image_submit_tag_with_confirmation
276 %(<input type="image" src="/images/save.gif" onclick="return confirm('Are you sure?');"/>),
277 image_submit_tag("save.gif", :confirm => "Are you sure?")
285 def test_field_set_tag_in_erb
286 __in_erb_template = ''
287 field_set_tag("Your details") { concat "Hello world!" }
289 expected = %(<fieldset><legend>Your details</legend>Hello world!</fieldset>)
290 assert_dom_equal expected, output_buffer
292 self.output_buffer = ''
293 field_set_tag { concat "Hello world!" }
295 expected = %(<fieldset>Hello world!</fieldset>)
296 assert_dom_equal expected, output_buffer
298 self.output_buffer = ''
299 field_set_tag('') { concat "Hello world!" }
301 expected = %(<fieldset>Hello world!</fieldset>)
302 assert_dom_equal expected, output_buffer
304 self.output_buffer = ''
305 field_set_tag('', :class => 'format') { concat "Hello world!" }
307 expected = %(<fieldset class="format">Hello world!</fieldset>)
308 assert_dom_equal expected, output_buffer
311 def protect_against_forgery?
317 def root_elem(rendered_content)
318 HTML::Document.new(rendered_content).root.children[0]