PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/rails/actionpack/test/controller/layout_test.rb

https://github.com/dosire/dosire
Ruby | 259 lines | 203 code | 53 blank | 3 comment | 0 complexity | 4678bedfc7d7ca3576b8d2c0b695d749 MD5 | raw file
Possible License(s): MIT, GPL-2.0, BSD-3-Clause
  1. require 'abstract_unit'
  2. # The view_paths array must be set on Base and not LayoutTest so that LayoutTest's inherited
  3. # method has access to the view_paths array when looking for a layout to automatically assign.
  4. old_load_paths = ActionController::Base.view_paths
  5. ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/layout_tests/' ]
  6. class LayoutTest < ActionController::Base
  7. def self.controller_path; 'views' end
  8. self.view_paths = ActionController::Base.view_paths.dup
  9. end
  10. # Restore view_paths to previous value
  11. ActionController::Base.view_paths = old_load_paths
  12. class ProductController < LayoutTest
  13. end
  14. class ItemController < LayoutTest
  15. end
  16. class ThirdPartyTemplateLibraryController < LayoutTest
  17. end
  18. module ControllerNameSpace
  19. end
  20. class ControllerNameSpace::NestedController < LayoutTest
  21. end
  22. class MultipleExtensions < LayoutTest
  23. end
  24. class MabView < ActionView::TemplateHandler
  25. def initialize(view)
  26. end
  27. def render(template)
  28. template.source
  29. end
  30. end
  31. ActionView::Template::register_template_handler :mab, MabView
  32. class LayoutAutoDiscoveryTest < Test::Unit::TestCase
  33. def setup
  34. @request = ActionController::TestRequest.new
  35. @response = ActionController::TestResponse.new
  36. @request.host = "www.nextangle.com"
  37. end
  38. def test_application_layout_is_default_when_no_controller_match
  39. @controller = ProductController.new
  40. get :hello
  41. assert_equal 'layout_test.rhtml hello.rhtml', @response.body
  42. end
  43. def test_controller_name_layout_name_match
  44. @controller = ItemController.new
  45. get :hello
  46. assert_equal 'item.rhtml hello.rhtml', @response.body
  47. end
  48. def test_third_party_template_library_auto_discovers_layout
  49. @controller = ThirdPartyTemplateLibraryController.new
  50. get :hello
  51. assert_equal 'layouts/third_party_template_library', @controller.active_layout
  52. assert_equal 'layouts/third_party_template_library', @response.layout
  53. assert_response :success
  54. assert_equal 'Mab', @response.body
  55. end
  56. def test_namespaced_controllers_auto_detect_layouts
  57. @controller = ControllerNameSpace::NestedController.new
  58. get :hello
  59. assert_equal 'layouts/controller_name_space/nested', @controller.active_layout
  60. assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body
  61. end
  62. def test_namespaced_controllers_auto_detect_layouts
  63. @controller = MultipleExtensions.new
  64. get :hello
  65. assert_equal 'layouts/multiple_extensions', @controller.active_layout
  66. assert_equal 'multiple_extensions.html.erb hello.rhtml', @response.body.strip
  67. end
  68. end
  69. class ExemptFromLayoutTest < Test::Unit::TestCase
  70. def setup
  71. @controller = LayoutTest.new
  72. @request = ActionController::TestRequest.new
  73. @response = ActionController::TestResponse.new
  74. end
  75. def test_rjs_exempt_from_layout
  76. assert @controller.send!(:template_exempt_from_layout?, 'test.rjs')
  77. end
  78. def test_rhtml_and_rxml_not_exempt_from_layout
  79. assert !@controller.send!(:template_exempt_from_layout?, 'test.rhtml')
  80. assert !@controller.send!(:template_exempt_from_layout?, 'test.rxml')
  81. end
  82. def test_other_extension_not_exempt_from_layout
  83. assert !@controller.send!(:template_exempt_from_layout?, 'test.random')
  84. end
  85. def test_add_extension_to_exempt_from_layout
  86. ['rpdf', :rpdf].each do |ext|
  87. assert_nothing_raised do
  88. ActionController::Base.exempt_from_layout ext
  89. end
  90. assert @controller.send!(:template_exempt_from_layout?, "test.#{ext}")
  91. end
  92. end
  93. def test_add_regexp_to_exempt_from_layout
  94. ActionController::Base.exempt_from_layout /\.rdoc/
  95. assert @controller.send!(:template_exempt_from_layout?, 'test.rdoc')
  96. end
  97. def test_rhtml_exempt_from_layout_status_should_prevent_layout_render
  98. ActionController::Base.exempt_from_layout :rhtml
  99. assert @controller.send!(:template_exempt_from_layout?, 'test.rhtml')
  100. assert @controller.send!(:template_exempt_from_layout?, 'hello.rhtml')
  101. get :hello
  102. assert_equal 'hello.rhtml', @response.body
  103. ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
  104. end
  105. end
  106. class DefaultLayoutController < LayoutTest
  107. end
  108. class HasOwnLayoutController < LayoutTest
  109. layout 'item'
  110. end
  111. class SetsLayoutInRenderController < LayoutTest
  112. def hello
  113. render :layout => 'third_party_template_library'
  114. end
  115. end
  116. class RendersNoLayoutController < LayoutTest
  117. def hello
  118. render :layout => false
  119. end
  120. end
  121. class LayoutSetInResponseTest < Test::Unit::TestCase
  122. def setup
  123. @request = ActionController::TestRequest.new
  124. @response = ActionController::TestResponse.new
  125. end
  126. def test_layout_set_when_using_default_layout
  127. @controller = DefaultLayoutController.new
  128. get :hello
  129. assert_equal 'layouts/layout_test', @response.layout
  130. end
  131. def test_layout_set_when_set_in_controller
  132. @controller = HasOwnLayoutController.new
  133. get :hello
  134. assert_equal 'layouts/item', @response.layout
  135. end
  136. def test_layout_set_when_using_render
  137. @controller = SetsLayoutInRenderController.new
  138. get :hello
  139. assert_equal 'layouts/third_party_template_library', @response.layout
  140. end
  141. def test_layout_is_not_set_when_none_rendered
  142. @controller = RendersNoLayoutController.new
  143. get :hello
  144. assert_nil @response.layout
  145. end
  146. def test_exempt_from_layout_honored_by_render_template
  147. ActionController::Base.exempt_from_layout :rhtml
  148. @controller = RenderWithTemplateOptionController.new
  149. assert @controller.send(:template_exempt_from_layout?, 'alt/hello.rhtml')
  150. get :hello
  151. assert_equal "alt/hello.rhtml", @response.body.strip
  152. ensure
  153. ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
  154. end
  155. end
  156. class RenderWithTemplateOptionController < LayoutTest
  157. def hello
  158. render :template => 'alt/hello'
  159. end
  160. end
  161. class SetsNonExistentLayoutFile < LayoutTest
  162. layout "nofile.rhtml"
  163. end
  164. class LayoutExceptionRaised < Test::Unit::TestCase
  165. def setup
  166. @request = ActionController::TestRequest.new
  167. @response = ActionController::TestResponse.new
  168. end
  169. def test_exception_raised_when_layout_file_not_found
  170. @controller = SetsNonExistentLayoutFile.new
  171. get :hello
  172. @response.template.class.module_eval { attr_accessor :exception }
  173. assert_equal ActionView::MissingTemplate, @response.template.exception.class
  174. end
  175. end
  176. class LayoutStatusIsRendered < LayoutTest
  177. def hello
  178. render :status => 401
  179. end
  180. end
  181. class LayoutStatusIsRenderedTest < Test::Unit::TestCase
  182. def setup
  183. @request = ActionController::TestRequest.new
  184. @response = ActionController::TestResponse.new
  185. end
  186. def test_layout_status_is_rendered
  187. @controller = LayoutStatusIsRendered.new
  188. get :hello
  189. assert_response 401
  190. end
  191. end
  192. class LayoutSymlinkedTest < LayoutTest
  193. layout "symlinked/symlinked_layout"
  194. end
  195. class LayoutSymlinkedIsRenderedTest < Test::Unit::TestCase
  196. def setup
  197. @request = ActionController::TestRequest.new
  198. @response = ActionController::TestResponse.new
  199. end
  200. def test_symlinked_layout_is_rendered
  201. @controller = LayoutSymlinkedTest.new
  202. get :hello
  203. assert_response 200
  204. assert_equal "layouts/symlinked/symlinked_layout", @response.layout
  205. end
  206. end