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