FIX: Fix for Default to subcategory when parent category does not allow posting (#21537)

* FIX: Fix for Default to subcategory when parent category does not allow posting

* added tests for edge case scenario

* implemented correct behaviour when parent category doesn't have subcategories

* implemented new fabricator for categories and suggested changes
This commit is contained in:
Juan David Martínez Cubillos 2023-05-19 07:37:23 -05:00 committed by GitHub
parent 0ffe463d79
commit 774313ef0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 71 additions and 35 deletions

View File

@ -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 (

View File

@ -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,

View File

@ -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

View File

@ -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