2017-06-22 22:59:18 -04:00
|
|
|
# frozen_string_literal: true
|
2016-08-06 12:50:17 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_support/core_ext/array/extract_options"
|
2006-01-12 02:16:23 -05:00
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
# The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited
|
|
|
|
# method has access to the view_paths array when looking for a layout to automatically assign.
|
|
|
|
old_load_paths = ActionController::Base.view_paths
|
2008-11-28 12:18:28 -05:00
|
|
|
|
2017-05-15 10:17:28 -04:00
|
|
|
ActionController::Base.view_paths = [ File.expand_path("../../fixtures/actionpack/layout_tests", __dir__) ]
|
2006-01-12 02:16:23 -05:00
|
|
|
|
|
|
|
class LayoutTest < ActionController::Base
|
2016-08-06 12:50:17 -04:00
|
|
|
def self.controller_path; "views" end
|
|
|
|
def self._implied_layout_name; to_s.underscore.gsub(/_controller$/, "") ; end
|
2007-02-04 15:47:05 -05:00
|
|
|
self.view_paths = ActionController::Base.view_paths.dup
|
2006-01-12 02:16:23 -05:00
|
|
|
end
|
|
|
|
|
2014-06-12 08:06:59 -04:00
|
|
|
module TemplateHandlerHelper
|
|
|
|
def with_template_handler(*extensions, handler)
|
|
|
|
ActionView::Template.register_template_handler(*extensions, handler)
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActionView::Template.unregister_template_handler(*extensions)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-02-04 15:47:05 -05:00
|
|
|
# Restore view_paths to previous value
|
|
|
|
ActionController::Base.view_paths = old_load_paths
|
2006-01-12 02:16:23 -05:00
|
|
|
|
|
|
|
class ProductController < LayoutTest
|
|
|
|
end
|
|
|
|
|
|
|
|
class ItemController < LayoutTest
|
|
|
|
end
|
|
|
|
|
|
|
|
class ThirdPartyTemplateLibraryController < LayoutTest
|
|
|
|
end
|
|
|
|
|
2006-01-15 06:28:55 -05:00
|
|
|
module ControllerNameSpace
|
|
|
|
end
|
|
|
|
|
|
|
|
class ControllerNameSpace::NestedController < LayoutTest
|
|
|
|
end
|
|
|
|
|
2007-04-12 13:11:48 -04:00
|
|
|
class MultipleExtensions < LayoutTest
|
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class LayoutAutoDiscoveryTest < ActionController::TestCase
|
2014-06-09 10:45:46 -04:00
|
|
|
include TemplateHandlerHelper
|
|
|
|
|
2006-01-12 02:16:23 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2006-01-12 02:16:23 -05:00
|
|
|
@request.host = "www.nextangle.com"
|
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2006-01-12 02:16:23 -05:00
|
|
|
def test_application_layout_is_default_when_no_controller_match
|
|
|
|
@controller = ProductController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "layout_test.erb hello.erb", @response.body
|
2006-01-12 02:16:23 -05:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2006-01-12 02:16:23 -05:00
|
|
|
def test_controller_name_layout_name_match
|
|
|
|
@controller = ItemController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "item.erb hello.erb", @response.body
|
2006-01-12 02:16:23 -05:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2006-01-12 02:16:23 -05:00
|
|
|
def test_third_party_template_library_auto_discovers_layout
|
2014-06-09 10:45:46 -04:00
|
|
|
with_template_handler :mab, lambda { |template| template.source.inspect } do
|
|
|
|
@controller = ThirdPartyTemplateLibraryController.new
|
|
|
|
get :hello
|
|
|
|
assert_response :success
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "layouts/third_party_template_library.mab", @response.body
|
2014-06-09 10:45:46 -04:00
|
|
|
end
|
2006-01-12 02:16:23 -05:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2009-05-21 21:14:20 -04:00
|
|
|
def test_namespaced_controllers_auto_detect_layouts1
|
2006-01-15 06:28:55 -05:00
|
|
|
@controller = ControllerNameSpace::NestedController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "controller_name_space/nested.erb hello.erb", @response.body
|
2006-01-15 06:28:55 -05:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2009-05-21 21:14:20 -04:00
|
|
|
def test_namespaced_controllers_auto_detect_layouts2
|
2007-04-12 13:11:48 -04:00
|
|
|
@controller = MultipleExtensions.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "multiple_extensions.html.erb hello.erb", @response.body.strip
|
2007-04-12 13:11:48 -04:00
|
|
|
end
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class DefaultLayoutController < LayoutTest
|
|
|
|
end
|
|
|
|
|
2012-03-05 14:21:04 -05:00
|
|
|
class StreamingLayoutController < LayoutTest
|
|
|
|
def render(*args)
|
2013-01-17 06:20:02 -05:00
|
|
|
options = args.extract_options!
|
2016-08-06 13:36:34 -04:00
|
|
|
super(*args, options.merge(stream: true))
|
2012-03-05 14:21:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-05 19:49:22 -05:00
|
|
|
class AbsolutePathLayoutController < LayoutTest
|
2017-05-15 10:17:28 -04:00
|
|
|
layout File.expand_path("../../fixtures/actionpack/layout_tests/layouts/layout_test", __dir__)
|
2009-03-05 19:49:22 -05:00
|
|
|
end
|
|
|
|
|
2006-05-17 03:25:36 -04:00
|
|
|
class HasOwnLayoutController < LayoutTest
|
2016-08-06 12:50:17 -04:00
|
|
|
layout "item"
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
|
|
|
|
2012-12-07 18:55:54 -05:00
|
|
|
class HasNilLayoutSymbol < LayoutTest
|
|
|
|
layout :nilz
|
|
|
|
|
|
|
|
def nilz
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class HasNilLayoutProc < LayoutTest
|
2013-03-28 06:28:03 -04:00
|
|
|
layout proc { nil }
|
2012-12-07 18:55:54 -05:00
|
|
|
end
|
|
|
|
|
2009-02-20 15:19:45 -05:00
|
|
|
class PrependsViewPathController < LayoutTest
|
|
|
|
def hello
|
2017-05-15 10:17:28 -04:00
|
|
|
prepend_view_path File.expand_path("../../fixtures/actionpack/layout_tests/alt", __dir__)
|
2016-08-06 13:36:34 -04:00
|
|
|
render layout: "alt"
|
2009-02-20 15:19:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
class OnlyLayoutController < LayoutTest
|
2016-08-06 13:36:34 -04:00
|
|
|
layout "item", only: "hello"
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class ExceptLayoutController < LayoutTest
|
2016-08-06 13:36:34 -04:00
|
|
|
layout "item", except: "goodbye"
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
|
2006-05-17 03:25:36 -04:00
|
|
|
class SetsLayoutInRenderController < LayoutTest
|
|
|
|
def hello
|
2016-08-06 13:36:34 -04:00
|
|
|
render layout: "third_party_template_library"
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RendersNoLayoutController < LayoutTest
|
|
|
|
def hello
|
2016-08-06 13:36:34 -04:00
|
|
|
render layout: false
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class LayoutSetInResponseTest < ActionController::TestCase
|
2009-12-02 23:01:01 -05:00
|
|
|
include ActionView::Template::Handlers
|
2014-06-09 10:45:46 -04:00
|
|
|
include TemplateHandlerHelper
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2006-05-17 03:25:36 -04:00
|
|
|
def test_layout_set_when_using_default_layout
|
|
|
|
@controller = DefaultLayoutController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "layout_test.erb"
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2012-03-05 14:21:04 -05:00
|
|
|
def test_layout_set_when_using_streaming_layout
|
|
|
|
@controller = StreamingLayoutController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "layout_test.erb"
|
2012-03-05 14:21:04 -05:00
|
|
|
end
|
|
|
|
|
2006-05-17 03:25:36 -04:00
|
|
|
def test_layout_set_when_set_in_controller
|
|
|
|
@controller = HasOwnLayoutController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "item.erb"
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2012-12-07 18:55:54 -05:00
|
|
|
def test_layout_symbol_set_in_controller_returning_nil_falls_back_to_default
|
|
|
|
@controller = HasNilLayoutSymbol.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "layout_test.erb"
|
2012-12-07 18:55:54 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_layout_proc_set_in_controller_returning_nil_falls_back_to_default
|
|
|
|
@controller = HasNilLayoutProc.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "layout_test.erb"
|
2012-12-07 18:55:54 -05:00
|
|
|
end
|
|
|
|
|
2009-01-22 17:18:10 -05:00
|
|
|
def test_layout_only_exception_when_included
|
|
|
|
@controller = OnlyLayoutController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "item.erb"
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_layout_only_exception_when_excepted
|
|
|
|
@controller = OnlyLayoutController.new
|
|
|
|
get :goodbye
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_not_includes @response.body, "item.erb"
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_layout_except_exception_when_included
|
|
|
|
@controller = ExceptLayoutController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "item.erb"
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_layout_except_exception_when_excepted
|
|
|
|
@controller = ExceptLayoutController.new
|
|
|
|
get :goodbye
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_not_includes @response.body, "item.erb"
|
2009-01-22 17:18:10 -05:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2006-05-17 03:25:36 -04:00
|
|
|
def test_layout_set_when_using_render
|
2014-06-09 10:45:46 -04:00
|
|
|
with_template_handler :mab, lambda { |template| template.source.inspect } do
|
|
|
|
@controller = SetsLayoutInRenderController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "layouts/third_party_template_library.mab"
|
2014-06-09 10:45:46 -04:00
|
|
|
end
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
2008-08-17 20:29:24 -04:00
|
|
|
|
2006-05-17 03:25:36 -04:00
|
|
|
def test_layout_is_not_set_when_none_rendered
|
|
|
|
@controller = RendersNoLayoutController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_equal "hello.erb", @response.body
|
2006-05-17 03:25:36 -04:00
|
|
|
end
|
2007-09-30 18:59:24 -04:00
|
|
|
|
2009-02-20 15:19:45 -05:00
|
|
|
def test_layout_is_picked_from_the_controller_instances_view_path
|
2009-06-17 15:00:23 -04:00
|
|
|
@controller = PrependsViewPathController.new
|
|
|
|
get :hello
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "alt.erb"
|
2009-02-20 15:19:45 -05:00
|
|
|
end
|
2009-03-05 19:49:22 -05:00
|
|
|
|
|
|
|
def test_absolute_pathed_layout
|
|
|
|
@controller = AbsolutePathLayoutController.new
|
|
|
|
get :hello
|
2010-10-09 19:14:50 -04:00
|
|
|
assert_equal "layout_test.erb hello.erb", @response.body.strip
|
2009-03-05 19:49:22 -05:00
|
|
|
end
|
2007-09-30 18:59:24 -04:00
|
|
|
end
|
2006-07-04 22:38:55 -04:00
|
|
|
|
|
|
|
class SetsNonExistentLayoutFile < LayoutTest
|
2011-09-22 09:37:38 -04:00
|
|
|
layout "nofile"
|
2006-07-04 22:38:55 -04:00
|
|
|
end
|
|
|
|
|
2012-07-29 16:47:05 -04:00
|
|
|
class LayoutExceptionRaisedTest < ActionController::TestCase
|
2006-07-04 22:38:55 -04:00
|
|
|
def test_exception_raised_when_layout_file_not_found
|
|
|
|
@controller = SetsNonExistentLayoutFile.new
|
2009-05-03 00:02:22 -04:00
|
|
|
assert_raise(ActionView::MissingTemplate) { get :hello }
|
2006-07-04 22:38:55 -04:00
|
|
|
end
|
|
|
|
end
|
2007-09-07 23:12:03 -04:00
|
|
|
|
|
|
|
class LayoutStatusIsRendered < LayoutTest
|
|
|
|
def hello
|
2016-08-06 13:36:34 -04:00
|
|
|
render status: 401
|
2007-09-07 23:12:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class LayoutStatusIsRenderedTest < ActionController::TestCase
|
2007-09-07 23:12:03 -04:00
|
|
|
def test_layout_status_is_rendered
|
|
|
|
@controller = LayoutStatusIsRendered.new
|
|
|
|
get :hello
|
|
|
|
assert_response 401
|
|
|
|
end
|
|
|
|
end
|
2008-03-28 16:54:26 -04:00
|
|
|
|
2016-11-30 06:57:50 -05:00
|
|
|
unless Gem.win_platform?
|
2008-12-27 12:28:28 -05:00
|
|
|
class LayoutSymlinkedTest < LayoutTest
|
|
|
|
layout "symlinked/symlinked_layout"
|
|
|
|
end
|
|
|
|
|
|
|
|
class LayoutSymlinkedIsRenderedTest < ActionController::TestCase
|
|
|
|
def test_symlinked_layout_is_rendered
|
|
|
|
@controller = LayoutSymlinkedTest.new
|
|
|
|
get :hello
|
|
|
|
assert_response 200
|
2016-08-06 12:50:17 -04:00
|
|
|
assert_includes @response.body, "This is my layout"
|
2008-12-27 12:28:28 -05:00
|
|
|
end
|
2008-03-28 16:54:26 -04:00
|
|
|
end
|
|
|
|
end
|