From 7be53b15880f40c104a06eb70b477f6d8b5ad0e9 Mon Sep 17 00:00:00 2001 From: Blake Erickson Date: Mon, 14 Nov 2022 11:09:57 -0700 Subject: [PATCH] FEATURE: Default Composer Category Site Setting (#18967) * FEATURE: Default Composer Category Site Setting - Create the default_composer_category site setting - Replace general_category_id logic for auto selecting the composer category - Prevent Uncategorized from being selected if not allowed - Add default_composer_category option to seeded categories - Create a migration to populate the default_composer_category site setting if there is a general_category_id populated - Added some tests * Add missing translation for the new site setting * fix some js tests * Just check that the header value is null --- .../discourse/app/models/composer.js | 11 +- .../tests/acceptance/composer-test.js | 109 +++++++++++++++++- .../select-kit/category-chooser-test.js | 8 +- .../addon/components/category-chooser.js | 7 +- config/locales/server.en.yml | 2 + config/site_settings.yml | 5 + ...5456_populate_default_composer_category.rb | 22 ++++ lib/seed_data/categories.rb | 9 +- .../default_composer_category_validator.rb | 19 +++ spec/lib/seed_data/categories_spec.rb | 1 + 10 files changed, 181 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20221110175456_populate_default_composer_category.rb create mode 100644 lib/validators/default_composer_category_validator.rb diff --git a/app/assets/javascripts/discourse/app/models/composer.js b/app/assets/javascripts/discourse/app/models/composer.js index e4058e78d1c..fdfc08c361b 100644 --- a/app/assets/javascripts/discourse/app/models/composer.js +++ b/app/assets/javascripts/discourse/app/models/composer.js @@ -143,10 +143,15 @@ const Composer = RestModel.extend({ const oldCategoryId = this._categoryId; if (isEmpty(categoryId)) { - // Set General as the default category - const generalCategoryId = this.siteSettings.general_category_id; + // Check if there is a default composer category to set + const defaultComposerCategoryId = parseInt( + this.siteSettings.default_composer_category, + 10 + ); categoryId = - generalCategoryId && generalCategoryId > 0 ? generalCategoryId : null; + defaultComposerCategoryId && defaultComposerCategoryId > 0 + ? defaultComposerCategoryId + : null; } this._categoryId = categoryId; diff --git a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js index a86afe5d080..e2e0593cc39 100644 --- a/app/assets/javascripts/discourse/tests/acceptance/composer-test.js +++ b/app/assets/javascripts/discourse/tests/acceptance/composer-test.js @@ -41,6 +41,7 @@ acceptance("Composer", function (needs) { needs.settings({ enable_whispers: true, general_category_id: 1, + default_composer_category: 1, }); needs.site({ can_tag_topics: true, @@ -90,7 +91,7 @@ acceptance("Composer", function (needs) { test("Composer is opened", async function (assert) { await visit("/"); await click("#create-topic"); - // Check that General category is selected + // Check that the default category is selected assert.strictEqual(selectKit(".category-chooser").header().value(), "1"); assert.strictEqual( @@ -1201,3 +1202,109 @@ acceptance("Composer - Focus Open and Closed", function (needs) { ); }); }); + +// Default Composer Category tests +acceptance("Composer - Default category", function (needs) { + needs.user(); + needs.settings({ + general_category_id: 1, + default_composer_category: 2, + }); + needs.site({ + categories: [ + { + id: 1, + name: "General", + slug: "general", + permission: 1, + ltopic_template: null, + }, + { + id: 2, + name: "test too", + slug: "test-too", + permission: 1, + topic_template: null, + }, + ], + }); + + test("Default category is selected over general category", async function (assert) { + await visit("/"); + await click("#create-topic"); + assert.strictEqual(selectKit(".category-chooser").header().value(), "2"); + assert.strictEqual( + selectKit(".category-chooser").header().name(), + "test too" + ); + }); +}); + +acceptance("Composer - Uncategorized category", function (needs) { + needs.user(); + needs.settings({ + general_category_id: -1, // For sites that never had this seeded + default_composer_category: -1, // For sites that never had this seeded + allow_uncategorized_topics: true, + }); + needs.site({ + categories: [ + { + id: 1, + name: "General", + slug: "general", + permission: 1, + ltopic_template: null, + }, + { + id: 2, + name: "test too", + slug: "test-too", + permission: 1, + topic_template: null, + }, + ], + }); + + test("Uncategorized category is selected", async function (assert) { + await visit("/"); + await click("#create-topic"); + assert.strictEqual(selectKit(".category-chooser").header().value(), null); + }); +}); + +acceptance("Composer - default category not set", function (needs) { + needs.user(); + needs.settings({ + default_composer_category: "", + }); + needs.site({ + categories: [ + { + id: 1, + name: "General", + slug: "general", + permission: 1, + ltopic_template: null, + }, + { + id: 2, + name: "test too", + slug: "test-too", + permission: 1, + topic_template: null, + }, + ], + }); + + test("Nothing is selected", async function (assert) { + await visit("/"); + await click("#create-topic"); + assert.strictEqual(selectKit(".category-chooser").header().value(), null); + assert.strictEqual( + selectKit(".category-chooser").header().name(), + "category…" + ); + }); +}); +// END: Default Composer Category tests diff --git a/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js b/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js index c99ad0e64bb..cf89f6f18cd 100644 --- a/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js +++ b/app/assets/javascripts/discourse/tests/integration/components/select-kit/category-chooser-test.js @@ -126,9 +126,9 @@ module( assert.strictEqual(this.subject.header().label(), "category…"); }); - test("with allowUncategorized=null and generalCategoryId present", async function (assert) { + test("with allowUncategorized=null and defaultComposerCategory present", async function (assert) { this.siteSettings.allow_uncategorized_topics = false; - this.siteSettings.general_category_id = 4; + this.siteSettings.default_composer_category = 4; await render(hbs`