discourse/plugins/discourse-solved/spec/lib/queries_spec.rb
David Battersby ce377f6949
DEV: add service objects for accept_answer and unaccept_answer (#37878)
Extracts `accept_answer!` and `unaccept_answer!` from plugin.rb and
moves the logic into separate service objects, along with adding more
detailed test coverage for each service.

---------

Co-authored-by: Loïc Guitaut <loic@discourse.org>
2026-03-04 12:34:37 +04:00

71 lines
2.6 KiB
Ruby

# frozen_string_literal: true
describe DiscourseSolved::Queries do
fab!(:user)
fab!(:admin)
describe ".solved_count" do
before { SiteSetting.allow_solved_on_all_topics = true }
it "returns the correct count of solved topics for a user" do
expect(described_class.solved_count(user.id)).to eq(0)
topic1 = Fabricate(:topic)
Fabricate(:post, topic: topic1)
post1 = Fabricate(:post, topic: topic1, user: user)
DiscourseSolved::AcceptAnswer.call!(params: { post_id: post1.id }, guardian: admin.guardian)
expect(described_class.solved_count(user.id)).to eq(1)
topic2 = Fabricate(:topic)
Fabricate(:post, topic: topic2)
post2 = Fabricate(:post, topic: topic2, user: user)
DiscourseSolved::AcceptAnswer.call!(params: { post_id: post2.id }, guardian: admin.guardian)
expect(described_class.solved_count(user.id)).to eq(2)
end
it "excludes deleted posts from the count" do
topic = Fabricate(:topic)
Fabricate(:post, topic: topic)
post = Fabricate(:post, topic: topic, user: user)
DiscourseSolved::AcceptAnswer.call!(params: { post_id: post.id }, guardian: admin.guardian)
expect(described_class.solved_count(user.id)).to eq(1)
post.update!(deleted_at: Time.zone.now)
expect(described_class.solved_count(user.id)).to eq(0)
end
it "excludes deleted topics from the count" do
topic = Fabricate(:topic)
Fabricate(:post, topic: topic)
post = Fabricate(:post, topic: topic, user: user)
DiscourseSolved::AcceptAnswer.call!(params: { post_id: post.id }, guardian: admin.guardian)
expect(described_class.solved_count(user.id)).to eq(1)
topic.update!(deleted_at: Time.zone.now)
expect(described_class.solved_count(user.id)).to eq(0)
end
it "excludes private messages from the count" do
topic = Fabricate(:topic)
Fabricate(:post, topic: topic)
post = Fabricate(:post, topic: topic, user: user)
DiscourseSolved::AcceptAnswer.call!(params: { post_id: post.id }, guardian: admin.guardian)
pm = Fabricate(:topic, archetype: Archetype.private_message, category_id: nil)
Fabricate(:post, topic: pm)
pm_post = Fabricate(:post, topic: pm, user: user)
DiscourseSolved::AcceptAnswer.call!(params: { post_id: pm_post.id }, guardian: admin.guardian)
expect(described_class.solved_count(user.id)).to eq(1)
end
it "returns 0 for users with no solutions" do
expect(described_class.solved_count(user.id)).to eq(0)
expect(described_class.solved_count(admin.id)).to eq(0)
end
end
end