category_spec.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://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, type: :model, current_user_id: 1 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 belong_to(:parent).optional }
  14. it { is_expected.to belong_to(:knowledge_base) }
  15. context 'in multilevel tree' do
  16. subject(:kb_category_with_tree) { create(:kb_category_with_tree) }
  17. let(:knowledge_base) { kb_category_with_tree.knowledge_base }
  18. let(:child_category) { kb_category_with_tree.children.order(position: :asc).last }
  19. let(:grandchild_category) { child_category.children.order(position: :asc).first }
  20. it 'tests to fetch all categories in KB' do
  21. expect(knowledge_base.categories.count).to eq(7)
  22. end
  23. it 'fetches root categories' do
  24. expect(knowledge_base.categories.root).to contain_exactly(kb_category_with_tree)
  25. end
  26. it 'fetches direct children' do
  27. expect(kb_category_with_tree.children.count).to eq 2
  28. end
  29. it 'fetches all children' do
  30. expect(kb_category_with_tree.self_with_children.count).to eq 7
  31. end
  32. it 'fetches all parents' do
  33. expect(grandchild_category.self_with_parents.count).to eq 3
  34. end
  35. it 'root category has no parent' do
  36. expect(kb_category_with_tree.parent).to be_blank
  37. end
  38. it 'children category has to have a parent' do
  39. expect(child_category.parent).to be_present
  40. end
  41. context 'when fetching self with children' do
  42. it 'root category has multiple layers children and matches all KB categories' do
  43. expect(kb_category_with_tree.self_with_children).to match_array(knowledge_base.categories)
  44. end
  45. it 'child category has multiple layers of children' do
  46. expect(child_category.self_with_children.count).to eq 5
  47. end
  48. it 'grandchild category has single layer of children' do
  49. expect(grandchild_category.self_with_children.count).to eq 3
  50. end
  51. end
  52. context 'when fetchching self with children ids' do
  53. it 'root category has multiple layers children ids' do
  54. expect(kb_category_with_tree.self_with_children_ids).to match_array(knowledge_base.category_ids)
  55. end
  56. it 'child category has with multiple layers of children ids' do
  57. expect(child_category.self_with_children_ids.count).to eq 5
  58. end
  59. it 'grandchild category has single layer of children ids count' do
  60. expect(grandchild_category.self_with_children_ids.count).to eq 3
  61. end
  62. it 'grandchild category children ids matches direct children ids' do
  63. expect(grandchild_category.self_with_children_ids).to match_array([grandchild_category.id] + grandchild_category.child_ids)
  64. end
  65. end
  66. context 'when checking if item is a parent of' do
  67. it 'root category is indirect (and direct) parent of child' do
  68. expect(child_category).to be_self_parent(kb_category_with_tree)
  69. end
  70. it 'root category is indirect parent of grandchild' do
  71. expect(grandchild_category).to be_self_parent(kb_category_with_tree)
  72. end
  73. it 'child category is not a parent of root category' do
  74. expect(kb_category_with_tree).not_to be_self_parent(grandchild_category)
  75. end
  76. end
  77. end
  78. describe '#public_content?' do
  79. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  80. it "returns #{is_visible} when contains #{state} answer" do
  81. object = create(:knowledge_base_category, "containing_#{state}")
  82. expect(object).send is_visible ? :to : :not_to, be_public_content(object.translations.first.kb_locale)
  83. end
  84. end
  85. include_examples 'verify visibility in given state', state: :published, is_visible: true
  86. include_examples 'verify visibility in given state', state: :internal, is_visible: false
  87. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  88. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  89. end
  90. describe '#internal_content?' do
  91. shared_examples 'verify visibility in given state' do |state:, is_visible:|
  92. it "returns #{is_visible} when contains #{state} answer" do
  93. object = create(:knowledge_base_category, "containing_#{state}")
  94. expect(object).send is_visible ? :to : :not_to, be_internal_content(object.translations.first.kb_locale)
  95. end
  96. end
  97. include_examples 'verify visibility in given state', state: :published, is_visible: true
  98. include_examples 'verify visibility in given state', state: :internal, is_visible: true
  99. include_examples 'verify visibility in given state', state: :draft, is_visible: false
  100. include_examples 'verify visibility in given state', state: :archived, is_visible: false
  101. end
  102. end