aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/test/controller/resources_test.rb
blob: 96b643156774493fb22b9f6f265a67917433072a (plain) (tree)
1
2
3
4
5
6
7
8
9


                                                    
                                         



                                                 
                                         



                                          













                                                                                      

                                     
                                         


       






                                                

                                                                           
                                                                                                         


       






                                                                                                         

                                                                      

                                      
                                                      




                                                                    






                                                                       

                                                      
                                                        




                                                                                               




           


















                                                                                                     

                                                                    

                                               
                                                      




                                                                                                  























                                                                                                                
                                   
                           
                                               


             






                                                                                                                                     






















                                                                                                                                       

























                                                                                                                                            

                                             




                                                                                   







                                                                                                                                                          
   
require File.dirname(__FILE__) + '/../abstract_unit'

class MessagesController < ActionController::Base
  def index() render :nothing => true end
  def rescue_action(e) raise e end
end

class CommentsController < ActionController::Base
  def index() render :nothing => true end
  def rescue_action(e) raise e end
end

class ResourcesTest < Test::Unit::TestCase
  def test_should_arrange_actions
    resource = ActionController::Resources::Resource.new(:messages, 
      :collection => { :rss => :get, :reorder => :post, :csv => :post },
      :member     => { :rss => :get, :atom => :get, :upload => :post, :fix => :post },
      :new        => { :preview => :get, :draft => :get })

    assert_resource_methods [:rss],                    resource, :collection, :get
    assert_resource_methods [:create, :csv, :reorder], resource, :collection, :post
    assert_resource_methods [:edit, :rss, :atom],      resource, :member,     :get
    assert_resource_methods [:upload, :fix],           resource, :member,     :post
    assert_resource_methods [:update],                 resource, :member,     :put
    assert_resource_methods [:new, :preview, :draft],  resource, :new,        :get
  end
  
  def test_default_restful_routes
    with_restful_routing :messages do
      assert_simply_restful_for :messages
    end
  end

  def test_multiple_default_restful_routes
    with_restful_routing :messages, :comments do
      assert_simply_restful_for :messages
      assert_simply_restful_for :comments
    end
  end

  def test_with_path_prefix
    with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do
      assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
    end
  end

  def test_multile_with_path_prefix
    with_restful_routing :messages, :comments, :path_prefix => '/thread/:thread_id' do
      assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
      assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
    end
  end

  def test_with_collection_action
    with_restful_routing :messages, :collection => { :rss => :get } do
      rss_options = {:action => 'rss'}
      rss_path    = "/messages;rss"
      assert_restful_routes_for :messages do |options|
        assert_routing rss_path, options.merge(rss_options)
      end
      
      assert_restful_named_routes_for :messages do |options|
        assert_named_route rss_path, :rss_messages_path, rss_options
      end
    end
  end

  def test_with_member_action
    [:put, :post].each do |method|
      with_restful_routing :messages, :member => { :mark => method } do
        mark_options = {:action => 'mark', :id => '1'}
        mark_path    = "/messages/1;mark"
        assert_restful_routes_for :messages do |options|
          assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method)
        end
        
        assert_restful_named_routes_for :messages do |options|
          assert_named_route mark_path, :mark_message_path, mark_options
        end
      end
    end
  end

  def test_with_two_member_actions_with_same_method
    [:put, :post].each do |method|
      with_restful_routing :messages, :member => { :mark => method, :unmark => method } do
        %w(mark unmark).each do |action|
          action_options = {:action => action, :id => '1'}
          action_path    = "/messages/1;#{action}"
          assert_restful_routes_for :messages do |options|
            assert_recognizes(options.merge(action_options), :path => action_path, :method => method)
          end
        
          assert_restful_named_routes_for :messages do |options|
            assert_named_route action_path, "#{action}_message_path".to_sym, action_options
          end
        end
      end
    end
  end


  def test_with_new_action
    with_restful_routing :messages, :new => { :preview => :post } do
      preview_options = {:action => 'preview'}
      preview_path    = "/messages/new;preview"
      assert_restful_routes_for :messages do |options|
        assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post)
      end
      
      assert_restful_named_routes_for :messages do |options|
        assert_named_route preview_path, :preview_new_message_path, preview_options
      end
    end
  end

  def xtest_nested_restful_routes
    with_routing do |set|
      set.draw do |map| 
        map.resources(:messages) do
          map.resources(:comments)
        end
      end

      with_options({ :controller => :comments }) do |controller|
        controller.assert_routing "/messages/1/comments",        :action => 'index'
        controller.assert_routing "/messages/1/comments.xml" ,   :action => 'index', :format => 'xml'
        controller.assert_routing "/messages/1/comments/new",    :action => 'new'
        controller.assert_routing "/messages/1/comments/1",      :action => 'show', :id => '1'
        controller.assert_routing "/messages/1/comments/1;edit", :action => 'edit', :id => '1'
        controller.assert_routing "/messages/1/comments/1.xml",  :action => 'show', :id => '1', :format => 'xml'
      end
    end
  end

  protected
    def with_restful_routing(*args)
      with_routing do |set|
        set.draw { |map| map.resources(*args) }
        yield
      end
    end

    # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block.
    def assert_simply_restful_for(controller_name, options = {})
      assert_restful_routes_for       controller_name, options
      assert_restful_named_routes_for controller_name, options
    end

    def assert_restful_routes_for(controller_name, options = {})
      (options[:options] ||= {})[:controller] = controller_name.to_s

      with_options(options[:options]) do |controller|
        controller.assert_routing "/#{options[:path_prefix]}#{controller_name}",        :action => 'index'
        controller.assert_routing "/#{options[:path_prefix]}#{controller_name}.xml" ,   :action => 'index', :format => 'xml'
        controller.assert_routing "/#{options[:path_prefix]}#{controller_name}/new",    :action => 'new'
        controller.assert_routing "/#{options[:path_prefix]}#{controller_name}/1",      :action => 'show', :id => '1'
        controller.assert_routing "/#{options[:path_prefix]}#{controller_name}/1;edit", :action => 'edit', :id => '1'
        controller.assert_routing "/#{options[:path_prefix]}#{controller_name}/1.xml",  :action => 'show', :id => '1', :format => 'xml'
      end

      assert_recognizes(
        options[:options].merge(:action => 'create'),
        {:path => "/#{options[:path_prefix]}#{controller_name}", :method => :post})

      assert_recognizes(
        options[:options].merge(:action => 'update', :id => '1'),
        {:path => "/#{options[:path_prefix]}#{controller_name}/1", :method => :put})

      assert_recognizes(
        options[:options].merge(:action => 'destroy', :id => '1'),
        {:path => "/#{options[:path_prefix]}#{controller_name}/1", :method => :delete})

      yield options[:options] if block_given?
    end
    
    # test named routes like foo_path and foos_path map to the correct options.
    def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {})
      if singular_name.is_a?(Hash)
        options       = singular_name
        singular_name = nil
      end
      singular_name ||= controller_name.to_s.singularize
      (options[:options] ||= {})[:controller] = controller_name.to_s
      @controller = "#{controller_name.to_s.camelize}Controller".constantize.new
      @request    = ActionController::TestRequest.new
      @response   = ActionController::TestResponse.new
      get :index, options[:options]
      options[:options].delete :action

      full_prefix = "/#{options[:path_prefix]}#{controller_name}"

      assert_named_route "#{full_prefix}",        "#{controller_name}_path",           options[:options]
      assert_named_route "#{full_prefix}.xml",    "formatted_#{controller_name}_path", options[:options].merge(:format => 'xml')
      assert_named_route "#{full_prefix}/new",    "new_#{singular_name}_path",         options[:options]
      assert_named_route "#{full_prefix}/1",      "#{singular_name}_path",             options[:options].merge(:id => '1')
      assert_named_route "#{full_prefix}/1;edit", "edit_#{singular_name}_path",        options[:options].merge(:id => '1')
      assert_named_route "#{full_prefix}/1.xml",  "formatted_#{singular_name}_path",   options[:options].merge(:format => 'xml', :id => '1')
      yield options[:options] if block_given?
    end

    def assert_named_route(expected, route, options)
      actual =  @controller.send(route, options) rescue $!.class.name
      assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})"
    end

    def assert_resource_methods(expected, resource, action_method, method)
      assert_equal expected.length, resource.send("#{action_method}_methods")[method].size, "#{resource.send("#{action_method}_methods")[method].inspect}"
      expected.each do |action|
        assert resource.send("#{action_method}_methods")[method].include?(action), 
          "#{method} not in #{action_method} methods: #{resource.send("#{action_method}_methods")[method].inspect}"
      end
    end
end