]> git.openstreetmap.org Git - rails.git/blob - test/controllers/browse_controller_test.rb
Add links to element versions from version headings
[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 "a[href='#{api_relation_path relation}']", :count => 1
48   end
49
50   def test_read_relation_history
51     relation = create(:relation, :with_history)
52     browse_check :relation_history_path, relation.id, "browse/history"
53     assert_select "h4", /^Version/ do
54       assert_select "a[href='#{old_relation_path relation, 1}']", :text => "1", :count => 1
55     end
56   end
57
58   def test_read_way
59     way = create(:way)
60     browse_check :way_path, way.id, "browse/feature"
61     assert_select "h4", /^Version/ do
62       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
63     end
64     assert_select "a[href='#{api_way_path way}']", :count => 1
65   end
66
67   def test_read_way_history
68     way = create(:way, :with_history)
69     browse_check :way_history_path, way.id, "browse/history"
70     assert_select "h4", /^Version/ do
71       assert_select "a[href='#{old_way_path way, 1}']", :text => "1", :count => 1
72     end
73   end
74
75   def test_read_node
76     node = create(:node)
77     browse_check :node_path, node.id, "browse/feature"
78     assert_select "h4", /^Version/ do
79       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
80     end
81     assert_select "a[href='#{api_node_path node}']", :count => 1
82   end
83
84   def test_read_deleted_node
85     node = create(:node, :visible => false)
86     browse_check :node_path, node.id, "browse/feature"
87     assert_select "h4", /^Version/ do
88       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
89     end
90     assert_select "a[href='#{api_node_path node}']", :count => 0
91   end
92
93   def test_read_node_history
94     node = create(:node, :with_history)
95     browse_check :node_history_path, node.id, "browse/history"
96     assert_select "h4", /^Version/ do
97       assert_select "a[href='#{old_node_path node, 1}']", :text => "1", :count => 1
98     end
99   end
100
101   def test_read_changeset
102     user = create(:user)
103     changeset = create(:changeset, :user => user)
104     create(:changeset, :user => user)
105     browse_check :changeset_path, changeset.id, "browse/changeset"
106   end
107
108   def test_read_private_changeset
109     user = create(:user)
110     changeset = create(:changeset, :user => create(:user, :data_public => false))
111     create(:changeset, :user => user)
112     browse_check :changeset_path, changeset.id, "browse/changeset"
113   end
114
115   def test_read_changeset_hidden_comments
116     changeset = create(:changeset)
117     create_list(:changeset_comment, 3, :changeset => changeset)
118     create(:changeset_comment, :visible => false, :changeset => changeset)
119
120     browse_check :changeset_path, changeset.id, "browse/changeset"
121     assert_select "div.changeset-comments ul li", :count => 3
122
123     session_for(create(:moderator_user))
124
125     browse_check :changeset_path, changeset.id, "browse/changeset"
126     assert_select "div.changeset-comments ul li", :count => 4
127   end
128
129   ##
130   #  Methods to check redaction.
131   #
132   # note that these are presently highly reliant on the structure of the
133   # page for the selection tests, which doesn't work out particularly
134   # well if that structure changes. so... if you change the page layout
135   # then please make it more easily (and robustly) testable!
136   ##
137   def test_redacted_node
138     node = create(:node, :with_history, :deleted, :version => 2)
139     node_v1 = node.old_nodes.find_by(:version => 1)
140     node_v1.redact!(create(:redaction))
141
142     get node_path(:id => node)
143     assert_response :success
144     assert_template "feature"
145
146     # check that we don't show lat/lon for a redacted node.
147     assert_select ".browse-section", 1
148     assert_select ".browse-section.browse-node", 1
149     assert_select ".browse-section.browse-node .latitude", 0
150     assert_select ".browse-section.browse-node .longitude", 0
151   end
152
153   def test_redacted_node_history
154     node = create(:node, :with_history, :deleted, :version => 2)
155     node_v1 = node.old_nodes.find_by(:version => 1)
156     node_v1.redact!(create(:redaction))
157
158     get node_history_path(:id => node)
159     assert_response :success
160     assert_template "browse/history"
161
162     # there are 2 revisions of the redacted node, but only one
163     # should be showing details here.
164     assert_select ".browse-section", 2
165     assert_select ".browse-section.browse-redacted", 1
166     assert_select ".browse-section.browse-node", 1
167     assert_select ".browse-section.browse-node .latitude", 0
168     assert_select ".browse-section.browse-node .longitude", 0
169   end
170
171   def test_redacted_way_history
172     way = create(:way, :with_history, :version => 4)
173     way_v1 = way.old_ways.find_by(:version => 1)
174     way_v1.redact!(create(:redaction))
175     way_v3 = way.old_ways.find_by(:version => 3)
176     way_v3.redact!(create(:redaction))
177
178     get way_history_path(:id => way)
179     assert_response :success
180     assert_template "browse/history"
181
182     # there are 4 revisions of the redacted way, but only 2
183     # should be showing details here.
184     assert_select ".browse-section", 4
185     assert_select ".browse-section.browse-redacted", 2
186     assert_select ".browse-section.browse-way", 2
187   end
188
189   def test_redacted_relation_history
190     relation = create(:relation, :with_history, :version => 4)
191     relation_v1 = relation.old_relations.find_by(:version => 1)
192     relation_v1.redact!(create(:redaction))
193     relation_v3 = relation.old_relations.find_by(:version => 3)
194     relation_v3.redact!(create(:redaction))
195
196     get relation_history_path(:id => relation)
197     assert_response :success
198     assert_template "browse/history"
199
200     # there are 4 revisions of the redacted relation, but only 2
201     # should be showing details here.
202     assert_select ".browse-section", 4
203     assert_select ".browse-section.browse-redacted", 2
204     assert_select ".browse-section.browse-relation", 2
205   end
206
207   def test_query
208     get query_path
209     assert_response :success
210     assert_template "browse/query"
211   end
212
213   private
214
215   # This is a convenience method for most of the above checks
216   # First we check that when we don't have an id, it will correctly return a 404
217   # then we check that we get the correct 404 when a non-existant id is passed
218   # then we check that it will get a successful response, when we do pass an id
219   def browse_check(path, id, template)
220     path_method = method(path)
221
222     assert_raise ActionController::UrlGenerationError do
223       get path_method.call
224     end
225
226     assert_raise ActionController::UrlGenerationError do
227       get path_method.call(:id => -10) # we won't have an id that's negative
228     end
229
230     get path_method.call(:id => 0)
231     assert_response :not_found
232     assert_template "browse/not_found"
233     assert_template :layout => "map"
234
235     get path_method.call(:id => 0), :xhr => true
236     assert_response :not_found
237     assert_template "browse/not_found"
238     assert_template :layout => "xhr"
239
240     get path_method.call(:id => id)
241     assert_response :success
242     assert_template template
243     assert_template :layout => "map"
244
245     get path_method.call(:id => id), :xhr => true
246     assert_response :success
247     assert_template template
248     assert_template :layout => "xhr"
249   end
250 end