]> git.openstreetmap.org Git - rails.git/blob - test/helpers/browse_tags_helper_test.rb
Protect I18n.locale around test which changes it
[rails.git] / test / helpers / browse_tags_helper_test.rb
1 require "test_helper"
2
3 class BrowseTagsHelperTest < ActionView::TestCase
4   include ERB::Util
5   include ApplicationHelper
6
7   def test_format_key
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
10
11     html = format_key("unknown")
12     assert_dom_equal "unknown", html
13   end
14
15   def test_format_value
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
18
19     html = format_value("highway", "unknown")
20     assert_dom_equal "unknown", html
21
22     html = format_value("unknown", "unknown")
23     assert_dom_equal "unknown", html
24
25     html = format_value("unknown", "abc;def")
26     assert_dom_equal "abc;def", html
27
28     html = format_value("unknown", "foo;")
29     assert_dom_equal "foo;", html
30
31     html = format_value("addr:street", "Rue de l'Amigo")
32     assert_dom_equal "Rue de l&#39;Amigo", html
33
34     html = format_value("phone", "+1234567890")
35     assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
36
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
39
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
42
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
45
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
49
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
52
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
55
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)
60
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
63
64     html = format_value("source", "https://example.com")
65     assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\" dir=\"auto\">https://example.com</a>", html
66
67     html = format_value("source", "https://example.com;hello;https://example.net")
68     assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\" dir=\"auto\">https://example.com</a>;hello;<a href=\"https://example.net\" rel=\"nofollow\" dir=\"auto\">https://example.net</a>", html
69   end
70
71   def test_wiki_link
72     link = wiki_link("key", "highway")
73     assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
74
75     link = wiki_link("tag", "highway=primary")
76     assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
77
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
81
82       link = wiki_link("tag", "highway=primary")
83       assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
84     end
85
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
89
90       link = wiki_link("tag", "highway=primary")
91       assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
92     end
93   end
94
95   def test_wikidata_links
96     ### Non-prefixed wikidata-tag (only one value allowed)
97
98     # Non-wikidata tag
99     links = wikidata_links("foo", "Test")
100     assert_nil links
101
102     # No URLs allowed
103     links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
104     assert_nil links
105
106     # No language-prefixes (as wikidata is multilanguage)
107     links = wikidata_links("wikidata", "en:Q1")
108     assert_nil links
109
110     # Needs a leading Q
111     links = wikidata_links("wikidata", "1")
112     assert_nil links
113
114     # No leading zeros allowed
115     links = wikidata_links("wikidata", "Q0123")
116     assert_nil links
117
118     # A valid value
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]
123
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]
130     end
131
132     ### Prefixed wikidata-tags
133
134     # Not anything is accepted as prefix (only limited set)
135     links = wikidata_links("anything:wikidata", "Q13")
136     assert_nil links
137
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]
142
143     # This verified buried is working
144     links = wikidata_links("buried:wikidata", "Q24")
145     assert_equal "//www.wikidata.org/entity/Q24?uselang=en", links[0][:url]
146     assert_equal "Q24", links[0][:title]
147
148     links = wikidata_links("species:wikidata", "Q26899")
149     assert_equal "//www.wikidata.org/entity/Q26899?uselang=en", links[0][:url]
150     assert_equal "Q26899", links[0][:title]
151
152     # Another allowed key, this time with multiple values and I18n
153     I18n.with_locale "dsb" do
154       links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
155       assert_equal 3, links.length
156       assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
157       assert_equal "Q936", links[0][:title]
158       assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
159       assert_equal "Q2013", links[1][:title]
160       assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
161       assert_equal "Q1568346", links[2][:title]
162     end
163
164     # and now with whitespaces...
165     links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
166     assert_equal 4, links.length
167     assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
168     assert_equal "Q6542248 ", links[0][:title]
169     assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
170     assert_equal "\tQ180\n ", links[1][:title]
171     assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
172     assert_equal "\rQ364\t\n\r ", links[2][:title]
173     assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
174     assert_equal "\nQ4006", links[3][:title]
175   end
176
177   def test_wikipedia_link
178     link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
179     assert_nil link
180
181     link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
182     assert_nil link
183
184     link = wikipedia_link("wikipedia", "Test")
185     assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
186     assert_equal "Test", link[:title]
187
188     link = wikipedia_link("wikipedia", "de:Test")
189     assert_equal "https://de.wikipedia.org/wiki/Test?uselang=en", link[:url]
190     assert_equal "de:Test", link[:title]
191
192     link = wikipedia_link("wikipedia:fr", "Portsea")
193     assert_equal "https://fr.wikipedia.org/wiki/Portsea?uselang=en", link[:url]
194     assert_equal "Portsea", link[:title]
195
196     link = wikipedia_link("wikipedia:fr", "de:Test")
197     assert_equal "https://de.wikipedia.org/wiki/Test?uselang=en", link[:url]
198     assert_equal "de:Test", link[:title]
199
200     link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
201     assert_equal "https://de.wikipedia.org/wiki/Englischer_Garten_%28M%C3%BCnchen%29?uselang=en#Japanisches_Teehaus", link[:url]
202     assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
203
204     link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
205     assert_equal "https://de.wikipedia.org/wiki/Alte_Br%C3%BCcke_%28Heidelberg%29?uselang=en#Br%C3%BCckenaffe", link[:url]
206     assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
207
208     link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
209     assert_equal "https://de.wikipedia.org/wiki/Liste_der_Baudenkm%C3%A4ler_in_Eichst%C3%A4tt?uselang=en#Br%C3%BCckenstra%C3%9Fe_1%2C_Ehemaliges_Bauernhaus", link[:url]
210     assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
211
212     link = wikipedia_link("wikipedia", "en:Are Years What? (for Marianne Moore)")
213     assert_equal "https://en.wikipedia.org/wiki/Are_Years_What%3F_%28for_Marianne_Moore%29?uselang=en", link[:url]
214     assert_equal "en:Are Years What? (for Marianne Moore)", link[:title]
215
216     I18n.with_locale "pt-BR" do
217       link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
218       assert_equal "https://zh-classical.wikipedia.org/wiki/Test?uselang=pt-BR#Section", link[:url]
219       assert_equal "zh-classical:Test#Section", link[:title]
220     end
221
222     link = wikipedia_link("subject:wikipedia", "en:Catherine McAuley")
223     assert_equal "https://en.wikipedia.org/wiki/Catherine_McAuley?uselang=en", link[:url]
224     assert_equal "en:Catherine McAuley", link[:title]
225
226     link = wikipedia_link("foo", "Test")
227     assert_nil link
228   end
229
230   def test_wikimedia_commons_link
231     link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
232     assert_nil link
233
234     link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
235     assert_nil link
236
237     link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
238     assert_nil link
239
240     link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
241     assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
242     assert_equal "File:Test.jpg", link[:title]
243
244     link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
245     assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
246     assert_equal "Category:Test_Category", link[:title]
247
248     link = wikimedia_commons_link("wikimedia_commons", "Category:What If? (Bonn)")
249     assert_equal "//commons.wikimedia.org/wiki/Category:What%20If%3F%20%28Bonn%29?uselang=en", link[:url]
250     assert_equal "Category:What If? (Bonn)", link[:title]
251
252     link = wikimedia_commons_link("wikimedia_commons", "File:Corsica-vizzavona-abri-southwell.jpg#mediaviewer/File:Corsica-vizzavona-abri-southwell.jpg")
253     assert_equal "//commons.wikimedia.org/wiki/File:Corsica-vizzavona-abri-southwell.jpg?uselang=en", link[:url]
254     assert_equal "File:Corsica-vizzavona-abri-southwell.jpg#mediaviewer/File:Corsica-vizzavona-abri-southwell.jpg", link[:title]
255
256     I18n.with_locale "pt-BR" do
257       link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
258       assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
259       assert_equal "File:Test.jpg", link[:title]
260     end
261
262     link = wikimedia_commons_link("foo", "Test")
263     assert_nil link
264   end
265
266   def test_email_link
267     email = email_link("foo", "Test")
268     assert_nil email
269
270     email = email_link("email", "123")
271     assert_nil email
272
273     email = email_link("email", "Abc.example.com")
274     assert_nil email
275
276     email = email_link("email", "a@b@c.com")
277     assert_nil email
278
279     email = email_link("email", "just\"not\"right@example.com")
280     assert_nil email
281
282     email = email_link("email", "123 abcdefg@space.com")
283     assert_nil email
284
285     email = email_link("email", "test@ abc")
286     assert_nil email
287
288     email = email_link("email", "using;semicolon@test.com")
289     assert_nil email
290
291     email = email_link("email", "x@example.com")
292     assert_equal "x@example.com", email
293
294     email = email_link("email", "other.email-with-hyphen@example.com")
295     assert_equal "other.email-with-hyphen@example.com", email
296
297     email = email_link("email", "user.name+tag+sorting@example.com")
298     assert_equal "user.name+tag+sorting@example.com", email
299
300     email = email_link("email", "dash-in@both-parts.com")
301     assert_equal "dash-in@both-parts.com", email
302
303     email = email_link("email", "example@s.example")
304     assert_equal "example@s.example", email
305
306     # Strips whitespace at ends
307     email = email_link("email", " test@email.com ")
308     assert_equal "test@email.com", email
309
310     email = email_link("contact:email", "example@example.com")
311     assert_equal "example@example.com", email
312
313     email = email_link("maxweight:conditional", "none@agricultural")
314     assert_nil email
315   end
316
317   def test_telephone_links
318     links = telephone_links("foo", "Test")
319     assert_nil links
320
321     links = telephone_links("phone", "+123")
322     assert_nil links
323
324     links = telephone_links("phone", "123")
325     assert_nil links
326
327     links = telephone_links("phone", "123 abcdefg")
328     assert_nil links
329
330     links = telephone_links("phone", "+1234567890 abc")
331     assert_nil links
332
333     # If multiple numbers are listed, all must be valid
334     links = telephone_links("phone", "+1234567890; +223")
335     assert_nil links
336
337     links = telephone_links("phone", "1234567890")
338     assert_nil links
339
340     links = telephone_links("phone", "+1234567890")
341     assert_equal 1, links.length
342     assert_equal "+1234567890", links[0][:phone_number]
343     assert_equal "tel:+1234567890", links[0][:url]
344
345     links = telephone_links("phone", "+1234-567-890")
346     assert_equal 1, links.length
347     assert_equal "+1234-567-890", links[0][:phone_number]
348     assert_equal "tel:+1234-567-890", links[0][:url]
349
350     links = telephone_links("phone", "+1234/567/890")
351     assert_equal 1, links.length
352     assert_equal "+1234/567/890", links[0][:phone_number]
353     assert_equal "tel:+1234/567/890", links[0][:url]
354
355     links = telephone_links("phone", "+1234.567.890")
356     assert_equal 1, links.length
357     assert_equal "+1234.567.890", links[0][:phone_number]
358     assert_equal "tel:+1234.567.890", links[0][:url]
359
360     links = telephone_links("phone", "   +1234 567-890  ")
361     assert_equal 1, links.length
362     assert_equal "+1234 567-890", links[0][:phone_number]
363     assert_equal "tel:+1234567-890", links[0][:url]
364
365     links = telephone_links("phone", "+1 234-567-890")
366     assert_equal 1, links.length
367     assert_equal "+1 234-567-890", links[0][:phone_number]
368     assert_equal "tel:+1234-567-890", links[0][:url]
369
370     links = telephone_links("phone", "+1 (234) 567-890")
371     assert_equal 1, links.length
372     assert_equal "+1 (234) 567-890", links[0][:phone_number]
373     assert_equal "tel:+1(234)567-890", links[0][:url]
374
375     # Multiple valid phone numbers separated by ;
376     links = telephone_links("phone", "+1234567890; +22334455667788")
377     assert_equal 2, links.length
378     assert_equal "+1234567890", links[0][:phone_number]
379     assert_equal "tel:+1234567890", links[0][:url]
380     assert_equal "+22334455667788", links[1][:phone_number]
381     assert_equal "tel:+22334455667788", links[1][:url]
382
383     links = telephone_links("phone", "+1 (234) 567-890 ;  +22(33)4455.66.7788 ")
384     assert_equal 2, links.length
385     assert_equal "+1 (234) 567-890", links[0][:phone_number]
386     assert_equal "tel:+1(234)567-890", links[0][:url]
387     assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
388     assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
389   end
390
391   def test_colour_preview
392     # basic positive tests
393     colour = colour_preview("colour", "red")
394     assert_equal "red", colour
395
396     colour = colour_preview("colour", "Red")
397     assert_equal "Red", colour
398
399     colour = colour_preview("colour", "darkRed")
400     assert_equal "darkRed", colour
401
402     colour = colour_preview("colour", "#f00")
403     assert_equal "#f00", colour
404
405     colour = colour_preview("colour", "#fF0000")
406     assert_equal "#fF0000", colour
407
408     # other tag variants:
409     colour = colour_preview("building:colour", "#f00")
410     assert_equal "#f00", colour
411
412     colour = colour_preview("ref:colour", "#f00")
413     assert_equal "#f00", colour
414
415     colour = colour_preview("int_ref:colour", "green")
416     assert_equal "green", colour
417
418     colour = colour_preview("roof:colour", "#f00")
419     assert_equal "#f00", colour
420
421     colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
422     assert_equal "#f00", colour
423
424     # negative tests:
425     colour = colour_preview("colour", "")
426     assert_nil colour
427
428     colour = colour_preview("colour", "   ")
429     assert_nil colour
430
431     colour = colour_preview("colour", nil)
432     assert_nil colour
433
434     # ignore US spelling variant
435     colour = colour_preview("color", "red")
436     assert_nil colour
437
438     # irrelevant tag names
439     colour = colour_preview("building", "red")
440     assert_nil colour
441
442     colour = colour_preview("ref:colour_no", "red")
443     assert_nil colour
444
445     colour = colour_preview("ref:colour-bg", "red")
446     assert_nil colour
447
448     colour = colour_preview("int_ref", "red")
449     assert_nil colour
450
451     # invalid hex codes
452     colour = colour_preview("colour", "#")
453     assert_nil colour
454
455     colour = colour_preview("colour", "#ff")
456     assert_nil colour
457
458     colour = colour_preview("colour", "#ffff")
459     assert_nil colour
460
461     colour = colour_preview("colour", "#fffffff")
462     assert_nil colour
463
464     colour = colour_preview("colour", "#ggg")
465     assert_nil colour
466
467     colour = colour_preview("colour", "#ff 00 00")
468     assert_nil colour
469
470     # invalid w3c color names:
471     colour = colour_preview("colour", "r")
472     assert_nil colour
473
474     colour = colour_preview("colour", "ffffff")
475     assert_nil colour
476
477     colour = colour_preview("colour", "f00")
478     assert_nil colour
479
480     colour = colour_preview("colour", "xxxred")
481     assert_nil colour
482
483     colour = colour_preview("colour", "dark red")
484     assert_nil colour
485
486     colour = colour_preview("colour", "dark_red")
487     assert_nil colour
488
489     colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")
490     assert_nil colour
491   end
492 end