]> git.openstreetmap.org Git - rails.git/blob - test/helpers/browse_tags_helper_test.rb
Preview colour tag value using svg
[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     links = wikidata_links("species:wikidata", "Q26899")
144     assert_equal "//www.wikidata.org/entity/Q26899?uselang=en", links[0][:url]
145     assert_equal "Q26899", links[0][:title]
146
147     # Another allowed key, this time with multiple values and I18n
148     I18n.with_locale "dsb" do
149       links = wikidata_links("brand:wikidata", "Q936;Q2013;Q1568346")
150       assert_equal 3, links.length
151       assert_equal "//www.wikidata.org/entity/Q936?uselang=dsb", links[0][:url]
152       assert_equal "Q936", links[0][:title]
153       assert_equal "//www.wikidata.org/entity/Q2013?uselang=dsb", links[1][:url]
154       assert_equal "Q2013", links[1][:title]
155       assert_equal "//www.wikidata.org/entity/Q1568346?uselang=dsb", links[2][:url]
156       assert_equal "Q1568346", links[2][:title]
157     end
158
159     # and now with whitespaces...
160     links = wikidata_links("subject:wikidata", "Q6542248 ;\tQ180\n ;\rQ364\t\n\r ;\nQ4006")
161     assert_equal 4, links.length
162     assert_equal "//www.wikidata.org/entity/Q6542248?uselang=en", links[0][:url]
163     assert_equal "Q6542248 ", links[0][:title]
164     assert_equal "//www.wikidata.org/entity/Q180?uselang=en", links[1][:url]
165     assert_equal "\tQ180\n ", links[1][:title]
166     assert_equal "//www.wikidata.org/entity/Q364?uselang=en", links[2][:url]
167     assert_equal "\rQ364\t\n\r ", links[2][:title]
168     assert_equal "//www.wikidata.org/entity/Q4006?uselang=en", links[3][:url]
169     assert_equal "\nQ4006", links[3][:title]
170   end
171
172   def test_wikipedia_link
173     link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
174     assert_nil link
175
176     link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
177     assert_nil link
178
179     link = wikipedia_link("wikipedia", "Test")
180     assert_equal "https://en.wikipedia.org/wiki/Test?uselang=en", link[:url]
181     assert_equal "Test", link[:title]
182
183     link = wikipedia_link("wikipedia", "de:Test")
184     assert_equal "https://de.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
185     assert_equal "de:Test", link[:title]
186
187     link = wikipedia_link("wikipedia:fr", "de:Test")
188     assert_equal "https://fr.wikipedia.org/wiki/de:Test?uselang=en", link[:url]
189     assert_equal "de:Test", link[:title]
190
191     link = wikipedia_link("wikipedia", "de:Englischer Garten (München)#Japanisches Teehaus")
192     assert_equal "https://de.wikipedia.org/wiki/de:Englischer Garten (München)?uselang=en#Japanisches_Teehaus", link[:url]
193     assert_equal "de:Englischer Garten (München)#Japanisches Teehaus", link[:title]
194
195     link = wikipedia_link("wikipedia", "de:Alte Brücke (Heidelberg)#Brückenaffe")
196     assert_equal "https://de.wikipedia.org/wiki/de:Alte Brücke (Heidelberg)?uselang=en#Br%C3%BCckenaffe", link[:url]
197     assert_equal "de:Alte Brücke (Heidelberg)#Brückenaffe", link[:title]
198
199     link = wikipedia_link("wikipedia", "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus")
200     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]
201     assert_equal "de:Liste der Baudenkmäler in Eichstätt#Brückenstraße 1, Ehemaliges Bauernhaus", link[:title]
202
203     I18n.with_locale "pt-BR" do
204       link = wikipedia_link("wikipedia", "zh-classical:Test#Section")
205       assert_equal "https://zh-classical.wikipedia.org/wiki/zh-classical:Test?uselang=pt-BR#Section", link[:url]
206       assert_equal "zh-classical:Test#Section", link[:title]
207     end
208
209     link = wikipedia_link("subject:wikipedia", "en:Catherine McAuley")
210     assert_equal "https://en.wikipedia.org/wiki/en:Catherine McAuley?uselang=en", link[:url]
211     assert_equal "en:Catherine McAuley", link[:title]
212
213     link = wikipedia_link("foo", "Test")
214     assert_nil link
215   end
216
217   def test_wikimedia_commons_link
218     link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
219     assert_nil link
220
221     link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
222     assert_nil link
223
224     link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
225     assert_nil link
226
227     link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
228     assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
229     assert_equal "File:Test.jpg", link[:title]
230
231     link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
232     assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
233     assert_equal "Category:Test_Category", link[:title]
234
235     I18n.with_locale "pt-BR" do
236       link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
237       assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
238       assert_equal "File:Test.jpg", link[:title]
239     end
240
241     link = wikimedia_commons_link("foo", "Test")
242     assert_nil link
243   end
244
245   def test_email_link
246     email = email_link("foo", "Test")
247     assert_nil email
248
249     email = email_link("email", "123")
250     assert_nil email
251
252     email = email_link("email", "Abc.example.com")
253     assert_nil email
254
255     email = email_link("email", "a@b@c.com")
256     assert_nil email
257
258     email = email_link("email", "just\"not\"right@example.com")
259     assert_nil email
260
261     email = email_link("email", "123 abcdefg@space.com")
262     assert_nil email
263
264     email = email_link("email", "test@ abc")
265     assert_nil email
266
267     email = email_link("email", "using;semicolon@test.com")
268     assert_nil email
269
270     email = email_link("email", "x@example.com")
271     assert_equal "x@example.com", email
272
273     email = email_link("email", "other.email-with-hyphen@example.com")
274     assert_equal "other.email-with-hyphen@example.com", email
275
276     email = email_link("email", "user.name+tag+sorting@example.com")
277     assert_equal "user.name+tag+sorting@example.com", email
278
279     email = email_link("email", "dash-in@both-parts.com")
280     assert_equal "dash-in@both-parts.com", email
281
282     email = email_link("email", "example@s.example")
283     assert_equal "example@s.example", email
284
285     # Strips whitespace at ends
286     email = email_link("email", " test@email.com ")
287     assert_equal "test@email.com", email
288
289     email = email_link("contact:email", "example@example.com")
290     assert_equal "example@example.com", email
291
292     email = email_link("maxweight:conditional", "none@agricultural")
293     assert_nil email
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