A platform for community discussion. Free, open, simple.
FIX: Prevent empty top menu configuration (#41710)
Roman Rizzid1c05c8b9f82
diff --git a/lib/validators/top_menu_validator.rb b/lib/validators/top_menu_validator.rbindex 729739b87b7..a8fe269f947 100644--- a/lib/validators/top_menu_validator.rb+++ b/lib/validators/top_menu_validator.rb@@ -6,21 +6,31 @@ class TopMenuValidatorenddef valid_value?(val)- return true if val.blank?+ @error_message = "site_settings.errors.invalid_string"++ if val.blank?+ @error_message = "site_settings.errors.must_include_latest"+ return false+ end+val_choices = val.split("|").map(&:strip).compact- return false if !val_choices.all? { |choice| TopMenu.choices.include?(choice) }+ if val_choices.include?("unread") && UpcomingChanges.enabled?(:enable_unified_new)+ @error_message = "site_settings.errors.top_menu_unread_not_allowed_with_unified_new"+ return false+ end- return false if !val_choices.include?("latest")+ return false if !val_choices.all? { |choice| TopMenu.choices.include?(choice) }- val_choices.each do |choice|- return false if choice == "unread" && UpcomingChanges.enabled?(:enable_unified_new)+ if !val_choices.include?("latest")+ @error_message = "site_settings.errors.must_include_latest"+ return falseendtrueenddef error_message- I18n.t("site_settings.errors.top_menu_unread_not_allowed_with_unified_new")+ I18n.t(@error_message)endenddiff --git a/spec/lib/validators/top_menu_validator_spec.rb b/spec/lib/validators/top_menu_validator_spec.rbindex 3a02bbf103e..ec67cb3fd1e 100644--- a/spec/lib/validators/top_menu_validator_spec.rb+++ b/spec/lib/validators/top_menu_validator_spec.rb@@ -4,13 +4,17 @@ RSpec.describe TopMenuValidator dodescribe "#valid_value?" dosubject(:validator) { described_class.new }- it "returns true for blank values" do- expect(validator.valid_value?("")).to eq(true)- expect(validator.valid_value?(nil)).to eq(true)+ it "returns false for blank values" do+ expect(validator.valid_value?("")).to eq(false)+ expect(validator.error_message).to eq(I18n.t("site_settings.errors.must_include_latest"))++ expect(validator.valid_value?(nil)).to eq(false)+ expect(validator.error_message).to eq(I18n.t("site_settings.errors.must_include_latest"))endit "returns false when latest is missing" doexpect(validator.valid_value?("categories|new")).to eq(false)+ expect(validator.error_message).to eq(I18n.t("site_settings.errors.must_include_latest"))endit "returns false when a choice is not in TopMenu.choices" do@@ -25,6 +29,9 @@ RSpec.describe TopMenuValidator doSiteSetting.enable_unified_new = trueexpect(validator.valid_value?("latest|new|unread|hot|categories")).to eq(false)+ expect(validator.error_message).to eq(+ I18n.t("site_settings.errors.top_menu_unread_not_allowed_with_unified_new"),+ )endit "returns true when unread is included and unified new is disabled" do@@ -33,14 +40,4 @@ RSpec.describe TopMenuValidator doexpect(validator.valid_value?("latest|new|unread|hot|categories")).to eq(true)endend-- describe "#error_message" do- subject(:validator) { described_class.new }-- it "returns the unread not allowed message" do- expect(validator.error_message).to eq(- I18n.t("site_settings.errors.top_menu_unread_not_allowed_with_unified_new"),- )- end- endenddiff --git a/spec/models/site_setting_spec.rb b/spec/models/site_setting_spec.rbindex b7bfe9ed273..6188b3ee824 100644--- a/spec/models/site_setting_spec.rb+++ b/spec/models/site_setting_spec.rb@@ -57,6 +57,10 @@ RSpec.describe SiteSetting do)end+ it "does not allow an empty menu" do+ expect do SiteSetting.top_menu = "" end.to raise_error(Discourse::InvalidParameters)+ end+it "does not allow random text" doexpect do SiteSetting.top_menu = "latest|random" end.to raise_error(Discourse::InvalidParameters,