ticket_notification_test.rb 54 KB

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