2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
require "rails_helper"
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
describe ::DiscoursePoll::PollsValidator do
|
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
subject { described_class.new(post) }
|
|
|
|
|
|
|
|
describe "#validate_polls" do
|
2018-12-31 04:48:30 -05:00
|
|
|
it "ensures that polls have valid arguments" do
|
|
|
|
raw = <<~RAW
|
|
|
|
[poll type=not_good1 status=not_good2 results=not_good3]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(I18n.t("poll.invalid_argument", argument: "type", value: "not_good1"))
|
|
|
|
expect(post.errors[:base]).to include(I18n.t("poll.invalid_argument", argument: "status", value: "not_good2"))
|
|
|
|
expect(post.errors[:base]).to include(I18n.t("poll.invalid_argument", argument: "results", value: "not_good3"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ensures that all possible values are valid" do
|
|
|
|
raw = <<~RAW
|
|
|
|
[poll type=regular result=always]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(true)
|
|
|
|
|
|
|
|
raw = <<~RAW
|
|
|
|
[poll type=multiple result=on_vote min=1 max=2]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* 3
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(true)
|
|
|
|
|
|
|
|
raw = <<~RAW
|
|
|
|
[poll type=number result=on_close min=3 max=7]
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that polls have unique names" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
|
|
|
|
[poll]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.multiple_polls_without_name")
|
|
|
|
)
|
|
|
|
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll name=test]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
|
|
|
|
[poll name=test]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
2018-11-19 08:50:00 -05:00
|
|
|
I18n.t("poll.multiple_polls_with_same_name", name: "test")
|
2016-06-06 11:04:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that polls have unique options" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll]
|
|
|
|
* 1
|
|
|
|
* 1
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_must_have_different_options")
|
|
|
|
)
|
|
|
|
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll name=test]
|
|
|
|
* 1
|
|
|
|
* 1
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
2018-11-19 08:50:00 -05:00
|
|
|
I18n.t("poll.named_poll_must_have_different_options", name: "test")
|
2016-06-06 11:04:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that polls have at least 2 options" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll]
|
|
|
|
* 1
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_must_have_at_least_2_options")
|
|
|
|
)
|
|
|
|
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll name=test]
|
|
|
|
* 1
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
2018-11-19 08:50:00 -05:00
|
|
|
I18n.t("poll.named_poll_must_have_at_least_2_options", name: "test")
|
2016-06-06 11:04:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that polls options do not exceed site settings" do
|
2016-06-06 11:04:56 -04:00
|
|
|
SiteSetting.poll_maximum_options = 2
|
|
|
|
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* 3
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(I18n.t(
|
|
|
|
"poll.default_poll_must_have_less_options",
|
|
|
|
count: SiteSetting.poll_maximum_options
|
|
|
|
))
|
|
|
|
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll name=test]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* 3
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(I18n.t(
|
|
|
|
"poll.named_poll_must_have_less_options",
|
2018-11-19 08:50:00 -05:00
|
|
|
name: "test", count: SiteSetting.poll_maximum_options
|
2016-06-06 11:04:56 -04:00
|
|
|
))
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "multiple type polls" do
|
|
|
|
it "ensure that min < max" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll type=multiple min=2 max=1]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* 3
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")
|
|
|
|
)
|
|
|
|
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll type=multiple min=2 max=1 name=test]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
* 3
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
2018-11-19 08:50:00 -05:00
|
|
|
I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: "test")
|
2016-06-06 11:04:56 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure max > 0" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll type=multiple max=-2]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that max <= number of options" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll type=multiple max=3]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
it "ensure that min >= 0" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 22:48:06 -04:00
|
|
|
[poll type=multiple min=-1]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 22:48:06 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
it "ensure that min cannot be 0" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 22:48:06 -04:00
|
|
|
[poll type=multiple min=0]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 22:48:06 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that min != number of options" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll type=multiple min=2]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")
|
|
|
|
)
|
2016-06-06 22:48:06 -04:00
|
|
|
end
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "ensure that min < number of options" do
|
2017-07-17 17:42:13 -04:00
|
|
|
raw = <<~RAW
|
2016-06-06 11:04:56 -04:00
|
|
|
[poll type=multiple min=3]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
2018-12-31 04:48:30 -05:00
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t("poll.default_poll_with_multiple_choices_has_invalid_parameters")
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2018-12-31 04:48:30 -05:00
|
|
|
|
|
|
|
it "number type polls are validated" do
|
|
|
|
raw = <<~RAW
|
|
|
|
[poll type=number min=-5 max=-10 step=-1]
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
|
|
|
expect(post.errors[:base]).to include("Min #{I18n.t("errors.messages.greater_than", count: 0)}")
|
|
|
|
expect(post.errors[:base]).to include("Max #{I18n.t("errors.messages.greater_than", count: "min")}")
|
|
|
|
expect(post.errors[:base]).to include("Step #{I18n.t("errors.messages.greater_than", count: 0)}")
|
|
|
|
|
|
|
|
raw = <<~RAW
|
|
|
|
[poll type=number min=9999999999 max=9999999999 step=1]
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
|
|
|
|
post.raw = raw
|
|
|
|
expect(post.valid?).to eq(false)
|
|
|
|
expect(post.errors[:base]).to include("Min #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}")
|
|
|
|
expect(post.errors[:base]).to include("Max #{I18n.t("errors.messages.less_than", count: 2_147_483_647)}")
|
|
|
|
expect(post.errors[:base]).to include(I18n.t("poll.default_poll_must_have_at_least_2_options"))
|
|
|
|
end
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
end
|