A platform for community discussion. Free, open, simple.
FIX: Add a blank poll options validation (#8652)
Jarek Radosz9e3fc1111d97
diff --git a/plugins/poll/config/locales/server.en.yml b/plugins/poll/config/locales/server.en.ymlindex 6fe344fddfb..a90e12313b6 100644--- a/plugins/poll/config/locales/server.en.yml+++ b/plugins/poll/config/locales/server.en.yml@@ -43,6 +43,9 @@ en:default_poll_must_have_different_options: "Poll must have different options."named_poll_must_have_different_options: "Poll named <strong>%{name}</strong> must have different options."+ default_poll_must_not_have_any_empty_options: "Poll must not have any empty options."+ named_poll_must_not_have_any_empty_options: "Poll named <strong>%{name}</strong> must not have any empty options."+default_poll_with_multiple_choices_has_invalid_parameters: "Poll with multiple choices has invalid parameters."named_poll_with_multiple_choices_has_invalid_parameters: "Poll named <strong>%{name}</strong> with multiple choice has invalid parameters."diff --git a/plugins/poll/lib/polls_validator.rb b/plugins/poll/lib/polls_validator.rbindex 58ec21977a2..88712bc6da4 100644--- a/plugins/poll/lib/polls_validator.rb+++ b/plugins/poll/lib/polls_validator.rb@@ -17,6 +17,7 @@ module DiscoursePollreturn false unless valid_numbers?(poll)return false unless unique_poll_name?(polls, poll)return false unless unique_options?(poll)+ return false unless any_blank_options?(poll)return false unless at_least_two_options?(poll)return false unless valid_number_of_options?(poll)return false unless valid_multiple_choice_settings?(poll)@@ -77,6 +78,20 @@ module DiscoursePolltrueend+ def any_blank_options?(poll)+ if poll["options"].any? { |o| o["html"].blank? }+ if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME+ @post.errors.add(:base, I18n.t("poll.default_poll_must_not_have_any_empty_options"))+ else+ @post.errors.add(:base, I18n.t("poll.named_poll_must_not_have_any_empty_options", name: poll["name"]))+ end++ return false+ end++ true+ end+def at_least_two_options?(poll)if poll["options"].size < 2if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAMEdiff --git a/plugins/poll/spec/lib/polls_validator_spec.rb b/plugins/poll/spec/lib/polls_validator_spec.rbindex 6dfe59a144c..46048806e25 100644--- a/plugins/poll/spec/lib/polls_validator_spec.rb+++ b/plugins/poll/spec/lib/polls_validator_spec.rb@@ -124,6 +124,36 @@ describe ::DiscoursePoll::PollsValidator do)end+ it "ensures that polls do not have any blank options" do+ raw = <<~RAW+ [poll]+ * 1+ *+ [/poll]+ RAW++ post.raw = raw+ expect(post.valid?).to eq(false)++ expect(post.errors[:base]).to include(+ I18n.t("poll.default_poll_must_not_have_any_empty_options")+ )++ raw = <<~RAW+ [poll name=test]+ *+ * 1+ [/poll]+ RAW++ post.raw = raw+ expect(post.valid?).to eq(false)++ expect(post.errors[:base]).to include(+ I18n.t("poll.named_poll_must_not_have_any_empty_options", name: "test")+ )+ end+it "ensure that polls have at least 2 options" doraw = <<~RAW[poll]