Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / actionpack / test / controller / polymorphic_routes_test.rb
1 require 'abstract_unit'
2
3 class Article
4 attr_reader :id
5 def save; @id = 1 end
6 def new_record?; @id.nil? end
7 def name
8 model = self.class.name.downcase
9 @id.nil? ? "new #{model}" : "#{model} ##{@id}"
10 end
11 end
12
13 class Response < Article
14 def post_id; 1 end
15 end
16
17 class Tag < Article
18 def response_id; 1 end
19 end
20
21 class Tax
22 attr_reader :id
23 def save; @id = 1 end
24 def new_record?; @id.nil? end
25 def name
26 model = self.class.name.downcase
27 @id.nil? ? "new #{model}" : "#{model} ##{@id}"
28 end
29 end
30
31 class Fax < Tax
32 def store_id; 1 end
33 end
34
35 # TODO: test nested models
36 class Response::Nested < Response; end
37
38 class PolymorphicRoutesTest < ActiveSupport::TestCase
39 include ActionController::PolymorphicRoutes
40
41 def setup
42 @article = Article.new
43 @response = Response.new
44 @tax = Tax.new
45 @fax = Fax.new
46 end
47
48 def test_with_record
49 @article.save
50 expects(:article_url).with(@article)
51 polymorphic_url(@article)
52 end
53
54 def test_with_new_record
55 expects(:articles_url).with()
56 @article.expects(:new_record?).returns(true)
57 polymorphic_url(@article)
58 end
59
60 def test_with_record_and_action
61 expects(:new_article_url).with()
62 @article.expects(:new_record?).never
63 polymorphic_url(@article, :action => 'new')
64 end
65
66 def test_url_helper_prefixed_with_new
67 expects(:new_article_url).with()
68 new_polymorphic_url(@article)
69 end
70
71 def test_url_helper_prefixed_with_edit
72 @article.save
73 expects(:edit_article_url).with(@article)
74 edit_polymorphic_url(@article)
75 end
76
77 def test_url_helper_prefixed_with_edit_with_url_options
78 @article.save
79 expects(:edit_article_url).with(@article, :param1 => '10')
80 edit_polymorphic_url(@article, :param1 => '10')
81 end
82
83 def test_url_helper_with_url_options
84 @article.save
85 expects(:article_url).with(@article, :param1 => '10')
86 polymorphic_url(@article, :param1 => '10')
87 end
88
89 def test_formatted_url_helper_is_deprecated
90 expects(:articles_url).with(:format => :pdf)
91 assert_deprecated do
92 formatted_polymorphic_url([@article, :pdf])
93 end
94 end
95
96 def test_format_option
97 @article.save
98 expects(:article_url).with(@article, :format => :pdf)
99 polymorphic_url(@article, :format => :pdf)
100 end
101
102 def test_format_option_with_url_options
103 @article.save
104 expects(:article_url).with(@article, :format => :pdf, :param1 => '10')
105 polymorphic_url(@article, :format => :pdf, :param1 => '10')
106 end
107
108 def test_id_and_format_option
109 @article.save
110 expects(:article_url).with(:id => @article, :format => :pdf)
111 polymorphic_url(:id => @article, :format => :pdf)
112 end
113
114 def test_with_nested
115 @response.save
116 expects(:article_response_url).with(@article, @response)
117 polymorphic_url([@article, @response])
118 end
119
120 def test_with_nested_unsaved
121 expects(:article_responses_url).with(@article)
122 polymorphic_url([@article, @response])
123 end
124
125 def test_new_with_array_and_namespace
126 expects(:new_admin_article_url).with()
127 polymorphic_url([:admin, @article], :action => 'new')
128 end
129
130 def test_unsaved_with_array_and_namespace
131 expects(:admin_articles_url).with()
132 polymorphic_url([:admin, @article])
133 end
134
135 def test_nested_unsaved_with_array_and_namespace
136 @article.save
137 expects(:admin_article_url).with(@article)
138 polymorphic_url([:admin, @article])
139 expects(:admin_article_responses_url).with(@article)
140 polymorphic_url([:admin, @article, @response])
141 end
142
143 def test_nested_with_array_and_namespace
144 @response.save
145 expects(:admin_article_response_url).with(@article, @response)
146 polymorphic_url([:admin, @article, @response])
147
148 # a ridiculously long named route tests correct ordering of namespaces and nesting:
149 @tag = Tag.new
150 @tag.save
151 expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
152 polymorphic_url([:site, :admin, @article, @response, @tag])
153 end
154
155 def test_nesting_with_array_ending_in_singleton_resource
156 expects(:article_response_url).with(@article)
157 polymorphic_url([@article, :response])
158 end
159
160 def test_nesting_with_array_containing_singleton_resource
161 @tag = Tag.new
162 @tag.save
163 expects(:article_response_tag_url).with(@article, @tag)
164 polymorphic_url([@article, :response, @tag])
165 end
166
167 def test_nesting_with_array_containing_namespace_and_singleton_resource
168 @tag = Tag.new
169 @tag.save
170 expects(:admin_article_response_tag_url).with(@article, @tag)
171 polymorphic_url([:admin, @article, :response, @tag])
172 end
173
174 def test_nesting_with_array_containing_singleton_resource_and_format
175 @tag = Tag.new
176 @tag.save
177 expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
178 polymorphic_url([@article, :response, @tag], :format => :pdf)
179 end
180
181 def test_nesting_with_array_containing_singleton_resource_and_format_option
182 @tag = Tag.new
183 @tag.save
184 expects(:article_response_tag_url).with(@article, @tag, :format => :pdf)
185 polymorphic_url([@article, :response, @tag], :format => :pdf)
186 end
187
188 def test_nesting_with_array_containing_nil
189 expects(:article_response_url).with(@article)
190 polymorphic_url([@article, nil, :response])
191 end
192
193 def test_with_array_containing_single_object
194 @article.save
195 expects(:article_url).with(@article)
196 polymorphic_url([nil, @article])
197 end
198
199 def test_with_array_containing_single_name
200 @article.save
201 expects(:articles_url)
202 polymorphic_url([:articles])
203 end
204
205 # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
206 def xtest_with_hash
207 expects(:article_url).with(@article)
208 @article.save
209 polymorphic_url(:id => @article)
210 end
211
212 def test_polymorphic_path_accepts_options
213 expects(:new_article_path).with()
214 polymorphic_path(@article, :action => :new)
215 end
216
217 def test_polymorphic_path_does_not_modify_arguments
218 expects(:admin_article_responses_url).with(@article)
219 path = [:admin, @article, @response]
220 assert_no_difference 'path.size' do
221 polymorphic_url(path)
222 end
223 end
224
225 # Tests for names where .plural.singular doesn't round-trip
226 def test_with_irregular_plural_record
227 @tax.save
228 expects(:taxis_url).with(@tax)
229 polymorphic_url(@tax)
230 end
231
232 def test_with_irregular_plural_new_record
233 expects(:taxes_url).with()
234 @tax.expects(:new_record?).returns(true)
235 polymorphic_url(@tax)
236 end
237
238 def test_with_irregular_plural_record_and_action
239 expects(:new_taxis_url).with()
240 @tax.expects(:new_record?).never
241 polymorphic_url(@tax, :action => 'new')
242 end
243
244 def test_irregular_plural_url_helper_prefixed_with_new
245 expects(:new_taxis_url).with()
246 new_polymorphic_url(@tax)
247 end
248
249 def test_irregular_plural_url_helper_prefixed_with_edit
250 @tax.save
251 expects(:edit_taxis_url).with(@tax)
252 edit_polymorphic_url(@tax)
253 end
254
255 def test_with_nested_irregular_plurals
256 @fax.save
257 expects(:taxis_faxis_url).with(@tax, @fax)
258 polymorphic_url([@tax, @fax])
259 end
260
261 def test_with_nested_unsaved_irregular_plurals
262 expects(:taxis_faxes_url).with(@tax)
263 polymorphic_url([@tax, @fax])
264 end
265
266 def test_new_with_irregular_plural_array_and_namespace
267 expects(:new_admin_taxis_url).with()
268 polymorphic_url([:admin, @tax], :action => 'new')
269 end
270
271 def test_unsaved_with_irregular_plural_array_and_namespace
272 expects(:admin_taxes_url).with()
273 polymorphic_url([:admin, @tax])
274 end
275
276 def test_nesting_with_irregular_plurals_and_array_ending_in_singleton_resource
277 expects(:taxis_faxis_url).with(@tax)
278 polymorphic_url([@tax, :faxis])
279 end
280
281 def test_with_array_containing_single_irregular_plural_object
282 @tax.save
283 expects(:taxis_url).with(@tax)
284 polymorphic_url([nil, @tax])
285 end
286
287 def test_with_array_containing_single_name_irregular_plural
288 @tax.save
289 expects(:taxes_url)
290 polymorphic_url([:taxes])
291 end
292
293 end