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
138 html = format_value("colour", "#f00")
139 assert_dom_equal %( <div class="colour-preview-box" style="background-color:#f00" title="Colour #f00 preview"></div>#f00 ), html
143 node = create(:node, :with_history, :version => 2)
144 node_v1 = node.old_nodes.find_by(:version => 1)
145 node_v2 = node.old_nodes.find_by(:version => 2)
146 node_v1.redact!(create(:redaction))
148 add_tags_selection(node)
150 tags = icon_tags(node)
151 assert_equal 3, tags.count
152 assert tags.include?(%w[building yes])
153 assert tags.include?(%w[tourism museum])
154 assert tags.include?(%w[shop gift])
156 add_old_tags_selection(node_v2)
157 add_old_tags_selection(node_v1)
159 tags = icon_tags(node_v2)
160 assert_equal 3, tags.count
161 assert tags.include?(%w[building yes])
162 assert tags.include?(%w[tourism museum])
163 assert tags.include?(%w[shop gift])
165 tags = icon_tags(node_v1)
166 assert_equal 3, tags.count
167 assert tags.include?(%w[building yes])
168 assert tags.include?(%w[tourism museum])
169 assert tags.include?(%w[shop gift])
173 link = wiki_link("key", "highway")
174 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
176 link = wiki_link("tag", "highway=primary")
177 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
181 link = wiki_link("key", "highway")
182 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
184 link = wiki_link("tag", "highway=primary")
185 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
189 link = wiki_link("key", "highway")
190 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
192 link = wiki_link("tag", "highway=primary")
193 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
196 def test_wikidata_links
197 ### Non-prefixed wikidata-tag (only one value allowed)
200 links = wikidata_links("foo", "Test")
204 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
207 # No language-prefixes (as wikidata is multilanguage)
208 links = wikidata_links("wikidata", "en:Q1")
212 links = wikidata_links("wikidata", "1")
215 # No leading zeros allowed
216 links = wikidata_links("wikidata", "Q0123")
220 links = wikidata_links("wikidata", "Q42")
221 assert_equal 1, links.length
222 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
223 assert_equal "Q42", links[0][:title]
225 # the language of the wikidata-page should match the current locale
226 I18n.locale = "zh-CN"
227 links = wikidata_links("wikidata", "Q1234")
228 assert_equal 1, links.length
229 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
230 assert_equal "Q1234", links[0][:title]
233 ### Prefixed wikidata-tags
235 # Not anything is accepted as prefix (only limited set)
236 links = wikidata_links("anything:wikidata", "Q13")
239 # This for example is an allowed key
240 links = wikidata_links("operator:wikidata", "Q24")
241 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
242 assert_equal "Q24", links[0][:title]
244 # Another allowed key, this time with multiple values and I18n
246 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
247 assert_equal 3, links.length
248 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
249 assert_equal "Q936", links[0][:title]
250 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
251 assert_equal "Q2013", links[1][:title]
252 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
253 assert_equal "Q1568346", links[2][:title]
256 # and now with whitespaces...
257 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
258 assert_equal 4, links.length
259 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
260 assert_equal "Q6542248 ", links[0][:title]
261 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
262 assert_equal "\tQ180\n ", links[1][:title]
263 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
264 assert_equal "\rQ364\t\n\r ", links[2][:title]
265 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
266 assert_equal "\nQ4006", links[3][:title]
269 def test_wikipedia_link
270 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
273 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
276 link = wikipedia_link("wikipedia", "Test")
277 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
278 assert_equal "Test", link[:title]
280 link = wikipedia_link("wikipedia", "de:Test")
281 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
282 assert_equal "de:Test", link[:title]
284 link = wikipedia_link("wikipedia:fr", "de:Test")
285 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
286 assert_equal "de:Test", link[:title]
288 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
289 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
290 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
292 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
293 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
294 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
296 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
297 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]
298 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
300 I18n.locale = "pt-BR"
302 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
303 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
304 assert_equal "zh-classical:Test#Section", link[:title]
306 link = wikipedia_link("foo", "Test")
310 def test_telephone_link
311 link = telephone_link("foo", "Test")
314 link = telephone_link("phone", "+123")
317 link = telephone_link("phone", "123")
320 link = telephone_link("phone", "123 abcdefg")
323 link = telephone_link("phone", "+1234567890 abc")
326 link = telephone_link("phone", "+1234567890; +22334455667788")
329 link = telephone_link("phone", "1234567890")
332 link = telephone_link("phone", "+1234567890")
333 assert_equal "tel:+1234567890", 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:+1234.567.890", link
344 link = telephone_link("phone", " +1234 567-890 ")
345 assert_equal "tel:+1234567-890", link
347 link = telephone_link("phone", "+1 234-567-890")
348 assert_equal "tel:+1234-567-890", link
350 link = telephone_link("phone", "+1 (234) 567-890")
351 assert_equal "tel:+1(234)567-890", link
354 def test_colour_preview
355 # basic positive tests
356 colour = colour_preview("colour", "red")
357 assert_equal "red", colour
359 colour = colour_preview("colour", "Red")
360 assert_equal "Red", colour
362 colour = colour_preview("colour", "darkRed")
363 assert_equal "darkRed", colour
365 colour = colour_preview("colour", "#f00")
366 assert_equal "#f00", colour
368 colour = colour_preview("colour", "#fF0000")
369 assert_equal "#fF0000", colour
371 # other tag variants:
372 colour = colour_preview("building:colour", "#f00")
373 assert_equal "#f00", colour
375 colour = colour_preview("ref:colour", "#f00")
376 assert_equal "#f00", colour
378 colour = colour_preview("roof:colour", "#f00")
379 assert_equal "#f00", colour
382 colour = colour_preview("color", "")
385 colour = colour_preview("color", " ")
388 colour = colour_preview("color", nil)
391 # ignore US spelling variant
392 colour = colour_preview("color", "red")
395 # irrelevant tag names
396 colour = colour_preview("building", "red")
400 colour = colour_preview("colour", "#")
403 colour = colour_preview("colour", "#ff")
406 colour = colour_preview("colour", "#ffff")
409 colour = colour_preview("colour", "#fffffff")
412 colour = colour_preview("colour", "#ggg")
415 colour = colour_preview("colour", "#ff 00 00")
418 # invalid w3c color names:
419 colour = colour_preview("colour", "r")
422 colour = colour_preview("colour", "ffffff")
425 colour = colour_preview("colour", "f00")
428 colour = colour_preview("colour", "xxxred")
431 colour = colour_preview("colour", "dark red")
434 colour = colour_preview("colour", "dark_red")
437 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")
441 def add_old_tags_selection(old_node)
442 { "building" => "yes",
444 "tourism" => "museum",
445 "name" => "Test Node",
446 "name:pt" => "Nó teste" }.each do |key, value|
447 create(:old_node_tag, :old_node => old_node, :k => key, :v => value)
451 def add_tags_selection(node)
452 { "building" => "yes",
454 "tourism" => "museum",
455 "name" => "Test Node",
456 "name:pt" => "Nó teste" }.each do |key, value|
457 create(:node_tag, :node => node, :k => key, :v => value)