From 02159c18735bd6046875e716a13ccfd4405aaead Mon Sep 17 00:00:00 2001 From: Mantas Date: Mon, 19 Oct 2020 08:38:37 +0300 Subject: [PATCH] Fixes #3148 - Knowledge Base search crashes if (parent) category is missing translation to the locale of the found object --- .../knowledge_base/search_controller.rb | 6 ++-- .../search_with_details_spec.rb | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/app/controllers/knowledge_base/search_controller.rb b/app/controllers/knowledge_base/search_controller.rb index 580d5dd83..d68f99071 100644 --- a/app/controllers/knowledge_base/search_controller.rb +++ b/app/controllers/knowledge_base/search_controller.rb @@ -1,6 +1,6 @@ class KnowledgeBase::SearchController < ApplicationController skip_before_action :verify_csrf_token - #skip_before_action :verify_authenticity_token + prepend_before_action :authentication_check_only include KnowledgeBaseHelper include ActionView::Helpers::SanitizeHelper @@ -74,7 +74,7 @@ class KnowledgeBase::SearchController < ApplicationController end def public_item_details_answer(meta, object) - category_translation = object.answer.category.translation_to(object.kb_locale) + category_translation = object.answer.category.translation_preferred(object.kb_locale) path = help_answer_path(category_translation, object, locale: object.kb_locale.system_locale.locale) url = case url_type @@ -97,7 +97,7 @@ class KnowledgeBase::SearchController < ApplicationController end def public_item_details_category(meta, object) - parent_category_translation = object.category.parent&.translation_to(object.kb_locale) + parent_category_translation = object.category.parent&.translation_preferred(object.kb_locale) path = help_category_path(object, locale: object.kb_locale.system_locale.locale) url = case url_type diff --git a/spec/requests/knowledge_base/search_with_details_spec.rb b/spec/requests/knowledge_base/search_with_details_spec.rb index 018cbda11..221a00519 100644 --- a/spec/requests/knowledge_base/search_with_details_spec.rb +++ b/spec/requests/knowledge_base/search_with_details_spec.rb @@ -30,4 +30,35 @@ RSpec.describe 'Knowledge Base search with details', type: :request, searchindex expect(json_response['details'][0]['id']).to be_a_kind_of Integer end end + + context 'when category translation to one of locales is missing' do + let(:search_phrase) { 'search_phrase' } + let(:alternative_translation) { create('knowledge_base/answer/translation', title: search_phrase, kb_locale: alternative_locale, answer: published_answer) } + + before do + alternative_translation + rebuild_searchindex + end + + it 'returns answer in locale without category translation' do + post endpoint, params: { query: search_phrase } + + expect(json_response['details'][0]['id']).to be alternative_translation.id + end + end + + context 'when parent category translation to one of locales is missing' do + let(:search_phrase) { 'search_phrase' } + let(:child_category) { create('knowledge_base/category', parent: category) } + let(:child_category_translation) { create('knowledge_base/category/translation', title: search_phrase, kb_locale: alternative_locale, category: child_category) } + + before do + child_category_translation && rebuild_searchindex + end + + it 'returns category in locale without category translation', authenticated_as: -> { create(:admin) } do + post endpoint, params: { query: search_phrase } + expect(json_response['details'][0]['subtitle']).to eq category.translation_to(primary_locale).title + end + end end