Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / test / template / form_helper_test.rb
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 alias_method :secret?, :secret
10
11 def new_record=(boolean)
12 @new_record = boolean
13 end
14
15 def new_record?
16 @new_record
17 end
18
19 attr_accessor :author
20 def author_attributes=(attributes); end
21
22 attr_accessor :comments
23 def comments_attributes=(attributes); end
24 end
25
26 class Comment
27 attr_reader :id
28 attr_reader :post_id
29 def initialize(id = nil, post_id = nil); @id, @post_id = id, post_id end
30 def save; @id = 1; @post_id = 1 end
31 def new_record?; @id.nil? end
32 def to_param; @id; end
33 def name
34 @id.nil? ? "new #{self.class.name.downcase}" : "#{self.class.name.downcase} ##{@id}"
35 end
36 end
37
38 class Author < Comment
39 attr_accessor :post
40 def post_attributes=(attributes); end
41 end
42 end
43
44 class FormHelperTest < ActionView::TestCase
45 tests ActionView::Helpers::FormHelper
46
47 def setup
48 @post = Post.new
49 @comment = Comment.new
50 def @post.errors()
51 Class.new{
52 def on(field); "can't be empty" if field == "author_name"; end
53 def empty?() false end
54 def count() 1 end
55 def full_messages() [ "Author name can't be empty" ] end
56 }.new
57 end
58 def @post.id; 123; end
59 def @post.id_before_type_cast; 123; end
60 def @post.to_param; '123'; end
61
62 @post.title = "Hello World"
63 @post.author_name = ""
64 @post.body = "Back to the hill and over it again!"
65 @post.secret = 1
66 @post.written_on = Date.new(2004, 6, 15)
67
68 @controller = Class.new do
69 attr_reader :url_for_options
70 def url_for(options)
71 @url_for_options = options
72 "http://www.example.com"
73 end
74 end
75 @controller = @controller.new
76 end
77
78 def test_label
79 assert_dom_equal('<label for="post_title">Title</label>', label("post", "title"))
80 assert_dom_equal('<label for="post_title">The title goes here</label>', label("post", "title", "The title goes here"))
81 assert_dom_equal(
82 '<label class="title_label" for="post_title">Title</label>',
83 label("post", "title", nil, :class => 'title_label')
84 )
85 assert_dom_equal('<label for="post_secret">Secret?</label>', label("post", "secret?"))
86 end
87
88 def test_label_with_symbols
89 assert_dom_equal('<label for="post_title">Title</label>', label(:post, :title))
90 assert_dom_equal('<label for="post_secret">Secret?</label>', label(:post, :secret?))
91 end
92
93 def test_label_with_for_attribute_as_symbol
94 assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, :for => "my_for"))
95 end
96
97 def test_label_with_for_attribute_as_string
98 assert_dom_equal('<label for="my_for">Title</label>', label(:post, :title, nil, "for" => "my_for"))
99 end
100
101 def test_text_field
102 assert_dom_equal(
103 '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
104 )
105 assert_dom_equal(
106 '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
107 )
108 assert_dom_equal(
109 '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
110 )
111 end
112
113 def test_text_field_with_escapes
114 @post.title = "<b>Hello World</b>"
115 assert_dom_equal(
116 '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
117 )
118 end
119
120 def test_text_field_with_html_entities
121 @post.title = "The HTML Entity for & is &amp;"
122 assert_dom_equal(
123 '<input id="post_title" name="post[title]" size="30" type="text" value="The HTML Entity for &amp; is &amp;amp;" />',
124 text_field("post", "title")
125 )
126 end
127
128 def test_text_field_with_options
129 expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
130 assert_dom_equal expected, text_field("post", "title", "size" => 35)
131 assert_dom_equal expected, text_field("post", "title", :size => 35)
132 end
133
134 def test_text_field_assuming_size
135 expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
136 assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
137 assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
138 end
139
140 def test_text_field_removing_size
141 expected = '<input id="post_title" maxlength="35" name="post[title]" type="text" value="Hello World" />'
142 assert_dom_equal expected, text_field("post", "title", "maxlength" => 35, "size" => nil)
143 assert_dom_equal expected, text_field("post", "title", :maxlength => 35, :size => nil)
144 end
145
146 def test_text_field_doesnt_change_param_values
147 object_name = 'post[]'
148 expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
149 assert_equal expected, text_field(object_name, "title")
150 assert_equal object_name, "post[]"
151 end
152
153 def test_hidden_field
154 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Hello World" />',
155 hidden_field("post", "title")
156 assert_dom_equal '<input id="post_secret" name="post[secret]" type="hidden" value="1" />',
157 hidden_field("post", "secret?")
158 end
159
160 def test_hidden_field_with_escapes
161 @post.title = "<b>Hello World</b>"
162 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="&lt;b&gt;Hello World&lt;/b&gt;" />',
163 hidden_field("post", "title")
164 end
165
166 def test_text_field_with_options
167 assert_dom_equal '<input id="post_title" name="post[title]" type="hidden" value="Something Else" />',
168 hidden_field("post", "title", :value => "Something Else")
169 end
170
171 def test_check_box
172 assert_dom_equal(
173 '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
174 check_box("post", "secret")
175 )
176 @post.secret = 0
177 assert_dom_equal(
178 '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
179 check_box("post", "secret")
180 )
181 assert_dom_equal(
182 '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
183 check_box("post", "secret" ,{"checked"=>"checked"})
184 )
185 @post.secret = true
186 assert_dom_equal(
187 '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
188 check_box("post", "secret")
189 )
190 assert_dom_equal(
191 '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
192 check_box("post", "secret?")
193 )
194
195 @post.secret = ['0']
196 assert_dom_equal(
197 '<input name="post[secret]" type="hidden" value="0" /><input id="post_secret" name="post[secret]" type="checkbox" value="1" />',
198 check_box("post", "secret")
199 )
200 @post.secret = ['1']
201 assert_dom_equal(
202 '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
203 check_box("post", "secret")
204 )
205 end
206
207 def test_check_box_with_explicit_checked_and_unchecked_values
208 @post.secret = "on"
209 assert_dom_equal(
210 '<input name="post[secret]" type="hidden" value="off" /><input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" />',
211 check_box("post", "secret", {}, "on", "off")
212 )
213 end
214
215 def test_checkbox_disabled_still_submits_checked_value
216 assert_dom_equal(
217 '<input name="post[secret]" type="hidden" value="1" /><input checked="checked" disabled="disabled" id="post_secret" name="post[secret]" type="checkbox" value="1" />',
218 check_box("post", "secret", { :disabled => :true })
219 )
220 end
221
222 def test_radio_button
223 assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
224 radio_button("post", "title", "Hello World")
225 )
226 assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
227 radio_button("post", "title", "Goodbye World")
228 )
229 assert_dom_equal('<input id="item_subobject_title_inside_world" name="item[subobject][title]" type="radio" value="inside world"/>',
230 radio_button("item[subobject]", "title", "inside world")
231 )
232 end
233
234 def test_radio_button_is_checked_with_integers
235 assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
236 radio_button("post", "secret", "1")
237 )
238 end
239
240 def test_radio_button_respects_passed_in_id
241 assert_dom_equal('<input checked="checked" id="foo" name="post[secret]" type="radio" value="1" />',
242 radio_button("post", "secret", "1", :id=>"foo")
243 )
244 end
245
246 def test_text_area
247 assert_dom_equal(
248 '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
249 text_area("post", "body")
250 )
251 end
252
253 def test_text_area_with_escapes
254 @post.body = "Back to <i>the</i> hill and over it again!"
255 assert_dom_equal(
256 '<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>',
257 text_area("post", "body")
258 )
259 end
260
261 def test_text_area_with_alternate_value
262 assert_dom_equal(
263 '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
264 text_area("post", "body", :value => 'Testing alternate values.')
265 )
266 end
267
268 def test_text_area_with_html_entities
269 @post.body = "The HTML Entity for & is &amp;"
270 assert_dom_equal(
271 '<textarea cols="40" id="post_body" name="post[body]" rows="20">The HTML Entity for &amp; is &amp;amp;</textarea>',
272 text_area("post", "body")
273 )
274 end
275
276 def test_text_area_with_size_option
277 assert_dom_equal(
278 '<textarea cols="183" id="post_body" name="post[body]" rows="820">Back to the hill and over it again!</textarea>',
279 text_area("post", "body", :size => "183x820")
280 )
281 end
282
283 def test_explicit_name
284 assert_dom_equal(
285 '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
286 )
287 assert_dom_equal(
288 '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
289 text_area("post", "body", "name" => "really!")
290 )
291 assert_dom_equal(
292 '<input name="i mean it" type="hidden" value="0" /><input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" />',
293 check_box("post", "secret", "name" => "i mean it")
294 )
295 assert_dom_equal text_field("post", "title", "name" => "dont guess"),
296 text_field("post", "title", :name => "dont guess")
297 assert_dom_equal text_area("post", "body", "name" => "really!"),
298 text_area("post", "body", :name => "really!")
299 assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
300 check_box("post", "secret", :name => "i mean it")
301 end
302
303 def test_explicit_id
304 assert_dom_equal(
305 '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
306 )
307 assert_dom_equal(
308 '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
309 text_area("post", "body", "id" => "really!")
310 )
311 assert_dom_equal(
312 '<input name="post[secret]" type="hidden" value="0" /><input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" />',
313 check_box("post", "secret", "id" => "i mean it")
314 )
315 assert_dom_equal text_field("post", "title", "id" => "dont guess"),
316 text_field("post", "title", :id => "dont guess")
317 assert_dom_equal text_area("post", "body", "id" => "really!"),
318 text_area("post", "body", :id => "really!")
319 assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
320 check_box("post", "secret", :id => "i mean it")
321 end
322
323 def test_auto_index
324 pid = @post.id
325 assert_dom_equal(
326 "<label for=\"post_#{pid}_title\">Title</label>",
327 label("post[]", "title")
328 )
329 assert_dom_equal(
330 "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
331 )
332 assert_dom_equal(
333 "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
334 text_area("post[]", "body")
335 )
336 assert_dom_equal(
337 "<input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" /><input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" />",
338 check_box("post[]", "secret")
339 )
340 assert_dom_equal(
341 "<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
342 radio_button("post[]", "title", "Hello World")
343 )
344 assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
345 radio_button("post[]", "title", "Goodbye World")
346 )
347 end
348
349 def test_form_for
350 form_for(:post, @post, :html => { :id => 'create-post' }) do |f|
351 concat f.label(:title)
352 concat f.text_field(:title)
353 concat f.text_area(:body)
354 concat f.check_box(:secret)
355 concat f.submit('Create post')
356 end
357
358 expected =
359 "<form action='http://www.example.com' id='create-post' method='post'>" +
360 "<label for='post_title'>Title</label>" +
361 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
362 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
363 "<input name='post[secret]' type='hidden' value='0' />" +
364 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
365 "<input name='commit' id='post_submit' type='submit' value='Create post' />" +
366 "</form>"
367
368 assert_dom_equal expected, output_buffer
369 end
370
371 def test_form_for_with_method
372 form_for(:post, @post, :html => { :id => 'create-post', :method => :put }) do |f|
373 concat f.text_field(:title)
374 concat f.text_area(:body)
375 concat f.check_box(:secret)
376 end
377
378 expected =
379 "<form action='http://www.example.com' id='create-post' method='post'>" +
380 "<div style='margin:0;padding:0'><input name='_method' type='hidden' value='put' /></div>" +
381 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
382 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
383 "<input name='post[secret]' type='hidden' value='0' />" +
384 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
385 "</form>"
386
387 assert_dom_equal expected, output_buffer
388 end
389
390 def test_form_for_without_object
391 form_for(:post, :html => { :id => 'create-post' }) do |f|
392 concat f.text_field(:title)
393 concat f.text_area(:body)
394 concat f.check_box(:secret)
395 end
396
397 expected =
398 "<form action='http://www.example.com' id='create-post' method='post'>" +
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]' type='hidden' value='0' />" +
402 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
403 "</form>"
404
405 assert_dom_equal expected, output_buffer
406 end
407
408 def test_form_for_with_index
409 form_for("post[]", @post) do |f|
410 concat f.label(:title)
411 concat f.text_field(:title)
412 concat f.text_area(:body)
413 concat f.check_box(:secret)
414 end
415
416 expected =
417 "<form action='http://www.example.com' method='post'>" +
418 "<label for=\"post_123_title\">Title</label>" +
419 "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
420 "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
421 "<input name='post[123][secret]' type='hidden' value='0' />" +
422 "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />" +
423 "</form>"
424
425 assert_dom_equal expected, output_buffer
426 end
427
428 def test_form_for_with_nil_index_option_override
429 form_for("post[]", @post, :index => nil) do |f|
430 concat f.text_field(:title)
431 concat f.text_area(:body)
432 concat f.check_box(:secret)
433 end
434
435 expected =
436 "<form action='http://www.example.com' method='post'>" +
437 "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
438 "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
439 "<input name='post[][secret]' type='hidden' value='0' />" +
440 "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />" +
441 "</form>"
442
443 assert_dom_equal expected, output_buffer
444 end
445
446 def test_nested_fields_for
447 form_for(:post, @post) do |f|
448 f.fields_for(:comment, @post) do |c|
449 concat c.text_field(:title)
450 end
451 end
452
453 expected = "<form action='http://www.example.com' method='post'>" +
454 "<input name='post[comment][title]' size='30' type='text' id='post_comment_title' value='Hello World' />" +
455 "</form>"
456
457 assert_dom_equal expected, output_buffer
458 end
459
460 def test_nested_fields_for_with_nested_collections
461 form_for('post[]', @post) do |f|
462 concat f.text_field(:title)
463 f.fields_for('comment[]', @comment) do |c|
464 concat c.text_field(:name)
465 end
466 end
467
468 expected = "<form action='http://www.example.com' method='post'>" +
469 "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
470 "<input name='post[123][comment][][name]' size='30' type='text' id='post_123_comment__name' value='new comment' />" +
471 "</form>"
472
473 assert_dom_equal expected, output_buffer
474 end
475
476 def test_nested_fields_for_with_index_and_parent_fields
477 form_for('post', @post, :index => 1) do |c|
478 concat c.text_field(:title)
479 c.fields_for('comment', @comment, :index => 1) do |r|
480 concat r.text_field(:name)
481 end
482 end
483
484 expected = "<form action='http://www.example.com' method='post'>" +
485 "<input name='post[1][title]' size='30' type='text' id='post_1_title' value='Hello World' />" +
486 "<input name='post[1][comment][1][name]' size='30' type='text' id='post_1_comment_1_name' value='new comment' />" +
487 "</form>"
488
489 assert_dom_equal expected, output_buffer
490 end
491
492 def test_form_for_with_index_and_nested_fields_for
493 form_for(:post, @post, :index => 1) do |f|
494 f.fields_for(:comment, @post) do |c|
495 concat c.text_field(:title)
496 end
497 end
498
499 expected = "<form action='http://www.example.com' method='post'>" +
500 "<input name='post[1][comment][title]' size='30' type='text' id='post_1_comment_title' value='Hello World' />" +
501 "</form>"
502
503 assert_dom_equal expected, output_buffer
504 end
505
506 def test_nested_fields_for_with_index_on_both
507 form_for(:post, @post, :index => 1) do |f|
508 f.fields_for(:comment, @post, :index => 5) do |c|
509 concat c.text_field(:title)
510 end
511 end
512
513 expected = "<form action='http://www.example.com' method='post'>" +
514 "<input name='post[1][comment][5][title]' size='30' type='text' id='post_1_comment_5_title' value='Hello World' />" +
515 "</form>"
516
517 assert_dom_equal expected, output_buffer
518 end
519
520 def test_nested_fields_for_with_auto_index
521 form_for("post[]", @post) do |f|
522 f.fields_for(:comment, @post) do |c|
523 concat c.text_field(:title)
524 end
525 end
526
527 expected = "<form action='http://www.example.com' method='post'>" +
528 "<input name='post[123][comment][title]' size='30' type='text' id='post_123_comment_title' value='Hello World' />" +
529 "</form>"
530
531 assert_dom_equal expected, output_buffer
532 end
533
534 def test_nested_fields_for_with_auto_index_on_both
535 form_for("post[]", @post) do |f|
536 f.fields_for("comment[]", @post) do |c|
537 concat c.text_field(:title)
538 end
539 end
540
541 expected = "<form action='http://www.example.com' method='post'>" +
542 "<input name='post[123][comment][123][title]' size='30' type='text' id='post_123_comment_123_title' value='Hello World' />" +
543 "</form>"
544
545 assert_dom_equal expected, output_buffer
546 end
547
548 def test_nested_fields_for_with_index_and_auto_index
549 form_for("post[]", @post) do |f|
550 f.fields_for(:comment, @post, :index => 5) do |c|
551 concat c.text_field(:title)
552 end
553 end
554
555 form_for(:post, @post, :index => 1) do |f|
556 f.fields_for("comment[]", @post) do |c|
557 concat c.text_field(:title)
558 end
559 end
560
561 expected = "<form action='http://www.example.com' method='post'>" +
562 "<input name='post[123][comment][5][title]' size='30' type='text' id='post_123_comment_5_title' value='Hello World' />" +
563 "</form>" +
564 "<form action='http://www.example.com' method='post'>" +
565 "<input name='post[1][comment][123][title]' size='30' type='text' id='post_1_comment_123_title' value='Hello World' />" +
566 "</form>"
567
568 assert_dom_equal expected, output_buffer
569 end
570
571 def test_nested_fields_for_with_a_new_record_on_a_nested_attributes_one_to_one_association
572 @post.author = Author.new
573
574 form_for(:post, @post) do |f|
575 concat f.text_field(:title)
576 f.fields_for(:author) do |af|
577 concat af.text_field(:name)
578 end
579 end
580
581 expected = '<form action="http://www.example.com" method="post">' +
582 '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
583 '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="new author" />' +
584 '</form>'
585
586 assert_dom_equal expected, output_buffer
587 end
588
589 def test_nested_fields_for_with_explicitly_passed_object_on_a_nested_attributes_one_to_one_association
590 form_for(:post, @post) do |f|
591 f.fields_for(:author, Author.new(123)) do |af|
592 assert_not_nil af.object
593 assert_equal 123, af.object.id
594 end
595 end
596 end
597
598 def test_nested_fields_for_with_an_existing_record_on_a_nested_attributes_one_to_one_association
599 @post.author = Author.new(321)
600
601 form_for(:post, @post) do |f|
602 concat f.text_field(:title)
603 f.fields_for(:author) do |af|
604 concat af.text_field(:name)
605 end
606 end
607
608 expected = '<form action="http://www.example.com" method="post">' +
609 '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
610 '<input id="post_author_attributes_id" name="post[author_attributes][id]" type="hidden" value="321" />' +
611 '<input id="post_author_attributes_name" name="post[author_attributes][name]" size="30" type="text" value="author #321" />' +
612 '</form>'
613
614 assert_dom_equal expected, output_buffer
615 end
616
617 def test_nested_fields_for_with_existing_records_on_a_nested_attributes_collection_association
618 @post.comments = Array.new(2) { |id| Comment.new(id + 1) }
619
620 form_for(:post, @post) do |f|
621 concat f.text_field(:title)
622 @post.comments.each do |comment|
623 f.fields_for(:comments, comment) do |cf|
624 concat cf.text_field(:name)
625 end
626 end
627 end
628
629 expected = '<form action="http://www.example.com" method="post">' +
630 '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
631 '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="1" />' +
632 '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #1" />' +
633 '<input id="post_comments_attributes_1_id" name="post[comments_attributes][1][id]" type="hidden" value="2" />' +
634 '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="comment #2" />' +
635 '</form>'
636
637 assert_dom_equal expected, output_buffer
638 end
639
640 def test_nested_fields_for_with_new_records_on_a_nested_attributes_collection_association
641 @post.comments = [Comment.new, Comment.new]
642
643 form_for(:post, @post) do |f|
644 concat f.text_field(:title)
645 @post.comments.each do |comment|
646 f.fields_for(:comments, comment) do |cf|
647 concat cf.text_field(:name)
648 end
649 end
650 end
651
652 expected = '<form action="http://www.example.com" method="post">' +
653 '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
654 '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="new comment" />' +
655 '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
656 '</form>'
657
658 assert_dom_equal expected, output_buffer
659 end
660
661 def test_nested_fields_for_with_existing_and_new_records_on_a_nested_attributes_collection_association
662 @post.comments = [Comment.new(321), Comment.new]
663
664 form_for(:post, @post) do |f|
665 concat f.text_field(:title)
666 @post.comments.each do |comment|
667 f.fields_for(:comments, comment) do |cf|
668 concat cf.text_field(:name)
669 end
670 end
671 end
672
673 expected = '<form action="http://www.example.com" method="post">' +
674 '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
675 '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
676 '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
677 '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
678 '</form>'
679
680 assert_dom_equal expected, output_buffer
681 end
682
683 def test_nested_fields_for_on_a_nested_attributes_collection_association_yields_only_builder
684 @post.comments = [Comment.new(321), Comment.new]
685 yielded_comments = []
686
687 form_for(:post, @post) do |f|
688 concat f.text_field(:title)
689 f.fields_for(:comments) do |cf|
690 concat cf.text_field(:name)
691 yielded_comments << cf.object
692 end
693 end
694
695 expected = '<form action="http://www.example.com" method="post">' +
696 '<input name="post[title]" size="30" type="text" id="post_title" value="Hello World" />' +
697 '<input id="post_comments_attributes_0_id" name="post[comments_attributes][0][id]" type="hidden" value="321" />' +
698 '<input id="post_comments_attributes_0_name" name="post[comments_attributes][0][name]" size="30" type="text" value="comment #321" />' +
699 '<input id="post_comments_attributes_1_name" name="post[comments_attributes][1][name]" size="30" type="text" value="new comment" />' +
700 '</form>'
701
702 assert_dom_equal expected, output_buffer
703 assert_equal yielded_comments, @post.comments
704 end
705
706 def test_nested_fields_for_with_child_index_option_override_on_a_nested_attributes_collection_association
707 @post.comments = []
708
709 form_for(:post, @post) do |f|
710 f.fields_for(:comments, Comment.new(321), :child_index => 'abc') do |cf|
711 concat cf.text_field(:name)
712 end
713 end
714
715 expected = '<form action="http://www.example.com" method="post">' +
716 '<input id="post_comments_attributes_abc_id" name="post[comments_attributes][abc][id]" type="hidden" value="321" />' +
717 '<input id="post_comments_attributes_abc_name" name="post[comments_attributes][abc][name]" size="30" type="text" value="comment #321" />' +
718 '</form>'
719
720 assert_dom_equal expected, output_buffer
721 end
722
723 def test_fields_for
724 fields_for(:post, @post) do |f|
725 concat f.text_field(:title)
726 concat f.text_area(:body)
727 concat f.check_box(:secret)
728 end
729
730 expected =
731 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
732 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
733 "<input name='post[secret]' type='hidden' value='0' />" +
734 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
735
736 assert_dom_equal expected, output_buffer
737 end
738
739 def test_fields_for_with_index
740 fields_for("post[]", @post) do |f|
741 concat f.text_field(:title)
742 concat f.text_area(:body)
743 concat f.check_box(:secret)
744 end
745
746 expected =
747 "<input name='post[123][title]' size='30' type='text' id='post_123_title' value='Hello World' />" +
748 "<textarea name='post[123][body]' id='post_123_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
749 "<input name='post[123][secret]' type='hidden' value='0' />" +
750 "<input name='post[123][secret]' checked='checked' type='checkbox' id='post_123_secret' value='1' />"
751
752 assert_dom_equal expected, output_buffer
753 end
754
755 def test_fields_for_with_nil_index_option_override
756 fields_for("post[]", @post, :index => nil) do |f|
757 concat f.text_field(:title)
758 concat f.text_area(:body)
759 concat f.check_box(:secret)
760 end
761
762 expected =
763 "<input name='post[][title]' size='30' type='text' id='post__title' value='Hello World' />" +
764 "<textarea name='post[][body]' id='post__body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
765 "<input name='post[][secret]' type='hidden' value='0' />" +
766 "<input name='post[][secret]' checked='checked' type='checkbox' id='post__secret' value='1' />"
767
768 assert_dom_equal expected, output_buffer
769 end
770
771 def test_fields_for_with_index_option_override
772 fields_for("post[]", @post, :index => "abc") do |f|
773 concat f.text_field(:title)
774 concat f.text_area(:body)
775 concat f.check_box(:secret)
776 end
777
778 expected =
779 "<input name='post[abc][title]' size='30' type='text' id='post_abc_title' value='Hello World' />" +
780 "<textarea name='post[abc][body]' id='post_abc_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
781 "<input name='post[abc][secret]' type='hidden' value='0' />" +
782 "<input name='post[abc][secret]' checked='checked' type='checkbox' id='post_abc_secret' value='1' />"
783
784 assert_dom_equal expected, output_buffer
785 end
786
787 def test_fields_for_without_object
788 fields_for(:post) do |f|
789 concat f.text_field(:title)
790 concat f.text_area(:body)
791 concat f.check_box(:secret)
792 end
793
794 expected =
795 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
796 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
797 "<input name='post[secret]' type='hidden' value='0' />" +
798 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
799
800 assert_dom_equal expected, output_buffer
801 end
802
803 def test_fields_for_with_only_object
804 fields_for(@post) do |f|
805 concat f.text_field(:title)
806 concat f.text_area(:body)
807 concat f.check_box(:secret)
808 end
809
810 expected =
811 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
812 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
813 "<input name='post[secret]' type='hidden' value='0' />" +
814 "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />"
815
816 assert_dom_equal expected, output_buffer
817 end
818
819 def test_fields_for_object_with_bracketed_name
820 fields_for("author[post]", @post) do |f|
821 concat f.label(:title)
822 concat f.text_field(:title)
823 end
824
825 assert_dom_equal "<label for=\"author_post_title\">Title</label>" +
826 "<input name='author[post][title]' size='30' type='text' id='author_post_title' value='Hello World' />",
827 output_buffer
828 end
829
830 def test_fields_for_object_with_bracketed_name_and_index
831 fields_for("author[post]", @post, :index => 1) do |f|
832 concat f.label(:title)
833 concat f.text_field(:title)
834 end
835
836 assert_dom_equal "<label for=\"author_post_1_title\">Title</label>" +
837 "<input name='author[post][1][title]' size='30' type='text' id='author_post_1_title' value='Hello World' />",
838 output_buffer
839 end
840
841 def test_form_builder_does_not_have_form_for_method
842 assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
843 end
844
845 def test_form_for_and_fields_for
846 form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
847 concat post_form.text_field(:title)
848 concat post_form.text_area(:body)
849
850 fields_for(:parent_post, @post) do |parent_fields|
851 concat parent_fields.check_box(:secret)
852 end
853 end
854
855 expected =
856 "<form action='http://www.example.com' id='create-post' method='post'>" +
857 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
858 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
859 "<input name='parent_post[secret]' type='hidden' value='0' />" +
860 "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
861 "</form>"
862
863 assert_dom_equal expected, output_buffer
864 end
865
866 def test_form_for_and_fields_for_with_object
867 form_for(:post, @post, :html => { :id => 'create-post' }) do |post_form|
868 concat post_form.text_field(:title)
869 concat post_form.text_area(:body)
870
871 post_form.fields_for(@comment) do |comment_fields|
872 concat comment_fields.text_field(:name)
873 end
874 end
875
876 expected =
877 "<form action='http://www.example.com' id='create-post' method='post'>" +
878 "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
879 "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
880 "<input name='post[comment][name]' type='text' id='post_comment_name' value='new comment' size='30' />" +
881 "</form>"
882
883 assert_dom_equal expected, output_buffer
884 end
885
886 class LabelledFormBuilder < ActionView::Helpers::FormBuilder
887 (field_helpers - %w(hidden_field)).each do |selector|
888 src = <<-END_SRC
889 def #{selector}(field, *args, &proc)
890 "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
891 end
892 END_SRC
893 class_eval src, __FILE__, __LINE__
894 end
895 end
896
897 def test_form_for_with_labelled_builder
898 form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
899 concat f.text_field(:title)
900 concat f.text_area(:body)
901 concat f.check_box(:secret)
902 end
903
904 expected =
905 "<form action='http://www.example.com' method='post'>" +
906 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
907 "<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/>" +
908 "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
909 "</form>"
910
911 assert_dom_equal expected, output_buffer
912 end
913
914 def test_default_form_builder
915 old_default_form_builder, ActionView::Base.default_form_builder =
916 ActionView::Base.default_form_builder, LabelledFormBuilder
917
918 form_for(:post, @post) do |f|
919 concat f.text_field(:title)
920 concat f.text_area(:body)
921 concat f.check_box(:secret)
922 end
923
924 expected =
925 "<form action='http://www.example.com' method='post'>" +
926 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
927 "<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/>" +
928 "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
929 "</form>"
930
931 assert_dom_equal expected, output_buffer
932 ensure
933 ActionView::Base.default_form_builder = old_default_form_builder
934 end
935
936 def test_default_form_builder_with_active_record_helpers
937 form_for(:post, @post) do |f|
938 concat f.error_message_on('author_name')
939 concat f.error_messages
940 end
941
942 expected = %(<form action='http://www.example.com' method='post'>) +
943 %(<div class='formError'>can't be empty</div>) +
944 %(<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>) +
945 %(</form>)
946
947 assert_dom_equal expected, output_buffer
948
949 end
950
951 def test_default_form_builder_no_instance_variable
952 post = @post
953 @post = nil
954
955 form_for(:post, post) do |f|
956 concat f.error_message_on('author_name')
957 concat f.error_messages
958 end
959
960 expected = %(<form action='http://www.example.com' method='post'>) +
961 %(<div class='formError'>can't be empty</div>) +
962 %(<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>) +
963 %(</form>)
964
965 assert_dom_equal expected, output_buffer
966
967 end
968
969 # Perhaps this test should be moved to prototype helper tests.
970 def test_remote_form_for_with_labelled_builder
971 self.extend ActionView::Helpers::PrototypeHelper
972
973 remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
974 concat f.text_field(:title)
975 concat f.text_area(:body)
976 concat f.check_box(:secret)
977 end
978
979 expected =
980 %(<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">) +
981 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
982 "<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/>" +
983 "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>" +
984 "</form>"
985
986 assert_dom_equal expected, output_buffer
987 end
988
989 def test_fields_for_with_labelled_builder
990 fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
991 concat f.text_field(:title)
992 concat f.text_area(:body)
993 concat f.check_box(:secret)
994 end
995
996 expected =
997 "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
998 "<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/>" +
999 "<label for='secret'>Secret:</label> <input name='post[secret]' type='hidden' value='0' /><input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' /><br/>"
1000
1001 assert_dom_equal expected, output_buffer
1002 end
1003
1004 def test_form_for_with_labelled_builder_with_nested_fields_for_without_options_hash
1005 klass = nil
1006
1007 form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
1008 f.fields_for(:comments, Comment.new) do |nested_fields|
1009 klass = nested_fields.class
1010 ''
1011 end
1012 end
1013
1014 assert_equal LabelledFormBuilder, klass
1015 end
1016
1017 def test_form_for_with_labelled_builder_with_nested_fields_for_with_options_hash
1018 klass = nil
1019
1020 form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
1021 f.fields_for(:comments, Comment.new, :index => 'foo') do |nested_fields|
1022 klass = nested_fields.class
1023 ''
1024 end
1025 end
1026
1027 assert_equal LabelledFormBuilder, klass
1028 end
1029
1030 class LabelledFormBuilderSubclass < LabelledFormBuilder; end
1031
1032 def test_form_for_with_labelled_builder_with_nested_fields_for_with_custom_builder
1033 klass = nil
1034
1035 form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
1036 f.fields_for(:comments, Comment.new, :builder => LabelledFormBuilderSubclass) do |nested_fields|
1037 klass = nested_fields.class
1038 ''
1039 end
1040 end
1041
1042 assert_equal LabelledFormBuilderSubclass, klass
1043 end
1044
1045 def test_form_for_with_html_options_adds_options_to_form_tag
1046 form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
1047 expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
1048
1049 assert_dom_equal expected, output_buffer
1050 end
1051
1052 def test_form_for_with_string_url_option
1053 form_for(:post, @post, :url => 'http://www.otherdomain.com') do |f| end
1054
1055 assert_equal '<form action="http://www.otherdomain.com" method="post"></form>', output_buffer
1056 end
1057
1058 def test_form_for_with_hash_url_option
1059 form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end
1060
1061 assert_equal 'controller', @controller.url_for_options[:controller]
1062 assert_equal 'action', @controller.url_for_options[:action]
1063 end
1064
1065 def test_form_for_with_record_url_option
1066 form_for(:post, @post, :url => @post) do |f| end
1067
1068 expected = "<form action=\"/posts/123\" method=\"post\"></form>"
1069 assert_equal expected, output_buffer
1070 end
1071
1072 def test_form_for_with_existing_object
1073 form_for(@post) do |f| end
1074
1075 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>"
1076 assert_equal expected, output_buffer
1077 end
1078
1079 def test_form_for_with_new_object
1080 post = Post.new
1081 post.new_record = true
1082 def post.id() nil end
1083
1084 form_for(post) do |f| end
1085
1086 expected = "<form action=\"/posts\" class=\"new_post\" id=\"new_post\" method=\"post\"></form>"
1087 assert_equal expected, output_buffer
1088 end
1089
1090 def test_form_for_with_existing_object_in_list
1091 @post.new_record = false
1092 @comment.save
1093
1094 form_for([@post, @comment]) {}
1095
1096 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>)
1097 assert_dom_equal expected, output_buffer
1098 end
1099
1100 def test_form_for_with_new_object_in_list
1101 @post.new_record = false
1102
1103 form_for([@post, @comment]) {}
1104
1105 expected = %(<form action="#{comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
1106 assert_dom_equal expected, output_buffer
1107 end
1108
1109 def test_form_for_with_existing_object_and_namespace_in_list
1110 @post.new_record = false
1111 @comment.save
1112
1113 form_for([:admin, @post, @comment]) {}
1114
1115 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>)
1116 assert_dom_equal expected, output_buffer
1117 end
1118
1119 def test_form_for_with_new_object_and_namespace_in_list
1120 @post.new_record = false
1121
1122 form_for([:admin, @post, @comment]) {}
1123
1124 expected = %(<form action="#{admin_comments_path(@post)}" class="new_comment" id="new_comment" method="post"></form>)
1125 assert_dom_equal expected, output_buffer
1126 end
1127
1128 def test_form_for_with_existing_object_and_custom_url
1129 form_for(@post, :url => "/super_posts") do |f| end
1130
1131 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>"
1132 assert_equal expected, output_buffer
1133 end
1134
1135 def test_remote_form_for_with_html_options_adds_options_to_form_tag
1136 self.extend ActionView::Helpers::PrototypeHelper
1137
1138 remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
1139 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>"
1140
1141 assert_dom_equal expected, output_buffer
1142 end
1143
1144 protected
1145 def comments_path(post)
1146 "/posts/#{post.id}/comments"
1147 end
1148 alias_method :post_comments_path, :comments_path
1149
1150 def comment_path(post, comment)
1151 "/posts/#{post.id}/comments/#{comment.id}"
1152 end
1153 alias_method :post_comment_path, :comment_path
1154
1155 def admin_comments_path(post)
1156 "/admin/posts/#{post.id}/comments"
1157 end
1158 alias_method :admin_post_comments_path, :admin_comments_path
1159
1160 def admin_comment_path(post, comment)
1161 "/admin/posts/#{post.id}/comments/#{comment.id}"
1162 end
1163 alias_method :admin_post_comment_path, :admin_comment_path
1164
1165 def posts_path
1166 "/posts"
1167 end
1168
1169 def post_path(post)
1170 "/posts/#{post.id}"
1171 end
1172
1173 def protect_against_forgery?
1174 false
1175 end
1176 end