1575674e18c86cf2c522011429ff2b3dd97d5c18
[feedcatcher.git] / vendor / rails / actionpack / test / controller / layout_test.rb
1 require 'abstract_unit'
2
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
6
7 ActionView::Template::register_template_handler :mab,
8 lambda { |template| template.source.inspect }
9
10 ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/layout_tests/' ]
11
12 class LayoutTest < ActionController::Base
13 def self.controller_path; 'views' end
14 self.view_paths = ActionController::Base.view_paths.dup
15 end
16
17 # Restore view_paths to previous value
18 ActionController::Base.view_paths = old_load_paths
19
20 class ProductController < LayoutTest
21 end
22
23 class ItemController < LayoutTest
24 end
25
26 class ThirdPartyTemplateLibraryController < LayoutTest
27 end
28
29 module ControllerNameSpace
30 end
31
32 class ControllerNameSpace::NestedController < LayoutTest
33 end
34
35 class MultipleExtensions < LayoutTest
36 end
37
38 class LayoutAutoDiscoveryTest < ActionController::TestCase
39 def setup
40 @request.host = "www.nextangle.com"
41 end
42
43 def test_application_layout_is_default_when_no_controller_match
44 @controller = ProductController.new
45 get :hello
46 assert_equal 'layout_test.rhtml hello.rhtml', @response.body
47 end
48
49 def test_controller_name_layout_name_match
50 @controller = ItemController.new
51 get :hello
52 assert_equal 'item.rhtml hello.rhtml', @response.body
53 end
54
55 def test_third_party_template_library_auto_discovers_layout
56 @controller = ThirdPartyTemplateLibraryController.new
57 get :hello
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
62 end
63
64 def test_namespaced_controllers_auto_detect_layouts
65 @controller = ControllerNameSpace::NestedController.new
66 get :hello
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
69 end
70
71 def test_namespaced_controllers_auto_detect_layouts
72 @controller = MultipleExtensions.new
73 get :hello
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
76 end
77 end
78
79 class DefaultLayoutController < LayoutTest
80 end
81
82 class AbsolutePathLayoutController < LayoutTest
83 layout File.expand_path(File.expand_path(__FILE__) + '/../../fixtures/layout_tests/layouts/layout_test.rhtml')
84 end
85
86 class HasOwnLayoutController < LayoutTest
87 layout 'item'
88 end
89
90 class PrependsViewPathController < LayoutTest
91 def hello
92 prepend_view_path File.dirname(__FILE__) + '/../fixtures/layout_tests/alt/'
93 render :layout => 'alt'
94 end
95 end
96
97 class SetsLayoutInRenderController < LayoutTest
98 def hello
99 render :layout => 'third_party_template_library'
100 end
101 end
102
103 class RendersNoLayoutController < LayoutTest
104 def hello
105 render :layout => false
106 end
107 end
108
109 class LayoutSetInResponseTest < ActionController::TestCase
110 def test_layout_set_when_using_default_layout
111 @controller = DefaultLayoutController.new
112 get :hello
113 assert_equal 'layouts/layout_test', @response.layout
114 end
115
116 def test_layout_set_when_set_in_controller
117 @controller = HasOwnLayoutController.new
118 get :hello
119 assert_equal 'layouts/item', @response.layout
120 end
121
122 def test_layout_set_when_using_render
123 @controller = SetsLayoutInRenderController.new
124 get :hello
125 assert_equal 'layouts/third_party_template_library', @response.layout
126 end
127
128 def test_layout_is_not_set_when_none_rendered
129 @controller = RendersNoLayoutController.new
130 get :hello
131 assert_nil @response.layout
132 end
133
134 def test_exempt_from_layout_honored_by_render_template
135 ActionController::Base.exempt_from_layout :rhtml
136 @controller = RenderWithTemplateOptionController.new
137
138 get :hello
139 assert_equal "alt/hello.rhtml", @response.body.strip
140
141 ensure
142 ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
143 end
144
145 def test_layout_is_picked_from_the_controller_instances_view_path
146 @controller = PrependsViewPathController.new
147 get :hello
148 assert_equal 'layouts/alt', @response.layout
149 end
150
151 def test_absolute_pathed_layout
152 @controller = AbsolutePathLayoutController.new
153 get :hello
154 assert_equal "layout_test.rhtml hello.rhtml", @response.body.strip
155 end
156 end
157
158 class RenderWithTemplateOptionController < LayoutTest
159 def hello
160 render :template => 'alt/hello'
161 end
162 end
163
164 class SetsNonExistentLayoutFile < LayoutTest
165 layout "nofile.rhtml"
166 end
167
168 class LayoutExceptionRaised < ActionController::TestCase
169 def test_exception_raised_when_layout_file_not_found
170 @controller = SetsNonExistentLayoutFile.new
171 get :hello
172 assert_kind_of ActionView::MissingTemplate, @response.template.instance_eval { @exception }
173 end
174 end
175
176 class LayoutStatusIsRendered < LayoutTest
177 def hello
178 render :status => 401
179 end
180 end
181
182 class LayoutStatusIsRenderedTest < ActionController::TestCase
183 def test_layout_status_is_rendered
184 @controller = LayoutStatusIsRendered.new
185 get :hello
186 assert_response 401
187 end
188 end
189
190 unless RUBY_PLATFORM =~ /(:?mswin|mingw|bccwin)/
191 class LayoutSymlinkedTest < LayoutTest
192 layout "symlinked/symlinked_layout"
193 end
194
195 class LayoutSymlinkedIsRenderedTest < ActionController::TestCase
196 def test_symlinked_layout_is_rendered
197 @controller = LayoutSymlinkedTest.new
198 get :hello
199 assert_response 200
200 assert_equal "layouts/symlinked/symlinked_layout", @response.layout
201 end
202 end
203 end
204