translation_spec.rb 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/contexts/factory_context'
  4. RSpec.describe KnowledgeBase::Answer::Translation, current_user_id: 1, type: :model do
  5. subject { create(:knowledge_base_answer_translation) }
  6. include_context 'factory'
  7. it { is_expected.to validate_presence_of(:title) }
  8. it { is_expected.to validate_uniqueness_of(:kb_locale_id).scoped_to(:answer_id).with_message(%r{}) }
  9. it { is_expected.to belong_to(:answer) }
  10. it { is_expected.to belong_to(:kb_locale) }
  11. def handle_elasticsearch(enabled)
  12. if enabled
  13. searchindex_model_reload([KnowledgeBase::Translation, KnowledgeBase::Category::Translation, KnowledgeBase::Answer::Translation])
  14. else
  15. Setting.set('es_url', nil)
  16. end
  17. end
  18. describe '.search' do
  19. include_context 'basic Knowledge Base'
  20. shared_examples 'verify given search backend' do |trait:, user_id:, is_visible:, elasticsearch:|
  21. prefix = is_visible ? 'lists' : 'does not list'
  22. it "#{prefix} #{trait} answer to #{user_id} when ES=#{elasticsearch}", searchindex: elasticsearch do
  23. user = create(user_id)
  24. object = create(:knowledge_base_answer, trait, knowledge_base: knowledge_base)
  25. handle_elasticsearch(elasticsearch)
  26. expect(described_class.search({ query: object.translations.first.title, current_user: user })).to is_visible ? be_present : be_blank
  27. end
  28. end
  29. shared_examples 'verify given user' do |trait:, user_id:, is_visible:|
  30. include_examples 'verify given search backend', trait: trait, user_id: user_id, is_visible: is_visible, elasticsearch: true
  31. include_examples 'verify given search backend', trait: trait, user_id: user_id, is_visible: is_visible, elasticsearch: false
  32. end
  33. shared_examples 'verify given permissions' do |trait:, admin:, agent:, customer:|
  34. include_examples 'verify given user', trait: trait, user_id: :admin, is_visible: admin
  35. include_examples 'verify given user', trait: trait, user_id: :agent, is_visible: agent
  36. include_examples 'verify given user', trait: trait, user_id: :customer, is_visible: customer
  37. end
  38. include_examples 'verify given permissions', trait: :published, admin: true, agent: true, customer: false
  39. include_examples 'verify given permissions', trait: :internal, admin: true, agent: true, customer: false
  40. include_examples 'verify given permissions', trait: :draft, admin: true, agent: false, customer: false
  41. include_examples 'verify given permissions', trait: :archived, admin: true, agent: false, customer: false
  42. shared_examples 'verify multiple KBs support' do |elasticsearch:|
  43. it 'searches in multiple KBs', searchindex: elasticsearch do
  44. title = Faker::Appliance.equipment
  45. create_list(:knowledge_base_answer, 2, :published, translation_attributes: { title: title })
  46. handle_elasticsearch(elasticsearch)
  47. expect(described_class.search({ query: title, current_user: create(:admin) }).count).to be 2
  48. end
  49. end
  50. include_examples 'verify multiple KBs support', elasticsearch: true
  51. include_examples 'verify multiple KBs support', elasticsearch: false
  52. end
  53. end