diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index b97de2b8178..b5d6fadb0c4 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -37,14 +37,14 @@ class TagsController < ::ApplicationController { id: tag_group.id, name: tag_group.name, tags: self.class.tag_counts_json(tag_group.tags) } end - ungrouped_tags = Tag.where("tags.id NOT IN (select tag_id from tag_group_memberships)") + ungrouped_tags = Tag.where("tags.id NOT IN (select tag_id from tag_group_memberships) AND tags.topic_count > 0") render json: { tags: self.class.tag_counts_json(ungrouped_tags), # tags that don't belong to a group extras: { tag_groups: grouped_tag_counts } } else - unrestricted_tags = Tag.where("tags.id NOT IN (select tag_id from category_tags)") + unrestricted_tags = Tag.where("tags.id NOT IN (select tag_id from category_tags) AND tags.topic_count > 0") categories = Category.where("id in (select category_id from category_tags)") .where("id in (?)", guardian.allowed_category_ids) diff --git a/spec/requests/tags_controller_spec.rb b/spec/requests/tags_controller_spec.rb index 568fa9858dd..9bef5d5f7c0 100644 --- a/spec/requests/tags_controller_spec.rb +++ b/spec/requests/tags_controller_spec.rb @@ -5,6 +5,42 @@ describe TagsController do SiteSetting.tagging_enabled = true end + describe '#index' do + + before do + tag = Fabricate(:tag, name: 'test') + topic_tag = Fabricate(:tag, name: 'topic-test', topic_count: 1) + end + + shared_examples "successfully retrieve tags with topic_count > 0" do + it "should return the right response" do + get "/tags.json" + + expect(response).to be_success + + tags = JSON.parse(response.body)["tags"] + expect(tags.length).to eq(1) + expect(tags[0]['text']).to eq("topic-test") + end + end + + context "with tags_listed_by_group enabled" do + before do + SiteSetting.tags_listed_by_group = true + end + + include_examples "successfully retrieve tags with topic_count > 0" + end + + context "with tags_listed_by_group disabled" do + before do + SiteSetting.tags_listed_by_group = false + end + + include_examples "successfully retrieve tags with topic_count > 0" + end + end + describe '#check_hashtag' do let(:tag) { Fabricate(:tag, name: 'test') }