0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-12 03:37:13 +08:00
discourse/plugins/discourse-solved/spec/services/discourse_solved/unaccept_answer_spec.rb
Chris Alberti 9706fb8cfb
DEV: Allow multiple solutions in solved plugin (#39806)
Adds a site setting to allow multiple solutions to be accepted in the
solved plugin. Behavior without the setting enabled should remain as
before these changes.

### Model/DB changes

Previously, solutions were stored in a `SolvedTopic` model backed by the
`discourse_solved_solved_topic` table, which pointed to a topic id, a
post id, an accepting user id, and optionally a timer id for a timer to
auto-close the topic after a set amount of time if configured.

Now, the `SolvedTopic` model drops the post id and accepting user id,
but still points to the topic id and the timer id (there will only be
one timer per topic), and adds a has_many relationship to a new
`TopicAnswer` model. The `TopicAnswer` model backed by the
`discourse_solved_topic_answer` table points to a `SolvedTopic` id, a
post id and accepting user id, with one `TopicAnswer` for each accepted
solution post.

References to `topic.accepted_answer` or `topic.solved.accepted_post`
have been changed to `topic.accepted_answers.first` or
`topic.solved.topic_answers.first.post`, etc. Updated all specs/tests
and added new cases for the multiple solutions mode.

Several references in other build-in plugins are also fixed in this PR.

### Allow multiple solutions mode
These changes add the site setting `solved_allow_multiple_solutions`
which is disabled by default.

When disabled, behavior should be as it was before these changes. Users
can not accept multiple solutions for a single topic. If a post in the
topic is already marked as a solution, then the "Solution" button is
hidden, and if a user still accepts a different solution then the
original solution will be unaccepted.

When enabled, users can accept more than one solution for each topic.
Even if a solution is already accepted, other posts still show the
"Solution" button. Users can still unaccept any accepted solutions.

If a topic has multiple solutions, each one is displayed as a summary
under the OP using the existing component to represent the summary. For
displaying the solved checkmark icon, filtering, etc, a topic is
considered solved if it has at least one solution.

### UI

Added a PostExcerptAccordion component which displays post excerpts in
an accordion style with a header and expandable/collapsible items, and
that component is used to render the solution post(s) under the OP. The
first item is displayed expanded while the others are collapsed, by
default. The expanded items are truncated based on a number of lines
calculated from the `solved_quote_length` site setting, with a "read
more" link to view the full post.

Without multiple solutions allowed:
<img width="1606" height="1046" alt="image"
src="https://github.com/user-attachments/assets/5d6229f2-9052-473a-8953-574667761544"
/>

With multiple solutions allowed:
<img width="1594" height="1254" alt="image"
src="https://github.com/user-attachments/assets/6e7b7ac5-23fa-4b0e-b6d6-b8a53cdf2756"
/>

With multiple solutions, after expanding each:
<img width="932" height="1000" alt="image"
src="https://github.com/user-attachments/assets/2a8842b6-040d-44d8-bad4-f61d819789ef"
/>

### QAPage schema

The QAPage schema metadata is updated to include multiple accepted
solutions, which is allowed according to [google's QAPage
doc](https://developers.google.com/search/docs/appearance/structured-data/qapage#question)

---------

Co-authored-by: discourse-patch-triage[bot] <272280883+discourse-patch-triage[bot]@users.noreply.github.com>
Co-authored-by: Manuel Kostka <manuel@discourse.org>
2026-05-28 07:12:28 -05:00

228 lines
6.9 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe DiscourseSolved::UnacceptAnswer do
describe ::DiscourseSolved::UnacceptAnswer::Contract, type: :model do
it { is_expected.to validate_presence_of(:post_id) }
end
describe ".call" do
subject(:result) { described_class.call(params:, **dependencies) }
fab!(:user)
fab!(:topic) { Fabricate(:topic, user:) }
fab!(:post_1, :post) { Fabricate(:post, topic:) }
fab!(:post) { Fabricate(:post, topic:) }
let(:params) { { post_id: post.id } }
let(:guardian) { user.guardian }
let(:dependencies) { { guardian: } }
before do
SiteSetting.solved_enabled = true
SiteSetting.allow_solved_on_all_topics = true
end
context "when contract is invalid" do
let(:params) { {} }
it { is_expected.to fail_a_contract }
end
context "when post is not found" do
let(:params) { { post_id: -1 } }
it { is_expected.to fail_to_find_a_model(:post) }
end
context "when post is trashed" do
before { post.trash! }
it { is_expected.to fail_to_find_a_model(:post) }
context "when user is staff" do
fab!(:user, :admin)
it { is_expected.to run_successfully }
end
end
context "when topic is not found" do
before { post.topic.destroy! }
it { is_expected.to fail_to_find_a_model(:topic) }
end
context "when topic is trashed" do
before { post.topic.trash! }
it { is_expected.to fail_to_find_a_model(:topic) }
context "when user is staff" do
fab!(:user, :admin)
it { is_expected.to run_successfully }
end
end
context "when user cannot unaccept answer" do
let(:guardian) { Guardian.new }
it { is_expected.to fail_a_policy(:can_unaccept_answer) }
end
context "when the post is not the accepted answer" do
it { is_expected.to run_successfully }
it "does not mark the topic as unsolved" do
expect { result }.not_to change { DiscourseSolved::TopicAnswer.count }
end
end
context "when the post is the accepted answer" do
fab!(:solved_topic) { Fabricate(:solved_topic, topic:) }
fab!(:topic_answer) { Fabricate(:topic_answer, solved_topic:, post: post, accepter: user) }
let(:messages) { MessageBus.track_publish("/topic/#{topic.id}") { result } }
let(:events) { DiscourseEvent.track_events(:unaccepted_solution) { result } }
before do
UserAction.log_action!(
action_type: UserAction::SOLVED,
user_id: post.user_id,
acting_user_id: user.id,
target_post_id: post.id,
target_topic_id: topic.id,
)
Notification.create!(
notification_type: Notification.types[:custom],
user_id: post.user_id,
topic_id: post.topic_id,
post_number: post.post_number,
data: { message: "solved.accepted_notification" }.to_json,
)
end
it { is_expected.to run_successfully }
it "revokes the post author's solved credit" do
expect { result }.to change {
UserAction.where(action_type: UserAction::SOLVED, target_post: post).count
}.by(-1)
end
it "removes the accepted answer notification" do
expect { result }.to change {
Notification.where(
notification_type: Notification.types[:custom],
user: post.user,
topic: post.topic,
post_number: post.post_number,
).count
}.by(-1)
end
it "marks the topic as unsolved" do
expect { result }.to change { DiscourseSolved::SolvedTopic.count }.by(-1)
end
context "when tracking/watching users have topic solved notifications" do
fab!(:watching_user, :user)
before do
Notification.create!(
notification_type: Notification.types[:custom],
user_id: watching_user.id,
topic_id: post.topic_id,
post_number: post.post_number,
data: { message: "solved.topic_solved_notification" }.to_json,
)
end
it "removes topic solved notifications" do
expect { result }.to change {
Notification.where(
notification_type: Notification.types[:custom],
user: watching_user,
topic: post.topic,
).count
}.by(-1)
end
end
context "when an unaccepted_solution webhook is active" do
fab!(:web_hook) { Fabricate(:web_hook, active: true) }
fab!(:unaccepted_solution_event_type) do
WebHookEventType.find_by(name: "unaccepted_solution")
end
before { web_hook.web_hook_event_types << unaccepted_solution_event_type }
it "enqueues the webhook" do
expect { result }.to change { Jobs::EmitWebHookEvent.jobs.size }.by(1)
end
end
it "triggers the :unaccepted_solution event" do
expect(events).to include(a_hash_including(params: [post]))
end
it "broadcasts the unaccepted solution" do
expect(messages).to include(
an_object_having_attributes(data: a_hash_including(type: :unaccepted_solution)),
)
end
context "when the post is trashed" do
fab!(:user, :admin)
before { post.trash! }
it "still marks the topic as unsolved" do
expect { result }.to change { DiscourseSolved::TopicAnswer.count }.by(-1)
end
end
context "when a different post is the accepted answer" do
before { solved_topic.topic_answers.first.update!(post: post_1) }
it "does not mark the topic as unsolved" do
expect { result }.not_to change { DiscourseSolved::TopicAnswer.count }
end
end
describe "with multiple solutions enabled and another existing solution" do
fab!(:post2) { Fabricate(:post, topic:) }
fab!(:topic_answer2) do
Fabricate(:topic_answer, solved_topic:, post: post2, accepter: user)
end
before { SiteSetting.solved_allow_multiple_solutions = true }
it "revokes the post author's solved credit" do
expect { result }.to change {
UserAction.where(action_type: UserAction::SOLVED, target_post: post).count
}.by(-1)
end
it "removes the accepted answer notification" do
expect { result }.to change {
Notification.where(
notification_type: Notification.types[:custom],
user: post.user,
topic: post.topic,
post_number: post.post_number,
).count
}.by(-1)
end
it "does not mark the topic as unsolved" do
expect { result }.not_to change { DiscourseSolved::SolvedTopic.count }
end
it "removes one TopicAnswer" do
expect { result }.to change { DiscourseSolved::TopicAnswer.count }.by(-1)
end
end
end
end
end