diff --git a/app/assets/javascripts/discourse/app/mixins/open-composer.js b/app/assets/javascripts/discourse/app/mixins/open-composer.js index 128bb838001..b96bc5ca638 100644 --- a/app/assets/javascripts/discourse/app/mixins/open-composer.js +++ b/app/assets/javascripts/discourse/app/mixins/open-composer.js @@ -7,11 +7,15 @@ export default Mixin.create({ openComposer(controller) { let categoryId = controller.get("category.id"); - if ( - !controller.canCreateTopicOnCategory && - this.siteSettings.default_subcategory_on_read_only_category - ) { - categoryId = controller.get("defaultSubcategory.id"); + if (this.siteSettings.default_subcategory_on_read_only_category) { + if ( + !controller.canCreateTopicOnCategory && + controller.canCreateTopicOnSubCategory + ) { + categoryId = controller.get("defaultSubcategory.id"); + } else { + categoryId = this.siteSettings.default_composer_category; + } } if ( diff --git a/app/assets/javascripts/discourse/app/routes/build-category-route.js b/app/assets/javascripts/discourse/app/routes/build-category-route.js index 40bb36abee1..e29def7cc37 100644 --- a/app/assets/javascripts/discourse/app/routes/build-category-route.js +++ b/app/assets/javascripts/discourse/app/routes/build-category-route.js @@ -157,26 +157,24 @@ export default (filterArg, params) => { let canCreateTopicOnCategory = canCreateTopic && category.get("permission") === PermissionType.FULL; - + let cannotCreateTopicOnCategory = !canCreateTopicOnCategory; let defaultSubcategory; let canCreateTopicOnSubCategory; - if ( - !canCreateTopicOnCategory && - this.siteSettings.default_subcategory_on_read_only_category - ) { - defaultSubcategory = category.subcategories.find((subcategory) => { - return subcategory.get("permission") === PermissionType.FULL; - }); + if (this.siteSettings.default_subcategory_on_read_only_category) { + cannotCreateTopicOnCategory = false; - canCreateTopicOnSubCategory = !!defaultSubcategory; + if (!canCreateTopicOnCategory && category.subcategories) { + defaultSubcategory = category.subcategories.find((subcategory) => { + return subcategory.get("permission") === PermissionType.FULL; + }); + canCreateTopicOnSubCategory = !!defaultSubcategory; + } } this.controllerFor("navigation/category").setProperties({ canCreateTopicOnCategory, - cannotCreateTopicOnCategory: !( - canCreateTopicOnCategory || canCreateTopicOnSubCategory - ), + cannotCreateTopicOnCategory, canCreateTopic, canCreateTopicOnSubCategory, defaultSubcategory, diff --git a/spec/fabricators/category_fabricator.rb b/spec/fabricators/category_fabricator.rb index eb6dbcbd0cf..6932d40ffcc 100644 --- a/spec/fabricators/category_fabricator.rb +++ b/spec/fabricators/category_fabricator.rb @@ -8,7 +8,7 @@ end Fabricator(:category_with_definition, from: :category) { skip_category_definition false } -Fabricator(:private_category, from: :category) do +Fabricator(:category_with_group_and_permission, from: :category) do transient :group transient :permission_type @@ -17,7 +17,6 @@ Fabricator(:private_category, from: :category) do user after_build do |cat, transients| - cat.update!(read_restricted: true) cat.category_groups.build( group_id: transients[:group].id, permission_type: transients[:permission_type] || CategoryGroup.permission_types[:full], @@ -25,6 +24,10 @@ Fabricator(:private_category, from: :category) do end end +Fabricator(:private_category, from: :category_with_group_and_permission) do + after_build { |cat, transients| cat.update!(read_restricted: true) } +end + Fabricator(:private_category_with_definition, from: :private_category) do skip_category_definition false end diff --git a/spec/system/composer/default_to_subcategory_spec.rb b/spec/system/composer/default_to_subcategory_spec.rb index 7de89c9fcd1..add412d8e83 100644 --- a/spec/system/composer/default_to_subcategory_spec.rb +++ b/spec/system/composer/default_to_subcategory_spec.rb @@ -6,32 +6,63 @@ describe "Default to Subcategory when parent Category doesn't allow posting", fab!(:user) { Fabricate(:user) } fab!(:group) { Fabricate(:group) } fab!(:group_user) { Fabricate(:group_user, user: user, group: group) } + fab!(:default_latest_category) { Fabricate(:category) } fab!(:category) { Fabricate(:private_category, group: group, permission_type: 3) } fab!(:subcategory) do Fabricate(:private_category, parent_category_id: category.id, group: group, permission_type: 1) end + fab!(:category_with_no_subcategory) do + Fabricate(:category_with_group_and_permission, group: group, permission_type: 3) + end let(:category_page) { PageObjects::Pages::Category.new } - before { sign_in(user) } - describe "Setting enabled and can't post on parent category" do - before { SiteSetting.default_subcategory_on_read_only_category = true } - - it "should have 'New Topic' button enabled and default Subcategory set in the composer" do - category_page.visit(category) - expect(category_page).to have_button("New Topic", disabled: false) - category_page.new_topic_button.click - select_kit = - PageObjects::Components::SelectKit.new(page.find("#reply-control.open .category-chooser")) - expect(select_kit).to have_selected_value(subcategory.id) + describe "anon user" do + it "can visit the category" do + category_page.visit(category_with_no_subcategory) + select_kit = PageObjects::Components::SelectKit.new(page.find(".navigation-container")) + expect(select_kit).to have_selected_value(category_with_no_subcategory.id) end end - describe "Setting disabled and can't post on parent category" do - before { SiteSetting.default_subcategory_on_read_only_category = false } + describe "logged in user" do + before { sign_in(user) } + describe "default_subcategory_on_read_only_category setting enabled and can't post on parent category" do + before do + SiteSetting.default_subcategory_on_read_only_category = true + SiteSetting.default_composer_category = default_latest_category.id + end + describe "Category has subcategory" do + it "should have 'New Topic' button enabled and default Subcategory set in the composer" do + category_page.visit(category) + expect(category_page).to have_button("New Topic", disabled: false) + category_page.new_topic_button.click + select_kit = + PageObjects::Components::SelectKit.new( + page.find("#reply-control.open .category-chooser"), + ) + expect(select_kit).to have_selected_value(subcategory.id) + end + end + describe "Category does not have subcategory" do + it "should have the 'New Topic' button enabled and default Subcategory set to latest default subcategory" do + category_page.visit(category_with_no_subcategory) + expect(category_page).to have_button("New Topic", disabled: false) + category_page.new_topic_button.click + select_kit = + PageObjects::Components::SelectKit.new( + page.find("#reply-control.open .category-chooser"), + ) + expect(select_kit).to have_selected_value(default_latest_category.id) + end + end + end - it "should have 'New Topic' button disabled" do - category_page.visit(category) - expect(category_page).to have_button("New Topic", disabled: true) + describe "Setting disabled and can't post on parent category" do + before { SiteSetting.default_subcategory_on_read_only_category = false } + it "should have 'New Topic' button disabled" do + category_page.visit(category) + expect(category_page).to have_button("New Topic", disabled: true) + end end end end