state_factory_spec.rb 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'lib/import/transaction_factory_examples'
  4. RSpec.describe Import::OTRS::StateFactory do
  5. let(:state_backend_param) do
  6. states = %w[new open merged pending_reminder pending_auto_close_p pending_auto_close_n pending_auto_close_p closed_successful closed_unsuccessful removed]
  7. state_backend_param = []
  8. states.each do |state|
  9. state_backend_param.push(load_state_json(state))
  10. end
  11. state_backend_param
  12. end
  13. it_behaves_like 'Import::TransactionFactory'
  14. it 'creates a state backup in the pre_import_hook' do
  15. expect(described_class).to receive(:backup)
  16. described_class.pre_import_hook([])
  17. end
  18. def load_state_json(file)
  19. json_fixture("import/otrs/state/#{file}")
  20. end
  21. it 'updates ObjectManager Ticket state_id and pending_time filter' do
  22. ticket_state_id = ObjectManager::Attribute.get(
  23. object: 'Ticket',
  24. name: 'state_id',
  25. )
  26. ticket_pending_time = ObjectManager::Attribute.get(
  27. object: 'Ticket',
  28. name: 'pending_time',
  29. )
  30. expect do
  31. described_class.import(state_backend_param)
  32. # sync changes
  33. ticket_state_id.reload
  34. ticket_pending_time.reload
  35. end.to change {
  36. ticket_state_id.data_option
  37. }.and change {
  38. ticket_state_id.screens
  39. }
  40. end
  41. it "doesn't update ObjectManager Ticket state_id and pending_time filter in diff import" do
  42. ticket_state_id = ObjectManager::Attribute.get(
  43. object: 'Ticket',
  44. name: 'state_id',
  45. )
  46. ticket_pending_time = ObjectManager::Attribute.get(
  47. object: 'Ticket',
  48. name: 'pending_time',
  49. )
  50. allow(Import::OTRS).to receive(:diff?).and_return(true)
  51. expect do
  52. described_class.update_attribute_settings
  53. # sync changes
  54. ticket_state_id.reload
  55. ticket_pending_time.reload
  56. end.to not_change {
  57. ticket_state_id.data_option
  58. }.and not_change {
  59. ticket_state_id.screens
  60. }.and not_change {
  61. ticket_pending_time.data_option
  62. }
  63. end
  64. it 'sets default create and update State' do
  65. state = Ticket::State.first
  66. state.default_create = false
  67. state.default_follow_up = false
  68. state.callback_loop = true
  69. state.save
  70. allow(Import::OTRS::SysConfigFactory).to receive(:postmaster_default_lookup).with(:state_default_create).and_return(state.name)
  71. allow(Import::OTRS::SysConfigFactory).to receive(:postmaster_default_lookup).with(:state_default_follow_up).and_return(state.name)
  72. described_class.update_attribute
  73. state.reload
  74. expect(state.default_create).to be true
  75. expect(state.default_follow_up).to be true
  76. end
  77. it "doesn't set default create and update State in diff import" do
  78. state = Ticket::State.first
  79. state.default_create = false
  80. state.default_follow_up = false
  81. state.callback_loop = true
  82. state.save
  83. allow(Import::OTRS).to receive(:diff?).and_return(true)
  84. described_class.update_attribute_settings
  85. state.reload
  86. expect(state.default_create).to be false
  87. expect(state.default_follow_up).to be false
  88. end
  89. it 'sets next state for pending auto states' do
  90. described_class.import(state_backend_param)
  91. state_pending_auto_close_n = Ticket::State.find_by(name: 'pending auto close-')
  92. state_pending_auto_close_p = Ticket::State.find_by(name: 'pending auto close+')
  93. expect(state_pending_auto_close_n.next_state_id).to eq(Ticket::State.find_by(name: 'closed unsuccessful').id)
  94. expect(state_pending_auto_close_p.next_state_id).to eq(Ticket::State.find_by(name: 'closed successful').id)
  95. end
  96. context 'when some default otrs states not exists' do
  97. let(:state_backend_param) do
  98. states = %w[new open merged pending_reminder pending_auto_close_p pending_auto_close_n pending_auto_close_p removed closed_other]
  99. state_backend_param = []
  100. states.each do |state|
  101. state_backend_param.push(load_state_json(state))
  102. end
  103. state_backend_param
  104. end
  105. it 'use fallback for next state for pending auto states' do
  106. described_class.import(state_backend_param)
  107. state_pending_auto_close_n = Ticket::State.find_by(name: 'pending auto close-')
  108. state_pending_auto_close_p = Ticket::State.find_by(name: 'pending auto close+')
  109. expect(state_pending_auto_close_n.next_state_id).to eq(Ticket::State.find_by(name: 'closed other').id)
  110. expect(state_pending_auto_close_p.next_state_id).to eq(Ticket::State.find_by(name: 'closed other').id)
  111. end
  112. end
  113. context 'changing Ticket::State IDs' do
  114. it 'updates Overviews' do
  115. name = 'My Pending Reached Tickets'
  116. overview = Overview.find_by(name: name)
  117. expect do
  118. described_class.import(state_backend_param)
  119. overview = Overview.find_by(name: name)
  120. end.to change {
  121. overview.id
  122. }.and change {
  123. overview.condition['ticket.state_id'][:value]
  124. }
  125. end
  126. it 'updates Macros' do
  127. name = 'Close & Tag as Spam'
  128. macro = Macro.find_by(name: name)
  129. expect do
  130. described_class.import(state_backend_param)
  131. macro = Macro.find_by(name: name)
  132. end.to change {
  133. macro.id
  134. }.and change {
  135. macro.perform['ticket.state_id'][:value]
  136. }
  137. end
  138. it 'updates Triggers' do
  139. name = 'auto reply (on new tickets)'
  140. trigger = Trigger.find_by(name: name)
  141. expect do
  142. described_class.import(state_backend_param)
  143. trigger = Trigger.find_by(name: name)
  144. end.to change {
  145. trigger.id
  146. }.and change {
  147. trigger.condition['ticket.state_id'][:value]
  148. }
  149. end
  150. end
  151. end