1 # -*- coding: utf-8 -*-
5 class BrowseHelperTest < ActionView::TestCase
7 include ApplicationHelper
19 def test_printable_name
20 add_tags_selection(current_nodes(:node_with_name))
21 create(:node_tag, :node => current_nodes(:node_with_ref_without_name), :k => "ref", :v => "3.1415926")
22 add_old_tags_selection(nodes(:node_with_name_current_version))
23 add_old_tags_selection(nodes(:node_with_name_redacted_version))
25 # current_nodes(:redacted_node) is deleted, so has no tags.
26 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
27 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
28 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
29 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
30 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
31 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
32 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
36 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
37 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
38 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
39 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
40 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
41 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
42 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
46 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
47 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
48 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
49 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
50 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
51 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
52 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
56 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
57 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
58 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
59 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
60 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
61 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
62 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
66 add_tags_selection(current_nodes(:node_with_name))
68 assert_equal "node", link_class("node", current_nodes(:visible_node))
69 assert_equal "node deleted", link_class("node", current_nodes(:invisible_node))
70 assert_equal "node deleted", link_class("node", current_nodes(:redacted_node))
71 assert_equal "node building yes shop gift tourism museum", link_class("node", current_nodes(:node_with_name))
73 add_old_tags_selection(nodes(:node_with_name_current_version))
74 add_old_tags_selection(nodes(:node_with_name_redacted_version))
75 assert_equal "node building yes shop gift tourism museum", link_class("node", nodes(:node_with_name_current_version))
76 assert_equal "node deleted", link_class("node", nodes(:node_with_name_redacted_version))
80 add_tags_selection(current_nodes(:node_with_name))
82 assert_equal "", link_title(current_nodes(:visible_node))
83 assert_equal "", link_title(current_nodes(:invisible_node))
84 assert_equal "", link_title(current_nodes(:redacted_node))
85 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(current_nodes(:node_with_name))
87 add_old_tags_selection(nodes(:node_with_name_current_version))
88 add_old_tags_selection(nodes(:node_with_name_redacted_version))
89 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(nodes(:node_with_name_current_version))
90 assert_equal "", link_title(nodes(:node_with_name_redacted_version))
94 html = format_key("highway")
95 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
97 html = format_key("unknown")
98 assert_dom_equal "unknown", html
101 def test_format_value
102 html = format_value("highway", "primary")
103 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
105 html = format_value("highway", "unknown")
106 assert_dom_equal "unknown", html
108 html = format_value("unknown", "unknown")
109 assert_dom_equal "unknown", html
111 html = format_value("phone", "+1234567890")
112 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
114 html = format_value("wikipedia", "Test")
115 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"http://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
117 html = format_value("wikidata", "Q42")
118 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q42?uselang=en\">Q42</a>", html
120 html = format_value("operator:wikidata", "Q12;Q98")
121 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
125 add_tags_selection(current_nodes(:node_with_name))
127 tags = icon_tags(current_nodes(:node_with_name))
128 assert_equal 3, tags.count
129 assert tags.include?(%w(building yes))
130 assert tags.include?(%w(tourism museum))
131 assert tags.include?(%w(shop gift))
133 add_old_tags_selection(nodes(:node_with_name_current_version))
134 add_old_tags_selection(nodes(:node_with_name_redacted_version))
136 tags = icon_tags(nodes(:node_with_name_current_version))
137 assert_equal 3, tags.count
138 assert tags.include?(%w(building yes))
139 assert tags.include?(%w(tourism museum))
140 assert tags.include?(%w(shop gift))
142 tags = icon_tags(nodes(:node_with_name_redacted_version))
143 assert_equal 3, tags.count
144 assert tags.include?(%w(building yes))
145 assert tags.include?(%w(tourism museum))
146 assert tags.include?(%w(shop gift))
150 link = wiki_link("key", "highway")
151 assert_equal "http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
153 link = wiki_link("tag", "highway=primary")
154 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
158 link = wiki_link("key", "highway")
159 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
161 link = wiki_link("tag", "highway=primary")
162 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
166 link = wiki_link("key", "highway")
167 assert_equal "http://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
169 link = wiki_link("tag", "highway=primary")
170 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
173 def test_wikidata_links
174 ### Non-prefixed wikidata-tag (only one value allowed)
177 links = wikidata_links("foo", "Test")
181 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
184 # No language-prefixes (as wikidata is multilanguage)
185 links = wikidata_links("wikidata", "en:Q1")
189 links = wikidata_links("wikidata", "1")
192 # No leading zeros allowed
193 links = wikidata_links("wikidata", "Q0123")
197 links = wikidata_links("wikidata", "Q42")
198 assert_equal 1, links.length
199 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
200 assert_equal "Q42", links[0][:title]
202 # the language of the wikidata-page should match the current locale
203 I18n.locale = "zh-CN"
204 links = wikidata_links("wikidata", "Q1234")
205 assert_equal 1, links.length
206 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
207 assert_equal "Q1234", links[0][:title]
210 ### Prefixed wikidata-tags
212 # Not anything is accepted as prefix (only limited set)
213 links = wikidata_links("anything:wikidata", "Q13")
216 # This for example is an allowed key
217 links = wikidata_links("operator:wikidata", "Q24")
218 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
219 assert_equal "Q24", links[0][:title]
221 # Another allowed key, this time with multiple values and I18n
223 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
224 assert_equal 3, links.length
225 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
226 assert_equal "Q936", links[0][:title]
227 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
228 assert_equal "Q2013", links[1][:title]
229 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
230 assert_equal "Q1568346", links[2][:title]
233 # and now with whitespaces...
234 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
235 assert_equal 4, links.length
236 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
237 assert_equal "Q6542248 ", links[0][:title]
238 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
239 assert_equal "\tQ180\n ", links[1][:title]
240 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
241 assert_equal "\rQ364\t\n\r ", links[2][:title]
242 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
243 assert_equal "\nQ4006", links[3][:title]
246 def test_wikipedia_link
247 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
250 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
253 link = wikipedia_link("wikipedia", "Test")
254 assert_equal "http://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
255 assert_equal "Test", link[:title]
257 link = wikipedia_link("wikipedia", "de:Test")
258 assert_equal "http://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
259 assert_equal "de:Test", link[:title]
261 link = wikipedia_link("wikipedia:fr", "de:Test")
262 assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
263 assert_equal "de:Test", link[:title]
265 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
266 assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
267 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
269 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
270 assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
271 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
273 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
274 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]
275 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
277 I18n.locale = "pt-BR"
279 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
280 assert_equal "http://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
281 assert_equal "zh-classical:Test#Section", link[:title]
283 link = wikipedia_link("foo", "Test")
287 def test_telephone_link
288 link = telephone_link("foo", "Test")
291 link = telephone_link("phone", "+123")
294 link = telephone_link("phone", "123")
297 link = telephone_link("phone", "123 abcdefg")
300 link = telephone_link("phone", "+1234567890 abc")
303 link = telephone_link("phone", "+1234567890; +22334455667788")
306 link = telephone_link("phone", "1234567890")
309 link = telephone_link("phone", "+1234567890")
310 assert_equal "tel:+1234567890", link
312 link = telephone_link("phone", "+1234-567-890")
313 assert_equal "tel:+1234-567-890", link
315 link = telephone_link("phone", "+1234/567/890")
316 assert_equal "tel:+1234/567/890", link
318 link = telephone_link("phone", "+1234.567.890")
319 assert_equal "tel:+1234.567.890", link
321 link = telephone_link("phone", " +1234 567-890 ")
322 assert_equal "tel:+1234567-890", link
324 link = telephone_link("phone", "+1 234-567-890")
325 assert_equal "tel:+1234-567-890", link
327 link = telephone_link("phone", "+1 (234) 567-890")
328 assert_equal "tel:+1(234)567-890", link
331 def add_old_tags_selection(old_node)
332 { "building" => "yes",
334 "tourism" => "museum",
335 "name" => "Test Node",
336 "name:pt" => "Nó teste" }.each do |key, value|
337 create(:old_node_tag, :old_node => old_node, :k => key, :v => value)
341 def add_tags_selection(node)
342 { "building" => "yes",
344 "tourism" => "museum",
345 "name" => "Test Node",
346 "name:pt" => "Nó teste" }.each do |key, value|
347 create(:node_tag, :node => node, :k => key, :v => value)