Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / actionpack / test / template / form_helper_test.rb
blobfa53f6ae1aea215a40d57bc9a779ba7526d7972a
1 require "#{File.dirname(__FILE__)}/../abstract_unit"
3 silence_warnings do
4   Post = Struct.new(:title, :author_name, :body, :secret, :written_on, :cost)
5   Post.class_eval do
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)
9     
10     def new_record=(boolean)
11       @new_record = boolean
12     end
13     
14     def new_record?
15       @new_record
16     end
17   end
19   class Comment
20     attr_reader :id
21     attr_reader :post_id
22     def save; @id = 1; @post_id = 1 end
23     def new_record?; @id.nil? end
24     def name
25       @id.nil? ? 'new comment' : "comment ##{@id}"
26     end
27   end
28 end
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
43   def setup
44     @post = Post.new
45     @comment = Comment.new
46     def @post.errors() 
47       Class.new{ 
48         def on(field); "can't be empty" if field == "author_name"; end 
49         def empty?() false end 
50         def count() 1 end
51         def full_messages() [ "Author name can't be empty" ] end  
52       }.new 
53     end
54     def @post.id; 123; end
55     def @post.id_before_type_cast; 123; end
56     def @post.to_param; '123'; end
58     @post.title       = "Hello World"
59     @post.author_name = ""
60     @post.body        = "Back to the hill and over it again!"
61     @post.secret      = 1
62     @post.written_on  = Date.new(2004, 6, 15)
64     @controller = Class.new do
65       attr_reader :url_for_options
66       def url_for(options)
67         @url_for_options = options
68         "http://www.example.com"
69       end
70     end
71     @controller = @controller.new
72   end
74   def test_label
75     assert_dom_equal('<label for="post_title">Title</label>', label("post", "title"))
76     assert_dom_equal('<label for="post_title">The title goes here</label>', label("post", "title", "The title goes here"))
77     assert_dom_equal(
78       '<label class="title_label" for="post_title">Title</label>',
79       label("post", "title", nil, :class => 'title_label')
80     )
81   end
82   
83   def test_label_with_symbols
84     assert_dom_equal('<label for="post_title">Title</label>', label(:post, :title))
85   end
87   def test_text_field
88     assert_dom_equal(
89       '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
90     )
91     assert_dom_equal(
92       '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
93     )
94     assert_dom_equal(
95       '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
96     )
97   end
99   def test_text_field_with_escapes
100     @post.title = "<b>Hello World</b>"
101     assert_dom_equal(
102       '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
103     )
104   end
106   def test_text_field_with_options
107     expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
108     assert_dom_equal expected, text_field("post", "title", "size" => 35)
109     assert_dom_equal expected, text_field("post", "title", :size => 35)
110   end
112   def test_text_field_assuming_size
113     expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
114     assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
115     assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
116   end
118   def test_text_field_removing_size
119     expected = '<input id="post_title" maxlength="35" name="post[title]" type="text" value="Hello World" />'
120     assert_dom_equal expected, text_field("post", "title", "maxlength" => 35, "size" => nil)
121     assert_dom_equal expected, text_field("post", "title", :maxlength => 35, :size => nil)
122   end
124   def test_text_field_doesnt_change_param_values
125     object_name = 'post[]'
126     expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
127     assert_equal expected, text_field(object_name, "title")
128     assert_equal object_name, "post[]"
129   end
131   def test_hidden_field
132     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
133       hidden_field("post", "title")
134   end
136   def test_hidden_field_with_escapes
137     @post.title = "<b>Hello World</b>"
138     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="&lt;b&gt;Hello World&lt;/b&gt;" />',
139       hidden_field("post", "title")
140   end
142   def test_text_field_with_options
143     assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Something Else" />',
144       hidden_field("post", "title", :value => "Something Else")
145   end
147   def test_check_box
148     assert_dom_equal(
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")
151     )
152     @post.secret = 0
153     assert_dom_equal(
154       '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
155       check_box("post", "secret")
156     )
157     assert_dom_equal(
158       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
159       check_box("post", "secret" ,{"checked"=>"checked"})
160     )
161     @post.secret = true
162     assert_dom_equal(
163       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
164       check_box("post", "secret")
165     )
166   end
168   def test_check_box_with_explicit_checked_and_unchecked_values
169     @post.secret = "on"
170     assert_dom_equal(
171       '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
172       check_box("post", "secret", {}, "on", "off")
173     )
174   end
176   def test_checkbox_disabled_still_submits_checked_value
177     assert_dom_equal(
178       '<input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="1" />',
179       check_box("post", "secret", { :disabled => :true })
180     )
181   end
183   def test_radio_button
184     assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
185       radio_button("post", "title", "Hello World")
186     )
187     assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
188       radio_button("post", "title", "Goodbye World")
189     )
190   end
192   def test_radio_button_is_checked_with_integers
193     assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
194       radio_button("post", "secret", "1")
195    )
196   end
198   def test_radio_button_respects_passed_in_id
199      assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
200        radio_button("post", "secret", "1", :id=>"foo")
201     )
202   end
204   def test_text_area
205     assert_dom_equal(
206       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
207       text_area("post", "body")
208     )
209   end
211   def test_text_area_with_escapes
212     @post.body        = "Back to <i>the</i> hill and over it again!"
213     assert_dom_equal(
214       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to &lt;i&gt;the&lt;/i&gt; hill and over it again!</textarea>',
215       text_area("post", "body")
216     )
217   end
219   def test_text_area_with_alternate_value
220     assert_dom_equal(
221       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
222       text_area("post", "body", :value => 'Testing alternate values.')
223     )
224   end
226   def test_text_area_with_size_option
227     assert_dom_equal(
228       '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
229       text_area("post", "body", :size => "183x820")
230     )
231   end
233   def test_explicit_name
234     assert_dom_equal(
235       '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
236     )
237     assert_dom_equal(
238       '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
239       text_area("post", "body", "name" => "really!")
240     )
241     assert_dom_equal(
242       '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
243       check_box("post", "secret", "name" => "i mean it")
244     )
245     assert_dom_equal text_field("post", "title", "name" => "dont guess"),
246                  text_field("post", "title", :name => "dont guess")
247     assert_dom_equal text_area("post", "body", "name" => "really!"),
248                  text_area("post", "body", :name => "really!")
249     assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
250                  check_box("post", "secret", :name => "i mean it")
251   end
253   def test_explicit_id
254     assert_dom_equal(
255       '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
256     )
257     assert_dom_equal(
258       '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
259       text_area("post", "body", "id" => "really!")
260     )
261     assert_dom_equal(
262       '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
263       check_box("post", "secret", "id" => "i mean it")
264     )
265     assert_dom_equal text_field("post", "title", "id" => "dont guess"),
266                  text_field("post", "title", :id => "dont guess")
267     assert_dom_equal text_area("post", "body", "id" => "really!"),
268                  text_area("post", "body", :id => "really!")
269     assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
270                  check_box("post", "secret", :id => "i mean it")
271   end
273   def test_auto_index
274     pid = @post.id
275     assert_dom_equal(
276       "<label for=\"post_#{pid}_title\">Title</label>",
277       label("post[]", "title")
278     )
279     assert_dom_equal(
280       "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
281     )
282     assert_dom_equal(
283       "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
284       text_area("post[]", "body")
285     )
286     assert_dom_equal(
287       "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
288       check_box("post[]", "secret")
289     )
290    assert_dom_equal(
291 "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
292       radio_button("post[]", "title", "Hello World")
293     )
294     assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
295       radio_button("post[]", "title", "Goodbye World")
296     )
297   end
299   def test_form_for
300     _erbout = ''
302     form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
303       _erbout.concat f.label(:title)
304       _erbout.concat f.text_field(:title)
305       _erbout.concat f.text_area(:body)
306       _erbout.concat f.check_box(:secret)
307       _erbout.concat f.submit('Create post')
308     end
310     expected = 
311       "<form action='http://www.example.com' id='create-post' method='post'>" +
312       "<label for='post_title'>Title</label>" +
313       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
314       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
315       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
316       "<input name='post[secret]' type='hidden' value='0' />" +
317       "<input name='commit' id='post_submit' type='submit' value='Create post' />" +
318       "</form>"
320     assert_dom_equal expected, _erbout
321   end
323   def test_form_for_with_method
324     _erbout = ''
326     form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
327       _erbout.concat f.text_field(:title)
328       _erbout.concat f.text_area(:body)
329       _erbout.concat f.check_box(:secret)
330     end
332     expected = 
333       "<form action='http://www.example.com' id='create-post' method='post'>" +
334       "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
335       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
336       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
337       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
338       "<input name='post[secret]' type='hidden' value='0' />" +
339       "</form>"
341     assert_dom_equal expected, _erbout
342   end
344   def test_form_for_without_object
345     _erbout = ''
347     form_for(:post, :html => { :id => 'create-post' }) do |f|
348       _erbout.concat f.text_field(:title)
349       _erbout.concat f.text_area(:body)
350       _erbout.concat f.check_box(:secret)
351     end
353     expected = 
354       "<form action='http://www.example.com' id='create-post' method='post'>" +
355       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
356       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
357       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
358       "<input name='post[secret]' type='hidden' value='0' />" +
359       "</form>"
361     assert_dom_equal expected, _erbout
362   end
364   def test_form_for_with_index
365     _erbout = ''
366     
367     form_for("post[]", @post) do |f|
368       _erbout.concat f.label(:title)
369       _erbout.concat f.text_field(:title)
370       _erbout.concat f.text_area(:body)
371       _erbout.concat f.check_box(:secret)
372     end
373     
374     expected = 
375       "<form action='http://www.example.com' method='post'>" +
376       "<label for=\"post_123_title\">Title</label>" +
377       "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
378       "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
379       "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
380       "<input name='post[123][secret]' type='hidden' value='0' />" +
381       "</form>"
383     assert_dom_equal expected, _erbout
384   end
386   def test_nested_fields_for
387     _erbout = ''
388     form_for(:post, @post) do |f|
389       f.fields_for(:comment, @post) do |c|
390         _erbout.concat c.text_field(:title)
391       end
392     end
394     expected = "<form action='http://www.example.com' method='post'>" +
395                "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
396                "</form>"
398     assert_dom_equal expected, _erbout
399   end
401   def test_fields_for
402     _erbout = ''
404     fields_for(:post, @post) do |f|
405       _erbout.concat f.text_field(:title)
406       _erbout.concat f.text_area(:body)
407       _erbout.concat f.check_box(:secret)
408     end
410     expected = 
411       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
412       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
413       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
414       "<input name='post[secret]' type='hidden' value='0' />"
416     assert_dom_equal expected, _erbout
417   end
419   def test_fields_for_without_object
420     _erbout = ''
421     fields_for(:post) do |f|
422       _erbout.concat f.text_field(:title)
423       _erbout.concat f.text_area(:body)
424       _erbout.concat f.check_box(:secret)
425     end
427     expected = 
428       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
429       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
430       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
431       "<input name='post[secret]' type='hidden' value='0' />"
433     assert_dom_equal expected, _erbout
434   end
436   def test_fields_for_object_with_bracketed_name
437     _erbout = ''
438     fields_for("author[post]", @post) do |f|
439       _erbout.concat f.label(:title)
440       _erbout.concat f.text_field(:title)
441     end
443     assert_dom_equal "<label for=\"author_post_title\">Title</label>" +
444     "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
445       _erbout
446   end
448   def test_form_builder_does_not_have_form_for_method
449     assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
450   end
452   def test_form_for_and_fields_for
453     _erbout = ''
455     form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
456       _erbout.concat post_form.text_field(:title)
457       _erbout.concat post_form.text_area(:body)
459       fields_for(:parent_post, @post) do |parent_fields|
460         _erbout.concat parent_fields.check_box(:secret)
461       end
462     end
464     expected = 
465       "<form action='http://www.example.com' id='create-post' method='post'>" +
466       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
467       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
468       "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
469       "<input name='parent_post[secret]' type='hidden' value='0' />" +
470       "</form>"
472     assert_dom_equal expected, _erbout
473   end
475   class LabelledFormBuilder < ActionView::Helpers::FormBuilder
476     (field_helpers - %w(hidden_field)).each do |selector|
477       src = <<-END_SRC
478         def #{selector}(field, *args, &proc)
479           "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
480         end
481       END_SRC
482       class_eval src, __FILE__, __LINE__
483     end
484   end
486   def test_form_for_with_labelled_builder
487     _erbout = ''
489     form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
490       _erbout.concat f.text_field(:title)
491       _erbout.concat f.text_area(:body)
492       _erbout.concat f.check_box(:secret)
493     end
495     expected = 
496       "<form action='http://www.example.com' method='post'>" +
497       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
498       "<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/>" +
499       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
500       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
501       "</form>"
503     assert_dom_equal expected, _erbout
504   end
506   def test_default_form_builder
507     old_default_form_builder, ActionView::Base.default_form_builder =
508       ActionView::Base.default_form_builder, LabelledFormBuilder
510     _erbout = ''
511     form_for(:post, @post) do |f|
512       _erbout.concat f.text_field(:title)
513       _erbout.concat f.text_area(:body)
514       _erbout.concat f.check_box(:secret)
515     end
517     expected = 
518       "<form action='http://www.example.com' method='post'>" +
519       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
520       "<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/>" +
521       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
522       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
523       "</form>"
525     assert_dom_equal expected, _erbout
526   ensure
527     ActionView::Base.default_form_builder = old_default_form_builder
528   end
530   def test_default_form_builder_with_active_record_helpers
531      
532     _erbout = '' 
533     form_for(:post, @post) do |f|
534        _erbout.concat f.error_message_on('author_name')
535        _erbout.concat f.error_messages
536     end    
537     
538     expected = %(<form action='http://www.example.com' method='post'>) + 
539                %(<div class='formError'>can't be empty</div>) + 
540                %(<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>) +
541                %(</form>)
542     
543     assert_dom_equal expected, _erbout
545   end
546   
547   def test_default_form_builder_no_instance_variable
548     post = @post
549     @post = nil
550     
551     _erbout = '' 
552     form_for(:post, post) do |f|
553        _erbout.concat f.error_message_on('author_name')
554        _erbout.concat f.error_messages
555     end    
556     
557     expected = %(<form action='http://www.example.com' method='post'>) + 
558                %(<div class='formError'>can't be empty</div>) + 
559                %(<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>) +
560                %(</form>)
561     
562     assert_dom_equal expected, _erbout
563     
564   end
566   # Perhaps this test should be moved to prototype helper tests.
567   def test_remote_form_for_with_labelled_builder
568     self.extend ActionView::Helpers::PrototypeHelper
569      _erbout = ''
571      remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
572        _erbout.concat f.text_field(:title)
573        _erbout.concat f.text_area(:body)
574        _erbout.concat f.check_box(:secret)
575      end
577      expected = 
578        %(<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">) +
579        "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
580        "<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/>" +
581        "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
582        "<input name='post[secret]' type='hidden' value='0' /><br/>" +
583        "</form>"
585      assert_dom_equal expected, _erbout
586   end
587    
588   def test_fields_for_with_labelled_builder
589     _erbout = ''
590     
591     fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
592       _erbout.concat f.text_field(:title)
593       _erbout.concat f.text_area(:body)
594       _erbout.concat f.check_box(:secret)
595     end
596     
597     expected = 
598       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
599       "<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/>" +
600       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
601       "<input name='post[secret]' type='hidden' value='0' /><br/>"
602     
603     assert_dom_equal expected, _erbout
604   end
606   def test_form_for_with_html_options_adds_options_to_form_tag
607     _erbout = ''
608     
609     form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
610     expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
611     
612     assert_dom_equal expected, _erbout
613   end
615   def test_form_for_with_string_url_option
616     _erbout = ''
618     form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
620     assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', _erbout
621   end
623   def test_form_for_with_hash_url_option
624     _erbout = ''
626     form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
628     assert_equal 'controller', @controller.url_for_options[:controller]
629     assert_equal 'action', @controller.url_for_options[:action]
630   end
632   def test_form_for_with_record_url_option
633     _erbout = ''
635     form_for(:post, @post, :url => @post) do |f| end
637     expected = "<form action=\"/posts/123\" method=\"post\"></form>"
638     assert_equal expected, _erbout
639   end
641   def test_form_for_with_existing_object
642     _erbout = ''
644     form_for(@post) do |f| end
646     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>"
647     assert_equal expected, _erbout
648   end
650   def test_form_for_with_new_object
651     _erbout = ''
653     post = Post.new
654     post.new_record = true
655     def post.id() nil end
657     form_for(post) do |f| end
659     expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
660     assert_equal expected, _erbout
661   end
663   def test_form_for_with_existing_object_in_list
664     @post.new_record = false
665     @comment.save
666     _erbout = ''
667     form_for([@post, @comment]) {}
669     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>)
670     assert_dom_equal expected, _erbout
671   end
673   def test_form_for_with_new_object_in_list
674     @post.new_record = false
675     _erbout = ''
676     form_for([@post, @comment]) {}
678     expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
679     assert_dom_equal expected, _erbout
680   end
682   def test_form_for_with_existing_object_and_namespace_in_list
683     @post.new_record = false
684     @comment.save
685     _erbout = ''
686     form_for([:admin, @post, @comment]) {}
687   
688     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>)
689     assert_dom_equal expected, _erbout
690   end
691   
692   def test_form_for_with_new_object_and_namespace_in_list
693     @post.new_record = false
694     _erbout = ''
695     form_for([:admin, @post, @comment]) {}
696   
697     expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
698     assert_dom_equal expected, _erbout
699   end
701   def test_form_for_with_existing_object_and_custom_url
702     _erbout = ''
704     form_for(@post, :url => "/super_posts") do |f| end
706     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>"
707     assert_equal expected, _erbout
708   end
710   def test_remote_form_for_with_html_options_adds_options_to_form_tag
711     self.extend ActionView::Helpers::PrototypeHelper
712     _erbout = ''
713     
714     remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
715     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>"
716     
717     assert_dom_equal expected, _erbout
718   end
721   protected
722     def comments_path(post)
723       "/posts/#{post.id}/comments"
724     end
725     alias_method :post_comments_path, :comments_path
727     def comment_path(post, comment)
728       "/posts/#{post.id}/comments/#{comment.id}"
729     end
730     alias_method :post_comment_path, :comment_path
731     
732     def admin_comments_path(post)
733       "/admin/posts/#{post.id}/comments"
734     end
735     alias_method :admin_post_comments_path, :admin_comments_path
736     
737     def admin_comment_path(post, comment)
738       "/admin/posts/#{post.id}/comments/#{comment.id}"
739     end
740     alias_method :admin_post_comment_path, :admin_comment_path
741     
742     def posts_path
743       "/posts"
744     end 
745     
746     def post_path(post)
747       "/posts/#{post.id}"
748     end
750     def protect_against_forgery?
751       false
752     end