0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-14 13:58:53 +08:00
discourse/plugins/discourse-solved/spec/requests/answer_controller_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

159 lines
4.9 KiB
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe DiscourseSolved::AnswerController do
fab!(:user)
fab!(:staff_user, :admin)
fab!(:category)
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:p) { Fabricate(:post, topic: topic) }
fab!(:solution_post) { Fabricate(:post, topic: topic) }
before do
SiteSetting.solved_enabled = true
SiteSetting.allow_solved_on_all_topics = true
category.custom_fields[DiscourseSolved::ENABLE_ACCEPTED_ANSWERS_CUSTOM_FIELD] = "true"
category.save_custom_fields
# Give permission to accept solutions
user.update!(trust_level: 1)
# Make user the topic creator so they can accept answers
topic.update!(user_id: user.id)
end
describe "#accept" do
context "with default rate limiting" do
it "applies rate limits to regular users" do
sign_in(user)
# Should be rate limited
RateLimiter.any_instance.expects(:performed!).raises(RateLimiter::LimitExceeded.new(60))
post "/solution/accept.json", params: { id: solution_post.id }
expect(response.status).to eq(429)
end
it "does not apply rate limits to staff" do
sign_in(staff_user)
post "/solution/accept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
end
end
context "with plugin modifier" do
it "allows plugins to bypass rate limiting" do
sign_in(user)
# Create a plugin instance and register a modifier
plugin_instance = Plugin::Instance.new
modifier_block = Proc.new { |_, _| false }
plugin_instance.register_modifier(
:solved_answers_controller_run_rate_limiter,
&modifier_block
)
post "/solution/accept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
post "/solution/unaccept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
# Unregister the modifier using DiscoursePluginRegistry
DiscoursePluginRegistry.unregister_modifier(
plugin_instance,
:solved_answers_controller_run_rate_limiter,
&modifier_block
)
end
end
it "returns single accepted answer in an array" do
sign_in(user)
post "/solution/accept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
result = response.parsed_body
expect(result).to be_an(Array)
expect(result.length).to eq(1)
expect(result[0]["id"]).to eq(solution_post.id)
end
context "with multiple solutions enabled" do
fab!(:solution_post2) { Fabricate(:post, topic: topic) }
before { SiteSetting.solved_allow_multiple_solutions = true }
it "returns an array with multiple accepted answers" do
sign_in(user)
post "/solution/accept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
result = response.parsed_body
expect(result).to be_an(Array)
expect(result.length).to eq(1)
expect(result[0]["id"]).to eq(solution_post.id)
post "/solution/accept.json", params: { id: solution_post2.id }
expect(response.status).to eq(200)
result = response.parsed_body
expect(result).to be_an(Array)
expect(result.length).to eq(2)
expect(result[0]["id"]).to eq(solution_post.id)
expect(result[1]["id"]).to eq(solution_post2.id)
end
end
end
describe "#unaccept" do
before do
# Setup an accepted solution
sign_in(user)
post "/solution/accept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
sign_out
end
it "applies rate limits to regular users" do
sign_in(user)
# Should be rate limited
RateLimiter.any_instance.expects(:performed!).raises(RateLimiter::LimitExceeded.new(60))
post "/solution/unaccept.json", params: { id: solution_post.id }
expect(response.status).to eq(429)
end
it "returns an empty array of accepted answers after unaccepting" do
sign_in(user)
post "/solution/unaccept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
result = response.parsed_body
expect(result).to be_nil
end
context "with multiple solutions enabled" do
fab!(:solution_post2) { Fabricate(:post, topic: topic) }
before do
SiteSetting.solved_allow_multiple_solutions = true
sign_in(user)
post "/solution/accept.json", params: { id: solution_post2.id }
sign_out
end
it "returns remaining accepted answers as an array" do
sign_in(user)
post "/solution/unaccept.json", params: { id: solution_post.id }
expect(response.status).to eq(200)
result = response.parsed_body
expect(result).to be_an(Array)
expect(result.length).to eq(1)
expect(result[0]["id"]).to eq(solution_post2.id)
end
end
end
end