Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix improper detection and handling of html_safe buffer in CacheHelper #2080

Merged
merged 3 commits into from Jul 24, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions actionpack/lib/action_view/helpers/cache_helper.rb
Expand Up @@ -53,10 +53,10 @@ def fragment_for(name = {}, options = nil, &block) #:nodoc:
# This dance is needed because Builder can't use capture
pos = output_buffer.length
yield
if output_buffer.is_a?(ActionView::OutputBuffer)
if output_buffer.html_safe?
safe_output_buffer = output_buffer.to_str
fragment = safe_output_buffer.slice!(pos..-1)
self.output_buffer = ActionView::OutputBuffer.new(safe_output_buffer)
self.output_buffer = output_buffer.class.new(safe_output_buffer)
else
fragment = output_buffer.slice!(pos..-1)
end
Expand Down
50 changes: 50 additions & 0 deletions actionpack/test/controller/caching_test.rb
Expand Up @@ -785,3 +785,53 @@ def test_xml_formatted_fragment_caching
assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached')
end
end

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 }
assert output_buffer.html_safe?, "Output buffer should stay html_safe"
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 }
assert output_buffer.html_safe?, "Output buffer should stay html_safe"
end
end

end