From 76fe4ce0e747f332ccb47237245c157e237aa0b7 Mon Sep 17 00:00:00 2001 From: Florian Liebe Date: Thu, 16 Jun 2022 14:24:57 +0200 Subject: [PATCH] Fixes #3931 - Changing the category of a Knowledge Base answer leads to alternative translation loop --- .../public/answers_controller.rb | 4 ++- .../knowledge_base_public/answers_spec.rb | 35 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/controllers/knowledge_base/public/answers_controller.rb b/app/controllers/knowledge_base/public/answers_controller.rb index aeb970291..fe605cf71 100644 --- a/app/controllers/knowledge_base/public/answers_controller.rb +++ b/app/controllers/knowledge_base/public/answers_controller.rb @@ -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? diff --git a/spec/requests/knowledge_base_public/answers_spec.rb b/spec/requests/knowledge_base_public/answers_spec.rb index 7026868f0..2e1eed324 100644 --- a/spec/requests/knowledge_base_public/answers_spec.rb +++ b/spec/requests/knowledge_base_public/answers_spec.rb @@ -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