FIX: ensures tag-groups are used to allow category edit on topics (#7141)

FIX: ensures tag-groups are used to allow category edit on topics (#7141)

diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb
index 16b2ea3..f65daef 100644
--- a/app/controllers/topics_controller.rb
+++ b/app/controllers/topics_controller.rb
@@ -290,7 +290,9 @@ class TopicsController < ApplicationController
       end
 
       if 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).uniq
 
         if topic_tags.present? && allowed_tags.present?
           invalid_tags = topic_tags - allowed_tags
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 52fad18..4e7c55b 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -1014,15 +1014,17 @@ RSpec.describe TopicsController do
         context 'updating to a category with restricted tags' do
           let!(: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 do
             SiteSetting.tagging_enabled = true
             topic.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' do
             restricted_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 do
             expect(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' do
             restricted_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 }

GitHub sha: 7ae1afa7