state_factory_spec.rb 5.4 KB

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