discourse/plugins/poll/spec/lib/polls_validator_spec.rb
Régis Hanol 4459665dee
REFACTOR: use tables instead of custom fields for polls (#6359)
Co-authored-by: Guo Xiang Tan <tgx_world@hotmail.com>
2018-11-19 14:50:00 +01:00

257 lines
5.6 KiB
Ruby

require "rails_helper"
describe ::DiscoursePoll::PollsValidator do
let(:post) { Fabricate(:post) }
subject { described_class.new(post) }
describe "#validate_polls" do
it "ensure that polls have unique names" do
raw = <<~RAW
[poll]
* 1
* 2
[/poll]
[poll]
* 1
* 2
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.multiple_polls_without_name")
)
raw = <<~RAW
[poll name=test]
* 1
* 2
[/poll]
[poll name=test]
* 1
* 2
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.multiple_polls_with_same_name", name: "test")
)
end
it "ensure that polls have unique options" do
raw = <<~RAW
[poll]
* 1
* 1
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.default_poll_must_have_different_options")
)
raw = <<~RAW
[poll name=test]
* 1
* 1
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.named_poll_must_have_different_options", name: "test")
)
end
it "ensure that polls have at least 2 options" do
raw = <<~RAW
[poll]
* 1
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.default_poll_must_have_at_least_2_options")
)
raw = <<~RAW
[poll name=test]
* 1
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.named_poll_must_have_at_least_2_options", name: "test")
)
end
it "ensure that polls options do not exceed site settings" do
SiteSetting.poll_maximum_options = 2
raw = <<~RAW
[poll]
* 1
* 2
* 3
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(I18n.t(
"poll.default_poll_must_have_less_options",
count: SiteSetting.poll_maximum_options
))
raw = <<~RAW
[poll name=test]
* 1
* 2
* 3
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(I18n.t(
"poll.named_poll_must_have_less_options",
name: "test", count: SiteSetting.poll_maximum_options
))
end
describe "multiple type polls" do
it "ensure that min < max" do
raw = <<~RAW
[poll type=multiple min=2 max=1]
* 1
* 2
* 3
[/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")
)
raw = <<~RAW
[poll type=multiple min=2 max=1 name=test]
* 1
* 2
* 3
[/poll]
RAW
expect(post.update_attributes(raw: raw)).to eq(false)
expect(post.errors[:base]).to include(
I18n.t("poll.named_poll_with_multiple_choices_has_invalid_parameters", name: "test")
)
end
it "ensure max > 0" do
raw = <<~RAW
[poll type=multiple max=-2]
* 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 "ensure that max <= number of options" do
raw = <<~RAW
[poll type=multiple max=3]
* 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 "ensure that min > 0" do
raw = <<~RAW
[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 "ensure that min != 0" do
raw = <<~RAW
[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 "ensure that min != number of options" do
raw = <<~RAW
[poll type=multiple min=2]
* 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 "ensure that min < number of options" do
raw = <<~RAW
[poll type=multiple min=3]
* 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
end
end
end