Skip to content

Commit 1ee2f7d

Browse files
authored
Merge pull request #276 from paviliondev/filter-categories-for-admin-staff
Filter categories for admin staff
2 parents 6d6ffa5 + 9656303 commit 1ee2f7d

File tree

3 files changed

+156
-9
lines changed

3 files changed

+156
-9
lines changed

assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,13 @@ export default {
8787
api.modifyClass("component:category-chooser", {
8888
categoriesByScope(options = {}) {
8989
let categories = this._super(options);
90-
91-
return categories.filter((category) => {
92-
return !category.custom_fields?.create_topic_wizard;
93-
});
90+
const currentUser = this.currentUser;
91+
if (!currentUser?.staff) {
92+
categories = categories.filter((category) => {
93+
return !category.custom_fields?.create_topic_wizard;
94+
});
95+
}
96+
return categories;
9497
},
9598
});
9699
});

plugin.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22
# name: discourse-custom-wizard
33
# about: Forms for Discourse. Better onboarding, structured posting, data enrichment, automated actions and much more.
4-
# version: 2.4.25
4+
# version: 2.4.26
55
# authors: Angus McLeod, Faizaan Gagan, Robert Barrow, Keegan George, Kaitlin Maddever, Juan Marcos Gutierrez Ramos
66
# url: https://github.com/paviliondev/discourse-custom-wizard
77
# contact_emails: development@pavilion.tech

test/javascripts/acceptance/category-chooser-initializer-test.js

Lines changed: 148 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import { acceptance } from "discourse/tests/helpers/qunit-helpers";
33
import selectKit from "discourse/tests/helpers/select-kit-helper";
44
import { test } from "qunit";
55

6-
acceptance("Category Chooser Initializer", function (needs) {
7-
needs.user();
6+
acceptance("Category Chooser Initializer for regular users", function (needs) {
7+
needs.user({ admin: false, moderator: false });
88
needs.settings({
99
allow_uncategorized_topics: false,
1010
});
@@ -45,9 +45,8 @@ acceptance("Category Chooser Initializer", function (needs) {
4545
],
4646
});
4747

48-
test("does not display category with create_topic_wizard custom field", async function (assert) {
48+
test("does not display category with create_topic_wizard for regular users", async function (assert) {
4949
const categoryChooser = selectKit(".category-chooser");
50-
5150
await visit("/");
5251
await click("#create-topic");
5352
await categoryChooser.expand();
@@ -80,3 +79,148 @@ acceptance("Category Chooser Initializer", function (needs) {
8079
);
8180
});
8281
});
82+
83+
acceptance("Category Chooser Initializer for Admins", function (needs) {
84+
needs.user({ admin: true });
85+
needs.settings({
86+
allow_uncategorized_topics: false,
87+
});
88+
needs.site({
89+
can_tag_topics: true,
90+
categories: [
91+
{
92+
id: 1,
93+
name: "General",
94+
slug: "general",
95+
permission: 1,
96+
topic_template: null,
97+
},
98+
{
99+
id: 2,
100+
name: "Category with custom field",
101+
slug: "category-custom-field",
102+
permission: 1,
103+
topic_template: "",
104+
custom_fields: {
105+
create_topic_wizard: "21",
106+
},
107+
},
108+
{
109+
id: 3,
110+
name: "Category 1",
111+
slug: "category-1",
112+
permission: 1,
113+
topic_template: "",
114+
},
115+
{
116+
id: 4,
117+
name: "Category 2",
118+
slug: "category-2",
119+
permission: 1,
120+
topic_template: "",
121+
},
122+
],
123+
});
124+
125+
test("displays all categories", async function (assert) {
126+
const categoryChooser = selectKit(".category-chooser");
127+
await visit("/");
128+
await click("#create-topic");
129+
await categoryChooser.expand();
130+
let categories = Array.from(
131+
document.querySelectorAll(".category-chooser .category-row")
132+
).filter((category) => category.getAttribute("data-name")); // Filter elements with a data-name attribute
133+
assert.equal(
134+
categories.length,
135+
4,
136+
"Correct number of categories are displayed"
137+
);
138+
const categoryNames = [
139+
"General",
140+
"Category 1",
141+
"Category 2",
142+
"Category with custom field",
143+
];
144+
145+
categoryNames.forEach((categoryName) => {
146+
assert.ok(
147+
categories.some(
148+
(category) => category.getAttribute("data-name") === categoryName
149+
),
150+
`Category '${categoryName}' is displayed`
151+
);
152+
});
153+
});
154+
});
155+
acceptance("Category Chooser Initializer for Staff", function (needs) {
156+
needs.user({ staff: true });
157+
needs.settings({
158+
allow_uncategorized_topics: false,
159+
});
160+
needs.site({
161+
can_tag_topics: true,
162+
categories: [
163+
{
164+
id: 1,
165+
name: "General",
166+
slug: "general",
167+
permission: 1,
168+
topic_template: null,
169+
},
170+
{
171+
id: 2,
172+
name: "Category with custom field",
173+
slug: "category-custom-field",
174+
permission: 1,
175+
topic_template: "",
176+
custom_fields: {
177+
create_topic_wizard: "21",
178+
},
179+
},
180+
{
181+
id: 3,
182+
name: "Category 1",
183+
slug: "category-1",
184+
permission: 1,
185+
topic_template: "",
186+
},
187+
{
188+
id: 4,
189+
name: "Category 2",
190+
slug: "category-2",
191+
permission: 1,
192+
topic_template: "",
193+
},
194+
],
195+
});
196+
197+
test("displays all categories", async function (assert) {
198+
const categoryChooser = selectKit(".category-chooser");
199+
await visit("/");
200+
await click("#create-topic");
201+
await categoryChooser.expand();
202+
let categories = Array.from(
203+
document.querySelectorAll(".category-chooser .category-row")
204+
).filter((category) => category.getAttribute("data-name")); // Filter elements with a data-name attribute
205+
assert.equal(
206+
categories.length,
207+
4,
208+
"Correct number of categories are displayed"
209+
);
210+
const categoryNames = [
211+
"General",
212+
"Category 1",
213+
"Category 2",
214+
"Category with custom field",
215+
];
216+
217+
categoryNames.forEach((categoryName) => {
218+
assert.ok(
219+
categories.some(
220+
(category) => category.getAttribute("data-name") === categoryName
221+
),
222+
`Category '${categoryName}' is displayed`
223+
);
224+
});
225+
});
226+
});

0 commit comments

Comments
 (0)