diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 2b9b45e0af6..1033c0808ab 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -197,15 +197,21 @@ class TagsController < ::ApplicationController slug_or_id = params[:category] return true if slug_or_id.nil? - parent_slug_or_id = params[:parent_category] + if slug_or_id == 'none' && params[:parent_category] + @filter_on_category = Category.query_category(params[:parent_category], nil) + params[:no_subcategories] = 'true' + else + parent_slug_or_id = params[:parent_category] - parent_category_id = nil - if parent_slug_or_id.present? - parent_category_id = Category.query_parent_category(parent_slug_or_id) - redirect_or_not_found and return if parent_category_id.blank? + parent_category_id = nil + if parent_slug_or_id.present? + parent_category_id = Category.query_parent_category(parent_slug_or_id) + redirect_or_not_found and return if parent_category_id.blank? + end + + @filter_on_category = Category.query_category(slug_or_id, parent_category_id) end - @filter_on_category = Category.query_category(slug_or_id, parent_category_id) redirect_or_not_found and return if !@filter_on_category guardian.ensure_can_see!(@filter_on_category) diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb new file mode 100644 index 00000000000..c79022a1e30 --- /dev/null +++ b/spec/controllers/tags_controller_spec.rb @@ -0,0 +1,42 @@ +require 'rails_helper' + +describe TagsController do + describe 'show_latest' do + let(:tag) { Fabricate(:tag) } + let(:category) { Fabricate(:category) } + let(:subcategory) { Fabricate(:category, parent_category_id: category.id) } + + context 'tagging disabled' do + it "returns 404" do + xhr :get, :show_latest, tag_id: tag.name + expect(response.status).to eq(404) + end + end + + context 'tagging enabled' do + before do + SiteSetting.tagging_enabled = true + end + + it "can filter by tag" do + xhr :get, :show_latest, tag_id: tag.name + expect(response).to be_success + end + + it "can filter by category and tag" do + xhr :get, :show_latest, tag_id: tag.name, category: category.slug + expect(response).to be_success + end + + it "can filter by category, sub-category, and tag" do + xhr :get, :show_latest, tag_id: tag.name, category: subcategory.slug, parent_category: category.slug + expect(response).to be_success + end + + it "can filter by category, no sub-category, and tag" do + xhr :get, :show_latest, tag_id: tag.name, category: 'none', parent_category: category.slug + expect(response).to be_success + end + end + end +end