2017-07-24 16:20:53 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
require "fileutils"
|
|
|
|
require "abstract_unit"
|
|
|
|
require "lib/controller/fake_models"
|
2006-08-03 19:59:38 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
CACHE_DIR = "test_cache"
|
2007-09-28 10:18:47 -04:00
|
|
|
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
|
2017-05-15 10:17:28 -04:00
|
|
|
FILE_STORE_PATH = File.join(__dir__, "../temp/", CACHE_DIR)
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
class FragmentCachingMetalTestController < ActionController::Metal
|
2012-09-28 00:10:53 -04:00
|
|
|
abstract!
|
|
|
|
|
|
|
|
include ActionController::Caching
|
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
def some_action; end
|
2012-09-28 00:10:53 -04:00
|
|
|
end
|
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
class FragmentCachingMetalTest < ActionController::TestCase
|
2012-09-28 00:10:53 -04:00
|
|
|
def setup
|
2012-10-03 12:14:28 -04:00
|
|
|
super
|
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@controller = FragmentCachingMetalTestController.new
|
|
|
|
@controller.perform_caching = true
|
|
|
|
@controller.cache_store = @store
|
2016-08-06 12:54:50 -04:00
|
|
|
@params = { controller: "posts", action: "index" }
|
2012-10-03 12:14:28 -04:00
|
|
|
@controller.params = @params
|
|
|
|
@controller.request = @request
|
|
|
|
@controller.response = @response
|
2012-09-28 00:10:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-04 19:50:57 -05:00
|
|
|
class CachingController < ActionController::Base
|
|
|
|
abstract!
|
|
|
|
|
|
|
|
self.cache_store = :file_store, FILE_STORE_PATH
|
|
|
|
end
|
|
|
|
|
|
|
|
class FragmentCachingTestController < CachingController
|
2013-09-12 15:52:53 -04:00
|
|
|
def some_action; end
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class FragmentCachingTest < ActionController::TestCase
|
2017-05-18 12:12:32 -04:00
|
|
|
ModelWithKeyAndVersion = Struct.new(:cache_key, :cache_version)
|
|
|
|
|
2008-01-03 10:35:10 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2008-01-03 16:05:12 -05:00
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
2008-01-03 10:35:10 -05:00
|
|
|
@controller = FragmentCachingTestController.new
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = true
|
2010-03-04 19:50:57 -05:00
|
|
|
@controller.cache_store = @store
|
2016-08-16 03:30:11 -04:00
|
|
|
@params = { controller: "posts", action: "index" }
|
2008-01-03 10:35:10 -05:00
|
|
|
@controller.params = @params
|
|
|
|
@controller.request = @request
|
|
|
|
@controller.response = @response
|
2017-05-18 12:12:32 -04:00
|
|
|
|
|
|
|
@m1v1 = ModelWithKeyAndVersion.new("model/1", "1")
|
|
|
|
@m1v2 = ModelWithKeyAndVersion.new("model/1", "2")
|
|
|
|
@m2v1 = ModelWithKeyAndVersion.new("model/2", "1")
|
|
|
|
@m2v2 = ModelWithKeyAndVersion.new("model/2", "2")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_cache_key
|
2017-05-18 12:12:32 -04:00
|
|
|
assert_deprecated do
|
|
|
|
assert_equal "views/what a key", @controller.fragment_cache_key("what a key")
|
|
|
|
assert_equal "views/test.host/fragment_caching_test/some_action",
|
|
|
|
@controller.fragment_cache_key(controller: "fragment_caching_test", action: "some_action")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_combined_fragment_cache_key
|
|
|
|
assert_equal [ :views, "what a key" ], @controller.combined_fragment_cache_key("what a key")
|
|
|
|
assert_equal [ :views, "test.host/fragment_caching_test/some_action" ],
|
|
|
|
@controller.combined_fragment_cache_key(controller: "fragment_caching_test", action: "some_action")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_read_fragment_with_caching_enabled
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/name", "value")
|
|
|
|
assert_equal "value", @controller.read_fragment("name")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_read_fragment_with_caching_disabled
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/name", "value")
|
|
|
|
assert_nil @controller.read_fragment("name")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2017-05-18 12:12:32 -04:00
|
|
|
def test_read_fragment_with_versioned_model
|
|
|
|
@controller.write_fragment([ "stuff", @m1v1 ], "hello")
|
|
|
|
assert_equal "hello", @controller.read_fragment([ "stuff", @m1v1 ])
|
|
|
|
assert_nil @controller.read_fragment([ "stuff", @m1v2 ])
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_fragment_exist_with_caching_enabled
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/name", "value")
|
|
|
|
assert @controller.fragment_exist?("name")
|
|
|
|
assert !@controller.fragment_exist?("other_name")
|
2008-05-16 13:10:30 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_fragment_exist_with_caching_disabled
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/name", "value")
|
|
|
|
assert !@controller.fragment_exist?("name")
|
|
|
|
assert !@controller.fragment_exist?("other_name")
|
2008-05-16 13:10:30 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_write_fragment_with_caching_enabled
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nil @store.read("views/name")
|
|
|
|
assert_equal "value", @controller.write_fragment("name", "value")
|
|
|
|
assert_equal "value", @store.read("views/name")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_write_fragment_with_caching_disabled
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nil @store.read("views/name")
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "value", @controller.write_fragment("name", "value")
|
|
|
|
assert_nil @store.read("views/name")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_expire_fragment_with_simple_key
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/name", "value")
|
|
|
|
@controller.expire_fragment "name"
|
|
|
|
assert_nil @store.read("views/name")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_expire_fragment_with_regexp
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/name", "value")
|
|
|
|
@store.write("views/another_name", "another_value")
|
|
|
|
@store.write("views/primalgrasp", "will not expire ;-)")
|
2008-01-03 10:35:10 -05:00
|
|
|
|
2010-09-18 17:14:46 -04:00
|
|
|
@controller.expire_fragment(/name/)
|
2008-01-03 10:35:10 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nil @store.read("views/name")
|
|
|
|
assert_nil @store.read("views/another_name")
|
|
|
|
assert_equal "will not expire ;-)", @store.read("views/primalgrasp")
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_fragment_for
|
2016-08-06 12:54:50 -04:00
|
|
|
@store.write("views/expensive", "fragment content")
|
2008-01-03 10:35:10 -05:00
|
|
|
fragment_computed = false
|
|
|
|
|
2010-03-18 18:52:43 -04:00
|
|
|
view_context = @controller.view_context
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
buffer = "generated till now -> ".html_safe
|
|
|
|
buffer << view_context.send(:fragment_for, "expensive") { fragment_computed = true }
|
2008-01-03 10:35:10 -05:00
|
|
|
|
|
|
|
assert !fragment_computed
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "generated till now -> fragment content", buffer
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
2009-08-08 11:46:44 -04:00
|
|
|
|
2010-03-14 21:55:13 -04:00
|
|
|
def test_html_safety
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_nil @store.read("views/name")
|
|
|
|
content = "value".html_safe
|
|
|
|
assert_equal content, @controller.write_fragment("name", content)
|
2010-03-14 21:55:13 -04:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
cached = @store.read("views/name")
|
2010-03-14 21:55:13 -04:00
|
|
|
assert_equal content, cached
|
|
|
|
assert_equal String, cached.class
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
html_safe = @controller.read_fragment("name")
|
2010-03-14 21:55:13 -04:00
|
|
|
assert_equal content, html_safe
|
2018-01-25 18:14:09 -05:00
|
|
|
assert_predicate html_safe, :html_safe?
|
2010-03-14 21:55:13 -04:00
|
|
|
end
|
2008-01-03 10:35:10 -05:00
|
|
|
end
|
2008-01-11 20:19:46 -05:00
|
|
|
|
2010-03-04 19:50:57 -05:00
|
|
|
class FunctionalCachingController < CachingController
|
2008-01-11 20:19:46 -05:00
|
|
|
def fragment_cached
|
|
|
|
end
|
|
|
|
|
|
|
|
def html_fragment_cached_with_partial
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def formatted_fragment_cached
|
|
|
|
respond_to do |format|
|
|
|
|
format.html
|
|
|
|
format.xml
|
|
|
|
end
|
|
|
|
end
|
2012-11-25 23:10:44 -05:00
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
def formatted_fragment_cached_with_variant
|
2015-07-08 19:15:50 -04:00
|
|
|
request.variant = :phone if params[:v] == "phone"
|
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
respond_to do |format|
|
|
|
|
format.html.phone
|
|
|
|
format.html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-25 23:10:44 -05:00
|
|
|
def fragment_cached_without_digest
|
|
|
|
end
|
2015-12-27 01:25:27 -05:00
|
|
|
|
|
|
|
def fragment_cached_with_options
|
|
|
|
end
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class FunctionalFragmentCachingTest < ActionController::TestCase
|
2008-01-11 20:19:46 -05:00
|
|
|
def setup
|
2009-04-08 20:33:06 -04:00
|
|
|
super
|
2008-01-11 20:19:46 -05:00
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@controller = FunctionalCachingController.new
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = true
|
2010-03-04 19:50:57 -05:00
|
|
|
@controller.cache_store = @store
|
2016-07-14 06:38:16 -04:00
|
|
|
@controller.enable_fragment_cache_logging = true
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2008-06-25 16:24:12 -04:00
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_caching
|
|
|
|
get :fragment_cached
|
|
|
|
assert_response :success
|
|
|
|
expected_body = <<-CACHED
|
|
|
|
Hello
|
|
|
|
This bit's fragment cached
|
2011-07-23 16:50:34 -04:00
|
|
|
Ciao
|
2008-01-11 20:19:46 -05:00
|
|
|
CACHED
|
|
|
|
assert_equal expected_body, @response.body
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
assert_equal "This bit's fragment cached",
|
2017-05-18 12:12:32 -04:00
|
|
|
@store.read("views/functional_caching/fragment_cached:#{template_digest("functional_caching/fragment_cached")}/fragment")
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_caching_in_partials
|
|
|
|
get :html_fragment_cached_with_partial
|
|
|
|
assert_response :success
|
2010-09-18 17:14:46 -04:00
|
|
|
assert_match(/Old fragment caching in a partial/, @response.body)
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
assert_match("Old fragment caching in a partial",
|
2017-05-18 12:12:32 -04:00
|
|
|
@store.read("views/functional_caching/_partial:#{template_digest("functional_caching/_partial")}/test.host/functional_caching/html_fragment_cached_with_partial"))
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2008-05-14 13:33:54 -04:00
|
|
|
|
2012-11-25 23:10:44 -05:00
|
|
|
def test_skipping_fragment_cache_digesting
|
2015-01-04 04:35:06 -05:00
|
|
|
get :fragment_cached_without_digest, format: "html"
|
2012-11-25 23:10:44 -05:00
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n<p>ERB</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
assert_equal "<p>ERB</p>", @store.read("views/nodigest")
|
|
|
|
end
|
|
|
|
|
2015-12-27 01:25:27 -05:00
|
|
|
def test_fragment_caching_with_options
|
2016-04-17 13:36:26 -04:00
|
|
|
time = Time.now
|
2015-12-27 01:25:27 -05:00
|
|
|
get :fragment_cached_with_options
|
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n<p>ERB</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
2016-04-17 13:36:26 -04:00
|
|
|
Time.stub(:now, time + 11) do
|
|
|
|
assert_nil @store.read("views/with_options")
|
|
|
|
end
|
2015-12-27 01:25:27 -05:00
|
|
|
end
|
|
|
|
|
2008-07-13 14:26:48 -04:00
|
|
|
def test_render_inline_before_fragment_caching
|
|
|
|
get :inline_fragment_cached
|
|
|
|
assert_response :success
|
2010-09-18 17:14:46 -04:00
|
|
|
assert_match(/Some inline content/, @response.body)
|
|
|
|
assert_match(/Some cached content/, @response.body)
|
2012-09-28 00:10:53 -04:00
|
|
|
assert_match("Some cached content",
|
2017-05-18 12:12:32 -04:00
|
|
|
@store.read("views/functional_caching/inline_fragment_cached:#{template_digest("functional_caching/inline_fragment_cached")}/test.host/functional_caching/inline_fragment_cached"))
|
2008-07-13 14:26:48 -04:00
|
|
|
end
|
|
|
|
|
2014-02-21 05:28:51 -05:00
|
|
|
def test_fragment_cache_instrumentation
|
|
|
|
payload = nil
|
|
|
|
|
|
|
|
subscriber = proc do |*args|
|
|
|
|
event = ActiveSupport::Notifications::Event.new(*args)
|
|
|
|
payload = event.payload
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveSupport::Notifications.subscribed(subscriber, "read_fragment.action_controller") do
|
|
|
|
get :inline_fragment_cached
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "functional_caching", payload[:controller]
|
|
|
|
assert_equal "inline_fragment_cached", payload[:action]
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_html_formatted_fragment_caching
|
2015-01-04 04:35:06 -05:00
|
|
|
get :formatted_fragment_cached, format: "html"
|
2008-07-15 21:52:29 -04:00
|
|
|
assert_response :success
|
2010-03-16 02:48:46 -04:00
|
|
|
expected_body = "<body>\n<p>ERB</p>\n</body>\n"
|
2008-07-15 21:52:29 -04:00
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
|
2012-09-28 00:10:53 -04:00
|
|
|
assert_equal "<p>ERB</p>",
|
2017-05-18 12:12:32 -04:00
|
|
|
@store.read("views/functional_caching/formatted_fragment_cached:#{template_digest("functional_caching/formatted_fragment_cached")}/fragment")
|
2008-07-15 21:52:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_formatted_fragment_caching
|
2015-01-04 04:35:06 -05:00
|
|
|
get :formatted_fragment_cached, format: "xml"
|
2008-07-15 21:52:29 -04:00
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n <p>Builder</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
assert_equal " <p>Builder</p>\n",
|
2017-05-18 12:12:32 -04:00
|
|
|
@store.read("views/functional_caching/formatted_fragment_cached:#{template_digest("functional_caching/formatted_fragment_cached")}/fragment")
|
2008-07-15 21:52:29 -04:00
|
|
|
end
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2014-03-08 17:09:54 -05:00
|
|
|
def test_fragment_caching_with_variant
|
2015-07-08 19:15:50 -04:00
|
|
|
get :formatted_fragment_cached_with_variant, format: "html", params: { v: :phone }
|
2014-03-08 17:09:54 -05:00
|
|
|
assert_response :success
|
|
|
|
expected_body = "<body>\n<p>PHONE</p>\n</body>\n"
|
|
|
|
|
|
|
|
assert_equal expected_body, @response.body
|
|
|
|
|
|
|
|
assert_equal "<p>PHONE</p>",
|
2017-05-18 12:12:32 -04:00
|
|
|
@store.read("views/functional_caching/formatted_fragment_cached_with_variant:#{template_digest("functional_caching/formatted_fragment_cached_with_variant")}/fragment")
|
2014-03-08 17:09:54 -05:00
|
|
|
end
|
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
private
|
2014-03-21 12:37:23 -04:00
|
|
|
def template_digest(name)
|
|
|
|
ActionView::Digestor.digest(name: name, finder: @controller.lookup_context)
|
2012-08-29 15:23:15 -04:00
|
|
|
end
|
2008-01-11 20:19:46 -05:00
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
|
|
|
|
class CacheHelperOutputBufferTest < ActionController::TestCase
|
|
|
|
class MockController
|
|
|
|
def read_fragment(name, options)
|
2017-10-28 04:20:38 -04:00
|
|
|
false
|
2011-07-23 16:50:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_fragment(name, fragment, options)
|
|
|
|
fragment
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_output_buffer
|
|
|
|
output_buffer = ActionView::OutputBuffer.new
|
|
|
|
controller = MockController.new
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper = Class.new do
|
2017-10-28 04:39:58 -04:00
|
|
|
def self.controller; end
|
|
|
|
def self.output_buffer; end
|
|
|
|
def self.output_buffer=; end
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
cache_helper.extend(ActionView::Helpers::CacheHelper)
|
|
|
|
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper.stub :controller, controller do
|
|
|
|
cache_helper.stub :output_buffer, output_buffer do
|
|
|
|
assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
|
|
|
|
assert_nothing_raised do
|
2016-08-16 03:30:11 -04:00
|
|
|
cache_helper.send :fragment_for, "Test fragment name", "Test fragment", &Proc.new { nil }
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_buffer
|
|
|
|
output_buffer = ActiveSupport::SafeBuffer.new
|
|
|
|
controller = MockController.new
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper = Class.new do
|
2017-10-28 04:39:58 -04:00
|
|
|
def self.controller; end
|
|
|
|
def self.output_buffer; end
|
|
|
|
def self.output_buffer=; end
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
cache_helper.extend(ActionView::Helpers::CacheHelper)
|
|
|
|
|
2015-09-05 10:58:21 -04:00
|
|
|
cache_helper.stub :controller, controller do
|
|
|
|
cache_helper.stub :output_buffer, output_buffer do
|
|
|
|
assert_called_with cache_helper, :output_buffer=, [output_buffer.class.new(output_buffer)] do
|
|
|
|
assert_nothing_raised do
|
2016-08-16 03:30:11 -04:00
|
|
|
cache_helper.send :fragment_for, "Test fragment name", "Test fragment", &Proc.new { nil }
|
2015-09-05 10:58:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-07-23 16:50:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-03 16:35:19 -04:00
|
|
|
|
2013-01-08 13:20:47 -05:00
|
|
|
class ViewCacheDependencyTest < ActionController::TestCase
|
|
|
|
class NoDependenciesController < ActionController::Base
|
|
|
|
end
|
|
|
|
|
|
|
|
class HasDependenciesController < ActionController::Base
|
|
|
|
view_cache_dependency { "trombone" }
|
|
|
|
view_cache_dependency { "flute" }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_view_cache_dependencies_are_empty_by_default
|
2018-01-25 18:16:57 -05:00
|
|
|
assert_empty NoDependenciesController.new.view_cache_dependencies
|
2013-01-08 13:20:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_view_cache_dependencies_are_listed_in_declaration_order
|
2013-01-08 14:02:11 -05:00
|
|
|
assert_equal %w(trombone flute), HasDependenciesController.new.view_cache_dependencies
|
2013-01-08 13:20:47 -05:00
|
|
|
end
|
|
|
|
end
|
2015-02-15 16:39:04 -05:00
|
|
|
|
|
|
|
class CollectionCacheController < ActionController::Base
|
2015-05-24 16:02:22 -04:00
|
|
|
attr_accessor :partial_rendered_times
|
|
|
|
|
2015-02-15 16:39:04 -05:00
|
|
|
def index
|
2016-08-06 12:54:50 -04:00
|
|
|
@customers = [Customer.new("david", params[:id] || 1)]
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def index_ordered
|
2016-08-06 12:54:50 -04:00
|
|
|
@customers = [Customer.new("david", 1), Customer.new("david", 2), Customer.new("david", 3)]
|
|
|
|
render "index"
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
2016-02-15 16:47:44 -05:00
|
|
|
def index_explicit_render_in_controller
|
2016-08-06 12:54:50 -04:00
|
|
|
@customers = [Customer.new("david", 1)]
|
|
|
|
render partial: "customers/customer", collection: @customers, cached: true
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def index_with_comment
|
2016-08-06 12:54:50 -04:00
|
|
|
@customers = [Customer.new("david", 1)]
|
|
|
|
render partial: "customers/commented_customer", collection: @customers, as: :customer, cached: true
|
2016-02-12 15:52:54 -05:00
|
|
|
end
|
2016-07-20 19:35:55 -04:00
|
|
|
|
|
|
|
def index_with_callable_cache_key
|
2016-08-06 12:54:50 -04:00
|
|
|
@customers = [Customer.new("david", 1)]
|
|
|
|
render partial: "customers/customer", collection: @customers, cached: -> customer { "cached_david" }
|
2016-07-20 19:35:55 -04:00
|
|
|
end
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
2016-07-20 19:35:55 -04:00
|
|
|
class CollectionCacheTest < ActionController::TestCase
|
2015-02-15 16:39:04 -05:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@controller = CollectionCacheController.new
|
|
|
|
@controller.perform_caching = true
|
2015-05-24 16:02:22 -04:00
|
|
|
@controller.partial_rendered_times = 0
|
2015-06-15 16:04:43 -04:00
|
|
|
@controller.cache_store = ActiveSupport::Cache::MemoryStore.new
|
2016-02-15 16:47:44 -05:00
|
|
|
ActionView::PartialRenderer.collection_cache = ActiveSupport::Cache::MemoryStore.new
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collection_fetches_cached_views
|
|
|
|
get :index
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2017-05-18 12:12:32 -04:00
|
|
|
assert_match "david, 1", ActionView::PartialRenderer.collection_cache.read("views/customers/_customer:7c228ab609f0baf0b1f2367469210937/david/1")
|
2015-02-15 16:39:04 -05:00
|
|
|
|
|
|
|
get :index
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_preserves_order_when_reading_from_cache_plus_rendering
|
|
|
|
get :index, params: { id: 2 }
|
2016-02-12 16:15:15 -05:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_select ":root", "david, 2"
|
2015-02-15 16:39:04 -05:00
|
|
|
|
2016-02-12 16:15:15 -05:00
|
|
|
get :index_ordered
|
|
|
|
assert_equal 3, @controller.partial_rendered_times
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_select ":root", "david, 1\n david, 2\n david, 3"
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_explicit_render_call_with_options
|
2016-02-15 16:47:44 -05:00
|
|
|
get :index_explicit_render_in_controller
|
2015-02-15 16:39:04 -05:00
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_select ":root", "david, 1"
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_caching_works_with_beginning_comment
|
|
|
|
get :index_with_comment
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
|
|
|
|
get :index_with_comment
|
2015-05-24 16:02:22 -04:00
|
|
|
assert_equal 1, @controller.partial_rendered_times
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
2016-02-12 15:52:54 -05:00
|
|
|
|
2016-07-20 19:35:55 -04:00
|
|
|
def test_caching_with_callable_cache_key
|
|
|
|
get :index_with_callable_cache_key
|
2017-05-18 12:12:32 -04:00
|
|
|
assert_match "david, 1", ActionView::PartialRenderer.collection_cache.read("views/customers/_customer:7c228ab609f0baf0b1f2367469210937/cached_david")
|
2016-07-20 19:35:55 -04:00
|
|
|
end
|
2015-02-15 16:39:04 -05:00
|
|
|
end
|
2015-12-14 20:53:43 -05:00
|
|
|
|
|
|
|
class FragmentCacheKeyTestController < CachingController
|
|
|
|
attr_accessor :account_id
|
|
|
|
|
|
|
|
fragment_cache_key "v1"
|
|
|
|
fragment_cache_key { account_id }
|
|
|
|
end
|
|
|
|
|
|
|
|
class FragmentCacheKeyTest < ActionController::TestCase
|
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@store = ActiveSupport::Cache::MemoryStore.new
|
|
|
|
@controller = FragmentCacheKeyTestController.new
|
|
|
|
@controller.perform_caching = true
|
|
|
|
@controller.cache_store = @store
|
|
|
|
end
|
|
|
|
|
2017-05-18 12:12:32 -04:00
|
|
|
def test_combined_fragment_cache_key
|
2015-12-14 20:53:43 -05:00
|
|
|
@controller.account_id = "123"
|
2017-05-18 12:12:32 -04:00
|
|
|
assert_equal [ :views, "v1", "123", "what a key" ], @controller.combined_fragment_cache_key("what a key")
|
2015-12-14 20:53:43 -05:00
|
|
|
|
|
|
|
@controller.account_id = nil
|
2017-05-18 12:12:32 -04:00
|
|
|
assert_equal [ :views, "v1", "what a key" ], @controller.combined_fragment_cache_key("what a key")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_combined_fragment_cache_key_with_envs
|
|
|
|
ENV["RAILS_APP_VERSION"] = "55"
|
|
|
|
assert_equal [ :views, "55", "v1", "what a key" ], @controller.combined_fragment_cache_key("what a key")
|
|
|
|
|
|
|
|
ENV["RAILS_CACHE_ID"] = "66"
|
|
|
|
assert_equal [ :views, "66", "v1", "what a key" ], @controller.combined_fragment_cache_key("what a key")
|
|
|
|
ensure
|
|
|
|
ENV["RAILS_CACHE_ID"] = ENV["RAILS_APP_VERSION"] = nil
|
2015-12-14 20:53:43 -05:00
|
|
|
end
|
|
|
|
end
|