PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/actionpack/test/template/asset_tag_helper_test.rb

http://monkeycharger.googlecode.com/
Ruby | 374 lines | 302 code | 72 blank | 0 comment | 0 complexity | f2471c3a34c32258876a3bd57e63baa6 MD5 | raw file
  1. require "#{File.dirname(__FILE__)}/../abstract_unit"
  2. class AssetTagHelperTest < Test::Unit::TestCase
  3. include ActionView::Helpers::TagHelper
  4. include ActionView::Helpers::UrlHelper
  5. include ActionView::Helpers::AssetTagHelper
  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. ActionView::Helpers::AssetTagHelper.send(
  14. :const_set,
  15. :STYLESHEETS_DIR,
  16. File.dirname(__FILE__) + "/../fixtures/public/stylesheets"
  17. )
  18. ActionView::Helpers::AssetTagHelper.send(
  19. :const_set,
  20. :ASSETS_DIR,
  21. File.dirname(__FILE__) + "/../fixtures/public"
  22. )
  23. end
  24. @controller = Class.new do
  25. attr_accessor :request
  26. def url_for(*args) "http://www.example.com" end
  27. end.new
  28. @request = Class.new do
  29. def relative_url_root() "" end
  30. end.new
  31. @controller.request = @request
  32. ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
  33. end
  34. def teardown
  35. ActionController::Base.perform_caching = false
  36. ActionController::Base.asset_host = nil
  37. ENV["RAILS_ASSET_ID"] = nil
  38. end
  39. AutoDiscoveryToTag = {
  40. %(auto_discovery_link_tag) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
  41. %(auto_discovery_link_tag(:rss)) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
  42. %(auto_discovery_link_tag(:atom)) => %(<link href="http://www.example.com" rel="alternate" title="ATOM" type="application/atom+xml" />),
  43. %(auto_discovery_link_tag(:xml)) => %(<link href="http://www.example.com" rel="alternate" title="XML" type="application/xml" />),
  44. %(auto_discovery_link_tag(:rss, :action => "feed")) => %(<link href="http://www.example.com" rel="alternate" title="RSS" type="application/rss+xml" />),
  45. %(auto_discovery_link_tag(:rss, "http://localhost/feed")) => %(<link href="http://localhost/feed" rel="alternate" title="RSS" type="application/rss+xml" />),
  46. %(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" />),
  47. %(auto_discovery_link_tag(:rss, {}, {:title => "My RSS"})) => %(<link href="http://www.example.com" rel="alternate" title="My RSS" type="application/rss+xml" />),
  48. %(auto_discovery_link_tag(nil, {}, {:type => "text/html"})) => %(<link href="http://www.example.com" rel="alternate" title="" type="text/html" />),
  49. %(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" />),
  50. %(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" />),
  51. %(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" />),
  52. }
  53. JavascriptPathToTag = {
  54. %(javascript_path("xmlhr")) => %(/javascripts/xmlhr.js),
  55. %(javascript_path("super/xmlhr")) => %(/javascripts/super/xmlhr.js),
  56. %(javascript_path("/super/xmlhr.js")) => %(/super/xmlhr.js)
  57. }
  58. JavascriptIncludeToTag = {
  59. %(javascript_include_tag("xmlhr")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
  60. %(javascript_include_tag("xmlhr.js")) => %(<script src="/javascripts/xmlhr.js" type="text/javascript"></script>),
  61. %(javascript_include_tag("xmlhr", :lang => "vbscript")) => %(<script lang="vbscript" src="/javascripts/xmlhr.js" type="text/javascript"></script>),
  62. %(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>),
  63. %(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>),
  64. %(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>),
  65. %(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>),
  66. %(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>)
  67. }
  68. StylePathToTag = {
  69. %(stylesheet_path("style")) => %(/stylesheets/style.css),
  70. %(stylesheet_path("style.css")) => %(/stylesheets/style.css),
  71. %(stylesheet_path('dir/file')) => %(/stylesheets/dir/file.css),
  72. %(stylesheet_path('/dir/file.rcss')) => %(/dir/file.rcss)
  73. }
  74. StyleLinkToTag = {
  75. %(stylesheet_link_tag("style")) => %(<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
  76. %(stylesheet_link_tag("style.css")) => %(<link href="/stylesheets/style.css" media="screen" rel="Stylesheet" type="text/css" />),
  77. %(stylesheet_link_tag("/dir/file")) => %(<link href="/dir/file.css" media="screen" rel="Stylesheet" type="text/css" />),
  78. %(stylesheet_link_tag("dir/file")) => %(<link href="/stylesheets/dir/file.css" media="screen" rel="Stylesheet" type="text/css" />),
  79. %(stylesheet_link_tag("style", :media => "all")) => %(<link href="/stylesheets/style.css" media="all" rel="Stylesheet" type="text/css" />),
  80. %(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" />),
  81. %(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" />),
  82. %(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" />),
  83. %(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" />)
  84. }
  85. ImagePathToTag = {
  86. %(image_path("xml.png")) => %(/images/xml.png),
  87. %(image_path("dir/xml.png")) => %(/images/dir/xml.png),
  88. %(image_path("/dir/xml.png")) => %(/dir/xml.png)
  89. }
  90. ImageLinkToTag = {
  91. %(image_tag("xml.png")) => %(<img alt="Xml" src="/images/xml.png" />),
  92. %(image_tag("rss.gif", :alt => "rss syndication")) => %(<img alt="rss syndication" src="/images/rss.gif" />),
  93. %(image_tag("gold.png", :size => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
  94. %(image_tag("gold.png", "size" => "45x70")) => %(<img alt="Gold" height="70" src="/images/gold.png" width="45" />),
  95. %(image_tag("error.png", "size" => "45")) => %(<img alt="Error" src="/images/error.png" />),
  96. %(image_tag("error.png", "size" => "45 x 70")) => %(<img alt="Error" src="/images/error.png" />),
  97. %(image_tag("error.png", "size" => "x")) => %(<img alt="Error" src="/images/error.png" />),
  98. %(image_tag("http://www.rubyonrails.com/images/rails.png")) => %(<img alt="Rails" src="http://www.rubyonrails.com/images/rails.png" />)
  99. }
  100. DeprecatedImagePathToTag = {
  101. %(image_path("xml")) => %(/images/xml.png)
  102. }
  103. def test_auto_discovery_link_tag
  104. AutoDiscoveryToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  105. end
  106. def test_javascript_path
  107. JavascriptPathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  108. end
  109. def test_javascript_include_tag
  110. ENV["RAILS_ASSET_ID"] = ""
  111. JavascriptIncludeToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  112. ENV["RAILS_ASSET_ID"] = "1"
  113. 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))
  114. end
  115. def test_register_javascript_include_default
  116. ENV["RAILS_ASSET_ID"] = ""
  117. ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'slider'
  118. 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)
  119. ActionView::Helpers::AssetTagHelper::register_javascript_include_default 'lib1', '/elsewhere/blub/lib2'
  120. 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)
  121. end
  122. def test_stylesheet_path
  123. StylePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  124. end
  125. def test_stylesheet_link_tag
  126. ENV["RAILS_ASSET_ID"] = ""
  127. StyleLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  128. end
  129. def test_image_path
  130. ImagePathToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  131. end
  132. def test_image_tag
  133. ImageLinkToTag.each { |method, tag| assert_dom_equal(tag, eval(method)) }
  134. end
  135. def test_should_deprecate_image_filename_with_no_extension
  136. DeprecatedImagePathToTag.each do |method, tag|
  137. assert_deprecated("image_path") { assert_dom_equal(tag, eval(method)) }
  138. end
  139. end
  140. def test_timebased_asset_id
  141. expected_time = File.stat(File.expand_path(File.dirname(__FILE__) + "/../fixtures/public/images/rails.png")).mtime.to_i.to_s
  142. assert_equal %(<img alt="Rails" src="/images/rails.png?#{expected_time}" />), image_tag("rails.png")
  143. end
  144. def test_should_skip_asset_id_on_complete_url
  145. assert_equal %(<img alt="Rails" src="http://www.example.com/rails.png" />), image_tag("http://www.example.com/rails.png")
  146. end
  147. def test_should_use_preset_asset_id
  148. ENV["RAILS_ASSET_ID"] = "4500"
  149. assert_equal %(<img alt="Rails" src="/images/rails.png?4500" />), image_tag("rails.png")
  150. end
  151. def test_preset_empty_asset_id
  152. ENV["RAILS_ASSET_ID"] = ""
  153. assert_equal %(<img alt="Rails" src="/images/rails.png" />), image_tag("rails.png")
  154. end
  155. def test_should_not_modify_source_string
  156. source = '/images/rails.png'
  157. copy = source.dup
  158. image_tag(source)
  159. assert_equal copy, source
  160. end
  161. def test_caching_javascript_include_tag_when_caching_on
  162. ENV["RAILS_ASSET_ID"] = ""
  163. ActionController::Base.asset_host = 'http://a%d.example.com'
  164. ActionController::Base.perform_caching = true
  165. assert_dom_equal(
  166. %(<script src="http://a0.example.com/javascripts/all.js" type="text/javascript"></script>),
  167. javascript_include_tag(:all, :cache => true)
  168. )
  169. assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
  170. assert_dom_equal(
  171. %(<script src="http://a2.example.com/javascripts/money.js" type="text/javascript"></script>),
  172. javascript_include_tag(:all, :cache => "money")
  173. )
  174. assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
  175. ensure
  176. File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
  177. File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
  178. end
  179. def test_caching_javascript_include_tag_when_caching_on_and_using_subdirectory
  180. ENV["RAILS_ASSET_ID"] = ""
  181. ActionController::Base.asset_host = 'http://a%d.example.com'
  182. ActionController::Base.perform_caching = true
  183. assert_dom_equal(
  184. %(<script src="http://a3.example.com/javascripts/cache/money.js" type="text/javascript"></script>),
  185. javascript_include_tag(:all, :cache => "cache/money")
  186. )
  187. assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js'))
  188. ensure
  189. File.delete(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'cache', 'money.js'))
  190. end
  191. def test_caching_javascript_include_tag_when_caching_off
  192. ENV["RAILS_ASSET_ID"] = ""
  193. ActionController::Base.perform_caching = false
  194. assert_dom_equal(
  195. %(<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>),
  196. javascript_include_tag(:all, :cache => true)
  197. )
  198. assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'all.js'))
  199. assert_dom_equal(
  200. %(<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>),
  201. javascript_include_tag(:all, :cache => "money")
  202. )
  203. assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::JAVASCRIPTS_DIR, 'money.js'))
  204. end
  205. def test_caching_stylesheet_link_tag_when_caching_on
  206. ENV["RAILS_ASSET_ID"] = ""
  207. ActionController::Base.asset_host = 'http://a%d.example.com'
  208. ActionController::Base.perform_caching = true
  209. assert_dom_equal(
  210. %(<link href="http://a3.example.com/stylesheets/all.css" media="screen" rel="Stylesheet" type="text/css" />),
  211. stylesheet_link_tag(:all, :cache => true)
  212. )
  213. assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
  214. assert_dom_equal(
  215. %(<link href="http://a3.example.com/stylesheets/money.css" media="screen" rel="Stylesheet" type="text/css" />),
  216. stylesheet_link_tag(:all, :cache => "money")
  217. )
  218. assert File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  219. ensure
  220. File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
  221. File.delete(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  222. end
  223. def test_caching_stylesheet_include_tag_when_caching_off
  224. ENV["RAILS_ASSET_ID"] = ""
  225. ActionController::Base.perform_caching = false
  226. assert_dom_equal(
  227. %(<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" />),
  228. stylesheet_link_tag(:all, :cache => true)
  229. )
  230. assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'all.css'))
  231. assert_dom_equal(
  232. %(<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" />),
  233. stylesheet_link_tag(:all, :cache => "money")
  234. )
  235. assert !File.exists?(File.join(ActionView::Helpers::AssetTagHelper::STYLESHEETS_DIR, 'money.css'))
  236. end
  237. end
  238. class AssetTagHelperNonVhostTest < Test::Unit::TestCase
  239. include ActionView::Helpers::TagHelper
  240. include ActionView::Helpers::UrlHelper
  241. include ActionView::Helpers::AssetTagHelper
  242. def setup
  243. @controller = Class.new do
  244. attr_accessor :request
  245. def url_for(options)
  246. "http://www.example.com/collaboration/hieraki"
  247. end
  248. end.new
  249. @request = Class.new do
  250. def relative_url_root
  251. "/collaboration/hieraki"
  252. end
  253. def protocol
  254. 'gopher://'
  255. end
  256. end.new
  257. @controller.request = @request
  258. ActionView::Helpers::AssetTagHelper::reset_javascript_include_default
  259. end
  260. def test_should_compute_proper_path
  261. assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
  262. assert_dom_equal(%(/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
  263. assert_dom_equal(%(/collaboration/hieraki/stylesheets/style.css), stylesheet_path("style"))
  264. assert_dom_equal(%(/collaboration/hieraki/images/xml.png), image_path("xml.png"))
  265. end
  266. def test_should_ignore_relative_root_path_on_complete_url
  267. assert_dom_equal(%(http://www.example.com/images/xml.png), image_path("http://www.example.com/images/xml.png"))
  268. end
  269. def test_should_compute_proper_path_with_asset_host
  270. ActionController::Base.asset_host = "http://assets.example.com"
  271. assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag)
  272. assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr"))
  273. assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/stylesheets/style.css), stylesheet_path("style"))
  274. assert_dom_equal(%(http://assets.example.com/collaboration/hieraki/images/xml.png), image_path("xml.png"))
  275. ensure
  276. ActionController::Base.asset_host = ""
  277. end
  278. def test_should_ignore_asset_host_on_complete_url
  279. ActionController::Base.asset_host = "http://assets.example.com"
  280. 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"))
  281. ensure
  282. ActionController::Base.asset_host = ""
  283. end
  284. def test_should_wildcard_asset_host_between_zero_and_four
  285. ActionController::Base.asset_host = 'http://a%d.example.com'
  286. assert_match %r(http://a[0123].example.com/collaboration/hieraki/images/xml.png), image_path('xml.png')
  287. ensure
  288. ActionController::Base.asset_host = nil
  289. end
  290. def test_asset_host_without_protocol_should_use_request_protocol
  291. ActionController::Base.asset_host = 'a.example.com'
  292. assert_equal 'gopher://a.example.com/collaboration/hieraki/images/xml.png', image_path('xml.png')
  293. ensure
  294. ActionController::Base.asset_host = nil
  295. end
  296. def test_asset_host_without_protocol_should_use_request_protocol_even_if_path_present
  297. ActionController::Base.asset_host = 'a.example.com/files/go/here'
  298. assert_equal 'gopher://a.example.com/files/go/here/collaboration/hieraki/images/xml.png', image_path('xml.png')
  299. ensure
  300. ActionController::Base.asset_host = nil
  301. end
  302. end