asset_tag_helper_test.rb 19.1 KB
Newer Older
1
require "#{File.dirname(__FILE__)}/../abstract_unit"
2 3 4 5 6 7 8

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

  def setup
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
    silence_warnings do
      ActionView::Helpers::AssetTagHelper.send(
        :const_set,
        :JAVASCRIPTS_DIR,
        File.dirname(__FILE__) + "/../fixtures/public/javascripts"
      )

      ActionView::Helpers::AssetTagHelper.send(
        :const_set,
        :STYLESHEETS_DIR,
        File.dirname(__FILE__) + "/../fixtures/public/stylesheets"
      )

      ActionView::Helpers::AssetTagHelper.send(
        :const_set,
        :ASSETS_DIR,
        File.dirname(__FILE__) + "/../fixtures/public"
      )
    end
28

29
    @controller = Class.new do
30
      attr_accessor :request
31
      def url_for(*args) "http://www.example.com" end
32
    end.new
33 34 35

    @request = Class.new do
      def relative_url_root() "" end
36
    end.new
37 38

    @controller.request = @request
39

40
    ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
41 42
  end

43
  def teardown
44
    ActionController::Base.perform_caching = false
45
    ActionController::Base.asset_host = nil
46 47 48
    ENV["RAILS_ASSET_ID"] = nil
  end

49
  AutoDiscoveryToTag = {
50
    %(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
51
    %(auto_discovery_link_tag(:rss)) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
52
    %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
53
    %(auto_discovery_link_tag(:xml)) => %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />),
54
    %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
55
    %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
56 57 58 59 60 61
    %(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" />),
62 63
  }

64 65
  JavascriptPathToTag = {
    %(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js),
66 67
    %(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js),
    %(javascript_path("/super/xmlhr.js")) => %(/super/xmlhr.js)
68 69
  }

70
  JavascriptIncludeToTag = {
D
David Heinemeier Hansson 已提交
71
    %(javascript_include_tag("xmlhr")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
72
    %(javascript_include_tag("xmlhr.js")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
73
    %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(<script lang="vbscript" src="/javascripts/xmlhr.js" type="text/javascript"></script>),
D
David Heinemeier Hansson 已提交
74
    %(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>),
75 76 77 78
    %(javascript_include_tag(:defaults)) => %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>),
    %(javascript_include_tag(:all)) => %(<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>),
    %(javascript_include_tag(:defaults, "test")) => %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/test.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>),
    %(javascript_include_tag("test", :defaults)) => %(<script src="/javascripts/test.js" type="text/javascript"></script>\n<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>)
79 80
  }

81 82
  StylePathToTag = {
    %(stylesheet_path("style")) => %(/stylesheets/style.css),
83
    %(stylesheet_path("style.css")) => %(/stylesheets/style.css),
84
    %(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css),
85
    %(stylesheet_path('/dir/file.rcss')) => %(/dir/file.rcss)
86 87
  }

88 89
  StyleLinkToTag = {
    %(stylesheet_link_tag("style")) => %(<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
90
    %(stylesheet_link_tag("style.css")) => %(<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
91 92
    %(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" />),
93
    %(stylesheet_link_tag("style", :media => "all")) => %(<link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" />),
94 95
    %(stylesheet_link_tag(:all)) => %(<link href="/stylesheets/bank.css" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="Stylesheet" type="text/css" />),
    %(stylesheet_link_tag(:all, :media => "all")) => %(<link href="/stylesheets/bank.css" media="all" rel="Stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="all" rel="Stylesheet" type="text/css" />),
96 97
    %(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" />),
    %(stylesheet_link_tag("http://www.example.com/styles/style")) => %(<link href="http://www.example.com/styles/style.css" media="screen" rel="Stylesheet" type="text/css" />)
98 99
  }

100
  ImagePathToTag = {
101 102 103
    %(image_path("xml.png")) => %(/images/xml.png),
    %(image_path("dir/xml.png")) => %(/images/dir/xml.png),
    %(image_path("/dir/xml.png")) => %(/dir/xml.png)    
104 105
  }

106
  ImageLinkToTag = {
107 108 109 110 111 112 113 114
    %(image_tag("xml.png")) => %(<img alt="Xml" src="/images/xml.png" />),
    %(image_tag("rss.gif", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.gif" />),
    %(image_tag("gold.png", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
    %(image_tag("gold.png", "size" => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
    %(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
    %(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
    %(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
    %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />)
115 116
  }

117 118 119 120 121 122
  DeprecatedImagePathToTag = {
    %(image_path("xml")) => %(/images/xml.png)
  }


  def test_auto_discovery_link_tag
123
    AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
124 125
  end

126
  def test_javascript_path
127
    JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
128 129
  end

130
  def test_javascript_include_tag
131
    ENV["RAILS_ASSET_ID"] = ""
132
    JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
133 134

    ENV["RAILS_ASSET_ID"] = "1"
135
    assert_dom_equal(%(<script src="/javascripts/prototype.js?1" type="text/javascript"></script>\n<script src="/javascripts/effects.js?1" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js?1" type="text/javascript"></script>\n<script src="/javascripts/controls.js?1" type="text/javascript"></script>\n<script src="/javascripts/application.js?1" type="text/javascript"></script>), javascript_include_tag(:defaults))
136
  end
137

138
  def test_register_javascript_include_default
139
    ENV["RAILS_ASSET_ID"] = ""
140
    ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider'
141
    assert_dom_equal  %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/slider.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>), javascript_include_tag(:defaults)
142
    ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'lib1', '/elsewhere/blub/lib2'
143
    assert_dom_equal  %(<script src="/javascripts/prototype.js" type="text/javascript"></script>\n<script src="/javascripts/effects.js" type="text/javascript"></script>\n<script src="/javascripts/dragdrop.js" type="text/javascript"></script>\n<script src="/javascripts/controls.js" type="text/javascript"></script>\n<script src="/javascripts/slider.js" type="text/javascript"></script>\n<script src="/javascripts/lib1.js" type="text/javascript"></script>\n<script src="/elsewhere/blub/lib2.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>), javascript_include_tag(:defaults)
144
  end
145
  
146
  def test_stylesheet_path
147
    StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
148 149
  end

150
  def test_stylesheet_link_tag
151
    ENV["RAILS_ASSET_ID"] = ""
152
    StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
153 154
  end

155
  def test_image_path
156
    ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
157
  end
158
  
159
  def test_image_tag
160 161 162 163 164 165
    ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  end
  
  def test_should_deprecate_image_filename_with_no_extension
    DeprecatedImagePathToTag.each do |method, tag| 
      assert_deprecated("image_path") { assert_dom_equal(tag, eval(method)) }
166
    end
167
  end
168

169 170 171 172
  def test_timebased_asset_id
    expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
    assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")
  end
D
David Heinemeier Hansson 已提交
173

174
  def test_should_skip_asset_id_on_complete_url
D
David Heinemeier Hansson 已提交
175 176
    assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png")
  end
177
  
178
  def test_should_use_preset_asset_id
179 180 181
    ENV["RAILS_ASSET_ID"] = "4500"
    assert_equal %(<img alt="Rails" src="/images/rails.png?4500" />), image_tag("rails.png")
  end
182

183 184 185 186 187
  def test_preset_empty_asset_id
    ENV["RAILS_ASSET_ID"] = ""
    assert_equal %(<img alt="Rails" src="/images/rails.png" />), image_tag("rails.png")
  end

188 189 190 191 192
  def test_should_not_modify_source_string
    source = '/images/rails.png'
    copy = source.dup
    image_tag(source)
    assert_equal copy, source
193
  end
194 195 196 197


  def test_caching_javascript_include_tag_when_caching_on
    ENV["RAILS_ASSET_ID"] = ""
198
    ActionController::Base.asset_host = 'http://a%d.example.com'
199 200 201
    ActionController::Base.perform_caching = true
    
    assert_dom_equal(
202
      %(<script src="http://a0.example.com/javascripts/all.js" type="text/javascript"></script>),
203 204 205 206 207 208
      javascript_include_tag(:all, :cache => true)
    )

    assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
    
    assert_dom_equal(
209
      %(<script src="http://a2.example.com/javascripts/money.js" type="text/javascript"></script>),
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
      javascript_include_tag(:all, :cache => "money")
    )

    assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
  ensure
    File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
    File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
  end
  
  def test_caching_javascript_include_tag_when_caching_off
    ENV["RAILS_ASSET_ID"] = ""
    ActionController::Base.perform_caching = false
    
    assert_dom_equal(
      %(<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>),
      javascript_include_tag(:all, :cache => true)
    )

    assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
    
    assert_dom_equal(
      %(<script src="/javascripts/application.js" type="text/javascript"></script>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>),
      javascript_include_tag(:all, :cache => "money")
    )

    assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
  end

  def test_caching_stylesheet_link_tag_when_caching_on
    ENV["RAILS_ASSET_ID"] = ""
240
    ActionController::Base.asset_host = 'http://a%d.example.com'
241 242 243
    ActionController::Base.perform_caching = true
    
    assert_dom_equal(
244
      %(<link href="http://a3.example.com/stylesheets/all.css" media="screen" rel="Stylesheet" type="text/css" />),
245 246 247 248 249 250
      stylesheet_link_tag(:all, :cache => true)
    )

    assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))

    assert_dom_equal(
251
      %(<link href="http://a3.example.com/stylesheets/money.css" media="screen" rel="Stylesheet" type="text/css" />),
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
      stylesheet_link_tag(:all, :cache => "money")
    )

    assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  ensure
    File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
    File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  end
  
  def test_caching_stylesheet_include_tag_when_caching_off
    ENV["RAILS_ASSET_ID"] = ""
    ActionController::Base.perform_caching = false
    
    assert_dom_equal(
      %(<link href="/stylesheets/bank.css" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="Stylesheet" type="text/css" />),
      stylesheet_link_tag(:all, :cache => true)
    )

    assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
    
    assert_dom_equal(
      %(<link href="/stylesheets/bank.css" media="screen" rel="Stylesheet" type="text/css" />\n<link href="/stylesheets/robber.css" media="screen" rel="Stylesheet" type="text/css" />),
      stylesheet_link_tag(:all, :cache => "money")
    )

    assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  end
279 280 281 282 283 284 285 286 287
end

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

  def setup
    @controller = Class.new do
288 289
      attr_accessor :request

290
      def url_for(options, *parameters_for_method_reference)
291
        "http://www.example.com/collaboration/hieraki"
292 293 294 295 296
      end
    end.new
    
    @request = Class.new do 
      def relative_url_root
297
        "/collaboration/hieraki"
298
      end
299 300 301 302

      def protocol
        'gopher://'
      end
303 304
    end.new
    
305
    @controller.request = @request
306 307
    
    ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
308 309
  end

310 311 312 313 314
  def test_should_compute_proper_path
    assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
    assert_dom_equal(%(/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
    assert_dom_equal(%(/collaboration/hieraki/stylesheets/style.css), stylesheet_path("style"))
    assert_dom_equal(%(/collaboration/hieraki/images/xml.png), image_path("xml.png"))
315
  end
316
  
317 318
  def test_should_ignore_relative_root_path_on_complete_url
    assert_dom_equal(%(http://www.example.com/images/xml.png), image_path("http://www.example.com/images/xml.png"))
319
  end
320

321 322 323 324 325 326 327 328
  def test_should_compute_proper_path_with_asset_host
    ActionController::Base.asset_host = "http://assets.example.com"
    assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
    assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
    assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/stylesheets/style.css), stylesheet_path("style"))
    assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/images/xml.png), image_path("xml.png"))
  ensure
    ActionController::Base.asset_host = ""
329
  end
330

331 332 333
  def test_should_ignore_asset_host_on_complete_url
    ActionController::Base.asset_host = "http://assets.example.com"
    assert_dom_equal(%(<link href="http://bar.example.com/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />), stylesheet_link_tag("http://bar.example.com/stylesheets/style.css"))
334 335 336
  ensure
    ActionController::Base.asset_host = ""
  end
337 338 339 340 341 342 343

  def test_should_wildcard_asset_host_between_zero_and_four
    ActionController::Base.asset_host = 'http://a%d.example.com'
    assert_match %r(http://a[0123].example.com/collaboration/hieraki/images/xml.png), image_path('xml.png')
  ensure
    ActionController::Base.asset_host = nil
  end
344 345 346 347 348 349 350

  def test_asset_host_without_protocol_should_use_request_protocol
    ActionController::Base.asset_host = 'a.example.com'
    assert_equal 'gopher://a.example.com/collaboration/hieraki/images/xml.png', image_path('xml.png')
  ensure
    ActionController::Base.asset_host = nil
  end
351
end