PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/bricooke/my-biz-expenses
Ruby | 366 lines | 306 code | 59 blank | 1 comment | 0 complexity | 5edd9f042660fd3bf85c6b24f9bc7b69 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. require File.dirname(__FILE__) + '/../../lib/action_view/helpers/url_helper'
  3. require File.dirname(__FILE__) + '/../../lib/action_view/helpers/asset_tag_helper'
  4. require File.dirname(__FILE__) + '/../../lib/action_view/helpers/tag_helper'
  5. RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port)
  6. class UrlHelperTest < Test::Unit::TestCase
  7. include ActionView::Helpers::AssetTagHelper
  8. include ActionView::Helpers::UrlHelper
  9. include ActionView::Helpers::TagHelper
  10. def setup
  11. @controller = Class.new do
  12. attr_accessor :url, :request
  13. def url_for(options, *parameters_for_method_reference)
  14. url
  15. end
  16. end
  17. @controller = @controller.new
  18. @controller.url = "http://www.example.com"
  19. end
  20. def test_url_for_escapes_urls
  21. @controller.url = "http://www.example.com?a=b&c=d"
  22. assert_equal "http://www.example.com?a=b&amp;c=d", url_for(:a => 'b', :c => 'd')
  23. assert_equal "http://www.example.com?a=b&amp;c=d", url_for(:a => 'b', :c => 'd', :escape => true)
  24. assert_equal "http://www.example.com?a=b&c=d", url_for(:a => 'b', :c => 'd', :escape => false)
  25. end
  26. # todo: missing test cases
  27. def test_button_to_with_straight_url
  28. assert_dom_equal "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com")
  29. end
  30. def test_button_to_with_query
  31. assert_dom_equal "<form method=\"post\" action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com/q1=v1&q2=v2")
  32. end
  33. def test_button_to_with_escaped_query
  34. assert_dom_equal "<form method=\"post\" action=\"http://www.example.com/q1=v1&amp;q2=v2\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>", button_to("Hello", "http://www.example.com/q1=v1&amp;q2=v2")
  35. end
  36. def test_button_to_with_query_and_no_name
  37. assert_dom_equal "<form method=\"post\" action=\"http://www.example.com?q1=v1&amp;q2=v2\" class=\"button-to\"><div><input type=\"submit\" value=\"http://www.example.com?q1=v1&amp;q2=v2\" /></div></form>", button_to(nil, "http://www.example.com?q1=v1&q2=v2")
  38. end
  39. def test_button_to_with_javascript_confirm
  40. assert_dom_equal(
  41. "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input onclick=\"return confirm('Are you sure?');\" type=\"submit\" value=\"Hello\" /></div></form>",
  42. button_to("Hello", "http://www.example.com", :confirm => "Are you sure?")
  43. )
  44. end
  45. def test_button_to_enabled_disabled
  46. assert_dom_equal(
  47. "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
  48. button_to("Hello", "http://www.example.com", :disabled => false)
  49. )
  50. assert_dom_equal(
  51. "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input disabled=\"disabled\" type=\"submit\" value=\"Hello\" /></div></form>",
  52. button_to("Hello", "http://www.example.com", :disabled => true)
  53. )
  54. end
  55. def test_button_to_with_method_delete
  56. assert_dom_equal(
  57. "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"hidden\" name=\"_method\" value=\"delete\" /><input type=\"submit\" value=\"Hello\" /></div></form>",
  58. button_to("Hello", "http://www.example.com", :method => :delete)
  59. )
  60. end
  61. def test_button_to_with_method_get
  62. assert_dom_equal(
  63. "<form method=\"get\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
  64. button_to("Hello", "http://www.example.com", :method => :get)
  65. )
  66. end
  67. def test_link_tag_with_straight_url
  68. assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")
  69. end
  70. def test_link_tag_with_query
  71. assert_dom_equal "<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">Hello</a>", link_to("Hello", "http://www.example.com?q1=v1&amp;q2=v2")
  72. end
  73. def test_link_tag_with_query_and_no_name
  74. assert_dom_equal "<a href=\"http://www.example.com?q1=v1&amp;q2=v2\">http://www.example.com?q1=v1&amp;q2=v2</a>", link_to(nil, "http://www.example.com?q1=v1&amp;q2=v2")
  75. end
  76. def test_link_tag_with_img
  77. assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' /></a>", link_to("<img src='/favicon.jpg' />", "http://www.example.com")
  78. end
  79. def test_link_with_nil_html_options
  80. assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", {:action => 'myaction'}, nil)
  81. end
  82. def test_link_tag_with_custom_onclick
  83. assert_dom_equal "<a href=\"http://www.example.com\" onclick=\"alert('yay!')\">Hello</a>", link_to("Hello", "http://www.example.com", :onclick => "alert('yay!')")
  84. end
  85. def test_link_tag_with_javascript_confirm
  86. assert_dom_equal(
  87. "<a href=\"http://www.example.com\" onclick=\"return confirm('Are you sure?');\">Hello</a>",
  88. link_to("Hello", "http://www.example.com", :confirm => "Are you sure?")
  89. )
  90. assert_dom_equal(
  91. "<a href=\"http://www.example.com\" onclick=\"return confirm('You can\\'t possibly be sure, can you?');\">Hello</a>",
  92. link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure, can you?")
  93. )
  94. assert_dom_equal(
  95. "<a href=\"http://www.example.com\" onclick=\"return confirm('You can\\'t possibly be sure,\\n can you?');\">Hello</a>",
  96. link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure,\n can you?")
  97. )
  98. end
  99. def test_link_tag_with_popup
  100. assert_dom_equal(
  101. "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
  102. link_to("Hello", "http://www.example.com", :popup => true)
  103. )
  104. assert_dom_equal(
  105. "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
  106. link_to("Hello", "http://www.example.com", :popup => 'true')
  107. )
  108. assert_dom_equal(
  109. "<a href=\"http://www.example.com\" onclick=\"window.open(this.href,'window_name','width=300,height=300');return false;\">Hello</a>",
  110. link_to("Hello", "http://www.example.com", :popup => ['window_name', 'width=300,height=300'])
  111. )
  112. end
  113. def test_link_tag_with_popup_and_javascript_confirm
  114. assert_dom_equal(
  115. "<a href=\"http://www.example.com\" onclick=\"if (confirm('Fo\\' sho\\'?')) { window.open(this.href); };return false;\">Hello</a>",
  116. link_to("Hello", "http://www.example.com", { :popup => true, :confirm => "Fo' sho'?" })
  117. )
  118. assert_dom_equal(
  119. "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { window.open(this.href,'window_name','width=300,height=300'); };return false;\">Hello</a>",
  120. link_to("Hello", "http://www.example.com", { :popup => ['window_name', 'width=300,height=300'], :confirm => "Are you serious?" })
  121. )
  122. end
  123. def test_link_tag_with_post_is_deprecated
  124. assert_deprecated 'post' do
  125. assert_dom_equal(
  126. "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>",
  127. link_to("Hello", "http://www.example.com", :post => true)
  128. )
  129. end
  130. end
  131. def test_link_tag_using_post_javascript
  132. assert_dom_equal(
  133. "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit();return false;\">Hello</a>",
  134. link_to("Hello", "http://www.example.com", :method => :post)
  135. )
  136. end
  137. def test_link_tag_using_delete_javascript
  138. assert_dom_equal(
  139. "<a href='http://www.example.com' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;var m = document.createElement('input'); m.setAttribute('type', 'hidden'); m.setAttribute('name', '_method'); m.setAttribute('value', 'delete'); f.appendChild(m);f.submit();return false;\">Destroy</a>",
  140. link_to("Destroy", "http://www.example.com", :method => :delete)
  141. )
  142. end
  143. def test_link_tag_using_post_javascript_and_confirm
  144. assert_dom_equal(
  145. "<a href=\"http://www.example.com\" onclick=\"if (confirm('Are you serious?')) { var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = this.href;f.submit(); };return false;\">Hello</a>",
  146. link_to("Hello", "http://www.example.com", :method => :post, :confirm => "Are you serious?")
  147. )
  148. end
  149. def test_link_tag_using_post_javascript_and_popup
  150. assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
  151. end
  152. def test_link_to_unless
  153. assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
  154. assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
  155. assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1)
  156. assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options, *parameters_for_method_reference|
  157. "<strong>#{name}</strong>"
  158. }
  159. assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name|
  160. "<strong>#{name}</strong>"
  161. }
  162. assert_equal "test", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) {
  163. "test"
  164. }
  165. end
  166. def test_link_to_if
  167. assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog")
  168. assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_if(true, "Listing", :action => "list", :controller => "weblog")
  169. assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1)
  170. end
  171. def test_link_unless_current
  172. @controller.request = RequestMock.new("http://www.example.com/weblog/show")
  173. @controller.url = "http://www.example.com/weblog/show"
  174. assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
  175. assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
  176. @controller.request = RequestMock.new("http://www.example.com/weblog/show")
  177. @controller.url = "http://www.example.com/weblog/list"
  178. assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
  179. link_to_unless_current("Listing", :action => "list", :controller => "weblog")
  180. assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
  181. link_to_unless_current("Listing", "http://www.example.com/weblog/list")
  182. end
  183. def test_mail_to
  184. assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>", mail_to("david@loudthinking.com")
  185. assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>", mail_to("david@loudthinking.com", "David Heinemeier Hansson")
  186. assert_dom_equal(
  187. "<a class=\"admin\" href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>",
  188. mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin")
  189. )
  190. assert_equal mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin"),
  191. mail_to("david@loudthinking.com", "David Heinemeier Hansson", :class => "admin")
  192. end
  193. def test_mail_to_with_javascript
  194. assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript")
  195. end
  196. def test_mail_with_options
  197. assert_dom_equal(
  198. %(<a href="mailto:me@example.com?cc=ccaddress%40example.com&amp;bcc=bccaddress%40example.com&amp;body=This%20is%20the%20body%20of%20the%20message.&amp;subject=This%20is%20an%20example%20email">My email</a>),
  199. mail_to("me@example.com", "My email", :cc => "ccaddress@example.com", :bcc => "bccaddress@example.com", :subject => "This is an example email", :body => "This is the body of the message.")
  200. )
  201. end
  202. def test_mail_to_with_img
  203. assert_dom_equal %(<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>), mail_to('feedback@example.com', '<img src="/feedback.png" />')
  204. end
  205. def test_mail_to_with_hex
  206. assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>", mail_to("me@domain.com", "My email", :encode => "hex")
  207. end
  208. def test_mail_to_with_replace_options
  209. assert_dom_equal "<a href=\"mailto:wolfgang@stufenlos.net\">wolfgang(at)stufenlos(dot)net</a>", mail_to("wolfgang@stufenlos.net", nil, :replace_at => "(at)", :replace_dot => "(dot)")
  210. assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain.com</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)")
  211. assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>", mail_to("me@domain.com", "My email", :encode => "hex", :replace_at => "(at)")
  212. assert_dom_equal "<a href=\"mailto:%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">me(at)domain(dot)com</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)", :replace_dot => "(dot)")
  213. assert_dom_equal "<script type=\"text/javascript\">eval(unescape('%64%6f%63%75%6d%65%6e%74%2e%77%72%69%74%65%28%27%3c%61%20%68%72%65%66%3d%22%6d%61%69%6c%74%6f%3a%6d%65%40%64%6f%6d%61%69%6e%2e%63%6f%6d%22%3e%4d%79%20%65%6d%61%69%6c%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", "My email", :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)")
  214. end
  215. end
  216. class UrlHelperWithControllerTest < Test::Unit::TestCase
  217. class UrlHelperController < ActionController::Base
  218. self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
  219. def self.controller_path; 'url_helper_with_controller' end
  220. def show_url_for
  221. render :inline => "<%= url_for :controller => 'url_helper_with_controller', :action => 'show_url_for' %>"
  222. end
  223. def show_named_route
  224. render :inline => "<%= show_named_route_#{params[:kind]} %>"
  225. end
  226. def rescue_action(e) raise e end
  227. end
  228. include ActionView::Helpers::UrlHelper
  229. def setup
  230. @request = ActionController::TestRequest.new
  231. @response = ActionController::TestResponse.new
  232. @controller = UrlHelperController.new
  233. end
  234. def test_url_for_shows_only_path
  235. get :show_url_for
  236. assert_equal '/url_helper_with_controller/show_url_for', @response.body
  237. end
  238. def test_named_route_shows_host_and_path
  239. with_url_helper_routing do
  240. get :show_named_route, :kind => 'url'
  241. assert_equal 'http://test.host/url_helper_with_controller/show_named_route', @response.body
  242. end
  243. end
  244. def test_named_route_path_shows_only_path
  245. with_url_helper_routing do
  246. get :show_named_route, :kind => 'path'
  247. assert_equal '/url_helper_with_controller/show_named_route', @response.body
  248. end
  249. end
  250. protected
  251. def with_url_helper_routing
  252. with_routing do |set|
  253. set.draw do |map|
  254. map.show_named_route 'url_helper_with_controller/show_named_route', :controller => 'url_helper_with_controller', :action => 'show_named_route'
  255. end
  256. yield
  257. end
  258. end
  259. end
  260. class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase
  261. class TasksController < ActionController::Base
  262. self.template_root = "#{File.dirname(__FILE__)}/../fixtures/"
  263. def self.controller_path; 'tasks' end
  264. def index
  265. render_default
  266. end
  267. def show
  268. render_default
  269. end
  270. def rescue_action(e) raise e end
  271. protected
  272. def render_default
  273. render :inline =>
  274. "<%= link_to_unless_current(\"tasks\", tasks_path) %>\n" +
  275. "<%= link_to_unless_current(\"tasks\", tasks_url) %>"
  276. end
  277. end
  278. include ActionView::Helpers::UrlHelper
  279. def setup
  280. @request = ActionController::TestRequest.new
  281. @response = ActionController::TestResponse.new
  282. @controller = TasksController.new
  283. end
  284. def test_link_to_unless_current_to_current
  285. with_restful_routing do
  286. get :index
  287. assert_equal "tasks\ntasks", @response.body
  288. end
  289. end
  290. def test_link_to_unless_current_shows_link
  291. with_restful_routing do
  292. get :show, :id => 1
  293. assert_equal "<a href=\"/tasks\">tasks</a>\n" +
  294. "<a href=\"#{@request.protocol}#{@request.host_with_port}/tasks\">tasks</a>",
  295. @response.body
  296. end
  297. end
  298. protected
  299. def with_restful_routing
  300. with_routing do |set|
  301. set.draw do |map|
  302. map.resources :tasks
  303. end
  304. yield
  305. end
  306. end
  307. end