PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://github.com/millbanksystems/epetitions
Ruby | 516 lines | 423 code | 92 blank | 1 comment | 0 complexity | a5c52fc08fc6e71bc3a2b8814d96f0c3 MD5 | raw file
Possible License(s): Apache-2.0
  1. require "#{File.dirname(__FILE__)}/../abstract_unit"
  2. RequestMock = Struct.new("Request", :request_uri, :protocol, :host_with_port)
  3. class UrlHelperTest < Test::Unit::TestCase
  4. include ActionView::Helpers::AssetTagHelper
  5. include ActionView::Helpers::UrlHelper
  6. include ActionView::Helpers::TagHelper
  7. def setup
  8. @controller = Class.new do
  9. attr_accessor :url, :request
  10. def url_for(options)
  11. url
  12. end
  13. end
  14. @controller = @controller.new
  15. @controller.url = "http://www.example.com"
  16. end
  17. def test_url_for_escapes_urls
  18. @controller.url = "http://www.example.com?a=b&c=d"
  19. assert_equal "http://www.example.com?a=b&amp;c=d", url_for(:a => 'b', :c => 'd')
  20. assert_equal "http://www.example.com?a=b&amp;c=d", url_for(:a => 'b', :c => 'd', :escape => true)
  21. assert_equal "http://www.example.com?a=b&c=d", url_for(:a => 'b', :c => 'd', :escape => false)
  22. end
  23. def test_url_for_escapes_url_once
  24. @controller.url = "http://www.example.com?a=b&amp;c=d"
  25. assert_equal "http://www.example.com?a=b&amp;c=d", url_for("http://www.example.com?a=b&amp;c=d")
  26. end
  27. # todo: missing test cases
  28. def test_button_to_with_straight_url
  29. 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")
  30. end
  31. def test_button_to_with_query
  32. 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")
  33. end
  34. def test_button_to_with_escaped_query
  35. 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")
  36. end
  37. def test_button_to_with_query_and_no_name
  38. 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")
  39. end
  40. def test_button_to_with_javascript_confirm
  41. assert_dom_equal(
  42. "<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>",
  43. button_to("Hello", "http://www.example.com", :confirm => "Are you sure?")
  44. )
  45. end
  46. def test_button_to_enabled_disabled
  47. assert_dom_equal(
  48. "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
  49. button_to("Hello", "http://www.example.com", :disabled => false)
  50. )
  51. assert_dom_equal(
  52. "<form method=\"post\" action=\"http://www.example.com\" class=\"button-to\"><div><input disabled=\"disabled\" type=\"submit\" value=\"Hello\" /></div></form>",
  53. button_to("Hello", "http://www.example.com", :disabled => true)
  54. )
  55. end
  56. def test_button_to_with_method_delete
  57. assert_dom_equal(
  58. "<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>",
  59. button_to("Hello", "http://www.example.com", :method => :delete)
  60. )
  61. end
  62. def test_button_to_with_method_get
  63. assert_dom_equal(
  64. "<form method=\"get\" action=\"http://www.example.com\" class=\"button-to\"><div><input type=\"submit\" value=\"Hello\" /></div></form>",
  65. button_to("Hello", "http://www.example.com", :method => :get)
  66. )
  67. end
  68. def test_link_tag_with_straight_url
  69. assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", "http://www.example.com")
  70. end
  71. def test_link_tag_without_host_option
  72. ActionController::Base.class_eval { attr_accessor :url }
  73. url = {:controller => 'weblog', :action => 'show'}
  74. @controller = ActionController::Base.new
  75. @controller.request = ActionController::TestRequest.new
  76. @controller.url = ActionController::UrlRewriter.new(@controller.request, url)
  77. assert_dom_equal(%q{<a href="/weblog/show">Test Link</a>}, link_to('Test Link', url))
  78. end
  79. def test_link_tag_with_host_option
  80. ActionController::Base.class_eval { attr_accessor :url }
  81. url = {:controller => 'weblog', :action => 'show', :host => 'www.example.com'}
  82. @controller = ActionController::Base.new
  83. @controller.request = ActionController::TestRequest.new
  84. @controller.url = ActionController::UrlRewriter.new(@controller.request, url)
  85. assert_dom_equal(%q{<a href="http://www.example.com/weblog/show">Test Link</a>}, link_to('Test Link', url))
  86. end
  87. def test_link_tag_with_query
  88. 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")
  89. end
  90. def test_link_tag_with_query_and_no_name
  91. 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")
  92. end
  93. def test_link_tag_with_img
  94. assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' /></a>", link_to("<img src='/favicon.jpg' />", "http://www.example.com")
  95. end
  96. def test_link_with_nil_html_options
  97. assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", {:action => 'myaction'}, nil)
  98. end
  99. def test_link_tag_with_custom_onclick
  100. assert_dom_equal "<a href=\"http://www.example.com\" onclick=\"alert('yay!')\">Hello</a>", link_to("Hello", "http://www.example.com", :onclick => "alert('yay!')")
  101. end
  102. def test_link_tag_with_javascript_confirm
  103. assert_dom_equal(
  104. "<a href=\"http://www.example.com\" onclick=\"return confirm('Are you sure?');\">Hello</a>",
  105. link_to("Hello", "http://www.example.com", :confirm => "Are you sure?")
  106. )
  107. assert_dom_equal(
  108. "<a href=\"http://www.example.com\" onclick=\"return confirm('You can\\'t possibly be sure, can you?');\">Hello</a>",
  109. link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure, can you?")
  110. )
  111. assert_dom_equal(
  112. "<a href=\"http://www.example.com\" onclick=\"return confirm('You can\\'t possibly be sure,\\n can you?');\">Hello</a>",
  113. link_to("Hello", "http://www.example.com", :confirm => "You can't possibly be sure,\n can you?")
  114. )
  115. end
  116. def test_link_tag_with_popup
  117. assert_dom_equal(
  118. "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
  119. link_to("Hello", "http://www.example.com", :popup => true)
  120. )
  121. assert_dom_equal(
  122. "<a href=\"http://www.example.com\" onclick=\"window.open(this.href);return false;\">Hello</a>",
  123. link_to("Hello", "http://www.example.com", :popup => 'true')
  124. )
  125. assert_dom_equal(
  126. "<a href=\"http://www.example.com\" onclick=\"window.open(this.href,'window_name','width=300,height=300');return false;\">Hello</a>",
  127. link_to("Hello", "http://www.example.com", :popup => ['window_name', 'width=300,height=300'])
  128. )
  129. end
  130. def test_link_tag_with_popup_and_javascript_confirm
  131. assert_dom_equal(
  132. "<a href=\"http://www.example.com\" onclick=\"if (confirm('Fo\\' sho\\'?')) { window.open(this.href); };return false;\">Hello</a>",
  133. link_to("Hello", "http://www.example.com", { :popup => true, :confirm => "Fo' sho'?" })
  134. )
  135. assert_dom_equal(
  136. "<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>",
  137. link_to("Hello", "http://www.example.com", { :popup => ['window_name', 'width=300,height=300'], :confirm => "Are you serious?" })
  138. )
  139. end
  140. def test_link_tag_using_post_javascript
  141. assert_dom_equal(
  142. "<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>",
  143. link_to("Hello", "http://www.example.com", :method => :post)
  144. )
  145. end
  146. def test_link_tag_using_delete_javascript
  147. assert_dom_equal(
  148. "<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>",
  149. link_to("Destroy", "http://www.example.com", :method => :delete)
  150. )
  151. end
  152. def test_link_tag_using_delete_javascript_and_href
  153. assert_dom_equal(
  154. "<a href='\#' onclick=\"var f = document.createElement('form'); f.style.display = 'none'; this.parentNode.appendChild(f); f.method = 'POST'; f.action = 'http://www.example.com';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>",
  155. link_to("Destroy", "http://www.example.com", :method => :delete, :href => '#')
  156. )
  157. end
  158. def test_link_tag_using_post_javascript_and_confirm
  159. assert_dom_equal(
  160. "<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>",
  161. link_to("Hello", "http://www.example.com", :method => :post, :confirm => "Are you serious?")
  162. )
  163. end
  164. def test_link_tag_using_post_javascript_and_popup
  165. assert_raises(ActionView::ActionViewError) { link_to("Hello", "http://www.example.com", :popup => true, :method => :post, :confirm => "Are you serious?") }
  166. end
  167. def test_link_to_unless
  168. assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog")
  169. assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog")
  170. assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1)
  171. assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options|
  172. "<strong>#{name}</strong>"
  173. }
  174. assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name|
  175. "<strong>#{name}</strong>"
  176. }
  177. assert_equal "test", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) {
  178. "test"
  179. }
  180. end
  181. def test_link_to_if
  182. assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog")
  183. assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_if(true, "Listing", :action => "list", :controller => "weblog")
  184. assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1)
  185. end
  186. def test_link_unless_current
  187. @controller.request = RequestMock.new("http://www.example.com/weblog/show")
  188. @controller.url = "http://www.example.com/weblog/show"
  189. assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" })
  190. assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show")
  191. @controller.request = RequestMock.new("http://www.example.com/weblog/show")
  192. @controller.url = "http://www.example.com/weblog/list"
  193. assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
  194. link_to_unless_current("Listing", :action => "list", :controller => "weblog")
  195. assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>",
  196. link_to_unless_current("Listing", "http://www.example.com/weblog/list")
  197. end
  198. def test_mail_to
  199. assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>", mail_to("david@loudthinking.com")
  200. assert_dom_equal "<a href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>", mail_to("david@loudthinking.com", "David Heinemeier Hansson")
  201. assert_dom_equal(
  202. "<a class=\"admin\" href=\"mailto:david@loudthinking.com\">David Heinemeier Hansson</a>",
  203. mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin")
  204. )
  205. assert_equal mail_to("david@loudthinking.com", "David Heinemeier Hansson", "class" => "admin"),
  206. mail_to("david@loudthinking.com", "David Heinemeier Hansson", :class => "admin")
  207. end
  208. def test_mail_to_with_javascript
  209. 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")
  210. end
  211. def test_mail_with_options
  212. assert_dom_equal(
  213. %(<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>),
  214. 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.")
  215. )
  216. end
  217. def test_mail_to_with_img
  218. assert_dom_equal %(<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>), mail_to('feedback@example.com', '<img src="/feedback.png" />')
  219. end
  220. def test_mail_to_with_hex
  221. assert_dom_equal "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">My email</a>", mail_to("me@domain.com", "My email", :encode => "hex")
  222. assert_dom_equal "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#64;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>", mail_to("me@domain.com", nil, :encode => "hex")
  223. end
  224. def test_mail_to_with_replace_options
  225. 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)")
  226. assert_dom_equal "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#40;&#97;&#116;&#41;&#100;&#111;&#109;&#97;&#105;&#110;&#46;&#99;&#111;&#109;</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)")
  227. assert_dom_equal "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%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)")
  228. assert_dom_equal "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;%6d%65@%64%6f%6d%61%69%6e.%63%6f%6d\">&#109;&#101;&#40;&#97;&#116;&#41;&#100;&#111;&#109;&#97;&#105;&#110;&#40;&#100;&#111;&#116;&#41;&#99;&#111;&#109;</a>", mail_to("me@domain.com", nil, :encode => "hex", :replace_at => "(at)", :replace_dot => "(dot)")
  229. 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)")
  230. end
  231. def protect_against_forgery?
  232. false
  233. end
  234. end
  235. class UrlHelperWithControllerTest < Test::Unit::TestCase
  236. class UrlHelperController < ActionController::Base
  237. self.view_paths = [ "#{File.dirname(__FILE__)}/../fixtures/" ]
  238. def self.controller_path; 'url_helper_with_controller' end
  239. def show_url_for
  240. render :inline => "<%= url_for :controller => 'url_helper_with_controller', :action => 'show_url_for' %>"
  241. end
  242. def show_named_route
  243. render :inline => "<%= show_named_route_#{params[:kind]} %>"
  244. end
  245. def rescue_action(e) raise e end
  246. end
  247. include ActionView::Helpers::UrlHelper
  248. def setup
  249. @request = ActionController::TestRequest.new
  250. @response = ActionController::TestResponse.new
  251. @controller = UrlHelperController.new
  252. end
  253. def test_url_for_shows_only_path
  254. get :show_url_for
  255. assert_equal '/url_helper_with_controller/show_url_for', @response.body
  256. end
  257. def test_named_route_shows_host_and_path
  258. with_url_helper_routing do
  259. get :show_named_route, :kind => 'url'
  260. assert_equal 'http://test.host/url_helper_with_controller/show_named_route', @response.body
  261. end
  262. end
  263. def test_named_route_path_shows_only_path
  264. with_url_helper_routing do
  265. get :show_named_route, :kind => 'path'
  266. assert_equal '/url_helper_with_controller/show_named_route', @response.body
  267. end
  268. end
  269. protected
  270. def with_url_helper_routing
  271. with_routing do |set|
  272. set.draw do |map|
  273. map.show_named_route 'url_helper_with_controller/show_named_route', :controller => 'url_helper_with_controller', :action => 'show_named_route'
  274. end
  275. yield
  276. end
  277. end
  278. end
  279. class LinkToUnlessCurrentWithControllerTest < Test::Unit::TestCase
  280. class TasksController < ActionController::Base
  281. self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
  282. def self.controller_path; 'tasks' end
  283. def index
  284. render_default
  285. end
  286. def show
  287. render_default
  288. end
  289. def rescue_action(e) raise e end
  290. protected
  291. def render_default
  292. render :inline =>
  293. "<%= link_to_unless_current(\"tasks\", tasks_path) %>\n" +
  294. "<%= link_to_unless_current(\"tasks\", tasks_url) %>"
  295. end
  296. end
  297. include ActionView::Helpers::UrlHelper
  298. def setup
  299. @request = ActionController::TestRequest.new
  300. @response = ActionController::TestResponse.new
  301. @controller = TasksController.new
  302. end
  303. def test_link_to_unless_current_to_current
  304. with_restful_routing do
  305. get :index
  306. assert_equal "tasks\ntasks", @response.body
  307. end
  308. end
  309. def test_link_to_unless_current_shows_link
  310. with_restful_routing do
  311. get :show, :id => 1
  312. assert_equal "<a href=\"/tasks\">tasks</a>\n" +
  313. "<a href=\"#{@request.protocol}#{@request.host_with_port}/tasks\">tasks</a>",
  314. @response.body
  315. end
  316. end
  317. protected
  318. def with_restful_routing
  319. with_routing do |set|
  320. set.draw do |map|
  321. map.resources :tasks
  322. end
  323. yield
  324. end
  325. end
  326. end
  327. class Workshop
  328. attr_accessor :id, :new_record
  329. def initialize(id, new_record)
  330. @id, @new_record = id, new_record
  331. end
  332. def new_record?
  333. @new_record
  334. end
  335. def to_s
  336. id.to_s
  337. end
  338. end
  339. class Session
  340. attr_accessor :id, :workshop_id, :new_record
  341. def initialize(id, new_record)
  342. @id, @new_record = id, new_record
  343. end
  344. def new_record?
  345. @new_record
  346. end
  347. def to_s
  348. id.to_s
  349. end
  350. end
  351. class PolymorphicControllerTest < Test::Unit::TestCase
  352. class WorkshopsController < ActionController::Base
  353. self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
  354. def self.controller_path; 'workshops' end
  355. def index
  356. @workshop = Workshop.new(1, true)
  357. render :inline => "<%= url_for(@workshop) %>\n<%= link_to('Workshop', @workshop) %>"
  358. end
  359. def show
  360. @workshop = Workshop.new(params[:id], false)
  361. render :inline => "<%= url_for(@workshop) %>\n<%= link_to('Workshop', @workshop) %>"
  362. end
  363. def rescue_action(e) raise e end
  364. end
  365. class SessionsController < ActionController::Base
  366. self.view_paths = ["#{File.dirname(__FILE__)}/../fixtures/"]
  367. def self.controller_path; 'sessions' end
  368. def index
  369. @workshop = Workshop.new(params[:workshop_id], false)
  370. @session = Session.new(1, true)
  371. render :inline => "<%= url_for([@workshop, @session]) %>\n<%= link_to('Session', [@workshop, @session]) %>"
  372. end
  373. def show
  374. @workshop = Workshop.new(params[:workshop_id], false)
  375. @session = Session.new(params[:id], false)
  376. render :inline => "<%= url_for([@workshop, @session]) %>\n<%= link_to('Session', [@workshop, @session]) %>"
  377. end
  378. def rescue_action(e) raise e end
  379. end
  380. include ActionView::Helpers::UrlHelper
  381. def setup
  382. @request = ActionController::TestRequest.new
  383. @response = ActionController::TestResponse.new
  384. end
  385. def test_new_resource
  386. @controller = WorkshopsController.new
  387. with_restful_routing do
  388. get :index
  389. assert_equal "/workshops\n<a href=\"/workshops\">Workshop</a>", @response.body
  390. end
  391. end
  392. def test_existing_resource
  393. @controller = WorkshopsController.new
  394. with_restful_routing do
  395. get :show, :id => 1
  396. assert_equal "/workshops/1\n<a href=\"/workshops/1\">Workshop</a>", @response.body
  397. end
  398. end
  399. def test_new_nested_resource
  400. @controller = SessionsController.new
  401. with_restful_routing do
  402. get :index, :workshop_id => 1
  403. assert_equal "/workshops/1/sessions\n<a href=\"/workshops/1/sessions\">Session</a>", @response.body
  404. end
  405. end
  406. def test_existing_nested_resource
  407. @controller = SessionsController.new
  408. with_restful_routing do
  409. get :show, :workshop_id => 1, :id => 1
  410. assert_equal "/workshops/1/sessions/1\n<a href=\"/workshops/1/sessions/1\">Session</a>", @response.body
  411. end
  412. end
  413. protected
  414. def with_restful_routing
  415. with_routing do |set|
  416. set.draw do |map|
  417. map.resources :workshops do |w|
  418. w.resources :sessions
  419. end
  420. end
  421. yield
  422. end
  423. end
  424. end