From 0e1d6151b9612de23d750dd7dddc11d9a9c5351b Mon Sep 17 00:00:00 2001 From: Kyle Zhao Date: Thu, 25 Jul 2019 09:21:01 -0400 Subject: [PATCH] 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`. --- app/models/topic_embed.rb | 4 ++-- spec/models/topic_embed_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/models/topic_embed.rb b/app/models/topic_embed.rb index f7b46b711f1..c1948d89af8 100644 --- a/app/models/topic_embed.rb +++ b/app/models/topic_embed.rb @@ -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) diff --git a/spec/models/topic_embed_spec.rb b/spec/models/topic_embed_spec.rb index 71c60afb3ab..18d4dd97648 100644 --- a/spec/models/topic_embed_spec.rb +++ b/spec/models/topic_embed_spec.rb @@ -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) { "

#{'a' * MAX_LENGTH_BEFORE_TRUNCATION}

\n

more

" } + + 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