diff options
Diffstat (limited to 'actionpack/test/abstract_controller')
15 files changed, 733 insertions, 0 deletions
diff --git a/actionpack/test/abstract_controller/abstract_controller_test.rb b/actionpack/test/abstract_controller/abstract_controller_test.rb new file mode 100644 index 0000000000..8763ded57e --- /dev/null +++ b/actionpack/test/abstract_controller/abstract_controller_test.rb @@ -0,0 +1,226 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module AbstractController + module Testing + + # Test basic dispatching. + # ==== + # * Call process + # * Test that the response_body is set correctly + class SimpleController < AbstractController::Base + end + + class Me < SimpleController + def index + self.response_body = "Hello world" + "Something else" + end + end + + class TestBasic < ActiveSupport::TestCase + test "dispatching works" do + result = Me.process(:index) + assert_equal "Hello world", result.response_obj[:body] + end + end + + # Test Render mixin + # ==== + class RenderingController < AbstractController::Base + use Renderer + + def _prefix() end + + def render(options = {}) + if options.is_a?(String) + options = {:_template_name => options} + end + + options[:_prefix] = _prefix + super + end + + append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + end + + class Me2 < RenderingController + def index + render "index.erb" + end + + def action_with_ivars + @my_ivar = "Hello" + render "action_with_ivars.erb" + end + + def naked_render + render + end + end + + class TestRenderer < ActiveSupport::TestCase + test "rendering templates works" do + result = Me2.process(:index) + assert_equal "Hello from index.erb", result.response_obj[:body] + end + + test "rendering passes ivars to the view" do + result = Me2.process(:action_with_ivars) + assert_equal "Hello from index_with_ivars.erb", result.response_obj[:body] + end + + test "rendering with no template name" do + result = Me2.process(:naked_render) + assert_equal "Hello from naked_render.erb", result.response_obj[:body] + end + end + + # Test rendering with prefixes + # ==== + # * self._prefix is used when defined + class PrefixedViews < RenderingController + private + def self.prefix + name.underscore + end + + def _prefix + self.class.prefix + end + end + + class Me3 < PrefixedViews + def index + render + end + + def formatted + self.formats = [:html] + render + end + end + + class TestPrefixedViews < ActiveSupport::TestCase + test "templates are located inside their 'prefix' folder" do + result = Me3.process(:index) + assert_equal "Hello from me3/index.erb", result.response_obj[:body] + end + + test "templates included their format" do + result = Me3.process(:formatted) + assert_equal "Hello from me3/formatted.html.erb", result.response_obj[:body] + end + end + + # Test rendering with layouts + # ==== + # self._layout is used when defined + class WithLayouts < PrefixedViews + use Layouts + + private + def self.layout(formats) + begin + view_paths.find_by_parts(name.underscore, formats, "layouts") + rescue ActionView::MissingTemplate + begin + view_paths.find_by_parts("application", formats, "layouts") + rescue ActionView::MissingTemplate + end + end + end + + def _layout + self.class.layout(formats) + end + + def render_to_body(options = {}) + options[:_layout] = options[:layout] || _layout + super + end + end + + class Me4 < WithLayouts + def index + render + end + end + + class Me5 < WithLayouts + def index + render + end + end + + class TestLayouts < ActiveSupport::TestCase + test "layouts are included" do + result = Me4.process(:index) + assert_equal "Me4 Enter : Hello from me4/index.erb : Exit", result.response_obj[:body] + end + + test "it can fall back to the application layout" do + result = Me5.process(:index) + assert_equal "Application Enter : Hello from me5/index.erb : Exit", result.response_obj[:body] + end + end + + # respond_to_action?(action_name) + # ==== + # * A method can be used as an action only if this method + # returns true when passed the method name as an argument + # * Defaults to true in AbstractController + class DefaultRespondToActionController < AbstractController::Base + def index() self.response_body = "success" end + end + + class ActionMissingRespondToActionController < AbstractController::Base + # No actions + private + def action_missing(action_name) + self.response_body = "success" + end + end + + class RespondToActionController < AbstractController::Base; + def index() self.response_body = "success" end + + def fail() self.response_body = "fail" end + + private + + def respond_to_action?(action_name) + action_name != :fail + end + + end + + class TestRespondToAction < ActiveSupport::TestCase + + def assert_dispatch(klass, body = "success", action = :index) + response = klass.process(action).response_obj[:body] + assert_equal body, response + end + + test "an arbitrary method is available as an action by default" do + assert_dispatch DefaultRespondToActionController, "success", :index + end + + test "raises ActionNotFound when method does not exist and action_missing is not defined" do + assert_raise(ActionNotFound) { DefaultRespondToActionController.process(:fail) } + end + + test "dispatches to action_missing when method does not exist and action_missing is defined" do + assert_dispatch ActionMissingRespondToActionController, "success", :ohai + end + + test "a method is available as an action if respond_to_action? returns true" do + assert_dispatch RespondToActionController, "success", :index + end + + test "raises ActionNotFound if method is defined but respond_to_action? returns false" do + assert_raise(ActionNotFound) { RespondToActionController.process(:fail) } + end + end + + end +end diff --git a/actionpack/test/abstract_controller/callbacks_test.rb b/actionpack/test/abstract_controller/callbacks_test.rb new file mode 100644 index 0000000000..5fce30f478 --- /dev/null +++ b/actionpack/test/abstract_controller/callbacks_test.rb @@ -0,0 +1,197 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module AbstractController + module Testing + + class ControllerWithCallbacks < AbstractController::Base + use AbstractController::Callbacks + end + + class Callback1 < ControllerWithCallbacks + process_action_callback :before, :first + + def first + @text = "Hello world" + end + + def index + self.response_body = @text + end + end + + class TestCallbacks < ActiveSupport::TestCase + test "basic callbacks work" do + result = Callback1.process(:index) + assert_equal "Hello world", result.response_obj[:body] + end + end + + class Callback2 < ControllerWithCallbacks + before_filter :first + after_filter :second + around_filter :aroundz + + def first + @text = "Hello world" + end + + def second + @second = "Goodbye" + end + + def aroundz + @aroundz = "FIRST" + yield + @aroundz << "SECOND" + end + + def index + self.response_body = @text + end + end + + class TestCallbacks < ActiveSupport::TestCase + test "before_filter works" do + result = Callback2.process(:index) + assert_equal "Hello world", result.response_obj[:body] + end + + test "after_filter works" do + result = Callback2.process(:index) + assert_equal "Goodbye", result.instance_variable_get("@second") + end + + test "around_filter works" do + result = Callback2.process(:index) + assert_equal "FIRSTSECOND", result.instance_variable_get("@aroundz") + end + end + + class Callback3 < ControllerWithCallbacks + before_filter do |c| + c.instance_variable_set("@text", "Hello world") + end + + after_filter do |c| + c.instance_variable_set("@second", "Goodbye") + end + + def index + self.response_body = @text + end + end + + class TestCallbacks < ActiveSupport::TestCase + test "before_filter works with procs" do + result = Callback3.process(:index) + assert_equal "Hello world", result.response_obj[:body] + end + + test "after_filter works with procs" do + result = Callback3.process(:index) + assert_equal "Goodbye", result.instance_variable_get("@second") + end + end + + class CallbacksWithConditions < ControllerWithCallbacks + before_filter :list, :only => :index + before_filter :authenticate, :except => :index + + def index + self.response_body = @list.join(", ") + end + + def sekrit_data + self.response_body = (@list + [@authenticated]).join(", ") + end + + private + def list + @list = ["Hello", "World"] + end + + def authenticate + @list = [] + @authenticated = "true" + end + end + + class TestCallbacks < ActiveSupport::TestCase + test "when :only is specified, a before filter is triggered on that action" do + result = CallbacksWithConditions.process(:index) + assert_equal "Hello, World", result.response_obj[:body] + end + + test "when :only is specified, a before filter is not triggered on other actions" do + result = CallbacksWithConditions.process(:sekrit_data) + assert_equal "true", result.response_obj[:body] + end + + test "when :except is specified, an after filter is not triggered on that action" do + result = CallbacksWithConditions.process(:index) + assert_nil result.instance_variable_get("@authenticated") + end + end + + class CallbacksWithArrayConditions < ControllerWithCallbacks + before_filter :list, :only => [:index, :listy] + before_filter :authenticate, :except => [:index, :listy] + + def index + self.response_body = @list.join(", ") + end + + def sekrit_data + self.response_body = (@list + [@authenticated]).join(", ") + end + + private + def list + @list = ["Hello", "World"] + end + + def authenticate + @list = [] + @authenticated = "true" + end + end + + class TestCallbacks < ActiveSupport::TestCase + test "when :only is specified with an array, a before filter is triggered on that action" do + result = CallbacksWithArrayConditions.process(:index) + assert_equal "Hello, World", result.response_obj[:body] + end + + test "when :only is specified with an array, a before filter is not triggered on other actions" do + result = CallbacksWithArrayConditions.process(:sekrit_data) + assert_equal "true", result.response_obj[:body] + end + + test "when :except is specified with an array, an after filter is not triggered on that action" do + result = CallbacksWithArrayConditions.process(:index) + assert_nil result.instance_variable_get("@authenticated") + end + end + + class ChangedConditions < Callback2 + before_filter :first, :only => :index + + def not_index + self.response_body = @text.to_s + end + end + + class TestCallbacks < ActiveSupport::TestCase + test "when a callback is modified in a child with :only, it works for the :only action" do + result = ChangedConditions.process(:index) + assert_equal "Hello world", result.response_obj[:body] + end + + test "when a callback is modified in a child with :only, it does not work for other actions" do + result = ChangedConditions.process(:not_index) + assert_equal "", result.response_obj[:body] + end + end + + end +end
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/helper_test.rb b/actionpack/test/abstract_controller/helper_test.rb new file mode 100644 index 0000000000..6284fa4f70 --- /dev/null +++ b/actionpack/test/abstract_controller/helper_test.rb @@ -0,0 +1,43 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module AbstractController + module Testing + + class ControllerWithHelpers < AbstractController::Base + use Renderer + use Helpers + + def render(string) + super(:_template_name => string) + end + + append_view_path File.expand_path(File.join(File.dirname(__FILE__), "views")) + end + + module HelperyTest + def included_method + "Included" + end + end + + class MyHelpers1 < ControllerWithHelpers + helper(HelperyTest) do + def helpery_test + "World" + end + end + + def index + render "helper_test.erb" + end + end + + class TestHelpers < ActiveSupport::TestCase + def test_helpers + result = MyHelpers1.process(:index) + assert_equal "Hello World : Included", result.response_obj[:body] + end + end + + end +end
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/layouts_test.rb b/actionpack/test/abstract_controller/layouts_test.rb new file mode 100644 index 0000000000..3d4570bfef --- /dev/null +++ b/actionpack/test/abstract_controller/layouts_test.rb @@ -0,0 +1,232 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), "test_helper") + +module AbstractControllerTests + module Layouts + + # Base controller for these tests + class Base < AbstractController::Base + use AbstractController::Renderer + use AbstractController::Layouts + + self.view_paths = [ActionView::FixtureTemplate::FixturePath.new( + "layouts/hello.erb" => "With String <%= yield %>", + "layouts/hello_override.erb" => "With Override <%= yield %>", + "layouts/abstract_controller_tests/layouts/with_string_implied_child.erb" => + "With Implied <%= yield %>", + "layouts/omg.erb" => "OMGHI2U <%= yield %>", + "layouts/with_false_layout.erb" => "False Layout <%= yield %>" + )] + + def self.controller_path + @controller_path ||= self.name.sub(/Controller$/, '').underscore + end + + def controller_path() self.class.controller_path end + + def render_to_body(options) + options[:_layout] = _default_layout + super + end + end + + class Blank < Base + self.view_paths = [] + + def index + render :_template => ActionView::TextTemplate.new("Hello blank!") + end + end + + class WithString < Base + layout "hello" + + def index + render :_template => ActionView::TextTemplate.new("Hello string!") + end + end + + class WithStringChild < WithString + end + + class WithStringOverriddenChild < WithString + layout "hello_override" + end + + class WithNilChild < WithString + layout nil + end + + class WithStringImpliedChild < WithString + end + + class WithChildOfImplied < WithStringImpliedChild + end + + class WithSymbol < Base + layout :hello + + def index + render :_template => ActionView::TextTemplate.new("Hello symbol!") + end + private + def hello + "omg" + end + end + + class WithSymbolReturningString < Base + layout :no_hello + + def index + render :_template => ActionView::TextTemplate.new("Hello missing symbol!") + end + private + def no_hello + nil + end + end + + class WithSymbolReturningNil < Base + layout :nilz + + def index + render :_template => ActionView::TextTemplate.new("Hello nilz!") + end + + def nilz() end + end + + class WithSymbolReturningObj < Base + layout :objekt + + def index + render :_template => ActionView::TextTemplate.new("Hello nilz!") + end + + def objekt + Object.new + end + end + + class WithSymbolAndNoMethod < Base + layout :omg_no_method + + def index + render :_template => ActionView::TextTemplate.new("Hello boom!") + end + end + + class WithMissingLayout < Base + layout "missing" + + def index + render :_template => ActionView::TextTemplate.new("Hello missing!") + end + end + + class WithFalseLayout < Base + layout false + + def index + render :_template => ActionView::TextTemplate.new("Hello false!") + end + end + + class WithNilLayout < Base + layout nil + + def index + render :_template => ActionView::TextTemplate.new("Hello nil!") + end + end + + # TODO Move to bootloader + AbstractController::Base.subclasses.each do |klass| + klass = klass.constantize + next unless klass < AbstractController::Layouts + klass.class_eval do + _write_layout_method + end + end + + class TestBase < ActiveSupport::TestCase + test "when no layout is specified, and no default is available, render without a layout" do + result = Blank.process(:index) + assert_equal "Hello blank!", result.response_obj[:body] + end + + test "when layout is specified as a string, render with that layout" do + result = WithString.process(:index) + assert_equal "With String Hello string!", result.response_obj[:body] + end + + test "when layout is specified as a string, but the layout is missing, raise an exception" do + assert_raises(ActionView::MissingTemplate) { WithMissingLayout.process(:index) } + end + + test "when layout is specified as false, do not use a layout" do + result = WithFalseLayout.process(:index) + assert_equal "Hello false!", result.response_obj[:body] + end + + test "when layout is specified as nil, do not use a layout" do + result = WithNilLayout.process(:index) + assert_equal "Hello nil!", result.response_obj[:body] + end + + test "when layout is specified as a symbol, call the requested method and use the layout returned" do + result = WithSymbol.process(:index) + assert_equal "OMGHI2U Hello symbol!", result.response_obj[:body] + end + + test "when layout is specified as a symbol and the method returns nil, don't use a layout" do + result = WithSymbolReturningNil.process(:index) + assert_equal "Hello nilz!", result.response_obj[:body] + end + + test "when the layout is specified as a symbol and the method doesn't exist, raise an exception" do + assert_raises(NoMethodError, /:nilz/) { WithSymbolAndNoMethod.process(:index) } + end + + test "when the layout is specified as a symbol and the method returns something besides a string/false/nil, raise an exception" do + assert_raises(ArgumentError) { WithSymbolReturningObj.process(:index) } + end + + test "when a child controller does not have a layout, use the parent controller layout" do + result = WithStringChild.process(:index) + assert_equal "With String Hello string!", result.response_obj[:body] + end + + test "when a child controller has specified a layout, use that layout and not the parent controller layout" do + result = WithStringOverriddenChild.process(:index) + assert_equal "With Override Hello string!", result.response_obj[:body] + end + + test "when a child controller has an implied layout, use that layout and not the parent controller layout" do + result = WithStringImpliedChild.process(:index) + assert_equal "With Implied Hello string!", result.response_obj[:body] + end + + test "when a child controller specifies layout nil, do not use the parent layout" do + result = WithNilChild.process(:index) + assert_equal "Hello string!", result.response_obj[:body] + end + + test "when a grandchild has no layout specified, the child has an implied layout, and the " \ + "parent has specified a layout, use the child controller layout" do + result = WithChildOfImplied.process(:index) + assert_equal "With Implied Hello string!", result.response_obj[:body] + end + + test "raises an exception when specifying layout true" do + assert_raises ArgumentError do + Object.class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 + class ::BadOmgFailLolLayout < AbstractControllerTests::Layouts::Base + layout true + end + RUBY_EVAL + end + end + end + end +end diff --git a/actionpack/test/abstract_controller/test_helper.rb b/actionpack/test/abstract_controller/test_helper.rb new file mode 100644 index 0000000000..b9248c6bbd --- /dev/null +++ b/actionpack/test/abstract_controller/test_helper.rb @@ -0,0 +1,25 @@ +$:.unshift(File.dirname(__FILE__) + '/../../lib') +$:.unshift(File.dirname(__FILE__) + '/../../../activesupport/lib') +$:.unshift(File.dirname(__FILE__) + '/../lib') + +require 'test/unit' +require 'active_support' +require 'active_support/test_case' +require 'action_controller' +require 'action_view/base' +require 'fixture_template' + +begin + require 'ruby-debug' + Debugger.settings[:autoeval] = true + Debugger.start +rescue LoadError + # Debugging disabled. `gem install ruby-debug` to enable. +end + +require 'action_controller/abstract' +# require 'action_controller/abstract/base' +# require 'action_controller/abstract/renderer' +# require 'action_controller/abstract/layouts' +# require 'action_controller/abstract/callbacks' +# require 'action_controller/abstract/helpers'
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb new file mode 100644 index 0000000000..785bf69191 --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/formatted.html.erb @@ -0,0 +1 @@ +Hello from me3/formatted.html.erb
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb new file mode 100644 index 0000000000..f079ad8204 --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me3/index.erb @@ -0,0 +1 @@ +Hello from me3/index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb new file mode 100644 index 0000000000..89dce12bdc --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me4/index.erb @@ -0,0 +1 @@ +Hello from me4/index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb b/actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb new file mode 100644 index 0000000000..84d0b7417e --- /dev/null +++ b/actionpack/test/abstract_controller/views/abstract_controller/testing/me5/index.erb @@ -0,0 +1 @@ +Hello from me5/index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/action_with_ivars.erb b/actionpack/test/abstract_controller/views/action_with_ivars.erb new file mode 100644 index 0000000000..8d8ae22fd7 --- /dev/null +++ b/actionpack/test/abstract_controller/views/action_with_ivars.erb @@ -0,0 +1 @@ +<%= @my_ivar %> from index_with_ivars.erb
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/helper_test.erb b/actionpack/test/abstract_controller/views/helper_test.erb new file mode 100644 index 0000000000..8ae45cc195 --- /dev/null +++ b/actionpack/test/abstract_controller/views/helper_test.erb @@ -0,0 +1 @@ +Hello <%= helpery_test %> : <%= included_method %>
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/index.erb b/actionpack/test/abstract_controller/views/index.erb new file mode 100644 index 0000000000..cc1a8b8c85 --- /dev/null +++ b/actionpack/test/abstract_controller/views/index.erb @@ -0,0 +1 @@ +Hello from index.erb
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb b/actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb new file mode 100644 index 0000000000..172dd56569 --- /dev/null +++ b/actionpack/test/abstract_controller/views/layouts/abstract_controller/testing/me4.erb @@ -0,0 +1 @@ +Me4 Enter : <%= yield %> : Exit
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/layouts/application.erb b/actionpack/test/abstract_controller/views/layouts/application.erb new file mode 100644 index 0000000000..27317140ad --- /dev/null +++ b/actionpack/test/abstract_controller/views/layouts/application.erb @@ -0,0 +1 @@ +Application Enter : <%= yield %> : Exit
\ No newline at end of file diff --git a/actionpack/test/abstract_controller/views/naked_render.erb b/actionpack/test/abstract_controller/views/naked_render.erb new file mode 100644 index 0000000000..1b3d03878b --- /dev/null +++ b/actionpack/test/abstract_controller/views/naked_render.erb @@ -0,0 +1 @@ +Hello from naked_render.erb
\ No newline at end of file |