]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Link to first and last versions from element pages
[rails.git] / test / controllers / browse_controller_test.rb
1 require "test_helper"
2
3 class BrowseControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/node/1", :method => :get },
9       { :controller => "browse", :action => "node", :id => "1" }
10     )
11     assert_routing(
12       { :path => "/node/1/history", :method => :get },
13       { :controller => "browse", :action => "node_history", :id => "1" }
14     )
15     assert_routing(
16       { :path => "/way/1", :method => :get },
17       { :controller => "browse", :action => "way", :id => "1" }
18     )
19     assert_routing(
20       { :path => "/way/1/history", :method => :get },
21       { :controller => "browse", :action => "way_history", :id => "1" }
22     )
23     assert_routing(
24       { :path => "/relation/1", :method => :get },
25       { :controller => "browse", :action => "relation", :id => "1" }
26     )
27     assert_routing(
28       { :path => "/relation/1/history", :method => :get },
29       { :controller => "browse", :action => "relation_history", :id => "1" }
30     )
31     assert_routing(
32       { :path => "/changeset/1", :method => :get },
33       { :controller => "browse", :action => "changeset", :id => "1" }
34     )
35     assert_routing(
36       { :path => "/query", :method => :get },
37       { :controller => "browse", :action => "query" }
38     )
39   end
40
41   def test_read_relation
42     relation = create(:relation)
43     browse_check :relation_path, relation.id, "browse/feature"
44     assert_select "h4", /^Version/ do
45       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
46     end
47     assert_select ".secondary-actions a[href='#{api_relation_path relation}']", :count => 1
48     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
49     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 0
50   end
51
52   def test_multiple_version_relation_links
53     relation = create(:relation, :with_history, :version => 2)
54     browse_check :relation_path, relation.id, "browse/feature"
55     assert_select ".secondary-actions a[href='#{relation_history_path relation}']", :count => 1
56     assert_select ".secondary-actions a[href='#{old_relation_path relation, 1}']", :count => 1
57     assert_select ".secondary-actions a[href='#{old_relation_path relation, 2}']", :count => 1
58   end
59
60   def test_read_relation_history
61     relation = create(:relation, :with_history)
62     browse_check :relation_history_path, relation.id, "browse/history"
63     assert_select "h4", /^Version/ do
64       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
65     end
66   end
67
68   def test_read_way
69     way = create(:way)
70     browse_check :way_path, way.id, "browse/feature"
71     assert_select "h4", /^Version/ do
72       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
73     end
74     assert_select ".secondary-actions a[href='#{api_way_path way}']", :count => 1
75     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
76     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 0
77   end
78
79   def test_multiple_version_way_links
80     way = create(:way, :with_history, :version => 2)
81     browse_check :way_path, way.id, "browse/feature"
82     assert_select ".secondary-actions a[href='#{way_history_path way}']", :count => 1
83     assert_select ".secondary-actions a[href='#{old_way_path way, 1}']", :count => 1
84     assert_select ".secondary-actions a[href='#{old_way_path way, 2}']", :count => 1
85   end
86
87   def test_read_way_history
88     way = create(:way, :with_history)
89     browse_check :way_history_path, way.id, "browse/history"
90     assert_select "h4", /^Version/ do
91       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
92     end
93   end
94
95   def test_read_node
96     node = create(:node)
97     browse_check :node_path, node.id, "browse/feature"
98     assert_select "h4", /^Version/ do
99       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
100     end
101     assert_select ".secondary-actions a[href='#{api_node_path node}']", :count => 1
102     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
103     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 0
104   end
105
106   def test_multiple_version_node_links
107     node = create(:node, :with_history, :version => 2)
108     browse_check :node_path, node.id, "browse/feature"
109     assert_select ".secondary-actions a[href='#{node_history_path node}']", :count => 1
110     assert_select ".secondary-actions a[href='#{old_node_path node, 1}']", :count => 1
111     assert_select ".secondary-actions a[href='#{old_node_path node, 2}']", :count => 1
112   end
113
114   def test_read_deleted_node
115     node = create(:node, :visible => false)
116     browse_check :node_path, node.id, "browse/feature"
117     assert_select "h4", /^Version/ do
118       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
119     end
120     assert_select "a[href='#{api_node_path node}']", :count => 0
121   end
122
123   def test_read_node_history
124     node = create(:node, :with_history)
125     browse_check :node_history_path, node.id, "browse/history"
126     assert_select "h4", /^Version/ do
127       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
128     end
129   end
130
131   def test_read_changeset
132     user = create(:user)
133     changeset = create(:changeset, :user => user)
134     create(:changeset, :user => user)
135     browse_check :changeset_path, changeset.id, "browse/changeset"
136   end
137
138   def test_read_private_changeset
139     user = create(:user)
140     changeset = create(:changeset, :user => create(:user, :data_public => false))
141     create(:changeset, :user => user)
142     browse_check :changeset_path, changeset.id, "browse/changeset"
143   end
144
145   def test_read_changeset_hidden_comments
146     changeset = create(:changeset)
147     create_list(:changeset_comment, 3, :changeset => changeset)
148     create(:changeset_comment, :visible => false, :changeset => changeset)
149
150     browse_check :changeset_path, changeset.id, "browse/changeset"
151     assert_select "div.changeset-comments ul li", :count => 3
152
153     session_for(create(:moderator_user))
154
155     browse_check :changeset_path, changeset.id, "browse/changeset"
156     assert_select "div.changeset-comments ul li", :count => 4
157   end
158
159   ##
160   #  Methods to check redaction.
161   #
162   # note that these are presently highly reliant on the structure of the
163   # page for the selection tests, which doesn't work out particularly
164   # well if that structure changes. so... if you change the page layout
165   # then please make it more easily (and robustly) testable!
166   ##
167   def test_redacted_node
168     node = create(:node, :with_history, :deleted, :version => 2)
169     node_v1 = node.old_nodes.find_by(:version => 1)
170     node_v1.redact!(create(:redaction))
171
172     get node_path(:id => node)
173     assert_response :success
174     assert_template "feature"
175
176     # check that we don't show lat/lon for a redacted node.
177     assert_select ".browse-section", 1
178     assert_select ".browse-section.browse-node", 1
179     assert_select ".browse-section.browse-node .latitude", 0
180     assert_select ".browse-section.browse-node .longitude", 0
181   end
182
183   def test_redacted_node_history
184     node = create(:node, :with_history, :deleted, :version => 2)
185     node_v1 = node.old_nodes.find_by(:version => 1)
186     node_v1.redact!(create(:redaction))
187
188     get node_history_path(:id => node)
189     assert_response :success
190     assert_template "browse/history"
191
192     # there are 2 revisions of the redacted node, but only one
193     # should be showing details here.
194     assert_select ".browse-section", 2
195     assert_select ".browse-section.browse-redacted", 1
196     assert_select ".browse-section.browse-node", 1
197     assert_select ".browse-section.browse-node .latitude", 0
198     assert_select ".browse-section.browse-node .longitude", 0
199   end
200
201   def test_redacted_way_history
202     way = create(:way, :with_history, :version => 4)
203     way_v1 = way.old_ways.find_by(:version => 1)
204     way_v1.redact!(create(:redaction))
205     way_v3 = way.old_ways.find_by(:version => 3)
206     way_v3.redact!(create(:redaction))
207
208     get way_history_path(:id => way)
209     assert_response :success
210     assert_template "browse/history"
211
212     # there are 4 revisions of the redacted way, but only 2
213     # should be showing details here.
214     assert_select ".browse-section", 4
215     assert_select ".browse-section.browse-redacted", 2
216     assert_select ".browse-section.browse-way", 2
217   end
218
219   def test_redacted_relation_history
220     relation = create(:relation, :with_history, :version => 4)
221     relation_v1 = relation.old_relations.find_by(:version => 1)
222     relation_v1.redact!(create(:redaction))
223     relation_v3 = relation.old_relations.find_by(:version => 3)
224     relation_v3.redact!(create(:redaction))
225
226     get relation_history_path(:id => relation)
227     assert_response :success
228     assert_template "browse/history"
229
230     # there are 4 revisions of the redacted relation, but only 2
231     # should be showing details here.
232     assert_select ".browse-section", 4
233     assert_select ".browse-section.browse-redacted", 2
234     assert_select ".browse-section.browse-relation", 2
235   end
236
237   def test_query
238     get query_path
239     assert_response :success
240     assert_template "browse/query"
241   end
242
243   private
244
245   # This is a convenience method for most of the above checks
246   # First we check that when we don't have an id, it will correctly return a 404
247   # then we check that we get the correct 404 when a non-existant id is passed
248   # then we check that it will get a successful response, when we do pass an id
249   def browse_check(path, id, template)
250     path_method = method(path)
251
252     assert_raise ActionController::UrlGenerationError do
253       get path_method.call
254     end
255
256     assert_raise ActionController::UrlGenerationError do
257       get path_method.call(:id => -10) # we won't have an id that's negative
258     end
259
260     get path_method.call(:id => 0)
261     assert_response :not_found
262     assert_template "browse/not_found"
263     assert_template :layout => "map"
264
265     get path_method.call(:id => 0), :xhr => true
266     assert_response :not_found
267     assert_template "browse/not_found"
268     assert_template :layout => "xhr"
269
270     get path_method.call(:id => id)
271     assert_response :success
272     assert_template template
273     assert_template :layout => "map"
274
275     get path_method.call(:id => id), :xhr => true
276     assert_response :success
277     assert_template template
278     assert_template :layout => "xhr"
279   end
280 end