From 362c74cb44becb3845503e4f8512fe9440359e5b Mon Sep 17 00:00:00 2001 From: jumagura Date: Fri, 6 Oct 2023 16:24:05 -0400 Subject: [PATCH 1/3] DEV: Add exception to display all categories for admin and staff --- .../initializers/custom-wizard-edits.js.es6 | 11 +- .../category-chooser-initializer-test.js | 152 +++++++++++++++++- 2 files changed, 155 insertions(+), 8 deletions(-) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index bd3c3d2d..cef4c659 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -87,10 +87,13 @@ export default { api.modifyClass("component:category-chooser", { categoriesByScope(options = {}) { let categories = this._super(options); - - return categories.filter((category) => { - return !category.custom_fields?.create_topic_wizard; - }); + const currentUser = this.currentUser; + if (!(currentUser && (currentUser.admin || currentUser.staff))) { + categories = categories.filter((category) => { + return !category.custom_fields?.create_topic_wizard; + }); + } + return categories; }, }); }); diff --git a/test/javascripts/acceptance/category-chooser-initializer-test.js b/test/javascripts/acceptance/category-chooser-initializer-test.js index f7e02bb8..edf9004c 100644 --- a/test/javascripts/acceptance/category-chooser-initializer-test.js +++ b/test/javascripts/acceptance/category-chooser-initializer-test.js @@ -3,8 +3,8 @@ import { acceptance } from "discourse/tests/helpers/qunit-helpers"; import selectKit from "discourse/tests/helpers/select-kit-helper"; import { test } from "qunit"; -acceptance("Category Chooser Initializer", function (needs) { - needs.user(); +acceptance("Category Chooser Initializer for regular users", function (needs) { + needs.user({ staff: false, admin: false, moderator: false }); needs.settings({ allow_uncategorized_topics: false, }); @@ -45,9 +45,8 @@ acceptance("Category Chooser Initializer", function (needs) { ], }); - test("does not display category with create_topic_wizard custom field", async function (assert) { + test("does not display category with create_topic_wizard for regular users", async function (assert) { const categoryChooser = selectKit(".category-chooser"); - await visit("/"); await click("#create-topic"); await categoryChooser.expand(); @@ -80,3 +79,148 @@ acceptance("Category Chooser Initializer", function (needs) { ); }); }); + +acceptance("Category Chooser Initializer for Admins", function (needs) { + needs.user({ admin: true }); + needs.settings({ + allow_uncategorized_topics: false, + }); + needs.site({ + can_tag_topics: true, + categories: [ + { + id: 1, + name: "General", + slug: "general", + permission: 1, + topic_template: null, + }, + { + id: 2, + name: "Category with custom field", + slug: "category-custom-field", + permission: 1, + topic_template: "", + custom_fields: { + create_topic_wizard: "21", + }, + }, + { + id: 3, + name: "Category 1", + slug: "category-1", + permission: 1, + topic_template: "", + }, + { + id: 4, + name: "Category 2", + slug: "category-2", + permission: 1, + topic_template: "", + }, + ], + }); + + test("displays all categories", async function (assert) { + const categoryChooser = selectKit(".category-chooser"); + await visit("/"); + await click("#create-topic"); + await categoryChooser.expand(); + let categories = Array.from( + document.querySelectorAll(".category-chooser .category-row") + ).filter((category) => category.getAttribute("data-name")); // Filter elements with a data-name attribute + assert.equal( + categories.length, + 4, + "Correct number of categories are displayed" + ); + const categoryNames = [ + "General", + "Category 1", + "Category 2", + "Category with custom field", + ]; + + categoryNames.forEach((categoryName) => { + assert.ok( + categories.some( + (category) => category.getAttribute("data-name") === categoryName + ), + `Category '${categoryName}' is displayed` + ); + }); + }); +}); +acceptance("Category Chooser Initializer for Staff", function (needs) { + needs.user({ staff: true }); + needs.settings({ + allow_uncategorized_topics: false, + }); + needs.site({ + can_tag_topics: true, + categories: [ + { + id: 1, + name: "General", + slug: "general", + permission: 1, + topic_template: null, + }, + { + id: 2, + name: "Category with custom field", + slug: "category-custom-field", + permission: 1, + topic_template: "", + custom_fields: { + create_topic_wizard: "21", + }, + }, + { + id: 3, + name: "Category 1", + slug: "category-1", + permission: 1, + topic_template: "", + }, + { + id: 4, + name: "Category 2", + slug: "category-2", + permission: 1, + topic_template: "", + }, + ], + }); + + test("displays all categories", async function (assert) { + const categoryChooser = selectKit(".category-chooser"); + await visit("/"); + await click("#create-topic"); + await categoryChooser.expand(); + let categories = Array.from( + document.querySelectorAll(".category-chooser .category-row") + ).filter((category) => category.getAttribute("data-name")); // Filter elements with a data-name attribute + assert.equal( + categories.length, + 4, + "Correct number of categories are displayed" + ); + const categoryNames = [ + "General", + "Category 1", + "Category 2", + "Category with custom field", + ]; + + categoryNames.forEach((categoryName) => { + assert.ok( + categories.some( + (category) => category.getAttribute("data-name") === categoryName + ), + `Category '${categoryName}' is displayed` + ); + }); + }); +}); From 61c45749088f5ec48de3bbeb79f88fd699b8dbdf Mon Sep 17 00:00:00 2001 From: jumagura Date: Fri, 6 Oct 2023 16:26:18 -0400 Subject: [PATCH 2/3] bump version --- plugin.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin.rb b/plugin.rb index 680c7191..a4413577 100644 --- a/plugin.rb +++ b/plugin.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true # name: discourse-custom-wizard # about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more. -# version: 2.4.25 +# version: 2.4.26 # authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos # url: https://github.com/paviliondev/discourse-custom-wizard # contact_emails: development@pavilion.tech From 9656303c632bd307eb76f5770da1c31fe5b45b84 Mon Sep 17 00:00:00 2001 From: jumagura Date: Fri, 6 Oct 2023 22:35:36 -0400 Subject: [PATCH 3/3] DEV: Refactor staff condition in category-chooser for simplicity --- .../discourse/initializers/custom-wizard-edits.js.es6 | 2 +- .../javascripts/acceptance/category-chooser-initializer-test.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index cef4c659..ca72fa4b 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -88,7 +88,7 @@ export default { categoriesByScope(options = {}) { let categories = this._super(options); const currentUser = this.currentUser; - if (!(currentUser && (currentUser.admin || currentUser.staff))) { + if (!currentUser?.staff) { categories = categories.filter((category) => { return !category.custom_fields?.create_topic_wizard; }); diff --git a/test/javascripts/acceptance/category-chooser-initializer-test.js b/test/javascripts/acceptance/category-chooser-initializer-test.js index edf9004c..0348e5bb 100644 --- a/test/javascripts/acceptance/category-chooser-initializer-test.js +++ b/test/javascripts/acceptance/category-chooser-initializer-test.js @@ -4,7 +4,7 @@ import selectKit from "discourse/tests/helpers/select-kit-helper"; import { test } from "qunit"; acceptance("Category Chooser Initializer for regular users", function (needs) { - needs.user({ staff: false, admin: false, moderator: false }); + needs.user({ admin: false, moderator: false }); needs.settings({ allow_uncategorized_topics: false, });