From c52e68a0c853d9418e81252665305c9ae92e17f9 Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 15 Nov 2018 12:20:48 +0100 Subject: [PATCH] FIX: better handling of missing welcome topic in wizard (#6606) --- config/locales/server.en.yml | 1 + lib/wizard/builder.rb | 18 +++++++---- spec/components/wizard/wizard_builder_spec.rb | 32 +++++++++++++++++++ 3 files changed, 44 insertions(+), 7 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 7910228739c..78c62221ea9 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -3999,6 +3999,7 @@ en: introduction: title: "Introduction" + disabled: "We couldn’t find any Welcome Topic. Create a topic with `Welcome to Discourse` as title to fix this issue." fields: welcome: diff --git a/lib/wizard/builder.rb b/lib/wizard/builder.rb index fd46229086a..38b7405b1a5 100644 --- a/lib/wizard/builder.rb +++ b/lib/wizard/builder.rb @@ -46,15 +46,19 @@ class Wizard @wizard.append_step('introduction') do |step| introduction = IntroductionUpdater.new(@wizard.user) - step.add_field(id: 'welcome', type: 'textarea', required: true, value: introduction.get_summary) + if @wizard.completed_steps?('introduction') && !introduction.get_summary + step.disabled = true + else + step.add_field(id: 'welcome', type: 'textarea', required: true, value: introduction.get_summary) - step.on_update do |updater| - value = updater.fields[:welcome].strip + step.on_update do |updater| + value = updater.fields[:welcome].strip - if value.index("\n") - updater.errors.add(:welcome, I18n.t("wizard.step.introduction.fields.welcome.one_paragraph")) - else - introduction.update_summary(value) + if value.index("\n") + updater.errors.add(:welcome, I18n.t("wizard.step.introduction.fields.welcome.one_paragraph")) + else + introduction.update_summary(value) + end end end end diff --git a/spec/components/wizard/wizard_builder_spec.rb b/spec/components/wizard/wizard_builder_spec.rb index 25f4d7aa6a6..a9cc3f0638c 100644 --- a/spec/components/wizard/wizard_builder_spec.rb +++ b/spec/components/wizard/wizard_builder_spec.rb @@ -37,4 +37,36 @@ describe Wizard::Builder do expect(invites_step.disabled).to be_truthy end + context 'introduction step' do + let(:wizard) { Wizard::Builder.new(moderator).build } + let(:introduction_step) { wizard.steps.find { |s| s.id == 'introduction' } } + + context 'step has not been completed' do + it 'enables the step' do + expect(introduction_step.disabled).to be_nil + end + end + + context 'step has been completed' do + before do + wizard = Wizard::Builder.new(moderator).build + introduction_step = wizard.steps.find { |s| s.id == 'introduction' } + + # manually sets the step as completed + logger = StaffActionLogger.new(moderator) + logger.log_wizard_step(introduction_step) + end + + it 'disables step if no welcome topic' do + expect(introduction_step.disabled).to eq(true) + end + + it 'enables step if welcome topic is present' do + topic = Fabricate(:topic, title: 'Welcome to Discourse') + welcome_post = Fabricate(:post, topic: topic, raw: "this will be the welcome topic post\n\ncool!") + + expect(introduction_step.disabled).to be_nil + end + end + end end