From 2f14a81fa4a5b38043512d5668d9754ab5f8400e Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 7 Jun 2016 10:48:06 +0800 Subject: [PATCH] FIX: `min` setting should be greater than 0 for multiple type polls. --- plugins/poll/lib/polls_validator.rb | 5 +-- plugins/poll/spec/lib/polls_validator_spec.rb | 34 ++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/plugins/poll/lib/polls_validator.rb b/plugins/poll/lib/polls_validator.rb index cb887616634..c02920bf383 100644 --- a/plugins/poll/lib/polls_validator.rb +++ b/plugins/poll/lib/polls_validator.rb @@ -92,10 +92,11 @@ module DiscoursePoll def valid_multiple_choice_settings?(poll) if poll["type"] == "multiple" + num_of_options = poll["options"].size min = (poll["min"].presence || 1).to_i - max = (poll["max"].presence || poll["options"].size).to_i + max = (poll["max"].presence || num_of_options).to_i - if min > max || max <= 0 || max > poll["options"].size || min >= poll["options"].size + if min > max || min <= 0 || max <= 0 || max > num_of_options || min >= num_of_options if poll["name"] == ::DiscoursePoll::DEFAULT_POLL_NAME @post.errors.add(:base, I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")) else diff --git a/plugins/poll/spec/lib/polls_validator_spec.rb b/plugins/poll/spec/lib/polls_validator_spec.rb index 7278f3fdd68..d5178fe854b 100644 --- a/plugins/poll/spec/lib/polls_validator_spec.rb +++ b/plugins/poll/spec/lib/polls_validator_spec.rb @@ -193,7 +193,37 @@ describe ::DiscoursePoll::PollsValidator do ) end - it "should ensure that min settings is smaller than the number of options" do + it "should ensure that min settings is not negative" do + raw = <<-RAW.strip_heredoc + [poll type=multiple min=-1] + * 1 + * 2 + [/poll] + RAW + + expect(post.update_attributes(raw: raw)).to eq(false) + + expect(post.errors[:base]).to include( + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + ) + end + + it "should ensure that min settings it not equal to zero" do + raw = <<-RAW.strip_heredoc + [poll type=multiple min=0] + * 1 + * 2 + [/poll] + RAW + + expect(post.update_attributes(raw: raw)).to eq(false) + + expect(post.errors[:base]).to include( + I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") + ) + end + + it "should ensure that min settings is not equal to the number of options" do raw = <<-RAW.strip_heredoc [poll type=multiple min=2] * 1 @@ -206,7 +236,9 @@ describe ::DiscoursePoll::PollsValidator do expect(post.errors[:base]).to include( I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters") ) + end + it "should ensure that min settings is not greater than the number of options" do raw = <<-RAW.strip_heredoc [poll type=multiple min=3] * 1