From 9e3fc1111d970f5b8bfce210d38c4c76ddb87526 Mon Sep 17 00:00:00 2001 From: Jarek Radosz Date: Fri, 3 Jan 2020 15:09:36 +0100 Subject: [PATCH] FIX: Add a blank poll options validation (#8652) Before, saving a post with a blank poll option resulted in error 500. --- plugins/poll/config/locales/server.en.yml | 3 ++ plugins/poll/lib/polls_validator.rb | 15 ++++++++++ plugins/poll/spec/lib/polls_validator_spec.rb | 30 +++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/plugins/poll/config/locales/server.en.yml b/plugins/poll/config/locales/server.en.yml index 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 %{name} 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 %{name} 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 %{name} with multiple choice has invalid parameters." diff --git a/plugins/poll/lib/polls_validator.rb b/plugins/poll/lib/polls_validator.rb index 58ec21977a2..88712bc6da4 100644 --- a/plugins/poll/lib/polls_validator.rb +++ b/plugins/poll/lib/polls_validator.rb @@ -17,6 +17,7 @@ module DiscoursePoll return 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 DiscoursePoll true end + 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 < 2 if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME diff --git a/plugins/poll/spec/lib/polls_validator_spec.rb b/plugins/poll/spec/lib/polls_validator_spec.rb index 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" do raw = <<~RAW [poll]