ticket_trigger_extended_test.rb 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class TicketTriggerExtendedTest < ActiveSupport::TestCase
  4. include BackgroundJobsHelper
  5. setup do
  6. Setting.set('ticket_trigger_recursive', true)
  7. end
  8. test 'recursive trigger' do
  9. Trigger.create!(
  10. name: '1) set prio to 3 high',
  11. condition: {
  12. 'ticket.action' => {
  13. 'operator' => 'is',
  14. 'value' => 'create',
  15. },
  16. 'ticket.state_id' => {
  17. 'operator' => 'is',
  18. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  19. },
  20. },
  21. perform: {
  22. 'ticket.priority_id' => {
  23. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  24. },
  25. },
  26. disable_notification: true,
  27. active: true,
  28. created_by_id: 1,
  29. updated_by_id: 1,
  30. )
  31. Trigger.create!(
  32. name: '2) set state to closed',
  33. condition: {
  34. 'ticket.action' => {
  35. 'operator' => 'is',
  36. 'value' => 'create',
  37. },
  38. 'ticket.priority_id' => {
  39. 'operator' => 'is',
  40. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  41. },
  42. },
  43. perform: {
  44. 'ticket.state_id' => {
  45. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  46. },
  47. },
  48. disable_notification: true,
  49. active: true,
  50. created_by_id: 1,
  51. updated_by_id: 1,
  52. )
  53. email_raw_string = 'From: me@example.com
  54. To: customer@example.com
  55. Subject: some new subject
  56. Some Text'
  57. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  58. assert_equal('some new subject', ticket_p.title)
  59. assert_equal('Users', ticket_p.group.name)
  60. assert_equal('3 high', ticket_p.priority.name)
  61. assert_equal('closed', ticket_p.state.name)
  62. assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify')
  63. end
  64. test 'recursive trigger - loop test' do
  65. Trigger.create!(
  66. name: '1) set prio to 3 high',
  67. condition: {
  68. 'ticket.priority_id' => {
  69. 'operator' => 'is',
  70. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  71. },
  72. },
  73. perform: {
  74. 'ticket.priority_id' => {
  75. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  76. },
  77. 'ticket.state_id' => {
  78. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  79. },
  80. },
  81. disable_notification: true,
  82. active: true,
  83. created_by_id: 1,
  84. updated_by_id: 1,
  85. )
  86. Trigger.create!(
  87. name: '2) set prio to 1 low',
  88. condition: {
  89. 'ticket.priority_id' => {
  90. 'operator' => 'is',
  91. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  92. },
  93. },
  94. perform: {
  95. 'ticket.priority_id' => {
  96. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  97. },
  98. 'ticket.state_id' => {
  99. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  100. },
  101. },
  102. disable_notification: true,
  103. active: true,
  104. created_by_id: 1,
  105. updated_by_id: 1,
  106. )
  107. Trigger.create!(
  108. name: '3) set prio to 3 high',
  109. condition: {
  110. 'ticket.priority_id' => {
  111. 'operator' => 'is',
  112. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  113. },
  114. },
  115. perform: {
  116. 'ticket.priority_id' => {
  117. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  118. },
  119. },
  120. disable_notification: true,
  121. active: true,
  122. created_by_id: 1,
  123. updated_by_id: 1,
  124. )
  125. email_raw_string = 'From: me@example.com
  126. To: customer@example.com
  127. Subject: some new subject
  128. Some Text'
  129. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  130. assert_equal('some new subject', ticket_p.title)
  131. assert_equal('Users', ticket_p.group.name)
  132. assert_equal('2 normal', ticket_p.priority.name)
  133. assert_equal('open', ticket_p.state.name)
  134. assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify')
  135. end
  136. test 'recursive trigger - 2 trigger will not trigger next trigger' do
  137. Trigger.create!(
  138. name: '1) set prio to 3 high',
  139. condition: {
  140. 'ticket.action' => {
  141. 'operator' => 'is',
  142. 'value' => 'create',
  143. },
  144. 'ticket.priority_id' => {
  145. 'operator' => 'is',
  146. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  147. },
  148. },
  149. perform: {
  150. 'ticket.priority_id' => {
  151. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  152. },
  153. },
  154. disable_notification: true,
  155. active: true,
  156. created_by_id: 1,
  157. updated_by_id: 1,
  158. )
  159. Trigger.create!(
  160. name: '2) set state to open',
  161. condition: {
  162. 'ticket.action' => {
  163. 'operator' => 'is',
  164. 'value' => 'create',
  165. },
  166. 'ticket.priority_id' => {
  167. 'operator' => 'is',
  168. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  169. },
  170. },
  171. perform: {
  172. 'ticket.state_id' => {
  173. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  174. },
  175. },
  176. disable_notification: true,
  177. active: true,
  178. created_by_id: 1,
  179. updated_by_id: 1,
  180. )
  181. Trigger.create!(
  182. name: '3) set state to closed',
  183. condition: {
  184. 'ticket.action' => {
  185. 'operator' => 'is',
  186. 'value' => 'create',
  187. },
  188. 'ticket.priority_id' => {
  189. 'operator' => 'is',
  190. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  191. },
  192. 'ticket.state_id' => {
  193. 'operator' => 'is',
  194. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  195. },
  196. },
  197. perform: {
  198. 'ticket.state_id' => {
  199. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  200. },
  201. },
  202. disable_notification: true,
  203. active: true,
  204. created_by_id: 1,
  205. updated_by_id: 1,
  206. )
  207. email_raw_string = 'From: me@example.com
  208. To: customer@example.com
  209. Subject: some new subject
  210. Some Text'
  211. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  212. assert_equal('some new subject', ticket_p.title)
  213. assert_equal('Users', ticket_p.group.name)
  214. assert_equal('3 high', ticket_p.priority.name)
  215. assert_equal('new', ticket_p.state.name)
  216. assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify')
  217. end
  218. test 'recursive trigger - 2 trigger will trigger next trigger - case 1' do
  219. Trigger.create!(
  220. name: '1) set state to closed',
  221. condition: {
  222. 'ticket.action' => {
  223. 'operator' => 'is',
  224. 'value' => 'create',
  225. },
  226. 'ticket.priority_id' => {
  227. 'operator' => 'is',
  228. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  229. },
  230. 'ticket.state_id' => {
  231. 'operator' => 'is',
  232. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  233. },
  234. },
  235. perform: {
  236. 'ticket.state_id' => {
  237. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  238. },
  239. },
  240. disable_notification: true,
  241. active: true,
  242. created_by_id: 1,
  243. updated_by_id: 1,
  244. )
  245. Trigger.create!(
  246. name: '2) set prio to 3 high',
  247. condition: {
  248. 'ticket.action' => {
  249. 'operator' => 'is',
  250. 'value' => 'create',
  251. },
  252. 'ticket.priority_id' => {
  253. 'operator' => 'is',
  254. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  255. },
  256. },
  257. perform: {
  258. 'ticket.priority_id' => {
  259. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  260. },
  261. },
  262. disable_notification: true,
  263. active: true,
  264. created_by_id: 1,
  265. updated_by_id: 1,
  266. )
  267. Trigger.create!(
  268. name: '3) set state to open',
  269. condition: {
  270. 'ticket.action' => {
  271. 'operator' => 'is',
  272. 'value' => 'create',
  273. },
  274. },
  275. perform: {
  276. 'ticket.state_id' => {
  277. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  278. },
  279. },
  280. disable_notification: true,
  281. active: true,
  282. created_by_id: 1,
  283. updated_by_id: 1,
  284. )
  285. email_raw_string = 'From: me@example.com
  286. To: customer@example.com
  287. Subject: some new subject
  288. Some Text'
  289. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  290. assert_equal('some new subject', ticket_p.title)
  291. assert_equal('Users', ticket_p.group.name)
  292. assert_equal('3 high', ticket_p.priority.name)
  293. assert_equal('closed', ticket_p.state.name)
  294. assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify')
  295. end
  296. test 'recursive trigger - 2 trigger will trigger next trigger - case 2' do
  297. Trigger.create!(
  298. name: '1) set prio to 3 high',
  299. condition: {
  300. 'ticket.action' => {
  301. 'operator' => 'is',
  302. 'value' => 'create',
  303. },
  304. 'ticket.priority_id' => {
  305. 'operator' => 'is',
  306. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  307. },
  308. 'ticket.state_id' => {
  309. 'operator' => 'is',
  310. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  311. },
  312. },
  313. perform: {
  314. 'ticket.priority_id' => {
  315. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  316. },
  317. },
  318. disable_notification: true,
  319. active: true,
  320. created_by_id: 1,
  321. updated_by_id: 1,
  322. )
  323. Trigger.create!(
  324. name: '2) set state to closed',
  325. condition: {
  326. 'ticket.action' => {
  327. 'operator' => 'is',
  328. 'value' => 'create',
  329. },
  330. 'ticket.priority_id' => {
  331. 'operator' => 'is',
  332. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  333. },
  334. 'ticket.state_id' => {
  335. 'operator' => 'is',
  336. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  337. },
  338. },
  339. perform: {
  340. 'ticket.state_id' => {
  341. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  342. },
  343. },
  344. disable_notification: true,
  345. active: true,
  346. created_by_id: 1,
  347. updated_by_id: 1,
  348. )
  349. Trigger.create!(
  350. name: '3) set state to open',
  351. condition: {
  352. 'ticket.action' => {
  353. 'operator' => 'is',
  354. 'value' => 'create',
  355. },
  356. },
  357. perform: {
  358. 'ticket.state_id' => {
  359. 'value' => Ticket::State.lookup(name: 'open').id.to_s,
  360. },
  361. },
  362. disable_notification: true,
  363. active: true,
  364. created_by_id: 1,
  365. updated_by_id: 1,
  366. )
  367. email_raw_string = 'From: me@example.com
  368. To: customer@example.com
  369. Subject: some new subject
  370. Some Text'
  371. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, email_raw_string)
  372. assert_equal('some new subject', ticket_p.title)
  373. assert_equal('Users', ticket_p.group.name)
  374. assert_equal('3 high', ticket_p.priority.name)
  375. assert_equal('closed', ticket_p.state.name)
  376. assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify')
  377. end
  378. test 'trigger based move and verify correct agent notifications' do
  379. group1 = Group.create!(
  380. name: 'Group 1',
  381. active: true,
  382. email_address: EmailAddress.first,
  383. created_by_id: 1,
  384. updated_by_id: 1,
  385. )
  386. group2 = Group.create!(
  387. name: 'Group 2',
  388. active: true,
  389. email_address: EmailAddress.first,
  390. created_by_id: 1,
  391. updated_by_id: 1,
  392. )
  393. group3 = Group.create!(
  394. name: 'Group 3',
  395. active: true,
  396. email_address: EmailAddress.first,
  397. created_by_id: 1,
  398. updated_by_id: 1,
  399. )
  400. roles = Role.where(name: 'Agent')
  401. user1 = User.create!(
  402. login: 'trigger1@example.org',
  403. firstname: 'trigger1',
  404. lastname: 'trigger1',
  405. email: 'trigger1@example.org',
  406. password: 'some_pass',
  407. active: true,
  408. groups: [group1],
  409. roles: roles,
  410. created_by_id: 1,
  411. updated_by_id: 1,
  412. )
  413. user2 = User.create!(
  414. login: 'trigger2@example.org',
  415. firstname: 'trigger2',
  416. lastname: 'trigger2',
  417. email: 'trigger2@example.org',
  418. password: 'some_pass',
  419. active: true,
  420. groups: [group2],
  421. roles: roles,
  422. created_by_id: 1,
  423. updated_by_id: 1,
  424. )
  425. # trigger, move ticket created in group1 into group3 and then into group2
  426. Trigger.create_or_update(
  427. name: '1 dispatch',
  428. condition: {
  429. 'ticket.action' => {
  430. 'operator' => 'is',
  431. 'value' => 'create',
  432. },
  433. 'ticket.group_id' => {
  434. 'operator' => 'is',
  435. 'value' => group3.id.to_s,
  436. },
  437. 'ticket.state_id' => {
  438. 'operator' => 'is',
  439. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  440. },
  441. },
  442. perform: {
  443. 'ticket.group_id' => {
  444. 'value' => group2.id.to_s,
  445. },
  446. },
  447. disable_notification: true,
  448. active: true,
  449. created_by_id: 1,
  450. updated_by_id: 1,
  451. )
  452. Trigger.create_or_update(
  453. name: '2 dispatch',
  454. condition: {
  455. 'ticket.action' => {
  456. 'operator' => 'is',
  457. 'value' => 'create',
  458. },
  459. 'ticket.state_id' => {
  460. 'operator' => 'is',
  461. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  462. },
  463. },
  464. perform: {
  465. 'ticket.group_id' => {
  466. 'value' => group3.id.to_s,
  467. },
  468. },
  469. disable_notification: true,
  470. active: true,
  471. created_by_id: 1,
  472. updated_by_id: 1,
  473. )
  474. ticket1 = Ticket.create!(
  475. title: '123',
  476. group: group1,
  477. customer_id: 2,
  478. state: Ticket::State.lookup(name: 'new'),
  479. priority: Ticket::Priority.lookup(name: '2 normal'),
  480. updated_by_id: 1,
  481. created_by_id: 1,
  482. )
  483. assert(ticket1)
  484. assert_equal(ticket1.title, '123')
  485. assert_equal(ticket1.group.name, group1.name)
  486. assert_equal(ticket1.state.name, 'new')
  487. Ticket::Article.create!(
  488. ticket_id: ticket1.id,
  489. from: 'some_sender@example.com',
  490. to: 'some_recipient@example.com',
  491. subject: 'some subject',
  492. message_id: 'some@id',
  493. body: 'some message',
  494. internal: false,
  495. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  496. type: Ticket::Article::Type.find_by(name: 'email'),
  497. updated_by_id: 1,
  498. created_by_id: 1,
  499. )
  500. # verfiy if agent1 got no notifcation
  501. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user1, 'email'), ticket1.id)
  502. # verfiy if agent2 got no notifcation
  503. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id)
  504. perform_enqueued_jobs commit_transaction: true
  505. ticket1.reload
  506. assert_equal('123', ticket1.title)
  507. assert_equal(group2.name, ticket1.group.name)
  508. assert_equal('new', ticket1.state.name)
  509. assert_equal('2 normal', ticket1.priority.name)
  510. assert_equal(1, ticket1.articles.count)
  511. # verfiy if agent1 got no notifcation
  512. assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user1, 'email'), ticket1.id)
  513. # verfiy if agent2 got notifcation
  514. assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id)
  515. end
  516. test 'recursive trigger loop check' do
  517. Setting.set('ticket_trigger_recursive_max_loop', 2)
  518. Trigger.create!(
  519. name: '000',
  520. condition: {
  521. 'ticket.action' => {
  522. 'operator' => 'is',
  523. 'value' => 'create',
  524. },
  525. 'ticket.priority_id' => {
  526. 'operator' => 'is',
  527. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  528. },
  529. },
  530. perform: {
  531. 'ticket.state_id' => {
  532. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  533. },
  534. },
  535. disable_notification: true,
  536. active: true,
  537. created_by_id: 1,
  538. updated_by_id: 1,
  539. )
  540. Trigger.create!(
  541. name: '001',
  542. condition: {
  543. 'ticket.action' => {
  544. 'operator' => 'is',
  545. 'value' => 'create',
  546. },
  547. 'ticket.priority_id' => {
  548. 'operator' => 'is',
  549. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  550. },
  551. },
  552. perform: {
  553. 'ticket.priority_id' => {
  554. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  555. },
  556. },
  557. disable_notification: true,
  558. active: true,
  559. created_by_id: 1,
  560. updated_by_id: 1,
  561. )
  562. Trigger.create!(
  563. name: '002',
  564. condition: {
  565. 'ticket.action' => {
  566. 'operator' => 'is',
  567. 'value' => 'create',
  568. },
  569. 'ticket.priority_id' => {
  570. 'operator' => 'is',
  571. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  572. },
  573. },
  574. perform: {
  575. 'ticket.priority_id' => {
  576. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  577. },
  578. },
  579. disable_notification: true,
  580. active: true,
  581. created_by_id: 1,
  582. updated_by_id: 1,
  583. )
  584. group1 = Group.find_by(name: 'Users')
  585. ticket1 = Ticket.create!(
  586. title: '123',
  587. group: group1,
  588. customer_id: 2,
  589. state: Ticket::State.lookup(name: 'new'),
  590. priority: Ticket::Priority.lookup(name: '2 normal'),
  591. updated_by_id: 1,
  592. created_by_id: 1,
  593. )
  594. assert(ticket1)
  595. assert_equal(ticket1.title, '123')
  596. assert_equal(ticket1.group.name, group1.name)
  597. assert_equal(ticket1.state.name, 'new')
  598. Ticket::Article.create!(
  599. ticket_id: ticket1.id,
  600. from: 'some_sender@example.com',
  601. to: 'some_recipient@example.com',
  602. subject: 'some subject',
  603. message_id: 'some@id',
  604. body: 'some message',
  605. internal: false,
  606. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  607. type: Ticket::Article::Type.find_by(name: 'email'),
  608. updated_by_id: 1,
  609. created_by_id: 1,
  610. )
  611. perform_enqueued_jobs commit_transaction: true
  612. ticket1.reload
  613. assert_equal('123', ticket1.title)
  614. assert_equal('new', ticket1.state.name)
  615. assert_equal('1 low', ticket1.priority.name)
  616. assert_equal(1, ticket1.articles.count)
  617. Setting.set('ticket_trigger_recursive_max_loop', 3)
  618. ticket1 = Ticket.create!(
  619. title: '123',
  620. group: group1,
  621. customer_id: 2,
  622. state: Ticket::State.lookup(name: 'new'),
  623. priority: Ticket::Priority.lookup(name: '2 normal'),
  624. updated_by_id: 1,
  625. created_by_id: 1,
  626. )
  627. assert(ticket1)
  628. assert_equal(ticket1.title, '123')
  629. assert_equal(ticket1.group.name, group1.name)
  630. assert_equal(ticket1.state.name, 'new')
  631. Ticket::Article.create!(
  632. ticket_id: ticket1.id,
  633. from: 'some_sender@example.com',
  634. to: 'some_recipient@example.com',
  635. subject: 'some subject',
  636. message_id: 'some@id',
  637. body: 'some message',
  638. internal: false,
  639. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  640. type: Ticket::Article::Type.find_by(name: 'email'),
  641. updated_by_id: 1,
  642. created_by_id: 1,
  643. )
  644. perform_enqueued_jobs commit_transaction: true
  645. ticket1.reload
  646. assert_equal('123', ticket1.title)
  647. assert_equal('closed', ticket1.state.name)
  648. assert_equal('1 low', ticket1.priority.name)
  649. assert_equal(1, ticket1.articles.count)
  650. end
  651. test 'recursive trigger with auto responder' do
  652. group1 = Group.create!(
  653. name: 'Group dispatch',
  654. active: true,
  655. created_by_id: 1,
  656. updated_by_id: 1,
  657. )
  658. group2 = Group.create!(
  659. name: 'Group with auto responder',
  660. active: true,
  661. email_address: EmailAddress.first,
  662. created_by_id: 1,
  663. updated_by_id: 1,
  664. )
  665. Trigger.create!(
  666. name: "002 - move ticket to #{group2.name}",
  667. condition: {
  668. 'ticket.action' => {
  669. 'operator' => 'is',
  670. 'value' => 'create',
  671. },
  672. 'ticket.group_id' => {
  673. 'operator' => 'is',
  674. 'value' => group1.id.to_s,
  675. },
  676. 'ticket.organization_id' => {
  677. 'operator' => 'is',
  678. 'pre_condition' => 'specific',
  679. 'value' => User.lookup(email: 'nicole.braun@zammad.org').organization_id.to_s,
  680. }
  681. },
  682. perform: {
  683. 'ticket.group_id' => {
  684. 'value' => group2.id.to_s,
  685. },
  686. },
  687. disable_notification: true,
  688. active: true,
  689. created_by_id: 1,
  690. updated_by_id: 1,
  691. )
  692. Trigger.create_or_update(
  693. name: "001 auto reply for tickets in group #{group1.name}",
  694. condition: {
  695. 'ticket.action' => {
  696. 'operator' => 'is',
  697. 'value' => 'create',
  698. },
  699. 'ticket.state_id' => {
  700. 'operator' => 'is',
  701. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  702. },
  703. 'ticket.group_id' => {
  704. 'operator' => 'is not',
  705. 'value' => group1.id.to_s,
  706. },
  707. },
  708. perform: {
  709. 'notification.email' => {
  710. 'body' => "some text<br>\#{ticket.customer.lastname}<br>\#{ticket.title}<br>\#{article.body}",
  711. 'recipient' => 'ticket_customer',
  712. 'subject' => "Thanks for your inquiry (\#{ticket.title})!",
  713. },
  714. 'ticket.priority_id' => {
  715. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  716. },
  717. 'ticket.tags' => {
  718. 'operator' => 'add',
  719. 'value' => 'aa, kk',
  720. },
  721. },
  722. disable_notification: true,
  723. active: true,
  724. created_by_id: 1,
  725. updated_by_id: 1,
  726. )
  727. ticket1 = Ticket.create!(
  728. title: "some <b>title</b>\n äöüß",
  729. group: group1,
  730. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  731. updated_by_id: 1,
  732. created_by_id: 1,
  733. )
  734. Ticket::Article.create!(
  735. ticket_id: ticket1.id,
  736. from: 'some_sender@example.com',
  737. to: 'some_recipient@example.com',
  738. subject: 'some subject',
  739. message_id: 'some@id',
  740. body: "some message <b>note</b>\nnew line",
  741. internal: false,
  742. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  743. type: Ticket::Article::Type.find_by(name: 'web'),
  744. updated_by_id: 1,
  745. created_by_id: 1,
  746. )
  747. ticket1.reload
  748. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  749. assert_equal('Group dispatch', ticket1.group.name, 'ticket1.group verify')
  750. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  751. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  752. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  753. assert_equal([], ticket1.tag_list)
  754. TransactionDispatcher.commit
  755. ticket1.reload
  756. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  757. assert_equal('Group with auto responder', ticket1.group.name, 'ticket1.group verify')
  758. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  759. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  760. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  761. assert_equal(%w[aa kk], ticket1.tag_list)
  762. email_raw = "From: nicole.braun@zammad.org
  763. To: zammad@example.com
  764. Subject: test 1
  765. X-Zammad-Ticket-Group: #{group1.name}
  766. test 1"
  767. ticket2, _article2, _user2 = Channel::EmailParser.new.process({ trusted: true }, email_raw)
  768. assert_equal('test 1', ticket2.title, 'ticket2.title verify')
  769. assert_equal('Group with auto responder', ticket2.group.name, 'ticket2.group verify')
  770. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  771. assert_equal('3 high', ticket2.priority.name, 'ticket2.priority verify')
  772. assert_equal(2, ticket2.articles.count, 'ticket2.articles verify')
  773. assert_equal(%w[aa kk], ticket2.tag_list)
  774. end
  775. end