require 'zlib' require 'abstract_unit' require 'active_support/ordered_options' class FakeController attr_accessor :request def config @config ||= ActiveSupport::InheritableOptions.new(ActionController::Base.config) end end class AssetTagHelperTest < ActionView::TestCase tests ActionView::Helpers::AssetTagHelper def setup super 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 = BasicController.new @request = Class.new do attr_accessor :script_name def protocol() 'http://' end def ssl?() false end def host_with_port() 'localhost' end end.new @controller.request = @request end def url_for(*args) "http://www.example.com" end def teardown 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(:rss, :action => "feed")) => %(), %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(), %(auto_discovery_link_tag(:rss, "//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) } JavascriptUrlToTag = { %(javascript_url("xmlhr")) => %(http://www.example.com/javascripts/xmlhr.js), %(javascript_url("super/xmlhr")) => %(http://www.example.com/javascripts/super/xmlhr.js), %(javascript_url("/super/xmlhr.js")) => %(http://www.example.com/super/xmlhr.js) } UrlToJavascriptToTag = { %(url_to_javascript("xmlhr")) => %(http://www.example.com/javascripts/xmlhr.js), %(url_to_javascript("super/xmlhr")) => %(http://www.example.com/javascripts/super/xmlhr.js), %(url_to_javascript("/super/xmlhr.js")) => %(http://www.example.com/super/xmlhr.js) } JavascriptIncludeToTag = { %(javascript_include_tag("bank")) => %(), %(javascript_include_tag("bank.js")) => %(), %(javascript_include_tag("bank", :lang => "vbscript")) => %(), %(javascript_include_tag("common.javascript", "/elsewhere/cools")) => %(\n), %(javascript_include_tag("http://example.com/all")) => %(), %(javascript_include_tag("http://example.com/all.js")) => %(), %(javascript_include_tag("//example.com/all.js")) => %(), } StylePathToTag = { %(stylesheet_path("bank")) => %(/stylesheets/bank.css), %(stylesheet_path("bank.css")) => %(/stylesheets/bank.css), %(stylesheet_path('subdir/subdir')) => %(/stylesheets/subdir/subdir.css), %(stylesheet_path('/subdir/subdir.css')) => %(/subdir/subdir.css) } 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) } StyleUrlToTag = { %(stylesheet_url("bank")) => %(http://www.example.com/stylesheets/bank.css), %(stylesheet_url("bank.css")) => %(http://www.example.com/stylesheets/bank.css), %(stylesheet_url('subdir/subdir')) => %(http://www.example.com/stylesheets/subdir/subdir.css), %(stylesheet_url('/subdir/subdir.css')) => %(http://www.example.com/subdir/subdir.css) } UrlToStyleToTag = { %(url_to_stylesheet("style")) => %(http://www.example.com/stylesheets/style.css), %(url_to_stylesheet("style.css")) => %(http://www.example.com/stylesheets/style.css), %(url_to_stylesheet('dir/file')) => %(http://www.example.com/stylesheets/dir/file.css), %(url_to_stylesheet('/dir/file.rcss')) => %(http://www.example.com/dir/file.rcss) } StyleLinkToTag = { %(stylesheet_link_tag("bank")) => %(), %(stylesheet_link_tag("bank.css")) => %(), %(stylesheet_link_tag("/elsewhere/file")) => %(), %(stylesheet_link_tag("subdir/subdir")) => %(), %(stylesheet_link_tag("bank", :media => "all")) => %(), %(stylesheet_link_tag("random.styles", "/elsewhere/file")) => %(\n), %(stylesheet_link_tag("http://www.example.com/styles/style")) => %(), %(stylesheet_link_tag("http://www.example.com/styles/style.css")) => %(), %(stylesheet_link_tag("//www.example.com/styles/style.css")) => %(), } 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) } ImageUrlToTag = { %(image_url("xml")) => %(http://www.example.com/images/xml), %(image_url("xml.png")) => %(http://www.example.com/images/xml.png), %(image_url("dir/xml.png")) => %(http://www.example.com/images/dir/xml.png), %(image_url("/dir/xml.png")) => %(http://www.example.com/dir/xml.png) } UrlToImageToTag = { %(url_to_image("xml")) => %(http://www.example.com/images/xml), %(url_to_image("xml.png")) => %(http://www.example.com/images/xml.png), %(url_to_image("dir/xml.png")) => %(http://www.example.com/images/dir/xml.png), %(url_to_image("/dir/xml.png")) => %(http://www.example.com/dir/xml.png) } ImageLinkToTag = { %(image_tag("xml.png")) => %(Xml), %(image_tag("rss.gif", :alt => "rss syndication")) => %(rss syndication), %(image_tag("gold.png", :size => "20")) => %(Gold), %(image_tag("gold.png", :size => "45x70")) => %(Gold), %(image_tag("gold.png", "size" => "45x70")) => %(Gold), %(image_tag("error.png", "size" => "45 x 70")) => %(Error), %(image_tag("error.png", "size" => "x")) => %(Error), %(image_tag("google.com.png")) => %(Google.com), %(image_tag("slash..png")) => %(Slash.), %(image_tag(".pdf.png")) => %(.pdf), %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(Rails), %(image_tag("//www.rubyonrails.com/images/rails.png")) => %(Rails), %(image_tag("mouse.png", :alt => nil)) => %(), %(image_tag("data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==", :alt => nil)) => %(), %(image_tag("")) => %() } FaviconLinkToTag = { %(favicon_link_tag) => %(), %(favicon_link_tag 'favicon.ico') => %(), %(favicon_link_tag 'favicon.ico', :rel => 'foo') => %(), %(favicon_link_tag 'favicon.ico', :rel => 'foo', :type => 'bar') => %(), %(favicon_link_tag 'mb-icon.png', :rel => 'apple-touch-icon', :type => 'image/png') => %() } VideoPathToTag = { %(video_path("xml")) => %(/videos/xml), %(video_path("xml.ogg")) => %(/videos/xml.ogg), %(video_path("dir/xml.ogg")) => %(/videos/dir/xml.ogg), %(video_path("/dir/xml.ogg")) => %(/dir/xml.ogg) } PathToVideoToTag = { %(path_to_video("xml")) => %(/videos/xml), %(path_to_video("xml.ogg")) => %(/videos/xml.ogg), %(path_to_video("dir/xml.ogg")) => %(/videos/dir/xml.ogg), %(path_to_video("/dir/xml.ogg")) => %(/dir/xml.ogg) } VideoUrlToTag = { %(video_url("xml")) => %(http://www.example.com/videos/xml), %(video_url("xml.ogg")) => %(http://www.example.com/videos/xml.ogg), %(video_url("dir/xml.ogg")) => %(http://www.example.com/videos/dir/xml.ogg), %(video_url("/dir/xml.ogg")) => %(http://www.example.com/dir/xml.ogg) } UrlToVideoToTag = { %(url_to_video("xml")) => %(http://www.example.com/videos/xml), %(url_to_video("xml.ogg")) => %(http://www.example.com/videos/xml.ogg), %(url_to_video("dir/xml.ogg")) => %(http://www.example.com/videos/dir/xml.ogg), %(url_to_video("/dir/xml.ogg")) => %(http://www.example.com/dir/xml.ogg) } VideoLinkToTag = { %(video_tag("xml.ogg")) => %(), %(video_tag("rss.m4v", :autoplay => true, :controls => true)) => %(), %(video_tag("rss.m4v", :autobuffer => true)) => %(), %(video_tag("gold.m4v", :size => "160x120")) => %(), %(video_tag("gold.m4v", "size" => "320x240")) => %(), %(video_tag("trailer.ogg", :poster => "screenshot.png")) => %(), %(video_tag("error.avi", "size" => "100")) => %(), %(video_tag("error.avi", "size" => "100 x 100")) => %(), %(video_tag("error.avi", "size" => "x")) => %(), %(video_tag("http://media.rubyonrails.org/video/rails_blog_2.mov")) => %(), %(video_tag("//media.rubyonrails.org/video/rails_blog_2.mov")) => %(), %(video_tag("multiple.ogg", "multiple.avi")) => %(), %(video_tag(["multiple.ogg", "multiple.avi"])) => %(), %(video_tag(["multiple.ogg", "multiple.avi"], :size => "160x120", :controls => true)) => %() } AudioPathToTag = { %(audio_path("xml")) => %(/audios/xml), %(audio_path("xml.wav")) => %(/audios/xml.wav), %(audio_path("dir/xml.wav")) => %(/audios/dir/xml.wav), %(audio_path("/dir/xml.wav")) => %(/dir/xml.wav) } PathToAudioToTag = { %(path_to_audio("xml")) => %(/audios/xml), %(path_to_audio("xml.wav")) => %(/audios/xml.wav), %(path_to_audio("dir/xml.wav")) => %(/audios/dir/xml.wav), %(path_to_audio("/dir/xml.wav")) => %(/dir/xml.wav) } AudioUrlToTag = { %(audio_url("xml")) => %(http://www.example.com/audios/xml), %(audio_url("xml.wav")) => %(http://www.example.com/audios/xml.wav), %(audio_url("dir/xml.wav")) => %(http://www.example.com/audios/dir/xml.wav), %(audio_url("/dir/xml.wav")) => %(http://www.example.com/dir/xml.wav) } UrlToAudioToTag = { %(url_to_audio("xml")) => %(http://www.example.com/audios/xml), %(url_to_audio("xml.wav")) => %(http://www.example.com/audios/xml.wav), %(url_to_audio("dir/xml.wav")) => %(http://www.example.com/audios/dir/xml.wav), %(url_to_audio("/dir/xml.wav")) => %(http://www.example.com/dir/xml.wav) } AudioLinkToTag = { %(audio_tag("xml.wav")) => %(), %(audio_tag("rss.wav", :autoplay => true, :controls => true)) => %(), %(audio_tag("http://media.rubyonrails.org/audio/rails_blog_2.mov")) => %(), %(audio_tag("//media.rubyonrails.org/audio/rails_blog_2.mov")) => %(), %(audio_tag("audio.mp3", "audio.ogg")) => %(), %(audio_tag(["audio.mp3", "audio.ogg"])) => %(), %(audio_tag(["audio.mp3", "audio.ogg"], :autobuffer => true, :controls => true)) => %() } def test_autodiscovery_link_tag_deprecated_types result = nil assert_deprecated do result = auto_discovery_link_tag(:xml) end expected = %() assert_equal expected, result end 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_url JavascriptUrlToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_url_to_javascript_alias_for_javascript_url UrlToJavascriptToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_javascript_include_tag_with_blank_asset_id ENV["RAILS_ASSET_ID"] = "" JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_javascript_include_tag_with_missing_source assert_nothing_raised { javascript_include_tag('missing_security_guard') } assert_nothing_raised { javascript_include_tag('http://example.com/css/missing_security_guard') } end def test_javascript_include_tag_is_html_safe assert javascript_include_tag("prototype").html_safe? end def test_all_javascript_expansion_not_include_application_js_if_not_exists FileUtils.mv(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.js'), File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.bak')) assert_no_match(/application\.js/, javascript_include_tag(:all)) ensure FileUtils.mv(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.bak'), File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'application.js')) end def test_stylesheet_path ENV["RAILS_ASSET_ID"] = "" 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_url ENV["RAILS_ASSET_ID"] = "" StyleUrlToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) } end def test_url_to_stylesheet_alias_for_stylesheet_url UrlToStyleToTag.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_stylesheet_link_tag_with_missing_source assert_nothing_raised { stylesheet_link_tag('missing_security_guard') } assert_nothing_raised { stylesheet_link_tag('http://example.com/css/missing_security_guard') } end def test_stylesheet_link_tag_is_html_safe ENV["RAILS_ASSET_ID"] = "" assert stylesheet_link_tag('dir/file').html_safe? assert stylesheet_link_tag('dir/other/file', 'dir/file2').html_safe? end def test_stylesheet_link_tag_escapes_options assert_dom_equal %(), stylesheet_link_tag('/file', :media => '