require 'abstract_unit' class AssetTagHelperTest < Test::Unit::TestCase include ActionView::Helpers::TagHelper include ActionView::Helpers::UrlHelper include ActionView::Helpers::AssetTagHelper def setup 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 @controller = Class.new do attr_accessor :request def url_for(*args) "http://www.example.com" end end.new @request = Class.new do def relative_url_root() "" end def protocol() 'http://' end def ssl?() false end def host_with_port() 'localhost' end end.new @controller.request = @request ActionView::Helpers::AssetTagHelper::reset_javascript_include_default ActionView::Base.computed_public_paths.clear end def teardown ActionController::Base.perform_caching = false ActionController::Base.asset_host = nil ENV.delete('RAILS_ASSET_ID') end AutoDiscoveryToTag = { %(auto_discovery_link_tag) => %(), %(auto_discovery_link_tag(:rss)) => %(), %(auto_discovery_link_tag(:atom)) => %(), %(auto_discovery_link_tag(:xml)) => %(), %(auto_discovery_link_tag(:rss, :action => "feed")) => %(), %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(), %(auto_discovery_link_tag(:rss, {:action => "feed"}, {:title => "My RSS"})) => %(), %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(), %(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(), %(auto_discovery_link_tag(nil, {}, {:title => "No stream.. really", :type => "text/html"})) => %(), %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS", :type => "text/html"})) => %(), %(auto_discovery_link_tag(:atom, {}, {:rel => "Not so alternate"})) => %(), } JavascriptPathToTag = { %(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js), %(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js), %(javascript_path("/super/xmlhr.js")) => %(/super/xmlhr.js) } PathToJavascriptToTag = { %(path_to_javascript("xmlhr")) => %(/javascripts/xmlhr.js), %(path_to_javascript("super/xmlhr")) => %(/javascripts/super/xmlhr.js), %(path_to_javascript("/super/xmlhr.js")) => %(/super/xmlhr.js) } JavascriptIncludeToTag = { %(javascript_include_tag("xmlhr")) => %(), %(javascript_include_tag("xmlhr.js")) => %(), %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(), %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(\n), %(javascript_include_tag(:defaults)) => %(\n\n\n\n), %(javascript_include_tag(:all)) => %(\n\n), %(javascript_include_tag(:defaults, "test")) => %(\n\n\n\n\n), %(javascript_include_tag("test", :defaults)) => %(\n\n\n\n\n) } StylePathToTag = { %(stylesheet_path("style")) => %(/stylesheets/style.css), %(stylesheet_path("style.css")) => %(/stylesheets/style.css), %(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css), %(stylesheet_path('/dir/file.rcss')) => %(/dir/file.rcss) } PathToStyleToTag = { %(path_to_stylesheet("style")) => %(/stylesheets/style.css), %(path_to_stylesheet("style.css")) => %(/stylesheets/style.css), %(path_to_stylesheet('dir/file')) => %(/stylesheets/dir/file.css), %(path_to_stylesheet('/dir/file.rcss')) => %(/dir/file.rcss) } StyleLinkToTag = { %(stylesheet_link_tag("style")) => %(), %(stylesheet_link_tag("style.css")) => %(), %(stylesheet_link_tag("/dir/file")) => %(), %(stylesheet_link_tag("dir/file")) => %(), %(stylesheet_link_tag("style", :media => "all")) => %(), %(stylesheet_link_tag(:all)) => %(\n), %(stylesheet_link_tag(:all, :media => "all")) => %(\n), %(stylesheet_link_tag("random.styles", "/css/stylish")) => %(\n), %(stylesheet_link_tag("http://www.example.com/styles/style")) => %() } ImagePathToTag = { %(image_path("xml")) => %(/images/xml), %(image_path("xml.png")) => %(/images/xml.png), %(image_path("dir/xml.png")) => %(/images/dir/xml.png), %(image_path("/dir/xml.png")) => %(/dir/xml.png) } PathToImageToTag = { %(path_to_image("xml")) => %(/images/xml), %(path_to_image("xml.png")) => %(/images/xml.png), %(path_to_image("dir/xml.png")) => %(/images/dir/xml.png), %(path_to_image("/dir/xml.png")) => %(/dir/xml.png) } ImageLinkToTag = { %(image_tag("xml.png")) => %(Xml), %(image_tag("..jpg")) => %(), %(image_tag("rss.gif", :alt => "rss syndication")) => %(rss syndication), %(image_tag("gold.png", :size => "45x70")) => %(Gold), %(image_tag("gold.png", "size" => "45x70")) => %(Gold), %(image_tag("error.png", "size" => "45")) => %(Error), %(image_tag("error.png", "size" => "45 x 70")) => %(Error), %(image_tag("error.png", "size" => "x")) => %(Error), %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails), %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails), %(image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) => %(Mouse), %(image_tag("mouse.png", :mouseover => image_path("mouse_over.png"))) => %(Mouse) } def test_auto_discovery_link_tag AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_javascript_path JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_path_to_javascript_alias_for_javascript_path PathToJavascriptToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_javascript_include_tag ENV["RAILS_ASSET_ID"] = "" JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } ActionView::Base.computed_public_paths.clear ENV["RAILS_ASSET_ID"] = "1" assert_dom_equal(%(\n\n\n\n), javascript_include_tag(:defaults)) end def test_register_javascript_include_default ENV["RAILS_ASSET_ID"] = "" ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider' assert_dom_equal %(\n\n\n\n\n), javascript_include_tag(:defaults) ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'lib1', '/elsewhere/blub/lib2' assert_dom_equal %(\n\n\n\n\n\n\n), javascript_include_tag(:defaults) end def test_custom_javascript_expansions ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"] assert_dom_equal %(\n\n\n\n), javascript_include_tag('first', :monkey, 'last') end def test_custom_javascript_expansions_and_defaults_puts_application_js_at_the_end ENV["RAILS_ASSET_ID"] = "" ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"] assert_dom_equal %(\n\n\n\n\n\n\n\n\n), javascript_include_tag('first', :defaults, :monkey, 'last') end def test_custom_javascript_expansions_with_undefined_symbol ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => nil assert_raise(ArgumentError) { javascript_include_tag('first', :monkey, 'last') } end def test_stylesheet_path StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_path_to_stylesheet_alias_for_stylesheet_path PathToStyleToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_stylesheet_link_tag ENV["RAILS_ASSET_ID"] = "" StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_custom_stylesheet_expansions ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => ["head", "body", "tail"] assert_dom_equal %(\n\n\n\n), stylesheet_link_tag('first', :monkey, 'last') end def test_custom_stylesheet_expansions_with_undefined_symbol ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => nil assert_raise(ArgumentError) { stylesheet_link_tag('first', :monkey, 'last') } end def test_image_path ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_path_to_image_alias_for_image_path PathToImageToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_image_tag ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end 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 %(Rails), image_tag("rails.png") end def test_should_skip_asset_id_on_complete_url assert_equal %(Rails), image_tag("http://www.example.com/rails.png") end def test_should_use_preset_asset_id ENV["RAILS_ASSET_ID"] = "4500" assert_equal %(Rails), image_tag("rails.png") end def test_preset_empty_asset_id ENV["RAILS_ASSET_ID"] = "" assert_equal %(Rails), image_tag("rails.png") end def test_should_not_modify_source_string source = '/images/rails.png' copy = source.dup image_tag(source) assert_equal copy, source end def test_caching_javascript_include_tag_when_caching_on ENV["RAILS_ASSET_ID"] = "" ActionController::Base.asset_host = 'http://a0.example.com' ActionController::Base.perform_caching = true assert_dom_equal( %(), javascript_include_tag(:all, :cache => true) ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) assert_dom_equal( %(), javascript_include_tag(:all, :cache => "money") ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) ensure FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) end def test_caching_javascript_include_tag_when_caching_on_with_proc_asset_host ENV['RAILS_ASSET_ID'] = '' ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } ActionController::Base.perform_caching = true assert_equal '/javascripts/scripts.js'.length, 23 assert_dom_equal( %(), javascript_include_tag(:all, :cache => 'scripts') ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js')) ensure FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js')) end def test_caching_javascript_include_tag_when_caching_on_with_2_argument_proc_asset_host ENV['RAILS_ASSET_ID'] = '' ActionController::Base.asset_host = Proc.new { |source, request| if request.ssl? "#{request.protocol}#{request.host_with_port}" else "#{request.protocol}assets#{source.length}.example.com" end } ActionController::Base.perform_caching = true assert_equal '/javascripts/vanilla.js'.length, 23 assert_dom_equal( %(), javascript_include_tag(:all, :cache => 'vanilla') ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js')) class << @controller.request def protocol() 'https://' end def ssl?() true end end assert_equal '/javascripts/secure.js'.length, 22 assert_dom_equal( %(), javascript_include_tag(:all, :cache => 'secure') ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js')) ensure FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js')) FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js')) end def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory ENV["RAILS_ASSET_ID"] = "" ActionController::Base.asset_host = 'http://a%d.example.com' ActionController::Base.perform_caching = true assert_dom_equal( %(), javascript_include_tag(:all, :cache => "cache/money") ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js')) ensure FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js')) end def test_caching_javascript_include_tag_when_caching_off ENV["RAILS_ASSET_ID"] = "" ActionController::Base.perform_caching = false assert_dom_equal( %(\n\n), javascript_include_tag(:all, :cache => true) ) assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js')) assert_dom_equal( %(\n\n), javascript_include_tag(:all, :cache => "money") ) assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js')) end def test_caching_stylesheet_link_tag_when_caching_on ENV["RAILS_ASSET_ID"] = "" ActionController::Base.asset_host = 'http://a0.example.com' ActionController::Base.perform_caching = true assert_dom_equal( %(), stylesheet_link_tag(:all, :cache => true) ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) assert_dom_equal( %(), stylesheet_link_tag(:all, :cache => "money") ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) ensure FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) end def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host ENV["RAILS_ASSET_ID"] = "" ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" } ActionController::Base.perform_caching = true assert_equal '/stylesheets/styles.css'.length, 23 assert_dom_equal( %(), stylesheet_link_tag(:all, :cache => 'styles') ) assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css')) ensure FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css')) end def test_caching_stylesheet_include_tag_when_caching_off ENV["RAILS_ASSET_ID"] = "" ActionController::Base.perform_caching = false assert_dom_equal( %(\n), stylesheet_link_tag(:all, :cache => true) ) assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css')) assert_dom_equal( %(\n), stylesheet_link_tag(:all, :cache => "money") ) assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css')) 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 attr_accessor :request def url_for(options) "http://www.example.com/collaboration/hieraki" end end.new @request = Class.new do def relative_url_root "/collaboration/hieraki" end def protocol 'gopher://' end end.new @controller.request = @request ActionView::Helpers::AssetTagHelper::reset_javascript_include_default end def test_should_compute_proper_path assert_dom_equal(%(), 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")) assert_dom_equal(%(Mouse), image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) assert_dom_equal(%(Mouse2), image_tag("mouse2.png", :mouseover => image_path("mouse_over2.png"))) end 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")) end def test_should_compute_proper_path_with_asset_host ActionController::Base.asset_host = "http://assets.example.com" assert_dom_equal(%(), 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")) assert_dom_equal(%(Mouse), image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) assert_dom_equal(%(Mouse2), image_tag("mouse2.png", :mouseover => image_path("mouse_over2.png"))) ensure ActionController::Base.asset_host = "" end def test_should_ignore_asset_host_on_complete_url ActionController::Base.asset_host = "http://assets.example.com" assert_dom_equal(%(), stylesheet_link_tag("http://bar.example.com/stylesheets/style.css")) ensure ActionController::Base.asset_host = "" end 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 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 def test_asset_host_without_protocol_should_use_request_protocol_even_if_path_present ActionController::Base.asset_host = 'a.example.com/files/go/here' assert_equal 'gopher://a.example.com/files/go/here/collaboration/hieraki/images/xml.png', image_path('xml.png') ensure ActionController::Base.asset_host = nil end end