PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/bricooke/my-biz-expenses
Ruby | 187 lines | 145 code | 39 blank | 3 comment | 0 complexity | a898147f09eeb7cfb3f17eed7db86ec5 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause
  1. require File.dirname(__FILE__) + '/../abstract_unit'
  2. # The template_root must be set on Base and not LayoutTest so that LayoutTest's inherited method has access to
  3. # the template_root when looking for a layout
  4. ActionController::Base.template_root = File.dirname(__FILE__) + '/../fixtures/layout_tests/'
  5. class LayoutTest < ActionController::Base
  6. def self.controller_path; 'views' end
  7. end
  8. # Restore template root to be unset
  9. ActionController::Base.template_root = nil
  10. class ProductController < LayoutTest
  11. end
  12. class ItemController < LayoutTest
  13. end
  14. class ThirdPartyTemplateLibraryController < LayoutTest
  15. end
  16. module ControllerNameSpace
  17. end
  18. class ControllerNameSpace::NestedController < LayoutTest
  19. end
  20. class MabView
  21. def initialize(view)
  22. end
  23. def render(text, locals = {})
  24. text
  25. end
  26. end
  27. ActionView::Base::register_template_handler :mab, MabView
  28. class LayoutAutoDiscoveryTest < Test::Unit::TestCase
  29. def setup
  30. @request = ActionController::TestRequest.new
  31. @response = ActionController::TestResponse.new
  32. @request.host = "www.nextangle.com"
  33. end
  34. def test_application_layout_is_default_when_no_controller_match
  35. @controller = ProductController.new
  36. get :hello
  37. assert_equal 'layout_test.rhtml hello.rhtml', @response.body
  38. end
  39. def test_controller_name_layout_name_match
  40. @controller = ItemController.new
  41. get :hello
  42. assert_equal 'item.rhtml hello.rhtml', @response.body
  43. end
  44. def test_third_party_template_library_auto_discovers_layout
  45. @controller = ThirdPartyTemplateLibraryController.new
  46. get :hello
  47. assert_equal 'layouts/third_party_template_library', @controller.active_layout
  48. assert_equal 'Mab', @response.body
  49. end
  50. def test_namespaced_controllers_auto_detect_layouts
  51. @controller = ControllerNameSpace::NestedController.new
  52. get :hello
  53. assert_equal 'layouts/controller_name_space/nested', @controller.active_layout
  54. assert_equal 'controller_name_space/nested.rhtml hello.rhtml', @response.body
  55. end
  56. end
  57. class ExemptFromLayoutTest < Test::Unit::TestCase
  58. def setup
  59. @controller = LayoutTest.new
  60. @request = ActionController::TestRequest.new
  61. @response = ActionController::TestResponse.new
  62. end
  63. def test_rjs_exempt_from_layout
  64. assert @controller.send(:template_exempt_from_layout?, 'test.rjs')
  65. end
  66. def test_rhtml_and_rxml_not_exempt_from_layout
  67. assert !@controller.send(:template_exempt_from_layout?, 'test.rhtml')
  68. assert !@controller.send(:template_exempt_from_layout?, 'test.rxml')
  69. end
  70. def test_other_extension_not_exempt_from_layout
  71. assert !@controller.send(:template_exempt_from_layout?, 'test.random')
  72. end
  73. def test_add_extension_to_exempt_from_layout
  74. ['rpdf', :rpdf].each do |ext|
  75. assert_nothing_raised do
  76. ActionController::Base.exempt_from_layout ext
  77. end
  78. assert @controller.send(:template_exempt_from_layout?, "test.#{ext}")
  79. end
  80. end
  81. def test_add_regexp_to_exempt_from_layout
  82. ActionController::Base.exempt_from_layout /\.rdoc/
  83. assert @controller.send(:template_exempt_from_layout?, 'test.rdoc')
  84. end
  85. def test_rhtml_exempt_from_layout_status_should_prevent_layout_render
  86. ActionController::Base.exempt_from_layout :rhtml
  87. assert @controller.send(:template_exempt_from_layout?, 'test.rhtml')
  88. get :hello
  89. assert_equal 'hello.rhtml', @response.body
  90. ActionController::Base.exempt_from_layout.delete(/\.rhtml$/)
  91. end
  92. end
  93. class DefaultLayoutController < LayoutTest
  94. end
  95. class HasOwnLayoutController < LayoutTest
  96. layout 'item'
  97. end
  98. class SetsLayoutInRenderController < LayoutTest
  99. def hello
  100. render :layout => 'third_party_template_library'
  101. end
  102. end
  103. class RendersNoLayoutController < LayoutTest
  104. def hello
  105. render :layout => false
  106. end
  107. end
  108. class LayoutSetInResponseTest < Test::Unit::TestCase
  109. def setup
  110. @request = ActionController::TestRequest.new
  111. @response = ActionController::TestResponse.new
  112. end
  113. def test_layout_set_when_using_default_layout
  114. @controller = DefaultLayoutController.new
  115. get :hello
  116. assert_equal 'layouts/layout_test', @response.layout
  117. end
  118. def test_layout_set_when_set_in_controller
  119. @controller = HasOwnLayoutController.new
  120. get :hello
  121. assert_equal 'layouts/item', @response.layout
  122. end
  123. def test_layout_set_when_using_render
  124. @controller = SetsLayoutInRenderController.new
  125. get :hello
  126. assert_equal 'layouts/third_party_template_library', @response.layout
  127. end
  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. end
  134. class SetsNonExistentLayoutFile < LayoutTest
  135. layout "nofile.rhtml"
  136. end
  137. class LayoutExceptionRaised < Test::Unit::TestCase
  138. def setup
  139. @request = ActionController::TestRequest.new
  140. @response = ActionController::TestResponse.new
  141. end
  142. def test_exception_raised_when_layout_file_not_found
  143. @controller = SetsNonExistentLayoutFile.new
  144. get :hello
  145. @response.template.class.module_eval { attr_accessor :exception }
  146. assert_equal ActionController::MissingTemplate, @response.template.exception.class
  147. end
  148. end