mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-13 05:23:46 +08:00
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>
71 lines
2.6 KiB
Ruby
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
|