form_helper_test.rb 10.5 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

D
Initial  
David Heinemeier Hansson 已提交
71
  def test_check_box
72
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
73 74 75 76
      '<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
77
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
78
      '<input id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
79
      check_box("post", "secret")
D
Initial  
David Heinemeier Hansson 已提交
80
    )
81
    assert_dom_equal(
82
      '<input checked="checked" id="post_secret" name="post[secret]" type="checkbox" value="1" /><input name="post[secret]" type="hidden" value="0" />',
83 84
      check_box("post", "secret" ,{"checked"=>"checked"})
    )
D
Initial  
David Heinemeier Hansson 已提交
85
    @post.secret = true
86
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
87 88 89 90
      '<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
91

92 93
  def test_check_box_with_explicit_checked_and_unchecked_values
    @post.secret = "on"
94
    assert_dom_equal(
95 96 97 98
      '<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
99 100

  def test_radio_button
101
    assert_dom_equal('<input checked="checked" id="post_title_hello_world" name="post[title]" type="radio" value="Hello World" />',
102
      radio_button("post", "title", "Hello World")
103
    )
104
    assert_dom_equal('<input id="post_title_goodbye_world" name="post[title]" type="radio" value="Goodbye World" />',
105
      radio_button("post", "title", "Goodbye World")
106 107
    )
  end
108

109 110 111 112 113 114
  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 已提交
115
  def test_text_area
116
    assert_dom_equal(
117
      '<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 已提交
118 119 120
      text_area("post", "body")
    )
  end
121

D
Initial  
David Heinemeier Hansson 已提交
122 123
  def test_text_area_with_escapes
    @post.body        = "Back to <i>the</i> hill and over it again!"
124
    assert_dom_equal(
125
      '<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 已提交
126 127 128 129 130
      text_area("post", "body")
    )
  end

  def test_date_selects
131
    assert_dom_equal(
132
      '<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 已提交
133 134 135
      text_area("post", "body")
    )
  end
136

D
Initial  
David Heinemeier Hansson 已提交
137
  def test_explicit_name
138
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
139
      '<input id="post_title" name="dont guess" size="30" type="text" value="Hello World" />', text_field("post", "title", "name" => "dont guess")
140
    )
141
    assert_dom_equal(
142
      '<textarea cols="40" id="post_body" name="really!" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
143 144
      text_area("post", "body", "name" => "really!")
    )
145
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
146 147 148
      '<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")
    )
149
    assert_dom_equal text_field("post", "title", "name" => "dont guess"),
150
                 text_field("post", "title", :name => "dont guess")
151
    assert_dom_equal text_area("post", "body", "name" => "really!"),
152
                 text_area("post", "body", :name => "really!")
153
    assert_dom_equal check_box("post", "secret", "name" => "i mean it"),
154
                 check_box("post", "secret", :name => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
155
  end
156

D
Initial  
David Heinemeier Hansson 已提交
157
  def test_explicit_id
158
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
159
      '<input id="dont guess" name="post[title]" size="30" type="text" value="Hello World" />', text_field("post", "title", "id" => "dont guess")
160
    )
161
    assert_dom_equal(
162
      '<textarea cols="40" id="really!" name="post[body]" rows="20">Back to the hill and over it again!</textarea>',
D
Initial  
David Heinemeier Hansson 已提交
163 164
      text_area("post", "body", "id" => "really!")
    )
165
    assert_dom_equal(
D
Initial  
David Heinemeier Hansson 已提交
166 167 168
      '<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")
    )
169
    assert_dom_equal text_field("post", "title", "id" => "dont guess"),
170
                 text_field("post", "title", :id => "dont guess")
171
    assert_dom_equal text_area("post", "body", "id" => "really!"),
172
                 text_area("post", "body", :id => "really!")
173
    assert_dom_equal check_box("post", "secret", "id" => "i mean it"),
174
                 check_box("post", "secret", :id => "i mean it")
D
Initial  
David Heinemeier Hansson 已提交
175
  end
176 177 178

  def test_auto_index
    pid = @post.id
179
    assert_dom_equal(
180 181
      "<input id=\"post_#{pid}_title\" name=\"post[#{pid}][title]\" size=\"30\" type=\"text\" value=\"Hello World\" />", text_field("post[]","title")
    )
182
    assert_dom_equal(
183
      "<textarea cols=\"40\" id=\"post_#{pid}_body\" name=\"post[#{pid}][body]\" rows=\"20\">Back to the hill and over it again!</textarea>",
184 185
      text_area("post[]", "body")
    )
186
    assert_dom_equal(
187 188 189
      "<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")
    )
190
   assert_dom_equal(
191
"<input checked=\"checked\" id=\"post_#{pid}_title_hello_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Hello World\" />",
192 193
      radio_button("post[]", "title", "Hello World")
    )
194
    assert_dom_equal("<input id=\"post_#{pid}_title_goodbye_world\" name=\"post[#{pid}][title]\" type=\"radio\" value=\"Goodbye World\" />",
195 196 197
      radio_button("post[]", "title", "Goodbye World")
    )
  end
198 199 200 201

  def test_form_for
    _erbout = ''

202
    form_for(:post, @post) do |f|
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
      _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 = ''

222
    fields_for(:post, @post) do |f|
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
      _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
  
  def test_form_for_and_fields_for
    _erbout = ''

240
    form_for(:post, @post) do |post_form|
241 242 243
      _erbout.concat post_form.text_field(:title)
      _erbout.concat post_form.text_area(:body)

244
      fields_for(:parent_post, @post) do |parent_fields|
245 246 247 248 249 250 251 252 253 254 255 256 257 258
        _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
259
end