macro_spec.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/concerns/has_collection_update_examples'
  4. require 'models/concerns/has_xss_sanitized_note_examples'
  5. require 'models/application_model/has_cache_examples'
  6. RSpec.describe Macro, type: :model do
  7. it_behaves_like 'HasCollectionUpdate', collection_factory: :macro
  8. it_behaves_like 'HasXssSanitizedNote', model_factory: :macro
  9. it_behaves_like 'Association clears cache', association: :groups
  10. describe 'validation' do
  11. it 'uses Validations::VerifyPerformRulesValidator' do
  12. expect(described_class).to have_validator(Validations::VerifyPerformRulesValidator).on(:perform)
  13. end
  14. end
  15. describe 'Instance methods:' do
  16. describe '#applicable_on?' do
  17. let(:ticket) { create(:ticket) }
  18. let(:ticket_a) { create(:ticket, group: group_a) }
  19. let(:ticket_b) { create(:ticket, group: group_b) }
  20. let(:ticket_c) { create(:ticket, group: group_c) }
  21. let(:group_a) { create(:group) }
  22. let(:group_b) { create(:group) }
  23. let(:group_c) { create(:group) }
  24. context 'when macro has no groups' do
  25. subject(:macro) { create(:macro, groups: []) }
  26. it 'return true for a single group' do
  27. expect(macro).to be_applicable_on(ticket)
  28. end
  29. it 'return true for multiple tickets' do
  30. expect(macro).to be_applicable_on([ticket, ticket_a, ticket_b])
  31. end
  32. end
  33. context 'when macro has a single group' do
  34. subject(:macro) { create(:macro, groups: [group_a]) }
  35. it 'returns true if macro group matches ticket' do
  36. expect(macro).to be_applicable_on(ticket_a)
  37. end
  38. it 'returns false if macro group does not match ticket' do
  39. expect(macro).not_to be_applicable_on(ticket_b)
  40. end
  41. it 'returns false if macro group match a ticket and not the other' do
  42. expect(macro).not_to be_applicable_on([ticket_a, ticket_b])
  43. end
  44. end
  45. context 'when macro has multiple groups' do
  46. subject(:macro) { create(:macro, groups: [group_a, group_c]) }
  47. it 'returns true if macro groups include ticket group' do
  48. expect(macro).to be_applicable_on(ticket_a)
  49. end
  50. it 'returns false if macro groups do not include ticket group' do
  51. expect(macro).not_to be_applicable_on(ticket_b)
  52. end
  53. it 'returns true if macro groups match tickets groups' do
  54. expect(macro).to be_applicable_on([ticket_a, ticket_c])
  55. end
  56. it 'returns false if macro groups does not match one of tickets group' do
  57. expect(macro).not_to be_applicable_on([ticket_a, ticket_b])
  58. end
  59. end
  60. end
  61. describe '#performable_on?' do
  62. let(:ticket) { create(:ticket) }
  63. let(:ticket_a) { create(:ticket, group: group_a) }
  64. let(:ticket_b) { create(:ticket, group: group_b) }
  65. let(:ticket_c) { create(:ticket, group: group_c) }
  66. let(:group_a) { create(:group) }
  67. let(:group_b) { create(:group) }
  68. let(:group_c) { create(:group) }
  69. context 'when macro has no groups' do
  70. subject(:macro) { create(:macro, groups: []) }
  71. it 'return true for a single group' do
  72. expect(macro).to be_performable_on(ticket, activator_type: nil)
  73. end
  74. context 'when macro is not active' do
  75. before { macro.update! active: false }
  76. it 'returns false if macro group does not match ticket' do
  77. expect(macro).not_to be_performable_on(ticket_b, activator_type: nil)
  78. end
  79. end
  80. end
  81. context 'when macro has a single group' do
  82. subject(:macro) { create(:macro, groups: [group_a]) }
  83. it 'returns true if macro group matches ticket' do
  84. expect(macro).to be_performable_on(ticket_a, activator_type: nil)
  85. end
  86. it 'returns false if macro group does not match ticket' do
  87. expect(macro).not_to be_performable_on(ticket_b, activator_type: nil)
  88. end
  89. end
  90. end
  91. end
  92. describe 'Class methods:' do
  93. describe '.available_in_groups' do
  94. let(:group) { create(:group) }
  95. let(:macro) { create(:macro, groups:) }
  96. before { macro }
  97. context 'when macro has a group' do
  98. let(:groups) { [group] }
  99. it 'returns macro if group matches' do
  100. expect(described_class.available_in_groups([group]))
  101. .to include(macro)
  102. end
  103. it 'returns macro if one of groups matches' do
  104. expect(described_class.available_in_groups([group, create(:group)]))
  105. .to include(macro)
  106. end
  107. it 'does not return macro if group does not match' do
  108. expect(described_class.available_in_groups([create(:group)]))
  109. .not_to include(macro)
  110. end
  111. context 'when macro is inactive' do
  112. before { macro.update!(active: false) }
  113. it 'does not return inactive macros' do
  114. expect(described_class.available_in_groups([group]))
  115. .not_to include(macro)
  116. end
  117. end
  118. end
  119. context 'when macro has multiple groups' do
  120. let(:groups) { [group, create(:group)] }
  121. it 'returns macro if one of given group matches' do
  122. expect(described_class.available_in_groups([group]))
  123. .to include(macro)
  124. end
  125. it 'returns macro if one of given groups matches' do
  126. expect(described_class.available_in_groups([group, create(:group)]))
  127. .to include(macro)
  128. end
  129. it 'does not return macro if no group matches' do
  130. expect(described_class.available_in_groups([create(:group)]))
  131. .not_to include(macro)
  132. end
  133. context 'when macro is inactive' do
  134. before { macro.update!(active: false) }
  135. it 'does not return inactive macros' do
  136. expect(described_class.available_in_groups([group]))
  137. .not_to include(macro)
  138. end
  139. end
  140. end
  141. context 'when macro has no group limitations' do
  142. let(:groups) { [] }
  143. it 'returns macro for any group' do
  144. expect(described_class.available_in_groups([group]))
  145. .to include(macro)
  146. end
  147. context 'when macro is inactive' do
  148. before { macro.update!(active: false) }
  149. it 'does not return inactive macros' do
  150. expect(described_class.available_in_groups([group]))
  151. .not_to include(macro)
  152. end
  153. end
  154. end
  155. end
  156. end
  157. end