A platform for community discussion. Free, open, simple.
FIX: confirmation for solved tags removal (#37720)
David Battersby0f45b33c89f6
diff --git a/plugins/discourse-solved/assets/javascripts/discourse/components/modal/category-change-solved-confirmation.gjs b/plugins/discourse-solved/assets/javascripts/discourse/components/modal/solved-removal-confirmation.gjssimilarity index 72%rename from plugins/discourse-solved/assets/javascripts/discourse/components/modal/category-change-solved-confirmation.gjsrename to plugins/discourse-solved/assets/javascripts/discourse/components/modal/solved-removal-confirmation.gjsindex 38d7fcbdd5f..8fd700d5104 100644--- a/plugins/discourse-solved/assets/javascripts/discourse/components/modal/category-change-solved-confirmation.gjs+++ b/plugins/discourse-solved/assets/javascripts/discourse/components/modal/solved-removal-confirmation.gjs@@ -7,7 +7,7 @@ import DButton from "discourse/components/d-button";import DModal from "discourse/components/d-modal";import { i18n } from "discourse-i18n";-export default class CategoryChangeSolvedConfirmationModal extends Component {+export default class SolvedRemovalConfirmationModal extends Component {@tracked dontShowAgain = false;@action@@ -31,15 +31,13 @@ export default class CategoryChangeSolvedConfirmationModal extends Component {<template><DModal@closeModal={{this.cancel}}- @title={{i18n "solved.confirm_category_change_solution_title"}}- class="category-change-solved-confirmation-modal"+ @title={{i18n "solved.confirm_solved_removal_title"}}+ class="solved-removal-confirmation-modal"><:body>- <p>{{i18n "solved.confirm_category_change_solution"}}</p>+ <p>{{i18n "solved.confirm_solved_removal"}}</p><div class="control-group">- <label- class="checkbox-label category-change-solution-dont-show-again"- >+ <label class="checkbox-label solved-removal-dont-show-again"><Input@type="checkbox"@checked={{this.dontShowAgain}}@@ -52,12 +50,12 @@ export default class CategoryChangeSolvedConfirmationModal extends Component {<:footer><DButton@action={{this.confirm}}- @label="solved.confirm_category_change_solution_confirm"+ @label="solved.confirm_solved_removal_confirm"class="btn-primary"/><DButton@action={{this.cancel}}- @label="solved.confirm_category_change_solution_cancel"+ @label="solved.confirm_solved_removal_cancel"class="btn-transparent"/></:footer>diff --git a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-change-confirmation.gjs b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-change-confirmation.gjsindex 9860b601937..34a168c6c8b 100644--- a/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-change-confirmation.gjs+++ b/plugins/discourse-solved/assets/javascripts/discourse/initializers/solved-category-change-confirmation.gjs@@ -1,7 +1,7 @@import { action } from "@ember/object";import { withPluginApi } from "discourse/lib/plugin-api";import Category from "discourse/models/category";-import CategoryChangeSolvedConfirmationModal from "../components/modal/category-change-solved-confirmation";+import SolvedRemovalConfirmationModal from "../components/modal/solved-removal-confirmation";const STORAGE_KEY = "discourse-solved-hide-category-change-confirmation";@@ -14,34 +14,48 @@ export default {"controller:topic",(Superclass) =>class extends Superclass {+ _solvedEnabled(categoryId, tags) {+ if (+ this.siteSettings.allow_solved_on_all_topics ||+ Category.findById(categoryId)?.enable_accepted_answers+ ) {+ return true;+ }++ const solvedTags = this.siteSettings.enable_solved_tags+ .split("|")+ .filter(Boolean);++ return tags.some((t) => solvedTags.includes(t.name));+ }+@actionasync finishedEditingTopic() {if (!this.editingTopic) {return;}- let isSolved = (id) =>- Category.findById(id)?.enable_accepted_answers;-const props = this.get("buffered.buffer");let solvedStateChanged = false;- if (- props.category_id !== undefined &&- !this.siteSettings.allow_solved_on_all_topics- ) {- const oldSolved = isSolved(this.model.category_id);- const newSolved = isSolved(props.category_id);+ if ("category_id" in props || "tags" in props) {+ const oldCategoryId = this.model.category_id;+ const newCategoryId = props.category_id ?? oldCategoryId;+ const oldTags = this.model.tags;+ const newTags = props.tags ?? oldTags;++ const oldAllowed = this._solvedEnabled(oldCategoryId, oldTags);+ const newAllowed = this._solvedEnabled(newCategoryId, newTags);- solvedStateChanged = oldSolved !== newSolved;+ solvedStateChanged = oldAllowed !== newAllowed;- if (this.model.accepted_answer && oldSolved && !newSolved) {+ if (this.model.accepted_answer && oldAllowed && !newAllowed) {const showConfirmation =localStorage.getItem(STORAGE_KEY) !== "true";if (showConfirmation) {const result = await this.modal.show(- CategoryChangeSolvedConfirmationModal+ SolvedRemovalConfirmationModal);if (!result?.confirmed) {diff --git a/plugins/discourse-solved/config/locales/client.en.yml b/plugins/discourse-solved/config/locales/client.en.ymlindex d4b1a270c8c..a4cac3f1cc3 100644--- a/plugins/discourse-solved/config/locales/client.en.yml+++ b/plugins/discourse-solved/config/locales/client.en.yml@@ -44,10 +44,10 @@ en:other: "Move posts"move_post_cancel: "Cancel"dont_show_again: "Got it, don't show me this again"- confirm_category_change_solution_title: "Solution will be removed"- confirm_category_change_solution: "The new category does not have solutions enabled. Changing the category will remove the accepted solution from this topic."- confirm_category_change_solution_confirm: "Change category"- confirm_category_change_solution_cancel: "Cancel"+ confirm_solved_removal_title: "Solution will be removed"+ confirm_solved_removal: "This change will disable solutions for this topic and remove the accepted solution."+ confirm_solved_removal_confirm: "Confirm"+ confirm_solved_removal_cancel: "Cancel"no_answer:title: Has your question been answered?diff --git a/plugins/discourse-solved/config/settings.yml b/plugins/discourse-solved/config/settings.ymlindex f5679863482..5a63ee11734 100644--- a/plugins/discourse-solved/config/settings.yml+++ b/plugins/discourse-solved/config/settings.yml@@ -64,4 +64,5 @@ discourse_solved:enable_solved_tags:type: tag_listdefault: ""+ client: truediff --git a/plugins/discourse-solved/plugin.rb b/plugins/discourse-solved/plugin.rbindex cb96650c29b..caf6d8c6ed9 100644--- a/plugins/discourse-solved/plugin.rb+++ b/plugins/discourse-solved/plugin.rb@@ -297,17 +297,22 @@ after_initialize doendon(:before_post_publish_changes) do |post_changes, topic_changes, options|- category_id_changes = topic_changes.diff["category_id"].to_a- tag_changes = topic_changes.diff["tags"].to_a+ topic = topic_changes.topic+ current_tag_names = topic.tags.map(&:name)- old_allowed = Guardian.new.solved_enabled_for_category?(category_id_changes[0], tag_changes[0])- new_allowed = Guardian.new.solved_enabled_for_category?(category_id_changes[1], tag_changes[1])+ category_id_diff = topic_changes.diff["category_id"]+ tag_diff = topic_changes.diff["tags"]++ old_category_id = category_id_diff ? category_id_diff[0] : topic.category_id+ old_tags = tag_diff ? tag_diff[0] : current_tag_names++ old_allowed = Guardian.new.solved_enabled_for_category?(old_category_id, old_tags)+ new_allowed = Guardian.new.solved_enabled_for_category?(topic.category_id, current_tag_names)if old_allowed != new_allowedoptions[:refresh_stream] = trueif !new_allowed- topic = topic_changes.topicif topic.solved.present?post = topic.solved.answer_postDiscourseSolved.unaccept_answer!(post, topic: topic) if postdiff --git a/plugins/discourse-solved/spec/components/post_revisor_spec.rb b/plugins/discourse-solved/spec/components/post_revisor_spec.rbindex 76fd94c1a1d..ed1eec57984 100644--- a/plugins/discourse-solved/spec/components/post_revisor_spec.rb+++ b/plugins/discourse-solved/spec/components/post_revisor_spec.rb@@ -93,6 +93,52 @@ describe PostRevisor doendend+ describe "Unaccepting answer with combined category and tag changes" do+ fab!(:solved_tag, :tag)++ before do+ SiteSetting.solved_enabled = true+ SiteSetting.tagging_enabled = true+ SiteSetting.enable_solved_tags = solved_tag.name+ end++ it "keeps answer when category changes to unsolved but topic has a solved tag" do+ topic = Fabricate(:topic, category: category_solved, tags: [solved_tag])+ post = Fabricate(:post, topic: topic)+ reply = Fabricate(:post, topic: topic, post_number: 2)+ DiscourseSolved.accept_answer!(reply, admin)++ described_class.new(post).revise!(admin, { category_id: category.id })+ topic.reload++ expect(topic.solved).to be_present+ expect(topic.solved.answer_post_id).to eq(reply.id)+ end++ it "keeps answer when solved tag is removed but topic is in a solved category" do+ topic = Fabricate(:topic, category: category_solved, tags: [solved_tag])+ post = Fabricate(:post, topic: topic)+ reply = Fabricate(:post, topic: topic, post_number: 2)+ DiscourseSolved.accept_answer!(reply, admin)++ described_class.new(post).revise!(admin, tags: [])+ topic.reload++ expect(topic.solved).to be_present+ expect(topic.solved.answer_post_id).to eq(reply.id)+ end++ it "unaccepts answer when both category changes to unsolved and solved tag is removed" do+ topic = Fabricate(:topic, category: category_solved, tags: [solved_tag])+ post = Fabricate(:post, topic: topic)+ reply = Fabricate(:post, topic: topic, post_number: 2)+ DiscourseSolved.accept_answer!(reply, admin)++ described_class.new(post).revise!(admin, { category_id: category.id, tags: [] })+ expect(topic.reload.solved).to be_nil+ end+ end+describe "Allowing solved via tags" dobefore doSiteSetting.solved_enabled = truediff --git a/plugins/discourse-solved/spec/system/solved_with_tags_spec.rb b/plugins/discourse-solved/spec/system/solved_with_tags_spec.rbindex 0f7f4202680..46a8d0c22ef 100644--- a/plugins/discourse-solved/spec/system/solved_with_tags_spec.rb+++ b/plugins/discourse-solved/spec/system/solved_with_tags_spec.rb@@ -50,6 +50,31 @@ describe "Solved with tags", type: :system doexpect(post_solved_button).to have_no_accept_buttonend+ describe "confirmation modal when removing solved tag" do+ fab!(:admin)+ fab!(:other_tag, :tag)+ fab!(:topic_with_solved_tag) { Fabricate(:topic_with_op, user: admin, tags: [tag, other_tag]) }+ fab!(:answer) { Fabricate(:post, topic: topic_with_solved_tag) }+ fab!(:solved_record) do+ Fabricate(:solved_topic, topic: topic_with_solved_tag, answer_post: answer)+ end++ it "shows confirmation when removing the solved tag from a topic with an accepted answer" do+ sign_in(admin)+ topic_page.visit_topic(topic_with_solved_tag)++ topic_page.click_topic_edit_title++ tag_chooser = PageObjects::Components::SelectKit.new("#topic-title .mini-tag-chooser")+ tag_chooser.expand+ tag_chooser.unselect_by_name(tag.name)++ topic_page.click_topic_title_submit_edit++ expect(page).to have_css(".solved-removal-confirmation-modal")+ end+ end+it "shows solved status filter on tag page and filters topics correctly" doSiteSetting.show_filter_by_solved_status = truediff --git a/plugins/discourse-solved/test/javascripts/acceptance/solved-category-change-test.js b/plugins/discourse-solved/test/javascripts/acceptance/solved-removal-confirmation-test.jssimilarity index 62%rename from plugins/discourse-solved/test/javascripts/acceptance/solved-category-change-test.jsrename to plugins/discourse-solved/test/javascripts/acceptance/solved-removal-confirmation-test.jsindex 796b28b8c39..8473ebee62f 100644--- a/plugins/discourse-solved/test/javascripts/acceptance/solved-category-change-test.js+++ b/plugins/discourse-solved/test/javascripts/acceptance/solved-removal-confirmation-test.js@@ -6,10 +6,10 @@ import selectKit from "discourse/tests/helpers/select-kit-helper";import { i18n } from "discourse-i18n";import { postStreamWithAcceptedAnswerExcerpt } from "../helpers/discourse-solved-helpers";-function solvedTopicFixture() {+function solvedTopicFixture(overrides = {}) {const topic = cloneJSON(postStreamWithAcceptedAnswerExcerpt("an answer"));topic.category_id = 100;- return topic;+ return Object.assign(topic, overrides);}function unsolvedTopicFixture() {@@ -25,12 +25,14 @@ function unsolvedTopicFixture() {const STORAGE_KEY = "discourse-solved-hide-category-change-confirmation";-acceptance("Discourse Solved | Category Change Confirmation", function (needs) {+acceptance("Discourse Solved | 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(() => {@@ -42,6 +44,7 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {});needs.site({+ can_tag_topics: true,categories: [{id: 100,@@ -77,9 +80,33 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {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) {@@ -93,12 +120,12 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").exists("confirmation modal is shown");assert- .dom(".category-change-solved-confirmation-modal .d-modal__title")- .hasText(i18n("solved.confirm_category_change_solution_title"));+ .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) {@@ -112,7 +139,7 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").doesNotExist("confirmation modal is not shown");});@@ -127,15 +154,15 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").exists("confirmation modal is shown");await click(- ".category-change-solved-confirmation-modal .d-modal__footer .btn-transparent"+ ".solved-removal-confirmation-modal .d-modal__footer .btn-transparent");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").doesNotExist("modal is closed");assert@@ -154,15 +181,15 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").exists("confirmation modal is shown");await click(- ".category-change-solved-confirmation-modal .d-modal__footer .btn-primary"+ ".solved-removal-confirmation-modal .d-modal__footer .btn-primary");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").doesNotExist("modal is closed");});@@ -177,7 +204,7 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").doesNotExist("confirmation modal is not shown for solved-to-solved change");@@ -194,12 +221,12 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .dom(".solved-removal-confirmation-modal").exists("confirmation modal is shown");- await click(".category-change-solution-dont-show-again input");+ await click(".solved-removal-dont-show-again input");await click(- ".category-change-solved-confirmation-modal .d-modal__footer .btn-primary"+ ".solved-removal-confirmation-modal .d-modal__footer .btn-primary");await visit("/t/test-solved/50");@@ -212,7 +239,56 @@ acceptance("Discourse Solved | Category Change Confirmation", function (needs) {await click("#topic-title .submit-edit");assert- .dom(".category-change-solved-confirmation-modal")+ .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"+ );+ });});