A platform for community discussion. Free, open, simple.
FIX: TopicQuery for NULL `category.topic_id` (#20664)
David Taylor964f37476dbb
diff --git a/lib/topic_query.rb b/lib/topic_query.rbindex 88f6e428be4..4be35415753 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -692,7 +692,10 @@ class TopicQueryresult = result.where("topics.category_id IN (?)", Category.subcategory_ids(category_id))if !SiteSetting.show_category_definitions_in_topic_listsresult =- result.where("categories.topic_id <> topics.id OR topics.category_id = ?", category_id)+ result.where(+ "categories.topic_id IS DISTINCT FROM topics.id OR topics.category_id = ?",+ category_id,+ )endendresult = result.references(:categories)diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rbindex 98442324c2d..8ca8ce492ef 100644--- a/spec/lib/topic_query_spec.rb+++ b/spec/lib/topic_query_spec.rb@@ -1909,4 +1909,31 @@ RSpec.describe TopicQuery do)endend++ describe "show_category_definitions_in_topic_lists setting" do+ fab!(:category) { Fabricate(:category_with_definition) }+ fab!(:subcategory) { Fabricate(:category_with_definition, parent_category: category) }+ fab!(:subcategory_regular_topic) { Fabricate(:topic, category: subcategory) }++ it "excludes subcategory definition topics by default" do+ expect(+ TopicQuery.new(nil, category: category.id).list_latest.topics.map(&:id),+ ).to contain_exactly(category.topic_id, subcategory_regular_topic.id)+ end++ it "works when topic_id is null" do+ subcategory.topic.destroy!+ subcategory.update!(topic_id: nil)+ expect(+ TopicQuery.new(nil, category: category.id).list_latest.topics.map(&:id),+ ).to contain_exactly(category.topic_id, subcategory_regular_topic.id)+ end++ it "includes subcategory definition when setting enabled" do+ SiteSetting.show_category_definitions_in_topic_lists = true+ expect(+ TopicQuery.new(nil, category: category.id).list_latest.topics.map(&:id),+ ).to contain_exactly(category.topic_id, subcategory.topic_id, subcategory_regular_topic.id)+ end+ endenddiff --git a/spec/requests/categories_controller_spec.rb b/spec/requests/categories_controller_spec.rbindex 863ad47be49..8eec0f2bcd4 100644--- a/spec/requests/categories_controller_spec.rb+++ b/spec/requests/categories_controller_spec.rb@@ -131,10 +131,17 @@ RSpec.describe CategoriesController docategory_list = response.parsed_body["category_list"]category_response = category_list["categories"].find { |c| c["id"] == category.id }- expect(category_response["topics"].map { |c| c["id"] }).to contain_exactly(topic1.id)+ expect(category_response["topics"].map { |c| c["id"] }).to contain_exactly(+ topic1.id,+ topic2.id,+ topic3.id,+ )subcategory_response = category_response["subcategory_list"][0]- expect(subcategory_response["topics"].map { |c| c["id"] }).to contain_exactly(topic2.id)+ expect(subcategory_response["topics"].map { |c| c["id"] }).to contain_exactly(+ topic2.id,+ topic3.id,+ )subsubcategory_response = subcategory_response["subcategory_list"][0]expect(subsubcategory_response["topics"].map { |c| c["id"] }).to contain_exactly(topic3.id)