ticket_trigger_extended_recursive_disabled_test.rb 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. require 'test_helper'
  2. class TicketTriggerExtendedRecursiveDisabledTest < ActiveSupport::TestCase
  3. setup do
  4. Setting.set('ticket_trigger_recursive', false)
  5. end
  6. test 'recursive trigger' do
  7. trigger1 = 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. trigger2 = 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. trigger1 = 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. trigger2 = 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. trigger3 = 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. trigger1 = 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. trigger2 = 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. trigger3 = 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. trigger1 = 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. trigger2 = 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. trigger3 = 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('open', 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. trigger1 = 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. trigger2 = 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. trigger3 = 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('2 normal', ticket_p.priority.name)
  373. assert_equal('open', 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. trigger1 = 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. trigger2 = 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. article_inbound1 = 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(group3.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(0, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id)
  514. end
  515. test 'recursive trigger loop check' do
  516. trigger0 = Trigger.create!(
  517. name: '000',
  518. condition: {
  519. 'ticket.action' => {
  520. 'operator' => 'is',
  521. 'value' => 'create',
  522. },
  523. 'ticket.priority_id' => {
  524. 'operator' => 'is',
  525. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  526. },
  527. },
  528. perform: {
  529. 'ticket.state_id' => {
  530. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  531. },
  532. },
  533. disable_notification: true,
  534. active: true,
  535. created_by_id: 1,
  536. updated_by_id: 1,
  537. )
  538. trigger1 = Trigger.create!(
  539. name: '001',
  540. condition: {
  541. 'ticket.action' => {
  542. 'operator' => 'is',
  543. 'value' => 'create',
  544. },
  545. 'ticket.priority_id' => {
  546. 'operator' => 'is',
  547. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  548. },
  549. },
  550. perform: {
  551. 'ticket.priority_id' => {
  552. 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s,
  553. },
  554. },
  555. disable_notification: true,
  556. active: true,
  557. created_by_id: 1,
  558. updated_by_id: 1,
  559. )
  560. trigger2 = Trigger.create!(
  561. name: '002',
  562. condition: {
  563. 'ticket.action' => {
  564. 'operator' => 'is',
  565. 'value' => 'create',
  566. },
  567. 'ticket.priority_id' => {
  568. 'operator' => 'is',
  569. 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s,
  570. },
  571. },
  572. perform: {
  573. 'ticket.priority_id' => {
  574. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  575. },
  576. },
  577. disable_notification: true,
  578. active: true,
  579. created_by_id: 1,
  580. updated_by_id: 1,
  581. )
  582. group1 = Group.find_by(name: 'Users')
  583. ticket1 = Ticket.create!(
  584. title: '123',
  585. group: group1,
  586. customer_id: 2,
  587. state: Ticket::State.lookup(name: 'new'),
  588. priority: Ticket::Priority.lookup(name: '2 normal'),
  589. updated_by_id: 1,
  590. created_by_id: 1,
  591. )
  592. assert(ticket1)
  593. assert_equal(ticket1.title, '123')
  594. assert_equal(ticket1.group.name, group1.name)
  595. assert_equal(ticket1.state.name, 'new')
  596. article_inbound1 = Ticket::Article.create!(
  597. ticket_id: ticket1.id,
  598. from: 'some_sender@example.com',
  599. to: 'some_recipient@example.com',
  600. subject: 'some subject',
  601. message_id: 'some@id',
  602. body: 'some message',
  603. internal: false,
  604. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  605. type: Ticket::Article::Type.find_by(name: 'email'),
  606. updated_by_id: 1,
  607. created_by_id: 1,
  608. )
  609. Observer::Transaction.commit
  610. Scheduler.worker(true)
  611. ticket1.reload
  612. assert_equal('123', ticket1.title)
  613. assert_equal('new', ticket1.state.name)
  614. assert_equal('3 high', ticket1.priority.name)
  615. assert_equal(1, ticket1.articles.count)
  616. end
  617. end