2017-07-23 15:36:41 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-26 03:42:21 +00:00
|
|
|
require "active_support/core_ext/module/redefine_method"
|
2016-08-06 16:48:35 +00:00
|
|
|
require "action_controller"
|
|
|
|
require "action_controller/test_case"
|
|
|
|
require "action_view"
|
2010-01-31 19:37:43 +00:00
|
|
|
|
2016-08-06 16:48:35 +00:00
|
|
|
require "rails-dom-testing"
|
2013-10-11 11:02:59 +00:00
|
|
|
|
2008-04-19 18:06:57 +00:00
|
|
|
module ActionView
|
2010-06-20 20:26:31 +00:00
|
|
|
# = Action View Test Case
|
2008-04-19 18:06:57 +00:00
|
|
|
class TestCase < ActiveSupport::TestCase
|
2009-09-28 18:31:30 +00:00
|
|
|
class TestController < ActionController::Base
|
2010-03-17 23:28:05 +00:00
|
|
|
include ActionDispatch::TestProcess
|
|
|
|
|
2009-09-28 18:31:30 +00:00
|
|
|
attr_accessor :request, :response, :params
|
|
|
|
|
2010-05-25 05:34:55 +00:00
|
|
|
class << self
|
2016-05-21 02:28:00 +00:00
|
|
|
attr_writer :controller_path
|
2010-05-25 05:34:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def controller_path=(path)
|
2016-10-29 03:05:58 +00:00
|
|
|
self.class.controller_path = (path)
|
2009-09-28 18:31:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def initialize
|
2010-09-29 09:18:31 +00:00
|
|
|
super
|
2010-05-25 05:34:55 +00:00
|
|
|
self.class.controller_path = ""
|
2016-08-11 05:01:32 +00:00
|
|
|
@request = ActionController::TestRequest.create(self.class)
|
2015-07-14 13:27:42 +00:00
|
|
|
@response = ActionDispatch::TestResponse.new
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2016-08-06 16:48:35 +00:00
|
|
|
@request.env.delete("PATH_INFO")
|
2015-12-29 15:37:07 +00:00
|
|
|
@params = ActionController::Parameters.new
|
2009-09-28 18:31:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
module Behavior
|
|
|
|
extend ActiveSupport::Concern
|
2008-11-07 20:42:34 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
include ActionDispatch::Assertions, ActionDispatch::TestProcess
|
2013-10-11 11:02:59 +00:00
|
|
|
include Rails::Dom::Testing::Assertions
|
2010-05-04 14:00:46 +00:00
|
|
|
include ActionController::TemplateAssertions
|
|
|
|
include ActionView::Context
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2010-07-20 19:07:59 +00:00
|
|
|
include ActionDispatch::Routing::PolymorphicRoutes
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
include AbstractController::Helpers
|
|
|
|
include ActionView::Helpers
|
2012-06-09 12:20:31 +00:00
|
|
|
include ActionView::RecordIdentifier
|
2012-06-21 16:39:35 +00:00
|
|
|
include ActionView::RoutingUrlFor
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2012-09-25 21:39:29 +00:00
|
|
|
include ActiveSupport::Testing::ConstantLookup
|
|
|
|
|
2016-08-06 17:36:34 +00:00
|
|
|
delegate :lookup_context, to: :controller
|
2010-05-04 14:00:46 +00:00
|
|
|
attr_accessor :controller, :output_buffer, :rendered
|
2010-08-14 05:13:00 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
module ClassMethods
|
|
|
|
def tests(helper_class)
|
2011-10-03 08:05:25 +00:00
|
|
|
case helper_class
|
|
|
|
when String, Symbol
|
|
|
|
self.helper_class = "#{helper_class.to_s.underscore}_helper".camelize.safe_constantize
|
|
|
|
when Module
|
|
|
|
self.helper_class = helper_class
|
|
|
|
end
|
2010-05-04 14:00:46 +00:00
|
|
|
end
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def determine_default_helper_class(name)
|
2012-09-24 21:37:45 +00:00
|
|
|
determine_constant_from_test_name(name) do |constant|
|
|
|
|
Module === constant && !(Class === constant)
|
|
|
|
end
|
2010-05-04 14:00:46 +00:00
|
|
|
end
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def helper_method(*methods)
|
|
|
|
# Almost a duplicate from ActionController::Helpers
|
|
|
|
methods.flatten.each do |method|
|
2017-05-23 06:08:59 +00:00
|
|
|
_helpers.module_eval <<-end_eval, __FILE__, __LINE__ + 1
|
2010-05-04 14:00:46 +00:00
|
|
|
def #{method}(*args, &block) # def current_user(*args, &block)
|
2011-10-03 06:36:13 +00:00
|
|
|
_test_case.send(%(#{method}), *args, &block) # _test_case.send(%(current_user), *args, &block)
|
2010-05-04 14:00:46 +00:00
|
|
|
end # end
|
|
|
|
end_eval
|
|
|
|
end
|
|
|
|
end
|
2010-03-04 01:36:08 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
attr_writer :helper_class
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def helper_class
|
|
|
|
@helper_class ||= determine_default_helper_class(name)
|
|
|
|
end
|
2008-04-19 18:06:57 +00:00
|
|
|
|
2010-09-13 19:51:42 +00:00
|
|
|
def new(*)
|
|
|
|
include_helper_modules!
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
private
|
2008-04-19 18:06:57 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def include_helper_modules!
|
|
|
|
helper(helper_class) if helper_class
|
|
|
|
include _helpers
|
2008-04-19 18:06:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def setup_with_controller
|
|
|
|
@controller = ActionView::TestCase::TestController.new
|
2010-06-19 03:20:10 +00:00
|
|
|
@request = @controller.request
|
2017-07-26 02:27:11 +00:00
|
|
|
@view_flow = ActionView::OutputFlow.new
|
2014-05-27 20:22:06 +00:00
|
|
|
# empty string ensures buffer has UTF-8 encoding as
|
|
|
|
# new without arguments returns ASCII-8BIT encoded buffer like String#new
|
2016-08-06 16:48:35 +00:00
|
|
|
@output_buffer = ActiveSupport::SafeBuffer.new ""
|
2018-05-17 08:32:27 +00:00
|
|
|
@rendered = +""
|
2010-05-04 14:00:46 +00:00
|
|
|
|
|
|
|
make_test_case_available_to_view!
|
2010-05-26 04:46:00 +00:00
|
|
|
say_no_to_protect_against_forgery!
|
2008-04-19 18:06:57 +00:00
|
|
|
end
|
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def config
|
|
|
|
@controller.config if @controller.respond_to?(:config)
|
2008-05-26 08:38:56 +00:00
|
|
|
end
|
2008-06-09 03:05:39 +00:00
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
def render(options = {}, local_assigns = {}, &block)
|
2010-10-02 17:35:17 +00:00
|
|
|
view.assign(view_assigns)
|
2010-06-23 15:19:13 +00:00
|
|
|
@rendered << output = view.render(options, local_assigns, &block)
|
2010-05-04 14:00:46 +00:00
|
|
|
output
|
|
|
|
end
|
|
|
|
|
2012-10-09 19:14:11 +00:00
|
|
|
def rendered_views
|
|
|
|
@_rendered_views ||= RenderedViewsCollection.new
|
|
|
|
end
|
|
|
|
|
2017-02-20 20:22:42 +00:00
|
|
|
def _routes
|
|
|
|
@controller._routes if @controller.respond_to?(:_routes)
|
|
|
|
end
|
|
|
|
|
2014-11-17 18:37:17 +00:00
|
|
|
# Need to experiment if this priority is the best one: rendered => output_buffer
|
2012-10-09 19:14:11 +00:00
|
|
|
class RenderedViewsCollection
|
|
|
|
def initialize
|
2012-12-15 19:21:40 +00:00
|
|
|
@rendered_views ||= Hash.new { |hash, key| hash[key] = [] }
|
2012-10-09 19:14:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def add(view, locals)
|
|
|
|
@rendered_views[view] ||= []
|
|
|
|
@rendered_views[view] << locals
|
|
|
|
end
|
|
|
|
|
|
|
|
def locals_for(view)
|
|
|
|
@rendered_views[view]
|
|
|
|
end
|
|
|
|
|
2012-12-15 19:20:34 +00:00
|
|
|
def rendered_views
|
|
|
|
@rendered_views.keys
|
|
|
|
end
|
|
|
|
|
2012-10-09 19:14:11 +00:00
|
|
|
def view_rendered?(view, expected_locals)
|
|
|
|
locals_for(view).any? do |actual_locals|
|
2016-08-16 07:30:11 +00:00
|
|
|
expected_locals.all? { |key, value| value == actual_locals[key] }
|
2012-10-09 19:14:11 +00:00
|
|
|
end
|
|
|
|
end
|
2010-06-22 06:40:22 +00:00
|
|
|
end
|
|
|
|
|
2010-05-04 14:00:46 +00:00
|
|
|
included do
|
|
|
|
setup :setup_with_controller
|
2016-08-25 07:22:48 +00:00
|
|
|
ActiveSupport.run_load_hooks(:action_view_test_case, self)
|
2010-05-04 14:00:46 +00:00
|
|
|
end
|
2008-04-19 18:06:57 +00:00
|
|
|
|
2009-09-28 18:31:30 +00:00
|
|
|
private
|
2010-05-04 14:00:46 +00:00
|
|
|
|
2014-11-18 20:47:22 +00:00
|
|
|
# Need to experiment if this priority is the best one: rendered => output_buffer
|
|
|
|
def document_root_element
|
|
|
|
Nokogiri::HTML::Document.parse(@rendered.blank? ? @output_buffer : @rendered).root
|
|
|
|
end
|
|
|
|
|
2010-05-26 04:46:00 +00:00
|
|
|
def say_no_to_protect_against_forgery!
|
|
|
|
_helpers.module_eval do
|
2017-05-26 03:42:21 +00:00
|
|
|
silence_redefinition_of_method :protect_against_forgery?
|
2010-05-26 04:46:00 +00:00
|
|
|
def protect_against_forgery?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-09-28 18:31:30 +00:00
|
|
|
def make_test_case_available_to_view!
|
|
|
|
test_case_instance = self
|
|
|
|
_helpers.module_eval do
|
2010-09-28 18:21:44 +00:00
|
|
|
unless private_method_defined?(:_test_case)
|
|
|
|
define_method(:_test_case) { test_case_instance }
|
|
|
|
private :_test_case
|
|
|
|
end
|
2009-09-28 18:31:30 +00:00
|
|
|
end
|
|
|
|
end
|
2008-04-19 18:06:57 +00:00
|
|
|
|
2010-06-22 06:40:22 +00:00
|
|
|
module Locals
|
2012-10-09 19:14:11 +00:00
|
|
|
attr_accessor :rendered_views
|
2010-06-22 06:40:22 +00:00
|
|
|
|
2011-05-01 09:14:38 +00:00
|
|
|
def render(options = {}, local_assigns = {})
|
|
|
|
case options
|
|
|
|
when Hash
|
|
|
|
if block_given?
|
2012-10-09 19:14:11 +00:00
|
|
|
rendered_views.add options[:layout], options[:locals]
|
2011-05-01 09:14:38 +00:00
|
|
|
elsif options.key?(:partial)
|
2012-10-09 19:14:11 +00:00
|
|
|
rendered_views.add options[:partial], options[:locals]
|
2011-05-01 09:14:38 +00:00
|
|
|
end
|
|
|
|
else
|
2012-10-09 19:14:11 +00:00
|
|
|
rendered_views.add options, local_assigns
|
2011-05-01 09:14:38 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
super
|
2010-06-22 06:40:22 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-23 15:19:13 +00:00
|
|
|
# The instance of ActionView::Base that is used by +render+.
|
|
|
|
def view
|
|
|
|
@view ||= begin
|
2011-01-12 18:44:56 +00:00
|
|
|
view = @controller.view_context
|
2015-02-01 04:12:37 +00:00
|
|
|
view.singleton_class.include(_helpers)
|
2010-09-29 09:18:31 +00:00
|
|
|
view.extend(Locals)
|
2017-01-05 08:20:57 +00:00
|
|
|
view.rendered_views = rendered_views
|
|
|
|
view.output_buffer = output_buffer
|
2010-09-29 09:18:31 +00:00
|
|
|
view
|
|
|
|
end
|
2009-09-28 18:31:30 +00:00
|
|
|
end
|
2009-08-10 06:54:17 +00:00
|
|
|
|
2010-06-23 15:19:13 +00:00
|
|
|
alias_method :_view, :view
|
|
|
|
|
2012-01-07 12:30:05 +00:00
|
|
|
INTERNAL_IVARS = [
|
2013-05-07 00:38:45 +00:00
|
|
|
:@NAME,
|
|
|
|
:@failures,
|
|
|
|
:@assertions,
|
2012-01-07 12:30:05 +00:00
|
|
|
:@__io__,
|
|
|
|
:@_assertion_wrapped,
|
|
|
|
:@_assertions,
|
|
|
|
:@_result,
|
|
|
|
:@_routes,
|
|
|
|
:@controller,
|
2012-09-30 08:54:50 +00:00
|
|
|
:@_layouts,
|
2013-02-27 15:44:25 +00:00
|
|
|
:@_files,
|
2012-10-09 19:14:11 +00:00
|
|
|
:@_rendered_views,
|
2012-01-07 12:30:05 +00:00
|
|
|
:@method_name,
|
|
|
|
:@output_buffer,
|
2012-09-30 08:54:50 +00:00
|
|
|
:@_partials,
|
2012-01-07 12:30:05 +00:00
|
|
|
:@passed,
|
|
|
|
:@rendered,
|
|
|
|
:@request,
|
|
|
|
:@routes,
|
2012-09-27 21:45:48 +00:00
|
|
|
:@tagged_logger,
|
2012-09-30 08:54:50 +00:00
|
|
|
:@_templates,
|
2012-01-07 12:30:05 +00:00
|
|
|
:@options,
|
|
|
|
:@test_passed,
|
|
|
|
:@view,
|
2014-04-08 04:42:27 +00:00
|
|
|
:@view_context_class,
|
2017-07-26 02:27:11 +00:00
|
|
|
:@view_flow,
|
2013-10-11 21:29:08 +00:00
|
|
|
:@_subscribers,
|
|
|
|
:@html_document
|
2012-01-07 12:30:05 +00:00
|
|
|
]
|
2009-09-28 18:31:30 +00:00
|
|
|
|
2010-10-02 17:35:17 +00:00
|
|
|
def _user_defined_ivars
|
2012-01-07 12:30:05 +00:00
|
|
|
instance_variables - INTERNAL_IVARS
|
2008-04-19 18:06:57 +00:00
|
|
|
end
|
|
|
|
|
2010-10-02 17:35:17 +00:00
|
|
|
# Returns a Hash of instance variables and their values, as defined by
|
|
|
|
# the user in the test case, which are then assigned to the view being
|
|
|
|
# rendered. This is generally intended for internal use and extension
|
|
|
|
# frameworks.
|
|
|
|
def view_assigns
|
2012-01-07 12:30:05 +00:00
|
|
|
Hash[_user_defined_ivars.map do |ivar|
|
|
|
|
[ivar[1..-1].to_sym, instance_variable_get(ivar)]
|
2010-10-03 15:34:34 +00:00
|
|
|
end]
|
2009-09-28 18:31:30 +00:00
|
|
|
end
|
2008-06-09 03:05:39 +00:00
|
|
|
|
2008-04-19 18:06:57 +00:00
|
|
|
def method_missing(selector, *args)
|
2015-08-10 18:01:18 +00:00
|
|
|
begin
|
|
|
|
routes = @controller.respond_to?(:_routes) && @controller._routes
|
|
|
|
rescue
|
2017-10-11 03:08:03 +00:00
|
|
|
# Don't call routes, if there is an error on _routes call
|
2015-08-10 18:01:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if routes &&
|
2016-10-29 03:05:58 +00:00
|
|
|
(routes.named_routes.route_defined?(selector) ||
|
|
|
|
routes.mounted_helpers.method_defined?(selector))
|
2009-09-28 18:31:30 +00:00
|
|
|
@controller.__send__(selector, *args)
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2008-04-19 18:06:57 +00:00
|
|
|
end
|
2017-08-03 20:22:58 +00:00
|
|
|
|
|
|
|
def respond_to_missing?(name, include_private = false)
|
|
|
|
begin
|
|
|
|
routes = @controller.respond_to?(:_routes) && @controller._routes
|
|
|
|
rescue
|
2017-10-11 03:08:03 +00:00
|
|
|
# Don't call routes, if there is an error on _routes call
|
2017-08-03 20:22:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
routes &&
|
|
|
|
(routes.named_routes.route_defined?(name) ||
|
|
|
|
routes.mounted_helpers.method_defined?(name))
|
|
|
|
end
|
2010-05-04 14:00:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
include Behavior
|
2008-04-19 18:06:57 +00:00
|
|
|
end
|
|
|
|
end
|