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
106 tags = icon_tags(current_nodes(:node_with_name))
107 assert_equal 3, tags.count
108 assert tags.include?(%w(building yes))
109 assert tags.include?(%w(tourism museum))
110 assert tags.include?(%w(shop gift))
112 tags = icon_tags(nodes(:node_with_name_current_version))
113 assert_equal 3, tags.count
114 assert tags.include?(%w(building yes))
115 assert tags.include?(%w(tourism museum))
116 assert tags.include?(%w(shop gift))
118 tags = icon_tags(nodes(:node_with_name_redacted_version))
119 assert_equal 3, tags.count
120 assert tags.include?(%w(building yes))
121 assert tags.include?(%w(tourism museum))
122 assert tags.include?(%w(shop gift))
126 link = wiki_link("key", "highway")
127 assert_equal "http://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
129 link = wiki_link("tag", "highway=primary")
130 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
134 link = wiki_link("key", "highway")
135 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
137 link = wiki_link("tag", "highway=primary")
138 assert_equal "http://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
142 link = wiki_link("key", "highway")
143 assert_equal "http://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
145 link = wiki_link("tag", "highway=primary")
146 assert_equal "http://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
149 def test_wikidata_links
150 ### Non-prefixed wikidata-tag (only one value allowed)
152 links = wikidata_links("foo", "Test")
156 links = wikidata_links("wikidata", "http://www.wikidata.org/wiki/Q1")
159 # No language-prefixes (as wikidata is multilanguage)
160 links = wikidata_links("wikidata", "en:Q1")
164 links = wikidata_links("wikidata", "1")
167 # No leading zeros allowed
168 links = wikidata_links("wikidata", "Q0123")
172 links = wikidata_links("wikidata", "Q42")
173 assert_equal 1, links.length
174 assert_equal "//www.wikidata.org/wiki/Q42?uselang=en", links[0][:url]
175 assert_equal "Q42", links[0][:title]
177 # the language of the wikidata-page should match the current locale
178 I18n.locale = "zh-CN"
179 links = wikidata_links("wikidata", "Q1234")
180 assert_equal 1, links.length
181 assert_equal "//www.wikidata.org/wiki/Q1234?uselang=zh-CN", links[0][:url]
182 assert_equal "Q1234", links[0][:title]
185 ### Prefixed wikidata-tags
187 # Not anything is accepted as prefix (only limited set)
188 links = wikidata_links("anything:wikidata", "Q13")
191 # This for example is an allowed key
192 links = wikidata_links("operator:wikidata", "Q24")
193 assert_equal "//www.wikidata.org/wiki/Q24?uselang=en", links[0][:url]
194 assert_equal "Q24", links[0][:title]
196 # another allowed key, this time with multiple values and I18n
199 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
200 assert_equal 3, links.length
201 assert_equal "//www.wikidata.org/wiki/Q936?uselang=dsb", links[0][:url]
202 assert_equal "Q936", links[0][:title]
203 assert_equal "//www.wikidata.org/wiki/Q2013?uselang=dsb", links[1][:url]
204 assert_equal "Q2013", links[1][:title]
205 assert_equal "//www.wikidata.org/wiki/Q1568346?uselang=dsb", links[2][:url]
206 assert_equal "Q1568346", links[2][:title]
209 # and now with whitespaces...
210 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
211 assert_equal 4, links.length
212 assert_equal "//www.wikidata.org/wiki/Q6542248?uselang=en", links[0][:url]
213 assert_equal "Q6542248 ", links[0][:title]
214 assert_equal "//www.wikidata.org/wiki/Q180?uselang=en", links[1][:url]
215 assert_equal "\tQ180\n ", links[1][:title]
216 assert_equal "//www.wikidata.org/wiki/Q364?uselang=en", links[2][:url]
217 assert_equal "\rQ364\t\n\r ", links[2][:title]
218 assert_equal "//www.wikidata.org/wiki/Q4006?uselang=en", links[3][:url]
219 assert_equal "\nQ4006", links[3][:title]
222 def test_wikipedia_link
223 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
226 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
229 link = wikipedia_link("wikipedia", "Test")
230 assert_equal "http://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
231 assert_equal "Test", link[:title]
233 link = wikipedia_link("wikipedia", "de:Test")
234 assert_equal "http://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
235 assert_equal "de:Test", link[:title]
237 link = wikipedia_link("wikipedia:fr", "de:Test")
238 assert_equal "http://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
239 assert_equal "de:Test", link[:title]
241 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
242 assert_equal "http://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
243 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
245 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
246 assert_equal "http://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
247 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
249 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
250 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]
251 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
253 I18n.locale = "pt-BR"
255 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
256 assert_equal "http://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
257 assert_equal "zh-classical:Test#Section", link[:title]
259 link = wikipedia_link("foo", "Test")
263 def test_telephone_link
264 link = telephone_link("foo", "Test")
267 link = telephone_link("phone", "+123")
270 link = telephone_link("phone", "123")
273 link = telephone_link("phone", "123 abcdefg")
276 link = telephone_link("phone", "+1234567890 abc")
279 link = telephone_link("phone", "+1234567890; +22334455667788")
282 link = telephone_link("phone", "1234567890")
285 link = telephone_link("phone", "+1234567890")
286 assert_equal "tel:+1234567890", link
288 link = telephone_link("phone", "+1234-567-890")
289 assert_equal "tel:+1234-567-890", 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:+1234567-890", link
300 link = telephone_link("phone", "+1 234-567-890")
301 assert_equal "tel:+1234-567-890", link
303 link = telephone_link("phone", "+1 (234) 567-890")
304 assert_equal "tel:+1(234)567-890", link