Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

root/trunk/actionpack/test/template/asset_tag_helper_test.rb

Revision 9065, 30.2 kB (checked in by bitsweat, 8 months ago)

Re-added ActionView::Helpers::register_javascript/stylesheet_expansion to make it easier for plugin developers to inject multiple assets. Closes #10350.

Line 
1 require 'abstract_unit'
2
3 class AssetTagHelperTest < Test::Unit::TestCase
4   include ActionView::Helpers::TagHelper
5   include ActionView::Helpers::UrlHelper
6   include ActionView::Helpers::AssetTagHelper
7
8   def setup
9     silence_warnings do
10       ActionView::Helpers::AssetTagHelper.send(
11         :const_set,
12         :JAVASCRIPTS_DIR,
13         File.dirname(__FILE__) + "/../fixtures/public/javascripts"
14       )
15
16       ActionView::Helpers::AssetTagHelper.send(
17         :const_set,
18         :STYLESHEETS_DIR,
19         File.dirname(__FILE__) + "/../fixtures/public/stylesheets"
20       )
21
22       ActionView::Helpers::AssetTagHelper.send(
23         :const_set,
24         :ASSETS_DIR,
25         File.dirname(__FILE__) + "/../fixtures/public"
26       )
27     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     @request = Class.new do
35       def relative_url_root() "" end
36       def protocol() 'http://' end
37       def ssl?() false end
38       def host_with_port() 'localhost' end
39     end.new
40
41     @controller.request = @request
42
43     ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
44
45     ActionView::Base.computed_public_paths.clear
46   end
47
48   def teardown
49     ActionController::Base.perform_caching = false
50     ActionController::Base.asset_host = nil
51     ENV.delete('RAILS_ASSET_ID')
52   end
53
54   AutoDiscoveryToTag = {
55     %(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
56     %(auto_discovery_link_tag(:rss)) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
57     %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
58     %(auto_discovery_link_tag(:xml)) => %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />),
59     %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
60     %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
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" />),
62     %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
63     %(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="" type="text/html" />),
64     %(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" />),
65     %(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" />),
66     %(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" />),
67   }
68
69   JavascriptPathToTag = {
70     %(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js),
71     %(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js),
72     %(javascript_path("/super/xmlhr.js")) => %(/super/xmlhr.js)
73   }
74
75   PathToJavascriptToTag = {
76     %(path_to_javascript("xmlhr")) => %(/javascripts/xmlhr.js),
77     %(path_to_javascript("super/xmlhr")) => %(/javascripts/super/xmlhr.js),
78     %(path_to_javascript("/super/xmlhr.js")) => %(/super/xmlhr.js)
79   }
80
81   JavascriptIncludeToTag = {
82     %(javascript_include_tag("xmlhr")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
83     %(javascript_include_tag("xmlhr.js")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
84     %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(<script lang="vbscript" src="/javascripts/xmlhr.js" type="text/javascript"></script>),
85     %(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>),
86     %(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>),
87     %(javascript_include_tag(:all)) => %(<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>\n<script src="/javascripts/bank.js" type="text/javascript"></script>\n<script src="/javascripts/robber.js" type="text/javascript"></script>),
88     %(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>),
89     %(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>)
90   }
91
92   StylePathToTag = {
93     %(stylesheet_path("style")) => %(/stylesheets/style.css),
94     %(stylesheet_path("style.css")) => %(/stylesheets/style.css),
95     %(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css),
96     %(stylesheet_path('/dir/file.rcss')) => %(/dir/file.rcss)
97   }
98
99   PathToStyleToTag = {
100     %(path_to_stylesheet("style")) => %(/stylesheets/style.css),
101     %(path_to_stylesheet("style.css")) => %(/stylesheets/style.css),
102     %(path_to_stylesheet('dir/file')) => %(/stylesheets/dir/file.css),
103     %(path_to_stylesheet('/dir/file.rcss')) => %(/dir/file.rcss)
104   }
105
106   StyleLinkToTag = {
107     %(stylesheet_link_tag("style")) => %(<link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />),
108     %(stylesheet_link_tag("style.css")) => %(<link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />),
109     %(stylesheet_link_tag("/dir/file")) => %(<link href="/dir/file.css" media="screen" rel="stylesheet" type="text/css" />),
110     %(stylesheet_link_tag("dir/file")) => %(<link href="/stylesheets/dir/file.css" media="screen" rel="stylesheet" type="text/css" />),
111     %(stylesheet_link_tag("style", :media => "all")) => %(<link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />),
112     %(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" />),
113     %(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" />),
114     %(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" />),
115     %(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" />)
116   }
117
118   ImagePathToTag = {
119     %(image_path("xml"))          => %(/images/xml),
120     %(image_path("xml.png"))      => %(/images/xml.png),
121     %(image_path("dir/xml.png"))  => %(/images/dir/xml.png),
122     %(image_path("/dir/xml.png")) => %(/dir/xml.png)   
123   }
124
125   PathToImageToTag = {
126     %(path_to_image("xml"))          => %(/images/xml),
127     %(path_to_image("xml.png"))      => %(/images/xml.png),
128     %(path_to_image("dir/xml.png"))  => %(/images/dir/xml.png),
129     %(path_to_image("/dir/xml.png")) => %(/dir/xml.png)
130   }
131
132   ImageLinkToTag = {
133     %(image_tag("xml.png")) => %(<img alt="Xml" src="/images/xml.png" />),
134     %(image_tag("..jpg")) => %(<img alt="" src="/images/..jpg" />),
135     %(image_tag("rss.gif", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.gif" />),
136     %(image_tag("gold.png", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
137     %(image_tag("gold.png", "size" => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
138     %(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
139     %(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
140     %(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
141     %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />),
142     %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />),
143     %(image_tag("mouse.png", :mouseover => "/images/mouse_over.png")) => %(<img alt="Mouse" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" src="/images/mouse.png" />),
144     %(image_tag("mouse.png", :mouseover => image_path("mouse_over.png"))) => %(<img alt="Mouse" onmouseover="this.src='/images/mouse_over.png'" onmouseout="this.src='/images/mouse.png'" src="/images/mouse.png" />)
145   }
146
147
148   def test_auto_discovery_link_tag
149     AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
150   end
151
152   def test_javascript_path
153     JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
154   end
155
156   def test_path_to_javascript_alias_for_javascript_path
157     PathToJavascriptToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
158   end
159
160   def test_javascript_include_tag
161     ENV["RAILS_ASSET_ID"] = ""
162     JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
163
164     ActionView::Base.computed_public_paths.clear
165
166     ENV["RAILS_ASSET_ID"] = "1"
167     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))
168   end
169
170   def test_register_javascript_include_default
171     ENV["RAILS_ASSET_ID"] = ""
172     ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider'
173     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)
174     ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'lib1', '/elsewhere/blub/lib2'
175     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)
176   end
177  
178   def test_custom_javascript_expansions
179     ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"]
180     assert_dom_equal  %(<script src="/javascripts/first.js" type="text/javascript"></script>\n<script src="/javascripts/head.js" type="text/javascript"></script>\n<script src="/javascripts/body.js" type="text/javascript"></script>\n<script src="/javascripts/tail.js" type="text/javascript"></script>\n<script src="/javascripts/last.js" type="text/javascript"></script>), javascript_include_tag('first', :monkey, 'last')
181   end
182
183   def test_custom_javascript_expansions_and_defaults_puts_application_js_at_the_end
184     ENV["RAILS_ASSET_ID"] = ""
185     ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"]
186     assert_dom_equal  %(<script src="/javascripts/first.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/head.js" type="text/javascript"></script>\n<script src="/javascripts/body.js" type="text/javascript"></script>\n<script src="/javascripts/tail.js" type="text/javascript"></script>\n<script src="/javascripts/last.js" type="text/javascript"></script>\n<script src="/javascripts/application.js" type="text/javascript"></script>), javascript_include_tag('first', :defaults, :monkey, 'last')
187   end
188
189   def test_custom_javascript_expansions_with_undefined_symbol
190     ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => nil
191     assert_raise(ArgumentError) { javascript_include_tag('first', :monkey, 'last') }
192   end
193
194   def test_stylesheet_path
195     StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
196   end
197
198   def test_path_to_stylesheet_alias_for_stylesheet_path
199     PathToStyleToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
200   end
201
202   def test_stylesheet_link_tag
203     ENV["RAILS_ASSET_ID"] = ""
204     StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
205   end
206
207   def test_custom_stylesheet_expansions
208     ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => ["head", "body", "tail"]
209     assert_dom_equal  %(<link href="/stylesheets/first.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/head.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/body.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/tail.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/last.css" media="screen" rel="stylesheet" type="text/css" />), stylesheet_link_tag('first', :monkey, 'last')
210   end
211
212   def test_custom_stylesheet_expansions_with_undefined_symbol
213     ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => nil
214     assert_raise(ArgumentError) { stylesheet_link_tag('first', :monkey, 'last') }
215   end
216
217   def test_image_path
218     ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
219   end
220  
221   def test_path_to_image_alias_for_image_path
222     PathToImageToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
223   end
224
225   def test_image_tag
226     ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
227   end
228  
229   def test_timebased_asset_id
230     expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
231     assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")
232   end
233
234   def test_should_skip_asset_id_on_complete_url
235     assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png")
236   end
237  
238   def test_should_use_preset_asset_id
239     ENV["RAILS_ASSET_ID"] = "4500"
240     assert_equal %(<img alt="Rails" src="/images/rails.png?4500" />), image_tag("rails.png")
241   end
242
243   def test_preset_empty_asset_id
244     ENV["RAILS_ASSET_ID"] = ""
245     assert_equal %(<img alt="Rails" src="/images/rails.png" />), image_tag("rails.png")
246   end
247
248   def test_should_not_modify_source_string
249     source = '/images/rails.png'
250     copy = source.dup
251     image_tag(source)
252     assert_equal copy, source
253