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 dom = Rails::Dom::Testing.html_document_fragment.parse html
58 assert_select dom, "svg>rect>@fill", "#f00"
59 assert_match(/#f00$/, html)
61 html = format_value("email", "foo@example.com")
62 assert_dom_equal "<a title=\"Email foo@example.com\" href=\"mailto:foo@example.com\">foo@example.com</a>", html
64 html = format_value("source", "https://example.com")
65 assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\">https://example.com</a>", html
67 html = format_value("source", "https://example.com;hello;https://example.net")
68 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
72 link = wiki_link("key", "highway")
73 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
75 link = wiki_link("tag", "highway=primary")
76 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
78 I18n.with_locale "de" do
79 link = wiki_link("key", "highway")
80 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
82 link = wiki_link("tag", "highway=primary")
83 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
86 I18n.with_locale "tr" do
87 link = wiki_link("key", "highway")
88 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
90 link = wiki_link("tag", "highway=primary")
91 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
95 def test_wikidata_links
96 ### Non-prefixed wikidata-tag (only one value allowed)
99 links = wikidata_links("foo", "Test")
103 links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
106 # No language-prefixes (as wikidata is multilanguage)
107 links = wikidata_links("wikidata", "en:Q1")
111 links = wikidata_links("wikidata", "1")
114 # No leading zeros allowed
115 links = wikidata_links("wikidata", "Q0123")
119 links = wikidata_links("wikidata", "Q42")
120 assert_equal 1, links.length
121 assert_equal "//www.wikidata.org/entity/Q42?uselang=en", links[0][:url]
122 assert_equal "Q42", links[0][:title]
124 # the language of the wikidata-page should match the current locale
125 I18n.with_locale "zh-CN" do
126 links = wikidata_links("wikidata", "Q1234")
127 assert_equal 1, links.length
128 assert_equal "//www.wikidata.org/entity/Q1234?uselang=zh-CN", links[0][:url]
129 assert_equal "Q1234", links[0][:title]
132 ### Prefixed wikidata-tags
134 # Not anything is accepted as prefix (only limited set)
135 links = wikidata_links("anything:wikidata", "Q13")
138 # This for example is an allowed key
139 links = wikidata_links("operator:wikidata", "Q24")
140 assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url]
141 assert_equal "Q24", links[0][:title]
143 links = wikidata_links("species:wikidata", "Q26899")
144 assert_equal "//www.wikidata.org/entity/Q26899?uselang=en", links[0][:url]
145 assert_equal "Q26899", links[0][:title]
147 # Another allowed key, this time with multiple values and I18n
148 I18n.with_locale "dsb" do
149 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
150 assert_equal 3, links.length
151 assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
152 assert_equal "Q936", links[0][:title]
153 assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
154 assert_equal "Q2013", links[1][:title]
155 assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
156 assert_equal "Q1568346", links[2][:title]
159 # and now with whitespaces...
160 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
161 assert_equal 4, links.length
162 assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
163 assert_equal "Q6542248 ", links[0][:title]
164 assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
165 assert_equal "\tQ180\n ", links[1][:title]
166 assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
167 assert_equal "\rQ364\t\n\r ", links[2][:title]
168 assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
169 assert_equal "\nQ4006", links[3][:title]
172 def test_wikipedia_link
173 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
176 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
179 link = wikipedia_link("wikipedia", "Test")
180 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
181 assert_equal "Test", link[:title]
183 link = wikipedia_link("wikipedia", "de:Test")
184 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
185 assert_equal "de:Test", link[:title]
187 link = wikipedia_link("wikipedia:fr", "de:Test")
188 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
189 assert_equal "de:Test", link[:title]
191 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
192 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
193 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
195 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
196 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br%C3%BCckenaffe", link[:url]
197 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
199 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
200 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]
201 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
203 I18n.with_locale "pt-BR" do
204 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
205 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
206 assert_equal "zh-classical:Test#Section", link[:title]
209 link = wikipedia_link("subject:wikipedia", "en:Catherine McAuley")
210 assert_equal "https://en.wikipedia.org/wiki/en:Catherine McAuley?uselang=en", link[:url]
211 assert_equal "en:Catherine McAuley", link[:title]
213 link = wikipedia_link("foo", "Test")
217 def test_wikimedia_commons_link
218 link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
221 link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
224 link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
227 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
228 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
229 assert_equal "File:Test.jpg", link[:title]
231 link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
232 assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
233 assert_equal "Category:Test_Category", link[:title]
235 I18n.with_locale "pt-BR" do
236 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
237 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
238 assert_equal "File:Test.jpg", link[:title]
241 link = wikimedia_commons_link("foo", "Test")
246 email = email_link("foo", "Test")
249 email = email_link("email", "123")
252 email = email_link("email", "Abc.example.com")
255 email = email_link("email", "a@b@c.com")
258 email = email_link("email", "just\"not\"right@example.com")
261 email = email_link("email", "123 abcdefg@space.com")
264 email = email_link("email", "test@ abc")
267 email = email_link("email", "using;semicolon@test.com")
270 email = email_link("email", "x@example.com")
271 assert_equal "x@example.com", email
273 email = email_link("email", "other.email-with-hyphen@example.com")
274 assert_equal "other.email-with-hyphen@example.com", email
276 email = email_link("email", "user.name+tag+sorting@example.com")
277 assert_equal "user.name+tag+sorting@example.com", email
279 email = email_link("email", "dash-in@both-parts.com")
280 assert_equal "dash-in@both-parts.com", email
282 email = email_link("email", "example@s.example")
283 assert_equal "example@s.example", email
285 # Strips whitespace at ends
286 email = email_link("email", " test@email.com ")
287 assert_equal "test@email.com", email
289 email = email_link("contact:email", "example@example.com")
290 assert_equal "example@example.com", email
292 email = email_link("maxweight:conditional", "none@agricultural")
296 def test_telephone_links
297 links = telephone_links("foo", "Test")
300 links = telephone_links("phone", "+123")
303 links = telephone_links("phone", "123")
306 links = telephone_links("phone", "123 abcdefg")
309 links = telephone_links("phone", "+1234567890 abc")
312 # If multiple numbers are listed, all must be valid
313 links = telephone_links("phone", "+1234567890; +223")
316 links = telephone_links("phone", "1234567890")
319 links = telephone_links("phone", "+1234567890")
320 assert_equal 1, links.length
321 assert_equal "+1234567890", links[0][:phone_number]
322 assert_equal "tel:+1234567890", links[0][:url]
324 links = telephone_links("phone", "+1234-567-890")
325 assert_equal 1, links.length
326 assert_equal "+1234-567-890", links[0][:phone_number]
327 assert_equal "tel:+1234-567-890", links[0][:url]
329 links = telephone_links("phone", "+1234/567/890")
330 assert_equal 1, links.length
331 assert_equal "+1234/567/890", links[0][:phone_number]
332 assert_equal "tel:+1234/567/890", links[0][:url]
334 links = telephone_links("phone", "+1234.567.890")
335 assert_equal 1, links.length
336 assert_equal "+1234.567.890", links[0][:phone_number]
337 assert_equal "tel:+1234.567.890", links[0][:url]
339 links = telephone_links("phone", " +1234 567-890 ")
340 assert_equal 1, links.length
341 assert_equal "+1234 567-890", links[0][:phone_number]
342 assert_equal "tel:+1234567-890", links[0][:url]
344 links = telephone_links("phone", "+1 234-567-890")
345 assert_equal 1, links.length
346 assert_equal "+1 234-567-890", links[0][:phone_number]
347 assert_equal "tel:+1234-567-890", links[0][:url]
349 links = telephone_links("phone", "+1 (234) 567-890")
350 assert_equal 1, links.length
351 assert_equal "+1 (234) 567-890", links[0][:phone_number]
352 assert_equal "tel:+1(234)567-890", links[0][:url]
354 # Multiple valid phone numbers separated by ;
355 links = telephone_links("phone", "+1234567890; +22334455667788")
356 assert_equal 2, links.length
357 assert_equal "+1234567890", links[0][:phone_number]
358 assert_equal "tel:+1234567890", links[0][:url]
359 assert_equal "+22334455667788", links[1][:phone_number]
360 assert_equal "tel:+22334455667788", links[1][:url]
362 links = telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
363 assert_equal 2, links.length
364 assert_equal "+1 (234) 567-890", links[0][:phone_number]
365 assert_equal "tel:+1(234)567-890", links[0][:url]
366 assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
367 assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
370 def test_colour_preview
371 # basic positive tests
372 colour = colour_preview("colour", "red")
373 assert_equal "red", colour
375 colour = colour_preview("colour", "Red")
376 assert_equal "Red", colour
378 colour = colour_preview("colour", "darkRed")
379 assert_equal "darkRed", colour
381 colour = colour_preview("colour", "#f00")
382 assert_equal "#f00", colour
384 colour = colour_preview("colour", "#fF0000")
385 assert_equal "#fF0000", colour
387 # other tag variants:
388 colour = colour_preview("building:colour", "#f00")
389 assert_equal "#f00", colour
391 colour = colour_preview("ref:colour", "#f00")
392 assert_equal "#f00", colour
394 colour = colour_preview("int_ref:colour", "green")
395 assert_equal "green", colour
397 colour = colour_preview("roof:colour", "#f00")
398 assert_equal "#f00", colour
400 colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
401 assert_equal "#f00", colour
404 colour = colour_preview("colour", "")
407 colour = colour_preview("colour", " ")
410 colour = colour_preview("colour", nil)
413 # ignore US spelling variant
414 colour = colour_preview("color", "red")
417 # irrelevant tag names
418 colour = colour_preview("building", "red")
421 colour = colour_preview("ref:colour_no", "red")
424 colour = colour_preview("ref:colour-bg", "red")
427 colour = colour_preview("int_ref", "red")
431 colour = colour_preview("colour", "#")
434 colour = colour_preview("colour", "#ff")
437 colour = colour_preview("colour", "#ffff")
440 colour = colour_preview("colour", "#fffffff")
443 colour = colour_preview("colour", "#ggg")
446 colour = colour_preview("colour", "#ff 00 00")
449 # invalid w3c color names:
450 colour = colour_preview("colour", "r")
453 colour = colour_preview("colour", "ffffff")
456 colour = colour_preview("colour", "f00")
459 colour = colour_preview("colour", "xxxred")
462 colour = colour_preview("colour", "dark red")
465 colour = colour_preview("colour", "dark_red")
468 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")