FIX: Add to tags result set only visible tags (#10580)

FIX: Add to tags result set only visible tags (#10580)

diff --git a/lib/search.rb b/lib/search.rb
index 9180e1a..23d274b 100644
--- a/lib/search.rb
+++ b/lib/search.rb
@@ -809,8 +809,10 @@ class Search
       .order("name asc")
       .limit(limit)
 
+    hidden_tag_names = DiscourseTagging.hidden_tag_names(@guardian)
+
     tags.each do |tag|
-      @results.add(tag)
+      @results.add(tag) if !hidden_tag_names.include?(tag.name)
     end
   end
 
diff --git a/spec/lib/search_spec.rb b/spec/lib/search_spec.rb
index e37367b..784b4f8 100644
--- a/spec/lib/search_spec.rb
+++ b/spec/lib/search_spec.rb
@@ -64,4 +64,34 @@ describe Search do
     end
   end
 
+  context "#execute" do
+    before do
+      SiteSetting.tagging_enabled = true
+    end
+
+    context "staff tags" do
+      fab!(:hidden_tag) { Fabricate(:tag) }
+      let!(:staff_tag_group) { Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name]) }
+      fab!(:topic) { Fabricate(:topic, tags: [hidden_tag]) }
+      fab!(:post) { Fabricate(:post, topic: topic) }
+
+      before do
+        SiteSetting.tagging_enabled = true
+
+        SearchIndexer.enable
+        SearchIndexer.index(hidden_tag, force: true)
+        SearchIndexer.index(topic, force: true)
+      end
+
+      it "are visible to staff users" do
+        result = Search.execute(hidden_tag.name, guardian: Guardian.new(Fabricate(:admin)))
+        expect(result.tags).to contain_exactly(hidden_tag)
+      end
+
+      it "are hidden to regular users" do
+        result = Search.execute(hidden_tag.name, guardian: Guardian.new(Fabricate(:user)))
+        expect(result.tags).to contain_exactly()
+      end
+    end
+  end
 end

GitHub sha: 38c9c871

This commit appears in #10580 which was merged by udan11.

Minor but this line should not be required as the same setting is enabled in the before block.