FIX: Permit new tags when allow_global_tags true. (#7722)

FIX: Permit new tags when allow_global_tags true. (#7722)

diff --git a/lib/discourse_tagging.rb b/lib/discourse_tagging.rb
index ab276c5..9f824a2 100644
--- a/lib/discourse_tagging.rb
+++ b/lib/discourse_tagging.rb
@@ -56,7 +56,7 @@ module DiscourseTagging
           selected_tags: tag_names
         ).to_a
 
-        if tags.size < tag_names.size && (category.nil? || (category.tags.count == 0 && category.tag_groups.count == 0))
+        if tags.size < tag_names.size && (category.nil? || category.allow_global_tags || (category.tags.count == 0 && category.tag_groups.count == 0))
           tag_names.each do |name|
             unless Tag.where_name(name).exists?
               tags << Tag.create(name: name)
diff --git a/spec/components/discourse_tagging_spec.rb b/spec/components/discourse_tagging_spec.rb
index 39ff189..8ab8b61 100644
--- a/spec/components/discourse_tagging_spec.rb
+++ b/spec/components/discourse_tagging_spec.rb
@@ -108,6 +108,22 @@ describe DiscourseTagging do
       end
     end
 
+    it 'respects category allow_global_tags setting' do
+      tag = Fabricate(:tag)
+      other_tag = Fabricate(:tag)
+      tag_group = Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [tag.name])
+      category = Fabricate(:category, allowed_tag_groups: [tag_group.name])
+      other_category = Fabricate(:category, allowed_tags: [other_tag.name])
+      topic = Fabricate(:topic, category: category)
+
+      DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name, other_tag.name, 'hello'])
+      expect(topic.tags.pluck(:name)).to contain_exactly(tag.name)
+
+      category.update!(allow_global_tags: true)
+      DiscourseTagging.tag_topic_by_names(topic, Guardian.new(admin), [tag.name, other_tag.name, 'hello'])
+      expect(topic.tags.pluck(:name)).to contain_exactly(tag.name, 'hello')
+    end
+
     context 'respects category minimum_required_tags setting' do
       fab!(:category) { Fabricate(:category, minimum_required_tags: 2) }
       fab!(:topic) { Fabricate(:topic, category: category) }

GitHub sha: 8bd815da

2 Likes