3 class BrowseHelperTest < ActionView::TestCase
5 include ApplicationHelper
15 def test_printable_name
16 node = create(:node, :with_history, :version => 2)
17 node_v1 = node.old_nodes.find_by(:version => 1)
18 node_v2 = node.old_nodes.find_by(:version => 2)
19 node_v1.redact!(create(:redaction))
21 add_tags_selection(node)
22 add_old_tags_selection(node_v2)
23 add_old_tags_selection(node_v1)
25 node_with_ref_without_name = create(:node)
26 create(:node_tag, :node => node_with_ref_without_name, :k => "ref", :v => "3.1415926")
28 deleted_node = create(:node, :deleted)
30 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
31 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
32 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
33 assert_dom_equal node.id.to_s, printable_name(node_v1)
34 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
35 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
36 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
40 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
41 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
42 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
43 assert_dom_equal node.id.to_s, printable_name(node_v1)
44 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
45 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
46 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
50 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
51 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
52 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
53 assert_dom_equal node.id.to_s, printable_name(node_v1)
54 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
55 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
56 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
60 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
61 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
62 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
63 assert_dom_equal node.id.to_s, printable_name(node_v1)
64 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
65 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
66 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
70 node = create(:node, :with_history, :version => 2)
71 node_v1 = node.old_nodes.find_by(:version => 1)
72 node_v2 = node.old_nodes.find_by(:version => 2)
73 node_v1.redact!(create(:redaction))
75 add_tags_selection(node)
76 add_old_tags_selection(node_v2)
77 add_old_tags_selection(node_v1)
79 assert_equal "node", link_class("node", create(:node))
80 assert_equal "node deleted", link_class("node", create(:node, :deleted))
82 assert_equal "node building yes shop gift tourism museum", link_class("node", node)
83 assert_equal "node building yes shop gift tourism museum", link_class("node", node_v2)
84 assert_equal "node deleted", link_class("node", node_v1)
88 node = create(:node, :with_history, :version => 2)
89 node_v1 = node.old_nodes.find_by(:version => 1)
90 node_v2 = node.old_nodes.find_by(:version => 2)
91 node_v1.redact!(create(:redaction))
93 add_tags_selection(node)
94 add_old_tags_selection(node_v2)
95 add_old_tags_selection(node_v1)
97 assert_equal "", link_title(create(:node))
98 assert_equal "", link_title(create(:node, :deleted))
100 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(node)
101 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(node_v2)
102 assert_equal "", link_title(node_v1)
106 html = format_key("highway")
107 assert_dom_equal "<a href=\"https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en\" title=\"The wiki description page for the highway tag\">highway</a>", html
109 html = format_key("unknown")
110 assert_dom_equal "unknown", html
113 def test_format_value
114 html = format_value("highway", "primary")
115 assert_dom_equal "<a href=\"https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en\" title=\"The wiki description page for the highway=primary tag\">primary</a>", html
117 html = format_value("highway", "unknown")
118 assert_dom_equal "unknown", html
120 html = format_value("unknown", "unknown")
121 assert_dom_equal "unknown", html
123 html = format_value("phone", "+1234567890")
124 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
126 html = format_value("wikipedia", "Test")
127 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"https://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
129 html = format_value("wikidata", "Q42")
130 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q42?uselang=en\">Q42</a>", html
132 html = format_value("operator:wikidata", "Q12;Q98")
133 assert_dom_equal "<a title=\"The Q12 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q12?uselang=en\">Q12</a>;<a title=\"The Q98 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q98?uselang=en\">Q98</a>", html
135 html = format_value("name:etymology:wikidata", "Q123")
136 assert_dom_equal "<a title=\"The Q123 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q123?uselang=en\">Q123</a>", html
140 node = create(:node, :with_history, :version => 2)
141 node_v1 = node.old_nodes.find_by(:version => 1)
142 node_v2 = node.old_nodes.find_by(:version => 2)
143 node_v1.redact!(create(:redaction))
145 add_tags_selection(node)
147 tags = icon_tags(node)
148 assert_equal 3, tags.count
149 assert tags.include?(%w[building yes])
150 assert tags.include?(%w[tourism museum])
151 assert tags.include?(%w[shop gift])
153 add_old_tags_selection(node_v2)
154 add_old_tags_selection(node_v1)
156 tags = icon_tags(node_v2)
157 assert_equal 3, tags.count
158 assert tags.include?(%w[building yes])
159 assert tags.include?(%w[tourism museum])
160 assert tags.include?(%w[shop gift])
162 tags = icon_tags(node_v1)
163 assert_equal 3, tags.count
164 assert tags.include?(%w[building yes])
165 assert tags.include?(%w[tourism museum])
166 assert tags.include?(%w[shop gift])
170 link = wiki_link("key", "highway")
171 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
173 link = wiki_link("tag", "highway=primary")
174 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
178 link = wiki_link("key", "highway")
179 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
181 link = wiki_link("tag", "highway=primary")
182 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
186 link = wiki_link("key", "highway")
187 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
189 link = wiki_link("tag", "highway=primary")
190 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
193 def test_wikidata_links
194 ### Non-prefixed wikidata-tag (only one value allowed)
197 links = wikidata_links("foo", "Test")
201 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
204 # No language-prefixes (as wikidata is multilanguage)
205 links = wikidata_links("wikidata", "en:Q1")
209 links = wikidata_links("wikidata", "1")
212 # No leading zeros allowed
213 links = wikidata_links("wikidata", "Q0123")
217 links = wikidata_links("wikidata", "Q42")
218 assert_equal 1, links.length
219 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
220 assert_equal "Q42", links[0][:title]
222 # the language of the wikidata-page should match the current locale
223 I18n.locale = "zh-CN"
224 links = wikidata_links("wikidata", "Q1234")
225 assert_equal 1, links.length
226 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
227 assert_equal "Q1234", links[0][:title]
230 ### Prefixed wikidata-tags
232 # Not anything is accepted as prefix (only limited set)
233 links = wikidata_links("anything:wikidata", "Q13")
236 # This for example is an allowed key
237 links = wikidata_links("operator:wikidata", "Q24")
238 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
239 assert_equal "Q24", links[0][:title]
241 # Another allowed key, this time with multiple values and I18n
243 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
244 assert_equal 3, links.length
245 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
246 assert_equal "Q936", links[0][:title]
247 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
248 assert_equal "Q2013", links[1][:title]
249 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
250 assert_equal "Q1568346", links[2][:title]
253 # and now with whitespaces...
254 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
255 assert_equal 4, links.length
256 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
257 assert_equal "Q6542248 ", links[0][:title]
258 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
259 assert_equal "\tQ180\n ", links[1][:title]
260 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
261 assert_equal "\rQ364\t\n\r ", links[2][:title]
262 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
263 assert_equal "\nQ4006", links[3][:title]
266 def test_wikipedia_link
267 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
270 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
273 link = wikipedia_link("wikipedia", "Test")
274 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
275 assert_equal "Test", link[:title]
277 link = wikipedia_link("wikipedia", "de:Test")
278 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
279 assert_equal "de:Test", link[:title]
281 link = wikipedia_link("wikipedia:fr", "de:Test")
282 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
283 assert_equal "de:Test", link[:title]
285 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
286 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
287 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
289 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
290 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
291 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
293 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
294 assert_equal "https://de.wikipedia.org/wiki/de:Liste der Baudenkmäler in Eichstätt?uselang=en#Br.C3.BCckenstra.C3.9Fe_1.2C_Ehemaliges_Bauernhaus", link[:url]
295 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
297 I18n.locale = "pt-BR"
299 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
300 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
301 assert_equal "zh-classical:Test#Section", link[:title]
303 link = wikipedia_link("foo", "Test")
307 def test_telephone_link
308 link = telephone_link("foo", "Test")
311 link = telephone_link("phone", "+123")
314 link = telephone_link("phone", "123")
317 link = telephone_link("phone", "123 abcdefg")
320 link = telephone_link("phone", "+1234567890 abc")
323 link = telephone_link("phone", "+1234567890; +22334455667788")
326 link = telephone_link("phone", "1234567890")
329 link = telephone_link("phone", "+1234567890")
330 assert_equal "tel:+1234567890", link
332 link = telephone_link("phone", "+1234-567-890")
333 assert_equal "tel:+1234-567-890", link
335 link = telephone_link("phone", "+1234/567/890")
336 assert_equal "tel:+1234/567/890", link
338 link = telephone_link("phone", "+1234.567.890")
339 assert_equal "tel:+1234.567.890", link
341 link = telephone_link("phone", " +1234 567-890 ")
342 assert_equal "tel:+1234567-890", link
344 link = telephone_link("phone", "+1 234-567-890")
345 assert_equal "tel:+1234-567-890", link
347 link = telephone_link("phone", "+1 (234) 567-890")
348 assert_equal "tel:+1(234)567-890", link
351 def add_old_tags_selection(old_node)
352 { "building" => "yes",
354 "tourism" => "museum",
355 "name" => "Test Node",
356 "name:pt" => "Nó teste" }.each do |key, value|
357 create(:old_node_tag, :old_node => old_node, :k => key, :v => value)
361 def add_tags_selection(node)
362 { "building" => "yes",
364 "tourism" => "museum",
365 "name" => "Test Node",
366 "name:pt" => "Nó teste" }.each do |key, value|
367 create(:node_tag, :node => node, :k => key, :v => value)