Module: Scrivito::TestRequest

Defined in:
app/cms/scrivito/test_request.rb

Instance Method Summary collapse

Instance Method Details

#for_scrivito_obj(test_obj = nil, routes = nil) ⇒ Object

Make a test request to act as a CMS request. This is necessary for testing controllers that include ControllerActions, because these controllers do not have explicit routes by default.

Examples:

class MyPageControllerTest < ActionController::TestCase
  test 'should respond with success' do
    request.for_scrivito_obj
    get :index
    assert_response :success
  end
end

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/cms/scrivito/test_request.rb', line 29

def for_scrivito_obj(test_obj = nil, routes = nil)
  env[Scrivito::CmsEnv::OBJ_ENV_KEY] = test_obj if test_obj

  # 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

  def self.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

#for_scrivito_user(test_user) ⇒ Object

Make a test request to act as it’s by a specific Scrivito user

Parameters:



69
70
71
# File 'app/cms/scrivito/test_request.rb', line 69

def for_scrivito_user(test_user)
  env[EditingContextMiddleware::ENVKEY] = EditingContext.new(editor: -> { test_user })
end