1 # -*- coding: utf-8 -*-
5 class BrowseHelperTest < ActionView::TestCase
7 include ApplicationHelper
17 def test_printable_name
18 node = create(:node, :with_history, :version => 2)
19 node_v1 = node.old_nodes.find_by(:version => 1)
20 node_v2 = node.old_nodes.find_by(:version => 2)
21 node_v1.redact!(create(:redaction))
23 add_tags_selection(node)
24 add_old_tags_selection(node_v2)
25 add_old_tags_selection(node_v1)
27 node_with_ref_without_name = create(:node)
28 create(:node_tag, :node => node_with_ref_without_name, :k => "ref", :v => "3.1415926")
30 deleted_node = create(:node, :deleted)
32 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
33 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
34 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
35 assert_dom_equal node.id.to_s, printable_name(node_v1)
36 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
37 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
38 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
42 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
43 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
44 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
45 assert_dom_equal node.id.to_s, printable_name(node_v1)
46 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
47 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
48 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
52 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
53 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
54 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
55 assert_dom_equal node.id.to_s, printable_name(node_v1)
56 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
57 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
58 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
62 assert_dom_equal deleted_node.id.to_s, printable_name(deleted_node)
63 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node)
64 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}</bdi>)", printable_name(node_v2)
65 assert_dom_equal node.id.to_s, printable_name(node_v1)
66 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>#{node.id}, v2</bdi>)", printable_name(node_v2, true)
67 assert_dom_equal "#{node.id}, v1", printable_name(node_v1, true)
68 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>#{node_with_ref_without_name.id}</bdi>)", printable_name(node_with_ref_without_name)
72 node = create(:node, :with_history, :version => 2)
73 node_v1 = node.old_nodes.find_by(:version => 1)
74 node_v2 = node.old_nodes.find_by(:version => 2)
75 node_v1.redact!(create(:redaction))
77 add_tags_selection(node)
78 add_old_tags_selection(node_v2)
79 add_old_tags_selection(node_v1)
81 assert_equal "node", link_class("node", create(:node))
82 assert_equal "node deleted", link_class("node", create(:node, :deleted))
84 assert_equal "node building yes shop gift tourism museum", link_class("node", node)
85 assert_equal "node building yes shop gift tourism museum", link_class("node", node_v2)
86 assert_equal "node deleted", link_class("node", node_v1)
90 node = create(:node, :with_history, :version => 2)
91 node_v1 = node.old_nodes.find_by(:version => 1)
92 node_v2 = node.old_nodes.find_by(:version => 2)
93 node_v1.redact!(create(:redaction))
95 add_tags_selection(node)
96 add_old_tags_selection(node_v2)
97 add_old_tags_selection(node_v1)
99 assert_equal "", link_title(create(:node))
100 assert_equal "", link_title(create(:node, :deleted))
102 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(node)
103 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(node_v2)
104 assert_equal "", link_title(node_v1)
108 html = format_key("highway")
109 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
111 html = format_key("unknown")
112 assert_dom_equal "unknown", html
115 def test_format_value
116 html = format_value("highway", "primary")
117 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
119 html = format_value("highway", "unknown")
120 assert_dom_equal "unknown", html
122 html = format_value("unknown", "unknown")
123 assert_dom_equal "unknown", html
125 html = format_value("phone", "+1234567890")
126 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
128 html = format_value("wikipedia", "Test")
129 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"http://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
131 html = format_value("wikidata", "Q42")
132 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q42?uselang=en\">Q42</a>", html
134 html = format_value("operator:wikidata", "Q12;Q98")
135 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
139 node = create(:node, :with_history, :version => 2)
140 node_v1 = node.old_nodes.find_by(:version => 1)
141 node_v2 = node.old_nodes.find_by(:version => 2)
142 node_v1.redact!(create(:redaction))
144 add_tags_selection(node)
146 tags = icon_tags(node)
147 assert_equal 3, tags.count
148 assert tags.include?(%w[building yes])
149 assert tags.include?(%w[tourism museum])
150 assert tags.include?(%w[shop gift])
152 add_old_tags_selection(node_v2)
153 add_old_tags_selection(node_v1)
155 tags = icon_tags(node_v2)
156 assert_equal 3, tags.count
157 assert tags.include?(%w[building yes])
158 assert tags.include?(%w[tourism museum])
159 assert tags.include?(%w[shop gift])
161 tags = icon_tags(node_v1)
162 assert_equal 3, tags.count
163 assert tags.include?(%w[building yes])
164 assert tags.include?(%w[tourism museum])
165 assert tags.include?(%w[shop gift])
169 link = wiki_link("key", "highway")
170 assert_equal "http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
172 link = wiki_link("tag", "highway=primary")
173 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
177 link = wiki_link("key", "highway")
178 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
180 link = wiki_link("tag", "highway=primary")
181 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
185 link = wiki_link("key", "highway")
186 assert_equal "http://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
188 link = wiki_link("tag", "highway=primary")
189 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
192 def test_wikidata_links
193 ### Non-prefixed wikidata-tag (only one value allowed)
196 links = wikidata_links("foo", "Test")
200 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
203 # No language-prefixes (as wikidata is multilanguage)
204 links = wikidata_links("wikidata", "en:Q1")
208 links = wikidata_links("wikidata", "1")
211 # No leading zeros allowed
212 links = wikidata_links("wikidata", "Q0123")
216 links = wikidata_links("wikidata", "Q42")
217 assert_equal 1, links.length
218 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
219 assert_equal "Q42", links[0][:title]
221 # the language of the wikidata-page should match the current locale
222 I18n.locale = "zh-CN"
223 links = wikidata_links("wikidata", "Q1234")
224 assert_equal 1, links.length
225 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
226 assert_equal "Q1234", links[0][:title]
229 ### Prefixed wikidata-tags
231 # Not anything is accepted as prefix (only limited set)
232 links = wikidata_links("anything:wikidata", "Q13")
235 # This for example is an allowed key
236 links = wikidata_links("operator:wikidata", "Q24")
237 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
238 assert_equal "Q24", links[0][:title]
240 # Another allowed key, this time with multiple values and I18n
242 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
243 assert_equal 3, links.length
244 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
245 assert_equal "Q936", links[0][:title]
246 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
247 assert_equal "Q2013", links[1][:title]
248 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
249 assert_equal "Q1568346", links[2][:title]
252 # and now with whitespaces...
253 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
254 assert_equal 4, links.length
255 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
256 assert_equal "Q6542248 ", links[0][:title]
257 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
258 assert_equal "\tQ180\n ", links[1][:title]
259 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
260 assert_equal "\rQ364\t\n\r ", links[2][:title]
261 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
262 assert_equal "\nQ4006", links[3][:title]
265 def test_wikipedia_link
266 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
269 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
272 link = wikipedia_link("wikipedia", "Test")
273 assert_equal "http://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
274 assert_equal "Test", link[:title]
276 link = wikipedia_link("wikipedia", "de:Test")
277 assert_equal "http://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
278 assert_equal "de:Test", link[:title]
280 link = wikipedia_link("wikipedia:fr", "de:Test")
281 assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
282 assert_equal "de:Test", link[:title]
284 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
285 assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
286 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
288 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
289 assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
290 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
292 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
293 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]
294 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
296 I18n.locale = "pt-BR"
298 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
299 assert_equal "http://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
300 assert_equal "zh-classical:Test#Section", link[:title]
302 link = wikipedia_link("foo", "Test")
306 def test_telephone_link
307 link = telephone_link("foo", "Test")
310 link = telephone_link("phone", "+123")
313 link = telephone_link("phone", "123")
316 link = telephone_link("phone", "123 abcdefg")
319 link = telephone_link("phone", "+1234567890 abc")
322 link = telephone_link("phone", "+1234567890; +22334455667788")
325 link = telephone_link("phone", "1234567890")
328 link = telephone_link("phone", "+1234567890")
329 assert_equal "tel:+1234567890", link
331 link = telephone_link("phone", "+1234-567-890")
332 assert_equal "tel:+1234-567-890", link
334 link = telephone_link("phone", "+1234/567/890")
335 assert_equal "tel:+1234/567/890", link
337 link = telephone_link("phone", "+1234.567.890")
338 assert_equal "tel:+1234.567.890", link
340 link = telephone_link("phone", " +1234 567-890 ")
341 assert_equal "tel:+1234567-890", link
343 link = telephone_link("phone", "+1 234-567-890")
344 assert_equal "tel:+1234-567-890", link
346 link = telephone_link("phone", "+1 (234) 567-890")
347 assert_equal "tel:+1(234)567-890", link
350 def add_old_tags_selection(old_node)
351 { "building" => "yes",
353 "tourism" => "museum",
354 "name" => "Test Node",
355 "name:pt" => "Nó teste" }.each do |key, value|
356 create(:old_node_tag, :old_node => old_node, :k => key, :v => value)
360 def add_tags_selection(node)
361 { "building" => "yes",
363 "tourism" => "museum",
364 "name" => "Test Node",
365 "name:pt" => "Nó teste" }.each do |key, value|
366 create(:node_tag, :node => node, :k => key, :v => value)