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("phone", "+1234567890")
26 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
28 html = format_value("phone", "+1 (234) 567-890 ; +22334455")
29 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
31 html = format_value("wikipedia", "Test")
32 assert_dom_equal "<a title=\"The Test article on Wikipedia\" href=\"https://en.wikipedia.org/wiki/Test?uselang=en\">Test</a>", html
34 html = format_value("wikidata", "Q42")
35 assert_dom_equal "<a title=\"The Q42 item on Wikidata\" href=\"//www.wikidata.org/entity/Q42?uselang=en\">Q42</a>", html
37 html = format_value("operator:wikidata", "Q12;Q98")
38 assert_dom_equal "<a title=\"The Q12 item on Wikidata\" href=\"//www.wikidata.org/entity/Q12?uselang=en\">Q12</a>;" \
39 "<a title=\"The Q98 item on Wikidata\" href=\"//www.wikidata.org/entity/Q98?uselang=en\">Q98</a>", html
41 html = format_value("name:etymology:wikidata", "Q123")
42 assert_dom_equal "<a title=\"The Q123 item on Wikidata\" href=\"//www.wikidata.org/entity/Q123?uselang=en\">Q123</a>", html
44 html = format_value("wikimedia_commons", "File:Test.jpg")
45 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
47 html = format_value("colour", "#f00")
48 assert_dom_equal %(<span class="colour-preview-box" data-colour="#f00" title="Colour #f00 preview"></span>#f00), html
52 link = wiki_link("key", "highway")
53 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
55 link = wiki_link("tag", "highway=primary")
56 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
58 I18n.with_locale "de" do
59 link = wiki_link("key", "highway")
60 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
62 link = wiki_link("tag", "highway=primary")
63 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
66 I18n.with_locale "tr" do
67 link = wiki_link("key", "highway")
68 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
70 link = wiki_link("tag", "highway=primary")
71 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
75 def test_wikidata_links
76 ### Non-prefixed wikidata-tag (only one value allowed)
79 links = wikidata_links("foo", "Test")
83 links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
86 # No language-prefixes (as wikidata is multilanguage)
87 links = wikidata_links("wikidata", "en:Q1")
91 links = wikidata_links("wikidata", "1")
94 # No leading zeros allowed
95 links = wikidata_links("wikidata", "Q0123")
99 links = wikidata_links("wikidata", "Q42")
100 assert_equal 1, links.length
101 assert_equal "//www.wikidata.org/entity/Q42?uselang=en", links[0][:url]
102 assert_equal "Q42", links[0][:title]
104 # the language of the wikidata-page should match the current locale
105 I18n.with_locale "zh-CN" do
106 links = wikidata_links("wikidata", "Q1234")
107 assert_equal 1, links.length
108 assert_equal "//www.wikidata.org/entity/Q1234?uselang=zh-CN", links[0][:url]
109 assert_equal "Q1234", links[0][:title]
112 ### Prefixed wikidata-tags
114 # Not anything is accepted as prefix (only limited set)
115 links = wikidata_links("anything:wikidata", "Q13")
118 # This for example is an allowed key
119 links = wikidata_links("operator:wikidata", "Q24")
120 assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url]
121 assert_equal "Q24", links[0][:title]
123 # Another allowed key, this time with multiple values and I18n
124 I18n.with_locale "dsb" do
125 links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
126 assert_equal 3, links.length
127 assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
128 assert_equal "Q936", links[0][:title]
129 assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
130 assert_equal "Q2013", links[1][:title]
131 assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
132 assert_equal "Q1568346", links[2][:title]
135 # and now with whitespaces...
136 links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
137 assert_equal 4, links.length
138 assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
139 assert_equal "Q6542248 ", links[0][:title]
140 assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
141 assert_equal "\tQ180\n ", links[1][:title]
142 assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
143 assert_equal "\rQ364\t\n\r ", links[2][:title]
144 assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
145 assert_equal "\nQ4006", links[3][:title]
148 def test_wikipedia_link
149 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
152 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
155 link = wikipedia_link("wikipedia", "Test")
156 assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
157 assert_equal "Test", link[:title]
159 link = wikipedia_link("wikipedia", "de:Test")
160 assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
161 assert_equal "de:Test", link[:title]
163 link = wikipedia_link("wikipedia:fr", "de:Test")
164 assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
165 assert_equal "de:Test", link[:title]
167 link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
168 assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
169 assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
171 link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
172 assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br%C3%BCckenaffe", link[:url]
173 assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
175 link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
176 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]
177 assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
179 I18n.with_locale "pt-BR" do
180 link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
181 assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
182 assert_equal "zh-classical:Test#Section", link[:title]
185 link = wikipedia_link("foo", "Test")
189 def test_wikimedia_commons_link
190 link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
193 link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
196 link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
199 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
200 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
201 assert_equal "File:Test.jpg", link[:title]
203 link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
204 assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
205 assert_equal "Category:Test_Category", link[:title]
207 I18n.with_locale "pt-BR" do
208 link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
209 assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
210 assert_equal "File:Test.jpg", link[:title]
213 link = wikimedia_commons_link("foo", "Test")
218 email = email_link("foo", "Test")
221 email = email_link("email", "123")
224 email = email_link("email", "Abc.example.com")
227 email = email_link("email", "a@b@c.com")
230 email = email_link("email", "just\"not\"right@example.com")
233 email = email_link("email", "123 abcdefg@space.com")
236 email = email_link("email", "test@ abc")
239 email = email_link("email", "using;semicolon@test.com")
242 email = email_link("email", "x@example.com")
243 assert_equal "x@example.com", email[:email]
244 assert_equal "mailto:x@example.com", email[:url]
246 email = email_link("email", "other.email-with-hyphen@example.com")
247 assert_equal "other.email-with-hyphen@example.com", email[:email]
248 assert_equal "mailto:other.email-with-hyphen@example.com", email[:url]
250 email = email_link("email", "user.name+tag+sorting@example.com")
251 assert_equal "user.name+tag+sorting@example.com", email[:email]
252 assert_equal "mailto:user.name+tag+sorting@example.com", email[:url]
254 email = email_link("email", "dash-in@both-parts.com")
255 assert_equal "dash-in@both-parts.com", email[:email]
256 assert_equal "mailto:dash-in@both-parts.com", email[:url]
258 email = email_link("email", "example@s.example")
259 assert_equal "example@s.example", email[:email]
260 assert_equal "mailto:example@s.example", email[:url]
262 # Strips whitespace at ends
263 email = email_link("email", " test@email.com ")
264 assert_equal "test@email.com", email[:email]
265 assert_equal "mailto:test@email.com", email[:url]
268 def test_telephone_links
269 links = telephone_links("foo", "Test")
272 links = telephone_links("phone", "+123")
275 links = telephone_links("phone", "123")
278 links = telephone_links("phone", "123 abcdefg")
281 links = telephone_links("phone", "+1234567890 abc")
284 # If multiple numbers are listed, all must be valid
285 links = telephone_links("phone", "+1234567890; +223")
288 links = telephone_links("phone", "1234567890")
291 links = telephone_links("phone", "+1234567890")
292 assert_equal 1, links.length
293 assert_equal "+1234567890", links[0][:phone_number]
294 assert_equal "tel:+1234567890", links[0][:url]
296 links = telephone_links("phone", "+1234-567-890")
297 assert_equal 1, links.length
298 assert_equal "+1234-567-890", links[0][:phone_number]
299 assert_equal "tel:+1234-567-890", links[0][:url]
301 links = telephone_links("phone", "+1234/567/890")
302 assert_equal 1, links.length
303 assert_equal "+1234/567/890", links[0][:phone_number]
304 assert_equal "tel:+1234/567/890", links[0][:url]
306 links = telephone_links("phone", "+1234.567.890")
307 assert_equal 1, links.length
308 assert_equal "+1234.567.890", links[0][:phone_number]
309 assert_equal "tel:+1234.567.890", links[0][:url]
311 links = telephone_links("phone", " +1234 567-890 ")
312 assert_equal 1, links.length
313 assert_equal "+1234 567-890", links[0][:phone_number]
314 assert_equal "tel:+1234567-890", links[0][:url]
316 links = telephone_links("phone", "+1 234-567-890")
317 assert_equal 1, links.length
318 assert_equal "+1 234-567-890", links[0][:phone_number]
319 assert_equal "tel:+1234-567-890", links[0][:url]
321 links = telephone_links("phone", "+1 (234) 567-890")
322 assert_equal 1, links.length
323 assert_equal "+1 (234) 567-890", links[0][:phone_number]
324 assert_equal "tel:+1(234)567-890", links[0][:url]
326 # Multiple valid phone numbers separated by ;
327 links = telephone_links("phone", "+1234567890; +22334455667788")
328 assert_equal 2, links.length
329 assert_equal "+1234567890", links[0][:phone_number]
330 assert_equal "tel:+1234567890", links[0][:url]
331 assert_equal "+22334455667788", links[1][:phone_number]
332 assert_equal "tel:+22334455667788", links[1][:url]
334 links = telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
335 assert_equal 2, links.length
336 assert_equal "+1 (234) 567-890", links[0][:phone_number]
337 assert_equal "tel:+1(234)567-890", links[0][:url]
338 assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
339 assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
342 def test_colour_preview
343 # basic positive tests
344 colour = colour_preview("colour", "red")
345 assert_equal "red", colour
347 colour = colour_preview("colour", "Red")
348 assert_equal "Red", colour
350 colour = colour_preview("colour", "darkRed")
351 assert_equal "darkRed", colour
353 colour = colour_preview("colour", "#f00")
354 assert_equal "#f00", colour
356 colour = colour_preview("colour", "#fF0000")
357 assert_equal "#fF0000", colour
359 # other tag variants:
360 colour = colour_preview("building:colour", "#f00")
361 assert_equal "#f00", colour
363 colour = colour_preview("ref:colour", "#f00")
364 assert_equal "#f00", colour
366 colour = colour_preview("int_ref:colour", "green")
367 assert_equal "green", colour
369 colour = colour_preview("roof:colour", "#f00")
370 assert_equal "#f00", colour
372 colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
373 assert_equal "#f00", colour
376 colour = colour_preview("colour", "")
379 colour = colour_preview("colour", " ")
382 colour = colour_preview("colour", nil)
385 # ignore US spelling variant
386 colour = colour_preview("color", "red")
389 # irrelevant tag names
390 colour = colour_preview("building", "red")
393 colour = colour_preview("ref:colour_no", "red")
396 colour = colour_preview("ref:colour-bg", "red")
399 colour = colour_preview("int_ref", "red")
403 colour = colour_preview("colour", "#")
406 colour = colour_preview("colour", "#ff")
409 colour = colour_preview("colour", "#ffff")
412 colour = colour_preview("colour", "#fffffff")
415 colour = colour_preview("colour", "#ggg")
418 colour = colour_preview("colour", "#ff 00 00")
421 # invalid w3c color names:
422 colour = colour_preview("colour", "r")
425 colour = colour_preview("colour", "ffffff")
428 colour = colour_preview("colour", "f00")
431 colour = colour_preview("colour", "xxxred")
434 colour = colour_preview("colour", "dark red")
437 colour = colour_preview("colour", "dark_red")
440 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")