0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-08 15:55:38 +08:00
discourse/plugins/discourse-topic-voting/spec/lib/topic_extension_spec.rb
Régis Hanol 73eb2a0b66
FIX: Show voters for closed topics in topic-voting (#40314)
Previously, opening the "who voted" popup on a closed topic returned an
empty list even though the vote count still showed the correct total.
The same was true for any topic whose votes had been archived (e.g.
moved out of a voting category, or trashed).

This was a regression from #39394, which added `votes.active` to
`Topic#who_voted` alongside the same filter applied to "my votes" and
`/topics/voted-by/:username`. That filter is correct for those per-user
listings — you don't want closed-topic votes polluting "your votes" —
but the `who_voted` popup is about historical participation on a
specific topic, not per-user vote accounting.

This change drops `.active` from `Topic#who_voted` so archived votes are
included, restoring the pre-regression behaviour. The accounting filters
in `Votes::Remove`, `User#topics_with_active_vote`, the voted-topics
query, and the `current_user_voted` flag are unchanged.

https://meta.discourse.org/t/403286

Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 19:18:25 +02:00

67 lines
2.2 KiB
Ruby
Vendored

# frozen_string_literal: true
describe DiscourseTopicVoting::TopicExtension do
let(:user) { Fabricate(:user) }
let(:user2) { Fabricate(:user) }
let(:topic) { Fabricate(:topic) }
let(:topic2) { Fabricate(:topic) }
before do
SiteSetting.topic_voting_enabled = true
SiteSetting.topic_voting_show_who_voted = true
end
describe "#update_vote_count" do
it "upserts topic votes count" do
topic.update_vote_count
topic2.update_vote_count
expect(topic.reload.topic_vote_count.votes_count).to eq(0)
expect(topic2.reload.topic_vote_count.votes_count).to eq(0)
DiscourseTopicVoting::Vote.create!(user: user, topic: topic)
topic.update_vote_count
topic2.update_vote_count
expect(topic.reload.topic_vote_count.votes_count).to eq(1)
expect(topic2.reload.topic_vote_count.votes_count).to eq(0)
DiscourseTopicVoting::Vote.create!(user: user2, topic: topic)
DiscourseTopicVoting::Vote.create!(user: user, topic: topic2)
topic.update_vote_count
topic2.update_vote_count
expect(topic.reload.topic_vote_count.votes_count).to eq(2)
expect(topic2.reload.topic_vote_count.votes_count).to eq(1)
end
end
describe "#who_voted" do
it "returns the most recent voters up to the limit" do
DiscourseTopicVoting::Vote.create!(user:, topic:, created_at: 2.hours.ago)
DiscourseTopicVoting::Vote.create!(user: user2, topic:, created_at: 1.hour.ago)
expect(topic.who_voted(limit: 1)).to eq([user2])
end
it "includes voters whose votes were archived (e.g. closed topics)" do
archived_user = Fabricate(:user)
DiscourseTopicVoting::Vote.create!(user: archived_user, topic:, archive: true)
expect(topic.who_voted(limit: 10)).to eq([archived_user])
end
end
describe "topic associations" do
it "keeps soft-deleted topics available from votes and vote counts" do
vote = DiscourseTopicVoting::Vote.create!(user: user, topic: topic)
topic_vote_count = DiscourseTopicVoting::TopicVoteCount.create!(topic: topic, votes_count: 1)
topic.trash!
expect(vote.reload.topic).to eq(topic)
expect(topic_vote_count.reload.topic).to eq(topic)
end
end
end