module Scrivito # # @api public # module TestRequest ROUTING_ERRORS = [ AbstractController::ActionNotFound, ActionController::RoutingError, ActionController::UrlGenerationError, ] # # Make a test request to act as a CMS request. # This is necessary for testing controllers that include {Scrivito::ControllerActions}, because # these controllers do not have explicit routes by default. # # @api public # @param [Scrivito::BasicObj Obj] test_obj requested +Obj+ # # @example # class MyPageControllerTest < ActionController::TestCase # test 'should respond with success' do # request.for_scrivito_obj # get :index # assert_response :success # end # end # def for_scrivito_obj(test_obj = nil, routes = nil) env[Scrivito::CmsEnv::OBJ_ENV_KEY] = test_obj if test_obj unless Scrivito::LegacySwitch.rails4? # If +assign_parameters+ is monkey patched, then +generate_extras+ also needs to # be monkey patched. See [1] for details. # # [1] https://github.com/rails/rails/commit/f6232a518bb7377948f339d2b0b25dc607e1e42a class << (routes || Rails.application.routes) def generate_extras(options, recall={}) super options, recall rescue *ROUTING_ERRORS => e begin super options.merge(controller: 'scrivito/cms_dispatch'), recall rescue *ROUTING_ERRORS raise e end end end end class << self def assign_parameters(routes, controller_path, *args) super routes, controller_path, *args rescue *ROUTING_ERRORS => e begin super routes, 'scrivito/cms_dispatch', *args rescue *ROUTING_ERRORS raise e end end end end def for_cms_object(*args) raise "The method `for_cms_object' was removed. Please use `for_scrivito_obj' instead" end # # Make a test request to act as it's by a specific Scrivito user # # @api public # @param [Scrivito::User] test_user requested +User+ # def for_scrivito_user(test_user) env[EditingContextMiddleware::ENVKEY] = EditingContext.new(editor: -> { test_user }) end end end