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