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