history_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class HistoryTest < ActiveSupport::TestCase
  4. current_user = User.lookup( :login => 'nicole.braun@zammad.org' )
  5. test 'ticket' do
  6. tests = [
  7. # test 1
  8. {
  9. :ticket_create => {
  10. :ticket => {
  11. :group_id => Group.lookup( :name => 'Users' ).id,
  12. :customer_id => current_user.id,
  13. :owner_id => User.lookup( :login => '-' ).id,
  14. :title => 'Unit Test 1 (äöüß)!',
  15. :state_id => Ticket::State.lookup( :name => 'new' ).id,
  16. :priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id,
  17. :updated_by_id => current_user.id,
  18. :created_by_id => current_user.id,
  19. },
  20. :article => {
  21. :updated_by_id => current_user.id,
  22. :created_by_id => current_user.id,
  23. :type_id => Ticket::Article::Type.lookup( :name => 'phone' ).id,
  24. :sender_id => Ticket::Article::Sender.lookup( :name => 'Customer' ).id,
  25. :from => 'Unit Test <unittest@example.com>',
  26. :body => 'Unit Test 123',
  27. :internal => false
  28. },
  29. },
  30. :ticket_update => {
  31. :ticket => {
  32. :title => 'Unit Test 1 (äöüß) - update!',
  33. :state_id => Ticket::State.lookup( :name => 'open' ).id,
  34. :priority_id => Ticket::Priority.lookup( :name => '1 low' ).id,
  35. },
  36. },
  37. :history_check => [
  38. {
  39. :result => true,
  40. :history_object => 'Ticket',
  41. :history_type => 'created',
  42. },
  43. {
  44. :result => true,
  45. :history_object => 'Ticket',
  46. :history_type => 'updated',
  47. :history_attribute => 'title',
  48. :value_from => 'Unit Test 1 (äöüß)!',
  49. :value_to => 'Unit Test 1 (äöüß) - update!',
  50. },
  51. {
  52. :result => true,
  53. :history_object => 'Ticket',
  54. :history_type => 'updated',
  55. :history_attribute => 'state',
  56. :value_from => 'new',
  57. :value_to => 'open',
  58. :id_from => Ticket::State.lookup( :name => 'new' ).id,
  59. :id_to => Ticket::State.lookup( :name => 'open' ).id,
  60. },
  61. {
  62. :result => true,
  63. :history_object => 'Ticket::Article',
  64. :history_type => 'created',
  65. },
  66. {
  67. :result => false,
  68. :history_object => 'User',
  69. :history_type => 'updated',
  70. },
  71. ]
  72. },
  73. # test 2
  74. {
  75. :ticket_create => {
  76. :ticket => {
  77. :group_id => Group.lookup( :name => 'Users' ).id,
  78. :customer_id => current_user.id,
  79. :owner_id => User.lookup( :login => '-' ).id,
  80. :title => 'Unit Test 2 (äöüß)!',
  81. :state_id => Ticket::State.lookup( :name => 'new' ).id,
  82. :priority_id => Ticket::Priority.lookup( :name => '2 normal' ).id,
  83. :updated_by_id => current_user.id,
  84. :created_by_id => current_user.id,
  85. },
  86. :article => {
  87. :created_by_id => current_user.id,
  88. :updated_by_id => current_user.id,
  89. :type_id => Ticket::Article::Type.lookup(:name => 'phone' ).id,
  90. :sender_id => Ticket::Article::Sender.lookup(:name => 'Customer' ).id,
  91. :from => 'Unit Test <unittest@example.com>',
  92. :body => 'Unit Test 123',
  93. :internal => false
  94. },
  95. },
  96. :ticket_update => {
  97. :ticket => {
  98. :title => 'Unit Test 2 (äöüß) - update!',
  99. :state_id => Ticket::State.lookup( :name => 'open' ).id,
  100. :owner_id => current_user.id,
  101. },
  102. :article => {
  103. :from => 'Unit 2 Test 2 <unittest@example.com>',
  104. },
  105. },
  106. :history_check => [
  107. {
  108. :result => true,
  109. :history_object => 'Ticket',
  110. :history_type => 'created',
  111. },
  112. {
  113. :result => true,
  114. :history_object => 'Ticket',
  115. :history_type => 'updated',
  116. :history_attribute => 'title',
  117. :value_from => 'Unit Test 2 (äöüß)!',
  118. :value_to => 'Unit Test 2 (äöüß) - update!',
  119. },
  120. {
  121. :result => true,
  122. :history_object => 'Ticket',
  123. :history_type => 'updated',
  124. :history_attribute => 'owner',
  125. :value_from => '-',
  126. :value_to => 'Nicole Braun',
  127. :id_from => User.lookup( :login => '-' ).id,
  128. :id_to => current_user.id,
  129. },
  130. {
  131. :result => true,
  132. :history_object => 'Ticket::Article',
  133. :history_type => 'created',
  134. },
  135. {
  136. :result => true,
  137. :history_object => 'Ticket::Article',
  138. :history_type => 'updated',
  139. :history_attribute => 'from',
  140. :value_from => 'Unit Test <unittest@example.com>',
  141. :value_to => 'Unit 2 Test 2 <unittest@example.com>',
  142. },
  143. ]
  144. },
  145. ]
  146. tickets = []
  147. tests.each { |test|
  148. ticket = nil
  149. article = nil
  150. # use transaction
  151. ActiveRecord::Base.transaction do
  152. ticket = Ticket.create( test[:ticket_create][:ticket])
  153. test[:ticket_create][:article][:ticket_id] = ticket.id
  154. article = Ticket::Article.create( test[:ticket_create][:article] )
  155. assert_equal( ticket.class.to_s, 'Ticket' )
  156. assert_equal( article.class.to_s, 'Ticket::Article' )
  157. # update ticket
  158. if test[:ticket_update][:ticket]
  159. ticket.update_attributes( test[:ticket_update][:ticket] )
  160. end
  161. if test[:ticket_update][:article]
  162. article.update_attributes( test[:ticket_update][:article] )
  163. end
  164. end
  165. # execute ticket events
  166. Observer::Ticket::Notification.transaction
  167. # remember ticket
  168. tickets.push ticket
  169. # check history
  170. history_check( ticket.history_get, test[:history_check] )
  171. }
  172. # delete tickets
  173. tickets.each { |ticket|
  174. ticket_id = ticket.id
  175. ticket.destroy
  176. found = Ticket.where( :id => ticket_id ).first
  177. assert( !found, "Ticket destroyed")
  178. }
  179. end
  180. test 'user' do
  181. tests = [
  182. # test 1
  183. {
  184. :user_create => {
  185. :user => {
  186. :login => 'some_login_test',
  187. :firstname => 'Bob',
  188. :lastname => 'Smith',
  189. :email => 'somebody@example.com',
  190. :active => true,
  191. :updated_by_id => current_user.id,
  192. :created_by_id => current_user.id,
  193. },
  194. },
  195. :user_update => {
  196. :user => {
  197. :firstname => 'Bob',
  198. :lastname => 'Master',
  199. :email => 'master@example.com', },
  200. :active => false,
  201. },
  202. :history_check => [
  203. {
  204. :result => true,
  205. :history_object => 'User',
  206. :history_type => 'created',
  207. },
  208. {
  209. :result => true,
  210. :history_object => 'User',
  211. :history_type => 'updated',
  212. :history_attribute => 'lastname',
  213. :value_from => 'Smith',
  214. :value_to => 'Master',
  215. },
  216. {
  217. :result => true,
  218. :history_object => 'User',
  219. :history_type => 'updated',
  220. :history_attribute => 'email',
  221. :value_from => 'somebody@example.com',
  222. :value_to => 'master@example.com',
  223. },
  224. {
  225. :result => true,
  226. :history_object => 'User',
  227. :history_type => 'updated',
  228. :history_attribute => 'active',
  229. :value_from => 'true',
  230. :value_to => 'false',
  231. },
  232. ],
  233. },
  234. ]
  235. users = []
  236. tests.each { |test|
  237. user = nil
  238. # user transaction
  239. ActiveRecord::Base.transaction do
  240. user = User.create( test[:user_create][:user])
  241. assert_equal( user.class.to_s, 'User' )
  242. # update user
  243. if test[:user_update][:user]
  244. test[:user_update][:user][:active] = false
  245. user.update_attributes( test[:user_update][:user] )
  246. end
  247. end
  248. # remember user
  249. users.push user
  250. # check history
  251. history_check( user.history_get, test[:history_check] )
  252. }
  253. # delete user
  254. users.each { |user|
  255. user_id = user.id
  256. user.destroy
  257. found = User.where( :id => user_id ).first
  258. assert( !found, "User destroyed")
  259. }
  260. end
  261. test 'organization' do
  262. tests = [
  263. # test 1
  264. {
  265. :organization_create => {
  266. :organization => {
  267. :name => 'Org äöüß',
  268. :note => 'some note',
  269. :updated_by_id => current_user.id,
  270. :created_by_id => current_user.id,
  271. },
  272. },
  273. :organization_update => {
  274. :organization => {
  275. :name => 'Org 123',
  276. :note => 'some note',
  277. },
  278. },
  279. :history_check => [
  280. {
  281. :result => true,
  282. :history_object => 'Organization',
  283. :history_type => 'created',
  284. },
  285. {
  286. :result => true,
  287. :history_object => 'Organization',
  288. :history_type => 'updated',
  289. :history_attribute => 'name',
  290. :value_from => 'Org äöüß',
  291. :value_to => 'Org 123',
  292. },
  293. ],
  294. },
  295. ]
  296. organizations = []
  297. tests.each { |test|
  298. organization = nil
  299. # user transaction
  300. ActiveRecord::Base.transaction do
  301. organization = Organization.create( test[:organization_create][:organization])
  302. assert_equal( organization.class.to_s, 'Organization' )
  303. # update organization
  304. if test[:organization_update][:organization]
  305. organization.update_attributes( test[:organization_update][:organization] )
  306. end
  307. end
  308. # remember user
  309. organizations.push organization
  310. # check history
  311. history_check( organization.history_get, test[:history_check] )
  312. }
  313. # delete user
  314. organizations.each { |organization|
  315. organization_id = organization.id
  316. organization.destroy
  317. found = Organization.where( :id => organization_id ).first
  318. assert( !found, "Organization destroyed")
  319. }
  320. end
  321. def history_check( history_list, history_check )
  322. # puts history_list.inspect
  323. history_check.each { |check_item|
  324. # puts '+++++++++++'
  325. # puts check_item.inspect
  326. match = false
  327. history_list.each { |history_item|
  328. next if match
  329. # puts '--------'
  330. # puts history_item.inspect
  331. # puts history_item.history_object.name
  332. next if history_item['object'] != check_item[:history_object]
  333. next if history_item['type'] != check_item[:history_type]
  334. if check_item[:history_attribute]
  335. next if check_item[:history_attribute] != history_item['attribute']
  336. end
  337. match = true
  338. if history_item['type'] == check_item[:history_type]
  339. assert( true, "History type #{history_item['type']} found!")
  340. end
  341. if check_item[:history_attribute]
  342. assert_equal( check_item[:history_attribute], history_item['attribute'], "check history attribute #{check_item[:history_attribute]}")
  343. end
  344. if check_item[:value_from]
  345. assert_equal( check_item[:value_from], history_item['value_from'], "check history :value_from #{history_item['value_from']} ok")
  346. end
  347. if check_item[:value_to]
  348. assert_equal( check_item[:value_to], history_item['value_to'], "check history :value_to #{history_item['value_to']} ok")
  349. end
  350. if check_item[:id_from]
  351. assert_equal( check_item[:id_from], history_item['id_from'], "check history :id_from #{history_item['id_from']} ok")
  352. end
  353. if check_item[:id_to]
  354. assert_equal( check_item[:id_to], history_item['id_to'], "check history :id_to #{history_item['id_to']} ok")
  355. end
  356. }
  357. if check_item[:result]
  358. assert( match, "history check not matched! #{check_item.inspect}")
  359. else
  360. assert( !match, "history check matched but should not! #{check_item.inspect}")
  361. end
  362. }
  363. end
  364. end