3 class BrowseTagsHelperTest < ActionView::TestCase
5 include ApplicationHelper
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
19 html = format_key("unknown")
20 assert_dom_equal "unknown", html
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
27 html = format_value("highway", "unknown")
28 assert_dom_equal "unknown", html
30 html = format_value("unknown", "unknown")
31 assert_dom_equal "unknown", html
33 html = format_value("phone", "+1234567890")
34 assert_dom_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
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
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
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
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
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
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
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
59 link = wiki_link("key", "highway")
60 assert_equal "https://wiki.openstreetmap.org/wiki/Key:highway?uselang=en", link
62 link = wiki_link("tag", "highway=primary")
63 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=en", link
67 link = wiki_link("key", "highway")
68 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Key:highway?uselang=de", link
70 link = wiki_link("tag", "highway=primary")
71 assert_equal "https://wiki.openstreetmap.org/wiki/DE:Tag:highway=primary?uselang=de", link
75 link = wiki_link("key", "highway")
76 assert_equal "https://wiki.openstreetmap.org/wiki/Tr:Key:highway?uselang=tr", link
78 link = wiki_link("tag", "highway=primary")
79 assert_equal "https://wiki.openstreetmap.org/wiki/Tag:highway=primary?uselang=tr", link
82 def test_wikidata_links
83 ### Non-prefixed wikidata-tag (only one value allowed)
86 links = wikidata_links("foo", "Test")
90 links = wikidata_links("wikidata", "http://www.wikidata.org/entity/Q1")
93 # No language-prefixes (as wikidata is multilanguage)
94 links = wikidata_links("wikidata", "en:Q1")
98 links = wikidata_links("wikidata", "1")
101 # No leading zeros allowed
102 links = wikidata_links("wikidata", "Q0123")
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]
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]
119 ### Prefixed wikidata-tags
121 # Not anything is accepted as prefix (only limited set)
122 links = wikidata_links("anything:wikidata", "Q13")
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]
130 # Another allowed key, this time with multiple values and I18n
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]
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]
155 def test_wikipedia_link
156 link = wikipedia_link("wikipedia", "http://en.wikipedia.org/wiki/Full%20URL")
159 link = wikipedia_link("wikipedia", "https://en.wikipedia.org/wiki/Full%20URL")
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]
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]
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]
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]
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]
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]
186 I18n.locale = "pt-BR"
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]
192 link = wikipedia_link("foo", "Test")
196 def test_wikimedia_commons_link
197 link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
200 link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
203 link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
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]
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]
214 I18n.locale = "pt-BR"
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]
220 link = wikimedia_commons_link("foo", "Test")
224 def test_telephone_links
225 links = telephone_links("foo", "Test")
228 links = telephone_links("phone", "+123")
231 links = telephone_links("phone", "123")
234 links = telephone_links("phone", "123 abcdefg")
237 links = telephone_links("phone", "+1234567890 abc")
240 # If multiple numbers are listed, all must be valid
241 links = telephone_links("phone", "+1234567890; +223")
244 links = telephone_links("phone", "1234567890")
247 links = telephone_links("phone", "+1234567890")
248 assert_equal 1, links.length
249 assert_equal "+1234567890", links[0][:phone_number]
250 assert_equal "tel:+1234567890", links[0][:url]
252 links = telephone_links("phone", "+1234-567-890")
253 assert_equal 1, links.length
254 assert_equal "+1234-567-890", links[0][:phone_number]
255 assert_equal "tel:+1234-567-890", links[0][:url]
257 links = telephone_links("phone", "+1234/567/890")
258 assert_equal 1, links.length
259 assert_equal "+1234/567/890", links[0][:phone_number]
260 assert_equal "tel:+1234/567/890", links[0][:url]
262 links = telephone_links("phone", "+1234.567.890")
263 assert_equal 1, links.length
264 assert_equal "+1234.567.890", links[0][:phone_number]
265 assert_equal "tel:+1234.567.890", links[0][:url]
267 links = telephone_links("phone", " +1234 567-890 ")
268 assert_equal 1, links.length
269 assert_equal "+1234 567-890", links[0][:phone_number]
270 assert_equal "tel:+1234567-890", links[0][:url]
272 links = telephone_links("phone", "+1 234-567-890")
273 assert_equal 1, links.length
274 assert_equal "+1 234-567-890", links[0][:phone_number]
275 assert_equal "tel:+1234-567-890", links[0][:url]
277 links = telephone_links("phone", "+1 (234) 567-890")
278 assert_equal 1, links.length
279 assert_equal "+1 (234) 567-890", links[0][:phone_number]
280 assert_equal "tel:+1(234)567-890", links[0][:url]
282 # Multiple valid phone numbers separated by ;
283 links = telephone_links("phone", "+1234567890; +22334455667788")
284 assert_equal 2, links.length
285 assert_equal "+1234567890", links[0][:phone_number]
286 assert_equal "tel:+1234567890", links[0][:url]
287 assert_equal "+22334455667788", links[1][:phone_number]
288 assert_equal "tel:+22334455667788", links[1][:url]
290 links = telephone_links("phone", "+1 (234) 567-890 ; +22(33)4455.66.7788 ")
291 assert_equal 2, links.length
292 assert_equal "+1 (234) 567-890", links[0][:phone_number]
293 assert_equal "tel:+1(234)567-890", links[0][:url]
294 assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
295 assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
298 def test_colour_preview
299 # basic positive tests
300 colour = colour_preview("colour", "red")
301 assert_equal "red", colour
303 colour = colour_preview("colour", "Red")
304 assert_equal "Red", colour
306 colour = colour_preview("colour", "darkRed")
307 assert_equal "darkRed", colour
309 colour = colour_preview("colour", "#f00")
310 assert_equal "#f00", colour
312 colour = colour_preview("colour", "#fF0000")
313 assert_equal "#fF0000", colour
315 # other tag variants:
316 colour = colour_preview("building:colour", "#f00")
317 assert_equal "#f00", colour
319 colour = colour_preview("ref:colour", "#f00")
320 assert_equal "#f00", colour
322 colour = colour_preview("int_ref:colour", "green")
323 assert_equal "green", colour
325 colour = colour_preview("roof:colour", "#f00")
326 assert_equal "#f00", colour
328 colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
329 assert_equal "#f00", colour
332 colour = colour_preview("colour", "")
335 colour = colour_preview("colour", " ")
338 colour = colour_preview("colour", nil)
341 # ignore US spelling variant
342 colour = colour_preview("color", "red")
345 # irrelevant tag names
346 colour = colour_preview("building", "red")
349 colour = colour_preview("ref:colour_no", "red")
352 colour = colour_preview("ref:colour-bg", "red")
355 colour = colour_preview("int_ref", "red")
359 colour = colour_preview("colour", "#")
362 colour = colour_preview("colour", "#ff")
365 colour = colour_preview("colour", "#ffff")
368 colour = colour_preview("colour", "#fffffff")
371 colour = colour_preview("colour", "#ggg")
374 colour = colour_preview("colour", "#ff 00 00")
377 # invalid w3c color names:
378 colour = colour_preview("colour", "r")
381 colour = colour_preview("colour", "ffffff")
384 colour = colour_preview("colour", "f00")
387 colour = colour_preview("colour", "xxxred")
390 colour = colour_preview("colour", "dark red")
393 colour = colour_preview("colour", "dark_red")
396 colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")