From d4afb0e8679cff6f343d574527aaeb0c75668627 Mon Sep 17 00:00:00 2001 From: Mantas Masalskis Date: Mon, 28 Oct 2019 16:38:14 +0100 Subject: [PATCH] Fixes issue #2789: not possible to search or link KB-answer within ticket. --- .../knowledge_base/search_controller.rb | 6 ++-- app/models/knowledge_base.rb | 2 +- .../search_with_details_spec.rb | 33 +++++++++++++++++++ .../linking_knowledge_base_answer_spec.rb | 26 +++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 spec/requests/knowledge_base/search_with_details_spec.rb create mode 100644 spec/system/ticket/linking_knowledge_base_answer_spec.rb diff --git a/app/controllers/knowledge_base/search_controller.rb b/app/controllers/knowledge_base/search_controller.rb index 580d5dd83..287427524 100644 --- a/app/controllers/knowledge_base/search_controller.rb +++ b/app/controllers/knowledge_base/search_controller.rb @@ -85,7 +85,7 @@ class KnowledgeBase::SearchController < ApplicationController end { - id: object.id, + id: object.id.to_s, type: object.class.name, icon: 'knowledge-base-answer', date: object.updated_at, @@ -108,7 +108,7 @@ class KnowledgeBase::SearchController < ApplicationController end { - id: object.id, + id: object.id.to_s, type: object.class.name, fontName: object.category.knowledge_base.iconset, date: object.updated_at, @@ -130,7 +130,7 @@ class KnowledgeBase::SearchController < ApplicationController end { - id: object.id, + id: object.id.to_s, type: object.class.name, icon: 'knowledge-base', date: object.updated_at, diff --git a/app/models/knowledge_base.rb b/app/models/knowledge_base.rb index 7b989ea06..ea9cd244d 100644 --- a/app/models/knowledge_base.rb +++ b/app/models/knowledge_base.rb @@ -121,7 +121,7 @@ class KnowledgeBase < ApplicationModel .active .joins(:kb_locales) .group('knowledge_bases.id') - .pluck('COUNT(knowledge_base_locales.id) as locales_count') + .pluck(Arel.sql('COUNT(knowledge_base_locales.id) as locales_count')) .any? { |e| e > 1 } end diff --git a/spec/requests/knowledge_base/search_with_details_spec.rb b/spec/requests/knowledge_base/search_with_details_spec.rb new file mode 100644 index 000000000..e52823866 --- /dev/null +++ b/spec/requests/knowledge_base/search_with_details_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe 'Knowledge Base search with details', type: :request, searchindex: true do + include_context 'basic Knowledge Base' + + before do + configure_elasticsearch(required: true, rebuild: true) do + published_answer + end + end + + let(:endpoint) { '/api/v1/knowledge_bases/search' } + + context 'ensure details ID type matches ES ID type' do + it 'for answers' do + post endpoint, params: { query: published_answer.translations.first.title } + + expect(json_response['details'][0]['id']).to be_a_kind_of String + end + + it 'for categories' do + post endpoint, params: { query: category.translations.first.title } + + expect(json_response['details'][0]['id']).to be_a_kind_of String + end + + it 'for knowledge base' do + post endpoint, params: { query: knowledge_base.translations.first.title } + + expect(json_response['details'][0]['id']).to be_a_kind_of String + end + end +end diff --git a/spec/system/ticket/linking_knowledge_base_answer_spec.rb b/spec/system/ticket/linking_knowledge_base_answer_spec.rb new file mode 100644 index 000000000..a61833e2e --- /dev/null +++ b/spec/system/ticket/linking_knowledge_base_answer_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe 'linking Knowledge Base answer', type: :system, authenticated: true, searchindex: true do + include_context 'basic Knowledge Base' + + before do + configure_elasticsearch(required: true, rebuild: true) do + published_answer + end + end + + it do + ticket = create :ticket, group: Group.find_by(name: 'Users') + visit "#ticket/zoom/#{ticket.id}" + + find(:css, '.active .link_kb_answers .js-add').click + + target_translation = published_answer.translations.first + + find(:css, '.active .link_kb_answers .js-input').send_keys target_translation.title + + find(:css, %(.active .link_kb_answers li[data-value="#{target_translation.id}"])).click + + expect(find(:css, '.active .link_kb_answers ol')).to have_text target_translation.title + end +end