3 class BrowseTagsHelperTest < ActionView::TestCase
5 include ApplicationHelper
8 html = format_key("highway")
9 assert_dom_equal "<a href=\"https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en\" title=\"The wiki description page for the highway tag\">highway</a>", html
11 html = format_key("unknown")
12 assert_dom_equal "unknown", html
16 html = format_value("highway", "primary")
17 assert_dom_equal "<a href=\"https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en\" title=\"The wiki description page for the highway=primary tag\">primary</a>", html
19 html = format_value("highway", "unknown")
20 assert_dom_equal "unknown", html
22 html = format_value("unknown", "unknown")
23 assert_dom_equal "unknown", html
25 html = format_value("addr:street", "Rue de l'Amigo")
26 assert_dom_equal "Rue de l'Amigo", html
28 html = format_value("phone", "+1234567890")
29 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
31 html = format_value("phone", "+1 (234) 567-890 ; +22334455")
32 assert_dom_equal "<a href=\"tel:+1(234)567-890\" title=\"Call +1 (234) 567-890\">+1 (234) 567-890</a>; <a href=\"tel:+22334455\" title=\"Call +22334455\">+22334455</a>", html
34 html = format_value("wikipedia", "Test")
35 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"https://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
37 html = format_value("wikidata", "Q42")
38 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/entity/Q42?uselang=en\">Q42</a>", html
40 html = format_value("operator:wikidata", "Q12;Q98")
41 assert_dom_equal "<a title=\"The Q12 item on Wikidata\" href=\"//www.wikidata.org/entity/Q12?uselang=en\">Q12</a>;" \
42 "<a title=\"The Q98 item on Wikidata\" href=\"//www.wikidata.org/entity/Q98?uselang=en\">Q98</a>", html
44 html = format_value("name:etymology:wikidata", "Q123")
45 assert_dom_equal "<a title=\"The Q123 item on Wikidata\" href=\"//www.wikidata.org/entity/Q123?uselang=en\">Q123</a>", html
47 html = format_value("wikimedia_commons", "File:Test.jpg")
48 assert_dom_equal "<a title=\"The File:Test.jpg item on Wikimedia Commons\" href=\"//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en\">File:Test.jpg</a>", html
50 html = format_value("colour", "#f00")
51 assert_dom_equal %(<span class="colour-preview-box" data-colour="#f00" title="Colour #f00 preview"></span>#f00), html
53 html = format_value("contact", "foo@example.com")
54 assert_dom_equal "<a title=\"Email foo@example.com\" href=\"mailto:foo@example.com\">foo@example.com</a>", html
56 html = format_value("source", "https://example.com")
57 assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\">https://example.com</a>", html
59 html = format_value("source", "https://example.com;hello;https://example.net")
60 assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\">https://example.com</a>;hello;<a href=\"https://example.net\" rel=\"nofollow\">https://example.net</a>", html
64 link = wiki_link("key", "highway")
65 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
67 link = wiki_link("tag", "highway=primary")
68 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
70 I18n.with_locale "de" do
71 link = wiki_link("key", "highway")
72 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
74 link = wiki_link("tag", "highway=primary")
75 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
78 I18n.with_locale "tr" do
79 link = wiki_link("key", "highway")
80 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
82 link = wiki_link("tag", "highway=primary")
83 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
87 def test_wikidata_links
88 ### Non-prefixed wikidata-tag (only one value allowed)
91 links = wikidata_links("foo", "Test")
95 links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
98 # No language-prefixes (as wikidata is multilanguage)
99 links = wikidata_links("wikidata", "en:Q1")
103 links = wikidata_links("wikidata", "1")
106 # No leading zeros allowed
107 links = wikidata_links("wikidata", "Q0123")
111 links = wikidata_links("wikidata", "Q42")
112 assert_equal 1, links.length
113 assert_equal "//www.wikidata.org/entity/Q42?uselang=en", links[0][:url]
114 assert_equal "Q42", links[0][:title]
116 # the language of the wikidata-page should match the current locale
117 I18n.with_locale "zh-CN" do
118 links = wikidata_links("wikidata", "Q1234")
119 assert_equal 1, links.length
120 assert_equal "//www.wikidata.org/entity/Q1234?uselang=zh-CN", links[0][:url]
121 assert_equal "Q1234", links[0][:title]
124 ### Prefixed wikidata-tags
126 # Not anything is accepted as prefix (only limited set)
127 links = wikidata_links("anything:wikidata", "Q13")
130 # This for example is an allowed key
131 links = wikidata_links("operator:wikidata", "Q24")
132 assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url]
133 assert_equal "Q24", links[0][:title]
135 # Another allowed key, this time with multiple values and I18n
136 I18n.with_locale "dsb" do
137 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
138 assert_equal 3, links.length
139 assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
140 assert_equal "Q936", links[0][:title]
141 assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
142 assert_equal "Q2013", links[1][:title]
143 assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
144 assert_equal "Q1568346", links[2][:title]
147 # and now with whitespaces...
148 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
149 assert_equal 4, links.length
150 assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
151 assert_equal "Q6542248 ", links[0][:title]
152 assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
153 assert_equal "\tQ180\n ", links[1][:title]
154 assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
155 assert_equal "\rQ364\t\n\r ", links[2][:title]
156 assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
157 assert_equal "\nQ4006", links[3][:title]
160 def test_wikipedia_link
161 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
164 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
167 link = wikipedia_link("wikipedia", "Test")
168 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
169 assert_equal "Test", link[:title]
171 link = wikipedia_link("wikipedia", "de:Test")
172 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
173 assert_equal "de:Test", link[:title]
175 link = wikipedia_link("wikipedia:fr", "de:Test")
176 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
177 assert_equal "de:Test", link[:title]
179 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
180 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
181 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
183 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
184 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br%C3%BCckenaffe", link[:url]
185 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
187 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
188 assert_equal "https://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]
189 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
191 I18n.with_locale "pt-BR" do
192 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
193 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
194 assert_equal "zh-classical:Test#Section", link[:title]
197 link = wikipedia_link("foo", "Test")
201 def test_wikimedia_commons_link
202 link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
205 link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
208 link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
211 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
212 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
213 assert_equal "File:Test.jpg", link[:title]
215 link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
216 assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
217 assert_equal "Category:Test_Category", link[:title]
219 I18n.with_locale "pt-BR" do
220 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
221 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
222 assert_equal "File:Test.jpg", link[:title]
225 link = wikimedia_commons_link("foo", "Test")
230 email = email_link("foo", "Test")
233 email = email_link("email", "123")
236 email = email_link("email", "Abc.example.com")
239 email = email_link("email", "a@b@c.com")
242 email = email_link("email", "just\"not\"right@example.com")
245 email = email_link("email", "123 abcdefg@space.com")
248 email = email_link("email", "test@ abc")
251 email = email_link("email", "using;semicolon@test.com")
254 email = email_link("email", "x@example.com")
255 assert_equal "x@example.com", email
257 email = email_link("email", "other.email-with-hyphen@example.com")
258 assert_equal "other.email-with-hyphen@example.com", email
260 email = email_link("email", "user.name+tag+sorting@example.com")
261 assert_equal "user.name+tag+sorting@example.com", email
263 email = email_link("email", "dash-in@both-parts.com")
264 assert_equal "dash-in@both-parts.com", email
266 email = email_link("email", "example@s.example")
267 assert_equal "example@s.example", email
269 # Strips whitespace at ends
270 email = email_link("email", " test@email.com ")
271 assert_equal "test@email.com", email
274 def test_telephone_links
275 links = telephone_links("foo", "Test")
278 links = telephone_links("phone", "+123")
281 links = telephone_links("phone", "123")
284 links = telephone_links("phone", "123 abcdefg")
287 links = telephone_links("phone", "+1234567890 abc")
290 # If multiple numbers are listed, all must be valid
291 links = telephone_links("phone", "+1234567890; +223")
294 links = telephone_links("phone", "1234567890")
297 links = telephone_links("phone", "+1234567890")
298 assert_equal 1, links.length
299 assert_equal "+1234567890", links[0][:phone_number]
300 assert_equal "tel:+1234567890", links[0][:url]
302 links = telephone_links("phone", "+1234-567-890")
303 assert_equal 1, links.length
304 assert_equal "+1234-567-890", links[0][:phone_number]
305 assert_equal "tel:+1234-567-890", links[0][:url]
307 links = telephone_links("phone", "+1234/567/890")
308 assert_equal 1, links.length
309 assert_equal "+1234/567/890", links[0][:phone_number]
310 assert_equal "tel:+1234/567/890", links[0][:url]
312 links = telephone_links("phone", "+1234.567.890")
313 assert_equal 1, links.length
314 assert_equal "+1234.567.890", links[0][:phone_number]
315 assert_equal "tel:+1234.567.890", links[0][:url]
317 links = telephone_links("phone", " +1234 567-890 ")
318 assert_equal 1, links.length
319 assert_equal "+1234 567-890", links[0][:phone_number]
320 assert_equal "tel:+1234567-890", links[0][:url]
322 links = telephone_links("phone", "+1 234-567-890")
323 assert_equal 1, links.length
324 assert_equal "+1 234-567-890", links[0][:phone_number]
325 assert_equal "tel:+1234-567-890", links[0][:url]
327 links = telephone_links("phone", "+1 (234) 567-890")
328 assert_equal 1, links.length
329 assert_equal "+1 (234) 567-890", links[0][:phone_number]
330 assert_equal "tel:+1(234)567-890", links[0][:url]
332 # Multiple valid phone numbers separated by ;
333 links = telephone_links("phone", "+1234567890; +22334455667788")
334 assert_equal 2, links.length
335 assert_equal "+1234567890", links[0][:phone_number]
336 assert_equal "tel:+1234567890", links[0][:url]
337 assert_equal "+22334455667788", links[1][:phone_number]
338 assert_equal "tel:+22334455667788", links[1][:url]
340 links = telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
341 assert_equal 2, links.length
342 assert_equal "+1 (234) 567-890", links[0][:phone_number]
343 assert_equal "tel:+1(234)567-890", links[0][:url]
344 assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
345 assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
348 def test_colour_preview
349 # basic positive tests
350 colour = colour_preview("colour", "red")
351 assert_equal "red", colour
353 colour = colour_preview("colour", "Red")
354 assert_equal "Red", colour
356 colour = colour_preview("colour", "darkRed")
357 assert_equal "darkRed", colour
359 colour = colour_preview("colour", "#f00")
360 assert_equal "#f00", colour
362 colour = colour_preview("colour", "#fF0000")
363 assert_equal "#fF0000", colour
365 # other tag variants:
366 colour = colour_preview("building:colour", "#f00")
367 assert_equal "#f00", colour
369 colour = colour_preview("ref:colour", "#f00")
370 assert_equal "#f00", colour
372 colour = colour_preview("int_ref:colour", "green")
373 assert_equal "green", colour
375 colour = colour_preview("roof:colour", "#f00")
376 assert_equal "#f00", colour
378 colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
379 assert_equal "#f00", colour
382 colour = colour_preview("colour", "")
385 colour = colour_preview("colour", " ")
388 colour = colour_preview("colour", nil)
391 # ignore US spelling variant
392 colour = colour_preview("color", "red")
395 # irrelevant tag names
396 colour = colour_preview("building", "red")
399 colour = colour_preview("ref:colour_no", "red")
402 colour = colour_preview("ref:colour-bg", "red")
405 colour = colour_preview("int_ref", "red")
409 colour = colour_preview("colour", "#")
412 colour = colour_preview("colour", "#ff")
415 colour = colour_preview("colour", "#ffff")
418 colour = colour_preview("colour", "#fffffff")
421 colour = colour_preview("colour", "#ggg")
424 colour = colour_preview("colour", "#ff 00 00")
427 # invalid w3c color names:
428 colour = colour_preview("colour", "r")
431 colour = colour_preview("colour", "ffffff")
434 colour = colour_preview("colour", "f00")
437 colour = colour_preview("colour", "xxxred")
440 colour = colour_preview("colour", "dark red")
443 colour = colour_preview("colour", "dark_red")
446 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")