ticket_trigger_extended_test.rb 25 KB

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