search_with_details_spec.rb 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Knowledge Base search with details', searchindex: true, type: :request do
  4. include_context 'basic Knowledge Base'
  5. before do
  6. published_answer
  7. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  8. end
  9. let(:endpoint) { '/api/v1/knowledge_bases/search' }
  10. context 'ensure details ID type matches ES ID type' do
  11. it 'for answers' do
  12. post endpoint, params: { query: published_answer.translations.first.title }
  13. expect(json_response['details'][0]['id']).to be_a Integer
  14. end
  15. it 'for categories' do
  16. post endpoint, params: { query: category.translations.first.title }
  17. expect(json_response['details'][0]['id']).to be_a Integer
  18. end
  19. it 'for knowledge base' do
  20. post endpoint, params: { query: knowledge_base.translations.first.title }
  21. expect(json_response['details'][0]['id']).to be_a Integer
  22. end
  23. end
  24. context 'when category translation to one of locales is missing' do
  25. let(:search_phrase) { 'search_phrase' }
  26. let(:alternative_translation) { create(:'knowledge_base/answer/translation', title: search_phrase, kb_locale: alternative_locale, answer: published_answer) }
  27. before do
  28. alternative_translation
  29. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  30. end
  31. it 'returns answer in locale without category translation' do
  32. post endpoint, params: { query: search_phrase }
  33. expect(json_response['details'][0]['id']).to be alternative_translation.id
  34. end
  35. end
  36. context 'when parent category translation to one of locales is missing' do
  37. let(:search_phrase) { 'search_phrase' }
  38. let(:child_category) { create(:'knowledge_base/category', parent: category) }
  39. let(:child_category_translation) { create(:'knowledge_base/category/translation', title: search_phrase, kb_locale: alternative_locale, category: child_category) }
  40. before do
  41. child_category_translation
  42. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  43. end
  44. it 'returns category in locale without category translation', authenticated_as: -> { create(:admin) } do
  45. post endpoint, params: { query: search_phrase }
  46. expect(json_response['details'][0]['subtitle']).to eq category.translation_to(primary_locale).title
  47. end
  48. end
  49. context 'when answer tree is long' do
  50. let(:category1) { create(:'knowledge_base/category') }
  51. let(:category2) { create(:'knowledge_base/category', parent: category1) }
  52. let(:category3) { create(:'knowledge_base/category', parent: category2) }
  53. let(:answer_cut_tree) { create(:knowledge_base_answer, :published, :with_attachment, category: category3) }
  54. let(:category4) { create(:'knowledge_base/category') }
  55. let(:category5) { create(:'knowledge_base/category', parent: category4) }
  56. let(:answer_full_tree) { create(:knowledge_base_answer, :published, :with_attachment, category: category5) }
  57. before do
  58. answer_cut_tree && answer_full_tree
  59. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  60. end
  61. it 'returns category with cut tree', authenticated_as: -> { create(:admin) } do
  62. post endpoint, params: { query: answer_cut_tree.translations.first.title }
  63. expect(json_response['details'][0]['subtitle']).to eq("#{category1.translations.first.title} > .. > #{category3.translations.first.title}")
  64. end
  65. it 'returns category with full tree', authenticated_as: -> { create(:admin) } do
  66. post endpoint, params: { query: answer_full_tree.translations.first.title }
  67. expect(json_response['details'][0]['subtitle']).to eq("#{category4.translations.first.title} > #{category5.translations.first.title}")
  68. end
  69. end
  70. context 'when using include_locale parameter' do
  71. context 'when no multiple locales exists' do
  72. it 'no locale added to title' do
  73. post endpoint, params: { query: published_answer.translations.first.title, include_locale: true }
  74. expect(json_response['details'][0]['title']).to not_include('(EN-US)')
  75. end
  76. end
  77. context 'when multiple locales exists' do
  78. before do
  79. # Create a alternative knowledge base locale.
  80. alternative_locale
  81. end
  82. it 'locale added to title' do
  83. post endpoint, params: { query: published_answer.translations.first.title, include_locale: true }
  84. expect(json_response['details'][0]['title']).to include('(EN-US)')
  85. end
  86. end
  87. end
  88. context 'when using paging' do
  89. let(:answers) do
  90. Array.new(20) do |nth|
  91. create(:knowledge_base_answer, :published, :with_attachment, category: category, translation_attributes: { title: "#{search_phrase} #{nth}" })
  92. end
  93. end
  94. let(:search_phrase) { 'paging test' }
  95. before do
  96. answers
  97. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  98. end
  99. it 'returns success' do
  100. post endpoint, params: { query: search_phrase, per_page: 10, page: 0 }
  101. expect(response).to have_http_status(:ok)
  102. end
  103. it 'returns defined amount of items' do
  104. post endpoint, params: { query: search_phrase, per_page: 7, page: 0 }
  105. expect(json_response['result'].count).to be 7
  106. end
  107. end
  108. context 'when sorting' do
  109. let(:answers) do
  110. Array.new(3) do |nth|
  111. travel nth.seconds do
  112. create(:knowledge_base_answer, :published, :with_attachment,
  113. category: category,
  114. translation_attributes: { title: "#{search_phrase} #{nth}" })
  115. end
  116. end
  117. end
  118. let(:search_phrase) { 'paging test' }
  119. before do |example|
  120. answers
  121. travel(3.hours) { answers[1].translations.first.touch }
  122. next if !example.metadata[:searchindex]
  123. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  124. end
  125. shared_examples 'test sorting' do
  126. it 'sorts answers from most up-to-date to oldest' do
  127. post endpoint, params: { query: search_phrase }
  128. returned_ids = json_response['details'].pluck('id')
  129. expected_ids = [answers[1], answers[2], answers[0]].map { |elem| elem.translations.first.id }
  130. expect(returned_ids).to eq expected_ids
  131. end
  132. end
  133. context 'with elasticsearch' do
  134. include_examples 'test sorting'
  135. end
  136. context 'with no elasticsearch', searchindex: false do
  137. include_examples 'test sorting'
  138. end
  139. end
  140. end