asset_tag_helper_test.rb 9.3 KB
Newer Older
1
require File.dirname(__FILE__) + '/../abstract_unit'
2 3 4 5 6 7 8 9

class AssetTagHelperTest < Test::Unit::TestCase
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::AssetTagHelper

  def setup
    @controller = Class.new do
10 11

      attr_accessor :request
12
    
13
      def url_for(options, *parameters_for_method_reference)
14
        "http://www.example.com"
15
      end
16 17 18 19 20 21 22 23
      
    end.new
    
    @request = Class.new do 
      def relative_url_root
        ""
      end       
    end.new
24 25

    @controller.request = @request
26
    
27 28 29
  end

  AutoDiscoveryToTag = {
30 31 32
    %(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
    %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
    %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
33
    %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
34 35 36 37 38 39
    %(auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
    %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
    %(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="" type="text/html" />),
    %(auto_discovery_link_tag(nil, {}, {:title => "No stream.. really", :type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="No stream.. really" type="text/html" />),
    %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS", :type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="text/html" />),
    %(auto_discovery_link_tag(:atom, {}, {:rel => "Not so alternate"})) => %(<link href="http://www.example.com" rel="Not so alternate" title="ATOM" type="application/atom+xml" />),
40 41
  }

42 43
  JavascriptPathToTag = {
    %(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js),
44
    %(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js)
45 46
  }

47
  JavascriptIncludeToTag = {
D
David Heinemeier Hansson 已提交
48
    %(javascript_include_tag("xmlhr")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
49
    %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(<script lang="vbscript" src="/javascripts/xmlhr.js" type="text/javascript"></script>),
D
David Heinemeier Hansson 已提交
50
    %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(<script src="/javascripts/common.javascript" type="text/javascript"></script>\n<script src="/elsewhere/cools.js" type="text/javascript"></script>),
51 52
  }

53 54
  StylePathToTag = {
    %(stylesheet_path("style")) => %(/stylesheets/style.css),
55 56
    %(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css),
    %(stylesheet_path('/dir/file')) => %(/dir/file.css)
57 58
  }

59 60
  StyleLinkToTag = {
    %(stylesheet_link_tag("style")) => %(<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
61 62
    %(stylesheet_link_tag("/dir/file")) => %(<link href="/dir/file.css" media="screen" rel="Stylesheet" type="text/css" />),
    %(stylesheet_link_tag("dir/file")) => %(<link href="/stylesheets/dir/file.css" media="screen" rel="Stylesheet" type="text/css" />),
63
    %(stylesheet_link_tag("style", :media => "all")) => %(<link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" />),
64 65 66
    %(stylesheet_link_tag("random.styles", "/css/stylish")) => %(<link href="/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />)
  }

67 68 69 70
  ImagePathToTag = {
    %(image_path("xml")) => %(/images/xml.png),
  }

71 72 73 74 75 76 77
  ImageLinkToTag = {
    %(image_tag("xml")) => %(<img alt="Xml" src="/images/xml.png" />),
    %(image_tag("rss", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.png" />),
    %(image_tag("gold", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
  }

  def test_auto_discovery
78
    AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
79 80
  end

81
  def test_javascript_path
82
    JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
83 84
  end

85
  def test_javascript_include
86
    JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
87 88
  end

89
  def test_style_path
90
    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
91 92
  end

93
  def test_style_link
94
    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
95 96
  end

97
  def test_image_path
98
    ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
99 100
  end

101
  def test_image_tag
102
    ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
103 104 105 106 107 108 109 110 111 112 113 114
  end
  
end

class AssetTagHelperNonVhostTest < Test::Unit::TestCase
  include ActionView::Helpers::TagHelper
  include ActionView::Helpers::UrlHelper
  include ActionView::Helpers::AssetTagHelper

  def setup
    @controller = Class.new do
    
115 116
      attr_accessor :request

117 118 119 120 121 122 123 124 125 126 127 128
      def url_for(options, *parameters_for_method_reference)
        "http://www.example.com/calloboration/hieraki"
      end
      
    end.new
    
    @request = Class.new do 
      def relative_url_root
        "/calloboration/hieraki"
      end
    end.new
    
129 130
    @controller.request = @request

131 132 133 134 135 136 137 138
  end

  AutoDiscoveryToTag = {
    %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com/calloboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />),
    %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com/calloboration/hieraki" rel="alternate" title="ATOM" type="application/atom+xml" />),
    %(auto_discovery_link_tag) => %(<link href="http://www.example.com/calloboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />),
  }

139 140 141 142
  JavascriptPathToTag = {
    %(javascript_path("xmlhr")) => %(/calloboration/hieraki/javascripts/xmlhr.js),
  }

143
  JavascriptIncludeToTag = {
D
David Heinemeier Hansson 已提交
144 145
    %(javascript_include_tag("xmlhr")) => %(<script src="/calloboration/hieraki/javascripts/xmlhr.js" type="text/javascript"></script>),
    %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(<script src="/calloboration/hieraki/javascripts/common.javascript" type="text/javascript"></script>\n<script src="/calloboration/hieraki/elsewhere/cools.js" type="text/javascript"></script>),
146
    %(javascript_include_tag(:defaults)) => %(<script src='/calloboration/hieraki/javascripts/prototype.js' type='text/javascript'></script>\n<script  src='/calloboration/hieraki/javascripts/scriptaculous.js' type='text/javascript'></script>),
147 148
  }

149 150 151 152
  StylePathToTag = {
    %(stylesheet_path("style")) => %(/calloboration/hieraki/stylesheets/style.css),
  }

153 154 155 156
  StyleLinkToTag = {
    %(stylesheet_link_tag("style")) => %(<link href="/calloboration/hieraki/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
    %(stylesheet_link_tag("random.styles", "/css/stylish")) => %(<link href="/calloboration/hieraki/stylesheets/random.styles" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/calloboration/hieraki/css/stylish.css" media="screen" rel="Stylesheet" type="text/css" />)
  }
157 158 159 160

  ImagePathToTag = {
    %(image_path("xml")) => %(/calloboration/hieraki/images/xml.png),
  }
161 162 163 164 165
  
  ImageLinkToTag = {
    %(image_tag("xml")) => %(<img alt="Xml" src="/calloboration/hieraki/images/xml.png" />),
    %(image_tag("rss", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/calloboration/hieraki/images/rss.png" />),
    %(image_tag("gold", :size => "45x70")) => %(<img alt="Gold" height="70" src="/calloboration/hieraki/images/gold.png" width="45" />),
166
    %(image_tag("http://www.example.com/images/icon.gif")) => %(<img alt="Icon" src="http://www.example.com/images/icon.gif" />),
167 168
  }

169
  def test_auto_discovery
170
    AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
171 172
  end

173
  def test_javascript_path
174
    JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
175 176
  end

177
  def test_javascript_include
178
    JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
179 180
  end

181
  def test_style_path
182
    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
183 184
  end

185
  def test_style_link
186
    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
187
  end
188

189
  def test_image_path
190
    ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
191
  end
192 193
  
  def test_image_tag
194
    ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
195 196
    # Assigning a default alt tag should not cause an exception to be raised
    assert_nothing_raised { image_tag('') }
197
  end
198
  
199 200 201 202 203 204 205 206 207 208
  def test_stylesheet_with_asset_host_already_encoded
    ActionController::Base.asset_host = "http://foo.example.com"
    result = stylesheet_link_tag("http://bar.example.com/stylesheets/style.css")
    assert_dom_equal(
      %(<link href="http://bar.example.com/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
      result)
  ensure
    ActionController::Base.asset_host = ""
  end
  
209
end