history_test.rb 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. # encoding: utf-8
  2. require 'test_helper'
  3. class HistoryTest < ActiveSupport::TestCase
  4. current_user = User.lookup(email: '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. body: 'Unit Test 123 - 2',
  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: 'body',
  140. value_from: 'Unit Test 123',
  141. value_to: 'Unit Test 123 - 2',
  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 object transaction
  166. Observer::Transaction.commit
  167. # execute background jobs
  168. Scheduler.worker(true)
  169. # remember ticket
  170. tickets.push ticket
  171. # check history
  172. history_check(ticket.history_get, test[:history_check])
  173. }
  174. # delete tickets
  175. tickets.each { |ticket|
  176. ticket_id = ticket.id
  177. ticket.destroy
  178. found = Ticket.where(id: ticket_id).first
  179. assert_not(found, 'Ticket destroyed')
  180. }
  181. end
  182. test 'user' do
  183. tests = [
  184. # test 1
  185. {
  186. user_create: {
  187. user: {
  188. login: 'some_login_test',
  189. firstname: 'Bob',
  190. lastname: 'Smith',
  191. email: 'somebody@example.com',
  192. active: true,
  193. updated_by_id: current_user.id,
  194. created_by_id: current_user.id,
  195. },
  196. },
  197. user_update: {
  198. user: {
  199. firstname: 'Bob',
  200. lastname: 'Master',
  201. email: 'master@example.com',
  202. active: false,
  203. },
  204. },
  205. history_check: [
  206. {
  207. result: true,
  208. history_object: 'User',
  209. history_type: 'created',
  210. },
  211. {
  212. result: true,
  213. history_object: 'User',
  214. history_type: 'updated',
  215. history_attribute: 'lastname',
  216. value_from: 'Smith',
  217. value_to: 'Master',
  218. },
  219. {
  220. result: true,
  221. history_object: 'User',
  222. history_type: 'updated',
  223. history_attribute: 'email',
  224. value_from: 'somebody@example.com',
  225. value_to: 'master@example.com',
  226. },
  227. {
  228. result: true,
  229. history_object: 'User',
  230. history_type: 'updated',
  231. history_attribute: 'active',
  232. value_from: 'true',
  233. value_to: 'false',
  234. },
  235. ],
  236. },
  237. ]
  238. users = []
  239. tests.each { |test|
  240. user = nil
  241. # user transaction
  242. ActiveRecord::Base.transaction do
  243. user = User.create(test[:user_create][:user])
  244. assert_equal(user.class.to_s, 'User')
  245. # update user
  246. if test[:user_update][:user]
  247. test[:user_update][:user][:active] = false
  248. user.update_attributes(test[:user_update][:user])
  249. end
  250. end
  251. # remember user
  252. users.push user
  253. # check history
  254. history_check(user.history_get, test[:history_check])
  255. }
  256. # delete user
  257. users.each { |user|
  258. user_id = user.id
  259. user.destroy
  260. found = User.where(id: user_id).first
  261. assert_not(found, 'User destroyed')
  262. }
  263. end
  264. test 'organization' do
  265. tests = [
  266. # test 1
  267. {
  268. organization_create: {
  269. organization: {
  270. name: 'Org äöüß',
  271. note: 'some note',
  272. updated_by_id: current_user.id,
  273. created_by_id: current_user.id,
  274. },
  275. },
  276. organization_update: {
  277. organization: {
  278. name: 'Org 123',
  279. note: 'some note',
  280. },
  281. },
  282. history_check: [
  283. {
  284. result: true,
  285. history_object: 'Organization',
  286. history_type: 'created',
  287. },
  288. {
  289. result: true,
  290. history_object: 'Organization',
  291. history_type: 'updated',
  292. history_attribute: 'name',
  293. value_from: 'Org äöüß',
  294. value_to: 'Org 123',
  295. },
  296. ],
  297. },
  298. ]
  299. organizations = []
  300. tests.each { |test|
  301. organization = nil
  302. # user transaction
  303. ActiveRecord::Base.transaction do
  304. organization = Organization.create(test[:organization_create][:organization])
  305. assert_equal(organization.class.to_s, 'Organization')
  306. # update organization
  307. if test[:organization_update][:organization]
  308. organization.update_attributes(test[:organization_update][:organization])
  309. end
  310. end
  311. # remember user
  312. organizations.push organization
  313. # check history
  314. history_check(organization.history_get, test[:history_check])
  315. }
  316. # delete user
  317. organizations.each { |organization|
  318. organization_id = organization.id
  319. organization.destroy
  320. found = Organization.where(id: organization_id).first
  321. assert_not(found, 'Organization destroyed')
  322. }
  323. end
  324. def history_check(history_list, history_check)
  325. history_check.each { |check_item|
  326. match = false
  327. history_list.each { |history_item|
  328. next if match
  329. next if history_item['object'] != check_item[:history_object]
  330. next if history_item['type'] != check_item[:history_type]
  331. if check_item[:history_attribute]
  332. next if check_item[:history_attribute] != history_item['attribute']
  333. end
  334. match = true
  335. if history_item['type'] == check_item[:history_type]
  336. assert(true, "History type #{history_item['type']} found!")
  337. end
  338. if check_item[:history_attribute]
  339. assert_equal(check_item[:history_attribute], history_item['attribute'], "check history attribute #{check_item[:history_attribute]}")
  340. end
  341. if check_item[:value_from]
  342. assert_equal(check_item[:value_from], history_item['value_from'], "check history :value_from #{history_item['value_from']} ok")
  343. end
  344. if check_item[:value_to]
  345. assert_equal(check_item[:value_to], history_item['value_to'], "check history :value_to #{history_item['value_to']} ok")
  346. end
  347. if check_item[:id_from]
  348. assert_equal(check_item[:id_from], history_item['id_from'], "check history :id_from #{history_item['id_from']} ok")
  349. end
  350. if check_item[:id_to]
  351. assert_equal(check_item[:id_to], history_item['id_to'], "check history :id_to #{history_item['id_to']} ok")
  352. end
  353. }
  354. if check_item[:result]
  355. assert(match, "history check not matched! #{check_item.inspect}")
  356. else
  357. assert_not(match, "history check matched but should not! #{check_item.inspect}")
  358. end
  359. }
  360. end
  361. end