]> git.openstreetmap.org Git - rails.git/blob - vendor/plugins/dynamic_form/test/dynamic_form_test.rb
Update rails translations
[rails.git] / vendor / plugins / dynamic_form / test / dynamic_form_test.rb
1 require 'test_helper'
2 require 'action_view/template/handlers/erb'
3
4 class DynamicFormTest < ActionView::TestCase
5   tests ActionView::Helpers::DynamicForm
6
7   def form_for(*)
8     @output_buffer = super
9   end
10
11   silence_warnings do
12     class Post < Struct.new(:title, :author_name, :body, :secret, :written_on)
13       extend ActiveModel::Naming
14       include ActiveModel::Conversion
15     end
16
17     class User < Struct.new(:email)
18       extend ActiveModel::Naming
19       include ActiveModel::Conversion
20     end
21
22     class Column < Struct.new(:type, :name, :human_name)
23       extend ActiveModel::Naming
24       include ActiveModel::Conversion
25     end
26   end
27
28   class DirtyPost
29     class Errors
30       def empty?
31         false
32       end
33
34       def count
35         1
36       end
37
38       def full_messages
39         ["Author name can't be <em>empty</em>"]
40       end
41
42       def [](field)
43         ["can't be <em>empty</em>"]
44       end
45     end
46
47     def errors
48       Errors.new
49     end
50   end
51
52   def setup_post
53     @post = Post.new
54     def @post.errors
55       Class.new {
56         def [](field)
57           case field.to_s
58           when "author_name"
59             ["can't be empty"]
60           when "body"
61             ['foo']
62           else
63             []
64           end
65         end
66         def empty?() false end
67         def count() 1 end
68         def full_messages() [ "Author name can't be empty" ] end
69       }.new
70     end
71
72     def @post.persisted?() false end
73     def @post.to_param() nil end
74
75     def @post.column_for_attribute(attr_name)
76       Post.content_columns.select { |column| column.name == attr_name }.first
77     end
78
79     silence_warnings do
80       def Post.content_columns() [ Column.new(:string, "title", "Title"), Column.new(:text, "body", "Body") ] end
81     end
82
83     @post.title       = "Hello World"
84     @post.author_name = ""
85     @post.body        = "Back to the hill and over it again!"
86     @post.secret = 1
87     @post.written_on  = Date.new(2004, 6, 15)
88   end
89
90   def setup_user
91     @user = User.new
92     def @user.errors
93       Class.new {
94         def [](field) field == "email" ? ['nonempty'] : [] end
95         def empty?() false end
96         def count() 1 end
97         def full_messages() [ "User email can't be empty" ] end
98       }.new
99     end
100
101     def @user.new_record?() true end
102     def @user.to_param() nil end
103
104     def @user.column_for_attribute(attr_name)
105       User.content_columns.select { |column| column.name == attr_name }.first
106     end
107
108     silence_warnings do
109       def User.content_columns() [ Column.new(:string, "email", "Email") ] end
110     end
111
112     @user.email = ""
113   end
114
115   def protect_against_forgery?
116     @protect_against_forgery ? true : false
117   end
118   attr_accessor :request_forgery_protection_token, :form_authenticity_token
119
120   def setup
121     super
122     setup_post
123     setup_user
124
125     @response = ActionController::TestResponse.new
126   end
127
128   def url_for(options)
129     options = options.symbolize_keys
130     [options[:action], options[:id].to_param].compact.join('/')
131   end
132
133   def test_generic_input_tag
134     assert_dom_equal(
135       %(<input id="post_title" name="post[title]" size="30" type="text" value="Hello World" />), input("post", "title")
136     )
137   end
138
139   def test_text_area_with_errors
140     assert_dom_equal(
141       %(<div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div>),
142       text_area("post", "body")
143     )
144   end
145
146   def test_text_field_with_errors
147     assert_dom_equal(
148       %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /></div>),
149       text_field("post", "author_name")
150     )
151   end
152
153   def test_field_error_proc
154     old_proc = ActionView::Base.field_error_proc
155     ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
156       %(<div class=\"fieldWithErrors\">#{html_tag} <span class="error">#{[instance.error_message].join(', ')}</span></div>).html_safe
157     end
158
159     assert_dom_equal(
160       %(<div class="fieldWithErrors"><input id="post_author_name" name="post[author_name]" size="30" type="text" value="" /> <span class="error">can't be empty</span></div>),
161       text_field("post", "author_name")
162     )
163   ensure
164     ActionView::Base.field_error_proc = old_proc if old_proc
165   end
166
167   def test_form_with_string
168     assert_dom_equal(
169       %(<form action="create" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
170       form("post")
171     )
172
173     silence_warnings do
174       class << @post
175         def persisted?() true end
176         def to_param() id end
177         def id() 1 end
178       end
179     end
180
181     assert_dom_equal(
182       %(<form action="update/1" method="post"><input id="post_id" name="post[id]" type="hidden" value="1" /><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Update" /></form>),
183       form("post")
184     )
185   end
186
187   def test_form_with_protect_against_forgery
188     @protect_against_forgery = true
189     @request_forgery_protection_token = 'authenticity_token'
190     @form_authenticity_token = '123'
191     assert_dom_equal(
192       %(<form action="create" method="post"><div style='margin:0;padding:0;display:inline'><input type='hidden' name='authenticity_token' value='123' /></div><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
193       form("post")
194     )
195   end
196
197   def test_form_with_method_option
198     assert_dom_equal(
199       %(<form action="create" method="get"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
200       form("post", :method=>'get')
201     )
202   end
203
204   def test_form_with_action_option
205     output_buffer << form("post", :action => "sign")
206     assert_select "form[action=sign]" do |form|
207       assert_select "input[type=submit][value=Sign]"
208     end
209   end
210
211   def test_form_with_date
212     silence_warnings do
213       def Post.content_columns() [ Column.new(:date, "written_on", "Written on") ] end
214     end
215
216     assert_dom_equal(
217       %(<form action="create" method="post"><p><label for="post_written_on">Written on</label><br /><select id="post_written_on_1i" name="post[written_on(1i)]">\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n</select>\n<select id="post_written_on_2i" name="post[written_on(2i)]">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n</select>\n<select id="post_written_on_3i" name="post[written_on(3i)]">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n</select>\n</p><input name="commit" type="submit" value="Create" /></form>),
218       form("post")
219     )
220   end
221
222   def test_form_with_datetime
223     silence_warnings do
224       def Post.content_columns() [ Column.new(:datetime, "written_on", "Written on") ] end
225     end
226     @post.written_on  = Time.gm(2004, 6, 15, 16, 30)
227
228     assert_dom_equal(
229       %(<form action="create" method="post"><p><label for="post_written_on">Written on</label><br /><select id="post_written_on_1i" name="post[written_on(1i)]">\n<option value="1999">1999</option>\n<option value="2000">2000</option>\n<option value="2001">2001</option>\n<option value="2002">2002</option>\n<option value="2003">2003</option>\n<option value="2004" selected="selected">2004</option>\n<option value="2005">2005</option>\n<option value="2006">2006</option>\n<option value="2007">2007</option>\n<option value="2008">2008</option>\n<option value="2009">2009</option>\n</select>\n<select id="post_written_on_2i" name="post[written_on(2i)]">\n<option value="1">January</option>\n<option value="2">February</option>\n<option value="3">March</option>\n<option value="4">April</option>\n<option value="5">May</option>\n<option value="6" selected="selected">June</option>\n<option value="7">July</option>\n<option value="8">August</option>\n<option value="9">September</option>\n<option value="10">October</option>\n<option value="11">November</option>\n<option value="12">December</option>\n</select>\n<select id="post_written_on_3i" name="post[written_on(3i)]">\n<option value="1">1</option>\n<option value="2">2</option>\n<option value="3">3</option>\n<option value="4">4</option>\n<option value="5">5</option>\n<option value="6">6</option>\n<option value="7">7</option>\n<option value="8">8</option>\n<option value="9">9</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15" selected="selected">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30">30</option>\n<option value="31">31</option>\n</select>\n &mdash; <select id="post_written_on_4i" name="post[written_on(4i)]">\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16" selected="selected">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n</select>\n : <select id="post_written_on_5i" name="post[written_on(5i)]">\n<option value="00">00</option>\n<option value="01">01</option>\n<option value="02">02</option>\n<option value="03">03</option>\n<option value="04">04</option>\n<option value="05">05</option>\n<option value="06">06</option>\n<option value="07">07</option>\n<option value="08">08</option>\n<option value="09">09</option>\n<option value="10">10</option>\n<option value="11">11</option>\n<option value="12">12</option>\n<option value="13">13</option>\n<option value="14">14</option>\n<option value="15">15</option>\n<option value="16">16</option>\n<option value="17">17</option>\n<option value="18">18</option>\n<option value="19">19</option>\n<option value="20">20</option>\n<option value="21">21</option>\n<option value="22">22</option>\n<option value="23">23</option>\n<option value="24">24</option>\n<option value="25">25</option>\n<option value="26">26</option>\n<option value="27">27</option>\n<option value="28">28</option>\n<option value="29">29</option>\n<option value="30" selected="selected">30</option>\n<option value="31">31</option>\n<option value="32">32</option>\n<option value="33">33</option>\n<option value="34">34</option>\n<option value="35">35</option>\n<option value="36">36</option>\n<option value="37">37</option>\n<option value="38">38</option>\n<option value="39">39</option>\n<option value="40">40</option>\n<option value="41">41</option>\n<option value="42">42</option>\n<option value="43">43</option>\n<option value="44">44</option>\n<option value="45">45</option>\n<option value="46">46</option>\n<option value="47">47</option>\n<option value="48">48</option>\n<option value="49">49</option>\n<option value="50">50</option>\n<option value="51">51</option>\n<option value="52">52</option>\n<option value="53">53</option>\n<option value="54">54</option>\n<option value="55">55</option>\n<option value="56">56</option>\n<option value="57">57</option>\n<option value="58">58</option>\n<option value="59">59</option>\n</select>\n</p><input name="commit" type="submit" value="Create" /></form>),
230       form("post")
231     )
232   end
233
234   def test_error_for_block
235     assert_dom_equal %(<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>), error_messages_for("post")
236     assert_equal %(<div class="errorDeathByClass" id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => "errorDeathById", :header_tag => "h1")
237     assert_equal %(<div id="errorDeathById"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => nil, :id => "errorDeathById", :header_tag => "h1")
238     assert_equal %(<div class="errorDeathByClass"><h1>1 error prohibited this post from being saved</h1><p>There were problems with the following fields:</p><ul><li>Author name can't be empty</li></ul></div>), error_messages_for("post", :class => "errorDeathByClass", :id => nil, :header_tag => "h1")
239   end
240
241   def test_error_messages_for_escapes_html
242     @dirty_post = DirtyPost.new
243     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>1 error prohibited this dirty post from being saved</h2><p>There were problems with the following fields:</p><ul><li>Author name can't be &lt;em&gt;empty&lt;/em&gt;</li></ul></div>), error_messages_for("dirty_post")
244   end
245
246   def test_error_messages_for_handles_nil
247     assert_equal "", error_messages_for("notthere")
248   end
249
250   def test_error_message_on_escapes_html
251     @dirty_post = DirtyPost.new
252     assert_dom_equal "<div class=\"formError\">can't be &lt;em&gt;empty&lt;/em&gt;</div>", error_message_on(:dirty_post, :author_name)
253   end
254
255   def test_error_message_on_handles_nil
256     assert_equal "", error_message_on("notthere", "notthere")
257   end
258
259   def test_error_message_on
260     assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(:post, :author_name)
261   end
262
263   def test_error_message_on_no_instance_variable
264     other_post = @post
265     assert_dom_equal "<div class=\"formError\">can't be empty</div>", error_message_on(other_post, :author_name)
266   end
267
268   def test_error_message_on_with_options_hash
269     assert_dom_equal "<div class=\"differentError\">beforecan't be emptyafter</div>", error_message_on(:post, :author_name, :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
270   end
271
272   def test_error_message_on_with_tag_option_in_options_hash
273     assert_dom_equal "<span class=\"differentError\">beforecan't be emptyafter</span>", error_message_on(:post, :author_name, :html_tag => "span", :css_class => 'differentError', :prepend_text => 'before', :append_text => 'after')
274   end
275
276   def test_error_message_on_handles_empty_errors
277     assert_equal "", error_message_on(@post, :tag)
278   end
279
280   def test_error_messages_for_many_objects
281     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors 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><li>User email can't be empty</li></ul></div>), error_messages_for("post", "user")
282
283     # reverse the order, error order changes and so does the title
284     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post")
285
286     # add the default to put post back in the title
287     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object_name => "post")
288
289     # symbols work as well
290     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this post from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => :post)
291
292     # any default works too
293     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this monkey from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "monkey")
294
295     # should space object name
296     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this chunky bacon from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :object_name => "chunky_bacon")
297
298     # hide header and explanation messages with nil or empty string
299     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => nil, :message => "")
300
301     # override header and explanation messages
302     header_message = "Yikes! Some errors"
303     message = "Please fix the following fields and resubmit:"
304     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>#{header_message}</h2><p>#{message}</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for(:user, :post, :header_message => header_message, :message => message)
305   end
306
307   def test_error_messages_for_non_instance_variable
308     actual_user = @user
309     actual_post = @post
310     @user = nil
311     @post = nil
312
313   #explicitly set object
314     assert_dom_equal %(<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>), error_messages_for("post", :object => actual_post)
315
316   #multiple objects
317     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>), error_messages_for("user", "post", :object => [actual_user, actual_post])
318
319   #nil object
320     assert_equal '', error_messages_for('user', :object => nil)
321   end
322
323   def test_error_messages_for_model_objects
324     error = error_messages_for(@post)
325     assert_dom_equal %(<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>),
326       error
327
328     error = error_messages_for(@user, @post)
329     assert_dom_equal %(<div class="errorExplanation" id="errorExplanation"><h2>2 errors prohibited this user from being saved</h2><p>There were problems with the following fields:</p><ul><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>),
330       error
331   end
332
333   def test_form_with_string_multipart
334     assert_dom_equal(
335       %(<form action="create" enctype="multipart/form-data" method="post"><p><label for="post_title">Title</label><br /><input id="post_title" name="post[title]" size="30" type="text" value="Hello World" /></p>\n<p><label for="post_body">Body</label><br /><div class="fieldWithErrors"><textarea cols="40" id="post_body" name="post[body]" rows="20">Back to the hill and over it again!</textarea></div></p><input name="commit" type="submit" value="Create" /></form>),
336       form("post", :multipart => true)
337     )
338   end
339
340   def test_default_form_builder_with_dynamic_form_helpers
341     form_for(@post, :as => :post, :url => {}) do |f|
342       concat f.error_message_on('author_name')
343       concat f.error_messages
344     end
345
346     expected = %(<form class="post_new" method="post" action="" id="post_new">) +
347                %(<div class="formError">can't be empty</div>) +
348                %(<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>) +
349                %(</form>)
350
351     assert_dom_equal expected, output_buffer
352   end
353
354   def test_default_form_builder_no_instance_variable
355     post = @post
356     @post = nil
357
358     form_for(post, :as => :post, :url => {}) do |f|
359       concat f.error_message_on('author_name')
360       concat f.error_messages
361     end
362
363     expected = %(<form class="post_new" method="post" action="" id="post_new">) +
364                %(<div class="formError">can't be empty</div>) +
365                %(<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>) +
366                %(</form>)
367
368     assert_dom_equal expected, output_buffer
369   end
370 end