1 # -*- coding: utf-8 -*-
5 class BrowseHelperTest < ActionView::TestCase
7 include ApplicationHelper
19 def test_printable_name
20 add_old_tags_selection(nodes(:node_with_name_current_version))
21 add_old_tags_selection(nodes(:node_with_name_redacted_version))
22 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
23 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
24 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
25 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
26 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
27 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
28 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
32 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
33 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
34 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
35 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
36 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
37 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
38 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
42 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
43 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
44 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
45 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
46 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
47 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
48 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
52 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
53 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
54 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
55 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
56 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
57 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
58 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
62 assert_equal "node", link_class("node", current_nodes(:visible_node))
63 assert_equal "node deleted", link_class("node", current_nodes(:invisible_node))
64 assert_equal "node deleted", link_class("node", current_nodes(:redacted_node))
65 assert_equal "node building yes shop gift tourism museum", link_class("node", current_nodes(:node_with_name))
67 add_old_tags_selection(nodes(:node_with_name_current_version))
68 add_old_tags_selection(nodes(:node_with_name_redacted_version))
69 assert_equal "node building yes shop gift tourism museum", link_class("node", nodes(:node_with_name_current_version))
70 assert_equal "node deleted", link_class("node", nodes(:node_with_name_redacted_version))
74 assert_equal "", link_title(current_nodes(:visible_node))
75 assert_equal "", link_title(current_nodes(:invisible_node))
76 assert_equal "", link_title(current_nodes(:redacted_node))
77 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(current_nodes(:node_with_name))
79 add_old_tags_selection(nodes(:node_with_name_current_version))
80 add_old_tags_selection(nodes(:node_with_name_redacted_version))
81 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(nodes(:node_with_name_current_version))
82 assert_equal "", link_title(nodes(:node_with_name_redacted_version))
86 html = format_key("highway")
87 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
89 html = format_key("unknown")
90 assert_dom_equal "unknown", html
94 html = format_value("highway", "primary")
95 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
97 html = format_value("highway", "unknown")
98 assert_dom_equal "unknown", html
100 html = format_value("unknown", "unknown")
101 assert_dom_equal "unknown", html
103 html = format_value("phone", "+1234567890")
104 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
106 html = format_value("wikipedia", "Test")
107 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"http://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
109 html = format_value("wikidata", "Q42")
110 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q42?uselang=en\">Q42</a>", html
112 html = format_value("operator:wikidata", "Q12;Q98")
113 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
117 tags = icon_tags(current_nodes(:node_with_name))
118 assert_equal 3, tags.count
119 assert tags.include?(%w(building yes))
120 assert tags.include?(%w(tourism museum))
121 assert tags.include?(%w(shop gift))
123 add_old_tags_selection(nodes(:node_with_name_current_version))
124 add_old_tags_selection(nodes(:node_with_name_redacted_version))
126 tags = icon_tags(nodes(:node_with_name_current_version))
127 assert_equal 3, tags.count
128 assert tags.include?(%w(building yes))
129 assert tags.include?(%w(tourism museum))
130 assert tags.include?(%w(shop gift))
132 tags = icon_tags(nodes(:node_with_name_redacted_version))
133 assert_equal 3, tags.count
134 assert tags.include?(%w(building yes))
135 assert tags.include?(%w(tourism museum))
136 assert tags.include?(%w(shop gift))
140 link = wiki_link("key", "highway")
141 assert_equal "http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
143 link = wiki_link("tag", "highway=primary")
144 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
148 link = wiki_link("key", "highway")
149 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
151 link = wiki_link("tag", "highway=primary")
152 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
156 link = wiki_link("key", "highway")
157 assert_equal "http://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
159 link = wiki_link("tag", "highway=primary")
160 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
163 def test_wikidata_links
164 ### Non-prefixed wikidata-tag (only one value allowed)
167 links = wikidata_links("foo", "Test")
171 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
174 # No language-prefixes (as wikidata is multilanguage)
175 links = wikidata_links("wikidata", "en:Q1")
179 links = wikidata_links("wikidata", "1")
182 # No leading zeros allowed
183 links = wikidata_links("wikidata", "Q0123")
187 links = wikidata_links("wikidata", "Q42")
188 assert_equal 1, links.length
189 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
190 assert_equal "Q42", links[0][:title]
192 # the language of the wikidata-page should match the current locale
193 I18n.locale = "zh-CN"
194 links = wikidata_links("wikidata", "Q1234")
195 assert_equal 1, links.length
196 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
197 assert_equal "Q1234", links[0][:title]
200 ### Prefixed wikidata-tags
202 # Not anything is accepted as prefix (only limited set)
203 links = wikidata_links("anything:wikidata", "Q13")
206 # This for example is an allowed key
207 links = wikidata_links("operator:wikidata", "Q24")
208 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
209 assert_equal "Q24", links[0][:title]
211 # Another allowed key, this time with multiple values and I18n
213 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
214 assert_equal 3, links.length
215 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
216 assert_equal "Q936", links[0][:title]
217 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
218 assert_equal "Q2013", links[1][:title]
219 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
220 assert_equal "Q1568346", links[2][:title]
223 # and now with whitespaces...
224 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
225 assert_equal 4, links.length
226 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
227 assert_equal "Q6542248 ", links[0][:title]
228 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
229 assert_equal "\tQ180\n ", links[1][:title]
230 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
231 assert_equal "\rQ364\t\n\r ", links[2][:title]
232 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
233 assert_equal "\nQ4006", links[3][:title]
236 def test_wikipedia_link
237 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
240 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
243 link = wikipedia_link("wikipedia", "Test")
244 assert_equal "http://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
245 assert_equal "Test", link[:title]
247 link = wikipedia_link("wikipedia", "de:Test")
248 assert_equal "http://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
249 assert_equal "de:Test", link[:title]
251 link = wikipedia_link("wikipedia:fr", "de:Test")
252 assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
253 assert_equal "de:Test", link[:title]
255 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
256 assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
257 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
259 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
260 assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
261 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
263 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
264 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]
265 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
267 I18n.locale = "pt-BR"
269 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
270 assert_equal "http://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
271 assert_equal "zh-classical:Test#Section", link[:title]
273 link = wikipedia_link("foo", "Test")
277 def test_telephone_link
278 link = telephone_link("foo", "Test")
281 link = telephone_link("phone", "+123")
284 link = telephone_link("phone", "123")
287 link = telephone_link("phone", "123 abcdefg")
290 link = telephone_link("phone", "+1234567890 abc")
293 link = telephone_link("phone", "+1234567890; +22334455667788")
296 link = telephone_link("phone", "1234567890")
299 link = telephone_link("phone", "+1234567890")
300 assert_equal "tel:+1234567890", link
302 link = telephone_link("phone", "+1234-567-890")
303 assert_equal "tel:+1234-567-890", link
305 link = telephone_link("phone", "+1234/567/890")
306 assert_equal "tel:+1234/567/890", link
308 link = telephone_link("phone", "+1234.567.890")
309 assert_equal "tel:+1234.567.890", link
311 link = telephone_link("phone", " +1234 567-890 ")
312 assert_equal "tel:+1234567-890", link
314 link = telephone_link("phone", "+1 234-567-890")
315 assert_equal "tel:+1234-567-890", link
317 link = telephone_link("phone", "+1 (234) 567-890")
318 assert_equal "tel:+1(234)567-890", link
321 def add_old_tags_selection(old_node)
322 { "building" => "yes",
324 "tourism" => "museum",
325 "name" => "Test Node",
326 "name:pt" => "Nó teste" }.each do |key, value|
327 create(:old_node_tag, :old_node => old_node, :k => key, :v => value)