0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-15 15:53:16 +08:00
discourse/plugins/discourse-solved/test/javascripts/acceptance/solved-removal-confirmation-test.js
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

294 lines
8.9 KiB
JavaScript
Vendored

import { click, visit } from "@ember/test-helpers";
import { test } from "qunit";
import { cloneJSON } from "discourse/lib/object";
import { acceptance } from "discourse/tests/helpers/qunit-helpers";
import selectKit from "discourse/tests/helpers/select-kit-helper";
import { i18n } from "discourse-i18n";
import { postStreamWithAcceptedAnswerExcerpt } from "../helpers/discourse-solved-helpers";
function solvedTopicFixture(overrides = {}) {
const topic = cloneJSON(postStreamWithAcceptedAnswerExcerpt("an answer"));
topic.category_id = 100;
return Object.assign(topic, overrides);
}
function unsolvedTopicFixture() {
const topic = cloneJSON(postStreamWithAcceptedAnswerExcerpt("an answer"));
topic.category_id = 100;
topic.accepted_answers = [];
topic.post_stream.posts.forEach((p) => {
p.accepted_answer = false;
p.can_accept_answer = false;
});
return topic;
}
const STORAGE_KEY = "discourse-solved-hide-category-change-confirmation";
acceptance("Solved Removal Confirmation", function (needs) {
needs.user({ admin: true });
needs.settings({
solved_enabled: true,
allow_solved_on_all_topics: false,
tagging_enabled: true,
enable_solved_tags: "solved-tag",
});
needs.hooks.beforeEach(() => {
localStorage.removeItem(STORAGE_KEY);
});
needs.hooks.afterEach(() => {
localStorage.removeItem(STORAGE_KEY);
});
needs.site({
can_tag_topics: true,
categories: [
{
id: 100,
name: "Solved Category",
slug: "solved-category",
color: "0088CC",
text_color: "FFFFFF",
permission: 1,
custom_fields: { enable_accepted_answers: "true" },
},
{
id: 200,
name: "Unsolved Category",
slug: "unsolved-category",
color: "FF0000",
text_color: "FFFFFF",
permission: 1,
custom_fields: {},
},
{
id: 300,
name: "Another Solved Category",
slug: "another-solved-category",
color: "00FF00",
text_color: "FFFFFF",
permission: 1,
custom_fields: { enable_accepted_answers: "true" },
},
],
});
needs.pretender((server, helper) => {
server.get("/t/50.json", () => helper.response(solvedTopicFixture()));
server.get("/t/51.json", () => helper.response(unsolvedTopicFixture()));
server.get("/t/23.json", () => helper.response(solvedTopicFixture()));
server.get("/t/52.json", () =>
helper.response(
solvedTopicFixture({
id: 52,
category_id: 200,
tags: [{ id: 1, name: "solved-tag", slug: "solved-tag" }],
})
)
);
server.get("/t/53.json", () =>
helper.response(
solvedTopicFixture({
id: 53,
category_id: 100,
tags: [{ id: 1, name: "solved-tag", slug: "solved-tag" }],
})
)
);
server.put("/t/test-solved/50", () =>
helper.response({ basic_topic: { id: 50, title: "Test solved" } })
);
server.put("/t/test-solved/52", () =>
helper.response({ basic_topic: { id: 52, title: "Test solved" } })
);
server.put("/t/test-solved/53", () =>
helper.response({ basic_topic: { id: 53, title: "Test solved" } })
);
});
test("shows confirmation modal when changing from solved to unsolved category", async function (assert) {
await visit("/t/test-solved/50");
await click("#topic-title .d-icon-pencil");
const categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.exists("confirmation modal is shown");
assert
.dom(".solved-removal-confirmation-modal .d-modal__title")
.hasText(i18n("solved.confirm_solved_removal_title"));
});
test("does not show modal when topic has no accepted answer", async function (assert) {
await visit("/t/test-solved/51");
await click("#topic-title .d-icon-pencil");
const categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist("confirmation modal is not shown");
});
test("canceling modal prevents category change", async function (assert) {
await visit("/t/test-solved/50");
await click("#topic-title .d-icon-pencil");
const categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.exists("confirmation modal is shown");
await click(
".solved-removal-confirmation-modal .d-modal__footer .btn-transparent"
);
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist("modal is closed");
assert
.dom("#topic-title .d-icon-pencil")
.doesNotExist("still in editing mode");
});
test("confirming modal proceeds with category change", async function (assert) {
await visit("/t/test-solved/50");
await click("#topic-title .d-icon-pencil");
const categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.exists("confirmation modal is shown");
await click(
".solved-removal-confirmation-modal .d-modal__footer .btn-primary"
);
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist("modal is closed");
});
test("does not show modal when changing to another solved category", async function (assert) {
await visit("/t/test-solved/50");
await click("#topic-title .d-icon-pencil");
const categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(300);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist(
"confirmation modal is not shown for solved-to-solved change"
);
});
test("respects 'don't show again' preference", async function (assert) {
await visit("/t/test-solved/50");
await click("#topic-title .d-icon-pencil");
let categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.exists("confirmation modal is shown");
await click(".solved-removal-dont-show-again input");
await click(
".solved-removal-confirmation-modal .d-modal__footer .btn-primary"
);
await visit("/t/test-solved/50");
await click("#topic-title .d-icon-pencil");
categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist("confirmation modal is not shown second time");
});
test("shows modal when removing solved tag from topic in non-solved category", async function (assert) {
await visit("/t/test-solved/52");
await click("#topic-title .d-icon-pencil");
await click(
".title-wrapper .mini-tag-chooser .selected-choice[data-name='solved-tag']"
);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.exists("confirmation modal is shown when removing solved tag");
});
test("does not show modal when removing solved tag but category still enables solutions", async function (assert) {
await visit("/t/test-solved/53");
await click("#topic-title .d-icon-pencil");
await click(
".title-wrapper .mini-tag-chooser .selected-choice[data-name='solved-tag']"
);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist(
"confirmation modal is not shown when category still enables solutions"
);
});
test("does not show modal when changing to non-solved category but solved tag remains", async function (assert) {
await visit("/t/test-solved/53");
await click("#topic-title .d-icon-pencil");
const categoryChooser = selectKit(".title-wrapper .category-chooser");
await categoryChooser.expand();
await categoryChooser.selectRowByValue(200);
await click("#topic-title .submit-edit");
assert
.dom(".solved-removal-confirmation-modal")
.doesNotExist(
"confirmation modal is not shown when solved tag still enables solutions"
);
});
});