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("unknown", "abc;def")
26 assert_dom_equal "abc;def", html
28 html = format_value("unknown", "foo;")
29 assert_dom_equal "foo;", html
31 html = format_value("addr:street", "Rue de l'Amigo")
32 assert_dom_equal "Rue de l'Amigo", html
34 html = format_value("phone", "+1234567890")
35 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
37 html = format_value("phone", "+1 (234) 567-890 ; +22334455")
38 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
40 html = format_value("wikipedia", "Test")
41 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"https://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
43 html = format_value("wikidata", "Q42")
44 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/entity/Q42?uselang=en\">Q42</a>", html
46 html = format_value("operator:wikidata", "Q12;Q98")
47 assert_dom_equal "<a title=\"The Q12 item on Wikidata\" href=\"//www.wikidata.org/entity/Q12?uselang=en\">Q12</a>;" \
48 "<a title=\"The Q98 item on Wikidata\" href=\"//www.wikidata.org/entity/Q98?uselang=en\">Q98</a>", html
50 html = format_value("name:etymology:wikidata", "Q123")
51 assert_dom_equal "<a title=\"The Q123 item on Wikidata\" href=\"//www.wikidata.org/entity/Q123?uselang=en\">Q123</a>", html
53 html = format_value("wikimedia_commons", "File:Test.jpg")
54 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
56 html = format_value("colour", "#f00")
57 assert_dom_equal %(<span class="colour-preview-box float-end m-1 border border-dark border-opacity-10" data-colour="#f00" title="Colour #f00 preview"></span>#f00), html
59 html = format_value("email", "foo@example.com")
60 assert_dom_equal "<a title=\"Email foo@example.com\" href=\"mailto:foo@example.com\">foo@example.com</a>", html
62 html = format_value("source", "https://example.com")
63 assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\">https://example.com</a>", html
65 html = format_value("source", "https://example.com;hello;https://example.net")
66 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
70 link = wiki_link("key", "highway")
71 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
73 link = wiki_link("tag", "highway=primary")
74 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
76 I18n.with_locale "de" do
77 link = wiki_link("key", "highway")
78 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
80 link = wiki_link("tag", "highway=primary")
81 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
84 I18n.with_locale "tr" do
85 link = wiki_link("key", "highway")
86 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
88 link = wiki_link("tag", "highway=primary")
89 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
93 def test_wikidata_links
94 ### Non-prefixed wikidata-tag (only one value allowed)
97 links = wikidata_links("foo", "Test")
101 links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
104 # No language-prefixes (as wikidata is multilanguage)
105 links = wikidata_links("wikidata", "en:Q1")
109 links = wikidata_links("wikidata", "1")
112 # No leading zeros allowed
113 links = wikidata_links("wikidata", "Q0123")
117 links = wikidata_links("wikidata", "Q42")
118 assert_equal 1, links.length
119 assert_equal "//www.wikidata.org/entity/Q42?uselang=en", links[0][:url]
120 assert_equal "Q42", links[0][:title]
122 # the language of the wikidata-page should match the current locale
123 I18n.with_locale "zh-CN" do
124 links = wikidata_links("wikidata", "Q1234")
125 assert_equal 1, links.length
126 assert_equal "//www.wikidata.org/entity/Q1234?uselang=zh-CN", links[0][:url]
127 assert_equal "Q1234", links[0][:title]
130 ### Prefixed wikidata-tags
132 # Not anything is accepted as prefix (only limited set)
133 links = wikidata_links("anything:wikidata", "Q13")
136 # This for example is an allowed key
137 links = wikidata_links("operator:wikidata", "Q24")
138 assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url]
139 assert_equal "Q24", links[0][:title]
141 links = wikidata_links("species:wikidata", "Q26899")
142 assert_equal "//www.wikidata.org/entity/Q26899?uselang=en", links[0][:url]
143 assert_equal "Q26899", links[0][:title]
145 # Another allowed key, this time with multiple values and I18n
146 I18n.with_locale "dsb" do
147 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
148 assert_equal 3, links.length
149 assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
150 assert_equal "Q936", links[0][:title]
151 assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
152 assert_equal "Q2013", links[1][:title]
153 assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
154 assert_equal "Q1568346", links[2][:title]
157 # and now with whitespaces...
158 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
159 assert_equal 4, links.length
160 assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
161 assert_equal "Q6542248 ", links[0][:title]
162 assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
163 assert_equal "\tQ180\n ", links[1][:title]
164 assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
165 assert_equal "\rQ364\t\n\r ", links[2][:title]
166 assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
167 assert_equal "\nQ4006", links[3][:title]
170 def test_wikipedia_link
171 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
174 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
177 link = wikipedia_link("wikipedia", "Test")
178 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
179 assert_equal "Test", link[:title]
181 link = wikipedia_link("wikipedia", "de:Test")
182 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
183 assert_equal "de:Test", link[:title]
185 link = wikipedia_link("wikipedia:fr", "de:Test")
186 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
187 assert_equal "de:Test", link[:title]
189 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
190 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
191 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
193 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
194 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br%C3%BCckenaffe", link[:url]
195 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
197 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
198 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]
199 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
201 I18n.with_locale "pt-BR" do
202 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
203 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
204 assert_equal "zh-classical:Test#Section", link[:title]
207 link = wikipedia_link("subject:wikipedia", "en:Catherine McAuley")
208 assert_equal "https://en.wikipedia.org/wiki/en:Catherine McAuley?uselang=en", link[:url]
209 assert_equal "en:Catherine McAuley", link[:title]
211 link = wikipedia_link("foo", "Test")
215 def test_wikimedia_commons_link
216 link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
219 link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
222 link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
225 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
226 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
227 assert_equal "File:Test.jpg", link[:title]
229 link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
230 assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
231 assert_equal "Category:Test_Category", link[:title]
233 I18n.with_locale "pt-BR" do
234 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
235 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
236 assert_equal "File:Test.jpg", link[:title]
239 link = wikimedia_commons_link("foo", "Test")
244 email = email_link("foo", "Test")
247 email = email_link("email", "123")
250 email = email_link("email", "Abc.example.com")
253 email = email_link("email", "a@b@c.com")
256 email = email_link("email", "just\"not\"right@example.com")
259 email = email_link("email", "123 abcdefg@space.com")
262 email = email_link("email", "test@ abc")
265 email = email_link("email", "using;semicolon@test.com")
268 email = email_link("email", "x@example.com")
269 assert_equal "x@example.com", email
271 email = email_link("email", "other.email-with-hyphen@example.com")
272 assert_equal "other.email-with-hyphen@example.com", email
274 email = email_link("email", "user.name+tag+sorting@example.com")
275 assert_equal "user.name+tag+sorting@example.com", email
277 email = email_link("email", "dash-in@both-parts.com")
278 assert_equal "dash-in@both-parts.com", email
280 email = email_link("email", "example@s.example")
281 assert_equal "example@s.example", email
283 # Strips whitespace at ends
284 email = email_link("email", " test@email.com ")
285 assert_equal "test@email.com", email
287 email = email_link("contact:email", "example@example.com")
288 assert_equal "example@example.com", email
290 email = email_link("maxweight:conditional", "none@agricultural")
294 def test_telephone_links
295 links = telephone_links("foo", "Test")
298 links = telephone_links("phone", "+123")
301 links = telephone_links("phone", "123")
304 links = telephone_links("phone", "123 abcdefg")
307 links = telephone_links("phone", "+1234567890 abc")
310 # If multiple numbers are listed, all must be valid
311 links = telephone_links("phone", "+1234567890; +223")
314 links = telephone_links("phone", "1234567890")
317 links = telephone_links("phone", "+1234567890")
318 assert_equal 1, links.length
319 assert_equal "+1234567890", links[0][:phone_number]
320 assert_equal "tel:+1234567890", links[0][:url]
322 links = telephone_links("phone", "+1234-567-890")
323 assert_equal 1, links.length
324 assert_equal "+1234-567-890", links[0][:phone_number]
325 assert_equal "tel:+1234-567-890", links[0][:url]
327 links = telephone_links("phone", "+1234/567/890")
328 assert_equal 1, links.length
329 assert_equal "+1234/567/890", links[0][:phone_number]
330 assert_equal "tel:+1234/567/890", links[0][:url]
332 links = telephone_links("phone", "+1234.567.890")
333 assert_equal 1, links.length
334 assert_equal "+1234.567.890", links[0][:phone_number]
335 assert_equal "tel:+1234.567.890", links[0][:url]
337 links = telephone_links("phone", " +1234 567-890 ")
338 assert_equal 1, links.length
339 assert_equal "+1234 567-890", links[0][:phone_number]
340 assert_equal "tel:+1234567-890", links[0][:url]
342 links = telephone_links("phone", "+1 234-567-890")
343 assert_equal 1, links.length
344 assert_equal "+1 234-567-890", links[0][:phone_number]
345 assert_equal "tel:+1234-567-890", links[0][:url]
347 links = telephone_links("phone", "+1 (234) 567-890")
348 assert_equal 1, links.length
349 assert_equal "+1 (234) 567-890", links[0][:phone_number]
350 assert_equal "tel:+1(234)567-890", links[0][:url]
352 # Multiple valid phone numbers separated by ;
353 links = telephone_links("phone", "+1234567890; +22334455667788")
354 assert_equal 2, links.length
355 assert_equal "+1234567890", links[0][:phone_number]
356 assert_equal "tel:+1234567890", links[0][:url]
357 assert_equal "+22334455667788", links[1][:phone_number]
358 assert_equal "tel:+22334455667788", links[1][:url]
360 links = telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
361 assert_equal 2, links.length
362 assert_equal "+1 (234) 567-890", links[0][:phone_number]
363 assert_equal "tel:+1(234)567-890", links[0][:url]
364 assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
365 assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
368 def test_colour_preview
369 # basic positive tests
370 colour = colour_preview("colour", "red")
371 assert_equal "red", colour
373 colour = colour_preview("colour", "Red")
374 assert_equal "Red", colour
376 colour = colour_preview("colour", "darkRed")
377 assert_equal "darkRed", colour
379 colour = colour_preview("colour", "#f00")
380 assert_equal "#f00", colour
382 colour = colour_preview("colour", "#fF0000")
383 assert_equal "#fF0000", colour
385 # other tag variants:
386 colour = colour_preview("building:colour", "#f00")
387 assert_equal "#f00", colour
389 colour = colour_preview("ref:colour", "#f00")
390 assert_equal "#f00", colour
392 colour = colour_preview("int_ref:colour", "green")
393 assert_equal "green", colour
395 colour = colour_preview("roof:colour", "#f00")
396 assert_equal "#f00", colour
398 colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
399 assert_equal "#f00", colour
402 colour = colour_preview("colour", "")
405 colour = colour_preview("colour", " ")
408 colour = colour_preview("colour", nil)
411 # ignore US spelling variant
412 colour = colour_preview("color", "red")
415 # irrelevant tag names
416 colour = colour_preview("building", "red")
419 colour = colour_preview("ref:colour_no", "red")
422 colour = colour_preview("ref:colour-bg", "red")
425 colour = colour_preview("int_ref", "red")
429 colour = colour_preview("colour", "#")
432 colour = colour_preview("colour", "#ff")
435 colour = colour_preview("colour", "#ffff")
438 colour = colour_preview("colour", "#fffffff")
441 colour = colour_preview("colour", "#ggg")
444 colour = colour_preview("colour", "#ff 00 00")
447 # invalid w3c color names:
448 colour = colour_preview("colour", "r")
451 colour = colour_preview("colour", "ffffff")
454 colour = colour_preview("colour", "f00")
457 colour = colour_preview("colour", "xxxred")
460 colour = colour_preview("colour", "dark red")
463 colour = colour_preview("colour", "dark_red")
466 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")