Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Error when voting on a multiple poll without the min/max attrs. #15457

Merged
merged 1 commit into from Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions plugins/poll/lib/poll.rb
Expand Up @@ -38,7 +38,10 @@ def self.vote(user, post_id, poll_name, options)

# Ensure consistency here as we do not have a unique index to limit the
# number of votes per the poll's configuration.
DB.query(<<~SQL, poll_id: poll_id, user_id: user.id, offset: serialized_poll[:type] == "multiple" ? serialized_poll[:max] : 1)
is_multiple = serialized_poll[:type] == "multiple"
offset = is_multiple ? (serialized_poll[:max] || serialized_poll[:options].length) : 1

DB.query(<<~SQL, poll_id: poll_id, user_id: user.id, offset: offset)
DELETE FROM poll_votes
USING (
SELECT
Expand Down Expand Up @@ -315,12 +318,12 @@ def self.validate_votes!(poll, options)
num_of_options = options.length

if poll.multiple?
if num_of_options < poll.min
if poll.min && (num_of_options < poll.min)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking, but are poll.min and poll.max nullable, or is 0 a valid option for them? Because if 0 is a valid option then these may not work correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 is not a valid option for a poll because it just mean that the user is not submitting any votes.

raise DiscoursePoll::Error.new(I18n.t(
"poll.min_vote_per_user",
count: poll.min
))
elsif num_of_options > poll.max
elsif poll.max && (num_of_options > poll.max)
raise DiscoursePoll::Error.new(I18n.t(
"poll.max_vote_per_user",
count: poll.max
Expand Down
48 changes: 48 additions & 0 deletions plugins/poll/spec/lib/poll_spec.rb
Expand Up @@ -127,6 +127,54 @@
I18n.t("poll.min_vote_per_user", count: poll.min)
)
end

it 'should allow user to vote on a multiple poll even if min option is not configured' do
post_with_multiple_poll = Fabricate(:post, raw: <<~RAW)
[poll type=multiple max=3]
* 1
* 2
* 3
* 4
* 5
[/poll]
RAW

poll = post_with_multiple_poll.polls.first

DiscoursePoll::Poll.vote(
user,
post_with_multiple_poll.id,
"poll",
[poll.poll_options.first.digest]
)

expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id))
.to contain_exactly(poll.poll_options.first.id)
end

it 'should allow user to vote on a multiple poll even if max option is not configured' do
post_with_multiple_poll = Fabricate(:post, raw: <<~RAW)
[poll type=multiple min=1]
* 1
* 2
* 3
* 4
* 5
[/poll]
RAW

poll = post_with_multiple_poll.polls.first

DiscoursePoll::Poll.vote(
user,
post_with_multiple_poll.id,
"poll",
[poll.poll_options.first.digest, poll.poll_options.second.digest]
)

expect(PollVote.where(poll: poll, user: user).pluck(:poll_option_id))
.to contain_exactly(poll.poll_options.first.id, poll.poll_options.second.id)
end
end

describe "post_created" do
Expand Down