1
require 'abstract_unit'
3
# The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited
4
# method has access to the view_paths array when looking for a layout to automatically assign.
5
old_load_paths = ActionController::Base.view_paths
7
ActionView::Template::register_template_handler :mab,
8
lambda { |template| template.source.inspect }
10
ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/layout_tests/' ]
12
class LayoutTest < ActionController::Base
13
def self.controller_path; 'views' end
14
self.view_paths = ActionController::Base.view_paths.dup
17
# Restore view_paths to previous value
18
ActionController::Base.view_paths = old_load_paths
20
class ProductController < LayoutTest
23
class ItemController < LayoutTest
26
class ThirdPartyTemplateLibraryController < LayoutTest
29
module ControllerNameSpace
32
class ControllerNameSpace::NestedController < LayoutTest
35
class MultipleExtensions < LayoutTest
38
class LayoutAutoDiscoveryTest < ActionController::TestCase
40
@request.host = "www.nextangle.com"
43
def test_application_layout_is_default_when_no_controller_match
44
@controller = ProductController.new
46
assert_equal 'layout_test.rhtml hello.rhtml', @response.body
49
def test_controller_name_layout_name_match
50
@controller = ItemController.new
52
assert_equal 'item.rhtml hello.rhtml', @response.body
55
def test_third_party_template_library_auto_discovers_layout
56
@controller = ThirdPartyTemplateLibraryController.new
58
assert_equal 'layouts/third_party_template_library.mab', @controller.active_layout.to_s
59
assert_equal 'layouts/third_party_template_library', @response.layout
60
assert_response :success
61
assert_equal 'Mab', @response.body
64
def test_namespaced_controllers_auto_detect_layouts
65
@controller = ControllerNameSpace::NestedController.new
67
assert_equal 'layouts/controller_name_space/nested', @controller.active_layout.to_s
68
assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body
71
def test_namespaced_controllers_auto_detect_layouts
72
@controller = MultipleExtensions.new
74
assert_equal 'layouts/multiple_extensions.html.erb', @controller.active_layout.to_s
75
assert_equal 'multiple_extensions.html.erb hello.rhtml', @response.body.strip
79
class DefaultLayoutController < LayoutTest
82
class AbsolutePathLayoutController < LayoutTest
83
layout File.expand_path(File.expand_path(__FILE__) + '/../../fixtures/layout_tests/layouts/layout_test.rhtml')
86
class AbsolutePathWithoutLayoutsController < LayoutTest
87
# Absolute layout path without 'layouts' in it.
88
layout File.expand_path(File.expand_path(__FILE__) + '/../../fixtures/layout_tests/abs_path_layout.rhtml')
91
class HasOwnLayoutController < LayoutTest
95
class PrependsViewPathController < LayoutTest
97
prepend_view_path File.dirname(__FILE__) + '/../fixtures/layout_tests/alt/'
98
render :layout => 'alt'
102
class SetsLayoutInRenderController < LayoutTest
104
render :layout => 'third_party_template_library'
108
class RendersNoLayoutController < LayoutTest
110
render :layout => false
114
class LayoutSetInResponseTest < ActionController::TestCase
115
def test_layout_set_when_using_default_layout
116
@controller = DefaultLayoutController.new
118
assert_equal 'layouts/layout_test', @response.layout
121
def test_layout_set_when_set_in_controller
122
@controller = HasOwnLayoutController.new
124
assert_equal 'layouts/item', @response.layout
127
def test_layout_set_when_using_render
128
@controller = SetsLayoutInRenderController.new
130
assert_equal 'layouts/third_party_template_library', @response.layout
133
def test_layout_is_not_set_when_none_rendered
134
@controller = RendersNoLayoutController.new
136
assert_nil @response.layout
139
def test_exempt_from_layout_honored_by_render_template
140
ActionController::Base.exempt_from_layout :rhtml
141
@controller = RenderWithTemplateOptionController.new
144
assert_equal "alt/hello.rhtml", @response.body.strip
147
ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
150
def test_layout_is_picked_from_the_controller_instances_view_path
151
@controller = PrependsViewPathController.new
153
assert_equal 'layouts/alt', @response.layout
156
def test_absolute_pathed_layout
157
@controller = AbsolutePathLayoutController.new
159
assert_equal "layout_test.rhtml hello.rhtml", @response.body.strip
162
def test_absolute_pathed_layout_without_layouts_in_path
163
@controller = AbsolutePathWithoutLayoutsController.new
165
assert_equal "abs_path_layout.rhtml hello.rhtml", @response.body.strip
169
class RenderWithTemplateOptionController < LayoutTest
171
render :template => 'alt/hello'
175
class SetsNonExistentLayoutFile < LayoutTest
176
layout "nofile.rhtml"
179
class LayoutExceptionRaised < ActionController::TestCase
180
def test_exception_raised_when_layout_file_not_found
181
@controller = SetsNonExistentLayoutFile.new
183
assert_kind_of ActionView::MissingTemplate, @response.template.instance_eval { @exception }
187
class LayoutStatusIsRendered < LayoutTest
189
render :status => 401
193
class LayoutStatusIsRenderedTest < ActionController::TestCase
194
def test_layout_status_is_rendered
195
@controller = LayoutStatusIsRendered.new
201
unless RUBY_PLATFORM =~ /(:?mswin|mingw|bccwin)/
202
class LayoutSymlinkedTest < LayoutTest
203
layout "symlinked/symlinked_layout"
206
class LayoutSymlinkedIsRenderedTest < ActionController::TestCase
207
def test_symlinked_layout_is_rendered
208
@controller = LayoutSymlinkedTest.new
211
assert_equal "layouts/symlinked/symlinked_layout", @response.layout