]> git.openstreetmap.org Git - rails.git/blob - test/helpers/browse_tags_helper_test.rb
Merge pull request #4401 from KasperFranz/buried-wikidata
[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\">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\">https://example.com</a>;hello;<a href=\"https://example.net\" rel=\"nofollow\">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/de:Test?uselang=en", link[:url]
190     assert_equal "de:Test", link[:title]
191
192     link = wikipedia_link("wikipedia:fr", "de:Test")
193     assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
194     assert_equal "de:Test", link[:title]
195
196     link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
197     assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
198     assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
199
200     link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
201     assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br%C3%BCckenaffe", link[:url]
202     assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
203
204     link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
205     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]
206     assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
207
208     I18n.with_locale "pt-BR" do
209       link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
210       assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
211       assert_equal "zh-classical:Test#Section", link[:title]
212     end
213
214     link = wikipedia_link("subject:wikipedia", "en:Catherine McAuley")
215     assert_equal "https://en.wikipedia.org/wiki/en:Catherine McAuley?uselang=en", link[:url]
216     assert_equal "en:Catherine McAuley", link[:title]
217
218     link = wikipedia_link("foo", "Test")
219     assert_nil link
220   end
221
222   def test_wikimedia_commons_link
223     link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
224     assert_nil link
225
226     link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
227     assert_nil link
228
229     link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
230     assert_nil link
231
232     link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
233     assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
234     assert_equal "File:Test.jpg", link[:title]
235
236     link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
237     assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
238     assert_equal "Category:Test_Category", link[:title]
239
240     I18n.with_locale "pt-BR" do
241       link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
242       assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
243       assert_equal "File:Test.jpg", link[:title]
244     end
245
246     link = wikimedia_commons_link("foo", "Test")
247     assert_nil link
248   end
249
250   def test_email_link
251     email = email_link("foo", "Test")
252     assert_nil email
253
254     email = email_link("email", "123")
255     assert_nil email
256
257     email = email_link("email", "Abc.example.com")
258     assert_nil email
259
260     email = email_link("email", "a@b@c.com")
261     assert_nil email
262
263     email = email_link("email", "just\"not\"right@example.com")
264     assert_nil email
265
266     email = email_link("email", "123 abcdefg@space.com")
267     assert_nil email
268
269     email = email_link("email", "test@ abc")
270     assert_nil email
271
272     email = email_link("email", "using;semicolon@test.com")
273     assert_nil email
274
275     email = email_link("email", "x@example.com")
276     assert_equal "x@example.com", email
277
278     email = email_link("email", "other.email-with-hyphen@example.com")
279     assert_equal "other.email-with-hyphen@example.com", email
280
281     email = email_link("email", "user.name+tag+sorting@example.com")
282     assert_equal "user.name+tag+sorting@example.com", email
283
284     email = email_link("email", "dash-in@both-parts.com")
285     assert_equal "dash-in@both-parts.com", email
286
287     email = email_link("email", "example@s.example")
288     assert_equal "example@s.example", email
289
290     # Strips whitespace at ends
291     email = email_link("email", " test@email.com ")
292     assert_equal "test@email.com", email
293
294     email = email_link("contact:email", "example@example.com")
295     assert_equal "example@example.com", email
296
297     email = email_link("maxweight:conditional", "none@agricultural")
298     assert_nil email
299   end
300
301   def test_telephone_links
302     links = telephone_links("foo", "Test")
303     assert_nil links
304
305     links = telephone_links("phone", "+123")
306     assert_nil links
307
308     links = telephone_links("phone", "123")
309     assert_nil links
310
311     links = telephone_links("phone", "123 abcdefg")
312     assert_nil links
313
314     links = telephone_links("phone", "+1234567890 abc")
315     assert_nil links
316
317     # If multiple numbers are listed, all must be valid
318     links = telephone_links("phone", "+1234567890; +223")
319     assert_nil links
320
321     links = telephone_links("phone", "1234567890")
322     assert_nil links
323
324     links = telephone_links("phone", "+1234567890")
325     assert_equal 1, links.length
326     assert_equal "+1234567890", links[0][:phone_number]
327     assert_equal "tel:+1234567890", links[0][:url]
328
329     links = telephone_links("phone", "+1234-567-890")
330     assert_equal 1, links.length
331     assert_equal "+1234-567-890", links[0][:phone_number]
332     assert_equal "tel:+1234-567-890", links[0][:url]
333
334     links = telephone_links("phone", "+1234/567/890")
335     assert_equal 1, links.length
336     assert_equal "+1234/567/890", links[0][:phone_number]
337     assert_equal "tel:+1234/567/890", links[0][:url]
338
339     links = telephone_links("phone", "+1234.567.890")
340     assert_equal 1, links.length
341     assert_equal "+1234.567.890", links[0][:phone_number]
342     assert_equal "tel:+1234.567.890", links[0][:url]
343
344     links = telephone_links("phone", "   +1234 567-890  ")
345     assert_equal 1, links.length
346     assert_equal "+1234 567-890", links[0][:phone_number]
347     assert_equal "tel:+1234567-890", links[0][:url]
348
349     links = telephone_links("phone", "+1 234-567-890")
350     assert_equal 1, links.length
351     assert_equal "+1 234-567-890", links[0][:phone_number]
352     assert_equal "tel:+1234-567-890", links[0][:url]
353
354     links = telephone_links("phone", "+1 (234) 567-890")
355     assert_equal 1, links.length
356     assert_equal "+1 (234) 567-890", links[0][:phone_number]
357     assert_equal "tel:+1(234)567-890", links[0][:url]
358
359     # Multiple valid phone numbers separated by ;
360     links = telephone_links("phone", "+1234567890; +22334455667788")
361     assert_equal 2, links.length
362     assert_equal "+1234567890", links[0][:phone_number]
363     assert_equal "tel:+1234567890", links[0][:url]
364     assert_equal "+22334455667788", links[1][:phone_number]
365     assert_equal "tel:+22334455667788", links[1][:url]
366
367     links = telephone_links("phone", "+1 (234) 567-890 ;  +22(33)4455.66.7788 ")
368     assert_equal 2, links.length
369     assert_equal "+1 (234) 567-890", links[0][:phone_number]
370     assert_equal "tel:+1(234)567-890", links[0][:url]
371     assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
372     assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
373   end
374
375   def test_colour_preview
376     # basic positive tests
377     colour = colour_preview("colour", "red")
378     assert_equal "red", colour
379
380     colour = colour_preview("colour", "Red")
381     assert_equal "Red", colour
382
383     colour = colour_preview("colour", "darkRed")
384     assert_equal "darkRed", colour
385
386     colour = colour_preview("colour", "#f00")
387     assert_equal "#f00", colour
388
389     colour = colour_preview("colour", "#fF0000")
390     assert_equal "#fF0000", colour
391
392     # other tag variants:
393     colour = colour_preview("building:colour", "#f00")
394     assert_equal "#f00", colour
395
396     colour = colour_preview("ref:colour", "#f00")
397     assert_equal "#f00", colour
398
399     colour = colour_preview("int_ref:colour", "green")
400     assert_equal "green", colour
401
402     colour = colour_preview("roof:colour", "#f00")
403     assert_equal "#f00", colour
404
405     colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
406     assert_equal "#f00", colour
407
408     # negative tests:
409     colour = colour_preview("colour", "")
410     assert_nil colour
411
412     colour = colour_preview("colour", "   ")
413     assert_nil colour
414
415     colour = colour_preview("colour", nil)
416     assert_nil colour
417
418     # ignore US spelling variant
419     colour = colour_preview("color", "red")
420     assert_nil colour
421
422     # irrelevant tag names
423     colour = colour_preview("building", "red")
424     assert_nil colour
425
426     colour = colour_preview("ref:colour_no", "red")
427     assert_nil colour
428
429     colour = colour_preview("ref:colour-bg", "red")
430     assert_nil colour
431
432     colour = colour_preview("int_ref", "red")
433     assert_nil colour
434
435     # invalid hex codes
436     colour = colour_preview("colour", "#")
437     assert_nil colour
438
439     colour = colour_preview("colour", "#ff")
440     assert_nil colour
441
442     colour = colour_preview("colour", "#ffff")
443     assert_nil colour
444
445     colour = colour_preview("colour", "#fffffff")
446     assert_nil colour
447
448     colour = colour_preview("colour", "#ggg")
449     assert_nil colour
450
451     colour = colour_preview("colour", "#ff 00 00")
452     assert_nil colour
453
454     # invalid w3c color names:
455     colour = colour_preview("colour", "r")
456     assert_nil colour
457
458     colour = colour_preview("colour", "ffffff")
459     assert_nil colour
460
461     colour = colour_preview("colour", "f00")
462     assert_nil colour
463
464     colour = colour_preview("colour", "xxxred")
465     assert_nil colour
466
467     colour = colour_preview("colour", "dark red")
468     assert_nil colour
469
470     colour = colour_preview("colour", "dark_red")
471     assert_nil colour
472
473     colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")
474     assert_nil colour
475   end
476 end