]> git.openstreetmap.org Git - rails.git/blob - test/system/query_features_test.rb
Localisation updates from https://translatewiki.net.
[rails.git] / test / system / query_features_test.rb
1 require "application_system_test_case"
2
3 class QueryFeaturesSystemTest < ApplicationSystemTestCase
4   test "sorts enclosing features correctly" do
5     visit "/#map=15/54.18315/7.88473"
6
7     within "#map" do
8       click_on "Query features"
9
10       stub_overpass :enclosing_elements => [
11         {
12           "type" => "relation",
13           "id" => 51477,
14           "bounds" => {
15             "minlat" => 47.2701114,
16             "minlon" => 5.8663153,
17             "maxlat" => 55.0991610,
18             "maxlon" => 15.0419309
19           },
20           "tags" => {
21             "admin_level" => "2",
22             "border_type" => "country",
23             "boundary" => "administrative",
24             "name" => "Deutschland",
25             "name:de" => "Deutschland",
26             "name:en" => "Germany",
27             "type" => "boundary"
28           }
29         },
30         {
31           "type" => "relation",
32           "id" => 51529,
33           "bounds" => {
34             "minlat" => 53.3598160,
35             "minlon" => 7.5211615,
36             "maxlat" => 55.0991610,
37             "maxlon" => 11.6723860
38           },
39           "tags" => {
40             "admin_level" => "4",
41             "border_type" => "state",
42             "boundary" => "administrative",
43             "name" => "Schleswig-Holstein",
44             "name:de" => "Schleswig-Holstein",
45             "name:en" => "Schleswig-Holstein",
46             "type" => "boundary"
47           }
48         },
49         {
50           "type" => "relation",
51           "id" => 3787052,
52           "bounds" => {
53             "minlat" => 54.1693099,
54             "minlon" => 7.8648436,
55             "maxlat" => 54.1907839,
56             "maxlon" => 7.9001626
57           },
58           "tags" => {
59             "name" => "Helgoland",
60             "name:de" => "Helgoland",
61             "name:en" => "Heligoland",
62             "place" => "island"
63           }
64         }
65       ]
66
67       click
68     end
69
70     within_sidebar do
71       assert_link "Heligoland"
72       assert_link "Schleswig-Holstein", :below => find_link("Heligoland")
73       assert_link "Germany", :below => find_link("Schleswig-Holstein")
74     end
75   end
76
77   test "sorts enclosing features correctly across antimeridian" do
78     visit "/#map=15/60/30"
79
80     within "#map" do
81       click_on "Query features"
82
83       stub_overpass :enclosing_elements => [
84         {
85           "type" => "relation",
86           "id" => 60189,
87           "bounds" => {
88             "minlat" => 41.1850968,
89             "minlon" => 19.4041722,
90             "maxlat" => 82.0586232,
91             "maxlon" => -168.9769440
92           },
93           "tags" => {
94             "admin_level" => "2",
95             "border_type" => "nation",
96             "boundary" => "administrative",
97             "name" => "Россия",
98             "name:en" => "Russia",
99             "name:ru" => "Россия",
100             "type" => "boundary"
101           }
102         },
103         {
104           "type" => "relation",
105           "id" => 1114253,
106           "bounds" => {
107             "minlat" => 59.8587853,
108             "minlon" => 29.6720697,
109             "maxlat" => 60.0370554,
110             "maxlon" => 30.2307057
111           },
112           "tags" => {
113             "name" => "Невская губа",
114             "name:en" => "Neva Bay",
115             "name:ru" => "Невская губа",
116             "natural" => "bay",
117             "type" => "multipolygon"
118           }
119         }
120       ]
121
122       click
123     end
124
125     within_sidebar do
126       assert_link "Neva Bay"
127       assert_link "Russia", :below => find_link("Neva Bay")
128     end
129   end
130
131   test "sorts enclosing features correctly with multiple bboxes across antimeridian" do
132     visit "/#map=15/-16.155/179.995"
133
134     within "#map" do
135       click_on "Query features"
136
137       stub_overpass :enclosing_elements => [
138         {
139           "type" => "relation",
140           "id" => 571747,
141           "bounds" => {
142             "minlat" => -21.9434274,
143             "minlon" => 174.4214965,
144             "maxlat" => -12.2613866,
145             "maxlon" => -178.0034928
146           },
147           "tags" => {
148             "admin_level" => "2",
149             "boundary" => "administrative",
150             "name" => "Viti",
151             "name:en" => "Fiji",
152             "name:fj" => "Viti",
153             "type" => "boundary"
154           }
155         },
156         {
157           "type" => "relation",
158           "id" => 2325025,
159           "bounds" => {
160             "minlat" => -17.0160140,
161             "minlon" => 178.4754914,
162             "maxlat" => -16.1243512,
163             "maxlon" => -179.6630100
164           },
165           "tags" => {
166             "name" => "Vanua Levu Group",
167             "place" => "archipelago",
168             "type" => "multipolygon",
169             "wikidata" => "Q2756586"
170           }
171         },
172         {
173           "type" => "relation",
174           "id" => 4097003,
175           "bounds" => {
176             "minlat" => -17.0160140,
177             "minlon" => 178.4754914,
178             "maxlat" => -16.1243512,
179             "maxlon" => -179.9513876
180           },
181           "tags" => {
182             "name" => "Vanua Levu",
183             "place" => "island",
184             "type" => "multipolygon",
185             "wikidata" => "Q327733"
186           }
187         }
188       ]
189
190       click
191     end
192
193     within_sidebar do
194       assert_link "Vanua Levu"
195       assert_link "Vanua Levu Group", :below => find_link("Vanua Levu")
196       assert_link "Fiji", :below => find_link("Vanua Levu Group")
197     end
198   end
199
200   private
201
202   def stub_overpass(nearby_elements: [], enclosing_elements: [])
203     execute_script <<~SCRIPT
204       {
205         const originalFetch = fetch;
206         window.fetch = (...args) => {
207           const [resource, options] = args;
208
209           if (resource != OSM.OVERPASS_URL) return originalFetch(...args);
210
211           const data = options.body.get("data"),
212                 elements = data.includes("is_in") ? #{enclosing_elements.to_json} : #{nearby_elements.to_json};
213
214           return Promise.resolve({
215             json: () => Promise.resolve({ elements })
216           });
217         }
218       }
219     SCRIPT
220   end
221 end