1 # -*- coding: utf-8 -*-
5 class BrowseHelperTest < ActionView::TestCase
7 include ApplicationHelper
19 def test_printable_name
20 node = create(:node, :with_history, :version => 2)
21 node_v1 = node.old_nodes.find_by(:version => 1)
22 node_v2 = node.old_nodes.find_by(:version => 2)
23 node_v1.redact!(create(:redaction))
25 add_tags_selection(node)
26 add_old_tags_selection(node_v2)
27 add_old_tags_selection(node_v1)
29 node_with_ref_without_name = create(:node)
30 create(:node_tag, :node => node_with_ref_without_name, :k => "ref", :v => "3.1415926")
32 deleted_node = create(:node, :deleted)
34 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
35 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
36 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
37 assert_dom_equal node.id.to_s, printable_name(node_v1)
38 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
39 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
40 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
44 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
45 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
46 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
47 assert_dom_equal node.id.to_s, printable_name(node_v1)
48 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
49 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
50 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
54 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
55 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
56 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
57 assert_dom_equal node.id.to_s, printable_name(node_v1)
58 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
59 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
60 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
64 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
65 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
66 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
67 assert_dom_equal node.id.to_s, printable_name(node_v1)
68 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
69 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
70 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
74 node = create(:node, :with_history, :version => 2)
75 node_v1 = node.old_nodes.find_by(:version => 1)
76 node_v2 = node.old_nodes.find_by(:version => 2)
77 node_v1.redact!(create(:redaction))
79 add_tags_selection(node)
80 add_old_tags_selection(node_v2)
81 add_old_tags_selection(node_v1)
83 assert_equal "node", link_class("node", create(:node))
84 assert_equal "node deleted", link_class("node", create(:node, :deleted))
86 assert_equal "node building yes shop gift tourism museum", link_class("node", node)
87 assert_equal "node building yes shop gift tourism museum", link_class("node", node_v2)
88 assert_equal "node deleted", link_class("node", node_v1)
92 node = create(:node, :with_history, :version => 2)
93 node_v1 = node.old_nodes.find_by(:version => 1)
94 node_v2 = node.old_nodes.find_by(:version => 2)
95 node_v1.redact!(create(:redaction))
97 add_tags_selection(node)
98 add_old_tags_selection(node_v2)
99 add_old_tags_selection(node_v1)
101 assert_equal "", link_title(create(:node))
102 assert_equal "", link_title(create(:node, :deleted))
104 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(node)
105 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(node_v2)
106 assert_equal "", link_title(node_v1)
110 html = format_key("highway")
111 assert_dom_equal "<a href=\"http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en\" title=\"The wiki description page for the highway tag\">highway</a>", html
113 html = format_key("unknown")
114 assert_dom_equal "unknown", html
117 def test_format_value
118 html = format_value("highway", "primary")
119 assert_dom_equal "<a href=\"http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en\" title=\"The wiki description page for the highway=primary tag\">primary</a>", html
121 html = format_value("highway", "unknown")
122 assert_dom_equal "unknown", html
124 html = format_value("unknown", "unknown")
125 assert_dom_equal "unknown", html
127 html = format_value("phone", "+1234567890")
128 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
130 html = format_value("wikipedia", "Test")
131 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"http://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
133 html = format_value("wikidata", "Q42")
134 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q42?uselang=en\">Q42</a>", html
136 html = format_value("operator:wikidata", "Q12;Q98")
137 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
141 node = create(:node, :with_history, :version => 2)
142 node_v1 = node.old_nodes.find_by(:version => 1)
143 node_v2 = node.old_nodes.find_by(:version => 2)
144 node_v1.redact!(create(:redaction))
146 add_tags_selection(node)
148 tags = icon_tags(node)
149 assert_equal 3, tags.count
150 assert tags.include?(%w(building yes))
151 assert tags.include?(%w(tourism museum))
152 assert tags.include?(%w(shop gift))
154 add_old_tags_selection(node_v2)
155 add_old_tags_selection(node_v1)
157 tags = icon_tags(node_v2)
158 assert_equal 3, tags.count
159 assert tags.include?(%w(building yes))
160 assert tags.include?(%w(tourism museum))
161 assert tags.include?(%w(shop gift))
163 tags = icon_tags(node_v1)
164 assert_equal 3, tags.count
165 assert tags.include?(%w(building yes))
166 assert tags.include?(%w(tourism museum))
167 assert tags.include?(%w(shop gift))
171 link = wiki_link("key", "highway")
172 assert_equal "http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
174 link = wiki_link("tag", "highway=primary")
175 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
179 link = wiki_link("key", "highway")
180 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
182 link = wiki_link("tag", "highway=primary")
183 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
187 link = wiki_link("key", "highway")
188 assert_equal "http://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
190 link = wiki_link("tag", "highway=primary")
191 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
194 def test_wikidata_links
195 ### Non-prefixed wikidata-tag (only one value allowed)
198 links = wikidata_links("foo", "Test")
202 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
205 # No language-prefixes (as wikidata is multilanguage)
206 links = wikidata_links("wikidata", "en:Q1")
210 links = wikidata_links("wikidata", "1")
213 # No leading zeros allowed
214 links = wikidata_links("wikidata", "Q0123")
218 links = wikidata_links("wikidata", "Q42")
219 assert_equal 1, links.length
220 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
221 assert_equal "Q42", links[0][:title]
223 # the language of the wikidata-page should match the current locale
224 I18n.locale = "zh-CN"
225 links = wikidata_links("wikidata", "Q1234")
226 assert_equal 1, links.length
227 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
228 assert_equal "Q1234", links[0][:title]
231 ### Prefixed wikidata-tags
233 # Not anything is accepted as prefix (only limited set)
234 links = wikidata_links("anything:wikidata", "Q13")
237 # This for example is an allowed key
238 links = wikidata_links("operator:wikidata", "Q24")
239 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
240 assert_equal "Q24", links[0][:title]
242 # Another allowed key, this time with multiple values and I18n
244 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
245 assert_equal 3, links.length
246 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
247 assert_equal "Q936", links[0][:title]
248 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
249 assert_equal "Q2013", links[1][:title]
250 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
251 assert_equal "Q1568346", links[2][:title]
254 # and now with whitespaces...
255 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
256 assert_equal 4, links.length
257 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
258 assert_equal "Q6542248 ", links[0][:title]
259 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
260 assert_equal "\tQ180\n ", links[1][:title]
261 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
262 assert_equal "\rQ364\t\n\r ", links[2][:title]
263 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
264 assert_equal "\nQ4006", links[3][:title]
267 def test_wikipedia_link
268 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
271 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
274 link = wikipedia_link("wikipedia", "Test")
275 assert_equal "http://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
276 assert_equal "Test", link[:title]
278 link = wikipedia_link("wikipedia", "de:Test")
279 assert_equal "http://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
280 assert_equal "de:Test", link[:title]
282 link = wikipedia_link("wikipedia:fr", "de:Test")
283 assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
284 assert_equal "de:Test", link[:title]
286 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
287 assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
288 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
290 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
291 assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
292 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
294 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
295 assert_equal "http://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]
296 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
298 I18n.locale = "pt-BR"
300 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
301 assert_equal "http://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
302 assert_equal "zh-classical:Test#Section", link[:title]
304 link = wikipedia_link("foo", "Test")
308 def test_telephone_link
309 link = telephone_link("foo", "Test")
312 link = telephone_link("phone", "+123")
315 link = telephone_link("phone", "123")
318 link = telephone_link("phone", "123 abcdefg")
321 link = telephone_link("phone", "+1234567890 abc")
324 link = telephone_link("phone", "+1234567890; +22334455667788")
327 link = telephone_link("phone", "1234567890")
330 link = telephone_link("phone", "+1234567890")
331 assert_equal "tel:+1234567890", link
333 link = telephone_link("phone", "+1234-567-890")
334 assert_equal "tel:+1234-567-890", link
336 link = telephone_link("phone", "+1234/567/890")
337 assert_equal "tel:+1234/567/890", link
339 link = telephone_link("phone", "+1234.567.890")
340 assert_equal "tel:+1234.567.890", link
342 link = telephone_link("phone", " +1234 567-890 ")
343 assert_equal "tel:+1234567-890", link
345 link = telephone_link("phone", "+1 234-567-890")
346 assert_equal "tel:+1234-567-890", link
348 link = telephone_link("phone", "+1 (234) 567-890")
349 assert_equal "tel:+1(234)567-890", link
352 def add_old_tags_selection(old_node)
353 { "building" => "yes",
355 "tourism" => "museum",
356 "name" => "Test Node",
357 "name:pt" => "Nó teste" }.each do |key, value|
358 create(:old_node_tag, :old_node => old_node, :k => key, :v => value)
362 def add_tags_selection(node)
363 { "building" => "yes",
365 "tourism" => "museum",
366 "name" => "Test Node",
367 "name:pt" => "Nó teste" }.each do |key, value|
368 create(:node_tag, :node => node, :k => key, :v => value)