2017-07-23 11:36:41 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
require "abstract_unit"
|
2010-06-24 07:23:43 -04:00
|
|
|
require "active_support/log_subscriber/test_helper"
|
|
|
|
require "action_view/log_subscriber"
|
2010-01-12 18:41:04 -05:00
|
|
|
require "controller/fake_models"
|
|
|
|
|
2010-02-15 09:44:30 -05:00
|
|
|
class AVLogSubscriberTest < ActiveSupport::TestCase
|
2010-06-24 07:23:43 -04:00
|
|
|
include ActiveSupport::LogSubscriber::TestHelper
|
2010-01-12 18:41:04 -05:00
|
|
|
|
|
|
|
def setup
|
2010-03-02 17:40:59 -05:00
|
|
|
super
|
2017-06-07 16:17:34 -04:00
|
|
|
|
2019-02-08 17:43:22 -05:00
|
|
|
ActionView::LookupContext::DetailsKey.clear
|
|
|
|
|
2019-01-23 19:11:54 -05:00
|
|
|
view_paths = ActionController::Base.view_paths
|
2019-01-23 17:19:50 -05:00
|
|
|
|
2012-06-14 06:04:49 -04:00
|
|
|
lookup_context = ActionView::LookupContext.new(view_paths, {}, ["test"])
|
2020-05-06 16:49:50 -04:00
|
|
|
@view = ActionView::Base.with_empty_template_cache.with_context(lookup_context)
|
2017-06-07 16:17:34 -04:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
ActionView::LogSubscriber.attach_to :action_view
|
2017-06-07 16:17:34 -04:00
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
unless Rails.respond_to?(:root)
|
|
|
|
@defined_root = true
|
|
|
|
def Rails.root; :defined_root; end # Minitest `stub` expects the method to be defined.
|
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
super
|
2020-07-29 23:11:19 -04:00
|
|
|
ActionController::Base.view_paths.map(&:clear_cache)
|
2017-06-07 16:17:34 -04:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
ActiveSupport::LogSubscriber.log_subscribers.clear
|
2017-06-07 16:17:34 -04:00
|
|
|
|
2015-08-22 00:03:53 -04:00
|
|
|
# We need to undef `root`, RenderTestCases don't want this to be defined
|
2017-11-01 20:58:54 -04:00
|
|
|
Rails.instance_eval { undef :root } if defined?(@defined_root)
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_logger(logger)
|
2012-01-18 12:05:36 -05:00
|
|
|
ActionView::Base.logger = logger
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
2016-07-14 06:38:16 -04:00
|
|
|
def set_cache_controller
|
|
|
|
controller = ActionController::Base.new
|
|
|
|
controller.perform_caching = true
|
|
|
|
controller.cache_store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@view.controller = controller
|
|
|
|
end
|
|
|
|
|
2016-08-07 13:18:15 -04:00
|
|
|
def set_view_cache_dependencies
|
|
|
|
def @view.view_cache_dependencies; []; end
|
2017-05-18 12:12:32 -04:00
|
|
|
def @view.combined_fragment_cache_key(*); "ahoy `controller` dependency"; end
|
2016-08-07 13:18:15 -04:00
|
|
|
end
|
|
|
|
|
2019-03-18 20:17:11 -04:00
|
|
|
def test_render_template_template
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
@view.render(template: "test/hello_world")
|
|
|
|
wait
|
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_equal 1, @logger.logged(:info).size
|
|
|
|
assert_match(/Rendering test\/hello_world\.erb/, @logger.logged(:debug).last)
|
2019-03-18 20:17:11 -04:00
|
|
|
assert_match(/Rendered test\/hello_world\.erb/, @logger.logged(:info).last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-20 16:28:35 -04:00
|
|
|
def test_render_template_with_layout
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
@view.render(template: "test/hello_world", layout: "layouts/yield")
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal 2, @logger.logged(:debug).size
|
|
|
|
assert_equal 2, @logger.logged(:info).size
|
|
|
|
|
|
|
|
assert_match(/Rendering layout layouts\/yield\.erb/, @logger.logged(:debug).first)
|
|
|
|
assert_match(/Rendering test\/hello_world\.erb within layouts\/yield/, @logger.logged(:debug).last)
|
|
|
|
assert_match(/Rendered test\/hello_world\.erb within layouts\/yield/, @logger.logged(:info).first)
|
|
|
|
assert_match(/Rendered layout layouts\/yield\.erb/, @logger.logged(:info).last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def test_render_file_template
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2019-03-18 20:17:11 -04:00
|
|
|
@view.render(file: "#{FIXTURE_LOAD_PATH}/test/hello_world.erb")
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-12 18:41:04 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_equal 1, @logger.logged(:info).size
|
|
|
|
assert_match(/Rendering test\/hello_world\.erb/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_match(/Rendered test\/hello_world\.erb/, @logger.logged(:info).last)
|
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_text_template
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-05-21 08:49:38 -04:00
|
|
|
@view.render(plain: "TEXT")
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-12 18:41:04 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_equal 1, @logger.logged(:info).size
|
|
|
|
assert_match(/Rendering text template/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_match(/Rendered text template/, @logger.logged(:info).last)
|
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_inline_template
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-08-06 13:36:34 -04:00
|
|
|
@view.render(inline: "<%= 'TEXT' %>")
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-12 18:41:04 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_equal 1, @logger.logged(:info).size
|
|
|
|
assert_match(/Rendering inline template/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
assert_match(/Rendered inline template/, @logger.logged(:info).last)
|
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
2016-07-14 06:38:16 -04:00
|
|
|
def test_render_partial_with_implicit_path
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-07-14 06:38:16 -04:00
|
|
|
@view.render(Customer.new("david"), greeting: "hi")
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-12 18:41:04 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered customers\/_customer\.html\.erb/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
2016-07-14 06:38:16 -04:00
|
|
|
def test_render_partial_with_cache_missed
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-08-07 13:18:15 -04:00
|
|
|
set_view_cache_dependencies
|
2016-07-14 06:38:16 -04:00
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-15 11:55:08 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, @logger.logged(:debug).last)
|
2016-07-14 06:38:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_with_cache_hitted
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-08-07 13:18:15 -04:00
|
|
|
set_view_cache_dependencies
|
2016-07-14 06:38:16 -04:00
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
# Second render should hit cache.
|
|
|
|
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
|
2017-06-07 16:17:34 -04:00
|
|
|
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
|
2016-07-14 06:38:16 -04:00
|
|
|
wait
|
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 2, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache hit\]/, @logger.logged(:debug).last)
|
2016-07-14 06:38:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-20 16:28:35 -04:00
|
|
|
def test_render_partial_as_layout
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
set_view_cache_dependencies
|
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(layout: "layouts/yield_only") { "hello" }
|
|
|
|
|
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered layouts\/_yield_only\.erb/, @logger.logged(:debug).first)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_partial_with_layout
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
set_view_cache_dependencies
|
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(partial: "partial", layout: "layouts/yield_only")
|
|
|
|
|
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered test\/_partial\.html\.erb within layouts\/_yield_only/, @logger.logged(:debug).first)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-07 16:17:34 -04:00
|
|
|
def test_render_uncached_outer_partial_with_inner_cached_partial_wont_mix_cache_hits_or_misses
|
2017-04-01 12:24:03 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
set_view_cache_dependencies
|
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(partial: "test/nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
|
|
|
|
wait
|
2019-08-25 14:39:58 -04:00
|
|
|
*, cached_inner, uncached_outer = @logger.logged(:debug)
|
2017-06-07 16:17:34 -04:00
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, cached_inner)
|
2018-10-09 14:08:25 -04:00
|
|
|
assert_match(/Rendered test\/_nested_cached_customer\.erb \(Duration: .*?ms \| Allocations: .*?\)$/, uncached_outer)
|
2017-04-01 12:24:03 -04:00
|
|
|
|
2017-06-07 16:17:34 -04:00
|
|
|
# Second render hits the cache for the _cached_customer partial. Outer template's log shouldn't be affected.
|
2017-04-01 12:24:03 -04:00
|
|
|
@view.render(partial: "test/nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
|
|
|
|
wait
|
2019-08-25 14:39:58 -04:00
|
|
|
*, cached_inner, uncached_outer = @logger.logged(:debug)
|
2017-06-07 16:17:34 -04:00
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache hit\]/, cached_inner)
|
2018-10-09 14:08:25 -04:00
|
|
|
assert_match(/Rendered test\/_nested_cached_customer\.erb \(Duration: .*?ms \| Allocations: .*?\)$/, uncached_outer)
|
2017-04-01 12:24:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-07 16:17:34 -04:00
|
|
|
def test_render_cached_outer_partial_with_cached_inner_partial
|
2017-04-01 12:24:03 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
set_view_cache_dependencies
|
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(partial: "test/cached_nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
|
|
|
|
wait
|
2019-08-25 14:39:58 -04:00
|
|
|
*, cached_inner, cached_outer = @logger.logged(:debug)
|
2017-06-07 16:17:34 -04:00
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, cached_inner)
|
|
|
|
assert_match(/Rendered test\/_cached_nested_cached_customer\.erb (.*) \[cache miss\]/, cached_outer)
|
|
|
|
|
|
|
|
# One render: inner partial skipped, because the outer has been cached.
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_difference -> { @logger.logged(:debug).size }, +1 do
|
2017-06-07 16:17:34 -04:00
|
|
|
@view.render(partial: "test/cached_nested_cached_customer", locals: { cached_customer: Customer.new("Stan") })
|
|
|
|
wait
|
|
|
|
end
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_match(/Rendered test\/_cached_nested_cached_customer\.erb (.*) \[cache hit\]/, @logger.logged(:debug).last)
|
2017-04-01 12:24:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-14 06:38:16 -04:00
|
|
|
def test_render_partial_with_cache_hitted_and_missed
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-08-07 13:18:15 -04:00
|
|
|
set_view_cache_dependencies
|
2016-07-14 06:38:16 -04:00
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
|
|
|
|
wait
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, @logger.logged(:debug).last)
|
2016-07-14 06:38:16 -04:00
|
|
|
|
|
|
|
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("david") })
|
|
|
|
wait
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache hit\]/, @logger.logged(:debug).last)
|
2016-07-14 06:38:16 -04:00
|
|
|
|
|
|
|
@view.render(partial: "test/cached_customer", locals: { cached_customer: Customer.new("Stan") })
|
|
|
|
wait
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_match(/Rendered test\/_cached_customer\.erb (.*) \[cache miss\]/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2010-01-15 11:55:08 -05:00
|
|
|
end
|
|
|
|
|
2010-01-12 18:41:04 -05:00
|
|
|
def test_render_collection_template
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2019-11-08 12:52:26 -05:00
|
|
|
set_cache_controller
|
|
|
|
|
2016-08-06 13:36:34 -04:00
|
|
|
@view.render(partial: "test/customer", collection: [ Customer.new("david"), Customer.new("mary") ])
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-12 18:41:04 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered collection of test\/_customer.erb \[2 times\]/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2010-01-12 18:41:04 -05:00
|
|
|
end
|
|
|
|
|
2020-04-20 16:28:35 -04:00
|
|
|
def test_render_collection_template_with_layout
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
|
|
|
set_cache_controller
|
|
|
|
|
|
|
|
@view.render(partial: "test/customer", layout: "layouts/yield_only", collection: [ Customer.new("david"), Customer.new("mary") ])
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered collection of test\/_customer.erb within layouts\/_yield_only \[2 times\]/, @logger.logged(:debug).last)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-15 11:55:08 -05:00
|
|
|
def test_render_collection_with_implicit_path
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2019-11-08 12:52:26 -05:00
|
|
|
set_cache_controller
|
|
|
|
|
2016-08-06 13:36:34 -04:00
|
|
|
@view.render([ Customer.new("david"), Customer.new("mary") ], greeting: "hi")
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-15 11:55:08 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered collection of customers\/_customer\.html\.erb \[2 times\]/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2010-01-15 11:55:08 -05:00
|
|
|
end
|
|
|
|
|
2010-01-15 11:43:45 -05:00
|
|
|
def test_render_collection_template_without_path
|
2015-08-22 00:03:53 -04:00
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2019-11-08 12:52:26 -05:00
|
|
|
set_cache_controller
|
|
|
|
|
2016-08-06 13:36:34 -04:00
|
|
|
@view.render([ GoodCustomer.new("david"), Customer.new("mary") ], greeting: "hi")
|
2015-08-22 00:03:53 -04:00
|
|
|
wait
|
2010-01-15 11:43:45 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered collection of templates/, @logger.logged(:debug).last)
|
2016-02-18 15:55:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_render_collection_with_cached_set
|
|
|
|
Rails.stub(:root, File.expand_path(FIXTURE_LOAD_PATH)) do
|
2016-08-07 13:18:15 -04:00
|
|
|
set_view_cache_dependencies
|
2019-11-08 12:52:26 -05:00
|
|
|
set_cache_controller
|
2016-02-18 15:55:42 -05:00
|
|
|
|
2016-08-06 12:50:17 -04:00
|
|
|
@view.render(partial: "customers/customer", collection: [ Customer.new("david"), Customer.new("mary") ], cached: true,
|
|
|
|
locals: { greeting: "hi" })
|
2016-02-18 15:55:42 -05:00
|
|
|
wait
|
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
assert_equal 1, @logger.logged(:debug).size
|
|
|
|
assert_match(/Rendered collection of customers\/_customer\.html\.erb \[0 \/ 2 cache hits\]/, @logger.logged(:debug).last)
|
2015-08-22 00:03:53 -04:00
|
|
|
end
|
2010-01-15 11:43:45 -05:00
|
|
|
end
|
2010-09-22 15:03:39 -04:00
|
|
|
end
|