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