ticket_notification_test.rb 55 KB

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