answer_spec.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/concerns/checks_kb_client_notification_examples'
  4. require 'models/concerns/has_tags_examples'
  5. require 'models/contexts/factory_context'
  6. RSpec.describe KnowledgeBase::Answer, current_user_id: 1, type: :model do
  7. subject!(:kb_answer) { create(:knowledge_base_answer) }
  8. it_behaves_like 'HasTags'
  9. include_context 'factory'
  10. it_behaves_like 'ChecksKbClientNotification'
  11. it { is_expected.not_to validate_presence_of(:category_id) }
  12. it { is_expected.to belong_to(:category) }
  13. it { expect(kb_answer.attachments).to be_blank }
  14. context 'with attachment' do
  15. subject(:kb_answer) { create(:knowledge_base_answer, :with_attachment) }
  16. it { expect(kb_answer.attachments).to be_present }
  17. end
  18. describe '#assets', current_user_id: -> { user.id } do
  19. let(:assets) { another_category_answer && internal_answer.assets }
  20. let(:user) { create(:agent) }
  21. let(:another_category) { create(:knowledge_base_category, knowledge_base: knowledge_base) }
  22. let(:another_category_answer) { create(:knowledge_base_answer, :internal, category: another_category) }
  23. include_context 'basic Knowledge Base'
  24. context 'without permissions' do
  25. it { expect(assets).to include_assets_of internal_answer }
  26. it { expect(assets).to include_assets_of category }
  27. end
  28. context 'with readable another category' do
  29. before do
  30. KnowledgeBase::PermissionsUpdate
  31. .new(another_category)
  32. .update! user.roles.first => 'reader'
  33. end
  34. it { expect(assets).to include_assets_of internal_answer }
  35. it { expect(assets).to include_assets_of category }
  36. end
  37. context 'with hidden another category' do
  38. before do
  39. KnowledgeBase::PermissionsUpdate
  40. .new(another_category)
  41. .update! user.roles.first => 'none'
  42. end
  43. it { expect(assets).to include_assets_of internal_answer }
  44. it { expect(assets).to include_assets_of category }
  45. context 'with published answer' do
  46. let(:another_category_published_answer) { create(:knowledge_base_answer, :published, category: another_category) }
  47. before { another_category_published_answer }
  48. it { expect(assets).to include_assets_of internal_answer }
  49. it { expect(assets).to include_assets_of category }
  50. end
  51. end
  52. end
  53. describe '#sorted_by_published' do
  54. it 'sorts by publishing or update date, whichever is greater' do
  55. described_class.destroy_all
  56. answer1 = create(:knowledge_base_answer, published_at: 1.day.ago)
  57. answer1.update! updated_at: 1.day.ago
  58. answer2 = create(:knowledge_base_answer, published_at: 1.day.ago)
  59. answer2.update! updated_at: 1.hour.ago
  60. answer3 = create(:knowledge_base_answer, published_at: 1.minute.ago)
  61. answer3.update! updated_at: 1.day.ago
  62. expect(described_class.sorted_by_published).to contain_exactly(answer3, answer1, answer2)
  63. end
  64. end
  65. describe '#sorted_by_internally_published' do
  66. it 'sorts by internally publishing or update date, whichever is greater' do
  67. described_class.destroy_all
  68. answer1 = create(:knowledge_base_answer, internal_at: 2.days.ago, published_at: 1.day.ago)
  69. answer1.update! updated_at: 2.days.ago
  70. answer2 = create(:knowledge_base_answer, published_at: 1.day.ago)
  71. answer2.update! updated_at: 1.hour.ago
  72. answer3 = create(:knowledge_base_answer, published_at: 30.minutes.ago)
  73. answer3.update! updated_at: 1.day.ago
  74. answer4 = create(:knowledge_base_answer, internal_at: 1.minute.ago)
  75. answer4.update! updated_at: 1.day.ago
  76. answer5 = create(:knowledge_base_answer, published_at: 1.week.ago, internal_at: nil)
  77. answer5.update! updated_at: 1.week.ago
  78. _answer6 = create(:knowledge_base_answer, internal_at: nil, published_at: nil)
  79. expect(described_class.sorted_by_internally_published).to contain_exactly(answer4, answer3, answer1, answer2, answer5)
  80. end
  81. end
  82. end