1 # -*- coding: utf-8 -*-
5 class BrowseHelperTest < ActionView::TestCase
7 include ApplicationHelper
19 def test_printable_name
20 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
21 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
22 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
23 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
24 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
25 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
26 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
30 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
31 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
32 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
33 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
34 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
35 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
36 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
40 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
41 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
42 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
43 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
44 assert_dom_equal "<bdi>Nó teste</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
45 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
46 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
50 assert_dom_equal "17", printable_name(current_nodes(:redacted_node))
51 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(current_nodes(:node_with_name))
52 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18</bdi>)", printable_name(nodes(:node_with_name_current_version))
53 assert_dom_equal "18", printable_name(nodes(:node_with_name_redacted_version))
54 assert_dom_equal "<bdi>Test Node</bdi> (<bdi>18, v2</bdi>)", printable_name(nodes(:node_with_name_current_version), true)
55 assert_dom_equal "18, v1", printable_name(nodes(:node_with_name_redacted_version), true)
56 assert_dom_equal "<bdi>3.1415926</bdi> (<bdi>19</bdi>)", printable_name(current_nodes(:node_with_ref_without_name))
60 assert_equal "node", link_class("node", current_nodes(:visible_node))
61 assert_equal "node deleted", link_class("node", current_nodes(:invisible_node))
62 assert_equal "node deleted", link_class("node", current_nodes(:redacted_node))
63 assert_equal "node building yes shop gift tourism museum", link_class("node", current_nodes(:node_with_name))
64 assert_equal "node building yes shop gift tourism museum", link_class("node", nodes(:node_with_name_current_version))
65 assert_equal "node deleted", link_class("node", nodes(:node_with_name_redacted_version))
69 assert_equal "", link_title(current_nodes(:visible_node))
70 assert_equal "", link_title(current_nodes(:invisible_node))
71 assert_equal "", link_title(current_nodes(:redacted_node))
72 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(current_nodes(:node_with_name))
73 assert_equal "building=yes, shop=gift, and tourism=museum", link_title(nodes(:node_with_name_current_version))
74 assert_equal "", link_title(nodes(:node_with_name_redacted_version))
78 html = format_key("highway")
79 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
81 html = format_key("unknown")
82 assert_dom_equal "unknown", html
86 html = format_value("highway", "primary")
87 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
89 html = format_value("highway", "unknown")
90 assert_dom_equal "unknown", html
92 html = format_value("unknown", "unknown")
93 assert_dom_equal "unknown", html
95 html = format_value("phone", "+1234567890")
96 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
98 html = format_value("wikipedia", "Test")
99 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"http://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
101 html = format_value("wikidata", "Q42")
102 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/wiki/Q42?uselang=en\">Q42</a>", html
104 html = format_value("operator:wikidata", "Q12;Q98")
105 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
109 tags = icon_tags(current_nodes(:node_with_name))
110 assert_equal 3, tags.count
111 assert tags.include?(%w(building yes))
112 assert tags.include?(%w(tourism museum))
113 assert tags.include?(%w(shop gift))
115 tags = icon_tags(nodes(:node_with_name_current_version))
116 assert_equal 3, tags.count
117 assert tags.include?(%w(building yes))
118 assert tags.include?(%w(tourism museum))
119 assert tags.include?(%w(shop gift))
121 tags = icon_tags(nodes(:node_with_name_redacted_version))
122 assert_equal 3, tags.count
123 assert tags.include?(%w(building yes))
124 assert tags.include?(%w(tourism museum))
125 assert tags.include?(%w(shop gift))
129 link = wiki_link("key", "highway")
130 assert_equal "http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
132 link = wiki_link("tag", "highway=primary")
133 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
137 link = wiki_link("key", "highway")
138 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
140 link = wiki_link("tag", "highway=primary")
141 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
145 link = wiki_link("key", "highway")
146 assert_equal "http://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
148 link = wiki_link("tag", "highway=primary")
149 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
152 def test_wikidata_links
153 ### Non-prefixed wikidata-tag (only one value allowed)
156 links = wikidata_links("foo", "Test")
160 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
163 # No language-prefixes (as wikidata is multilanguage)
164 links = wikidata_links("wikidata", "en:Q1")
168 links = wikidata_links("wikidata", "1")
171 # No leading zeros allowed
172 links = wikidata_links("wikidata", "Q0123")
176 links = wikidata_links("wikidata", "Q42")
177 assert_equal 1, links.length
178 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
179 assert_equal "Q42", links[0][:title]
181 # the language of the wikidata-page should match the current locale
182 I18n.locale = "zh-CN"
183 links = wikidata_links("wikidata", "Q1234")
184 assert_equal 1, links.length
185 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
186 assert_equal "Q1234", links[0][:title]
189 ### Prefixed wikidata-tags
191 # Not anything is accepted as prefix (only limited set)
192 links = wikidata_links("anything:wikidata", "Q13")
195 # This for example is an allowed key
196 links = wikidata_links("operator:wikidata", "Q24")
197 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
198 assert_equal "Q24", links[0][:title]
200 # Another allowed key, this time with multiple values and I18n
202 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
203 assert_equal 3, links.length
204 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
205 assert_equal "Q936", links[0][:title]
206 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
207 assert_equal "Q2013", links[1][:title]
208 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
209 assert_equal "Q1568346", links[2][:title]
212 # and now with whitespaces...
213 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
214 assert_equal 4, links.length
215 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
216 assert_equal "Q6542248 ", links[0][:title]
217 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
218 assert_equal "\tQ180\n ", links[1][:title]
219 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
220 assert_equal "\rQ364\t\n\r ", links[2][:title]
221 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
222 assert_equal "\nQ4006", links[3][:title]
225 def test_wikipedia_link
226 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
229 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
232 link = wikipedia_link("wikipedia", "Test")
233 assert_equal "http://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
234 assert_equal "Test", link[:title]
236 link = wikipedia_link("wikipedia", "de:Test")
237 assert_equal "http://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
238 assert_equal "de:Test", link[:title]
240 link = wikipedia_link("wikipedia:fr", "de:Test")
241 assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
242 assert_equal "de:Test", link[:title]
244 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
245 assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
246 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
248 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
249 assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
250 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
252 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
253 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]
254 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
256 I18n.locale = "pt-BR"
258 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
259 assert_equal "http://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
260 assert_equal "zh-classical:Test#Section", link[:title]
262 link = wikipedia_link("foo", "Test")
266 def test_telephone_link
267 link = telephone_link("foo", "Test")
270 link = telephone_link("phone", "+123")
273 link = telephone_link("phone", "123")
276 link = telephone_link("phone", "123 abcdefg")
279 link = telephone_link("phone", "+1234567890 abc")
282 link = telephone_link("phone", "+1234567890; +22334455667788")
285 link = telephone_link("phone", "1234567890")
288 link = telephone_link("phone", "+1234567890")
289 assert_equal "tel:+1234567890", link
291 link = telephone_link("phone", "+1234-567-890")
292 assert_equal "tel:+1234-567-890", link
294 link = telephone_link("phone", "+1234/567/890")
295 assert_equal "tel:+1234/567/890", link
297 link = telephone_link("phone", "+1234.567.890")
298 assert_equal "tel:+1234.567.890", link
300 link = telephone_link("phone", " +1234 567-890 ")
301 assert_equal "tel:+1234567-890", link
303 link = telephone_link("phone", "+1 234-567-890")
304 assert_equal "tel:+1234-567-890", link
306 link = telephone_link("phone", "+1 (234) 567-890")
307 assert_equal "tel:+1(234)567-890", link