]> git.openstreetmap.org Git - rails.git/blob - test/helpers/browse_tags_helper_test.rb
Keep trailing semicolons in formatted tag values
[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     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
58
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
61
62     html = format_value("source", "https://example.com")
63     assert_dom_equal "<a href=\"https://example.com\" rel=\"nofollow\">https://example.com</a>", html
64
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
67   end
68
69   def test_wiki_link
70     link = wiki_link("key", "highway")
71     assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
72
73     link = wiki_link("tag", "highway=primary")
74     assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
75
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
79
80       link = wiki_link("tag", "highway=primary")
81       assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
82     end
83
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
87
88       link = wiki_link("tag", "highway=primary")
89       assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
90     end
91   end
92
93   def test_wikidata_links
94     ### Non-prefixed wikidata-tag (only one value allowed)
95
96     # Non-wikidata tag
97     links = wikidata_links("foo", "Test")
98     assert_nil links
99
100     # No URLs allowed
101     links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
102     assert_nil links
103
104     # No language-prefixes (as wikidata is multilanguage)
105     links = wikidata_links("wikidata", "en:Q1")
106     assert_nil links
107
108     # Needs a leading Q
109     links = wikidata_links("wikidata", "1")
110     assert_nil links
111
112     # No leading zeros allowed
113     links = wikidata_links("wikidata", "Q0123")
114     assert_nil links
115
116     # A valid value
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]
121
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]
128     end
129
130     ### Prefixed wikidata-tags
131
132     # Not anything is accepted as prefix (only limited set)
133     links = wikidata_links("anything:wikidata", "Q13")
134     assert_nil links
135
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]
140
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]
144
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]
155     end
156
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]
168   end
169
170   def test_wikipedia_link
171     link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
172     assert_nil link
173
174     link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
175     assert_nil link
176
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]
180
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]
184
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]
188
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]
192
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]
196
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]
200
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]
205     end
206
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]
210
211     link = wikipedia_link("foo", "Test")
212     assert_nil link
213   end
214
215   def test_wikimedia_commons_link
216     link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
217     assert_nil link
218
219     link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
220     assert_nil link
221
222     link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
223     assert_nil link
224
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]
228
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]
232
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]
237     end
238
239     link = wikimedia_commons_link("foo", "Test")
240     assert_nil link
241   end
242
243   def test_email_link
244     email = email_link("foo", "Test")
245     assert_nil email
246
247     email = email_link("email", "123")
248     assert_nil email
249
250     email = email_link("email", "Abc.example.com")
251     assert_nil email
252
253     email = email_link("email", "a@b@c.com")
254     assert_nil email
255
256     email = email_link("email", "just\"not\"right@example.com")
257     assert_nil email
258
259     email = email_link("email", "123 abcdefg@space.com")
260     assert_nil email
261
262     email = email_link("email", "test@ abc")
263     assert_nil email
264
265     email = email_link("email", "using;semicolon@test.com")
266     assert_nil email
267
268     email = email_link("email", "x@example.com")
269     assert_equal "x@example.com", email
270
271     email = email_link("email", "other.email-with-hyphen@example.com")
272     assert_equal "other.email-with-hyphen@example.com", email
273
274     email = email_link("email", "user.name+tag+sorting@example.com")
275     assert_equal "user.name+tag+sorting@example.com", email
276
277     email = email_link("email", "dash-in@both-parts.com")
278     assert_equal "dash-in@both-parts.com", email
279
280     email = email_link("email", "example@s.example")
281     assert_equal "example@s.example", email
282
283     # Strips whitespace at ends
284     email = email_link("email", " test@email.com ")
285     assert_equal "test@email.com", email
286
287     email = email_link("contact:email", "example@example.com")
288     assert_equal "example@example.com", email
289
290     email = email_link("maxweight:conditional", "none@agricultural")
291     assert_nil email
292   end
293
294   def test_telephone_links
295     links = telephone_links("foo", "Test")
296     assert_nil links
297
298     links = telephone_links("phone", "+123")
299     assert_nil links
300
301     links = telephone_links("phone", "123")
302     assert_nil links
303
304     links = telephone_links("phone", "123 abcdefg")
305     assert_nil links
306
307     links = telephone_links("phone", "+1234567890 abc")
308     assert_nil links
309
310     # If multiple numbers are listed, all must be valid
311     links = telephone_links("phone", "+1234567890; +223")
312     assert_nil links
313
314     links = telephone_links("phone", "1234567890")
315     assert_nil links
316
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]
321
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]
326
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]
331
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]
336
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]
341
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]
346
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]
351
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]
359
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]
366   end
367
368   def test_colour_preview
369     # basic positive tests
370     colour = colour_preview("colour", "red")
371     assert_equal "red", colour
372
373     colour = colour_preview("colour", "Red")
374     assert_equal "Red", colour
375
376     colour = colour_preview("colour", "darkRed")
377     assert_equal "darkRed", colour
378
379     colour = colour_preview("colour", "#f00")
380     assert_equal "#f00", colour
381
382     colour = colour_preview("colour", "#fF0000")
383     assert_equal "#fF0000", colour
384
385     # other tag variants:
386     colour = colour_preview("building:colour", "#f00")
387     assert_equal "#f00", colour
388
389     colour = colour_preview("ref:colour", "#f00")
390     assert_equal "#f00", colour
391
392     colour = colour_preview("int_ref:colour", "green")
393     assert_equal "green", colour
394
395     colour = colour_preview("roof:colour", "#f00")
396     assert_equal "#f00", colour
397
398     colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
399     assert_equal "#f00", colour
400
401     # negative tests:
402     colour = colour_preview("colour", "")
403     assert_nil colour
404
405     colour = colour_preview("colour", "   ")
406     assert_nil colour
407
408     colour = colour_preview("colour", nil)
409     assert_nil colour
410
411     # ignore US spelling variant
412     colour = colour_preview("color", "red")
413     assert_nil colour
414
415     # irrelevant tag names
416     colour = colour_preview("building", "red")
417     assert_nil colour
418
419     colour = colour_preview("ref:colour_no", "red")
420     assert_nil colour
421
422     colour = colour_preview("ref:colour-bg", "red")
423     assert_nil colour
424
425     colour = colour_preview("int_ref", "red")
426     assert_nil colour
427
428     # invalid hex codes
429     colour = colour_preview("colour", "#")
430     assert_nil colour
431
432     colour = colour_preview("colour", "#ff")
433     assert_nil colour
434
435     colour = colour_preview("colour", "#ffff")
436     assert_nil colour
437
438     colour = colour_preview("colour", "#fffffff")
439     assert_nil colour
440
441     colour = colour_preview("colour", "#ggg")
442     assert_nil colour
443
444     colour = colour_preview("colour", "#ff 00 00")
445     assert_nil colour
446
447     # invalid w3c color names:
448     colour = colour_preview("colour", "r")
449     assert_nil colour
450
451     colour = colour_preview("colour", "ffffff")
452     assert_nil colour
453
454     colour = colour_preview("colour", "f00")
455     assert_nil colour
456
457     colour = colour_preview("colour", "xxxred")
458     assert_nil colour
459
460     colour = colour_preview("colour", "dark red")
461     assert_nil colour
462
463     colour = colour_preview("colour", "dark_red")
464     assert_nil colour
465
466     colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")
467     assert_nil colour
468   end
469 end