Fixes #3931 - Changing the category of a Knowledge Base answer leads to alternative translation loop

This commit is contained in:
Florian Liebe 2022-06-16 14:24:57 +02:00
parent cadb462208
commit 76fe4ce0e7
2 changed files with 38 additions and 1 deletions

View file

@ -13,7 +13,9 @@ class KnowledgeBase::Public::AnswersController < KnowledgeBase::Public::BaseCont
private
def render_alternative
@alternative = find_answer @knowledge_base.answers.eager_load(translations: :kb_locale), params[:answer], locale: false
answers = @knowledge_base.answers.where(category: params[:category]).eager_load(translations: :kb_locale)
@alternative = find_answer(answers, params[:answer], locale: false)
raise ActiveRecord::RecordNotFound if !@alternative&.translations&.any?

View file

@ -35,4 +35,39 @@ RSpec.describe 'KnowledgeBase public answers', type: :request do
end
end
end
describe '#render_alternative' do
context 'when a translation is available' do
before { create(:knowledge_base_translation, kb_locale: alternative_locale) }
it 'returns OK for published answer' do
get help_answer_path(alternative_locale.system_locale.locale, category, published_answer)
expect(response).to have_http_status :ok
end
it 'returns NOT FOUND for draft answer' do
get help_answer_path(alternative_locale.system_locale.locale, category, draft_answer)
expect(response).to have_http_status :not_found
end
# https://github.com/zammad/zammad/issues/3931
context 'when the category has been updated' do
let(:new_category) { create(:knowledge_base_category, knowledge_base: knowledge_base) }
it 'returns NOT FOUND for published answer if old category is used' do
published_answer.update! category_id: new_category.id
get help_answer_path(alternative_locale.system_locale.locale, category, published_answer)
expect(response).to have_http_status :not_found
end
it 'returns OK for published answer if new category is used' do
published_answer.update! category_id: new_category.id
get help_answer_path(alternative_locale.system_locale.locale, new_category, published_answer)
expect(response).to have_http_status :ok
end
end
end
end
end