From 90fcede832f04c3824c9bf3c50b5e16f75e8e5bb Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 1 Apr 2020 15:42:36 -0400 Subject: [PATCH] FIX: replace default welcome topic post with new value from wizard Previously the text entered in the wizard would be prepended onto the default first paragraph. --- lib/introduction_updater.rb | 9 ++++--- spec/lib/introduction_updater_spec.rb | 38 +++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/lib/introduction_updater.rb b/lib/introduction_updater.rb index b6c1a459664..dc2ffbe3a5c 100644 --- a/lib/introduction_updater.rb +++ b/lib/introduction_updater.rb @@ -18,9 +18,12 @@ class IntroductionUpdater if previous_value != new_value revisor = PostRevisor.new(post) - - remaining = post.raw.split("\n")[1..-1] - revisor.revise!(@user, raw: "#{new_value}\n#{remaining.join("\n")}") + if post.raw.chomp == I18n.t('discourse_welcome_topic.body', base_path: Discourse.base_path).chomp + revisor.revise!(@user, raw: new_value) + else + remaining = post.raw[previous_value.length..-1] + revisor.revise!(@user, raw: "#{new_value}#{remaining}") + end end end diff --git a/spec/lib/introduction_updater_spec.rb b/spec/lib/introduction_updater_spec.rb index 66f91a6341f..49698e56e05 100644 --- a/spec/lib/introduction_updater_spec.rb +++ b/spec/lib/introduction_updater_spec.rb @@ -55,4 +55,42 @@ describe IntroductionUpdater do end end end + + describe "update_summary" do + let(:welcome_topic) do + topic = Fabricate(:topic, title: I18n.t("discourse_welcome_topic.title")) + Fabricate( + :post, + topic: topic, + raw: I18n.t("discourse_welcome_topic.body", base_path: Discourse.base_path), + post_number: 1 + ) + topic + end + + let(:first_post) { welcome_topic.posts.first } + + let(:new_summary) { "Welcome to my new site. It's gonna be good." } + + subject { IntroductionUpdater.new(Fabricate(:admin)).update_summary(new_summary) } + + before do + SiteSetting.welcome_topic_id = welcome_topic.id + end + + it "completely replaces post if it has default value" do + subject + expect { + expect(first_post.reload.raw).to eq(new_summary) + }.to_not change { welcome_topic.reload.category_id } + end + + it "only replaces first paragraph if it has custom content" do + paragraph1 = "This is the summary of my community" + paragraph2 = "And this is something I added later" + first_post.update!(raw: [paragraph1, paragraph2].join("\n\n")) + subject + expect(first_post.reload.raw).to eq([new_summary, paragraph2].join("\n\n")) + end + end end