diff options
Diffstat (limited to 'actionpack/test')
-rw-r--r-- | actionpack/test/abstract_unit.rb | 20 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing_test.rb | 51 | ||||
-rw-r--r-- | actionpack/test/template/active_model_helper_test.rb | 9 | ||||
-rw-r--r-- | actionpack/test/template/asset_tag_helper_test.rb | 26 | ||||
-rw-r--r-- | actionpack/test/template/erb/helper.rb | 11 | ||||
-rw-r--r-- | actionpack/test/template/form_helper_test.rb | 18 | ||||
-rw-r--r-- | actionpack/test/template/form_tag_helper_test.rb | 20 | ||||
-rw-r--r-- | actionpack/test/template/number_helper_test.rb | 9 | ||||
-rw-r--r-- | actionpack/test/template/prototype_helper_test.rb | 25 | ||||
-rw-r--r-- | actionpack/test/template/render_test.rb | 8 | ||||
-rw-r--r-- | actionpack/test/template/scriptaculous_helper_test.rb | 33 | ||||
-rw-r--r-- | actionpack/test/template/url_helper_test.rb | 419 |
12 files changed, 351 insertions, 298 deletions
diff --git a/actionpack/test/abstract_unit.rb b/actionpack/test/abstract_unit.rb index 143491a640..fe78b8ec1f 100644 --- a/actionpack/test/abstract_unit.rb +++ b/actionpack/test/abstract_unit.rb @@ -57,6 +57,18 @@ module RackTestUtils extend self end +module RenderERBUtils + def render_erb(string) + template = ActionView::Template.new( + string.strip, + "test template", + ActionView::Template::Handlers::ERB, + {}) + + template.render(self, {}).strip + end +end + module SetupOnce extend ActiveSupport::Concern @@ -225,6 +237,14 @@ class Rack::TestCase < ActionController::IntegrationTest end end +class ActionController::Base + def self.test_routes(&block) + router = ActionDispatch::Routing::RouteSet.new + router.draw(&block) + include router.url_helpers + end +end + class ::ApplicationController < ActionController::Base end diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index e58653cb8c..19538cb88b 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -58,8 +58,9 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest get 'admin/accounts' => "queenbee#accounts" end - scope 'es' do - resources :projects, :path_names => { :edit => 'cambiar' }, :as => 'projeto' + scope 'pt', :name_prefix => 'pt' do + resources :projects, :path_names => { :edit => 'editar' }, :path => 'projetos' + resource :admin, :path_names => { :new => 'novo' }, :path => 'administrador' end resources :projects, :controller => :project do @@ -74,10 +75,14 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resource :avatar, :controller => :avatar end - resources :images do + resources :images, :as => :funny_images do post :revise, :on => :member end + resource :manager, :as => :super_manager do + post :fire + end + resources :people do nested do scope "/:access_token" do @@ -144,7 +149,7 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end namespace :forum do - resources :products, :as => '' do + resources :products, :path => '' do resources :questions end end @@ -430,15 +435,35 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_project_manager + with_test_routes do + get '/projects/1/manager' + assert_equal 'managers#show', @response.body + assert_equal '/projects/1/manager', project_super_manager_path(:project_id => '1') + + get '/projects/1/manager/new' + assert_equal 'managers#new', @response.body + assert_equal '/projects/1/manager/new', new_project_super_manager_path(:project_id => '1') + + post '/projects/1/manager/fire' + assert_equal 'managers#fire', @response.body + assert_equal '/projects/1/manager/fire', fire_project_super_manager_path(:project_id => '1') + end + end + def test_project_images with_test_routes do get '/projects/1/images' assert_equal 'images#index', @response.body - assert_equal '/projects/1/images', project_images_path(:project_id => '1') + assert_equal '/projects/1/images', project_funny_images_path(:project_id => '1') + + get '/projects/1/images/new' + assert_equal 'images#new', @response.body + assert_equal '/projects/1/images/new', new_project_funny_image_path(:project_id => '1') post '/projects/1/images/1/revise' assert_equal 'images#revise', @response.body - assert_equal '/projects/1/images/1/revise', revise_project_image_path(:project_id => '1', :id => '1') + assert_equal '/projects/1/images/1/revise', revise_project_funny_image_path(:project_id => '1', :id => '1') end end @@ -552,11 +577,21 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest def test_path_names with_test_routes do - get '/es/projeto' + get '/pt/projetos' assert_equal 'projects#index', @response.body + assert_equal '/pt/projetos', pt_projects_path - get '/es/projeto/1/cambiar' + get '/pt/projetos/1/editar' assert_equal 'projects#edit', @response.body + assert_equal '/pt/projetos/1/editar', edit_pt_project_path(1) + + get '/pt/administrador' + assert_equal 'admins#show', @response.body + assert_equal '/pt/administrador', pt_admin_path + + get '/pt/administrador/novo' + assert_equal 'admins#new', @response.body + assert_equal '/pt/administrador/novo', new_pt_admin_path end end diff --git a/actionpack/test/template/active_model_helper_test.rb b/actionpack/test/template/active_model_helper_test.rb index 1a5316a689..47eb620f7a 100644 --- a/actionpack/test/template/active_model_helper_test.rb +++ b/actionpack/test/template/active_model_helper_test.rb @@ -118,13 +118,12 @@ class ActiveModelHelperTest < ActionView::TestCase setup_user @response = ActionController::TestResponse.new + end - @controller = Object.new - def @controller.url_for(options) - options = options.symbolize_keys + def url_for(options) + options = options.symbolize_keys - [options[:action], options[:id].to_param].compact.join('/') - end + [options[:action], options[:id].to_param].compact.join('/') end def test_generic_input_tag diff --git a/actionpack/test/template/asset_tag_helper_test.rb b/actionpack/test/template/asset_tag_helper_test.rb index fbd504ae7d..223a430f92 100644 --- a/actionpack/test/template/asset_tag_helper_test.rb +++ b/actionpack/test/template/asset_tag_helper_test.rb @@ -34,9 +34,7 @@ class AssetTagHelperTest < ActionView::TestCase ) end - @controller = Class.new(BasicController) do - def url_for(*args) "http://www.example.com" end - end.new + @controller = BasicController.new @request = Class.new do def protocol() 'http://' end @@ -49,6 +47,10 @@ class AssetTagHelperTest < ActionView::TestCase ActionView::Helpers::AssetTagHelper::reset_javascript_include_default end + def url_for(*args) + "http://www.example.com" + end + def teardown config.perform_caching = false ENV.delete('RAILS_ASSET_ID') @@ -893,25 +895,19 @@ class AssetTagHelperNonVhostTest < ActionView::TestCase def setup super - @controller = Class.new(BasicController) do - def url_for(options) - "http://www.example.com/collaboration/hieraki" - end - end.new - + @controller = BasicController.new @controller.config.relative_url_root = "/collaboration/hieraki" - @request = Class.new do - def protocol - 'gopher://' - end - end.new - + @request = Struct.new(:protocol).new("gopher://") @controller.request = @request ActionView::Helpers::AssetTagHelper::reset_javascript_include_default end + def url_for(options) + "http://www.example.com/collaboration/hieraki" + end + def test_should_compute_proper_path assert_dom_equal(%(<link href="http://www.example.com/collaboration/hieraki" rel="alternate" title="RSS" type="application/rss+xml" />), auto_discovery_link_tag) assert_dom_equal(%(/collaboration/hieraki/javascripts/xmlhr.js), javascript_path("xmlhr")) diff --git a/actionpack/test/template/erb/helper.rb b/actionpack/test/template/erb/helper.rb index 7147178849..799f9e4036 100644 --- a/actionpack/test/template/erb/helper.rb +++ b/actionpack/test/template/erb/helper.rb @@ -1,20 +1,13 @@ module ERBTest class ViewContext - mock_controller = Class.new do - include SharedTestRoutes.url_helpers - end - + include SharedTestRoutes.url_helpers include ActionView::Helpers::TagHelper include ActionView::Helpers::JavaScriptHelper include ActionView::Helpers::FormHelper - attr_accessor :output_buffer + attr_accessor :output_buffer, :controller def protect_against_forgery?() false end - - define_method(:controller) do - mock_controller.new - end end class BlockTestCase < ActiveSupport::TestCase diff --git a/actionpack/test/template/form_helper_test.rb b/actionpack/test/template/form_helper_test.rb index 7c5ccfd6ed..4af38e52dd 100644 --- a/actionpack/test/template/form_helper_test.rb +++ b/actionpack/test/template/form_helper_test.rb @@ -63,15 +63,15 @@ class FormHelperTest < ActionView::TestCase @post.body = "Back to the hill and over it again!" @post.secret = 1 @post.written_on = Date.new(2004, 6, 15) + end - @controller = Class.new do - attr_reader :url_for_options - def url_for(options) - @url_for_options = options - "http://www.example.com" - end + def url_for(object) + @url_for_options = object + if object.is_a?(Hash) + "http://www.example.com" + else + super end - @controller = @controller.new end def test_label @@ -1348,8 +1348,8 @@ class FormHelperTest < ActionView::TestCase def test_form_for_with_hash_url_option form_for(:post, @post, :url => {:controller => 'controller', :action => 'action'}) do |f| end - assert_equal 'controller', @controller.url_for_options[:controller] - assert_equal 'action', @controller.url_for_options[:action] + assert_equal 'controller', @url_for_options[:controller] + assert_equal 'action', @url_for_options[:action] end def test_form_for_with_record_url_option diff --git a/actionpack/test/template/form_tag_helper_test.rb b/actionpack/test/template/form_tag_helper_test.rb index 868a35c476..3b8760351e 100644 --- a/actionpack/test/template/form_tag_helper_test.rb +++ b/actionpack/test/template/form_tag_helper_test.rb @@ -8,11 +8,15 @@ class FormTagHelperTest < ActionView::TestCase def setup super - @controller = Class.new(BasicController) do - def url_for(options) - "http://www.example.com" - end - end.new + @controller = BasicController.new + end + + def url_for(options) + if options.is_a?(Hash) + "http://www.example.com" + else + super + end end VALID_HTML_ID = /^[A-Za-z][-_:.A-Za-z0-9]*$/ # see http://www.w3.org/TR/html4/types.html#type-name @@ -158,6 +162,12 @@ class FormTagHelperTest < ActionView::TestCase assert_dom_equal expected, actual end + def test_select_tag_with_array_options + assert_deprecated /array/ do + select_tag "people", ["<option>david</option>"] + end + end + def test_text_area_tag_size_string actual = text_area_tag "body", "hello world", "size" => "20x40" expected = %(<textarea cols="20" id="body" name="body" rows="40">hello world</textarea>) diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 50c57a5588..a21a1a68e4 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -102,6 +102,9 @@ class NumberHelperTest < ActionView::TestCase assert_equal("3268", number_with_precision((32.6751 * 100.00), :precision => 0)) assert_equal("112", number_with_precision(111.50, :precision => 0)) assert_equal("1234567892", number_with_precision(1234567891.50, :precision => 0)) + assert_equal("0", number_with_precision(0, :precision => 0)) + assert_equal("0.00100", number_with_precision(0.001, :precision => 5)) + assert_equal("0.001", number_with_precision(0.00111, :precision => 3)) end def test_number_with_precision_with_custom_delimiter_and_separator @@ -122,11 +125,17 @@ class NumberHelperTest < ActionView::TestCase assert_equal "53", number_with_precision(52.7923, :precision => 2, :significant => true ) assert_equal "9775.00", number_with_precision(9775, :precision => 6, :significant => true ) assert_equal "5.392900", number_with_precision(5.3929, :precision => 7, :significant => true ) + assert_equal "0.0", number_with_precision(0, :precision => 2, :significant => true ) + assert_equal "0", number_with_precision(0, :precision => 1, :significant => true ) + assert_equal "0.0001", number_with_precision(0.0001, :precision => 1, :significant => true ) + assert_equal "0.000100", number_with_precision(0.0001, :precision => 3, :significant => true ) + assert_equal "0.0001", number_with_precision(0.0001111, :precision => 1, :significant => true ) end def test_number_with_precision_with_strip_insignificant_zeros assert_equal "9775.43", number_with_precision(9775.43, :precision => 4, :strip_insignificant_zeros => true ) assert_equal "9775.2", number_with_precision(9775.2, :precision => 6, :significant => true, :strip_insignificant_zeros => true ) + assert_equal "0", number_with_precision(0, :precision => 6, :significant => true, :strip_insignificant_zeros => true ) end def test_number_with_precision_with_significant_true_and_zero_precision diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index 619293dc43..0ff37f44c2 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -47,19 +47,18 @@ class PrototypeHelperBaseTest < ActionView::TestCase def setup super @template = self - @controller = Class.new do - def url_for(options) - if options.is_a?(String) - options - else - url = "http://www.example.com/" - url << options[:action].to_s if options and options[:action] - url << "?a=#{options[:a]}" if options && options[:a] - url << "&b=#{options[:b]}" if options && options[:a] && options[:b] - url - end - end - end.new + end + + def url_for(options) + if options.is_a?(String) + options + else + url = "http://www.example.com/" + url << options[:action].to_s if options and options[:action] + url << "?a=#{options[:a]}" if options && options[:a] + url << "&b=#{options[:b]}" if options && options[:a] && options[:b] + url + end end protected diff --git a/actionpack/test/template/render_test.rb b/actionpack/test/template/render_test.rb index e54ebfbf8d..5f33c933db 100644 --- a/actionpack/test/template/render_test.rb +++ b/actionpack/test/template/render_test.rb @@ -281,6 +281,10 @@ class CachedViewRenderTest < ActiveSupport::TestCase assert_equal ActionView::FileSystemResolver, view_paths.first.class setup_view(view_paths) end + + def teardown + GC.start + end end class LazyViewRenderTest < ActiveSupport::TestCase @@ -294,4 +298,8 @@ class LazyViewRenderTest < ActiveSupport::TestCase assert_equal ActionView::FileSystemResolver.new(FIXTURE_LOAD_PATH), view_paths.first setup_view(view_paths) end + + def teardown + GC.start + end end diff --git a/actionpack/test/template/scriptaculous_helper_test.rb b/actionpack/test/template/scriptaculous_helper_test.rb index bebc3cb9f4..233012bfdd 100644 --- a/actionpack/test/template/scriptaculous_helper_test.rb +++ b/actionpack/test/template/scriptaculous_helper_test.rb @@ -3,21 +3,16 @@ require 'abstract_unit' class ScriptaculousHelperTest < ActionView::TestCase tests ActionView::Helpers::ScriptaculousHelper - def setup - super - @controller = Class.new do - def url_for(options) - url = "http://www.example.com/" - url << options[:action].to_s if options and options[:action] - url - end - end.new + def url_for(options) + url = "http://www.example.com/" + url << options[:action].to_s if options and options[:action] + url end - + def test_effect assert_equal "new Effect.Highlight(\"posts\",{});", visual_effect(:highlight, "posts") assert_equal "new Effect.Highlight(\"posts\",{});", visual_effect("highlight", :posts) - assert_equal "new Effect.Highlight(\"posts\",{});", visual_effect(:highlight, :posts) + assert_equal "new Effect.Highlight(\"posts\",{});", visual_effect(:highlight, :posts) assert_equal "new Effect.Fade(\"fademe\",{duration:4.0});", visual_effect(:fade, "fademe", :duration => 4.0) assert_equal "new Effect.Shake(element,{});", visual_effect(:shake) assert_equal "new Effect.DropOut(\"dropme\",{queue:'end'});", visual_effect(:drop_out, 'dropme', :queue => :end) @@ -43,7 +38,7 @@ class ScriptaculousHelperTest < ActionView::TestCase assert ve[2].include?("scope:'test'") assert ve[2].include?("position:'end'") end - + def test_toggle_effects assert_equal "Effect.toggle(\"posts\",'appear',{});", visual_effect(:toggle_appear, "posts") assert_equal "Effect.toggle(\"posts\",'slide',{});", visual_effect(:toggle_slide, "posts") @@ -52,26 +47,26 @@ class ScriptaculousHelperTest < ActionView::TestCase assert_equal "Effect.toggle(\"posts\",'slide',{});", visual_effect("toggle_slide", "posts") assert_equal "Effect.toggle(\"posts\",'blind',{});", visual_effect("toggle_blind", "posts") end - + def test_sortable_element - assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>), + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>), sortable_element("mylist", :url => { :action => "order" }) - assert_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}, tag:'div'})\n//]]>\n</script>), + assert_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}, tag:'div'})\n//]]>\n</script>), sortable_element("mylist", :tag => "div", :constraint => "horizontal", :url => { :action => "order" }) - assert_dom_equal %|<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:['list1','list2'], onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>|, + assert_dom_equal %|<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:['list1','list2'], onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>|, sortable_element("mylist", :containment => ['list1','list2'], :constraint => "horizontal", :url => { :action => "order" }) - assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:'list1', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>), + assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nSortable.create(\"mylist\", {constraint:'horizontal', containment:'list1', onUpdate:function(){new Ajax.Request('http://www.example.com/order', {asynchronous:true, evalScripts:true, parameters:Sortable.serialize(\"mylist\")})}})\n//]]>\n</script>), sortable_element("mylist", :containment => 'list1', :constraint => "horizontal", :url => { :action => "order" }) end - + def test_draggable_element assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Draggable(\"product_13\", {})\n//]]>\n</script>), draggable_element("product_13") assert_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nnew Draggable(\"product_13\", {revert:true})\n//]]>\n</script>), draggable_element("product_13", :revert => true) end - + def test_drop_receiving_element assert_dom_equal %(<script type=\"text/javascript\">\n//<![CDATA[\nDroppables.add(\"droptarget1\", {onDrop:function(element){new Ajax.Request('http://www.example.com/', {asynchronous:true, evalScripts:true, parameters:'id=' + encodeURIComponent(element.id)})}})\n//]]>\n</script>), drop_receiving_element("droptarget1") diff --git a/actionpack/test/template/url_helper_test.rb b/actionpack/test/template/url_helper_test.rb index 87b2e59255..35e73fbf1e 100644 --- a/actionpack/test/template/url_helper_test.rb +++ b/actionpack/test/template/url_helper_test.rb @@ -3,58 +3,73 @@ require 'abstract_unit' require 'active_support/ordered_options' require 'controller/fake_controllers' -class UrlHelperTest < ActionView::TestCase - - def setup - super - @controller = Class.new(BasicController) do - attr_accessor :url - def url_for(options) - url - end - end +class UrlHelperTest < ActiveSupport::TestCase + + # In a few cases, the helper proxies to 'controller' + # or request. + # + # In those cases, we'll set up a simple mock + attr_accessor :controller, :request + + routes = ActionDispatch::Routing::RouteSet.new + routes.draw do + match "/" => "foo#bar" + match "/other" => "foo#other" + end + + include routes.url_helpers + + include ActionView::Helpers::UrlHelper + include ActionDispatch::Assertions::DomAssertions + include ActionView::Context + include RenderERBUtils + + # self.default_url_options = {:host => "www.example.com"} + + # TODO: This shouldn't be needed (see template.rb:53) + def assigns + {} + end - @controller = @controller.new - @request = @controller.request = ActionDispatch::TestRequest.new - @controller.url = "http://www.example.com" + def abcd(hash = {}) + hash_for(:a => :b, :c => :d).merge(hash) end + def hash_for(opts = {}) + {:controller => "foo", :action => "bar"}.merge(opts) + end + alias url_hash hash_for + def test_url_for_escapes_urls - @controller.url = "http://www.example.com?a=b&c=d" - assert_equal "http://www.example.com?a=b&c=d", url_for(:a => 'b', :c => 'd') - assert_equal "http://www.example.com?a=b&c=d", url_for(:a => 'b', :c => 'd', :escape => true) - assert_equal "http://www.example.com?a=b&c=d", url_for(:a => 'b', :c => 'd', :escape => false) + assert_equal "/?a=b&c=d", url_for(abcd) + assert_equal "/?a=b&c=d", url_for(abcd(:escape => true)) + assert_equal "/?a=b&c=d", url_for(abcd(:escape => false)) end def test_url_for_escaping_is_safety_aware - assert url_for(:a => 'b', :c => 'd', :escape => true).html_safe?, "escaped urls should be html_safe?" - assert !url_for(:a => 'b', :c => 'd', :escape => false).html_safe?, "non-escaped urls shouldn't be safe" + assert url_for(abcd(:escape => true)).html_safe?, "escaped urls should be html_safe?" + assert !url_for(abcd(:escape => false)).html_safe?, "non-escaped urls should not be html_safe?" end def test_url_for_escapes_url_once - @controller.url = "http://www.example.com?a=b&c=d" - assert_equal "http://www.example.com?a=b&c=d", url_for("http://www.example.com?a=b&c=d") + assert_equal "/?a=b&c=d", url_for("/?a=b&c=d") end def test_url_for_with_back - @request.env['HTTP_REFERER'] = 'http://www.example.com/referer' + referer = 'http://www.example.com/referer' + @controller = Struct.new(:request).new(Struct.new(:env).new({"HTTP_REFERER" => referer})) + assert_equal 'http://www.example.com/referer', url_for(:back) end def test_url_for_with_back_and_no_referer - @request.env['HOST_NAME'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' + @controller = Struct.new(:request).new(Struct.new(:env).new({})) assert_equal 'javascript:history.back()', url_for(:back) end def test_url_for_from_hash_doesnt_escape_ampersand - @controller = TestController.new - @view = ActionView::Base.new - @view.controller = @controller - - path = @view.url_for(:controller => :cheeses, :foo => :bar, :baz => :quux) - - assert_equal '/cheeses?baz=quux&foo=bar', sort_query_string_params(path) + path = url_for(hash_for(:foo => :bar, :baz => :quux)) + assert_equal '/?baz=quux&foo=bar', sort_query_string_params(path) end # todo: missing test cases @@ -118,65 +133,54 @@ class UrlHelperTest < ActionView::TestCase end def test_link_tag_without_host_option - ActionController::Base.class_eval { attr_accessor :url } - url = {:controller => 'weblog', :action => 'show'} - @controller = ActionController::Base.new - @controller.request = ActionController::TestRequest.new - assert_dom_equal(%q{<a href="/weblog/show">Test Link</a>}, link_to('Test Link', url)) + assert_dom_equal(%q{<a href="/">Test Link</a>}, link_to('Test Link', url_hash)) end def test_link_tag_with_host_option - ActionController::Base.class_eval { attr_accessor :url } - url = {:controller => 'weblog', :action => 'show', :host => 'www.example.com'} - @controller = ActionController::Base.new - @controller.request = ActionController::TestRequest.new - assert_dom_equal(%q{<a href="http://www.example.com/weblog/show">Test Link</a>}, link_to('Test Link', url)) + hash = hash_for(:host => "www.example.com") + expected = %q{<a href="http://www.example.com/">Test Link</a>} + assert_dom_equal(expected, link_to('Test Link', hash)) end def test_link_tag_with_query - assert_dom_equal "<a href=\"http://www.example.com?q1=v1&q2=v2\">Hello</a>", link_to("Hello", "http://www.example.com?q1=v1&q2=v2") + expected = %{<a href="http://www.example.com?q1=v1&q2=v2">Hello</a>} + assert_dom_equal expected, link_to("Hello", "http://www.example.com?q1=v1&q2=v2") end def test_link_tag_with_query_and_no_name - assert_dom_equal "<a href=\"http://www.example.com?q1=v1&q2=v2\">http://www.example.com?q1=v1&q2=v2</a>", link_to(nil, "http://www.example.com?q1=v1&q2=v2") + link = link_to(nil, "http://www.example.com?q1=v1&q2=v2") + expected = %{<a href="http://www.example.com?q1=v1&q2=v2">http://www.example.com?q1=v1&q2=v2</a>} + assert_dom_equal expected, link end def test_link_tag_with_back - @request.env['HOST_NAME'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - @request.env['HTTP_REFERER'] = 'http://www.example.com/referer' - assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back) + env = {"HTTP_REFERER" => "http://www.example.com/referer"} + @controller = Struct.new(:request).new(Struct.new(:env).new(env)) + expected = %{<a href="#{env["HTTP_REFERER"]}">go back</a>} + assert_dom_equal expected, link_to('go back', :back) end def test_link_tag_with_back_and_no_referer - @request.env['HOST_NAME'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back) - end - - def test_link_tag_with_back - @request.env['HOST_NAME'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - @request.env['HTTP_REFERER'] = 'http://www.example.com/referer' - assert_dom_equal "<a href=\"http://www.example.com/referer\">go back</a>", link_to('go back', :back) - end - - def test_link_tag_with_back_and_no_referer - @request.env['HOST_NAME'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - assert_dom_equal "<a href=\"javascript:history.back()\">go back</a>", link_to('go back', :back) + @controller = Struct.new(:request).new(Struct.new(:env).new({})) + link = link_to('go back', :back) + assert_dom_equal %{<a href="javascript:history.back()">go back</a>}, link end def test_link_tag_with_img - assert_dom_equal "<a href=\"http://www.example.com\"><img src='/favicon.jpg' alt=\"Favicon\" /></a>", link_to(image_tag("/favicon.jpg"), "http://www.example.com") + link = link_to("<img src='/favicon.jpg' />".html_safe, "/") + expected = %{<a href="/"><img src='/favicon.jpg' /></a>} + assert_dom_equal expected, link end def test_link_with_nil_html_options - assert_dom_equal "<a href=\"http://www.example.com\">Hello</a>", link_to("Hello", {:action => 'myaction'}, nil) + link = link_to("Hello", url_hash, nil) + assert_dom_equal %{<a href="/">Hello</a>}, link end def test_link_tag_with_custom_onclick - assert_dom_equal "<a href=\"http://www.example.com\" onclick=\"alert('yay!')\">Hello</a>", link_to("Hello", "http://www.example.com", :onclick => "alert('yay!')") + link = link_to("Hello", "http://www.example.com", :onclick => "alert('yay!')") + expected = %{<a href="http://www.example.com" onclick="alert('yay!')">Hello</a>} + assert_dom_equal expected, link end def test_link_tag_with_javascript_confirm @@ -238,91 +242,106 @@ class UrlHelperTest < ActionView::TestCase end def test_link_tag_using_block_in_erb - output_buffer = link_to("http://example.com") { concat("Example site") } - assert_equal '<a href="http://example.com">Example site</a>', output_buffer + out = render_erb %{<%= link_to('/') do %>Example site<% end %>} + assert_equal '<a href="/">Example site</a>', out end def test_link_to_unless - assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog") - assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_unless(false, "Listing", :action => "list", :controller => "weblog") - assert_equal "Showing", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) - assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name, options, html_options| - "<strong>#{name}</strong>" - } - assert_equal "<strong>Showing</strong>", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { |name| - "<strong>#{name}</strong>" - } - assert_equal "test", link_to_unless(true, "Showing", :action => "show", :controller => "weblog", :id => 1) { - "test" - } + assert_equal "Showing", link_to_unless(true, "Showing", url_hash) + + assert_dom_equal %{<a href="/">Listing</a>}, + link_to_unless(false, "Listing", url_hash) + + assert_equal "Showing", link_to_unless(true, "Showing", url_hash) + + assert_equal "<strong>Showing</strong>", + link_to_unless(true, "Showing", url_hash) { |name| + "<strong>#{name}</strong>" + } + + assert_equal "<strong>Showing</strong>", + link_to_unless(true, "Showing", url_hash) { |name| + "<strong>#{name}</strong>" + } + + assert_equal "test", + link_to_unless(true, "Showing", url_hash) { + "test" + } end def test_link_to_if - assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog") - assert_dom_equal "<a href=\"http://www.example.com\">Listing</a>", link_to_if(true, "Listing", :action => "list", :controller => "weblog") - assert_equal "Showing", link_to_if(false, "Showing", :action => "show", :controller => "weblog", :id => 1) + assert_equal "Showing", link_to_if(false, "Showing", url_hash) + assert_dom_equal %{<a href="/">Listing</a>}, link_to_if(true, "Listing", url_hash) + assert_equal "Showing", link_to_if(false, "Showing", url_hash) + end + + def request_for_url(url) + env = Rack::MockRequest.env_for("http://www.example.com#{url}") + ActionDispatch::Request.new(env) end def test_current_page_with_simple_url - @request.env['HTTP_HOST'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - @controller.url = "http://www.example.com/weblog/show" - assert current_page?({ :action => "show", :controller => "weblog" }) - assert current_page?("http://www.example.com/weblog/show") + @request = request_for_url("/") + assert current_page?(url_hash) + assert current_page?("http://www.example.com/") end def test_current_page_ignoring_params - @request.env['HTTP_HOST'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - @request.env['QUERY_STRING'] = 'order=desc&page=1' - @controller.url = "http://www.example.com/weblog/show?order=desc&page=1" - assert current_page?({ :action => "show", :controller => "weblog" }) - assert current_page?("http://www.example.com/weblog/show") + @request = request_for_url("/?order=desc&page=1") + + assert current_page?(url_hash) + assert current_page?("http://www.example.com/") end def test_current_page_with_params_that_match - @request.env['HTTP_HOST'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - @request.env['QUERY_STRING'] = 'order=desc&page=1' - @controller.url = "http://www.example.com/weblog/show?order=desc&page=1" - assert current_page?({ :action => "show", :controller => "weblog", :order => "desc", :page => "1" }) - assert current_page?("http://www.example.com/weblog/show?order=desc&page=1") + @request = request_for_url("/?order=desc&page=1") + + assert current_page?(hash_for(:order => "desc", :page => "1")) + assert current_page?("http://www.example.com/?order=desc&page=1") end def test_link_unless_current - @request.env['HTTP_HOST'] = 'www.example.com' - @request.env['PATH_INFO'] = '/weblog/show' - @controller.url = "http://www.example.com/weblog/show" - assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) - assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show") - - @request.env['QUERY_STRING'] = 'order=desc' - @controller.url = "http://www.example.com/weblog/show" - assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) - assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show") - - @request.env['QUERY_STRING'] = 'order=desc&page=1' - @controller.url = "http://www.example.com/weblog/show?order=desc&page=1" - assert_equal "Showing", link_to_unless_current("Showing", { :action => "show", :controller => "weblog", :order=>'desc', :page=>'1' }) - assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1") - assert_equal "Showing", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=1") - - @request.env['QUERY_STRING'] = 'order=desc' - @controller.url = "http://www.example.com/weblog/show?order=asc" - assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) - assert_equal "<a href=\"http://www.example.com/weblog/show?order=asc\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=asc") - - @request.env['QUERY_STRING'] = 'order=desc&page=1' - @controller.url = "http://www.example.com/weblog/show?order=desc&page=2" - assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&page=2\">Showing</a>", link_to_unless_current("Showing", { :action => "show", :controller => "weblog" }) - assert_equal "<a href=\"http://www.example.com/weblog/show?order=desc&page=2\">Showing</a>", link_to_unless_current("Showing", "http://www.example.com/weblog/show?order=desc&page=2") - - @request.env['QUERY_STRING'] = '' - @controller.url = "http://www.example.com/weblog/list" - assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>", - link_to_unless_current("Listing", :action => "list", :controller => "weblog") - assert_equal "<a href=\"http://www.example.com/weblog/list\">Listing</a>", - link_to_unless_current("Listing", "http://www.example.com/weblog/list") + @request = request_for_url("/") + + assert_equal "Showing", + link_to_unless_current("Showing", url_hash) + assert_equal "Showing", + link_to_unless_current("Showing", "http://www.example.com/") + + @request = request_for_url("/?order=desc") + + assert_equal "Showing", + link_to_unless_current("Showing", url_hash) + assert_equal "Showing", + link_to_unless_current("Showing", "http://www.example.com/") + + @request = request_for_url("/?order=desc&page=1") + + assert_equal "Showing", + link_to_unless_current("Showing", hash_for(:order=>'desc', :page=>'1')) + assert_equal "Showing", + link_to_unless_current("Showing", "http://www.example.com/?order=desc&page=1") + + @request = request_for_url("/?order=desc") + + assert_equal %{<a href="/?order=asc">Showing</a>}, + link_to_unless_current("Showing", hash_for(:order => :asc)) + assert_equal %{<a href="http://www.example.com/?order=asc">Showing</a>}, + link_to_unless_current("Showing", "http://www.example.com/?order=asc") + + @request = request_for_url("/?order=desc") + assert_equal %{<a href="/?order=desc&page=2\">Showing</a>}, + link_to_unless_current("Showing", hash_for(:order => "desc", :page => 2)) + assert_equal %{<a href="http://www.example.com/?order=desc&page=2">Showing</a>}, + link_to_unless_current("Showing", "http://www.example.com/?order=desc&page=2") + + @request = request_for_url("/show") + + assert_equal %{<a href="/">Listing</a>}, + link_to_unless_current("Listing", url_hash) + assert_equal %{<a href="http://www.example.com/">Listing</a>}, + link_to_unless_current("Listing", "http://www.example.com/") end def test_mail_to @@ -352,7 +371,8 @@ class UrlHelperTest < ActionView::TestCase end def test_mail_to_with_img - assert_dom_equal %(<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>), mail_to('feedback@example.com', '<img src="/feedback.png" />'.html_safe) + assert_dom_equal %(<a href="mailto:feedback@example.com"><img src="/feedback.png" /></a>), + mail_to('feedback@example.com', '<img src="/feedback.png" />'.html_safe) end def test_mail_to_with_hex @@ -369,6 +389,7 @@ class UrlHelperTest < ActionView::TestCase assert_dom_equal "<script type=\"text/javascript\">eval(decodeURIComponent('%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%6d%65%28%61%74%29%64%6f%6d%61%69%6e%28%64%6f%74%29%63%6f%6d%3c%2f%61%3e%27%29%3b'))</script>", mail_to("me@domain.com", nil, :encode => "javascript", :replace_at => "(at)", :replace_dot => "(dot)") end + # TODO: button_to looks at this ... why? def protect_against_forgery? false end @@ -383,6 +404,15 @@ end class UrlHelperControllerTest < ActionController::TestCase class UrlHelperController < ActionController::Base + test_routes do |map| + match 'url_helper_controller_test/url_helper/show_named_route', + :to => 'url_helper_controller_test/url_helper#show_named_route', + :as => :show_named_route + + map.connect ":controller/:action/:id" + # match "/:controller(/:action(/:id))" + end + def show_url_for render :inline => "<%= url_for :controller => 'url_helper_controller_test/url_helper', :action => 'show_url_for' %>" end @@ -406,17 +436,14 @@ class UrlHelperControllerTest < ActionController::TestCase end def test_named_route_url_shows_host_and_path - with_url_helper_routing do - get :show_named_route, :kind => 'url' - assert_equal 'http://test.host/url_helper_controller_test/url_helper/show_named_route', @response.body - end + get :show_named_route, :kind => 'url' + assert_equal 'http://test.host/url_helper_controller_test/url_helper/show_named_route', + @response.body end def test_named_route_path_shows_only_path - with_url_helper_routing do - get :show_named_route, :kind => 'path' - assert_equal '/url_helper_controller_test/url_helper/show_named_route', @response.body - end + get :show_named_route, :kind => 'path' + assert_equal '/url_helper_controller_test/url_helper/show_named_route', @response.body end def test_url_for_nil_returns_current_path @@ -431,24 +458,16 @@ class UrlHelperControllerTest < ActionController::TestCase end end - with_url_helper_routing do - get :show_named_route, :kind => 'url' - assert_equal 'http://testtwo.host/url_helper_controller_test/url_helper/show_named_route', @response.body - end + get :show_named_route, :kind => 'url' + assert_equal 'http://testtwo.host/url_helper_controller_test/url_helper/show_named_route', @response.body end - - protected - def with_url_helper_routing - with_routing do |set| - set.draw do |map| - match 'url_helper_controller_test/url_helper/show_named_route', :to => 'url_helper_controller_test/url_helper#show_named_route', :as => :show_named_route - end - yield - end - end end class TasksController < ActionController::Base + test_routes do + resources :tasks + end + def index render_default end @@ -468,36 +487,19 @@ class TasksController < ActionController::Base end class LinkToUnlessCurrentWithControllerTest < ActionController::TestCase - def setup - super - @controller = TasksController.new - end + tests TasksController def test_link_to_unless_current_to_current - with_restful_routing do - get :index - assert_equal "tasks\ntasks", @response.body - end + get :index + assert_equal "tasks\ntasks", @response.body end def test_link_to_unless_current_shows_link - with_restful_routing do - get :show, :id => 1 - assert_equal "<a href=\"/tasks\">tasks</a>\n" + - "<a href=\"#{@request.protocol}#{@request.host_with_port}/tasks\">tasks</a>", - @response.body - end + get :show, :id => 1 + assert_equal "<a href=\"/tasks\">tasks</a>\n" + + "<a href=\"#{@request.protocol}#{@request.host_with_port}/tasks\">tasks</a>", + @response.body end - - protected - def with_restful_routing - with_routing do |set| - set.draw do |map| - resources :tasks - end - yield - end - end end class Workshop @@ -537,6 +539,12 @@ class Session end class WorkshopsController < ActionController::Base + test_routes do + resources :workshops do + resources :sessions + end + end + def index @workshop = Workshop.new(nil) render :inline => "<%= url_for(@workshop) %>\n<%= link_to('Workshop', @workshop) %>" @@ -551,6 +559,12 @@ class WorkshopsController < ActionController::Base end class SessionsController < ActionController::Base + test_routes do + resources :workshops do + resources :sessions + end + end + def index @workshop = Workshop.new(params[:workshop_id]) @session = Session.new(nil) @@ -567,56 +581,31 @@ class SessionsController < ActionController::Base end class PolymorphicControllerTest < ActionController::TestCase - def setup - super - @response = ActionController::TestResponse.new - end - def test_new_resource @controller = WorkshopsController.new - with_restful_routing do - get :index - assert_equal "/workshops\n<a href=\"/workshops\">Workshop</a>", @response.body - end + get :index + assert_equal "/workshops\n<a href=\"/workshops\">Workshop</a>", @response.body end def test_existing_resource @controller = WorkshopsController.new - with_restful_routing do - get :show, :id => 1 - assert_equal "/workshops/1\n<a href=\"/workshops/1\">Workshop</a>", @response.body - end + get :show, :id => 1 + assert_equal "/workshops/1\n<a href=\"/workshops/1\">Workshop</a>", @response.body end def test_new_nested_resource @controller = SessionsController.new - with_restful_routing do - get :index, :workshop_id => 1 - assert_equal "/workshops/1/sessions\n<a href=\"/workshops/1/sessions\">Session</a>", @response.body - end + get :index, :workshop_id => 1 + assert_equal "/workshops/1/sessions\n<a href=\"/workshops/1/sessions\">Session</a>", @response.body end - + def test_existing_nested_resource @controller = SessionsController.new - - with_restful_routing do - get :show, :workshop_id => 1, :id => 1 - assert_equal "/workshops/1/sessions/1\n<a href=\"/workshops/1/sessions/1\">Session</a>", @response.body - end + + get :show, :workshop_id => 1, :id => 1 + assert_equal "/workshops/1/sessions/1\n<a href=\"/workshops/1/sessions/1\">Session</a>", @response.body end - - protected - def with_restful_routing - with_routing do |set| - set.draw do |map| - resources :workshops do - resources :sessions - end - end - yield - end - end end |