history_test.rb 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class HistoryTest < ActiveSupport::TestCase
  4. test 'ticket' do
  5. tests = [
  6. # test 1
  7. {
  8. :ticket_create => {
  9. :ticket => {
  10. :group_id => Group.lookup( :name => 'Users' ).id,
  11. :customer_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  12. :owner_id => User.lookup( :login => '-' ).id,
  13. :title => 'Unit Test 1 (äöüß)!',
  14. :ticket_state_id => Ticket::State.lookup( :name => 'new' ).id,
  15. :ticket_priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id,
  16. :updated_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  17. :created_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  18. },
  19. :article => {
  20. :updated_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  21. :created_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  22. :ticket_article_type_id => Ticket::Article::Type.lookup( :name => 'phone' ).id,
  23. :ticket_article_sender_id => Ticket::Article::Sender.lookup( :name => 'Customer' ).id,
  24. :from => 'Unit Test <unittest@example.com>',
  25. :body => 'Unit Test 123',
  26. :internal => false
  27. },
  28. },
  29. :ticket_update => {
  30. :ticket => {
  31. :title => 'Unit Test 1 (äöüß) - update!',
  32. :ticket_state_id => Ticket::State.lookup( :name => 'open' ).id,
  33. :ticket_priority_id => Ticket::Priority.lookup( :name => '1 low' ).id,
  34. },
  35. },
  36. :history_check => [
  37. {
  38. :history_object => 'Ticket',
  39. :history_type => 'created',
  40. },
  41. {
  42. :history_object => 'Ticket',
  43. :history_type => 'updated',
  44. :history_attribute => 'title',
  45. :value_from => 'Unit Test 1 (äöüß)!',
  46. :value_to => 'Unit Test 1 (äöüß) - update!',
  47. },
  48. {
  49. :history_object => 'Ticket',
  50. :history_type => 'updated',
  51. :history_attribute => 'ticket_state',
  52. :value_from => 'new',
  53. :value_to => 'open',
  54. :id_from => Ticket::State.lookup( :name => 'new' ).id,
  55. :id_to => Ticket::State.lookup( :name => 'open' ).id,
  56. },
  57. {
  58. :history_object => 'Ticket::Article',
  59. :history_type => 'created',
  60. },
  61. ]
  62. },
  63. # test 2
  64. {
  65. :ticket_create => {
  66. :ticket => {
  67. :group_id => Group.lookup( :name => 'Users' ).id,
  68. :customer_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  69. :owner_id => User.lookup( :login => '-' ).id,
  70. :title => 'Unit Test 2 (äöüß)!',
  71. :ticket_state_id => Ticket::State.lookup( :name => 'new' ).id,
  72. :ticket_priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id,
  73. :updated_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  74. :created_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  75. },
  76. :article => {
  77. :created_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  78. :updated_by_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  79. :ticket_article_type_id => Ticket::Article::Type.lookup(:name => 'phone' ).id,
  80. :ticket_article_sender_id => Ticket::Article::Sender.lookup(:name => 'Customer' ).id,
  81. :from => 'Unit Test <unittest@example.com>',
  82. :body => 'Unit Test 123',
  83. :internal => false
  84. },
  85. },
  86. :ticket_update => {
  87. :ticket => {
  88. :title => 'Unit Test 2 (äöüß) - update!',
  89. :ticket_state_id => Ticket::State.lookup( :name => 'open' ).id,
  90. :owner_id => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  91. },
  92. :article => {
  93. :from => 'Unit 2 Test 2 <unittest@example.com>',
  94. },
  95. },
  96. :history_check => [
  97. {
  98. :history_object => 'Ticket',
  99. :history_type => 'created',
  100. },
  101. {
  102. :history_object => 'Ticket',
  103. :history_type => 'updated',
  104. :history_attribute => 'title',
  105. :value_from => 'Unit Test 2 (äöüß)!',
  106. :value_to => 'Unit Test 2 (äöüß) - update!',
  107. },
  108. {
  109. :history_object => 'Ticket',
  110. :history_type => 'updated',
  111. :history_attribute => 'owner',
  112. :value_from => '-',
  113. :value_to => 'Nicole Braun',
  114. :id_from => User.lookup( :login => '-' ).id,
  115. :id_to => User.lookup( :login => 'nicole.braun@zammad.org' ).id,
  116. },
  117. {
  118. :history_object => 'Ticket::Article',
  119. :history_type => 'created',
  120. },
  121. {
  122. :history_object => 'Ticket::Article',
  123. :history_type => 'updated',
  124. :history_attribute => 'from',
  125. :value_from => 'Unit Test <unittest@example.com>',
  126. :value_to => 'Unit 2 Test 2 <unittest@example.com>',
  127. },
  128. ]
  129. },
  130. ]
  131. tickets = []
  132. tests.each { |test|
  133. ticket = nil
  134. article = nil
  135. # use transaction
  136. ActiveRecord::Base.transaction do
  137. ticket = Ticket.create( test[:ticket_create][:ticket])
  138. test[:ticket_create][:article][:ticket_id] = ticket.id
  139. article = Ticket::Article.create( test[:ticket_create][:article] )
  140. assert_equal( ticket.class.to_s, 'Ticket' )
  141. assert_equal( article.class.to_s, 'Ticket::Article' )
  142. # update ticket
  143. if test[:ticket_update][:ticket]
  144. ticket.update_attributes( test[:ticket_update][:ticket] )
  145. end
  146. if test[:ticket_update][:article]
  147. article.update_attributes( test[:ticket_update][:article] )
  148. end
  149. end
  150. # execute ticket events
  151. Observer::Ticket::Notification.transaction
  152. # remember ticket
  153. tickets.push ticket
  154. # get history
  155. history_list = History.list( 'Ticket', ticket.id, 'Ticket::Article' )
  156. # puts history_list.inspect
  157. test[:history_check].each { |check_item|
  158. # puts '+++++++++++'
  159. # puts check_item.inspect
  160. match = false
  161. history_list.each { |history_item|
  162. next if match
  163. # puts '--------'
  164. # puts history_item.inspect
  165. next if history_item.history_object.name != check_item[:history_object]
  166. next if history_item.history_type.name != check_item[:history_type]
  167. if check_item[:history_attribute]
  168. next if check_item[:history_attribute] != history_item.history_attribute.name
  169. end
  170. match = true
  171. if history_item.history_type.name == check_item[:history_type]
  172. assert( true, "History type #{history_item.history_type.name} found!")
  173. end
  174. if check_item[:history_attribute]
  175. assert_equal( check_item[:history_attribute], history_item.history_attribute.name, "check history attribute #{check_item[:history_attribute]}")
  176. end
  177. if check_item[:value_from]
  178. assert_equal( check_item[:value_from], history_item.value_from, "check history :value_from #{history_item.value_from} ok")
  179. end
  180. if check_item[:value_to]
  181. assert_equal( check_item[:value_to], history_item.value_to, "check history :value_to #{history_item.value_to} ok")
  182. end
  183. if check_item[:id_from]
  184. assert_equal( check_item[:id_from], history_item.id_from, "check history :id_from #{history_item.id_from} ok")
  185. end
  186. if check_item[:id_to]
  187. assert_equal( check_item[:id_to], history_item.id_to, "check history :id_to #{history_item.id_to} ok")
  188. end
  189. }
  190. assert( match, "history check not matched! #{check_item.inspect}")
  191. }
  192. }
  193. # delete tickets
  194. tickets.each { |ticket|
  195. ticket_id = ticket.id
  196. ticket.destroy
  197. found = Ticket.where( :id => ticket_id ).first
  198. assert( !found, "Ticket destroyed")
  199. }
  200. end
  201. end