1 require "#{File.dirname(__FILE__)}/../abstract_unit"
4 Post = Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
6 alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
7 alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
8 alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
10 def new_record=(boolean)
22 def save; @id = 1; @post_id = 1 end
23 def new_record?; @id.nil? end
25 @id.nil? ? 'new comment' : "comment ##{@id}"
30 class Comment::Nested < Comment; end
33 class FormHelperTest < Test::Unit::TestCase
34 include ActionView::Helpers::FormHelper
35 include ActionView::Helpers::FormTagHelper
36 include ActionView::Helpers::UrlHelper
37 include ActionView::Helpers::TagHelper
38 include ActionView::Helpers::TextHelper
39 include ActionView::Helpers::ActiveRecordHelper
40 include ActionView::Helpers::RecordIdentificationHelper
41 include ActionController::PolymorphicRoutes
45 @comment = Comment.new
48 def on(field); "can't be empty" if field == "author_name"; end
49 def empty?() false end
51 def full_messages() [ "Author name can't be empty" ] end
54 def @post.id; 123; end
55 def @post.id_before_type_cast; 123; end
57 @post.title = "Hello World"
58 @post.author_name = ""
59 @post.body = "Back to the hill and over it again!"
61 @post.written_on = Date.new(2004, 6, 15)
63 @controller = Class.new do
64 attr_reader :url_for_options
66 @url_for_options = options
67 "http://www.example.com"
70 @controller = @controller.new
75 '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
78 '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
81 '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
85 def test_text_field_with_escapes
86 @post.title = "<b>Hello World</b>"
88 '<input id="post_title" name="post[title]" size="30" type="text" value="<b>Hello World</b>" />', text_field("post", "title")
92 def test_text_field_with_options
93 expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
94 assert_dom_equal expected, text_field("post", "title", "size" => 35)
95 assert_dom_equal expected, text_field("post", "title", :size => 35)
98 def test_text_field_assuming_size
99 expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
100 assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
101 assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
104 def test_text_field_removing_size
105 expected = '<input id="post_title" maxlength="35" name="post[title]" type="text" value="Hello World" />'
106 assert_dom_equal expected, text_field("post", "title", "maxlength" => 35, "size" => nil)
107 assert_dom_equal expected, text_field("post", "title", :maxlength => 35, :size => nil)
110 def test_text_field_doesnt_change_param_values
111 object_name = 'post[]'
112 expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
113 assert_equal expected, text_field(object_name, "title")
114 assert_equal object_name, "post[]"
117 def test_hidden_field
118 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
119 hidden_field("post", "title")
122 def test_hidden_field_with_escapes
123 @post.title = "<b>Hello World</b>"
124 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="<b>Hello World</b>" />',
125 hidden_field("post", "title")
128 def test_text_field_with_options
129 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Something Else" />',
130 hidden_field("post", "title", :value => "Something Else")
135 '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
136 check_box("post", "secret")
140 '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
141 check_box("post", "secret")
144 '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
145 check_box("post", "secret" ,{"checked"=>"checked"})
149 '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
150 check_box("post", "secret")
154 def test_check_box_with_explicit_checked_and_unchecked_values
157 '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
158 check_box("post", "secret", {}, "on", "off")
162 def test_radio_button
163 assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
164 radio_button("post", "title", "Hello World")
166 assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
167 radio_button("post", "title", "Goodbye World")
171 def test_radio_button_is_checked_with_integers
172 assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
173 radio_button("post", "secret", "1")
177 def test_radio_button_respects_passed_in_id
178 assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
179 radio_button("post", "secret", "1", :id=>"foo")
185 '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
186 text_area("post", "body")
190 def test_text_area_with_escapes
191 @post.body = "Back to <i>the</i> hill and over it again!"
193 '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to <i>the</i> hill and over it again!</textarea>',
194 text_area("post", "body")
198 def test_text_area_with_alternate_value
200 '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
201 text_area("post", "body", :value => 'Testing alternate values.')
205 def test_text_area_with_size_option
207 '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
208 text_area("post", "body", :size => "183x820")
212 def test_explicit_name
214 '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
217 '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
218 text_area("post", "body", "name" => "really!")
221 '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
222 check_box("post", "secret", "name" => "i mean it")
224 assert_dom_equal text_field("post", "title", "name" => "dont guess"),
225 text_field("post", "title", :name => "dont guess")
226 assert_dom_equal text_area("post", "body", "name" => "really!"),
227 text_area("post", "body", :name => "really!")
228 assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
229 check_box("post", "secret", :name => "i mean it")
234 '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
237 '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
238 text_area("post", "body", "id" => "really!")
241 '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
242 check_box("post", "secret", "id" => "i mean it")
244 assert_dom_equal text_field("post", "title", "id" => "dont guess"),
245 text_field("post", "title", :id => "dont guess")
246 assert_dom_equal text_area("post", "body", "id" => "really!"),
247 text_area("post", "body", :id => "really!")
248 assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
249 check_box("post", "secret", :id => "i mean it")
255 "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
258 "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
259 text_area("post[]", "body")
262 "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
263 check_box("post[]", "secret")
266 "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
267 radio_button("post[]", "title", "Hello World")
269 assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
270 radio_button("post[]", "title", "Goodbye World")
277 form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
278 _erbout.concat f.text_field(:title)
279 _erbout.concat f.text_area(:body)
280 _erbout.concat f.check_box(:secret)
281 _erbout.concat f.submit('Create post')
285 "<form action='http://www.example.com' id='create-post' method='post'>" +
286 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
287 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
288 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
289 "<input name='post[secret]' type='hidden' value='0' />" +
290 "<input name='commit' id='post_submit' type='submit' value='Create post' />" +
293 assert_dom_equal expected, _erbout
296 def test_form_for_with_method
299 form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
300 _erbout.concat f.text_field(:title)
301 _erbout.concat f.text_area(:body)
302 _erbout.concat f.check_box(:secret)
306 "<form action='http://www.example.com' id='create-post' method='post'>" +
307 "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
308 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
309 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
310 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
311 "<input name='post[secret]' type='hidden' value='0' />" +
314 assert_dom_equal expected, _erbout
317 def test_form_for_without_object
320 form_for(:post, :html => { :id => 'create-post' }) do |f|
321 _erbout.concat f.text_field(:title)
322 _erbout.concat f.text_area(:body)
323 _erbout.concat f.check_box(:secret)
327 "<form action='http://www.example.com' id='create-post' method='post'>" +
328 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
329 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
330 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
331 "<input name='post[secret]' type='hidden' value='0' />" +
334 assert_dom_equal expected, _erbout
337 def test_form_for_with_index
340 form_for("post[]", @post) do |f|
341 _erbout.concat f.text_field(:title)
342 _erbout.concat f.text_area(:body)
343 _erbout.concat f.check_box(:secret)
347 "<form action='http://www.example.com' method='post'>" +
348 "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
349 "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
350 "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
351 "<input name='post[123][secret]' type='hidden' value='0' />" +
354 assert_dom_equal expected, _erbout
357 def test_nested_fields_for
359 form_for(:post, @post) do |f|
360 f.fields_for(:comment, @post) do |c|
361 _erbout.concat c.text_field(:title)
365 expected = "<form action='http://www.example.com' method='post'>" +
366 "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
369 assert_dom_equal expected, _erbout
375 fields_for(:post, @post) do |f|
376 _erbout.concat f.text_field(:title)
377 _erbout.concat f.text_area(:body)
378 _erbout.concat f.check_box(:secret)
382 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
383 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
384 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
385 "<input name='post[secret]' type='hidden' value='0' />"
387 assert_dom_equal expected, _erbout
390 def test_fields_for_without_object
392 fields_for(:post) do |f|
393 _erbout.concat f.text_field(:title)
394 _erbout.concat f.text_area(:body)
395 _erbout.concat f.check_box(:secret)
399 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
400 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
401 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
402 "<input name='post[secret]' type='hidden' value='0' />"
404 assert_dom_equal expected, _erbout
407 def test_fields_for_object_with_bracketed_name
409 fields_for("author[post]", @post) do |f|
410 _erbout.concat f.text_field(:title)
413 assert_dom_equal "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
417 def test_form_builder_does_not_have_form_for_method
418 assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
421 def test_form_for_and_fields_for
424 form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
425 _erbout.concat post_form.text_field(:title)
426 _erbout.concat post_form.text_area(:body)
428 fields_for(:parent_post, @post) do |parent_fields|
429 _erbout.concat parent_fields.check_box(:secret)
434 "<form action='http://www.example.com' id='create-post' method='post'>" +
435 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
436 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
437 "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
438 "<input name='parent_post[secret]' type='hidden' value='0' />" +
441 assert_dom_equal expected, _erbout
444 class LabelledFormBuilder < ActionView::Helpers::FormBuilder
445 (field_helpers - %w(hidden_field)).each do |selector|
447 def #{selector}(field, *args, &proc)
448 "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
451 class_eval src, __FILE__, __LINE__
455 def test_form_for_with_labelled_builder
458 form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
459 _erbout.concat f.text_field(:title)
460 _erbout.concat f.text_area(:body)
461 _erbout.concat f.check_box(:secret)
465 "<form action='http://www.example.com' method='post'>" +
466 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
467 "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
468 "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
469 "<input name='post[secret]' type='hidden' value='0' /><br/>" +
472 assert_dom_equal expected, _erbout
475 def test_default_form_builder
476 old_default_form_builder, ActionView::Base.default_form_builder =
477 ActionView::Base.default_form_builder, LabelledFormBuilder
480 form_for(:post, @post) do |f|
481 _erbout.concat f.text_field(:title)
482 _erbout.concat f.text_area(:body)
483 _erbout.concat f.check_box(:secret)
487 "<form action='http://www.example.com' method='post'>" +
488 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
489 "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
490 "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
491 "<input name='post[secret]' type='hidden' value='0' /><br/>" +
494 assert_dom_equal expected, _erbout
496 ActionView::Base.default_form_builder = old_default_form_builder
499 def test_default_form_builder_with_active_record_helpers
502 form_for(:post, @post) do |f|
503 _erbout.concat f.error_message_on('author_name')
504 _erbout.concat f.error_messages
507 expected = %(<form action='http://www.example.com' method='post'>) +
508 %(<div class='formError'>can't be empty</div>) +
509 %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>) +
512 assert_dom_equal expected, _erbout
516 # Perhaps this test should be moved to prototype helper tests.
517 def test_remote_form_for_with_labelled_builder
518 self.extend ActionView::Helpers::PrototypeHelper
521 remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
522 _erbout.concat f.text_field(:title)
523 _erbout.concat f.text_area(:body)
524 _erbout.concat f.check_box(:secret)
528 %(<form action="http://www.example.com" onsubmit="new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;" method="post">) +
529 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
530 "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
531 "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
532 "<input name='post[secret]' type='hidden' value='0' /><br/>" +
535 assert_dom_equal expected, _erbout
538 def test_fields_for_with_labelled_builder
541 fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
542 _erbout.concat f.text_field(:title)
543 _erbout.concat f.text_area(:body)
544 _erbout.concat f.check_box(:secret)
548 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
549 "<label for='body'>Body:</label> <textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea><br/>" +
550 "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
551 "<input name='post[secret]' type='hidden' value='0' /><br/>"
553 assert_dom_equal expected, _erbout
556 def test_form_for_with_html_options_adds_options_to_form_tag
559 form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
560 expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
562 assert_dom_equal expected, _erbout
565 def test_form_for_with_string_url_option
568 form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
570 assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', _erbout
573 def test_form_for_with_hash_url_option
576 form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
578 assert_equal 'controller', @controller.url_for_options[:controller]
579 assert_equal 'action', @controller.url_for_options[:action]
582 def test_form_for_with_record_url_option
585 form_for(:post, @post, :url => @post) do |f| end
587 expected = "<form action=\"/posts/123\" method=\"post\"></form>"
588 assert_equal expected, _erbout
591 def test_form_for_with_existing_object
594 form_for(@post) do |f| end
596 expected = "<form action=\"/posts/123\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>"
597 assert_equal expected, _erbout
600 def test_form_for_with_new_object
604 post.new_record = true
605 def post.id() nil end
607 form_for(post) do |f| end
609 expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
610 assert_equal expected, _erbout
613 def test_form_for_with_existing_object_in_list
614 @post.new_record = false
617 form_for([@post, @comment]) {}
619 expected = %(<form action="#{comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
620 assert_dom_equal expected, _erbout
623 def test_form_for_with_new_object_in_list
624 @post.new_record = false
626 form_for([@post, @comment]) {}
628 expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
629 assert_dom_equal expected, _erbout
632 def test_form_for_with_existing_object_and_namespace_in_list
633 @post.new_record = false
636 form_for([:admin, @post, @comment]) {}
638 expected = %(<form action="#{admin_comment_path(@post, @comment)}" class="edit_comment" id="edit_comment_1" method="post"><div style="margin:0;padding:0"><input name="_method" type="hidden" value="put" /></div></form>)
639 assert_dom_equal expected, _erbout
642 def test_form_for_with_new_object_and_namespace_in_list
643 @post.new_record = false
645 form_for([:admin, @post, @comment]) {}
647 expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
648 assert_dom_equal expected, _erbout
651 def test_form_for_with_existing_object_and_custom_url
654 form_for(@post, :url => "/super_posts") do |f| end
656 expected = "<form action=\"/super_posts\" class=\"edit_post\" id=\"edit_post_123\" method=\"post\"><div style=\"margin:0;padding:0\"><input name=\"_method\" type=\"hidden\" value=\"put\" /></div></form>"
657 assert_equal expected, _erbout
660 def test_remote_form_for_with_html_options_adds_options_to_form_tag
661 self.extend ActionView::Helpers::PrototypeHelper
664 remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
665 expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\" onsubmit=\"new Ajax.Request('http://www.example.com', {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)}); return false;\"></form>"
667 assert_dom_equal expected, _erbout
672 def comments_path(post)
673 "/posts/#{post.id}/comments"
675 alias_method :post_comments_path, :comments_path
677 def comment_path(post, comment)
678 "/posts/#{post.id}/comments/#{comment.id}"
680 alias_method :post_comment_path, :comment_path
682 def admin_comments_path(post)
683 "/admin/posts/#{post.id}/comments"
685 alias_method :admin_post_comments_path, :admin_comments_path
687 def admin_comment_path(post, comment)
688 "/admin/posts/#{post.id}/comments/#{comment.id}"
690 alias_method :admin_post_comment_path, :admin_comment_path