ticket_spec.rb 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. require 'rails_helper'
  2. RSpec.describe Ticket do
  3. describe '#merge_to' do
  4. it 'reassigns all links to the target ticket after merge' do
  5. source_ticket = create(:ticket)
  6. target_ticket = create(:ticket)
  7. important_ticket1 = create(:ticket)
  8. important_ticket2 = create(:ticket)
  9. important_ticket3 = create(:ticket)
  10. create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket1.id)
  11. create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket2.id)
  12. create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket3.id)
  13. source_ticket.merge_to(
  14. ticket_id: target_ticket.id,
  15. user_id: 1,
  16. )
  17. links = Link.list(
  18. link_object: 'Ticket',
  19. link_object_value: target_ticket.id,
  20. )
  21. expected_ticket_ids = [source_ticket.id, important_ticket1.id, important_ticket2.id, important_ticket3.id ]
  22. check_ticket_ids = links.collect { |link| link['link_object_value'] }
  23. expect(check_ticket_ids).to match_array(expected_ticket_ids)
  24. end
  25. it 'prevents cross merging tickets' do
  26. source_ticket = create(:ticket)
  27. target_ticket = create(:ticket)
  28. result = source_ticket.merge_to(
  29. ticket_id: target_ticket.id,
  30. user_id: 1,
  31. )
  32. expect(result).to be(true)
  33. expect do
  34. result = target_ticket.merge_to(
  35. ticket_id: source_ticket.id,
  36. user_id: 1,
  37. )
  38. end.to raise_error('ticket already merged, no merge into merged ticket possible')
  39. end
  40. it 'prevents merging ticket in it self' do
  41. source_ticket = create(:ticket)
  42. expect do
  43. result = source_ticket.merge_to(
  44. ticket_id: source_ticket.id,
  45. user_id: 1,
  46. )
  47. end.to raise_error('Can\'t merge ticket with it self!')
  48. end
  49. end
  50. describe '#destroy' do
  51. it 'deletes all related objects before destroy' do
  52. ApplicationHandleInfo.current = 'application_server'
  53. source_ticket = create(:ticket)
  54. # create some links
  55. important_ticket1 = create(:ticket)
  56. important_ticket2 = create(:ticket)
  57. important_ticket3 = create(:ticket)
  58. # create some articles
  59. create(:ticket_article, ticket_id: source_ticket.id)
  60. create(:ticket_article, ticket_id: source_ticket.id)
  61. create(:ticket_article, ticket_id: source_ticket.id)
  62. create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket1.id)
  63. create(:link, link_object_source_value: important_ticket2.id, link_object_target_value: source_ticket.id)
  64. create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket3.id)
  65. create(:online_notification, o_id: source_ticket.id)
  66. create(:tag, o_id: source_ticket.id)
  67. Observer::Transaction.commit
  68. Scheduler.worker(true)
  69. # get before destroy
  70. activities = ActivityStream.where(
  71. activity_stream_object_id: ObjectLookup.by_name('Ticket'),
  72. o_id: source_ticket.id,
  73. )
  74. links = Link.list(
  75. link_object: 'Ticket',
  76. link_object_value: source_ticket.id
  77. )
  78. articles = Ticket::Article.where(ticket_id: source_ticket.id)
  79. history = History.list('Ticket', source_ticket.id, nil, true)
  80. karma_log = Karma::ActivityLog.where(
  81. object_lookup_id: ObjectLookup.by_name('Ticket'),
  82. o_id: source_ticket.id,
  83. )
  84. online_notifications = OnlineNotification.where(
  85. object_lookup_id: ObjectLookup.by_name('Ticket'),
  86. o_id: source_ticket.id,
  87. )
  88. recent_views = OnlineNotification.where(
  89. object_lookup_id: ObjectLookup.by_name('Ticket'),
  90. o_id: source_ticket.id,
  91. )
  92. tags = Tag.tag_list(
  93. object: 'Ticket',
  94. o_id: source_ticket.id,
  95. )
  96. # check before destroy
  97. expect(activities.count).to be >= 0
  98. expect(links.count).to be >= 0
  99. expect(articles.count).to be >= 0
  100. expect(history[:list].count).to be >= 0
  101. expect(karma_log.count).to be >= 0
  102. expect(online_notifications.count).to be >= 0
  103. expect(recent_views.count).to be >= 0
  104. expect(tags.count).to be >= 0
  105. # destroy ticket
  106. source_ticket.destroy
  107. # get after destroy
  108. activities = ActivityStream.where(
  109. activity_stream_object_id: ObjectLookup.by_name('Ticket'),
  110. o_id: source_ticket.id,
  111. )
  112. links = Link.list(
  113. link_object: 'Ticket',
  114. link_object_value: source_ticket.id
  115. )
  116. articles = Ticket::Article.where(ticket_id: source_ticket.id)
  117. history = History.list('Ticket', source_ticket.id, nil, true)
  118. karma_log = Karma::ActivityLog.where(
  119. object_lookup_id: ObjectLookup.by_name('Ticket'),
  120. o_id: source_ticket.id,
  121. )
  122. online_notifications = OnlineNotification.where(
  123. object_lookup_id: ObjectLookup.by_name('Ticket'),
  124. o_id: source_ticket.id,
  125. )
  126. recent_views = OnlineNotification.where(
  127. object_lookup_id: ObjectLookup.by_name('Ticket'),
  128. o_id: source_ticket.id,
  129. )
  130. tags = Tag.tag_list(
  131. object: 'Ticket',
  132. o_id: source_ticket.id,
  133. )
  134. # check after destroy
  135. expect(activities.count).to be == 0
  136. expect(links.count).to be == 0
  137. expect(articles.count).to be == 0
  138. expect(history[:list].count).to be == 0
  139. expect(karma_log.count).to be == 0
  140. expect(online_notifications.count).to be == 0
  141. expect(recent_views.count).to be == 0
  142. expect(tags.count).to be == 0
  143. end
  144. end
  145. describe '#perform_changes' do
  146. it 'performes a ticket state change on a ticket' do
  147. source_ticket = create(:ticket)
  148. changes = {
  149. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s },
  150. }
  151. source_ticket.perform_changes(changes, 'trigger', source_ticket, User.find(1))
  152. source_ticket.reload
  153. expect(source_ticket.state.name).to eq('closed')
  154. end
  155. it 'performes a ticket deletion on a ticket' do
  156. source_ticket = create(:ticket)
  157. changes = {
  158. 'ticket.state_id' => { 'value' => Ticket::State.lookup(name: 'closed').id.to_s },
  159. 'ticket.action' => { 'value' => 'delete' },
  160. }
  161. source_ticket.perform_changes(changes, 'trigger', source_ticket, User.find(1))
  162. ticket_with_source_ids = Ticket.where(id: source_ticket.id)
  163. expect(ticket_with_source_ids).to match_array([])
  164. end
  165. end
  166. context 'callbacks' do
  167. describe '#reset_pending_time' do
  168. it 'resets the pending time on state change' do
  169. ticket = create(:ticket,
  170. state: Ticket::State.lookup(name: 'pending reminder'),
  171. pending_time: Time.zone.now + 2.days)
  172. expect(ticket.pending_time).not_to be nil
  173. ticket.update!(state: Ticket::State.lookup(name: 'open'))
  174. expect(ticket.pending_time).to be nil
  175. end
  176. it 'lets handle ActiveRecord nil as new value' do
  177. ticket = create(:ticket)
  178. expect do
  179. ticket.update!(state: nil)
  180. end.to raise_error(ActiveRecord::StatementInvalid)
  181. end
  182. end
  183. end
  184. end