A platform for community discussion. Free, open, simple.
FIX: ensures tag-groups are used to allow category edit on topics (#7141)
Joffrey JAFFEUX7ae1afa7d98b
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 16b2ea39d39..f65daef0a18 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -290,7 +290,9 @@ class TopicsController < ApplicationControllerendif category && topic_tags = (params[:tags] || topic.tags.pluck(:name))- allowed_tags = category.tags.pluck(:name)+ category_tags = category.tags.pluck(:name)+ category_tag_groups = category.tag_groups.joins(:tags).pluck("tags.name")+ allowed_tags = (category_tags + category_tag_groups).uniqif topic_tags.present? && allowed_tags.present?invalid_tags = topic_tags - allowed_tagsdiff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 52fad1866f4..4e7c55bb4c6 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -1014,15 +1014,17 @@ RSpec.describe TopicsController docontext 'updating to a category with restricted tags' dolet!(:category) { Fabricate(:category) }let!(:restricted_category) { Fabricate(:category) }- let!(:tag1) { Fabricate(:tag, name: 'tag1') }- let!(:tag2) { Fabricate(:tag, name: 'tag2') }+ let!(:tag1) { Fabricate(:tag) }+ let!(:tag2) { Fabricate(:tag) }+ let!(:tag_group_1) { Fabricate(:tag_group, tag_names: [tag1.name]) }+ let!(:tag_group_2) { Fabricate(:tag_group) }before doSiteSetting.tagging_enabled = truetopic.update!(tags: [tag1])end- it 'can change to a category disallowing this topic current tags' do+ it 'can’t change to a category disallowing this topic current tags' dorestricted_category.allowed_tags = [tag2.name]put "/t/#{topic.slug}/#{topic.id}.json", params: { category_id: restricted_category.id }@@ -1034,9 +1036,30 @@ RSpec.describe TopicsController doexpect(topic.reload.category_id).not_to eq(restricted_category.id)end+ it 'can’t change to a category disallowing this topic current tag (through tag_group)' do+ tag_group_2.tags = [tag2]+ restricted_category.allowed_tag_groups = [tag_group_2.name]++ put "/t/#{topic.slug}/#{topic.id}.json", params: { category_id: restricted_category.id }++ result = ::JSON.parse(response.body)++ expect(response.status).to eq(422)+ expect(result['errors']).to be_present+ expect(topic.reload.category_id).not_to eq(restricted_category.id)+ end+it 'can change to a category allowing this topic current tags' dorestricted_category.allowed_tags = [tag1.name]- restricted_category.reload++ put "/t/#{topic.slug}/#{topic.id}.json", params: { category_id: restricted_category.id }++ expect(response.status).to eq(200)+ end++ it 'can change to a category allowing this topic current tags (through tag_group)' do+ tag_group_1.tags = [tag1]+ restricted_category.allowed_tag_groups = [tag_group_1.name]put "/t/#{topic.slug}/#{topic.id}.json", params: { category_id: restricted_category.id }