Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/test/template/form_helper_test.rb

Revision 9088, 33.3 kB (checked in by david, 2 years ago)

Fixed that FormHelper#radio_button would produce invalid ids (closes #11298) [harlancrystal]

Line 
1 require 'abstract_unit'
2
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
18
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
29
30 class Comment::Nested < Comment; end
31
32
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
42
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
57
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)
63
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
73
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
86
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
98
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
105
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
111
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
117
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
123
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
130
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
135
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
141
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
146
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
167
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
175
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
182
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     assert_dom_equal('<input id="item_subobject_title_inside_world" name="item[subobject][title]" type="radio" value="inside world"/>',
191       radio_button("item[subobject]", "title", "inside world")
192     )
193   end
194
195   def test_radio_button_is_checked_with_integers
196     assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
197       radio_button("post", "secret", "1")
198    )
199   end
200
201   def test_radio_button_respects_passed_in_id
202      assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
203        radio_button("post", "secret", "1", :id=>"foo")
204     )
205   end
206
207   def test_text_area
208     assert_dom_equal(
209       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
210       text_area("post", "body")
211     )
212   end
213
214   def test_text_area_with_escapes
215     @post.body        = "Back to <i>the</i> hill and over it again!"
216     assert_dom_equal(
217       '<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>',
218       text_area("post", "body")
219     )
220   end
221
222   def test_text_area_with_alternate_value
223     assert_dom_equal(
224       '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
225       text_area("post", "body", :value => 'Testing alternate values.')
226     )
227   end
228
229   def test_text_area_with_size_option
230     assert_dom_equal(
231       '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
232       text_area("post", "body", :size => "183x820")
233     )
234   end
235
236   def test_explicit_name
237     assert_dom_equal(
238       '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
239     )
240     assert_dom_equal(
241       '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
242       text_area("post", "body", "name" => "really!")
243     )
244     assert_dom_equal(
245       '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
246       check_box("post", "secret", "name" => "i mean it")
247     )
248     assert_dom_equal text_field("post", "title", "name" => "dont guess"),
249                  text_field("post", "title", :name => "dont guess")
250     assert_dom_equal text_area("post", "body", "name" => "really!"),
251                  text_area("post", "body", :name => "really!")
252     assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
253                  check_box("post", "secret", :name => "i mean it")
254   end
255
256   def test_explicit_id
257     assert_dom_equal(
258       '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
259     )
260     assert_dom_equal(
261       '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
262       text_area("post", "body", "id" => "really!")
263     )
264     assert_dom_equal(
265       '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
266       check_box("post", "secret", "id" => "i mean it")
267     )
268     assert_dom_equal text_field("post", "title", "id" => "dont guess"),
269                  text_field("post", "title", :id => "dont guess")
270     assert_dom_equal text_area("post", "body", "id" => "really!"),
271                  text_area("post", "body", :id => "really!")
272     assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
273                  check_box("post", "secret", :id => "i mean it")
274   end
275
276   def test_auto_index
277     pid = @post.id
278     assert_dom_equal(
279       "<label for=\"post_#{pid}_title\">Title</label>",
280       label("post[]", "title")
281     )
282     assert_dom_equal(
283       "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
284     )
285     assert_dom_equal(
286       "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
287       text_area("post[]", "body")
288     )
289     assert_dom_equal(
290       "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
291       check_box("post[]", "secret")
292     )
293    assert_dom_equal(
294 "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
295       radio_button("post[]", "title", "Hello World")
296     )
297     assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
298       radio_button("post[]", "title", "Goodbye World")
299     )
300   end
301
302   def test_form_for
303     _erbout = ''
304
305     form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
306       _erbout.concat f.label(:title)
307       _erbout.concat f.text_field(:title)
308       _erbout.concat f.text_area(:body)
309       _erbout.concat f.check_box(:secret)
310       _erbout.concat f.submit('Create post')
311     end
312
313     expected =
314       "<form action='http://www.example.com' id='create-post' method='post'>" +
315       "<label for='post_title'>Title</label>" +
316       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
317       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
318       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
319       "<input name='post[secret]' type='hidden' value='0' />" +
320       "<input name='commit' id='post_submit' type='submit' value='Create post' />" +
321       "</form>"
322
323     assert_dom_equal expected, _erbout
324   end
325
326   def test_form_for_with_method
327     _erbout = ''
328
329     form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
330       _erbout.concat f.text_field(:title)
331       _erbout.concat f.text_area(:body)
332       _erbout.concat f.check_box(:secret)
333     end
334
335     expected =
336       "<form action='http://www.example.com' id='create-post' method='post'>" +
337       "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
338       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
339       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
340       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
341       "<input name='post[secret]' type='hidden' value='0' />" +
342       "</form>"
343
344     assert_dom_equal expected, _erbout
345   end
346
347   def test_form_for_without_object
348     _erbout = ''
349
350     form_for(:post, :html => { :id => 'create-post' }) do |f|
351       _erbout.concat f.text_field(:title)
352       _erbout.concat f.text_area(:body)
353       _erbout.concat f.check_box(:secret)
354     end
355
356     expected =
357       "<form action='http://www.example.com' id='create-post' method='post'>" +
358       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
359       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
360       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
361       "<input name='post[secret]' type='hidden' value='0' />" +
362       "</form>"
363
364     assert_dom_equal expected, _erbout
365   end
366
367   def test_form_for_with_index
368     _erbout = ''
369
370     form_for("post[]", @post) do |f|
371       _erbout.concat f.label(:title)
372       _erbout.concat f.text_field(:title)
373       _erbout.concat f.text_area(:body)
374       _erbout.concat f.check_box(:secret)
375     end
376
377     expected =
378       "<form action='http://www.example.com' method='post'>" +
379       "<label for=\"post_123_title\">Title</label>" +
380       "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
381       "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
382       "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
383       "<input name='post[123][secret]' type='hidden' value='0' />" +
384       "</form>"
385
386     assert_dom_equal expected, _erbout
387   end
388
389   def test_form_for_with_nil_index_option_override
390     _erbout = ''
391
392     form_for("post[]", @post, :index => nil) do |f|
393       _erbout.concat f.text_field(:title)
394       _erbout.concat f.text_area(:body)
395       _erbout.concat f.check_box(:secret)
396     end
397
398     expected =
399       "<form action='http://www.example.com' method='post'>" +
400       "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
401       "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
402       "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
403       "<input name='post[][secret]' type='hidden' value='0' />" +
404       "</form>"
405
406     assert_dom_equal expected, _erbout
407   end
408
409   def test_nested_fields_for
410     _erbout = ''
411     form_for(:post, @post) do |f|
412       f.fields_for(:comment, @post) do |c|
413         _erbout.concat c.text_field(:title)
414       end
415     end
416
417     expected = "<form action='http://www.example.com' method='post'>" +
418                "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
419                "</form>"
420
421     assert_dom_equal expected, _erbout
422   end
423
424   def test_fields_for
425     _erbout = ''
426
427     fields_for(:post, @post) do |f|
428       _erbout.concat f.text_field(:title)
429       _erbout.concat f.text_area(:body)
430       _erbout.concat f.check_box(:secret)
431     end
432
433     expected =
434       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
435       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
436       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
437       "<input name='post[secret]' type='hidden' value='0' />"
438
439     assert_dom_equal expected, _erbout
440   end
441
442   def test_fields_for_with_index
443     _erbout = ''
444
445     fields_for("post[]", @post) do |f|
446       _erbout.concat f.text_field(:title)
447       _erbout.concat f.text_area(:body)
448       _erbout.concat f.check_box(:secret)
449     end
450
451     expected =
452       "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
453       "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
454       "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
455       "<input name='post[123][secret]' type='hidden' value='0' />"
456
457     assert_dom_equal expected, _erbout
458   end
459
460   def test_fields_for_with_nil_index_option_override
461     _erbout = ''
462
463     fields_for("post[]", @post, :index => nil) do |f|
464       _erbout.concat f.text_field(:title)
465       _erbout.concat f.text_area(:body)
466       _erbout.concat f.check_box(:secret)
467     end
468
469     expected =
470       "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
471       "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
472       "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
473       "<input name='post[][secret]' type='hidden' value='0' />"
474
475     assert_dom_equal expected, _erbout
476   end
477
478   def test_fields_for_with_index_option_override
479     _erbout = ''
480
481     fields_for("post[]", @post, :index => "abc") do |f|
482       _erbout.concat f.text_field(:title)
483       _erbout.concat f.text_area(:body)
484       _erbout.concat f.check_box(:secret)
485     end
486
487     expected =
488       "<input name='post[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" +
489       "<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
490       "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />" +
491       "<input name='post[abc][secret]' type='hidden' value='0' />"
492
493     assert_dom_equal expected, _erbout
494   end
495
496   def test_fields_for_without_object
497     _erbout = ''
498     fields_for(:post) do |f|
499       _erbout.concat f.text_field(:title)
500       _erbout.concat f.text_area(:body)
501       _erbout.concat f.check_box(:secret)
502     end
503
504     expected =
505       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
506       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
507       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
508       "<input name='post[secret]' type='hidden' value='0' />"
509
510     assert_dom_equal expected, _erbout
511   end
512
513   def test_fields_for_with_only_object
514     _erbout = ''
515     fields_for(@post) do |f|
516       _erbout.concat f.text_field(:title)
517       _erbout.concat f.text_area(:body)
518       _erbout.concat f.check_box(:secret)
519     end
520
521     expected =
522       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
523       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
524       "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
525       "<input name='post[secret]' type='hidden' value='0' />"
526
527     assert_dom_equal expected, _erbout
528   end
529
530   def test_fields_for_object_with_bracketed_name
531     _erbout = ''
532     fields_for("author[post]", @post) do |f|
533       _erbout.concat f.label(:title)
534       _erbout.concat f.text_field(:title)
535     end
536
537     assert_dom_equal "<label for=\"author_post_title\">Title</label>" +
538     "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
539       _erbout
540   end
541
542   def test_form_builder_does_not_have_form_for_method
543     assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
544   end
545
546   def test_form_for_and_fields_for
547     _erbout = ''
548
549     form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
550       _erbout.concat post_form.text_field(:title)
551       _erbout.concat post_form.text_area(:body)
552
553       fields_for(:parent_post, @post) do |parent_fields|
554         _erbout.concat parent_fields.check_box(:secret)
555       end
556     end
557
558     expected =
559       "<form action='http://www.example.com' id='create-post' method='post'>" +
560       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
561       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
562       "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
563       "<input name='parent_post[secret]' type='hidden' value='0' />" +
564       "</form>"
565
566     assert_dom_equal expected, _erbout
567   end
568
569   def test_form_for_and_fields_for_with_object
570     _erbout = ''
571
572     form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
573       _erbout.concat post_form.text_field(:title)
574       _erbout.concat post_form.text_area(:body)
575
576       post_form.fields_for(@comment) do |comment_fields|
577         _erbout.concat comment_fields.text_field(:name)
578       end
579     end
580
581     expected =
582       "<form action='http://www.example.com' id='create-post' method='post'>" +
583       "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
584       "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
585       "<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' size='30' />" +
586       "</form>"
587
588     assert_dom_equal expected, _erbout
589   end
590
591   class LabelledFormBuilder < ActionView::Helpers::FormBuilder
592     (field_helpers - %w(hidden_field)).each do |selector|
593       src = <<-END_SRC
594         def #{selector}(field, *args, &proc)
595           "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
596         end
597       END_SRC
598       class_eval src, __FILE__, __LINE__
599     end
600   end
601
602   def test_form_for_with_labelled_builder
603     _erbout = ''
604
605     form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
606       _erbout.concat f.text_field(:title)
607       _erbout.concat f.text_area(:body)
608       _erbout.concat f.check_box(:secret)
609     end
610
611     expected =
612       "<form action='http://www.example.com' method='post'>" +
613       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
614       "<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/>" +
615       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
616       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
617       "</form>"
618
619     assert_dom_equal expected, _erbout
620   end
621
622   def test_default_form_builder
623     old_default_form_builder, ActionView::Base.default_form_builder =
624       ActionView::Base.default_form_builder, LabelledFormBuilder
625
626     _erbout = ''
627     form_for(:post, @post) do |f|
628       _erbout.concat f.text_field(:title)
629       _erbout.concat f.text_area(:body)
630       _erbout.concat f.check_box(:secret)
631     end
632
633     expected =
634       "<form action='http://www.example.com' method='post'>" +
635       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
636       "<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/>" +
637       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
638       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
639       "</form>"
640
641     assert_dom_equal expected, _erbout
642   ensure
643     ActionView::Base.default_form_builder = old_default_form_builder
644   end
645
646   def test_default_form_builder_with_active_record_helpers
647      
648     _erbout = ''
649     form_for(:post, @post) do |f|
650        _erbout.concat f.error_message_on('author_name')
651        _erbout.concat f.error_messages
652     end   
653    
654     expected = %(<form action='http://www.example.com' method='post'>) +
655                %(<div class='formError'>can't be empty</div>) +
656                %(<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>) +
657                %(</form>)
658    
659     assert_dom_equal expected, _erbout
660
661   end
662  
663   def test_default_form_builder_no_instance_variable
664     post = @post
665     @post = nil
666    
667     _erbout = ''
668     form_for(:post, post) do |f|
669        _erbout.concat f.error_message_on('author_name')
670        _erbout.concat f.error_messages
671     end   
672    
673     expected = %(<form action='http://www.example.com' method='post'>) +
674                %(<div class='formError'>can't be empty</div>) +
675                %(<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>) +
676                %(</form>)
677    
678     assert_dom_equal expected, _erbout
679    
680   end
681
682   # Perhaps this test should be moved to prototype helper tests.
683   def test_remote_form_for_with_labelled_builder
684     self.extend ActionView::Helpers::PrototypeHelper
685      _erbout = ''
686
687      remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
688        _erbout.concat f.text_field(:title)
689        _erbout.concat f.text_area(:body)
690        _erbout.concat f.check_box(:secret)
691      end
692
693      expected =
694        %(<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">) +
695        "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
696        "<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/>" +
697        "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
698        "<input name='post[secret]' type='hidden' value='0' /><br/>" +
699        "</form>"
700
701      assert_dom_equal expected, _erbout
702   end
703    
704   def test_fields_for_with_labelled_builder
705     _erbout = ''
706    
707     fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
708       _erbout.concat f.text_field(:title)
709       _erbout.concat f.text_area(:body)
710       _erbout.concat f.check_box(:secret)
711     end
712    
713     expected =
714       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
715       "<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/>" +
716       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
717       "<input name='post[secret]' type='hidden' value='0' /><br/>"
718    
719     assert_dom_equal expected, _erbout
720   end
721
722   def test_form_for_with_html_options_adds_options_to_form_tag
723     _erbout = ''
724    
725     form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
726     expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
727    
728     assert_dom_equal expected, _erbout
729   end
730
731   def test_form_for_with_string_url_option
732     _erbout = ''
733
734     form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
735
736     assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', _erbout
737   end
738
739   def test_form_for_with_hash_url_option
740     _erbout = ''
741
742     form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
743
744     assert_equal 'controller', @controller.url_for_options[:controller]
745     assert_equal 'action', @controller.url_for_options[:action]
746   end
747
748   def test_form_for_with_record_url_option
749     _erbout = ''
750
751     form_for(:post, @post, :url => @post) do |f| end
752
753     expected = "<form action=\"/posts/123\" method=\"post\"></form>"
754     assert_equal expected, _erbout
755   end
756
757   def test_form_for_with_existing_object
758     _erbout = ''
759
760     form_for(@post) do |f| end
761
762     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>"
763     assert_equal expected, _erbout
764   end
765
766   def test_form_for_with_new_object
767     _erbout = ''
768
769     post = Post.new
770     post.new_record = true
771     def post.id() nil end
772
773     form_for(post) do |f| end
774
775     expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
776     assert_equal expected, _erbout
777   end
778
779   def test_form_for_with_existing_object_in_list
780     @post.new_record = false
781     @comment.save
782     _erbout = ''
783     form_for([@post, @comment]) {}
784
785     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>)
786     assert_dom_equal expected, _erbout
787   end
788
789   def test_form_for_with_new_object_in_list
790     @post.new_record = false
791     _erbout = ''
792     form_for([@post, @comment]) {}
793
794     expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
795     assert_dom_equal expected, _erbout
796   end
797
798   def test_form_for_with_existing_object_and_namespace_in_list
799     @post.new_record = false
800     @comment.save
801     _erbout = ''
802     form_for([:admin, @post, @comment]) {}
803  
804     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>)
805     assert_dom_equal expected, _erbout
806   end
807  
808   def test_form_for_with_new_object_and_namespace_in_list
809     @post.new_record = false
810     _erbout = ''
811     form_for([:admin, @post, @comment]) {}
812  
813     expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
814     assert_dom_equal expected, _erbout
815   end
816
817   def test_form_for_with_existing_object_and_custom_url
818     _erbout = ''
819
820     form_for(@post, :url => "/super_posts") do |f| end
821
822     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>"
823     assert_equal expected, _erbout
824   end
825
826   def test_remote_form_for_with_html_options_adds_options_to_form_tag
827     self.extend ActionView::Helpers::PrototypeHelper
828     _erbout = ''
829    
830     remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
831     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>"
832    
833     assert_dom_equal expected, _erbout
834   end
835
836
837   protected
838     def comments_path(post)
839       "/posts/#{post.id}/comments"
840     end
841     alias_method :post_comments_path, :comments_path
842
843     def comment_path(post, comment)
844       "/posts/#{post.id}/comments/#{comment.id}"
845     end
846     alias_method :post_comment_path, :comment_path
847    
848     def admin_comments_path(post)
849       "/admin/posts/#{post.id}/comments"
850     end
851     alias_method :admin_post_comments_path, :admin_comments_path
852    
853     def admin_comment_path(post, comment)
854       "/admin/posts/#{post.id}/comments/#{comment.id}"
855     end
856     alias_method :admin_post_comment_path, :admin_comment_path
857    
858     def posts_path
859       "/posts"
860     end
861    
862     def post_path(post)
863       "/posts/#{post.id}"
864     end
865
866     def protect_against_forgery?
867       false
868     end
869 end
Note: See TracBrowser for help on using the browser.