2006-08-03 19:59:38 -04:00
|
|
|
require 'fileutils'
|
2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2006-08-03 19:59:38 -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
|
2006-08-03 19:59:38 -04:00
|
|
|
FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../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
|
2013-09-12 15:52:53 -04:00
|
|
|
@params = { controller: 'posts', action: 'index' }
|
2012-10-03 12:14:28 -04:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
@controller.params = @params
|
|
|
|
@controller.request = @request
|
|
|
|
@controller.response = @response
|
2012-09-28 00:10:53 -04:00
|
|
|
end
|
|
|
|
|
2012-10-03 12:14:28 -04:00
|
|
|
def test_fragment_cache_key
|
|
|
|
assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
|
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
|
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
|
2008-01-03 10:35:10 -05:00
|
|
|
@params = {:controller => 'posts', :action => 'index'}
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
@controller.params = @params
|
|
|
|
@controller.request = @request
|
|
|
|
@controller.response = @response
|
|
|
|
end
|
|
|
|
|
2008-01-11 20:19:46 -05:00
|
|
|
def test_fragment_cache_key
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_equal 'views/what a key', @controller.fragment_cache_key('what a key')
|
2008-07-15 21:52:29 -04:00
|
|
|
assert_equal "views/test.host/fragment_caching_test/some_action",
|
|
|
|
@controller.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
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
2008-01-03 10:35:10 -05:00
|
|
|
assert_equal 'value', @controller.read_fragment('name')
|
|
|
|
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
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
2008-01-03 10:35:10 -05:00
|
|
|
assert_nil @controller.read_fragment('name')
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_fragment_exist_with_caching_enabled
|
2008-05-16 13:10:30 -04:00
|
|
|
@store.write('views/name', 'value')
|
|
|
|
assert @controller.fragment_exist?('name')
|
|
|
|
assert !@controller.fragment_exist?('other_name')
|
|
|
|
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
|
2008-05-16 13:10:30 -04:00
|
|
|
@store.write('views/name', 'value')
|
|
|
|
assert !@controller.fragment_exist?('name')
|
|
|
|
assert !@controller.fragment_exist?('other_name')
|
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_write_fragment_with_caching_enabled
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
2008-01-03 10:35:10 -05:00
|
|
|
assert_equal 'value', @controller.write_fragment('name', 'value')
|
2008-01-03 16:05:12 -05:00
|
|
|
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
|
2008-01-03 16:05:12 -05:00
|
|
|
assert_nil @store.read('views/name')
|
2010-04-15 15:58:54 -04:00
|
|
|
@controller.perform_caching = false
|
2008-11-22 14:19:11 -05:00
|
|
|
assert_equal 'value', @controller.write_fragment('name', 'value')
|
2008-01-03 16:05:12 -05:00
|
|
|
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
|
2008-01-03 16:05:12 -05:00
|
|
|
@store.write('views/name', 'value')
|
2008-01-03 10:35:10 -05:00
|
|
|
@controller.expire_fragment 'name'
|
2008-01-03 16:05:12 -05:00
|
|
|
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
|
2008-01-03 16:05:12 -05: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
|
|
|
|
2008-01-03 16:05:12 -05: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
|
2008-01-03 16:05:12 -05: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
|
|
|
|
|
For performance reasons, you can no longer call html_safe! on Strings. Instead, all Strings are always not html_safe?. Instead, you can get a SafeBuffer from a String by calling #html_safe, which will SafeBuffer.new(self).
* Additionally, instead of doing concat("</form>".html_safe), you can do
safe_concat("</form>"), which will skip both the flag set, and the flag
check.
* For the first pass, I converted virtually all #html_safe!s to #html_safe,
and the tests pass. A further optimization would be to try to use
#safe_concat as much as possible, reducing the performance impact if
we know up front that a String is safe.
2010-01-31 22:17:42 -05:00
|
|
|
buffer = 'generated till now -> '.html_safe
|
2010-03-18 18:52:43 -04:00
|
|
|
buffer << view_context.send(:fragment_for, 'expensive') { fragment_computed = true }
|
2008-01-03 10:35:10 -05:00
|
|
|
|
|
|
|
assert !fragment_computed
|
|
|
|
assert_equal 'generated till now -> fragment content', buffer
|
|
|
|
end
|
2009-08-08 11:46:44 -04:00
|
|
|
|
2010-03-14 21:55:13 -04:00
|
|
|
def test_html_safety
|
|
|
|
assert_nil @store.read('views/name')
|
|
|
|
content = 'value'.html_safe
|
|
|
|
assert_equal content, @controller.write_fragment('name', content)
|
|
|
|
|
|
|
|
cached = @store.read('views/name')
|
|
|
|
assert_equal content, cached
|
|
|
|
assert_equal String, cached.class
|
|
|
|
|
|
|
|
html_safe = @controller.read_fragment('name')
|
|
|
|
assert_equal content, html_safe
|
|
|
|
assert html_safe.html_safe?
|
|
|
|
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
|
|
|
|
|
|
|
def fragment_cached_without_digest
|
|
|
|
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
|
2008-01-11 20:19:46 -05:00
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
|
|
|
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",
|
|
|
|
@store.read("views/test.host/functional_caching/fragment_cached/#{template_digest("functional_caching/fragment_cached", "html")}")
|
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",
|
|
|
|
@store.read("views/test.host/functional_caching/html_fragment_cached_with_partial/#{template_digest("functional_caching/_partial", "html")}"))
|
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
|
|
|
|
get :fragment_cached_without_digest, :format => "html"
|
|
|
|
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
|
|
|
|
|
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",
|
2012-08-29 15:23:15 -04:00
|
|
|
@store.read("views/test.host/functional_caching/inline_fragment_cached/#{template_digest("functional_caching/inline_fragment_cached", "html")}"))
|
2008-07-13 14:26:48 -04:00
|
|
|
end
|
|
|
|
|
2008-07-15 21:52:29 -04:00
|
|
|
def test_html_formatted_fragment_caching
|
|
|
|
get :formatted_fragment_cached, :format => "html"
|
|
|
|
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>",
|
2012-08-29 15:23:15 -04:00
|
|
|
@store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "html")}")
|
2008-07-15 21:52:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_xml_formatted_fragment_caching
|
|
|
|
get :formatted_fragment_cached, :format => "xml"
|
|
|
|
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",
|
|
|
|
@store.read("views/test.host/functional_caching/formatted_fragment_cached/#{template_digest("functional_caching/formatted_fragment_cached", "xml")}")
|
2008-07-15 21:52:29 -04:00
|
|
|
end
|
2012-09-28 00:10:53 -04:00
|
|
|
|
2012-08-29 15:23:15 -04:00
|
|
|
private
|
|
|
|
def template_digest(name, format)
|
|
|
|
ActionView::Digestor.digest(name, format, @controller.lookup_context)
|
|
|
|
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)
|
|
|
|
return false
|
|
|
|
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
|
|
|
|
cache_helper = Object.new
|
|
|
|
cache_helper.extend(ActionView::Helpers::CacheHelper)
|
|
|
|
cache_helper.expects(:controller).returns(controller).at_least(0)
|
|
|
|
cache_helper.expects(:output_buffer).returns(output_buffer).at_least(0)
|
|
|
|
# if the output_buffer is changed, the new one should be html_safe and of the same type
|
|
|
|
cache_helper.expects(:output_buffer=).with(responds_with(:html_safe?, true)).with(instance_of(output_buffer.class)).at_least(0)
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_safe_buffer
|
|
|
|
output_buffer = ActiveSupport::SafeBuffer.new
|
|
|
|
controller = MockController.new
|
|
|
|
cache_helper = Object.new
|
|
|
|
cache_helper.extend(ActionView::Helpers::CacheHelper)
|
|
|
|
cache_helper.expects(:controller).returns(controller).at_least(0)
|
|
|
|
cache_helper.expects(:output_buffer).returns(output_buffer).at_least(0)
|
|
|
|
# if the output_buffer is changed, the new one should be html_safe and of the same type
|
|
|
|
cache_helper.expects(:output_buffer=).with(responds_with(:html_safe?, true)).with(instance_of(output_buffer.class)).at_least(0)
|
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
cache_helper.send :fragment_for, 'Test fragment name', 'Test fragment', &Proc.new{ nil }
|
|
|
|
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
|
2013-01-08 14:02:11 -05:00
|
|
|
assert NoDependenciesController.new.view_cache_dependencies.empty?
|
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
|