2019-11-26 01:51:01 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe ::DiscoursePoll::Poll do
|
2019-11-26 01:51:01 +08:00
|
|
|
describe ".transform_for_user_field_override" do
|
|
|
|
it "Transforms UserField name if a matching CustomUserField is present" do
|
|
|
|
user_field_name = "Something Cool"
|
|
|
|
user_field = Fabricate(:user_field, name: user_field_name)
|
|
|
|
expect(::DiscoursePoll::Poll.transform_for_user_field_override(user_field_name)).to eq(
|
|
|
|
"user_field_#{user_field.id}",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not transform UserField name if a matching CustomUserField is not present" do
|
|
|
|
user_field_name = "Something Cool"
|
|
|
|
user_field = Fabricate(:user_field, name: "Something Else!")
|
|
|
|
expect(::DiscoursePoll::Poll.transform_for_user_field_override(user_field_name)).to eq(
|
|
|
|
user_field_name,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2019-12-24 05:04:35 +08:00
|
|
|
|
|
|
|
describe "can see results?" do
|
|
|
|
it "everyone can see results when results setting is always" do
|
|
|
|
post = Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]")
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect(post.polls.first.can_see_results?(user)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "users who voted can see results when results setting is on_vote" do
|
|
|
|
post = Fabricate(:post, raw: "[poll results=on_vote]\n- A\n- B\n[/poll]")
|
|
|
|
user = Fabricate(:user)
|
|
|
|
poll = post.polls.first
|
|
|
|
option = poll.poll_options.first
|
|
|
|
|
|
|
|
expect(poll.can_see_results?(user)).to eq(false)
|
2020-03-21 02:29:00 +08:00
|
|
|
poll.poll_votes.create!(poll_option_id: option.id, user_id: user.id)
|
2019-12-24 05:04:35 +08:00
|
|
|
expect(poll.can_see_results?(user)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2020-03-21 02:29:00 +08:00
|
|
|
it "author can see results when results setting is on_vote" do
|
2024-01-23 09:35:14 +08:00
|
|
|
author = Fabricate(:user, refresh_auto_groups: true)
|
2020-03-21 02:29:00 +08:00
|
|
|
post = Fabricate(:post, user: author, raw: "[poll results=on_vote]\n- A\n- B\n[/poll]")
|
|
|
|
poll = post.polls.first
|
|
|
|
option = poll.poll_options.first
|
|
|
|
|
|
|
|
expect(poll.can_see_results?(author)).to eq(true)
|
|
|
|
poll.poll_votes.create!(poll_option_id: option.id, user_id: author.id)
|
|
|
|
expect(poll.can_see_results?(author)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2019-12-24 05:04:35 +08:00
|
|
|
it "everyone can see results when results setting is on_vote and poll is closed" do
|
|
|
|
post = Fabricate(:post, raw: "[poll results=on_vote]\n- A\n- B\n[/poll]")
|
|
|
|
user = Fabricate(:user)
|
|
|
|
poll = post.polls.first
|
|
|
|
|
|
|
|
expect(poll.can_see_results?(user)).to eq(false)
|
|
|
|
poll.update(close_at: Date.yesterday)
|
|
|
|
expect(poll.can_see_results?(user)).to eq(true)
|
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
it "only staff members can see results when results setting is staff_only" do
|
2019-12-24 05:04:35 +08:00
|
|
|
post = Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]")
|
2024-01-23 09:35:14 +08:00
|
|
|
user = Fabricate(:user, refresh_auto_groups: true)
|
2019-12-24 05:04:35 +08:00
|
|
|
poll = post.polls.first
|
|
|
|
option = poll.poll_options.first
|
|
|
|
|
|
|
|
expect(poll.can_see_results?(user)).to eq(false)
|
|
|
|
poll.poll_votes.create!(poll_option_id: option.id, user_id: user.id)
|
|
|
|
expect(poll.can_see_results?(user)).to eq(false)
|
|
|
|
user.update!(moderator: true)
|
|
|
|
expect(poll.can_see_results?(user)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2020-04-28 21:43:09 +08:00
|
|
|
|
|
|
|
describe "when post is trashed" do
|
|
|
|
it "maintains the association" do
|
2024-01-23 09:35:14 +08:00
|
|
|
user = Fabricate(:user, refresh_auto_groups: true)
|
2020-04-28 21:43:09 +08:00
|
|
|
post = Fabricate(:post, raw: "[poll results=staff_only]\n- A\n- B\n[/poll]", user: user)
|
|
|
|
poll = post.polls.first
|
|
|
|
|
|
|
|
post.trash!
|
|
|
|
poll.reload
|
|
|
|
|
|
|
|
expect(poll.post).to eq(post)
|
|
|
|
end
|
2021-05-27 06:39:51 +08:00
|
|
|
end
|
2020-04-28 21:43:09 +08:00
|
|
|
|
2021-05-27 06:39:51 +08:00
|
|
|
it "is not throwing an error when double save" do
|
|
|
|
post = Fabricate(:post, raw: "[poll]\n- A\n- B\n[/poll]")
|
|
|
|
expect { post.save! }.not_to raise_error
|
2020-04-28 21:43:09 +08:00
|
|
|
end
|
2019-11-26 01:51:01 +08:00
|
|
|
end
|