form_helper_test.rb 15.3 KB
Newer Older
1
require File.dirname(__FILE__) + '/../abstract_unit'
D
Initial  
David Heinemeier Hansson 已提交
2 3 4

class FormHelperTest < Test::Unit::TestCase
  include ActionView::Helpers::FormHelper
5 6 7 8
  include ActionView::Helpers::FormTagHelper
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::TextHelper
D
Initial  
David Heinemeier Hansson 已提交
9

10 11 12 13 14 15 16
  silence_warnings do
    Post = Struct.new("Post", :title, :author_name, :body, :secret, :written_on, :cost)
    Post.class_eval do
      alias_method :title_before_type_cast, :title unless respond_to?(:title_before_type_cast)
      alias_method :body_before_type_cast, :body unless respond_to?(:body_before_type_cast)
      alias_method :author_name_before_type_cast, :author_name unless respond_to?(:author_name_before_type_cast)
    end
17
  end
D
Initial  
David Heinemeier Hansson 已提交
18 19

  def setup
20
    @post = Post.new
D
Initial  
David Heinemeier Hansson 已提交
21 22
    def @post.errors() Class.new{ def on(field) field == "author_name" end }.new end

23
    def @post.id; 123; end
24
    def @post.id_before_type_cast; 123; end
25

D
Initial  
David Heinemeier Hansson 已提交
26 27 28 29 30
    @post.title       = "Hello World"
    @post.author_name = ""
    @post.body        = "Back to the hill and over it again!"
    @post.secret      = 1
    @post.written_on  = Date.new(2004, 6, 15)
31 32 33 34 35 36 37

    @controller = Class.new do
      def url_for(options, *parameters_for_method_reference)
        "http://www.example.com"
      end
    end
    @controller = @controller.new
D
Initial  
David Heinemeier Hansson 已提交
38 39 40
  end

  def test_text_field
41
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
42 43
      '<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title")
    )
44
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
45 46
      '<input id="post_title" name="post[title]" size="30" type="password" value="Hello World" />', password_field("post", "title")
    )
47
    assert_dom_equal(
48
      '<input id="person_name" name="person[name]" size="30" type="password" />', password_field("person", "name")
D
Initial  
David Heinemeier Hansson 已提交
49 50 51 52 53
    )
  end

  def test_text_field_with_escapes
    @post.title = "<b>Hello World</b>"
54
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
55 56 57 58 59
      '<input id="post_title" name="post[title]" size="30" type="text" value="&lt;b&gt;Hello World&lt;/b&gt;" />', text_field("post", "title")
    )
  end

  def test_text_field_with_options
60
    expected = '<input id="post_title" name="post[title]" size="35" type="text" value="Hello World" />'
61 62
    assert_dom_equal expected, text_field("post", "title", "size" => 35)
    assert_dom_equal expected, text_field("post", "title", :size => 35)
D
Initial  
David Heinemeier Hansson 已提交
63
  end
64

D
Initial  
David Heinemeier Hansson 已提交
65
  def test_text_field_assuming_size
66
    expected = '<input id="post_title" maxlength="35" name="post[title]" size="35" type="text" value="Hello World" />'
67 68
    assert_dom_equal expected, text_field("post", "title", "maxlength" => 35)
    assert_dom_equal expected, text_field("post", "title", :maxlength => 35)
D
Initial  
David Heinemeier Hansson 已提交
69
  end
70

71 72 73 74 75 76 77
  def test_text_field_doesnt_change_param_values
    object_name = 'post[]'
    expected = '<input id="post_123_title" name="post[123][title]" size="30" type="text" value="Hello World" />'
    assert_equal expected, text_field(object_name, "title")
    assert_equal object_name, "post[]"
  end

D
Initial  
David Heinemeier Hansson 已提交
78
  def test_check_box
79
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
80 81 82 83
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
      check_box("post", "secret")
    )
    @post.secret = 0
84
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
85
      '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
86
      check_box("post", "secret")
D
Initial  
David Heinemeier Hansson 已提交
87
    )
88
    assert_dom_equal(
89
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
90 91
      check_box("post", "secret" ,{"checked"=>"checked"})
    )
D
Initial  
David Heinemeier Hansson 已提交
92
    @post.secret = true
93
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
94 95 96 97
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
      check_box("post", "secret")
    )
  end
98

99 100
  def test_check_box_with_explicit_checked_and_unchecked_values
    @post.secret = "on"
101
    assert_dom_equal(
102 103 104 105
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="on" /><input name="post[secret]" type="hidden" value="off" />',
      check_box("post", "secret", {}, "on", "off")
    )
  end
106 107

  def test_radio_button
108
    assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
109
      radio_button("post", "title", "Hello World")
110
    )
111
    assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
112
      radio_button("post", "title", "Goodbye World")
113 114
    )
  end
115

116 117 118 119 120 121
  def test_radio_button_is_checked_with_integers
    assert_dom_equal('<input checked="checked" id="post_secret_1" name="post[secret]" type="radio" value="1" />',
      radio_button("post", "secret", "1")
   )
  end

D
Initial  
David Heinemeier Hansson 已提交
122
  def test_text_area
123
    assert_dom_equal(
124
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
125 126 127
      text_area("post", "body")
    )
  end
128

D
Initial  
David Heinemeier Hansson 已提交
129 130
  def test_text_area_with_escapes
    @post.body        = "Back to <i>the</i> hill and over it again!"
131
    assert_dom_equal(
132
      '<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>',
D
Initial  
David Heinemeier Hansson 已提交
133 134 135
      text_area("post", "body")
    )
  end
136 137 138 139 140 141 142 143
  
  def test_text_area_with_alternate_value
    assert_dom_equal(
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Testing alternate values.</textarea>',
      text_area("post", "body", :value => 'Testing alternate values.')
    )
  end
  
D
Initial  
David Heinemeier Hansson 已提交
144
  def test_date_selects
145
    assert_dom_equal(
146
      '<textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
147 148 149
      text_area("post", "body")
    )
  end
150

D
Initial  
David Heinemeier Hansson 已提交
151
  def test_explicit_name
152
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
153
      '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
154
    )
155
    assert_dom_equal(
156
      '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
157 158
      text_area("post", "body", "name" => "really!")
    )
159
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
160 161 162
      '<input checked="checked" id="post_secret" name="i mean it" type="checkbox" value="1" /><input name="i mean it" type="hidden" value="0" />',
      check_box("post", "secret", "name" => "i mean it")
    )
163
    assert_dom_equal text_field("post", "title", "name" => "dont guess"),
164
                 text_field("post", "title", :name => "dont guess")
165
    assert_dom_equal text_area("post", "body", "name" => "really!"),
166
                 text_area("post", "body", :name => "really!")
167
    assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
168
                 check_box("post", "secret", :name => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
169
  end
170

D
Initial  
David Heinemeier Hansson 已提交
171
  def test_explicit_id
172
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
173
      '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
174
    )
175
    assert_dom_equal(
176
      '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
177 178
      text_area("post", "body", "id" => "really!")
    )
179
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
180 181 182
      '<input checked="checked" id="i mean it" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
      check_box("post", "secret", "id" => "i mean it")
    )
183
    assert_dom_equal text_field("post", "title", "id" => "dont guess"),
184
                 text_field("post", "title", :id => "dont guess")
185
    assert_dom_equal text_area("post", "body", "id" => "really!"),
186
                 text_area("post", "body", :id => "really!")
187
    assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
188
                 check_box("post", "secret", :id => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
189
  end
190 191 192

  def test_auto_index
    pid = @post.id
193
    assert_dom_equal(
194 195
      "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
    )
196
    assert_dom_equal(
197
      "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
198 199
      text_area("post[]", "body")
    )
200
    assert_dom_equal(
201 202 203
      "<input checked=\"checked\" id=\"post_#{pid}_secret\" name=\"post[#{pid}][secret]\" type=\"checkbox\" value=\"1\" /><input name=\"post[#{pid}][secret]\" type=\"hidden\" value=\"0\" />",
      check_box("post[]", "secret")
    )
204
   assert_dom_equal(
205
"<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
206 207
      radio_button("post[]", "title", "Hello World")
    )
208
    assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
209 210 211
      radio_button("post[]", "title", "Goodbye World")
    )
  end
212 213 214 215

  def test_form_for
    _erbout = ''

216
    form_for(:post, @post) do |f|
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
      _erbout.concat f.text_field(:title)
      _erbout.concat f.text_area(:body)
      _erbout.concat f.check_box(:secret)
    end

    expected = 
      "<form action='http://www.example.com' method='post'>" +
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
      "<input name='post[secret]' type='hidden' value='0' />" +
      "</form>"

    assert_dom_equal expected, _erbout
  end

  def test_fields_for
    _erbout = ''

236
    fields_for(:post, @post) do |f|
237 238 239 240 241 242 243 244 245 246 247 248 249 250
      _erbout.concat f.text_field(:title)
      _erbout.concat f.text_area(:body)
      _erbout.concat f.check_box(:secret)
    end

    expected = 
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
      "<input name='post[secret]' type='hidden' value='0' />"

    assert_dom_equal expected, _erbout
  end
  
251 252 253 254
  def test_form_builder_does_not_have_form_for_method
    assert ! ActionView::Helpers::FormBuilder.instance_methods.include?('form_for')
  end
  
255 256 257
  def test_form_for_and_fields_for
    _erbout = ''

258
    form_for(:post, @post) do |post_form|
259 260 261
      _erbout.concat post_form.text_field(:title)
      _erbout.concat post_form.text_area(:body)

262
      fields_for(:parent_post, @post) do |parent_fields|
263 264 265 266 267 268 269 270 271 272 273 274 275 276
        _erbout.concat parent_fields.check_box(:secret)
      end
    end

    expected = 
      "<form action='http://www.example.com' method='post'>" +
      "<input name='post[title]' size='30' type='text' id='post_title' value='Hello World' />" +
      "<textarea name='post[body]' id='post_body' rows='20' cols='40'>Back to the hill and over it again!</textarea>" +
      "<input name='parent_post[secret]' checked='checked' type='checkbox' id='parent_post_secret' value='1' />" +
      "<input name='parent_post[secret]' type='hidden' value='0' />" +
      "</form>"

    assert_dom_equal expected, _erbout
  end
277
  
278
  class LabelledFormBuilder < ActionView::Helpers::FormBuilder
279 280 281 282 283 284 285 286 287 288 289 290 291
    (field_helpers - %w(hidden_field)).each do |selector|
      src = <<-END_SRC
        def #{selector}(field, *args, &proc)
          "<label for='\#{field}'>\#{field.to_s.humanize}:</label> " + super + "<br/>"
        end
      END_SRC
      class_eval src, __FILE__, __LINE__
    end
  end
  
  def test_form_for_with_labelled_builder
    _erbout = ''

292
    form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      _erbout.concat f.text_field(:title)
      _erbout.concat f.text_area(:body)
      _erbout.concat f.check_box(:secret)
    end

    expected = 
      "<form action='http://www.example.com' method='post'>" +
      "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
      "<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/>" +
      "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
      "<input name='post[secret]' type='hidden' value='0' /><br/>" +
      "</form>"

    assert_dom_equal expected, _erbout
  end
  
  # Perhaps this test should be moved to prototype helper tests.
  def test_remote_form_for_with_labelled_builder
    self.extend ActionView::Helpers::PrototypeHelper
     _erbout = ''

314
     remote_form_for(:post, @post, :builder => LabelledFormBuilder) do |f|
315 316 317 318 319 320
       _erbout.concat f.text_field(:title)
       _erbout.concat f.text_area(:body)
       _erbout.concat f.check_box(:secret)
     end

     expected = 
321
       %(<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">) +
322 323 324 325 326 327 328 329 330 331 332
       "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
       "<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/>" +
       "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
       "<input name='post[secret]' type='hidden' value='0' /><br/>" +
       "</form>"

     assert_dom_equal expected, _erbout
  end
   
  def test_fields_for_with_labelled_builder
    _erbout = ''
333 334
    
    fields_for(:post, @post, :builder => LabelledFormBuilder) do |f|
335 336 337 338
      _erbout.concat f.text_field(:title)
      _erbout.concat f.text_area(:body)
      _erbout.concat f.check_box(:secret)
    end
339
    
340 341 342 343 344
    expected = 
      "<label for='title'>Title:</label> <input name='post[title]' size='30' type='text' id='post_title' value='Hello World' /><br/>" +
      "<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/>" +
      "<label for='secret'>Secret:</label> <input name='post[secret]' checked='checked' type='checkbox' id='post_secret' value='1' />" +
      "<input name='post[secret]' type='hidden' value='0' /><br/>"
345
    
346 347
    assert_dom_equal expected, _erbout
  end
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
  
  def test_form_for_with_html_options_adds_options_to_form_tag
    _erbout = ''
    
    form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
    expected = "<form action=\"http://www.example.com\" class=\"some_class\" id=\"some_form\" method=\"post\"></form>"
    
    assert_dom_equal expected, _erbout
  end
  
  def test_remote_form_for_with_html_options_adds_options_to_form_tag
    self.extend ActionView::Helpers::PrototypeHelper
    _erbout = ''
    
    remote_form_for(:post, @post, :html => {:id => 'some_form', :class => 'some_class'}) do |f| end
    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>"
    
    assert_dom_equal expected, _erbout
  end
367
end