From 82e170d6a636ac849c3e12af702252555719e183 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Thu, 28 Jul 2016 16:18:51 -0400 Subject: [PATCH] FIX: 404 when filtering by category, no sub-category, and a tag --- app/controllers/tags_controller.rb | 18 ++++++---- spec/controllers/tags_controller_spec.rb | 42 ++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 spec/controllers/tags_controller_spec.rb diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 2b9b45e0a..1033c0808 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 000000000..c79022a1e --- /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