FIX: Frozen string error in `TopicEmbed.import` (#7938)

When `SiteSetting.embed_truncate` is enabled (by default), the truncated
string is mutatable and does not raise an error.

However, when the setting is disabled, the `contents` string is frozen
and immutable, and will raise a `FrozenError`.
This commit is contained in:
Kyle Zhao 2019-07-25 09:21:01 -04:00 committed by Robin Ward
parent a61ff16740
commit 0e1d6151b9
2 changed files with 22 additions and 2 deletions

View File

@ -36,8 +36,8 @@ class TopicEmbed < ActiveRecord::Base
if SiteSetting.embed_truncate
contents = first_paragraph_from(contents)
end
contents ||= +''
contents << imported_from_html(url)
contents ||= ''
contents = +contents << imported_from_html(url)
url = normalize_url(url)

View File

@ -86,6 +86,26 @@ describe TopicEmbed do
expect(post.cook_method).to eq(Post.cook_methods[:regular])
end
end
describe 'embedded content truncation' do
MAX_LENGTH_BEFORE_TRUNCATION = 100
let(:long_content) { "<p>#{'a' * MAX_LENGTH_BEFORE_TRUNCATION}</p>\n<p>more</p>" }
it 'truncates the imported post when truncation is enabled' do
SiteSetting.embed_truncate = true
post = TopicEmbed.import(user, url, title, long_content)
expect(post.raw).not_to include(long_content)
end
it 'keeps everything in the imported post when truncation is disabled' do
SiteSetting.embed_truncate = false
post = TopicEmbed.import(user, url, title, long_content)
expect(post.raw).to include(long_content)
end
end
end
context '.topic_id_for_embed' do