category_spec.rb 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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/contexts/factory_context'
  5. RSpec.describe KnowledgeBase::Category, current_user_id: 1, type: :model do
  6. subject(:kb_category) { create(:knowledge_base_category) }
  7. include_context 'factory'
  8. it_behaves_like 'ChecksKbClientNotification'
  9. it { is_expected.to validate_presence_of(:category_icon) }
  10. it { is_expected.not_to validate_presence_of(:parent_id) }
  11. it { is_expected.to have_many(:answers) }
  12. it { is_expected.to have_many(:children) }
  13. it { is_expected.to have_many(:permissions) }
  14. it { is_expected.to belong_to(:parent).optional }
  15. it { is_expected.to belong_to(:knowledge_base) }
  16. context 'in multilevel tree' do
  17. subject(:kb_category_with_tree) { create(:kb_category_with_tree) }
  18. let(:knowledge_base) { kb_category_with_tree.knowledge_base }
  19. let(:child_category) { kb_category_with_tree.children.sorted.last }
  20. let(:grandchild_category) { child_category.children.sorted.first }
  21. it 'tests to fetch all categories in KB' do
  22. expect(knowledge_base.categories.count).to eq(7)
  23. end
  24. it 'fetches root categories' do
  25. expect(knowledge_base.categories.root).to contain_exactly(kb_category_with_tree)
  26. end
  27. it 'fetches direct children' do
  28. expect(kb_category_with_tree.children.count).to eq 2
  29. end
  30. it 'fetches all children' do
  31. expect(kb_category_with_tree.self_with_children.count).to eq 7
  32. end
  33. it 'fetches all parents' do
  34. expect(grandchild_category.self_with_parents.count).to eq 3
  35. end
  36. it 'root category has no parent' do
  37. expect(kb_category_with_tree.parent).to be_blank
  38. end
  39. it 'children category has to have a parent' do
  40. expect(child_category.parent).to be_present
  41. end
  42. context 'when fetching self with children' do
  43. it 'root category has multiple layers children and matches all KB categories' do
  44. expect(kb_category_with_tree.self_with_children).to match_array(knowledge_base.categories)
  45. end
  46. it 'child category has multiple layers of children' do
  47. expect(child_category.self_with_children.count).to eq 5
  48. end
  49. it 'grandchild category has single layer of children' do
  50. expect(grandchild_category.self_with_children.count).to eq 3
  51. end
  52. end
  53. context 'when fetchching self with children ids' do
  54. it 'root category has multiple layers children ids' do
  55. expect(kb_category_with_tree.self_with_children_ids).to match_array(knowledge_base.category_ids)
  56. end
  57. it 'child category has with multiple layers of children ids' do
  58. expect(child_category.self_with_children_ids.count).to eq 5
  59. end
  60. it 'grandchild category has single layer of children ids count' do
  61. expect(grandchild_category.self_with_children_ids.count).to eq 3
  62. end
  63. it 'grandchild category children ids matches direct children ids' do
  64. expect(grandchild_category.self_with_children_ids).to match_array([grandchild_category.id] + grandchild_category.child_ids)
  65. end
  66. end
  67. context 'when checking if item is a parent of' do
  68. it 'root category is indirect (and direct) parent of child' do
  69. expect(child_category).to be_self_parent(kb_category_with_tree)
  70. end
  71. it 'root category is indirect parent of grandchild' do
  72. expect(grandchild_category).to be_self_parent(kb_category_with_tree)
  73. end
  74. it 'child category is not a parent of root category' do
  75. expect(kb_category_with_tree).not_to be_self_parent(grandchild_category)
  76. end
  77. end
  78. end
  79. describe '#public_content?' do
  80. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  81. it "returns #{is_visible} when contains #{state} answer" do
  82. object = create(:knowledge_base_category, "containing_#{state}")
  83. expect(object).send is_visible ? :to : :not_to, be_public_content(object.translations.first.kb_locale) # rubocop:disable RSpec/MissingExpectationTargetMethod
  84. end
  85. end
  86. include_examples 'verify visibility in given state', state: :published, is_visible: true
  87. include_examples 'verify visibility in given state', state: :internal, is_visible: false
  88. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  89. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  90. end
  91. describe '#internal_content?' do
  92. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  93. it "returns #{is_visible} when contains #{state} answer" do
  94. object = create(:knowledge_base_category, "containing_#{state}")
  95. expect(object).send is_visible ? :to : :not_to, be_internal_content(object.translations.first.kb_locale) # rubocop:disable RSpec/MissingExpectationTargetMethod
  96. end
  97. end
  98. include_examples 'verify visibility in given state', state: :published, is_visible: true
  99. include_examples 'verify visibility in given state', state: :internal, is_visible: true
  100. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  101. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  102. end
  103. describe '#assets', current_user_id: -> { user.id } do
  104. subject(:assets) { another_category_answer && internal_answer && category.assets }
  105. include_context 'basic Knowledge Base'
  106. let(:user) { create(:agent) }
  107. let(:another_category) { create(:knowledge_base_category, knowledge_base: knowledge_base) }
  108. let(:another_category_answer) { create(:knowledge_base_answer, :internal, category: another_category) }
  109. context 'without permissions' do
  110. it { expect(assets).to include_assets_of category }
  111. end
  112. context 'with readable another category' do
  113. before do
  114. KnowledgeBase::PermissionsUpdate
  115. .new(another_category)
  116. .update! user.roles.first => 'reader'
  117. end
  118. it { expect(assets).to include_assets_of category }
  119. end
  120. context 'with hidden another category' do
  121. before do
  122. KnowledgeBase::PermissionsUpdate
  123. .new(another_category)
  124. .update! user.roles.first => 'none'
  125. end
  126. it { expect(assets).to include_assets_of category }
  127. it { expect(assets).not_to include_assets_of another_category }
  128. context 'with published answer' do
  129. let(:another_category_published_answer) { create(:knowledge_base_answer, :published, category: another_category) }
  130. before { another_category_published_answer }
  131. it { expect(assets).to include_assets_of category }
  132. end
  133. end
  134. end
  135. end