ticket_notification_test.rb 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. require 'test_helper'
  2. class TicketNotificationTest < ActiveSupport::TestCase
  3. setup do
  4. Trigger.create_or_update(
  5. name: 'auto reply - new ticket',
  6. condition: {
  7. 'ticket.action' => {
  8. 'operator' => 'is',
  9. 'value' => 'create',
  10. },
  11. 'ticket.state_id' => {
  12. 'operator' => 'is not',
  13. 'value' => Ticket::State.lookup(name: 'closed').id,
  14. },
  15. 'article.type_id' => {
  16. 'operator' => 'is',
  17. 'value' => [
  18. Ticket::Article::Type.lookup(name: 'email').id,
  19. Ticket::Article::Type.lookup(name: 'phone').id,
  20. Ticket::Article::Type.lookup(name: 'web').id,
  21. ],
  22. },
  23. },
  24. perform: {
  25. 'notification.email' => {
  26. # rubocop:disable Lint/InterpolationCheck
  27. 'body' => '<p>Your request (Ticket##{ticket.number}) has been received and will be reviewed by our support staff.<p>
  28. <br/>
  29. <p>To provide additional information, please reply to this email or click on the following link:
  30. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  31. </p>
  32. <br/>
  33. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  34. 'recipient' => 'ticket_customer',
  35. 'subject' => 'Thanks for your inquiry (#{ticket.title})',
  36. # rubocop:enable Lint/InterpolationCheck
  37. },
  38. },
  39. disable_notification: true,
  40. active: true,
  41. created_by_id: 1,
  42. updated_by_id: 1,
  43. )
  44. # create @agent1 & @agent2
  45. Group.create_or_update(
  46. name: 'TicketNotificationTest',
  47. updated_by_id: 1,
  48. created_by_id: 1
  49. )
  50. groups = Group.where(name: 'TicketNotificationTest')
  51. roles = Role.where(name: 'Agent')
  52. @agent1 = User.create_or_update(
  53. login: 'ticket-notification-agent1@example.com',
  54. firstname: 'Notification',
  55. lastname: 'Agent1',
  56. email: 'ticket-notification-agent1@example.com',
  57. password: 'agentpw',
  58. out_of_office: false,
  59. active: true,
  60. roles: roles,
  61. groups: groups,
  62. preferences: {
  63. locale: 'de-de',
  64. },
  65. updated_by_id: 1,
  66. created_by_id: 1,
  67. )
  68. @agent2 = User.create_or_update(
  69. login: 'ticket-notification-agent2@example.com',
  70. firstname: 'Notification',
  71. lastname: 'Agent2',
  72. email: 'ticket-notification-agent2@example.com',
  73. password: 'agentpw',
  74. out_of_office: false,
  75. active: true,
  76. roles: roles,
  77. groups: groups,
  78. preferences: {
  79. locale: 'en-ca',
  80. },
  81. updated_by_id: 1,
  82. created_by_id: 1,
  83. )
  84. @agent3 = User.create_or_update(
  85. login: 'ticket-notification-agent3@example.com',
  86. firstname: 'Notification',
  87. lastname: 'Agent3',
  88. email: 'ticket-notification-agent3@example.com',
  89. password: 'agentpw',
  90. out_of_office: false,
  91. active: true,
  92. roles: roles,
  93. groups: groups,
  94. preferences: {
  95. locale: 'de-de',
  96. },
  97. updated_by_id: 1,
  98. created_by_id: 1,
  99. )
  100. @agent4 = User.create_or_update(
  101. login: 'ticket-notification-agent4@example.com',
  102. firstname: 'Notification',
  103. lastname: 'Agent4',
  104. email: 'ticket-notification-agent4@example.com',
  105. password: 'agentpw',
  106. out_of_office: false,
  107. active: true,
  108. roles: roles,
  109. groups: groups,
  110. preferences: {
  111. locale: 'de-de',
  112. },
  113. updated_by_id: 1,
  114. created_by_id: 1,
  115. )
  116. Group.create_if_not_exists(
  117. name: 'WithoutAccess',
  118. note: 'Test for notification check.',
  119. updated_by_id: 1,
  120. created_by_id: 1
  121. )
  122. # create @customer
  123. roles = Role.where(name: 'Customer')
  124. @customer = User.create_or_update(
  125. login: 'ticket-notification-customer@example.com',
  126. firstname: 'Notification',
  127. lastname: 'Customer',
  128. email: 'ticket-notification-customer@example.com',
  129. password: 'agentpw',
  130. active: true,
  131. roles: roles,
  132. groups: groups,
  133. updated_by_id: 1,
  134. created_by_id: 1,
  135. )
  136. end
  137. test 'ticket notification - to all agents / to explicit agents' do
  138. # create ticket in group
  139. ApplicationHandleInfo.current = 'scheduler.postmaster'
  140. ticket1 = Ticket.create!(
  141. title: 'some notification test 1',
  142. group: Group.lookup(name: 'TicketNotificationTest'),
  143. customer: @customer,
  144. state: Ticket::State.lookup(name: 'new'),
  145. priority: Ticket::Priority.lookup(name: '2 normal'),
  146. updated_by_id: @agent1.id,
  147. created_by_id: @agent1.id,
  148. )
  149. Ticket::Article.create!(
  150. ticket_id: ticket1.id,
  151. from: 'some_sender@example.com',
  152. to: 'some_recipient@example.com',
  153. subject: 'some subject',
  154. message_id: 'some@id',
  155. body: 'some message',
  156. internal: false,
  157. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  158. type: Ticket::Article::Type.where(name: 'email').first,
  159. updated_by_id: @agent1.id,
  160. created_by_id: @agent1.id,
  161. )
  162. assert(ticket1)
  163. # execute object transaction
  164. Observer::Transaction.commit
  165. Scheduler.worker(true)
  166. # verify notifications to @agent1 + @agent2
  167. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  168. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  169. # create ticket in group
  170. ApplicationHandleInfo.current = 'application_server'
  171. ticket1 = Ticket.create!(
  172. title: 'some notification test 2',
  173. group: Group.lookup(name: 'TicketNotificationTest'),
  174. customer: @customer,
  175. state: Ticket::State.lookup(name: 'new'),
  176. priority: Ticket::Priority.lookup(name: '2 normal'),
  177. updated_by_id: @agent1.id,
  178. created_by_id: @agent1.id,
  179. )
  180. Ticket::Article.create!(
  181. ticket_id: ticket1.id,
  182. from: 'some_sender@example.com',
  183. to: 'some_recipient@example.com',
  184. subject: 'some subject',
  185. message_id: 'some@id',
  186. body: 'some message',
  187. internal: false,
  188. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  189. type: Ticket::Article::Type.where(name: 'email').first,
  190. updated_by_id: @agent1.id,
  191. created_by_id: @agent1.id,
  192. )
  193. assert(ticket1)
  194. # execute object transaction
  195. Observer::Transaction.commit
  196. Scheduler.worker(true)
  197. # verify notifications to @agent1 + @agent2
  198. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  199. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  200. end
  201. test 'ticket notification - simple' do
  202. # create ticket in group
  203. ApplicationHandleInfo.current = 'application_server'
  204. ticket1 = Ticket.create!(
  205. title: 'some notification test 3',
  206. group: Group.lookup(name: 'TicketNotificationTest'),
  207. customer: @customer,
  208. state: Ticket::State.lookup(name: 'new'),
  209. priority: Ticket::Priority.lookup(name: '2 normal'),
  210. updated_by_id: @customer.id,
  211. created_by_id: @customer.id,
  212. )
  213. Ticket::Article.create!(
  214. ticket_id: ticket1.id,
  215. from: 'some_sender@example.com',
  216. to: 'some_recipient@example.com',
  217. subject: 'some subject',
  218. message_id: 'some@id',
  219. body: 'some message',
  220. internal: false,
  221. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  222. type: Ticket::Article::Type.where(name: 'email').first,
  223. updated_by_id: @customer.id,
  224. created_by_id: @customer.id,
  225. )
  226. assert(ticket1, 'ticket created - ticket notification simple')
  227. # execute object transaction
  228. Observer::Transaction.commit
  229. Scheduler.worker(true)
  230. # verify notifications to @agent1 + @agent2
  231. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  232. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  233. # update ticket attributes
  234. ticket1.title = "#{ticket1.title} - #2"
  235. ticket1.priority = Ticket::Priority.lookup(name: '3 high')
  236. ticket1.save!
  237. # execute object transaction
  238. Observer::Transaction.commit
  239. Scheduler.worker(true)
  240. # verify notifications to @agent1 + @agent2
  241. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  242. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  243. # add article to ticket
  244. Ticket::Article.create!(
  245. ticket_id: ticket1.id,
  246. from: 'some person',
  247. subject: 'some note',
  248. body: 'some message',
  249. internal: true,
  250. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  251. type: Ticket::Article::Type.where(name: 'note').first,
  252. updated_by_id: @agent1.id,
  253. created_by_id: @agent1.id,
  254. )
  255. # execute object transaction
  256. Observer::Transaction.commit
  257. Scheduler.worker(true)
  258. # verify notifications to not to @agent1 but to @agent2
  259. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  260. assert_equal(3, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  261. # update ticket by user
  262. ticket1.owner_id = @agent1.id
  263. ticket1.updated_by_id = @agent1.id
  264. ticket1.save!
  265. Ticket::Article.create!(
  266. ticket_id: ticket1.id,
  267. from: 'some person',
  268. subject: 'some note',
  269. body: 'some message',
  270. internal: true,
  271. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  272. type: Ticket::Article::Type.where(name: 'note').first,
  273. updated_by_id: @agent1.id,
  274. created_by_id: @agent1.id,
  275. )
  276. # execute object transaction
  277. Observer::Transaction.commit
  278. Scheduler.worker(true)
  279. # verify notifications to not to @agent1 but to @agent2
  280. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  281. assert_equal(3, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  282. # create ticket with @agent1 as owner
  283. ticket2 = Ticket.create!(
  284. title: 'some notification test 4',
  285. group: Group.lookup(name: 'TicketNotificationTest'),
  286. customer_id: 2,
  287. owner_id: @agent1.id,
  288. state: Ticket::State.lookup(name: 'new'),
  289. priority: Ticket::Priority.lookup(name: '2 normal'),
  290. updated_by_id: @agent1.id,
  291. created_by_id: @agent1.id,
  292. )
  293. Ticket::Article.create!(
  294. ticket_id: ticket2.id,
  295. from: 'some_sender@example.com',
  296. to: 'some_recipient@example.com',
  297. subject: 'some subject',
  298. message_id: 'some@id',
  299. body: 'some message',
  300. internal: false,
  301. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  302. type: Ticket::Article::Type.where(name: 'phone').first,
  303. updated_by_id: @agent1.id,
  304. created_by_id: @agent1.id,
  305. )
  306. # execute object transaction
  307. Observer::Transaction.commit
  308. Scheduler.worker(true)
  309. assert(ticket2, 'ticket created')
  310. # verify notifications to no one
  311. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  312. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  313. # update ticket
  314. ticket2.title = "#{ticket2.title} - #2"
  315. ticket2.updated_by_id = @agent1.id
  316. ticket2.priority = Ticket::Priority.lookup(name: '3 high')
  317. ticket2.save!
  318. # execute object transaction
  319. Observer::Transaction.commit
  320. Scheduler.worker(true)
  321. # verify notifications to none
  322. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  323. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  324. # update ticket
  325. ticket2.title = "#{ticket2.title} - #3"
  326. ticket2.updated_by_id = @agent2.id
  327. ticket2.priority = Ticket::Priority.lookup(name: '2 normal')
  328. ticket2.save!
  329. # execute object transaction
  330. Observer::Transaction.commit
  331. Scheduler.worker(true)
  332. # verify notifications to @agent1 and not to @agent2
  333. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  334. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  335. # create ticket with @agent2 and @agent1 as owner
  336. ticket3 = Ticket.create!(
  337. title: 'some notification test 5',
  338. group: Group.lookup(name: 'TicketNotificationTest'),
  339. customer_id: 2,
  340. owner_id: @agent1.id,
  341. state: Ticket::State.lookup(name: 'new'),
  342. priority: Ticket::Priority.lookup(name: '2 normal'),
  343. updated_by_id: @agent2.id,
  344. created_by_id: @agent2.id,
  345. )
  346. article_inbound = Ticket::Article.create!(
  347. ticket_id: ticket3.id,
  348. from: 'some_sender@example.com',
  349. to: 'some_recipient@example.com',
  350. subject: 'some subject',
  351. message_id: 'some@id',
  352. body: 'some message',
  353. internal: false,
  354. sender: Ticket::Article::Sender.where(name: 'Agent').first,
  355. type: Ticket::Article::Type.where(name: 'phone').first,
  356. updated_by_id: @agent2.id,
  357. created_by_id: @agent2.id,
  358. )
  359. # execute object transaction
  360. Observer::Transaction.commit
  361. Scheduler.worker(true)
  362. assert(ticket3, 'ticket created')
  363. # verify notifications to @agent1 and not to @agent2
  364. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket3, @agent1, 'email'), ticket3.id)
  365. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket3, @agent2, 'email'), ticket3.id)
  366. # update ticket
  367. ticket3.title = "#{ticket3.title} - #2"
  368. ticket3.updated_by_id = @agent1.id
  369. ticket3.priority = Ticket::Priority.lookup(name: '3 high')
  370. ticket3.save!
  371. # execute object transaction
  372. Observer::Transaction.commit
  373. Scheduler.worker(true)
  374. # verify notifications to no one
  375. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket3, @agent1, 'email'), ticket3.id)
  376. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket3, @agent2, 'email'), ticket3.id)
  377. # update ticket
  378. ticket3.title = "#{ticket3.title} - #3"
  379. ticket3.updated_by_id = @agent2.id
  380. ticket3.priority = Ticket::Priority.lookup(name: '2 normal')
  381. ticket3.save!
  382. # execute object transaction
  383. Observer::Transaction.commit
  384. Scheduler.worker(true)
  385. # verify notifications to @agent1 and not to @agent2
  386. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket3, @agent1, 'email'), ticket3.id)
  387. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket3, @agent2, 'email'), ticket3.id)
  388. # update article / not notification should be sent
  389. article_inbound.internal = true
  390. article_inbound.save!
  391. # execute object transaction
  392. Observer::Transaction.commit
  393. Scheduler.worker(true)
  394. # verify notifications not to @agent1 and not to @agent2
  395. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket3, @agent1, 'email'), ticket3.id)
  396. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket3, @agent2, 'email'), ticket3.id)
  397. delete = ticket1.destroy
  398. assert(delete, 'ticket1 destroy')
  399. delete = ticket2.destroy
  400. assert(delete, 'ticket2 destroy')
  401. delete = ticket3.destroy
  402. assert(delete, 'ticket3 destroy')
  403. end
  404. test 'ticket notification - no notification' do
  405. # create ticket in group
  406. ticket1 = Ticket.create!(
  407. title: 'some notification test 1 - no notification',
  408. group: Group.lookup(name: 'TicketNotificationTest'),
  409. customer: @customer,
  410. state: Ticket::State.lookup(name: 'new'),
  411. priority: Ticket::Priority.lookup(name: '2 normal'),
  412. updated_by_id: @customer.id,
  413. created_by_id: @customer.id,
  414. )
  415. Ticket::Article.create!(
  416. ticket_id: ticket1.id,
  417. from: 'some_sender@example.com',
  418. to: 'some_recipient@example.com',
  419. subject: 'some subject',
  420. message_id: 'some@id',
  421. body: 'some message',
  422. internal: false,
  423. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  424. type: Ticket::Article::Type.where(name: 'email').first,
  425. updated_by_id: @customer.id,
  426. created_by_id: @customer.id,
  427. )
  428. assert(ticket1, 'ticket created - ticket no notification')
  429. # execute object transaction
  430. Observer::Transaction.commit(disable_notification: true)
  431. Scheduler.worker(true)
  432. # verify notifications to @agent1 + @agent2
  433. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  434. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  435. end
  436. test 'ticket notification - z preferences tests' do
  437. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  438. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  439. @agent1.preferences['notification_config']['matrix']['create']['criteria']['no'] = false
  440. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  441. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  442. @agent1.preferences['notification_config']['matrix']['update']['criteria']['no'] = false
  443. @agent1.save!
  444. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = false
  445. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  446. @agent2.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  447. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = false
  448. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  449. @agent2.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  450. @agent2.save!
  451. # create ticket in group
  452. ApplicationHandleInfo.current = 'scheduler.postmaster'
  453. ticket1 = Ticket.create!(
  454. title: 'some notification test - z preferences tests 1',
  455. group: Group.lookup(name: 'TicketNotificationTest'),
  456. customer: @customer,
  457. state: Ticket::State.lookup(name: 'new'),
  458. priority: Ticket::Priority.lookup(name: '2 normal'),
  459. updated_by_id: @customer.id,
  460. created_by_id: @customer.id,
  461. )
  462. Ticket::Article.create!(
  463. ticket_id: ticket1.id,
  464. from: 'some_sender@example.com',
  465. to: 'some_recipient@example.com',
  466. subject: 'some subject',
  467. message_id: 'some@id',
  468. body: 'some message',
  469. internal: false,
  470. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  471. type: Ticket::Article::Type.where(name: 'email').first,
  472. updated_by_id: @customer.id,
  473. created_by_id: @customer.id,
  474. )
  475. # execute object transaction
  476. Observer::Transaction.commit
  477. Scheduler.worker(true)
  478. # verify notifications to @agent1 + @agent2
  479. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  480. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  481. # update ticket attributes
  482. ticket1.title = "#{ticket1.title} - #2"
  483. ticket1.priority = Ticket::Priority.lookup(name: '3 high')
  484. ticket1.save!
  485. # execute object transaction
  486. Observer::Transaction.commit
  487. Scheduler.worker(true)
  488. # verify notifications to @agent1 + @agent2
  489. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  490. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  491. # create ticket in group
  492. ticket2 = Ticket.create!(
  493. title: 'some notification test - z preferences tests 2',
  494. group: Group.lookup(name: 'TicketNotificationTest'),
  495. customer: @customer,
  496. owner: @agent1,
  497. state: Ticket::State.lookup(name: 'new'),
  498. priority: Ticket::Priority.lookup(name: '2 normal'),
  499. updated_by_id: @customer.id,
  500. created_by_id: @customer.id,
  501. )
  502. Ticket::Article.create!(
  503. ticket_id: ticket2.id,
  504. from: 'some_sender@example.com',
  505. to: 'some_recipient@example.com',
  506. subject: 'some subject',
  507. message_id: 'some@id',
  508. body: 'some message',
  509. internal: false,
  510. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  511. type: Ticket::Article::Type.where(name: 'email').first,
  512. updated_by_id: @customer.id,
  513. created_by_id: @customer.id,
  514. )
  515. # execute object transaction
  516. Observer::Transaction.commit
  517. Scheduler.worker(true)
  518. # verify notifications to @agent1 + @agent2
  519. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  520. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  521. # update ticket attributes
  522. ticket2.title = "#{ticket2.title} - #2"
  523. ticket2.priority = Ticket::Priority.lookup(name: '3 high')
  524. ticket2.save!
  525. # execute object transaction
  526. Observer::Transaction.commit
  527. Scheduler.worker(true)
  528. # verify notifications to @agent1 + @agent2
  529. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  530. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  531. # create ticket in group
  532. ticket3 = Ticket.create!(
  533. title: 'some notification test - z preferences tests 3',
  534. group: Group.lookup(name: 'TicketNotificationTest'),
  535. customer: @customer,
  536. owner: @agent2,
  537. state: Ticket::State.lookup(name: 'new'),
  538. priority: Ticket::Priority.lookup(name: '2 normal'),
  539. updated_by_id: @customer.id,
  540. created_by_id: @customer.id,
  541. )
  542. Ticket::Article.create!(
  543. ticket_id: ticket3.id,
  544. from: 'some_sender@example.com',
  545. to: 'some_recipient@example.com',
  546. subject: 'some subject',
  547. message_id: 'some@id',
  548. body: 'some message',
  549. internal: false,
  550. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  551. type: Ticket::Article::Type.where(name: 'email').first,
  552. updated_by_id: @customer.id,
  553. created_by_id: @customer.id,
  554. )
  555. # execute object transaction
  556. Observer::Transaction.commit
  557. Scheduler.worker(true)
  558. # verify notifications to @agent1 + @agent2
  559. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket3, @agent1, 'email'), ticket3.id)
  560. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket3, @agent2, 'email'), ticket3.id)
  561. # update ticket attributes
  562. ticket3.title = "#{ticket3.title} - #2"
  563. ticket3.priority = Ticket::Priority.lookup(name: '3 high')
  564. ticket3.save!
  565. # execute object transaction
  566. Observer::Transaction.commit
  567. Scheduler.worker(true)
  568. # verify notifications to @agent1 + @agent2
  569. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket3, @agent1, 'email'), ticket3.id)
  570. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket3, @agent2, 'email'), ticket3.id)
  571. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  572. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  573. @agent1.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  574. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  575. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  576. @agent1.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  577. @agent1.preferences['notification_config']['group_ids'] = [Group.lookup(name: 'TicketNotificationTest').id.to_s]
  578. @agent1.save!
  579. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = false
  580. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  581. @agent2.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  582. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = false
  583. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  584. @agent2.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  585. @agent1.preferences['notification_config']['group_ids'] = ['-']
  586. @agent2.save!
  587. # create ticket in group
  588. ApplicationHandleInfo.current = 'scheduler.postmaster'
  589. ticket4 = Ticket.create!(
  590. title: 'some notification test - z preferences tests 4',
  591. group: Group.lookup(name: 'TicketNotificationTest'),
  592. customer: @customer,
  593. state: Ticket::State.lookup(name: 'new'),
  594. priority: Ticket::Priority.lookup(name: '2 normal'),
  595. updated_by_id: @customer.id,
  596. created_by_id: @customer.id,
  597. )
  598. Ticket::Article.create!(
  599. ticket_id: ticket4.id,
  600. from: 'some_sender@example.com',
  601. to: 'some_recipient@example.com',
  602. subject: 'some subject',
  603. message_id: 'some@id',
  604. body: 'some message',
  605. internal: false,
  606. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  607. type: Ticket::Article::Type.where(name: 'email').first,
  608. updated_by_id: @customer.id,
  609. created_by_id: @customer.id,
  610. )
  611. # execute object transaction
  612. Observer::Transaction.commit
  613. Scheduler.worker(true)
  614. # verify notifications to @agent1 + @agent2
  615. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket4, @agent1, 'email'), ticket4.id)
  616. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket4, @agent2, 'email'), ticket4.id)
  617. # update ticket attributes
  618. ticket4.title = "#{ticket4.title} - #2"
  619. ticket4.priority = Ticket::Priority.lookup(name: '3 high')
  620. ticket4.save!
  621. # execute object transaction
  622. Observer::Transaction.commit
  623. Scheduler.worker(true)
  624. # verify notifications to @agent1 + @agent2
  625. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket4, @agent1, 'email'), ticket4.id)
  626. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket4, @agent2, 'email'), ticket4.id)
  627. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  628. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  629. @agent1.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  630. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  631. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  632. @agent1.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  633. @agent1.preferences['notification_config']['group_ids'] = [Group.lookup(name: 'TicketNotificationTest').id.to_s]
  634. @agent1.save!
  635. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = false
  636. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  637. @agent2.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  638. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = false
  639. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  640. @agent2.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  641. @agent2.preferences['notification_config']['group_ids'] = [99]
  642. @agent2.save!
  643. # create ticket in group
  644. ApplicationHandleInfo.current = 'scheduler.postmaster'
  645. ticket5 = Ticket.create!(
  646. title: 'some notification test - z preferences tests 5',
  647. group: Group.lookup(name: 'TicketNotificationTest'),
  648. customer: @customer,
  649. state: Ticket::State.lookup(name: 'new'),
  650. priority: Ticket::Priority.lookup(name: '2 normal'),
  651. updated_by_id: @customer.id,
  652. created_by_id: @customer.id,
  653. )
  654. Ticket::Article.create!(
  655. ticket_id: ticket5.id,
  656. from: 'some_sender@example.com',
  657. to: 'some_recipient@example.com',
  658. subject: 'some subject',
  659. message_id: 'some@id',
  660. body: 'some message',
  661. internal: false,
  662. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  663. type: Ticket::Article::Type.where(name: 'email').first,
  664. updated_by_id: @customer.id,
  665. created_by_id: @customer.id,
  666. )
  667. # execute object transaction
  668. Observer::Transaction.commit
  669. Scheduler.worker(true)
  670. # verify notifications to @agent1 + @agent2
  671. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket5, @agent1, 'email'), ticket5.id)
  672. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket5, @agent2, 'email'), ticket5.id)
  673. # update ticket attributes
  674. ticket5.title = "#{ticket5.title} - #2"
  675. ticket5.priority = Ticket::Priority.lookup(name: '3 high')
  676. ticket5.save!
  677. # execute object transaction
  678. Observer::Transaction.commit
  679. Scheduler.worker(true)
  680. # verify notifications to @agent1 + @agent2
  681. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket5, @agent1, 'email'), ticket5.id)
  682. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket5, @agent2, 'email'), ticket5.id)
  683. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  684. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  685. @agent1.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  686. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  687. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  688. @agent1.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  689. @agent1.preferences['notification_config']['group_ids'] = [999]
  690. @agent1.save!
  691. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  692. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  693. @agent2.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  694. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  695. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  696. @agent2.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  697. @agent2.preferences['notification_config']['group_ids'] = [999]
  698. @agent2.save!
  699. # create ticket in group
  700. ApplicationHandleInfo.current = 'scheduler.postmaster'
  701. ticket6 = Ticket.create!(
  702. title: 'some notification test - z preferences tests 6',
  703. group: Group.lookup(name: 'TicketNotificationTest'),
  704. customer: @customer,
  705. owner: @agent1,
  706. state: Ticket::State.lookup(name: 'new'),
  707. priority: Ticket::Priority.lookup(name: '2 normal'),
  708. updated_by_id: @customer.id,
  709. created_by_id: @customer.id,
  710. )
  711. Ticket::Article.create!(
  712. ticket_id: ticket6.id,
  713. from: 'some_sender@example.com',
  714. to: 'some_recipient@example.com',
  715. subject: 'some subject',
  716. message_id: 'some@id',
  717. body: 'some message',
  718. internal: false,
  719. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  720. type: Ticket::Article::Type.where(name: 'email').first,
  721. updated_by_id: @customer.id,
  722. created_by_id: @customer.id,
  723. )
  724. # execute object transaction
  725. Observer::Transaction.commit
  726. Scheduler.worker(true)
  727. # verify notifications to @agent1 + @agent2
  728. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket6, @agent1, 'email'), ticket6.id)
  729. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket6, @agent1, 'online'), ticket6.id)
  730. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket6, @agent2, 'email'), ticket6.id)
  731. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket6, @agent2, 'online'), ticket6.id)
  732. # update ticket attributes
  733. ticket6.title = "#{ticket6.title} - #2"
  734. ticket6.priority = Ticket::Priority.lookup(name: '3 high')
  735. ticket6.save!
  736. # execute object transaction
  737. Observer::Transaction.commit
  738. Scheduler.worker(true)
  739. # verify notifications to @agent1 + @agent2
  740. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket6, @agent1, 'email'), ticket6.id)
  741. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket6, @agent1, 'online'), ticket6.id)
  742. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket6, @agent2, 'email'), ticket6.id)
  743. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket6, @agent2, 'online'), ticket6.id)
  744. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  745. @agent1.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  746. @agent1.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  747. @agent1.preferences['notification_config']['matrix']['create']['channel']['email'] = false
  748. @agent1.preferences['notification_config']['matrix']['create']['channel']['online'] = true
  749. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  750. @agent1.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  751. @agent1.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  752. @agent1.preferences['notification_config']['matrix']['update']['channel']['email'] = false
  753. @agent1.preferences['notification_config']['matrix']['update']['channel']['online'] = true
  754. @agent1.preferences['notification_config']['group_ids'] = [999]
  755. @agent1.save!
  756. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_me'] = true
  757. @agent2.preferences['notification_config']['matrix']['create']['criteria']['owned_by_nobody'] = false
  758. @agent2.preferences['notification_config']['matrix']['create']['criteria']['no'] = true
  759. @agent2.preferences['notification_config']['matrix']['create']['channel']['email'] = false
  760. @agent2.preferences['notification_config']['matrix']['create']['channel']['online'] = true
  761. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_me'] = true
  762. @agent2.preferences['notification_config']['matrix']['update']['criteria']['owned_by_nobody'] = false
  763. @agent2.preferences['notification_config']['matrix']['update']['criteria']['no'] = true
  764. @agent2.preferences['notification_config']['matrix']['update']['channel']['email'] = false
  765. @agent2.preferences['notification_config']['matrix']['update']['channel']['online'] = true
  766. @agent2.preferences['notification_config']['group_ids'] = [999]
  767. @agent2.save!
  768. # create ticket in group
  769. ApplicationHandleInfo.current = 'scheduler.postmaster'
  770. ticket7 = Ticket.create!(
  771. title: 'some notification test - z preferences tests 7',
  772. group: Group.lookup(name: 'TicketNotificationTest'),
  773. customer: @customer,
  774. owner: @agent1,
  775. state: Ticket::State.lookup(name: 'new'),
  776. priority: Ticket::Priority.lookup(name: '2 normal'),
  777. updated_by_id: @customer.id,
  778. created_by_id: @customer.id,
  779. )
  780. Ticket::Article.create!(
  781. ticket_id: ticket7.id,
  782. from: 'some_sender@example.com',
  783. to: 'some_recipient@example.com',
  784. subject: 'some subject',
  785. message_id: 'some@id',
  786. body: 'some message',
  787. internal: false,
  788. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  789. type: Ticket::Article::Type.where(name: 'email').first,
  790. updated_by_id: @customer.id,
  791. created_by_id: @customer.id,
  792. )
  793. # execute object transaction
  794. Observer::Transaction.commit
  795. Scheduler.worker(true)
  796. # verify notifications to @agent1 + @agent2
  797. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket7, @agent1, 'email'), ticket7.id)
  798. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket7, @agent1, 'online'), ticket7.id)
  799. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket7, @agent2, 'email'), ticket7.id)
  800. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket7, @agent2, 'online'), ticket7.id)
  801. # update ticket attributes
  802. ticket7.title = "#{ticket7.title} - #2"
  803. ticket7.priority = Ticket::Priority.lookup(name: '3 high')
  804. ticket7.save!
  805. # execute object transaction
  806. Observer::Transaction.commit
  807. Scheduler.worker(true)
  808. # verify notifications to @agent1 + @agent2
  809. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket7, @agent1, 'email'), ticket7.id)
  810. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket7, @agent1, 'online'), ticket7.id)
  811. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket7, @agent2, 'email'), ticket7.id)
  812. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket7, @agent2, 'online'), ticket7.id)
  813. end
  814. test 'ticket notification events' do
  815. # create ticket in group
  816. ticket1 = Ticket.create!(
  817. title: 'some notification event test 1',
  818. group: Group.lookup(name: 'TicketNotificationTest'),
  819. customer: @customer,
  820. state: Ticket::State.lookup(name: 'new'),
  821. priority: Ticket::Priority.lookup(name: '2 normal'),
  822. updated_by_id: @customer.id,
  823. created_by_id: @customer.id,
  824. )
  825. Ticket::Article.create!(
  826. ticket_id: ticket1.id,
  827. from: 'some_sender@example.com',
  828. to: 'some_recipient@example.com',
  829. subject: 'some subject',
  830. message_id: 'some@id',
  831. body: 'some message',
  832. internal: false,
  833. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  834. type: Ticket::Article::Type.where(name: 'email').first,
  835. updated_by_id: @customer.id,
  836. created_by_id: @customer.id,
  837. )
  838. assert(ticket1, 'ticket created')
  839. # execute object transaction
  840. Observer::Transaction.commit
  841. # update ticket attributes
  842. ticket1.title = "#{ticket1.title} - #2"
  843. ticket1.priority = Ticket::Priority.lookup(name: '3 high')
  844. ticket1.save!
  845. list = EventBuffer.list('transaction')
  846. list_objects = Observer::Transaction.get_uniq_changes(list)
  847. assert_equal('some notification event test 1', list_objects['Ticket'][ticket1.id][:changes]['title'][0])
  848. assert_equal('some notification event test 1 - #2', list_objects['Ticket'][ticket1.id][:changes]['title'][1])
  849. assert_not(list_objects['Ticket'][ticket1.id][:changes]['priority'])
  850. assert_equal(2, list_objects['Ticket'][ticket1.id][:changes]['priority_id'][0])
  851. assert_equal(3, list_objects['Ticket'][ticket1.id][:changes]['priority_id'][1])
  852. # update ticket attributes
  853. ticket1.title = "#{ticket1.title} - #3"
  854. ticket1.priority = Ticket::Priority.lookup(name: '1 low')
  855. ticket1.save!
  856. list = EventBuffer.list('transaction')
  857. list_objects = Observer::Transaction.get_uniq_changes(list)
  858. assert_equal('some notification event test 1', list_objects['Ticket'][ticket1.id][:changes]['title'][0])
  859. assert_equal('some notification event test 1 - #2 - #3', list_objects['Ticket'][ticket1.id][:changes]['title'][1])
  860. assert_not(list_objects['Ticket'][ticket1.id][:changes]['priority'])
  861. assert_equal(2, list_objects['Ticket'][ticket1.id][:changes]['priority_id'][0])
  862. assert_equal(1, list_objects['Ticket'][ticket1.id][:changes]['priority_id'][1])
  863. end
  864. test 'ticket notification - out of office' do
  865. # create ticket in group
  866. ticket1 = Ticket.create!(
  867. title: 'some notification test out of office',
  868. group: Group.lookup(name: 'TicketNotificationTest'),
  869. customer: @customer,
  870. owner_id: @agent2.id,
  871. #state: Ticket::State.lookup(name: 'new'),
  872. #priority: Ticket::Priority.lookup(name: '2 normal'),
  873. updated_by_id: @customer.id,
  874. created_by_id: @customer.id,
  875. )
  876. Ticket::Article.create!(
  877. ticket_id: ticket1.id,
  878. from: 'some_sender@example.com',
  879. to: 'some_recipient@example.com',
  880. subject: 'some subject',
  881. message_id: 'some@id',
  882. body: 'some message',
  883. internal: false,
  884. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  885. type: Ticket::Article::Type.where(name: 'email').first,
  886. updated_by_id: @customer.id,
  887. created_by_id: @customer.id,
  888. )
  889. assert(ticket1, 'ticket created - ticket notification simple')
  890. # execute object transaction
  891. Observer::Transaction.commit
  892. Scheduler.worker(true)
  893. # verify notifications to @agent1 + @agent2
  894. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent1, 'email'), ticket1.id)
  895. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, @agent2, 'email'), ticket1.id)
  896. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent3, 'email'), ticket1.id)
  897. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, @agent4, 'email'), ticket1.id)
  898. @agent2.out_of_office = true
  899. @agent2.preferences[:out_of_office_text] = 'at the doctor'
  900. @agent2.out_of_office_replacement_id = @agent3.id
  901. @agent2.out_of_office_start_at = Time.zone.today - 2.days
  902. @agent2.out_of_office_end_at = Time.zone.today + 2.days
  903. @agent2.save!
  904. # create ticket in group
  905. ticket2 = Ticket.create!(
  906. title: 'some notification test out of office',
  907. group: Group.lookup(name: 'TicketNotificationTest'),
  908. customer: @customer,
  909. owner_id: @agent2.id,
  910. #state: Ticket::State.lookup(name: 'new'),
  911. #priority: Ticket::Priority.lookup(name: '2 normal'),
  912. updated_by_id: @customer.id,
  913. created_by_id: @customer.id,
  914. )
  915. Ticket::Article.create!(
  916. ticket_id: ticket2.id,
  917. from: 'some_sender@example.com',
  918. to: 'some_recipient@example.com',
  919. subject: 'some subject',
  920. message_id: 'some@id',
  921. body: 'some message',
  922. internal: false,
  923. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  924. type: Ticket::Article::Type.where(name: 'email').first,
  925. updated_by_id: @customer.id,
  926. created_by_id: @customer.id,
  927. )
  928. assert(ticket2, 'ticket created - ticket notification simple')
  929. # execute object transaction
  930. Observer::Transaction.commit
  931. Scheduler.worker(true)
  932. # verify notifications to @agent1 + @agent2
  933. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  934. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  935. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket2, @agent3, 'email'), ticket2.id)
  936. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent4, 'email'), ticket2.id)
  937. # update ticket attributes
  938. ticket2.title = "#{ticket2.title} - #2"
  939. ticket2.priority = Ticket::Priority.lookup(name: '3 high')
  940. ticket2.save!
  941. # execute object transaction
  942. Observer::Transaction.commit
  943. Scheduler.worker(true)
  944. # verify notifications to @agent1 + @agent2
  945. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  946. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  947. assert_equal(2, NotificationFactory::Mailer.already_sent?(ticket2, @agent3, 'email'), ticket2.id)
  948. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent4, 'email'), ticket2.id)
  949. @agent3.out_of_office = true
  950. @agent3.preferences[:out_of_office_text] = 'at the doctor'
  951. @agent3.out_of_office_replacement_id = @agent4.id
  952. @agent3.out_of_office_start_at = Time.zone.today - 2.days
  953. @agent3.out_of_office_end_at = Time.zone.today + 2.days
  954. @agent3.save!
  955. # update ticket attributes
  956. ticket2.title = "#{ticket2.title} - #3"
  957. ticket2.priority = Ticket::Priority.lookup(name: '3 high')
  958. ticket2.save!
  959. # execute object transaction
  960. Observer::Transaction.commit
  961. Scheduler.worker(true)
  962. # verify notifications to @agent1 + @agent2
  963. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket2, @agent1, 'email'), ticket2.id)
  964. assert_equal(3, NotificationFactory::Mailer.already_sent?(ticket2, @agent2, 'email'), ticket2.id)
  965. assert_equal(3, NotificationFactory::Mailer.already_sent?(ticket2, @agent3, 'email'), ticket2.id)
  966. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket2, @agent4, 'email'), ticket2.id)
  967. end
  968. test 'ticket notification template' do
  969. # create ticket in group
  970. ticket1 = Ticket.create!(
  971. title: 'some notification template test 1 Bobs\'s resumé',
  972. group: Group.lookup(name: 'TicketNotificationTest'),
  973. customer: @customer,
  974. state: Ticket::State.lookup(name: 'new'),
  975. priority: Ticket::Priority.lookup(name: '2 normal'),
  976. updated_by_id: @customer.id,
  977. created_by_id: @customer.id,
  978. )
  979. article = Ticket::Article.create!(
  980. ticket_id: ticket1.id,
  981. from: 'some_sender@example.com',
  982. to: 'some_recipient@example.com',
  983. subject: 'some subject',
  984. message_id: 'some@id',
  985. body: "some message\nnewline1 abc\nnewline2",
  986. internal: false,
  987. sender: Ticket::Article::Sender.where(name: 'Customer').first,
  988. type: Ticket::Article::Type.where(name: 'email').first,
  989. updated_by_id: @customer.id,
  990. created_by_id: @customer.id,
  991. )
  992. assert(ticket1, 'ticket created - ticket notification template')
  993. bg = Transaction::Notification.new(
  994. ticket_id: ticket1.id,
  995. article_id: article.id,
  996. type: 'update',
  997. changes: {
  998. 'priority_id' => [1, 2],
  999. 'pending_time' => [nil, Time.zone.parse('2015-01-11 23:33:47 UTC')],
  1000. },
  1001. user_id: ticket1.updated_by_id,
  1002. )
  1003. # check changed attributes
  1004. human_changes = bg.human_changes(@agent2, ticket1)
  1005. assert(human_changes['Priority'], 'Check if attributes translated based on ObjectManager::Attribute')
  1006. assert(human_changes['Pending till'], 'Check if attributes translated based on ObjectManager::Attribute')
  1007. assert_equal('1 low', human_changes['Priority'][0])
  1008. assert_equal('2 normal', human_changes['Priority'][1])
  1009. assert_equal('', human_changes['Pending till'][0].to_s)
  1010. assert_equal('2015-01-11 23:33:47 UTC', human_changes['Pending till'][1].to_s)
  1011. assert_not(human_changes['priority_id'])
  1012. assert_not(human_changes['pending_time'])
  1013. assert_not(human_changes['pending_till'])
  1014. # en notification
  1015. result = NotificationFactory::Mailer.template(
  1016. locale: @agent2.preferences[:locale],
  1017. template: 'ticket_update',
  1018. objects: {
  1019. ticket: ticket1,
  1020. article: article,
  1021. recipient: @agent2,
  1022. changes: human_changes,
  1023. },
  1024. )
  1025. assert_match(/Bobs's resumé/, result[:subject])
  1026. assert_match(/Priority/, result[:body])
  1027. assert_match(/1 low/, result[:body])
  1028. assert_match(/2 normal/, result[:body])
  1029. assert_match(/Pending till/, result[:body])
  1030. assert_match(/2015-01-11 23:33:47 UTC/, result[:body])
  1031. assert_match(/update/, result[:body])
  1032. assert_no_match(/pending_till/, result[:body])
  1033. assert_no_match(/i18n/, result[:body])
  1034. human_changes = bg.human_changes(@agent1, ticket1)
  1035. assert(human_changes['Priority'], 'Check if attributes translated based on ObjectManager::Attribute')
  1036. assert(human_changes['Pending till'], 'Check if attributes translated based on ObjectManager::Attribute')
  1037. assert_equal('1 niedrig', human_changes['Priority'][0])
  1038. assert_equal('2 normal', human_changes['Priority'][1])
  1039. assert_equal('', human_changes['Pending till'][0].to_s)
  1040. assert_equal('2015-01-11 23:33:47 UTC', human_changes['Pending till'][1].to_s)
  1041. assert_not(human_changes['priority_id'])
  1042. assert_not(human_changes['pending_time'])
  1043. assert_not(human_changes['pending_till'])
  1044. # de notification
  1045. result = NotificationFactory::Mailer.template(
  1046. locale: @agent1.preferences[:locale],
  1047. template: 'ticket_update',
  1048. objects: {
  1049. ticket: ticket1,
  1050. article: article,
  1051. recipient: @agent1,
  1052. changes: human_changes,
  1053. },
  1054. )
  1055. assert_match(/Bobs's resumé/, result[:subject])
  1056. assert_match(/Priorität/, result[:body])
  1057. assert_match(/1 niedrig/, result[:body])
  1058. assert_match(/2 normal/, result[:body])
  1059. assert_match(/Warten/, result[:body])
  1060. assert_match(/2015-01-11 23:33:47 UTC/, result[:body])
  1061. assert_match(/aktualis/, result[:body])
  1062. assert_no_match(/pending_till/, result[:body])
  1063. assert_no_match(/i18n/, result[:body])
  1064. bg = Transaction::Notification.new(
  1065. ticket_id: ticket1.id,
  1066. article_id: article.id,
  1067. type: 'update',
  1068. changes: {
  1069. title: ['some notification template test old 1', 'some notification template test 1 #2'],
  1070. priority_id: [2, 3],
  1071. },
  1072. user_id: @customer.id,
  1073. )
  1074. # check changed attributes
  1075. human_changes = bg.human_changes(@agent1, ticket1)
  1076. assert(human_changes['Title'], 'Check if attributes translated based on ObjectManager::Attribute')
  1077. assert(human_changes['Priority'], 'Check if attributes translated based on ObjectManager::Attribute')
  1078. assert_equal('2 normal', human_changes['Priority'][0])
  1079. assert_equal('3 hoch', human_changes['Priority'][1])
  1080. assert_equal('some notification template test old 1', human_changes['Title'][0])
  1081. assert_equal('some notification template test 1 #2', human_changes['Title'][1])
  1082. assert_not(human_changes['priority_id'])
  1083. assert_not(human_changes['pending_time'])
  1084. assert_not(human_changes['pending_till'])
  1085. # de notification
  1086. result = NotificationFactory::Mailer.template(
  1087. locale: @agent1.preferences[:locale],
  1088. template: 'ticket_update',
  1089. objects: {
  1090. ticket: ticket1,
  1091. article: article,
  1092. recipient: @agent1,
  1093. changes: human_changes,
  1094. }
  1095. )
  1096. assert_match(/Bobs's resumé/, result[:subject])
  1097. assert_match(/Titel/, result[:body])
  1098. assert_no_match(/Title/, result[:body])
  1099. assert_match(/some notification template test old 1/, result[:body])
  1100. assert_match(/some notification template test 1 #2/, result[:body])
  1101. assert_match(/Priorität/, result[:body])
  1102. assert_no_match(/Priority/, result[:body])
  1103. assert_match(/3 hoch/, result[:body])
  1104. assert_match(/2 normal/, result[:body])
  1105. assert_match(/aktualisier/, result[:body])
  1106. human_changes = bg.human_changes(@agent2, ticket1)
  1107. # en notification
  1108. result = NotificationFactory::Mailer.template(
  1109. locale: @agent2.preferences[:locale],
  1110. template: 'ticket_update',
  1111. objects: {
  1112. ticket: ticket1,
  1113. article: article,
  1114. recipient: @agent2,
  1115. changes: human_changes,
  1116. }
  1117. )
  1118. assert_match(/Bobs's resumé/, result[:subject])
  1119. assert_match(/Title/, result[:body])
  1120. assert_match(/some notification template test old 1/, result[:body])
  1121. assert_match(/some notification template test 1 #2/, result[:body])
  1122. assert_match(/Priority/, result[:body])
  1123. assert_match(/3 high/, result[:body])
  1124. assert_match(/2 normal/, result[:body])
  1125. assert_no_match(/Pending till/, result[:body])
  1126. assert_no_match(/2015-01-11 23:33:47 UTC/, result[:body])
  1127. assert_match(/update/, result[:body])
  1128. assert_no_match(/pending_till/, result[:body])
  1129. assert_no_match(/i18n/, result[:body])
  1130. end
  1131. end