Froze rails gems
[depot.git] / vendor / rails / actionpack / test / template / asset_tag_helper_test.rb
1 require 'abstract_unit'
2
3 class AssetTagHelperTest < ActionView::TestCase
4 tests ActionView::Helpers::AssetTagHelper
5
6 def setup
7 silence_warnings do
8 ActionView::Helpers::AssetTagHelper.send(
9 :const_set,
10 :JAVASCRIPTS_DIR,
11 File.dirname(__FILE__) + "/../fixtures/public/javascripts"
12 )
13
14 ActionView::Helpers::AssetTagHelper.send(
15 :const_set,
16 :STYLESHEETS_DIR,
17 File.dirname(__FILE__) + "/../fixtures/public/stylesheets"
18 )
19
20 ActionView::Helpers::AssetTagHelper.send(
21 :const_set,
22 :ASSETS_DIR,
23 File.dirname(__FILE__) + "/../fixtures/public"
24 )
25 end
26
27 @controller = Class.new do
28 attr_accessor :request
29 def url_for(*args) "http://www.example.com" end
30 end.new
31
32 @request = Class.new do
33 def protocol() 'http://' end
34 def ssl?() false end
35 def host_with_port() 'localhost' end
36 end.new
37
38 @controller.request = @request
39
40 ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
41 AssetTag::Cache.clear
42 AssetCollection::Cache.clear
43 end
44
45 def teardown
46 ActionController::Base.perform_caching = false
47 ActionController::Base.asset_host = nil
48 ENV.delete('RAILS_ASSET_ID')
49 end
50
51 AutoDiscoveryToTag = {
52 %(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
53 %(auto_discovery_link_tag(:rss)) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
54 %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
55 %(auto_discovery_link_tag(:xml)) => %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />),
56 %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
57 %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
58 %(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" />),
59 %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
60 %(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="" type="text/html" />),
61 %(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" />),
62 %(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" />),
63 %(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" />),
64 }
65
66 JavascriptPathToTag = {
67 %(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js),
68 %(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js),
69 %(javascript_path("/super/xmlhr.js")) => %(/super/xmlhr.js)
70 }
71
72 PathToJavascriptToTag = {
73 %(path_to_javascript("xmlhr")) => %(/javascripts/xmlhr.js),
74 %(path_to_javascript("super/xmlhr")) => %(/javascripts/super/xmlhr.js),
75 %(path_to_javascript("/super/xmlhr.js")) => %(/super/xmlhr.js)
76 }
77
78 JavascriptIncludeToTag = {
79 %(javascript_include_tag("xmlhr")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
80 %(javascript_include_tag("xmlhr.js")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
81 %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(<script lang="vbscript" src="/javascripts/xmlhr.js" type="text/javascript"></script>),
82 %(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>),
83 %(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>),
84 %(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>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
85 %(javascript_include_tag(:all, :recursive => true)) => %(<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>\n<script src="/javascripts/subdir/subdir.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
86 %(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>),
87 %(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>)
88 }
89
90 StylePathToTag = {
91 %(stylesheet_path("style")) => %(/stylesheets/style.css),
92 %(stylesheet_path("style.css")) => %(/stylesheets/style.css),
93 %(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css),
94 %(stylesheet_path('/dir/file.rcss')) => %(/dir/file.rcss)
95 }
96
97 PathToStyleToTag = {
98 %(path_to_stylesheet("style")) => %(/stylesheets/style.css),
99 %(path_to_stylesheet("style.css")) => %(/stylesheets/style.css),
100 %(path_to_stylesheet('dir/file')) => %(/stylesheets/dir/file.css),
101 %(path_to_stylesheet('/dir/file.rcss')) => %(/dir/file.rcss)
102 }
103
104 StyleLinkToTag = {
105 %(stylesheet_link_tag("style")) => %(<link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />),
106 %(stylesheet_link_tag("style.css")) => %(<link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />),
107 %(stylesheet_link_tag("/dir/file")) => %(<link href="/dir/file.css" media="screen" rel="stylesheet" type="text/css" />),
108 %(stylesheet_link_tag("dir/file")) => %(<link href="/stylesheets/dir/file.css" media="screen" rel="stylesheet" type="text/css" />),
109 %(stylesheet_link_tag("style", :media => "all")) => %(<link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />),
110 %(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" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
111 %(stylesheet_link_tag(:all, :recursive => true)) => %(<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" />\n<link href="/stylesheets/subdir/subdir.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
112 %(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" />\n<link href="/stylesheets/version.1.0.css" media="all" rel="stylesheet" type="text/css" />),
113 %(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" />),
114 %(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" />)
115 }
116
117 ImagePathToTag = {
118 %(image_path("xml")) => %(/images/xml),
119 %(image_path("xml.png")) => %(/images/xml.png),
120 %(image_path("dir/xml.png")) => %(/images/dir/xml.png),
121 %(image_path("/dir/xml.png")) => %(/dir/xml.png)
122 }
123
124 PathToImageToTag = {
125 %(path_to_image("xml")) => %(/images/xml),
126 %(path_to_image("xml.png")) => %(/images/xml.png),
127 %(path_to_image("dir/xml.png")) => %(/images/dir/xml.png),
128 %(path_to_image("/dir/xml.png")) => %(/dir/xml.png)
129 }
130
131 ImageLinkToTag = {
132 %(image_tag("xml.png")) => %(<img alt="Xml" src="/images/xml.png" />),
133 %(image_tag("..jpg")) => %(<img alt="" src="/images/..jpg" />),
134 %(image_tag("rss.gif", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.gif" />),
135 %(image_tag("gold.png", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
136 %(image_tag("gold.png", "size" => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
137 %(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
138 %(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
139 %(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
140 %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.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("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" />),
143 %(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" />)
144 }
145
146
147 def test_auto_discovery_link_tag
148 AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
149 end
150
151 def test_javascript_path
152 JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
153 end
154
155 def test_path_to_javascript_alias_for_javascript_path
156 PathToJavascriptToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
157 end
158
159 def test_javascript_include_tag_with_blank_asset_id
160 ENV["RAILS_ASSET_ID"] = ""
161 JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
162 end
163
164 def test_javascript_include_tag_with_given_asset_id
165 ENV["RAILS_ASSET_ID"] = "1"
166 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))
167 end
168
169 def test_register_javascript_include_default
170 ENV["RAILS_ASSET_ID"] = ""
171 ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider'
172 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)
173 end
174
175 def test_register_javascript_include_default_mixed_defaults
176 ENV["RAILS_ASSET_ID"] = ""
177 ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider'
178 ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'lib1', '/elsewhere/blub/lib2'
179 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)
180 end
181
182 def test_custom_javascript_expansions
183 ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"]
184 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')
185 end
186
187 def test_custom_javascript_expansions_and_defaults_puts_application_js_at_the_end
188 ENV["RAILS_ASSET_ID"] = ""
189 ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => ["head", "body", "tail"]
190 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')
191 end
192
193 def test_custom_javascript_expansions_with_undefined_symbol
194 ActionView::Helpers::AssetTagHelper::register_javascript_expansion :monkey => nil
195 assert_raise(ArgumentError) { javascript_include_tag('first', :monkey, 'last') }
196 end
197
198 def test_stylesheet_path
199 StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
200 end
201
202 def test_path_to_stylesheet_alias_for_stylesheet_path
203 PathToStyleToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
204 end
205
206 def test_stylesheet_link_tag
207 ENV["RAILS_ASSET_ID"] = ""
208 StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
209 end
210
211 def test_custom_stylesheet_expansions
212 ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => ["head", "body", "tail"]
213 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')
214 end
215
216 def test_custom_stylesheet_expansions_with_undefined_symbol
217 ActionView::Helpers::AssetTagHelper::register_stylesheet_expansion :monkey => nil
218 assert_raise(ArgumentError) { stylesheet_link_tag('first', :monkey, 'last') }
219 end
220
221 def test_image_path
222 ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
223 end
224
225 def test_path_to_image_alias_for_image_path
226 PathToImageToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
227 end
228
229 def test_image_tag
230 ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
231 end
232
233 uses_mocha 'test image tag with windows behaviour' do
234 def test_image_tag_windows_behaviour
235 old_asset_id, ENV["RAILS_ASSET_ID"] = ENV["RAILS_ASSET_ID"], "1"
236 # This simulates the behaviour of File#exist? on windows when testing a file ending in "."
237 # If the file "rails.png" exists, windows will return true when asked if "rails.png." exists (notice trailing ".")
238 # OS X, linux etc will return false in this case.
239 File.stubs(:exist?).with('template/../fixtures/public/images/rails.png.').returns(true)
240 assert_equal '<img alt="Rails" src="/images/rails.png?1" />', image_tag('rails.png')
241 ensure
242 if old_asset_id
243 ENV["RAILS_ASSET_ID"] = old_asset_id
244 else
245 ENV.delete("RAILS_ASSET_ID")
246 end
247 end
248 end
249
250 def test_timebased_asset_id
251 expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
252 assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")
253 end
254
255 def test_timebased_asset_id_with_relative_url_root
256 ActionController::Base.relative_url_root = "/collaboration/hieraki"
257 expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
258 assert_equal %(<img alt="Rails" src="#{ActionController::Base.relative_url_root}/images/rails.png?#{expected_time}" />), image_tag("rails.png")
259 ensure
260 ActionController::Base.relative_url_root = ""
261 end
262
263 def test_should_skip_asset_id_on_complete_url
264 assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png")
265 end
266
267 def test_should_use_preset_asset_id
268 ENV["RAILS_ASSET_ID"] = "4500"
269 assert_equal %(<img alt="Rails" src="/images/rails.png?4500" />), image_tag("rails.png")
270 end
271
272 def test_preset_empty_asset_id
273 ENV["RAILS_ASSET_ID"] = ""
274 assert_equal %(<img alt="Rails" src="/images/rails.png" />), image_tag("rails.png")
275 end
276
277 def test_should_not_modify_source_string
278 source = '/images/rails.png'
279 copy = source.dup
280 image_tag(source)
281 assert_equal copy, source
282 end
283
284 def test_caching_javascript_include_tag_when_caching_on
285 ENV["RAILS_ASSET_ID"] = ""
286 ActionController::Base.asset_host = 'http://a0.example.com'
287 ActionController::Base.perform_caching = true
288
289 assert_dom_equal(
290 %(<script src="http://a0.example.com/javascripts/all.js" type="text/javascript"></script>),
291 javascript_include_tag(:all, :cache => true)
292 )
293
294 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
295
296 assert_dom_equal(
297 %(<script src="http://a0.example.com/javascripts/money.js" type="text/javascript"></script>),
298 javascript_include_tag(:all, :cache => "money")
299 )
300
301 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
302
303 ensure
304 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
305 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
306 end
307
308 def test_caching_javascript_include_tag_when_caching_on_with_proc_asset_host
309 ENV['RAILS_ASSET_ID'] = ''
310 ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" }
311 ActionController::Base.perform_caching = true
312
313 assert_equal '/javascripts/scripts.js'.length, 23
314 assert_dom_equal(
315 %(<script src="http://a23.example.com/javascripts/scripts.js" type="text/javascript"></script>),
316 javascript_include_tag(:all, :cache => 'scripts')
317 )
318
319 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js'))
320
321 ensure
322 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'scripts.js'))
323 end
324
325 def test_caching_javascript_include_tag_when_caching_on_with_2_argument_proc_asset_host
326 ENV['RAILS_ASSET_ID'] = ''
327 ActionController::Base.asset_host = Proc.new { |source, request|
328 if request.ssl?
329 "#{request.protocol}#{request.host_with_port}"
330 else
331 "#{request.protocol}assets#{source.length}.example.com"
332 end
333 }
334 ActionController::Base.perform_caching = true
335
336 assert_equal '/javascripts/vanilla.js'.length, 23
337 assert_dom_equal(
338 %(<script src="http://assets23.example.com/javascripts/vanilla.js" type="text/javascript"></script>),
339 javascript_include_tag(:all, :cache => 'vanilla')
340 )
341
342 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js'))
343
344 class << @controller.request
345 def protocol() 'https://' end
346 def ssl?() true end
347 end
348
349 assert_equal '/javascripts/secure.js'.length, 22
350 assert_dom_equal(
351 %(<script src="https://localhost/javascripts/secure.js" type="text/javascript"></script>),
352 javascript_include_tag(:all, :cache => 'secure')
353 )
354
355 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js'))
356
357 ensure
358 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'vanilla.js'))
359 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'secure.js'))
360 end
361
362 def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory
363 ENV["RAILS_ASSET_ID"] = ""
364 ActionController::Base.asset_host = 'http://a%d.example.com'
365 ActionController::Base.perform_caching = true
366
367 hash = '/javascripts/cache/money.js'.hash % 4
368 assert_dom_equal(
369 %(<script src="http://a#{hash}.example.com/javascripts/cache/money.js" type="text/javascript"></script>),
370 javascript_include_tag(:all, :cache => "cache/money")
371 )
372
373 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js'))
374 ensure
375 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js'))
376 end
377
378 def test_caching_javascript_include_tag_with_all_and_recursive_puts_defaults_at_the_start_of_the_file
379 ENV["RAILS_ASSET_ID"] = ""
380 ActionController::Base.asset_host = 'http://a0.example.com'
381 ActionController::Base.perform_caching = true
382
383 assert_dom_equal(
384 %(<script src="http://a0.example.com/javascripts/combined.js" type="text/javascript"></script>),
385 javascript_include_tag(:all, :cache => "combined", :recursive => true)
386 )
387
388 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
389
390 assert_equal(
391 %(// prototype js\n\n// effects js\n\n// dragdrop js\n\n// controls js\n\n// application js\n\n// bank js\n\n// robber js\n\n// subdir js\n\n\n// version.1.0 js),
392 IO.read(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
393 )
394
395 ensure
396 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
397 end
398
399 def test_caching_javascript_include_tag_with_all_puts_defaults_at_the_start_of_the_file
400 ENV["RAILS_ASSET_ID"] = ""
401 ActionController::Base.asset_host = 'http://a0.example.com'
402 ActionController::Base.perform_caching = true
403
404 assert_dom_equal(
405 %(<script src="http://a0.example.com/javascripts/combined.js" type="text/javascript"></script>),
406 javascript_include_tag(:all, :cache => "combined")
407 )
408
409 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
410
411 assert_equal(
412 %(// prototype js\n\n// effects js\n\n// dragdrop js\n\n// controls js\n\n// application js\n\n// bank js\n\n// robber js\n\n// version.1.0 js),
413 IO.read(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
414 )
415
416 ensure
417 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'combined.js'))
418 end
419
420 def test_caching_javascript_include_tag_with_relative_url_root
421 ENV["RAILS_ASSET_ID"] = ""
422 ActionController::Base.relative_url_root = "/collaboration/hieraki"
423 ActionController::Base.perform_caching = true
424
425 assert_dom_equal(
426 %(<script src="/collaboration/hieraki/javascripts/all.js" type="text/javascript"></script>),
427 javascript_include_tag(:all, :cache => true)
428 )
429
430 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
431
432 assert_dom_equal(
433 %(<script src="/collaboration/hieraki/javascripts/money.js" type="text/javascript"></script>),
434 javascript_include_tag(:all, :cache => "money")
435 )
436
437 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
438
439 ensure
440 ActionController::Base.relative_url_root = nil
441 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
442 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
443 end
444
445 def test_caching_javascript_include_tag_when_caching_off
446 ENV["RAILS_ASSET_ID"] = ""
447 ActionController::Base.perform_caching = false
448
449 assert_dom_equal(
450 %(<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>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
451 javascript_include_tag(:all, :cache => true)
452 )
453
454 assert_dom_equal(
455 %(<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>\n<script src="/javascripts/subdir/subdir.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
456 javascript_include_tag(:all, :cache => true, :recursive => true)
457 )
458
459 assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
460
461 assert_dom_equal(
462 %(<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>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
463 javascript_include_tag(:all, :cache => "money")
464 )
465
466 assert_dom_equal(
467 %(<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>\n<script src="/javascripts/subdir/subdir.js" type="text/javascript"></script>\n<script src="/javascripts/version.1.0.js" type="text/javascript"></script>),
468 javascript_include_tag(:all, :cache => "money", :recursive => true)
469 )
470
471 assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
472 end
473
474 def test_caching_stylesheet_link_tag_when_caching_on
475 ENV["RAILS_ASSET_ID"] = ""
476 ActionController::Base.asset_host = 'http://a0.example.com'
477 ActionController::Base.perform_caching = true
478
479 assert_dom_equal(
480 %(<link href="http://a0.example.com/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />),
481 stylesheet_link_tag(:all, :cache => true)
482 )
483
484 expected = Dir["#{ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR}/*.css"].map { |p| File.mtime(p) }.max
485 assert_equal expected, File.mtime(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
486
487 assert_dom_equal(
488 %(<link href="http://a0.example.com/stylesheets/money.css" media="screen" rel="stylesheet" type="text/css" />),
489 stylesheet_link_tag(:all, :cache => "money")
490 )
491
492 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
493 ensure
494 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
495 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
496 end
497
498 def test_caching_stylesheet_link_tag_when_caching_on_with_proc_asset_host
499 ENV["RAILS_ASSET_ID"] = ""
500 ActionController::Base.asset_host = Proc.new { |source| "http://a#{source.length}.example.com" }
501 ActionController::Base.perform_caching = true
502
503 assert_equal '/stylesheets/styles.css'.length, 23
504 assert_dom_equal(
505 %(<link href="http://a23.example.com/stylesheets/styles.css" media="screen" rel="stylesheet" type="text/css" />),
506 stylesheet_link_tag(:all, :cache => 'styles')
507 )
508
509 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css'))
510
511 ensure
512 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'styles.css'))
513 end
514
515 def test_caching_stylesheet_link_tag_with_relative_url_root
516 ENV["RAILS_ASSET_ID"] = ""
517 ActionController::Base.relative_url_root = "/collaboration/hieraki"
518 ActionController::Base.perform_caching = true
519
520 assert_dom_equal(
521 %(<link href="/collaboration/hieraki/stylesheets/all.css" media="screen" rel="stylesheet" type="text/css" />),
522 stylesheet_link_tag(:all, :cache => true)
523 )
524
525 expected = Dir["#{ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR}/*.css"].map { |p| File.mtime(p) }.max
526 assert_equal expected, File.mtime(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
527
528 assert_dom_equal(
529 %(<link href="/collaboration/hieraki/stylesheets/money.css" media="screen" rel="stylesheet" type="text/css" />),
530 stylesheet_link_tag(:all, :cache => "money")
531 )
532
533 assert File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
534 ensure
535 ActionController::Base.relative_url_root = nil
536 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
537 FileUtils.rm_f(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
538 end
539
540 def test_caching_stylesheet_include_tag_when_caching_off
541 ENV["RAILS_ASSET_ID"] = ""
542 ActionController::Base.perform_caching = false
543
544 assert_dom_equal(
545 %(<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" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
546 stylesheet_link_tag(:all, :cache => true)
547 )
548
549 assert_dom_equal(
550 %(<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" />\n<link href="/stylesheets/subdir/subdir.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
551 stylesheet_link_tag(:all, :cache => true, :recursive => true)
552 )
553
554 assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
555
556 assert_dom_equal(
557 %(<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" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
558 stylesheet_link_tag(:all, :cache => "money")
559 )
560
561 assert_dom_equal(
562 %(<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" />\n<link href="/stylesheets/subdir/subdir.css" media="screen" rel="stylesheet" type="text/css" />\n<link href="/stylesheets/version.1.0.css" media="screen" rel="stylesheet" type="text/css" />),
563 stylesheet_link_tag(:all, :cache => "money", :recursive => true)
564 )
565
566 assert !File.exist?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
567 end
568 end
569
570 class AssetTagHelperNonVhostTest < ActionView::TestCase
571 tests ActionView::Helpers::AssetTagHelper
572
573 def setup
574 ActionController::Base.relative_url_root = "/collaboration/hieraki"
575
576 @controller = Class.new do
577 attr_accessor :request
578
579 def url_for(options)
580 "http://www.example.com/collaboration/hieraki"
581 end
582 end.new
583
584 @request = Class.new do
585 def protocol
586 'gopher://'
587 end
588 end.new
589
590 @controller.request = @request
591
592 ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
593 end
594
595 def teardown
596 ActionController::Base.relative_url_root = nil
597 end
598
599 def test_should_compute_proper_path
600 assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
601 assert_dom_equal(%(/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
602 assert_dom_equal(%(/collaboration/hieraki/stylesheets/style.css), stylesheet_path("style"))
603 assert_dom_equal(%(/collaboration/hieraki/images/xml.png), image_path("xml.png"))
604 assert_dom_equal(%(<img alt="Mouse" onmouseover="this.src='/collaboration/hieraki/images/mouse_over.png'" onmouseout="this.src='/collaboration/hieraki/images/mouse.png'" src="/collaboration/hieraki/images/mouse.png" />), image_tag("mouse.png", :mouseover => "/images/mouse_over.png"))
605 assert_dom_equal(%(<img alt="Mouse2" onmouseover="this.src='/collaboration/hieraki/images/mouse_over2.png'" onmouseout="this.src='/collaboration/hieraki/images/mouse2.png'" src="/collaboration/hieraki/images/mouse2.png" />), image_tag("mouse2.png", :mouseover => image_path("mouse_over2.png")))
606 end
607
608 def test_should_ignore_relative_root_path_on_complete_url
609 assert_dom_equal(%(http://www.example.com/images/xml.png), image_path("http://www.example.com/images/xml.png"))
610 end
611
612 def test_should_compute_proper_path_with_asset_host
613 ActionController::Base.asset_host = "http://assets.example.com"
614 assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
615 assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
616 assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/stylesheets/style.css), stylesheet_path("style"))
617 assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/images/xml.png), image_path("xml.png"))
618 assert_dom_equal(%(<img alt="Mouse" onmouseover="this.src='http://assets.example.com/collaboration/hieraki/images/mouse_over.png'" onmouseout="this.src='http://assets.example.com/collaboration/hieraki/images/mouse.png'" src="http://assets.example.com/collaboration/hieraki/images/mouse.png" />), image_tag("mouse.png", :mouseover => "/images/mouse_over.png"))
619 assert_dom_equal(%(<img alt="Mouse2" onmouseover="this.src='http://assets.example.com/collaboration/hieraki/images/mouse_over2.png'" onmouseout="this.src='http://assets.example.com/collaboration/hieraki/images/mouse2.png'" src="http://assets.example.com/collaboration/hieraki/images/mouse2.png" />), image_tag("mouse2.png", :mouseover => image_path("mouse_over2.png")))
620 ensure
621 ActionController::Base.asset_host = ""
622 end
623
624 def test_should_ignore_asset_host_on_complete_url
625 ActionController::Base.asset_host = "http://assets.example.com"
626 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"))
627 ensure
628 ActionController::Base.asset_host = ""
629 end
630
631 def test_should_wildcard_asset_host_between_zero_and_four
632 ActionController::Base.asset_host = 'http://a%d.example.com'
633 assert_match %r(http://a[0123].example.com/collaboration/hieraki/images/xml.png), image_path('xml.png')
634 ensure
635 ActionController::Base.asset_host = nil
636 end
637
638 def test_asset_host_without_protocol_should_use_request_protocol
639 ActionController::Base.asset_host = 'a.example.com'
640 assert_equal 'gopher://a.example.com/collaboration/hieraki/images/xml.png', image_path('xml.png')
641 ensure
642 ActionController::Base.asset_host = nil
643 end
644
645 def test_asset_host_without_protocol_should_use_request_protocol_even_if_path_present
646 ActionController::Base.asset_host = 'a.example.com/files/go/here'
647 assert_equal 'gopher://a.example.com/files/go/here/collaboration/hieraki/images/xml.png', image_path('xml.png')
648 ensure
649 ActionController::Base.asset_host = nil
650 end
651
652 def test_assert_css_and_js_of_the_same_name_return_correct_extension
653 assert_dom_equal(%(/collaboration/hieraki/javascripts/foo.js), javascript_path("foo"))
654 assert_dom_equal(%(/collaboration/hieraki/stylesheets/foo.css), stylesheet_path("foo"))
655
656 end
657 end