history_test.rb 11 KB

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