2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe DiscoursePoll::PollsUpdater do
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
def update(post, polls)
|
|
|
|
DiscoursePoll::PollsUpdater.update(post, polls)
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:user) { Fabricate(:user) }
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:post) {
|
|
|
|
Fabricate(:post, raw: <<~RAW)
|
|
|
|
[poll]
|
|
|
|
* 1
|
|
|
|
* 2
|
|
|
|
[/poll]
|
2016-06-06 11:04:56 -04:00
|
|
|
RAW
|
2018-11-19 08:50:00 -05:00
|
|
|
}
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:post_with_3_options) {
|
|
|
|
Fabricate(:post, raw: <<~RAW)
|
|
|
|
[poll]
|
|
|
|
- a
|
|
|
|
- b
|
|
|
|
- c
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
}
|
|
|
|
|
|
|
|
let(:post_with_some_attributes) {
|
|
|
|
Fabricate(:post, raw: <<~RAW)
|
|
|
|
[poll close=#{1.week.from_now.to_formatted_s(:iso8601)} results=on_close]
|
|
|
|
- A
|
|
|
|
- B
|
|
|
|
- C
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
}
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:polls) {
|
2016-06-06 11:04:56 -04:00
|
|
|
DiscoursePoll::PollsValidator.new(post).validate_polls
|
2018-11-19 08:50:00 -05:00
|
|
|
}
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:polls_with_3_options) {
|
|
|
|
DiscoursePoll::PollsValidator.new(post_with_3_options).validate_polls
|
|
|
|
}
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
let(:polls_with_some_attributes) {
|
|
|
|
DiscoursePoll::PollsValidator.new(post_with_some_attributes).validate_polls
|
|
|
|
}
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "update" do
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "does nothing when there are no changes" do
|
2019-05-20 04:26:45 -04:00
|
|
|
message = MessageBus.track_publish("/polls/#{post.topic_id}") do
|
2018-11-19 08:50:00 -05:00
|
|
|
update(post, polls)
|
|
|
|
end.first
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(message).to be(nil)
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
|
2018-12-19 05:29:13 -05:00
|
|
|
describe "when editing" do
|
|
|
|
|
|
|
|
let(:raw) do
|
|
|
|
<<~RAW
|
|
|
|
This is a new poll with three options.
|
|
|
|
|
|
|
|
[poll type=multiple results=always min=1 max=2]
|
|
|
|
* first
|
|
|
|
* second
|
|
|
|
* third
|
|
|
|
[/poll]
|
|
|
|
RAW
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post, raw: raw) }
|
|
|
|
|
|
|
|
it "works if poll is closed and unmodified" do
|
2021-10-05 04:38:49 -04:00
|
|
|
DiscoursePoll::Poll.vote(post.user, post.id, "poll", ["e55de753c08b93d04d677ce05e942d3c"])
|
|
|
|
DiscoursePoll::Poll.toggle_status(post.user, post.id, "poll", "closed")
|
2018-12-19 05:29:13 -05:00
|
|
|
|
|
|
|
freeze_time (SiteSetting.poll_edit_window_mins + 1).minutes.from_now
|
|
|
|
update(post, DiscoursePoll::PollsValidator.new(post).validate_polls)
|
|
|
|
|
|
|
|
expect(post.errors[:base].size).to equal(0)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "deletes polls" do
|
2016-06-07 06:55:01 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "that were removed" do
|
|
|
|
update(post, {})
|
2016-06-09 00:10:27 -04:00
|
|
|
|
2016-06-07 06:55:01 -04:00
|
|
|
post.reload
|
2016-06-09 00:10:27 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(Poll.where(post: post).exists?).to eq(false)
|
|
|
|
expect(post.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(nil)
|
2016-06-09 00:10:27 -04:00
|
|
|
end
|
|
|
|
|
2016-06-07 06:55:01 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "creates polls" do
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "that were added" do
|
|
|
|
post = Fabricate(:post)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(Poll.find_by(post: post)).to_not be
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2019-05-20 04:26:45 -04:00
|
|
|
message = MessageBus.track_publish("/polls/#{post.topic_id}") do
|
2018-11-19 08:50:00 -05:00
|
|
|
update(post, polls)
|
|
|
|
end.first
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = Poll.find_by(post: post)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll).to be
|
|
|
|
expect(poll.poll_options.size).to eq(2)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll.post.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(true)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(message.data[:post_id]).to eq(post.id)
|
|
|
|
expect(message.data[:polls][0][:name]).to eq(poll.name)
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "updates polls" do
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "when there are no votes" do
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "at any time" do
|
|
|
|
post # create the post
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
freeze_time 1.month.from_now
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2019-05-20 04:26:45 -04:00
|
|
|
message = MessageBus.track_publish("/polls/#{post.topic_id}") do
|
2018-11-19 08:50:00 -05:00
|
|
|
update(post, polls_with_some_attributes)
|
|
|
|
end.first
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = Poll.find_by(post: post)
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll).to be
|
|
|
|
expect(poll.poll_options.size).to eq(3)
|
|
|
|
expect(poll.poll_votes.size).to eq(0)
|
|
|
|
expect(poll.on_close?).to eq(true)
|
|
|
|
expect(poll.close_at).to be
|
2017-07-13 23:32:26 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll.post.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(true)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(message.data[:post_id]).to eq(post.id)
|
|
|
|
expect(message.data[:polls][0][:name]).to eq(poll.name)
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "when there are votes" do
|
2017-01-16 09:51:57 -05:00
|
|
|
|
2018-03-08 03:45:06 -05:00
|
|
|
before do
|
2018-11-19 08:50:00 -05:00
|
|
|
expect {
|
2021-10-05 04:38:49 -04:00
|
|
|
DiscoursePoll::Poll.vote(user, post.id, "poll", [polls["poll"]["options"][0]["id"]])
|
2018-11-19 08:50:00 -05:00
|
|
|
}.to change { PollVote.count }.by(1)
|
2018-03-08 03:45:06 -05:00
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "inside the edit window" do
|
2018-03-08 03:45:06 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "and deletes the votes" do
|
2019-05-20 04:26:45 -04:00
|
|
|
message = MessageBus.track_publish("/polls/#{post.topic_id}") do
|
2018-11-19 08:50:00 -05:00
|
|
|
update(post, polls_with_some_attributes)
|
|
|
|
end.first
|
2018-03-08 03:45:06 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = Poll.find_by(post: post)
|
2018-03-08 03:45:06 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll).to be
|
|
|
|
expect(poll.poll_options.size).to eq(3)
|
|
|
|
expect(poll.poll_votes.size).to eq(0)
|
|
|
|
expect(poll.on_close?).to eq(true)
|
|
|
|
expect(poll.close_at).to be
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll.post.custom_fields[DiscoursePoll::HAS_POLLS]).to eq(true)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(message.data[:post_id]).to eq(post.id)
|
|
|
|
expect(message.data[:polls][0][:name]).to eq(poll.name)
|
|
|
|
end
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
describe "outside the edit window" do
|
2017-01-16 09:51:57 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
it "throws an error" do
|
|
|
|
edit_window = SiteSetting.poll_edit_window_mins
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
freeze_time (edit_window + 1).minutes.from_now
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
update(post, polls_with_some_attributes)
|
2016-06-06 11:04:56 -04:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
poll = Poll.find_by(post: post)
|
2017-01-16 09:51:57 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(poll).to be
|
|
|
|
expect(poll.poll_options.size).to eq(2)
|
|
|
|
expect(poll.poll_votes.size).to eq(1)
|
|
|
|
expect(poll.on_close?).to eq(false)
|
|
|
|
expect(poll.close_at).to_not be
|
2017-01-16 09:51:57 -05:00
|
|
|
|
2018-11-19 08:50:00 -05:00
|
|
|
expect(post.errors[:base]).to include(
|
|
|
|
I18n.t(
|
|
|
|
"poll.edit_window_expired.cannot_edit_default_poll_with_votes",
|
|
|
|
minutes: edit_window
|
|
|
|
)
|
|
|
|
)
|
2017-01-16 09:51:57 -05:00
|
|
|
end
|
2016-06-06 11:04:56 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2018-11-19 08:50:00 -05:00
|
|
|
|
2016-06-06 11:04:56 -04:00
|
|
|
end
|