Froze rails gems
[depot.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 # TODO: test nested models
22 class Response::Nested < Response; end
23
24 uses_mocha 'polymorphic URL helpers' do
25 class PolymorphicRoutesTest < Test::Unit::TestCase
26
27 include ActionController::PolymorphicRoutes
28
29 def setup
30 @article = Article.new
31 @response = Response.new
32 end
33
34 def test_with_record
35 @article.save
36 expects(:article_url).with(@article)
37 polymorphic_url(@article)
38 end
39
40 def test_with_new_record
41 expects(:articles_url).with()
42 @article.expects(:new_record?).returns(true)
43 polymorphic_url(@article)
44 end
45
46 def test_with_record_and_action
47 expects(:new_article_url).with()
48 @article.expects(:new_record?).never
49 polymorphic_url(@article, :action => 'new')
50 end
51
52 def test_url_helper_prefixed_with_new
53 expects(:new_article_url).with()
54 new_polymorphic_url(@article)
55 end
56
57 def test_url_helper_prefixed_with_edit
58 @article.save
59 expects(:edit_article_url).with(@article)
60 edit_polymorphic_url(@article)
61 end
62
63 def test_url_helper_prefixed_with_edit_with_url_options
64 @article.save
65 expects(:edit_article_url).with(@article, :param1 => '10')
66 edit_polymorphic_url(@article, :param1 => '10')
67 end
68
69 def test_url_helper_with_url_options
70 @article.save
71 expects(:article_url).with(@article, :param1 => '10')
72 polymorphic_url(@article, :param1 => '10')
73 end
74
75 def test_formatted_url_helper
76 expects(:formatted_article_url).with(@article, :pdf)
77 formatted_polymorphic_url([@article, :pdf])
78 end
79
80 def test_format_option
81 @article.save
82 expects(:formatted_article_url).with(@article, :pdf)
83 polymorphic_url(@article, :format => :pdf)
84 end
85
86 def test_format_option_with_url_options
87 @article.save
88 expects(:formatted_article_url).with(@article, :pdf, :param1 => '10')
89 polymorphic_url(@article, :format => :pdf, :param1 => '10')
90 end
91
92 def test_id_and_format_option
93 @article.save
94 expects(:article_url).with(:id => @article, :format => :pdf)
95 polymorphic_url(:id => @article, :format => :pdf)
96 end
97
98 def test_with_nested
99 @response.save
100 expects(:article_response_url).with(@article, @response)
101 polymorphic_url([@article, @response])
102 end
103
104 def test_with_nested_unsaved
105 expects(:article_responses_url).with(@article)
106 polymorphic_url([@article, @response])
107 end
108
109 def test_new_with_array_and_namespace
110 expects(:new_admin_article_url).with()
111 polymorphic_url([:admin, @article], :action => 'new')
112 end
113
114 def test_unsaved_with_array_and_namespace
115 expects(:admin_articles_url).with()
116 polymorphic_url([:admin, @article])
117 end
118
119 def test_nested_unsaved_with_array_and_namespace
120 @article.save
121 expects(:admin_article_url).with(@article)
122 polymorphic_url([:admin, @article])
123 expects(:admin_article_responses_url).with(@article)
124 polymorphic_url([:admin, @article, @response])
125 end
126
127 def test_nested_with_array_and_namespace
128 @response.save
129 expects(:admin_article_response_url).with(@article, @response)
130 polymorphic_url([:admin, @article, @response])
131
132 # a ridiculously long named route tests correct ordering of namespaces and nesting:
133 @tag = Tag.new
134 @tag.save
135 expects(:site_admin_article_response_tag_url).with(@article, @response, @tag)
136 polymorphic_url([:site, :admin, @article, @response, @tag])
137 end
138
139 def test_nesting_with_array_ending_in_singleton_resource
140 expects(:article_response_url).with(@article)
141 polymorphic_url([@article, :response])
142 end
143
144 def test_nesting_with_array_containing_singleton_resource
145 @tag = Tag.new
146 @tag.save
147 expects(:article_response_tag_url).with(@article, @tag)
148 polymorphic_url([@article, :response, @tag])
149 end
150
151 def test_nesting_with_array_containing_namespace_and_singleton_resource
152 @tag = Tag.new
153 @tag.save
154 expects(:admin_article_response_tag_url).with(@article, @tag)
155 polymorphic_url([:admin, @article, :response, @tag])
156 end
157
158 def test_nesting_with_array_containing_singleton_resource_and_format
159 @tag = Tag.new
160 @tag.save
161 expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
162 formatted_polymorphic_url([@article, :response, @tag, :pdf])
163 end
164
165 def test_nesting_with_array_containing_singleton_resource_and_format_option
166 @tag = Tag.new
167 @tag.save
168 expects(:formatted_article_response_tag_url).with(@article, @tag, :pdf)
169 polymorphic_url([@article, :response, @tag], :format => :pdf)
170 end
171
172 def test_nesting_with_array_containing_nil
173 expects(:article_response_url).with(@article)
174 polymorphic_url([@article, nil, :response])
175 end
176
177 def test_with_array_containing_single_object
178 @article.save
179 expects(:article_url).with(@article)
180 polymorphic_url([nil, @article])
181 end
182
183 def test_with_array_containing_single_name
184 @article.save
185 expects(:articles_url)
186 polymorphic_url([:articles])
187 end
188
189 # TODO: Needs to be updated to correctly know about whether the object is in a hash or not
190 def xtest_with_hash
191 expects(:article_url).with(@article)
192 @article.save
193 polymorphic_url(:id => @article)
194 end
195
196 def test_polymorphic_path_accepts_options
197 expects(:new_article_path).with()
198 polymorphic_path(@article, :action => :new)
199 end
200
201 def test_polymorphic_path_does_not_modify_arguments
202 expects(:admin_article_responses_url).with(@article)
203 path = [:admin, @article, @response]
204 assert_no_difference 'path.size' do
205 polymorphic_url(path)
206 end
207 end
208 end
209 end