ticket_notification_test.rb 54 KB

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