ticket_notification_test.rb 54 KB

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