]> git.openstreetmap.org Git - rails.git/blobdiff - test/helpers/browse_tags_helper_test.rb
change validation to accept only single email values
[rails.git] / test / helpers / browse_tags_helper_test.rb
index 195bd69de0185fa852a30ccaa0ce9e8979c3f527..fb90eb51d8ffcacf773dadfe9449793971ff68e4 100644 (file)
@@ -47,6 +47,12 @@ class BrowseTagsHelperTest < ActionView::TestCase
 
     html = format_value("name:etymology:wikidata", "Q123")
     assert_dom_equal "<a title=\"The Q123 item on Wikidata\" href=\"//www.wikidata.org/entity/Q123?uselang=en\">Q123</a>", html
+
+    html = format_value("wikimedia_commons", "File:Test.jpg")
+    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
+
+    html = format_value("colour", "#f00")
+    assert_dom_equal %(<span class="colour-preview-box" data-colour="#f00" title="Colour #f00 preview"></span>#f00), html
   end
 
   def test_wiki_link
@@ -187,6 +193,85 @@ class BrowseTagsHelperTest < ActionView::TestCase
     assert_nil link
   end
 
+  def test_wikimedia_commons_link
+    link = wikimedia_commons_link("wikimedia_commons", "http://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
+    assert_nil link
+
+    link = wikimedia_commons_link("wikimedia_commons", "https://commons.wikimedia.org/wiki/File:Full%20URL.jpg")
+    assert_nil link
+
+    link = wikimedia_commons_link("wikimedia_commons", "Test.jpg")
+    assert_nil link
+
+    link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
+    assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=en", link[:url]
+    assert_equal "File:Test.jpg", link[:title]
+
+    link = wikimedia_commons_link("wikimedia_commons", "Category:Test_Category")
+    assert_equal "//commons.wikimedia.org/wiki/Category:Test_Category?uselang=en", link[:url]
+    assert_equal "Category:Test_Category", link[:title]
+
+    I18n.locale = "pt-BR"
+
+    link = wikimedia_commons_link("wikimedia_commons", "File:Test.jpg")
+    assert_equal "//commons.wikimedia.org/wiki/File:Test.jpg?uselang=pt-BR", link[:url]
+    assert_equal "File:Test.jpg", link[:title]
+
+    link = wikimedia_commons_link("foo", "Test")
+    assert_nil link
+  end
+
+  def test_email_link
+    email = email_link("foo", "Test")
+    assert_nil email
+
+    email = email_link("email", "123")
+    assert_nil email
+
+    email = email_link("email", "Abc.example.com")
+    assert_nil email
+
+    email = email_link("email", "a@b@c.com")
+    assert_nil email
+
+    email = email_link("email", "just\"not\"right@example.com")
+    assert_nil email
+
+    email = email_link("email", "123 abcdefg@space.com")
+    assert_nil email
+
+    email = email_link("email", "test@ abc")
+    assert_nil email
+
+    email = email_link("email", "using;semicolon@test.com")
+    assert_nil email
+
+    email = email_link("email", "x@example.com")
+    assert_equal "x@example.com", email[:email]
+    assert_equal "mailto:x@example.com", email[:url]
+
+    email = email_link("email", "other.email-with-hyphen@example.com")
+    assert_equal "other.email-with-hyphen@example.com", email[:email]
+    assert_equal "mailto:other.email-with-hyphen@example.com", email[:url]
+
+    email = email_link("email", "user.name+tag+sorting@example.com")
+    assert_equal "user.name+tag+sorting@example.com", email[:email]
+    assert_equal "mailto:user.name+tag+sorting@example.com", email[:url]
+
+    email = email_link("email", "dash-in@both-parts.com")
+    assert_equal "dash-in@both-parts.com", email[:email]
+    assert_equal "mailto:dash-in@both-parts.com", email[:url]
+
+    email = email_link("email", "example@s.example")
+    assert_equal "example@s.example", email[:email]
+    assert_equal "mailto:example@s.example", email[:url]
+
+    # Strips whitespace at ends
+    email = email_link("email", " test@email.com ")
+    assert_equal "test@email.com", email[:email]
+    assert_equal "mailto:test@email.com", email[:url]
+  end
+
   def test_telephone_links
     links = telephone_links("foo", "Test")
     assert_nil links
@@ -260,4 +345,106 @@ class BrowseTagsHelperTest < ActionView::TestCase
     assert_equal "+22(33)4455.66.7788", links[1][:phone_number]
     assert_equal "tel:+22(33)4455.66.7788", links[1][:url]
   end
+
+  def test_colour_preview
+    # basic positive tests
+    colour = colour_preview("colour", "red")
+    assert_equal "red", colour
+
+    colour = colour_preview("colour", "Red")
+    assert_equal "Red", colour
+
+    colour = colour_preview("colour", "darkRed")
+    assert_equal "darkRed", colour
+
+    colour = colour_preview("colour", "#f00")
+    assert_equal "#f00", colour
+
+    colour = colour_preview("colour", "#fF0000")
+    assert_equal "#fF0000", colour
+
+    # other tag variants:
+    colour = colour_preview("building:colour", "#f00")
+    assert_equal "#f00", colour
+
+    colour = colour_preview("ref:colour", "#f00")
+    assert_equal "#f00", colour
+
+    colour = colour_preview("int_ref:colour", "green")
+    assert_equal "green", colour
+
+    colour = colour_preview("roof:colour", "#f00")
+    assert_equal "#f00", colour
+
+    colour = colour_preview("seamark:beacon_lateral:colour", "#f00")
+    assert_equal "#f00", colour
+
+    # negative tests:
+    colour = colour_preview("colour", "")
+    assert_nil colour
+
+    colour = colour_preview("colour", "   ")
+    assert_nil colour
+
+    colour = colour_preview("colour", nil)
+    assert_nil colour
+
+    # ignore US spelling variant
+    colour = colour_preview("color", "red")
+    assert_nil colour
+
+    # irrelevant tag names
+    colour = colour_preview("building", "red")
+    assert_nil colour
+
+    colour = colour_preview("ref:colour_no", "red")
+    assert_nil colour
+
+    colour = colour_preview("ref:colour-bg", "red")
+    assert_nil colour
+
+    colour = colour_preview("int_ref", "red")
+    assert_nil colour
+
+    # invalid hex codes
+    colour = colour_preview("colour", "#")
+    assert_nil colour
+
+    colour = colour_preview("colour", "#ff")
+    assert_nil colour
+
+    colour = colour_preview("colour", "#ffff")
+    assert_nil colour
+
+    colour = colour_preview("colour", "#fffffff")
+    assert_nil colour
+
+    colour = colour_preview("colour", "#ggg")
+    assert_nil colour
+
+    colour = colour_preview("colour", "#ff 00 00")
+    assert_nil colour
+
+    # invalid w3c color names:
+    colour = colour_preview("colour", "r")
+    assert_nil colour
+
+    colour = colour_preview("colour", "ffffff")
+    assert_nil colour
+
+    colour = colour_preview("colour", "f00")
+    assert_nil colour
+
+    colour = colour_preview("colour", "xxxred")
+    assert_nil colour
+
+    colour = colour_preview("colour", "dark red")
+    assert_nil colour
+
+    colour = colour_preview("colour", "dark_red")
+    assert_nil colour
+
+    colour = colour_preview("colour", "ADarkDummyLongColourNameWithAPurpleUndertone")
+    assert_nil colour
+  end
 end