3 class BrowseTagsHelperTest < ActionView::TestCase
5 include ApplicationHelper
16 html = format_key("highway")
17 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
19 html = format_key("unknown")
20 assert_dom_equal "unknown", html
24 html = format_value("highway", "primary")
25 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
27 html = format_value("highway", "unknown")
28 assert_dom_equal "unknown", html
30 html = format_value("unknown", "unknown")
31 assert_dom_equal "unknown", html
33 html = format_value("phone", "+1234567890")
34 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
36 html = format_value("phone", "+1 (234) 567-890 ; +22334455")
37 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
39 html = format_value("wikipedia", "Test")
40 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"https://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
42 html = format_value("wikidata", "Q42")
43 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/entity/Q42?uselang=en\">Q42</a>", html
45 html = format_value("operator:wikidata", "Q12;Q98")
46 assert_dom_equal "<a title=\"The Q12 item on Wikidata\" href=\"//www.wikidata.org/entity/Q12?uselang=en\">Q12</a>;" \
47 "<a title=\"The Q98 item on Wikidata\" href=\"//www.wikidata.org/entity/Q98?uselang=en\">Q98</a>", html
49 html = format_value("name:etymology:wikidata", "Q123")
50 assert_dom_equal "<a title=\"The Q123 item on Wikidata\" href=\"//www.wikidata.org/entity/Q123?uselang=en\">Q123</a>", html
52 html = format_value("wikimedia_commons", "File:Test.jpg")
53 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
55 html = format_value("colour", "#f00")
56 assert_dom_equal %(<span class="colour-preview-box" data-colour="#f00" title="Colour #f00 preview"></span>#f00), html
60 link = wiki_link("key", "highway")
61 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
63 link = wiki_link("tag", "highway=primary")
64 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
68 link = wiki_link("key", "highway")
69 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
71 link = wiki_link("tag", "highway=primary")
72 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
76 link = wiki_link("key", "highway")
77 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
79 link = wiki_link("tag", "highway=primary")
80 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
83 def test_wikidata_links
84 ### Non-prefixed wikidata-tag (only one value allowed)
87 links = wikidata_links("foo", "Test")
91 links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
94 # No language-prefixes (as wikidata is multilanguage)
95 links = wikidata_links("wikidata", "en:Q1")
99 links = wikidata_links("wikidata", "1")
102 # No leading zeros allowed
103 links = wikidata_links("wikidata", "Q0123")
107 links = wikidata_links("wikidata", "Q42")
108 assert_equal 1, links.length
109 assert_equal "//www.wikidata.org/entity/Q42?uselang=en", links[0][:url]
110 assert_equal "Q42", links[0][:title]
112 # the language of the wikidata-page should match the current locale
113 I18n.locale = "zh-CN"
114 links = wikidata_links("wikidata", "Q1234")
115 assert_equal 1, links.length
116 assert_equal "//www.wikidata.org/entity/Q1234?uselang=zh-CN", links[0][:url]
117 assert_equal "Q1234", links[0][:title]
120 ### Prefixed wikidata-tags
122 # Not anything is accepted as prefix (only limited set)
123 links = wikidata_links("anything:wikidata", "Q13")
126 # This for example is an allowed key
127 links = wikidata_links("operator:wikidata", "Q24")
128 assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url]
129 assert_equal "Q24", links[0][:title]
131 # Another allowed key, this time with multiple values and I18n
133 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
134 assert_equal 3, links.length
135 assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
136 assert_equal "Q936", links[0][:title]
137 assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
138 assert_equal "Q2013", links[1][:title]
139 assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
140 assert_equal "Q1568346", links[2][:title]
143 # and now with whitespaces...
144 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
145 assert_equal 4, links.length
146 assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
147 assert_equal "Q6542248 ", links[0][:title]
148 assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
149 assert_equal "\tQ180\n ", links[1][:title]
150 assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
151 assert_equal "\rQ364\t\n\r ", links[2][:title]
152 assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
153 assert_equal "\nQ4006", links[3][:title]
156 def test_wikipedia_link
157 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
160 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
163 link = wikipedia_link("wikipedia", "Test")
164 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
165 assert_equal "Test", link[:title]
167 link = wikipedia_link("wikipedia", "de:Test")
168 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
169 assert_equal "de:Test", link[:title]
171 link = wikipedia_link("wikipedia:fr", "de:Test")
172 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
173 assert_equal "de:Test", link[:title]
175 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
176 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
177 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
179 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
180 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br.C3.BCckenaffe", link[:url]
181 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
183 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
184 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]
185 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
187 I18n.locale = "pt-BR"
189 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
190 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
191 assert_equal "zh-classical:Test#Section", link[:title]
193 link = wikipedia_link("foo", "Test")
197 def test_wikimedia_commons_link
198 link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
201 link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
204 link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
207 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
208 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
209 assert_equal "File:Test.jpg", link[:title]
211 link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
212 assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
213 assert_equal "Category:Test_Category", link[:title]
215 I18n.locale = "pt-BR"
217 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
218 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
219 assert_equal "File:Test.jpg", link[:title]
221 link = wikimedia_commons_link("foo", "Test")
226 email = email_link("foo", "Test")
229 email = email_link("email", "123")
232 email = email_link("email", "Abc.example.com")
235 email = email_link("email", "a@b@c.com")
238 email = email_link("email", "just\"not\"right@example.com")
241 email = email_link("email", "123 abcdefg@space.com")
244 email = email_link("email", "test@ abc")
247 email = email_link("email", "using;semicolon@test.com")
250 email = email_link("email", "x@example.com")
251 assert_equal "x@example.com", email[:email]
252 assert_equal "mailto:x@example.com", email[:url]
254 email = email_link("email", "other.email-with-hyphen@example.com")
255 assert_equal "other.email-with-hyphen@example.com", email[:email]
256 assert_equal "mailto:other.email-with-hyphen@example.com", email[:url]
258 email = email_link("email", "user.name+tag+sorting@example.com")
259 assert_equal "user.name+tag+sorting@example.com", email[:email]
260 assert_equal "mailto:user.name+tag+sorting@example.com", email[:url]
262 email = email_link("email", "dash-in@both-parts.com")
263 assert_equal "dash-in@both-parts.com", email[:email]
264 assert_equal "mailto:dash-in@both-parts.com", email[:url]
266 email = email_link("email", "example@s.example")
267 assert_equal "example@s.example", email[:email]
268 assert_equal "mailto:example@s.example", email[:url]
270 # Strips whitespace at ends
271 email = email_link("email", " test@email.com ")
272 assert_equal "test@email.com", email[:email]
273 assert_equal "mailto:test@email.com", email[:url]
276 def test_telephone_links
277 links = telephone_links("foo", "Test")
280 links = telephone_links("phone", "+123")
283 links = telephone_links("phone", "123")
286 links = telephone_links("phone", "123 abcdefg")
289 links = telephone_links("phone", "+1234567890 abc")
292 # If multiple numbers are listed, all must be valid
293 links = telephone_links("phone", "+1234567890; +223")
296 links = telephone_links("phone", "1234567890")
299 links = telephone_links("phone", "+1234567890")
300 assert_equal 1, links.length
301 assert_equal "+1234567890", links[0][:phone_number]
302 assert_equal "tel:+1234567890", links[0][:url]
304 links = telephone_links("phone", "+1234-567-890")
305 assert_equal 1, links.length
306 assert_equal "+1234-567-890", links[0][:phone_number]
307 assert_equal "tel:+1234-567-890", links[0][:url]
309 links = telephone_links("phone", "+1234/567/890")
310 assert_equal 1, links.length
311 assert_equal "+1234/567/890", links[0][:phone_number]
312 assert_equal "tel:+1234/567/890", links[0][:url]
314 links = telephone_links("phone", "+1234.567.890")
315 assert_equal 1, links.length
316 assert_equal "+1234.567.890", links[0][:phone_number]
317 assert_equal "tel:+1234.567.890", links[0][:url]
319 links = telephone_links("phone", " +1234 567-890 ")
320 assert_equal 1, links.length
321 assert_equal "+1234 567-890", links[0][:phone_number]
322 assert_equal "tel:+1234567-890", links[0][:url]
324 links = telephone_links("phone", "+1 234-567-890")
325 assert_equal 1, links.length
326 assert_equal "+1 234-567-890", links[0][:phone_number]
327 assert_equal "tel:+1234-567-890", links[0][:url]
329 links = telephone_links("phone", "+1 (234) 567-890")
330 assert_equal 1, links.length
331 assert_equal "+1 (234) 567-890", links[0][:phone_number]
332 assert_equal "tel:+1(234)567-890", links[0][:url]
334 # Multiple valid phone numbers separated by ;
335 links = telephone_links("phone", "+1234567890; +22334455667788")
336 assert_equal 2, links.length
337 assert_equal "+1234567890", links[0][:phone_number]
338 assert_equal "tel:+1234567890", links[0][:url]
339 assert_equal "+22334455667788", links[1][:phone_number]
340 assert_equal "tel:+22334455667788", links[1][:url]
342 links = telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
343 assert_equal 2, links.length
344 assert_equal "+1 (234) 567-890", links[0][:phone_number]
345 assert_equal "tel:+1(234)567-890", links[0][:url]
346 assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
347 assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
350 def test_colour_preview
351 # basic positive tests
352 colour = colour_preview("colour", "red")
353 assert_equal "red", colour
355 colour = colour_preview("colour", "Red")
356 assert_equal "Red", colour
358 colour = colour_preview("colour", "darkRed")
359 assert_equal "darkRed", colour
361 colour = colour_preview("colour", "#f00")
362 assert_equal "#f00", colour
364 colour = colour_preview("colour", "#fF0000")
365 assert_equal "#fF0000", colour
367 # other tag variants:
368 colour = colour_preview("building:colour", "#f00")
369 assert_equal "#f00", colour
371 colour = colour_preview("ref:colour", "#f00")
372 assert_equal "#f00", colour
374 colour = colour_preview("int_ref:colour", "green")
375 assert_equal "green", colour
377 colour = colour_preview("roof:colour", "#f00")
378 assert_equal "#f00", colour
380 colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
381 assert_equal "#f00", colour
384 colour = colour_preview("colour", "")
387 colour = colour_preview("colour", " ")
390 colour = colour_preview("colour", nil)
393 # ignore US spelling variant
394 colour = colour_preview("color", "red")
397 # irrelevant tag names
398 colour = colour_preview("building", "red")
401 colour = colour_preview("ref:colour_no", "red")
404 colour = colour_preview("ref:colour-bg", "red")
407 colour = colour_preview("int_ref", "red")
411 colour = colour_preview("colour", "#")
414 colour = colour_preview("colour", "#ff")
417 colour = colour_preview("colour", "#ffff")
420 colour = colour_preview("colour", "#fffffff")
423 colour = colour_preview("colour", "#ggg")
426 colour = colour_preview("colour", "#ff 00 00")
429 # invalid w3c color names:
430 colour = colour_preview("colour", "r")
433 colour = colour_preview("colour", "ffffff")
436 colour = colour_preview("colour", "f00")
439 colour = colour_preview("colour", "xxxred")
442 colour = colour_preview("colour", "dark red")
445 colour = colour_preview("colour", "dark_red")
448 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")