Fixes #3148 - Knowledge Base search crashes if (parent) category is missing translation to the locale of the found object

This commit is contained in:
Mantas 2020-10-19 08:38:37 +03:00
parent 971ece82e4
commit 02159c1873
2 changed files with 34 additions and 3 deletions

View file

@ -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

View file

@ -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