2023-06-15 11:18:51 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
RSpec.describe UserMerger do
|
|
|
|
fab!(:target_user) { Fabricate(:user, username: "galahad", email: "galahad@knights.com") }
|
|
|
|
fab!(:source_user) { Fabricate(:user, username: "lancelot", email: "lancelot@knights.com") }
|
|
|
|
|
2023-06-27 18:58:16 +08:00
|
|
|
fab!(:poll_regular) { Fabricate(:poll) }
|
|
|
|
fab!(:poll_regular_option1) { Fabricate(:poll_option, poll: poll_regular, html: "Option 1") }
|
|
|
|
fab!(:poll_regular_option2) { Fabricate(:poll_option, poll: poll_regular, html: "Option 2") }
|
|
|
|
|
|
|
|
fab!(:poll_multiple) { Fabricate(:poll) }
|
|
|
|
fab!(:poll_multiple_optionA) { Fabricate(:poll_option, poll: poll_multiple, html: "Option A") }
|
|
|
|
fab!(:poll_multiple_optionB) { Fabricate(:poll_option, poll: poll_multiple, html: "Option B") }
|
|
|
|
fab!(:poll_multiple_optionC) { Fabricate(:poll_option, poll: poll_multiple, html: "Option C") }
|
|
|
|
|
|
|
|
it "will end up with no votes from source user" do
|
|
|
|
Fabricate(:poll_vote, poll: poll_regular, user: source_user, poll_option: poll_regular_option2)
|
|
|
|
Fabricate(
|
|
|
|
:poll_vote,
|
|
|
|
poll: poll_multiple,
|
|
|
|
user: source_user,
|
|
|
|
poll_option: poll_multiple_optionB,
|
|
|
|
)
|
2023-06-15 11:18:51 +08:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:merging_users, source_user, target_user)
|
|
|
|
|
|
|
|
expect(PollVote.where(user: source_user).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2023-06-27 18:58:16 +08:00
|
|
|
it "will not use source user's vote if target_user already voted in the same poll" do
|
|
|
|
Fabricate(:poll_vote, poll: poll_regular, user: target_user, poll_option: poll_regular_option1)
|
|
|
|
Fabricate(:poll_vote, poll: poll_regular, user: source_user, poll_option: poll_regular_option2)
|
|
|
|
|
|
|
|
Fabricate(
|
|
|
|
:poll_vote,
|
|
|
|
poll: poll_multiple,
|
|
|
|
user: target_user,
|
|
|
|
poll_option: poll_multiple_optionA,
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:poll_vote,
|
|
|
|
poll: poll_multiple,
|
|
|
|
user: source_user,
|
|
|
|
poll_option: poll_multiple_optionB,
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:poll_vote,
|
|
|
|
poll: poll_multiple,
|
|
|
|
user: source_user,
|
|
|
|
poll_option: poll_multiple_optionC,
|
|
|
|
)
|
2023-06-15 11:18:51 +08:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:merging_users, source_user, target_user)
|
|
|
|
|
2023-06-27 18:58:16 +08:00
|
|
|
expect(PollVote.where(user: target_user).pluck(:poll_option_id)).to contain_exactly(
|
|
|
|
poll_multiple_optionA.id,
|
|
|
|
poll_regular_option1.id,
|
|
|
|
)
|
2023-06-15 11:18:51 +08:00
|
|
|
end
|
|
|
|
|
2023-06-27 18:58:16 +08:00
|
|
|
it "reassigns source_user vote to target_user if target user has never voted in the poll" do
|
|
|
|
Fabricate(:poll_vote, poll: poll_regular, user: source_user)
|
2023-06-15 11:18:51 +08:00
|
|
|
|
|
|
|
expect { DiscourseEvent.trigger(:merging_users, source_user, target_user) }.to change(
|
|
|
|
PollVote.where(user: target_user),
|
|
|
|
:count,
|
|
|
|
).from(0).to(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "keeps any existing target_user votes" do
|
2023-06-27 18:58:16 +08:00
|
|
|
Fabricate(:poll_vote, poll: poll_regular, user: target_user)
|
2023-06-15 11:18:51 +08:00
|
|
|
|
|
|
|
expect { DiscourseEvent.trigger(:merging_users, source_user, target_user) }.to not_change(
|
|
|
|
PollVote.where(user: target_user),
|
|
|
|
:count,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|