]> git.openstreetmap.org Git - rails.git/blob - test/controllers/geocoder_controller_test.rb
Merge remote-tracking branch 'upstream/pull/4720'
[rails.git] / test / controllers / geocoder_controller_test.rb
1 require "test_helper"
2
3 class GeocoderControllerTest < ActionDispatch::IntegrationTest
4   ##
5   # test all routes which lead to this controller
6   def test_routes
7     assert_routing(
8       { :path => "/search", :method => :get },
9       { :controller => "geocoder", :action => "search" }
10     )
11     assert_routing(
12       { :path => "/geocoder/search_latlon", :method => :post },
13       { :controller => "geocoder", :action => "search_latlon" }
14     )
15     assert_routing(
16       { :path => "/geocoder/search_osm_nominatim", :method => :post },
17       { :controller => "geocoder", :action => "search_osm_nominatim" }
18     )
19     assert_routing(
20       { :path => "/geocoder/search_osm_nominatim_reverse", :method => :post },
21       { :controller => "geocoder", :action => "search_osm_nominatim_reverse" }
22     )
23   end
24
25   ##
26   # Test identification with no arguments
27   def test_identify_error
28     get search_path
29     assert_response :bad_request
30
31     get search_path, :xhr => true
32     assert_response :bad_request
33   end
34
35   ##
36   # Test identification of basic lat/lon pairs
37   def test_identify_latlon_basic
38     [
39       "50.06773 14.37742",
40       "50.06773, 14.37742",
41       "+50.06773 +14.37742",
42       "+50.06773, +14.37742"
43     ].each do |code|
44       latlon_check code, 50.06773, 14.37742
45     end
46   end
47
48   ##
49   # Test identification of lat/lon pairs using N/E with degrees
50   def test_identify_latlon_ne_d
51     [
52       "N50.06773 E14.37742",
53       "N50.06773, E14.37742",
54       "50.06773N 14.37742E",
55       "50.06773N, 14.37742E"
56     ].each do |code|
57       latlon_check code, 50.06773, 14.37742
58     end
59   end
60
61   ##
62   # Test identification of lat/lon pairs using N/W with degrees
63   def test_identify_latlon_nw_d
64     [
65       "N50.06773 W14.37742",
66       "N50.06773, W14.37742",
67       "50.06773N 14.37742W",
68       "50.06773N, 14.37742W"
69     ].each do |code|
70       latlon_check code, 50.06773, -14.37742
71     end
72   end
73
74   ##
75   # Test identification of lat/lon pairs using S/E with degrees
76   def test_identify_latlon_se_d
77     [
78       "S50.06773 E14.37742",
79       "S50.06773, E14.37742",
80       "50.06773S 14.37742E",
81       "50.06773S, 14.37742E"
82     ].each do |code|
83       latlon_check code, -50.06773, 14.37742
84     end
85   end
86
87   ##
88   # Test identification of lat/lon pairs using S/W with degrees
89   def test_identify_latlon_sw_d
90     [
91       "S50.06773 W14.37742",
92       "S50.06773, W14.37742",
93       "50.06773S 14.37742W",
94       "50.06773S, 14.37742W"
95     ].each do |code|
96       latlon_check code, -50.06773, -14.37742
97     end
98   end
99
100   ##
101   # Test identification of lat/lon pairs using N/E with degrees/mins
102   def test_identify_latlon_ne_dm
103     [
104       "N 50° 04.064 E 014° 22.645",
105       "N 50° 04.064' E 014° 22.645",
106       "N 50° 04.064', E 014° 22.645'",
107       "N50° 04.064 E14° 22.645",
108       "N 50 04.064 E 014 22.645",
109       "N50 4.064 E14 22.645",
110       "50° 04.064' N, 014° 22.645' E"
111     ].each do |code|
112       latlon_check code, 50.06773, 14.37742
113     end
114   end
115
116   ##
117   # Test identification of lat/lon pairs using N/W with degrees/mins
118   def test_identify_latlon_nw_dm
119     [
120       "N 50° 04.064 W 014° 22.645",
121       "N 50° 04.064' W 014° 22.645",
122       "N 50° 04.064', W 014° 22.645'",
123       "N50° 04.064 W14° 22.645",
124       "N 50 04.064 W 014 22.645",
125       "N50 4.064 W14 22.645",
126       "50° 04.064' N, 014° 22.645' W"
127     ].each do |code|
128       latlon_check code, 50.06773, -14.37742
129     end
130   end
131
132   ##
133   # Test identification of lat/lon pairs using S/E with degrees/mins
134   def test_identify_latlon_se_dm
135     [
136       "S 50° 04.064 E 014° 22.645",
137       "S 50° 04.064' E 014° 22.645",
138       "S 50° 04.064', E 014° 22.645'",
139       "S50° 04.064 E14° 22.645",
140       "S 50 04.064 E 014 22.645",
141       "S50 4.064 E14 22.645",
142       "50° 04.064' S, 014° 22.645' E"
143     ].each do |code|
144       latlon_check code, -50.06773, 14.37742
145     end
146   end
147
148   ##
149   # Test identification of lat/lon pairs using S/W with degrees/mins
150   def test_identify_latlon_sw_dm
151     [
152       "S 50° 04.064 W 014° 22.645",
153       "S 50° 04.064' W 014° 22.645",
154       "S 50° 04.064', W 014° 22.645'",
155       "S50° 04.064 W14° 22.645",
156       "S 50 04.064 W 014 22.645",
157       "S50 4.064 W14 22.645",
158       "50° 04.064' S, 014° 22.645' W"
159     ].each do |code|
160       latlon_check code, -50.06773, -14.37742
161     end
162   end
163
164   ##
165   # Test identification of lat/lon pairs using N/E with degrees/mins/secs
166   def test_identify_latlon_ne_dms
167     [
168       "N 50° 4' 03.828\" E 14° 22' 38.712\"",
169       "N 50° 4' 03.828\", E 14° 22' 38.712\"",
170       "N 50° 4′ 03.828″, E 14° 22′ 38.712″",
171       "N50 4 03.828 E14 22 38.712",
172       "N50 4 03.828, E14 22 38.712",
173       "50°4'3.828\"N 14°22'38.712\"E"
174     ].each do |code|
175       latlon_check code, 50.06773, 14.37742
176     end
177   end
178
179   ##
180   # Test identification of lat/lon pairs using N/W with degrees/mins/secs
181   def test_identify_latlon_nw_dms
182     [
183       "N 50° 4' 03.828\" W 14° 22' 38.712\"",
184       "N 50° 4' 03.828\", W 14° 22' 38.712\"",
185       "N 50° 4′ 03.828″, W 14° 22′ 38.712″",
186       "N50 4 03.828 W14 22 38.712",
187       "N50 4 03.828, W14 22 38.712",
188       "50°4'3.828\"N 14°22'38.712\"W"
189     ].each do |code|
190       latlon_check code, 50.06773, -14.37742
191     end
192   end
193
194   ##
195   # Test identification of lat/lon pairs using S/E with degrees/mins/secs
196   def test_identify_latlon_se_dms
197     [
198       "S 50° 4' 03.828\" E 14° 22' 38.712\"",
199       "S 50° 4' 03.828\", E 14° 22' 38.712\"",
200       "S 50° 4′ 03.828″, E 14° 22′ 38.712″",
201       "S50 4 03.828 E14 22 38.712",
202       "S50 4 03.828, E14 22 38.712",
203       "50°4'3.828\"S 14°22'38.712\"E"
204     ].each do |code|
205       latlon_check code, -50.06773, 14.37742
206     end
207   end
208
209   ##
210   # Test identification of lat/lon pairs using S/W with degrees/mins/secs
211   def test_identify_latlon_sw_dms
212     [
213       "S 50° 4' 03.828\" W 14° 22' 38.712\"",
214       "S 50° 4' 03.828\", W 14° 22' 38.712\"",
215       "S 50° 4′ 03.828″, W 14° 22′ 38.712″",
216       "S50 4 03.828 W14 22 38.712",
217       "S50 4 03.828, W14 22 38.712",
218       "50°4'3.828\"S 14°22'38.712\"W"
219     ].each do |code|
220       latlon_check code, -50.06773, -14.37742
221     end
222   end
223
224   ##
225   # Test identification of US zipcodes
226   def test_identify_us_postcode
227     %w[
228       12345
229       12345-6789
230     ].each do |code|
231       search_check code, %w[osm_nominatim]
232     end
233   end
234
235   ##
236   # Test identification of UK postcodes using examples from
237   # http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom
238   def test_identify_uk_postcode
239     [
240       "EC1A 1BB",
241       "W1A 1HQ",
242       "M1 1AA",
243       "B33 8TH",
244       "CR2 6XH",
245       "DN55 1PT"
246     ].each do |code|
247       search_check code, %w[osm_nominatim]
248     end
249   end
250
251   ##
252   # Test identification of Canadian postcodes
253   def test_identify_ca_postcode
254     search_check "A1B 2C3", %w[osm_nominatim]
255   end
256
257   ##
258   # Test identification fall through to the default case
259   def test_identify_default
260     search_check "foo bar baz", %w[osm_nominatim]
261   end
262
263   ##
264   # Test the builtin latitude+longitude search
265   def test_search_latlon
266     post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16), :xhr => true
267     results_check :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16
268
269     post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16), :xhr => true
270     results_check_error "Latitude -91.23 out of range"
271
272     post geocoder_search_latlon_path(:lat => 91.23, :lon => 4.56, :zoom => 16), :xhr => true
273     results_check_error "Latitude 91.23 out of range"
274
275     post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16), :xhr => true
276     results_check_error "Longitude -180.23 out of range"
277
278     post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16), :xhr => true
279     results_check_error "Longitude 180.23 out of range"
280   end
281
282   def test_search_latlon_digits
283     post geocoder_search_latlon_path(:lat => 1.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
284     results_check({ :name => "1.23, 4.56", :lat => 1.23, :lon => 4.56, :zoom => 16 },
285                   { :name => "4.56, 1.23", :lat => 4.56, :lon => 1.23, :zoom => 16 })
286
287     post geocoder_search_latlon_path(:lat => -91.23, :lon => 4.56, :zoom => 16, :latlon_digits => true), :xhr => true
288     results_check :name => "4.56, -91.23", :lat => 4.56, :lon => -91.23, :zoom => 16
289
290     post geocoder_search_latlon_path(:lat => -1.23, :lon => 170.23, :zoom => 16, :latlon_digits => true), :xhr => true
291     results_check :name => "-1.23, 170.23", :lat => -1.23, :lon => 170.23, :zoom => 16
292
293     post geocoder_search_latlon_path(:lat => 91.23, :lon => 94.56, :zoom => 16, :latlon_digits => true), :xhr => true
294     results_check_error "Latitude or longitude are out of range"
295
296     post geocoder_search_latlon_path(:lat => -91.23, :lon => -94.56, :zoom => 16, :latlon_digits => true), :xhr => true
297     results_check_error "Latitude or longitude are out of range"
298
299     post geocoder_search_latlon_path(:lat => 1.23, :lon => -180.23, :zoom => 16, :latlon_digits => true), :xhr => true
300     results_check_error "Latitude or longitude are out of range"
301
302     post geocoder_search_latlon_path(:lat => 1.23, :lon => 180.23, :zoom => 16, :latlon_digits => true), :xhr => true
303     results_check_error "Latitude or longitude are out of range"
304   end
305
306   ##
307   # Test the nominatim forward search
308   def test_search_osm_nominatim
309     with_http_stubs "nominatim" do
310       post geocoder_search_osm_nominatim_path(:query => "Hoddesdon", :zoom => 10,
311                                               :minlon => -0.559, :minlat => 51.217,
312                                               :maxlon => 0.836, :maxlat => 51.766), :xhr => true
313       results_check "name" => "Hoddesdon, Hertfordshire, East of England, England, United Kingdom",
314                     "min-lat" => 51.7216709, "max-lat" => 51.8016709,
315                     "min-lon" => -0.0512898, "max-lon" => 0.0287102,
316                     "type" => "node", "id" => 18007599
317
318       post geocoder_search_osm_nominatim_path(:query => "Broxbourne", :zoom => 10,
319                                               :minlon => -0.559, :minlat => 51.217,
320                                               :maxlon => 0.836, :maxlat => 51.766), :xhr => true
321       results_check({ "prefix" => "Suburb",
322                       "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
323                       "min-lat" => 51.7265723, "max-lat" => 51.7665723,
324                       "min-lon" => -0.0390782, "max-lon" => 0.0009218,
325                       "type" => "node", "id" => 28825933 },
326                     { "prefix" => "Village",
327                       "name" => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
328                       "min-lat" => 51.6808751, "max-lat" => 51.7806237,
329                       "min-lon" => -0.114204, "max-lon" => 0.0145267,
330                       "type" => "relation", "id" => 2677978 },
331                     { "prefix" => "Railway Station",
332                       "name" => "Broxbourne, Stafford Drive, Broxbourne, Hertfordshire, East of England, England, United Kingdom",
333                       "min-lat" => 51.7418469, "max-lat" => 51.7518469,
334                       "min-lon" => -0.0156773, "max-lon" => -0.0056773,
335                       "type" => "node", "id" => 17044599 })
336     end
337   end
338
339   ##
340   # Test the nominatim reverse search
341   def test_search_osm_nominatim_reverse
342     with_http_stubs "nominatim" do
343       post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 15), :xhr => true
344       results_check :name => "Broxbourne, Hertfordshire, East of England, England, United Kingdom",
345                     :lat => 51.7465723, :lon => -0.0190782,
346                     :type => "node", :id => 28825933, :zoom => 15
347
348       post geocoder_search_osm_nominatim_reverse_path(:lat => 51.7632, :lon => -0.0076, :zoom => 17), :xhr => true
349       results_check :name => "Dinant Link Road, Broxbourne, Hertfordshire, East of England, England, EN11 8HX, United Kingdom",
350                     :lat => 51.7634883, :lon => -0.0088373,
351                     :type => "way", :id => 3489841, :zoom => 17
352
353       post geocoder_search_osm_nominatim_reverse_path(:lat => 13.7709, :lon => 100.50507, :zoom => 19), :xhr => true
354       results_check :name => "MM Steak&Grill, ถนนศรีอยุธยา, บางขุนพรหม, กรุงเทพมหานคร, เขตดุสิต, กรุงเทพมหานคร, 10300, ประเทศไทย",
355                     :lat => 13.7708691, :lon => 100.505073233221,
356                     :type => "way", :id => 542901374, :zoom => 19
357     end
358   end
359
360   private
361
362   def latlon_check(query, lat, lon)
363     get search_path(:query => query)
364     assert_response :success
365     assert_template :search
366     assert_template :layout => "map"
367     assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources)
368     assert_nil @controller.params[:query]
369     assert_in_delta lat, @controller.params[:lat]
370     assert_in_delta lon, @controller.params[:lon]
371
372     get search_path(:query => query), :xhr => true
373     assert_response :success
374     assert_template :search
375     assert_template :layout => "xhr"
376     assert_equal %w[latlon osm_nominatim_reverse], assigns(:sources)
377     assert_nil @controller.params[:query]
378     assert_in_delta lat, @controller.params[:lat]
379     assert_in_delta lon, @controller.params[:lon]
380   end
381
382   def search_check(query, sources)
383     get search_path(:query => query)
384     assert_response :success
385     assert_template :search
386     assert_template :layout => "map"
387     assert_equal sources, assigns(:sources)
388
389     get search_path(:query => query), :xhr => true
390     assert_response :success
391     assert_template :search
392     assert_template :layout => "xhr"
393     assert_equal sources, assigns(:sources)
394   end
395
396   def results_check(*results)
397     assert_response :success
398     assert_template :results
399     assert_template :layout => nil
400     if results.empty?
401       assert_select "ul.results-list", 0
402     else
403       assert_select "ul.results-list", 1 do
404         assert_select "li.search_results_entry", results.count
405
406         results.each do |result|
407           attrs = result.collect { |k, v| "[data-#{k}='#{v}']" }.join
408           assert_select "li.search_results_entry a.set_position#{attrs}", result[:name]
409         end
410       end
411     end
412   end
413
414   def results_check_error(error)
415     assert_response :success
416     assert_template :error
417     assert_template :layout => nil
418     assert_select ".alert.alert-danger", error
419   end
420 end