conditions_spec.rb 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. require 'models/core_workflow/base'
  4. RSpec.describe 'CoreWorkflow > Conditions', mariadb: true, type: :model do
  5. include_context 'with core workflow base'
  6. describe '.perform - Condition - today' do
  7. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  8. let(:payload) do
  9. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  10. end
  11. context 'when match' do
  12. before do
  13. travel_to DateTime.new 2023, 7, 21, 7, 0
  14. end
  15. let!(:workflow) do
  16. create(:core_workflow,
  17. object: 'Ticket',
  18. condition_selected: {
  19. 'ticket.created_at': {
  20. operator: 'today',
  21. },
  22. })
  23. end
  24. it 'does match' do
  25. expect(result[:matched_workflows]).to include(workflow.id)
  26. end
  27. end
  28. context 'when mismatch' do
  29. before do
  30. travel_to DateTime.new 2023, 8, 21, 7, 0
  31. end
  32. let!(:workflow) do
  33. create(:core_workflow,
  34. object: 'Ticket',
  35. condition_selected: {
  36. 'ticket.created_at': {
  37. operator: 'today',
  38. },
  39. })
  40. end
  41. it 'does not match' do
  42. expect(result[:matched_workflows]).not_to include(workflow.id)
  43. end
  44. end
  45. end
  46. describe '.perform - Condition - after (absolute)' do
  47. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  48. let(:payload) do
  49. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  50. end
  51. context 'when match' do
  52. let!(:workflow) do
  53. create(:core_workflow,
  54. object: 'Ticket',
  55. condition_selected: {
  56. 'ticket.created_at': {
  57. operator: 'after (absolute)',
  58. value: DateTime.new(2023, 7, 21, 9, 0).to_s
  59. },
  60. })
  61. end
  62. it 'does match' do
  63. expect(result[:matched_workflows]).to include(workflow.id)
  64. end
  65. end
  66. context 'when mismatch' do
  67. let!(:workflow) do
  68. create(:core_workflow,
  69. object: 'Ticket',
  70. condition_selected: {
  71. 'ticket.created_at': {
  72. operator: 'after (absolute)',
  73. value: DateTime.new(2023, 7, 21, 11, 0).to_s
  74. },
  75. })
  76. end
  77. it 'does not match' do
  78. expect(result[:matched_workflows]).not_to include(workflow.id)
  79. end
  80. end
  81. end
  82. describe '.perform - Condition - after (relative)' do
  83. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  84. let(:payload) do
  85. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  86. end
  87. before do
  88. travel_to DateTime.new 2023, 7, 21, 7, 0
  89. end
  90. context 'when match' do
  91. let!(:workflow) do
  92. create(:core_workflow,
  93. object: 'Ticket',
  94. condition_selected: {
  95. 'ticket.created_at': {
  96. operator: 'after (relative)',
  97. range: 'hour',
  98. value: '1',
  99. },
  100. })
  101. end
  102. it 'does match' do
  103. expect(result[:matched_workflows]).to include(workflow.id)
  104. end
  105. end
  106. context 'when mismatch' do
  107. let!(:workflow) do
  108. create(:core_workflow,
  109. object: 'Ticket',
  110. condition_selected: {
  111. 'ticket.created_at': {
  112. operator: 'after (relative)',
  113. range: 'hour',
  114. value: '4',
  115. },
  116. })
  117. end
  118. it 'does not match' do
  119. expect(result[:matched_workflows]).not_to include(workflow.id)
  120. end
  121. end
  122. end
  123. describe '.perform - Condition - before (absolute)' do
  124. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  125. let(:payload) do
  126. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  127. end
  128. context 'when match' do
  129. let!(:workflow) do
  130. create(:core_workflow,
  131. object: 'Ticket',
  132. condition_selected: {
  133. 'ticket.created_at': {
  134. operator: 'before (absolute)',
  135. value: DateTime.new(2023, 7, 21, 11, 0).to_s
  136. },
  137. })
  138. end
  139. it 'does match' do
  140. expect(result[:matched_workflows]).to include(workflow.id)
  141. end
  142. end
  143. context 'when mismatch' do
  144. let!(:workflow) do
  145. create(:core_workflow,
  146. object: 'Ticket',
  147. condition_selected: {
  148. 'ticket.created_at': {
  149. operator: 'before (absolute)',
  150. value: DateTime.new(2023, 7, 21, 9, 0).to_s
  151. },
  152. })
  153. end
  154. it 'does not match' do
  155. expect(result[:matched_workflows]).not_to include(workflow.id)
  156. end
  157. end
  158. end
  159. describe '.perform - Condition - before (relative)' do
  160. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  161. let(:payload) do
  162. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  163. end
  164. before do
  165. travel_to DateTime.new 2023, 7, 21, 12, 0
  166. end
  167. context 'when match' do
  168. let!(:workflow) do
  169. create(:core_workflow,
  170. object: 'Ticket',
  171. condition_selected: {
  172. 'ticket.created_at': {
  173. operator: 'before (relative)',
  174. range: 'hour',
  175. value: '1',
  176. },
  177. })
  178. end
  179. it 'does match' do
  180. expect(result[:matched_workflows]).to include(workflow.id)
  181. end
  182. end
  183. context 'when mismatch' do
  184. let!(:workflow) do
  185. create(:core_workflow,
  186. object: 'Ticket',
  187. condition_selected: {
  188. 'ticket.created_at': {
  189. operator: 'before (relative)',
  190. range: 'hour',
  191. value: '4',
  192. },
  193. })
  194. end
  195. it 'does not match' do
  196. expect(result[:matched_workflows]).not_to include(workflow.id)
  197. end
  198. end
  199. context 'when empty' do
  200. let(:payload) do
  201. base_payload
  202. end
  203. let!(:workflow) do
  204. create(:core_workflow,
  205. object: 'Ticket',
  206. condition_selected: {
  207. 'ticket.created_at': {
  208. operator: 'before (relative)',
  209. range: 'month',
  210. value: '1',
  211. },
  212. })
  213. end
  214. it 'does not match' do
  215. expect(result[:matched_workflows]).not_to include(workflow.id)
  216. end
  217. end
  218. end
  219. describe '.perform - Condition - from (relative)' do
  220. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  221. let(:payload) do
  222. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  223. end
  224. before do
  225. travel_to DateTime.new 2023, 7, 21, 12, 0
  226. end
  227. context 'when match' do
  228. let!(:workflow) do
  229. create(:core_workflow,
  230. object: 'Ticket',
  231. condition_selected: {
  232. 'ticket.created_at': {
  233. operator: 'from (relative)',
  234. range: 'hour',
  235. value: '4',
  236. },
  237. })
  238. end
  239. it 'does match' do
  240. expect(result[:matched_workflows]).to include(workflow.id)
  241. end
  242. end
  243. context 'when mismatch' do
  244. let!(:workflow) do
  245. create(:core_workflow,
  246. object: 'Ticket',
  247. condition_selected: {
  248. 'ticket.created_at': {
  249. operator: 'from (relative)',
  250. range: 'hour',
  251. value: '1',
  252. },
  253. })
  254. end
  255. it 'does not match' do
  256. expect(result[:matched_workflows]).not_to include(workflow.id)
  257. end
  258. end
  259. end
  260. describe '.perform - Condition - till (relative)' do
  261. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  262. let(:payload) do
  263. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  264. end
  265. before do
  266. travel_to DateTime.new 2023, 7, 21, 8, 0
  267. end
  268. context 'when match' do
  269. let!(:workflow) do
  270. create(:core_workflow,
  271. object: 'Ticket',
  272. condition_selected: {
  273. 'ticket.created_at': {
  274. operator: 'till (relative)',
  275. range: 'hour',
  276. value: '4',
  277. },
  278. })
  279. end
  280. it 'does match' do
  281. expect(result[:matched_workflows]).to include(workflow.id)
  282. end
  283. end
  284. context 'when mismatch' do
  285. let!(:workflow) do
  286. create(:core_workflow,
  287. object: 'Ticket',
  288. condition_selected: {
  289. 'ticket.created_at': {
  290. operator: 'till (relative)',
  291. range: 'hour',
  292. value: '1',
  293. },
  294. })
  295. end
  296. it 'does not match' do
  297. expect(result[:matched_workflows]).not_to include(workflow.id)
  298. end
  299. end
  300. end
  301. describe '.perform - Condition - within last (relative)' do
  302. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  303. let(:payload) do
  304. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  305. end
  306. before do
  307. travel_to DateTime.new 2023, 7, 21, 12, 0
  308. end
  309. context 'when match' do
  310. let!(:workflow) do
  311. create(:core_workflow,
  312. object: 'Ticket',
  313. condition_selected: {
  314. 'ticket.created_at': {
  315. operator: 'within last (relative)',
  316. range: 'hour',
  317. value: '4',
  318. },
  319. })
  320. end
  321. it 'does match' do
  322. expect(result[:matched_workflows]).to include(workflow.id)
  323. end
  324. end
  325. context 'when mismatch' do
  326. let!(:workflow) do
  327. create(:core_workflow,
  328. object: 'Ticket',
  329. condition_selected: {
  330. 'ticket.created_at': {
  331. operator: 'within last (relative)',
  332. range: 'hour',
  333. value: '1',
  334. },
  335. })
  336. end
  337. it 'does not match' do
  338. expect(result[:matched_workflows]).not_to include(workflow.id)
  339. end
  340. end
  341. end
  342. describe '.perform - Condition - within next (relative)' do
  343. let(:ticket_created_at) { DateTime.new 2023, 7, 21, 10, 0 }
  344. let(:payload) do
  345. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  346. end
  347. before do
  348. travel_to DateTime.new 2023, 7, 21, 8, 0
  349. end
  350. context 'when match' do
  351. let!(:workflow) do
  352. create(:core_workflow,
  353. object: 'Ticket',
  354. condition_selected: {
  355. 'ticket.created_at': {
  356. operator: 'within next (relative)',
  357. range: 'hour',
  358. value: '4',
  359. },
  360. })
  361. end
  362. it 'does match' do
  363. expect(result[:matched_workflows]).to include(workflow.id)
  364. end
  365. end
  366. context 'when mismatch' do
  367. let!(:workflow) do
  368. create(:core_workflow,
  369. object: 'Ticket',
  370. condition_selected: {
  371. 'ticket.created_at': {
  372. operator: 'within next (relative)',
  373. range: 'hour',
  374. value: '1',
  375. },
  376. })
  377. end
  378. it 'does not match' do
  379. expect(result[:matched_workflows]).not_to include(workflow.id)
  380. end
  381. end
  382. end
  383. describe '.perform - Condition - Mention User IDs' do
  384. let(:payload) do
  385. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  386. end
  387. before do
  388. ticket.mentions.create!(user: action_user, created_by_id: 1, updated_by_id: 1)
  389. end
  390. context 'when pre_condtion specific' do
  391. context 'when match' do
  392. let!(:workflow) do
  393. create(:core_workflow,
  394. object: 'Ticket',
  395. condition_selected: {
  396. 'ticket.mention_user_ids': {
  397. pre_condition: 'specific',
  398. operator: 'is',
  399. value: [action_user.id.to_s],
  400. },
  401. })
  402. end
  403. it 'does match' do
  404. expect(result[:matched_workflows]).to include(workflow.id)
  405. end
  406. end
  407. context 'when mismatch' do
  408. let!(:workflow) do
  409. create(:core_workflow,
  410. object: 'Ticket',
  411. condition_selected: {
  412. 'ticket.mention_user_ids': {
  413. pre_condition: 'specific',
  414. operator: 'is',
  415. value: ['999'],
  416. },
  417. })
  418. end
  419. it 'does not match' do
  420. expect(result[:matched_workflows]).not_to include(workflow.id)
  421. end
  422. end
  423. end
  424. context 'when pre_condtion current_user.id' do
  425. context 'when match' do
  426. let!(:workflow) do
  427. create(:core_workflow,
  428. object: 'Ticket',
  429. condition_selected: {
  430. 'ticket.mention_user_ids': {
  431. pre_condition: 'current_user.id',
  432. operator: 'is',
  433. value: [],
  434. },
  435. })
  436. end
  437. it 'does match' do
  438. expect(result[:matched_workflows]).to include(workflow.id)
  439. end
  440. end
  441. context 'when mismatch' do
  442. let!(:workflow) do
  443. create(:core_workflow,
  444. object: 'Ticket',
  445. condition_selected: {
  446. 'ticket.mention_user_ids': {
  447. pre_condition: 'current_user.id',
  448. operator: 'is',
  449. value: [],
  450. },
  451. })
  452. end
  453. before do
  454. ticket.mentions.destroy_all
  455. end
  456. it 'does not match' do
  457. expect(result[:matched_workflows]).not_to include(workflow.id)
  458. end
  459. end
  460. end
  461. context 'when pre_condtion not_set' do
  462. context 'when match' do
  463. let!(:workflow) do
  464. create(:core_workflow,
  465. object: 'Ticket',
  466. condition_selected: {
  467. 'ticket.mention_user_ids': {
  468. pre_condition: 'not_set',
  469. operator: 'is',
  470. value: [],
  471. },
  472. })
  473. end
  474. before do
  475. ticket.mentions.destroy_all
  476. end
  477. it 'does match' do
  478. expect(result[:matched_workflows]).to include(workflow.id)
  479. end
  480. end
  481. context 'when mismatch' do
  482. let!(:workflow) do
  483. create(:core_workflow,
  484. object: 'Ticket',
  485. condition_selected: {
  486. 'ticket.mention_user_ids': {
  487. pre_condition: 'not_set',
  488. operator: 'is',
  489. value: [],
  490. },
  491. })
  492. end
  493. it 'does not match' do
  494. expect(result[:matched_workflows]).not_to include(workflow.id)
  495. end
  496. end
  497. end
  498. end
  499. describe '.perform - Condition - Tags' do
  500. context 'when create' do
  501. context 'when match' do
  502. let(:payload) do
  503. base_payload.merge('params' => { 'tags' => 'special, special2' })
  504. end
  505. let!(:workflow) do
  506. create(:core_workflow,
  507. object: 'Ticket',
  508. condition_selected: {
  509. 'ticket.tags': {
  510. operator: 'contains all',
  511. value: ['special'],
  512. },
  513. })
  514. end
  515. it 'does match' do
  516. expect(result[:matched_workflows]).to include(workflow.id)
  517. end
  518. end
  519. context 'when mismatch' do
  520. let(:payload) do
  521. base_payload.merge('params' => { 'tags' => 'special' }, 'screen' => 'create')
  522. end
  523. let!(:workflow) do
  524. create(:core_workflow,
  525. object: 'Ticket',
  526. condition_selected: {
  527. 'ticket.tags': {
  528. operator: 'contains all',
  529. value: ['NOPE'],
  530. },
  531. })
  532. end
  533. it 'does not match' do
  534. expect(result[:matched_workflows]).not_to include(workflow.id)
  535. end
  536. end
  537. end
  538. context 'when edit' do
  539. let(:payload) do
  540. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  541. end
  542. before do
  543. ticket.tag_add('special', 1)
  544. end
  545. context 'when match' do
  546. let!(:workflow) do
  547. create(:core_workflow,
  548. object: 'Ticket',
  549. condition_selected: {
  550. 'ticket.tags': {
  551. operator: 'contains all',
  552. value: ['special'],
  553. },
  554. })
  555. end
  556. it 'does match' do
  557. expect(result[:matched_workflows]).to include(workflow.id)
  558. end
  559. end
  560. context 'when mismatch' do
  561. let!(:workflow) do
  562. create(:core_workflow,
  563. object: 'Ticket',
  564. condition_selected: {
  565. 'ticket.tags': {
  566. operator: 'contains all',
  567. value: ['NOPE'],
  568. },
  569. })
  570. end
  571. it 'does not match' do
  572. expect(result[:matched_workflows]).not_to include(workflow.id)
  573. end
  574. end
  575. end
  576. context 'when mobile create' do
  577. context 'when match' do
  578. let(:payload) do
  579. base_payload.merge('params' => { 'tags' => %w[special special2] })
  580. end
  581. let!(:workflow) do
  582. create(:core_workflow,
  583. object: 'Ticket',
  584. condition_selected: {
  585. 'ticket.tags': {
  586. operator: 'contains all',
  587. value: ['special'],
  588. },
  589. })
  590. end
  591. it 'does match' do
  592. expect(result[:matched_workflows]).to include(workflow.id)
  593. end
  594. end
  595. context 'when mismatch' do
  596. let(:payload) do
  597. base_payload.merge('params' => { 'tags' => ['special'] }, 'screen' => 'create')
  598. end
  599. let!(:workflow) do
  600. create(:core_workflow,
  601. object: 'Ticket',
  602. condition_selected: {
  603. 'ticket.tags': {
  604. operator: 'contains all',
  605. value: ['NOPE'],
  606. },
  607. })
  608. end
  609. it 'does not match' do
  610. expect(result[:matched_workflows]).not_to include(workflow.id)
  611. end
  612. end
  613. end
  614. context 'when contains one' do
  615. context 'when match' do
  616. let(:payload) do
  617. base_payload.merge('params' => { 'tags' => 'special, special2' })
  618. end
  619. let!(:workflow) do
  620. create(:core_workflow,
  621. object: 'Ticket',
  622. condition_selected: {
  623. 'ticket.tags': {
  624. operator: 'contains one',
  625. value: ['special'],
  626. },
  627. })
  628. end
  629. it 'does match' do
  630. expect(result[:matched_workflows]).to include(workflow.id)
  631. end
  632. end
  633. context 'when mismatch' do
  634. let(:payload) do
  635. base_payload.merge('params' => { 'tags' => 'special' }, 'screen' => 'create')
  636. end
  637. let!(:workflow) do
  638. create(:core_workflow,
  639. object: 'Ticket',
  640. condition_selected: {
  641. 'ticket.tags': {
  642. operator: 'contains one',
  643. value: ['NOPE'],
  644. },
  645. })
  646. end
  647. it 'does not match' do
  648. expect(result[:matched_workflows]).not_to include(workflow.id)
  649. end
  650. end
  651. end
  652. end
  653. describe '.perform - Condition - Article' do
  654. let(:payload) do
  655. base_payload.merge('params' => { 'article' => {
  656. body: 'hello',
  657. type: 'note',
  658. internal: true,
  659. form_id: SecureRandom.uuid,
  660. shared_draft_id: '',
  661. subtype: '',
  662. in_reply_to: '',
  663. to: '',
  664. cc: '',
  665. subject: '',
  666. from: 'Test Admin Agent',
  667. ticket_id: 5,
  668. content_type: 'text/html',
  669. sender_id: 1,
  670. type_id: 10
  671. } })
  672. end
  673. context 'when match' do
  674. let!(:workflow) do
  675. create(:core_workflow,
  676. object: 'Ticket',
  677. condition_selected: {
  678. 'article.body': {
  679. operator: 'matches regex',
  680. value: 'hello',
  681. },
  682. })
  683. end
  684. it 'does match' do
  685. expect(result[:matched_workflows]).to include(workflow.id)
  686. end
  687. end
  688. context 'when mismatch' do
  689. let!(:workflow) do
  690. create(:core_workflow,
  691. object: 'Ticket',
  692. condition_selected: {
  693. 'article.body': {
  694. operator: 'matches regex',
  695. value: 'NOPE',
  696. },
  697. })
  698. end
  699. it 'does not match' do
  700. expect(result[:matched_workflows]).not_to include(workflow.id)
  701. end
  702. end
  703. end
  704. describe '.perform - Condition - owner_id not set' do
  705. let!(:workflow) do
  706. create(:core_workflow,
  707. object: 'Ticket',
  708. condition_selected: {
  709. 'ticket.owner_id': {
  710. operator: 'not_set',
  711. },
  712. })
  713. end
  714. it 'does match' do
  715. expect(result[:matched_workflows]).to include(workflow.id)
  716. end
  717. describe 'for owner id 1' do
  718. let(:payload) do
  719. base_payload.merge(
  720. 'params' => { 'owner_id' => '1' },
  721. )
  722. end
  723. it 'does match' do
  724. expect(result[:matched_workflows]).to include(workflow.id)
  725. end
  726. end
  727. end
  728. describe '.perform - Condition - session.role_ids' do
  729. let!(:workflow) do
  730. create(:core_workflow,
  731. object: 'Ticket',
  732. condition_selected: {
  733. 'session.role_ids': {
  734. operator: 'is',
  735. value: [ Role.find_by(name: 'Agent').id.to_s ],
  736. },
  737. })
  738. end
  739. it 'does match' do
  740. expect(result[:matched_workflows]).to include(workflow.id)
  741. end
  742. describe 'for customer' do
  743. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  744. it 'does not match' do
  745. expect(result[:matched_workflows]).not_to include(workflow.id)
  746. end
  747. end
  748. end
  749. describe '.perform - Condition - session.group_ids_full' do
  750. let!(:workflow) do
  751. create(:core_workflow,
  752. object: 'Ticket',
  753. condition_selected: {
  754. 'session.group_ids_full': {
  755. operator: 'is',
  756. value: [ ticket.group.id.to_s ],
  757. },
  758. })
  759. end
  760. it 'does match' do
  761. expect(result[:matched_workflows]).to include(workflow.id)
  762. end
  763. describe 'for customer' do
  764. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  765. it 'does not match' do
  766. expect(result[:matched_workflows]).not_to include(workflow.id)
  767. end
  768. end
  769. end
  770. describe '.perform - Condition - session.group_ids_change' do
  771. let!(:workflow) do
  772. create(:core_workflow,
  773. object: 'Ticket',
  774. condition_selected: {
  775. 'session.group_ids_change': {
  776. operator: 'is',
  777. value: [ ticket.group.id.to_s ],
  778. },
  779. })
  780. end
  781. it 'does match' do
  782. expect(result[:matched_workflows]).to include(workflow.id)
  783. end
  784. describe 'for customer' do
  785. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  786. it 'does not match' do
  787. expect(result[:matched_workflows]).not_to include(workflow.id)
  788. end
  789. end
  790. end
  791. describe '.perform - Condition - session.group_ids_read' do
  792. let!(:workflow) do
  793. create(:core_workflow,
  794. object: 'Ticket',
  795. condition_selected: {
  796. 'session.group_ids_read': {
  797. operator: 'is',
  798. value: [ ticket.group.id.to_s ],
  799. },
  800. })
  801. end
  802. it 'does match' do
  803. expect(result[:matched_workflows]).to include(workflow.id)
  804. end
  805. describe 'for customer' do
  806. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  807. it 'does not match' do
  808. expect(result[:matched_workflows]).not_to include(workflow.id)
  809. end
  810. end
  811. end
  812. describe '.perform - Condition - session.group_ids_overview' do
  813. let!(:workflow) do
  814. create(:core_workflow,
  815. object: 'Ticket',
  816. condition_selected: {
  817. 'session.group_ids_overview': {
  818. operator: 'is',
  819. value: [ ticket.group.id.to_s ],
  820. },
  821. })
  822. end
  823. it 'does match' do
  824. expect(result[:matched_workflows]).to include(workflow.id)
  825. end
  826. describe 'for customer' do
  827. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  828. it 'does not match' do
  829. expect(result[:matched_workflows]).not_to include(workflow.id)
  830. end
  831. end
  832. end
  833. describe '.perform - Condition - session.group_ids_create' do
  834. let!(:workflow) do
  835. create(:core_workflow,
  836. object: 'Ticket',
  837. condition_selected: {
  838. 'session.group_ids_create': {
  839. operator: 'is',
  840. value: [ ticket.group.id.to_s ],
  841. },
  842. })
  843. end
  844. it 'does match' do
  845. expect(result[:matched_workflows]).to include(workflow.id)
  846. end
  847. describe 'for customer' do
  848. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  849. it 'does not match' do
  850. expect(result[:matched_workflows]).not_to include(workflow.id)
  851. end
  852. end
  853. end
  854. describe '.perform - Condition - session.permission_ids' do
  855. let!(:workflow) do
  856. create(:core_workflow,
  857. object: 'Ticket',
  858. condition_selected: {
  859. 'session.permission_ids': {
  860. operator: 'is',
  861. value: [ Permission.find_by(name: 'ticket.agent').id.to_s ],
  862. },
  863. })
  864. end
  865. it 'does match' do
  866. expect(result[:matched_workflows]).to include(workflow.id)
  867. end
  868. describe 'for customer' do
  869. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  870. it 'does not match' do
  871. expect(result[:matched_workflows]).not_to include(workflow.id)
  872. end
  873. end
  874. end
  875. describe '.perform - Condition - Matches regex' do
  876. let(:payload) do
  877. base_payload.merge(
  878. 'params' => { 'title' => 'workflow ticket' },
  879. )
  880. end
  881. let!(:workflow) do
  882. create(:core_workflow,
  883. object: 'Ticket',
  884. condition_selected: {
  885. 'ticket.title': {
  886. operator: 'matches regex',
  887. value: [ '^workflow' ],
  888. },
  889. })
  890. end
  891. it 'does match' do
  892. expect(result[:matched_workflows]).to include(workflow.id)
  893. end
  894. describe 'for invalid regex' do
  895. let!(:workflow) do
  896. create(:core_workflow,
  897. object: 'Ticket',
  898. condition_selected: {
  899. 'ticket.title': {
  900. operator: 'matches regex',
  901. value: [ '^workfluw' ],
  902. },
  903. })
  904. end
  905. it 'does not match' do
  906. expect(result[:matched_workflows]).not_to include(workflow.id)
  907. end
  908. end
  909. end
  910. describe '.perform - Condition - Does not match regex' do
  911. let(:payload) do
  912. base_payload.merge(
  913. 'params' => { 'title' => 'workflow ticket' },
  914. )
  915. end
  916. let!(:workflow) do
  917. create(:core_workflow,
  918. object: 'Ticket',
  919. condition_selected: {
  920. 'ticket.title': {
  921. operator: 'does not match regex',
  922. value: [ '^workfluw' ],
  923. },
  924. })
  925. end
  926. it 'does match' do
  927. expect(result[:matched_workflows]).to include(workflow.id)
  928. end
  929. describe 'for invalid regex' do
  930. let!(:workflow) do
  931. create(:core_workflow,
  932. object: 'Ticket',
  933. condition_selected: {
  934. 'ticket.title': {
  935. operator: 'does not match regex',
  936. value: [ '^workflow' ],
  937. },
  938. })
  939. end
  940. it 'does not match' do
  941. expect(result[:matched_workflows]).not_to include(workflow.id)
  942. end
  943. end
  944. end
  945. describe '.perform - Condition - Contains', db_strategy: :reset do
  946. let(:field_name) { SecureRandom.uuid }
  947. let!(:workflow) do
  948. create(:core_workflow,
  949. object: 'Ticket',
  950. condition_selected: {
  951. "ticket.#{field_name}": {
  952. operator: 'contains',
  953. value: %w[key_1 key_2],
  954. },
  955. })
  956. end
  957. before do
  958. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  959. ObjectManager::Attribute.migration_execute
  960. end
  961. context 'when empty' do
  962. let(:payload) do
  963. base_payload.merge(
  964. 'params' => { field_name => [] },
  965. )
  966. end
  967. it 'does not match' do
  968. expect(result[:matched_workflows]).not_to include(workflow.id)
  969. end
  970. end
  971. context 'when same value' do
  972. let(:payload) do
  973. base_payload.merge(
  974. 'params' => { field_name => %w[key_1 key_2] },
  975. )
  976. end
  977. it 'does match' do
  978. expect(result[:matched_workflows]).to include(workflow.id)
  979. end
  980. end
  981. context 'when 50% value' do
  982. let(:payload) do
  983. base_payload.merge(
  984. 'params' => { field_name => ['key_1'] },
  985. )
  986. end
  987. it 'does match' do
  988. expect(result[:matched_workflows]).to include(workflow.id)
  989. end
  990. end
  991. context 'when value differs' do
  992. let(:payload) do
  993. base_payload.merge(
  994. 'params' => { field_name => ['key_3'] },
  995. )
  996. end
  997. it 'does not match' do
  998. expect(result[:matched_workflows]).not_to include(workflow.id)
  999. end
  1000. end
  1001. end
  1002. describe '.perform - Condition - Contains not', db_strategy: :reset do
  1003. let(:field_name) { SecureRandom.uuid }
  1004. let!(:workflow) do
  1005. create(:core_workflow,
  1006. object: 'Ticket',
  1007. condition_selected: {
  1008. "ticket.#{field_name}": {
  1009. operator: 'contains not',
  1010. value: %w[key_1 key_2],
  1011. },
  1012. })
  1013. end
  1014. before do
  1015. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  1016. ObjectManager::Attribute.migration_execute
  1017. end
  1018. context 'when empty' do
  1019. let(:payload) do
  1020. base_payload.merge(
  1021. 'params' => { field_name => [] },
  1022. )
  1023. end
  1024. it 'does match' do
  1025. expect(result[:matched_workflows]).to include(workflow.id)
  1026. end
  1027. end
  1028. context 'when same value' do
  1029. let(:payload) do
  1030. base_payload.merge(
  1031. 'params' => { field_name => %w[key_1 key_2] },
  1032. )
  1033. end
  1034. it 'does not match' do
  1035. expect(result[:matched_workflows]).not_to include(workflow.id)
  1036. end
  1037. end
  1038. context 'when 50% value' do
  1039. let(:payload) do
  1040. base_payload.merge(
  1041. 'params' => { field_name => ['key_1'] },
  1042. )
  1043. end
  1044. it 'does not match' do
  1045. expect(result[:matched_workflows]).not_to include(workflow.id)
  1046. end
  1047. end
  1048. context 'when value differs' do
  1049. let(:payload) do
  1050. base_payload.merge(
  1051. 'params' => { field_name => ['key_3'] },
  1052. )
  1053. end
  1054. it 'does match' do
  1055. expect(result[:matched_workflows]).to include(workflow.id)
  1056. end
  1057. end
  1058. end
  1059. describe '.perform - Condition - Contains all', db_strategy: :reset do
  1060. let(:field_name) { SecureRandom.uuid }
  1061. let!(:workflow) do
  1062. create(:core_workflow,
  1063. object: 'Ticket',
  1064. condition_selected: {
  1065. "ticket.#{field_name}": {
  1066. operator: 'contains all',
  1067. value: %w[key_1 key_2],
  1068. },
  1069. })
  1070. end
  1071. before do
  1072. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  1073. ObjectManager::Attribute.migration_execute
  1074. end
  1075. context 'when empty' do
  1076. let(:payload) do
  1077. base_payload.merge(
  1078. 'params' => { field_name => [] },
  1079. )
  1080. end
  1081. it 'does not match' do
  1082. expect(result[:matched_workflows]).not_to include(workflow.id)
  1083. end
  1084. end
  1085. context 'when same value' do
  1086. let(:payload) do
  1087. base_payload.merge(
  1088. 'params' => { field_name => %w[key_1 key_2] },
  1089. )
  1090. end
  1091. it 'does match' do
  1092. expect(result[:matched_workflows]).to include(workflow.id)
  1093. end
  1094. end
  1095. context 'when 50% value' do
  1096. let(:payload) do
  1097. base_payload.merge(
  1098. 'params' => { field_name => ['key_1'] },
  1099. )
  1100. end
  1101. it 'does not match' do
  1102. expect(result[:matched_workflows]).not_to include(workflow.id)
  1103. end
  1104. end
  1105. context 'when value differs' do
  1106. let(:payload) do
  1107. base_payload.merge(
  1108. 'params' => { field_name => ['key_3'] },
  1109. )
  1110. end
  1111. it 'does not match' do
  1112. expect(result[:matched_workflows]).not_to include(workflow.id)
  1113. end
  1114. end
  1115. end
  1116. describe '.perform - Condition - Contains all not', db_strategy: :reset do
  1117. let(:field_name) { SecureRandom.uuid }
  1118. let!(:workflow) do
  1119. create(:core_workflow,
  1120. object: 'Ticket',
  1121. condition_selected: {
  1122. "ticket.#{field_name}": {
  1123. operator: 'contains all not',
  1124. value: %w[key_1 key_2],
  1125. },
  1126. })
  1127. end
  1128. before do
  1129. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  1130. ObjectManager::Attribute.migration_execute
  1131. end
  1132. context 'when empty' do
  1133. let(:payload) do
  1134. base_payload.merge(
  1135. 'params' => { field_name => [] },
  1136. )
  1137. end
  1138. it 'does match' do
  1139. expect(result[:matched_workflows]).to include(workflow.id)
  1140. end
  1141. end
  1142. context 'when same value' do
  1143. let(:payload) do
  1144. base_payload.merge(
  1145. 'params' => { field_name => %w[key_1 key_2] },
  1146. )
  1147. end
  1148. it 'does not match' do
  1149. expect(result[:matched_workflows]).not_to include(workflow.id)
  1150. end
  1151. end
  1152. context 'when 50% value' do
  1153. let(:payload) do
  1154. base_payload.merge(
  1155. 'params' => { field_name => ['key_1'] },
  1156. )
  1157. end
  1158. it 'does not match' do
  1159. expect(result[:matched_workflows]).not_to include(workflow.id)
  1160. end
  1161. end
  1162. context 'when value differs' do
  1163. let(:payload) do
  1164. base_payload.merge(
  1165. 'params' => { field_name => ['key_3'] },
  1166. )
  1167. end
  1168. it 'does match' do
  1169. expect(result[:matched_workflows]).to include(workflow.id)
  1170. end
  1171. end
  1172. end
  1173. describe '.perform - Condition - group active is true' do
  1174. let(:payload) do
  1175. base_payload.merge('params' => {
  1176. 'group_id' => Group.first.id,
  1177. })
  1178. end
  1179. let!(:workflow) do
  1180. create(:core_workflow,
  1181. object: 'Ticket',
  1182. condition_selected: { 'group.active'=>{ 'operator' => 'is', 'value' => true } })
  1183. end
  1184. it 'does match' do
  1185. expect(result[:matched_workflows]).to include(workflow.id)
  1186. end
  1187. end
  1188. describe '.perform - Condition - group.assignment_timeout (Integer) matches' do
  1189. let(:group) { create(:group, assignment_timeout: 10) }
  1190. let(:payload) do
  1191. base_payload.merge('params' => {
  1192. 'group_id' => group.id,
  1193. })
  1194. end
  1195. before do
  1196. create(:core_workflow,
  1197. object: 'Ticket',
  1198. condition_selected: { 'group.assignment_timeout'=>{ 'operator' => 'is', 'value' => 10 } },
  1199. perform: { 'ticket.priority_id'=>{ 'operator' => 'hide', 'hide' => 'true' } },)
  1200. end
  1201. it 'does match' do
  1202. expect(result[:visibility]['priority_id']).to eq('hide')
  1203. end
  1204. end
  1205. describe '.perform - Condition - starts with' do
  1206. let(:payload) do
  1207. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1208. end
  1209. context 'when match' do
  1210. before do
  1211. travel_to DateTime.new 2023, 7, 21, 7, 0
  1212. end
  1213. let!(:workflow) do
  1214. create(:core_workflow,
  1215. object: 'Ticket',
  1216. condition_selected: {
  1217. 'ticket.title': {
  1218. operator: 'starts with',
  1219. value: ticket_title[0..5]
  1220. },
  1221. })
  1222. end
  1223. it 'does match' do
  1224. expect(result[:matched_workflows]).to include(workflow.id)
  1225. end
  1226. end
  1227. context 'when mismatch' do
  1228. before do
  1229. travel_to DateTime.new 2023, 8, 21, 7, 0
  1230. end
  1231. let!(:workflow) do
  1232. create(:core_workflow,
  1233. object: 'Ticket',
  1234. condition_selected: {
  1235. 'ticket.title': {
  1236. operator: 'starts with',
  1237. value: 'xxx',
  1238. },
  1239. })
  1240. end
  1241. it 'does not match' do
  1242. expect(result[:matched_workflows]).not_to include(workflow.id)
  1243. end
  1244. end
  1245. context 'when empty' do
  1246. before do
  1247. travel_to DateTime.new 2023, 8, 21, 7, 0
  1248. ticket.customer.update(note: nil)
  1249. end
  1250. let!(:workflow) do
  1251. create(:core_workflow,
  1252. object: 'Ticket',
  1253. condition_selected: {
  1254. 'customer.note': {
  1255. operator: 'starts with',
  1256. value: 'xxx',
  1257. },
  1258. })
  1259. end
  1260. it 'does not match' do
  1261. expect(result[:matched_workflows]).not_to include(workflow.id)
  1262. end
  1263. end
  1264. end
  1265. describe '.perform - Condition - starts with one of' do
  1266. let(:payload) do
  1267. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1268. end
  1269. context 'when match' do
  1270. before do
  1271. travel_to DateTime.new 2023, 7, 21, 7, 0
  1272. end
  1273. let!(:workflow) do
  1274. create(:core_workflow,
  1275. object: 'Ticket',
  1276. condition_selected: {
  1277. 'ticket.title': {
  1278. operator: 'starts with one of',
  1279. value: [ticket_title[0..5]]
  1280. },
  1281. })
  1282. end
  1283. it 'does match' do
  1284. expect(result[:matched_workflows]).to include(workflow.id)
  1285. end
  1286. end
  1287. context 'when mismatch' do
  1288. before do
  1289. travel_to DateTime.new 2023, 8, 21, 7, 0
  1290. end
  1291. let!(:workflow) do
  1292. create(:core_workflow,
  1293. object: 'Ticket',
  1294. condition_selected: {
  1295. 'ticket.title': {
  1296. operator: 'starts with one of',
  1297. value: ['xxx'],
  1298. },
  1299. })
  1300. end
  1301. it 'does not match' do
  1302. expect(result[:matched_workflows]).not_to include(workflow.id)
  1303. end
  1304. end
  1305. context 'when empty' do
  1306. before do
  1307. travel_to DateTime.new 2023, 8, 21, 7, 0
  1308. ticket.customer.update(note: nil)
  1309. end
  1310. let!(:workflow) do
  1311. create(:core_workflow,
  1312. object: 'Ticket',
  1313. condition_selected: {
  1314. 'customer.note': {
  1315. operator: 'starts with one of',
  1316. value: ['xxx'],
  1317. },
  1318. })
  1319. end
  1320. it 'does not match' do
  1321. expect(result[:matched_workflows]).not_to include(workflow.id)
  1322. end
  1323. end
  1324. end
  1325. describe '.perform - Condition - ends with' do
  1326. let(:payload) do
  1327. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1328. end
  1329. context 'when match' do
  1330. before do
  1331. travel_to DateTime.new 2023, 7, 21, 7, 0
  1332. end
  1333. let!(:workflow) do
  1334. create(:core_workflow,
  1335. object: 'Ticket',
  1336. condition_selected: {
  1337. 'ticket.title': {
  1338. operator: 'ends with',
  1339. value: ticket_title[-5..]
  1340. },
  1341. })
  1342. end
  1343. it 'does match' do
  1344. expect(result[:matched_workflows]).to include(workflow.id)
  1345. end
  1346. end
  1347. context 'when mismatch' do
  1348. before do
  1349. travel_to DateTime.new 2023, 8, 21, 7, 0
  1350. end
  1351. let!(:workflow) do
  1352. create(:core_workflow,
  1353. object: 'Ticket',
  1354. condition_selected: {
  1355. 'ticket.title': {
  1356. operator: 'ends with',
  1357. value: 'xxx',
  1358. },
  1359. })
  1360. end
  1361. it 'does not match' do
  1362. expect(result[:matched_workflows]).not_to include(workflow.id)
  1363. end
  1364. end
  1365. context 'when empty' do
  1366. before do
  1367. travel_to DateTime.new 2023, 8, 21, 7, 0
  1368. ticket.customer.update(note: nil)
  1369. end
  1370. let!(:workflow) do
  1371. create(:core_workflow,
  1372. object: 'Ticket',
  1373. condition_selected: {
  1374. 'customer.note': {
  1375. operator: 'ends with',
  1376. value: 'xxx',
  1377. },
  1378. })
  1379. end
  1380. it 'does not match' do
  1381. expect(result[:matched_workflows]).not_to include(workflow.id)
  1382. end
  1383. end
  1384. end
  1385. describe '.perform - Condition - ends with one of' do
  1386. let(:payload) do
  1387. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1388. end
  1389. context 'when match' do
  1390. before do
  1391. travel_to DateTime.new 2023, 7, 21, 7, 0
  1392. end
  1393. let!(:workflow) do
  1394. create(:core_workflow,
  1395. object: 'Ticket',
  1396. condition_selected: {
  1397. 'ticket.title': {
  1398. operator: 'ends with one of',
  1399. value: [ticket_title[-5..]]
  1400. },
  1401. })
  1402. end
  1403. it 'does match' do
  1404. expect(result[:matched_workflows]).to include(workflow.id)
  1405. end
  1406. end
  1407. context 'when mismatch' do
  1408. before do
  1409. travel_to DateTime.new 2023, 8, 21, 7, 0
  1410. end
  1411. let!(:workflow) do
  1412. create(:core_workflow,
  1413. object: 'Ticket',
  1414. condition_selected: {
  1415. 'ticket.title': {
  1416. operator: 'ends with one of',
  1417. value: ['xxx'],
  1418. },
  1419. })
  1420. end
  1421. it 'does not match' do
  1422. expect(result[:matched_workflows]).not_to include(workflow.id)
  1423. end
  1424. end
  1425. context 'when empty' do
  1426. before do
  1427. travel_to DateTime.new 2023, 8, 21, 7, 0
  1428. ticket.customer.update(note: nil)
  1429. end
  1430. let!(:workflow) do
  1431. create(:core_workflow,
  1432. object: 'Ticket',
  1433. condition_selected: {
  1434. 'customer.note': {
  1435. operator: 'ends with one of',
  1436. value: ['xxx'],
  1437. },
  1438. })
  1439. end
  1440. it 'does not match' do
  1441. expect(result[:matched_workflows]).not_to include(workflow.id)
  1442. end
  1443. end
  1444. end
  1445. describe '.perform - Condition - is any of' do
  1446. let(:payload) do
  1447. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1448. end
  1449. context 'when match' do
  1450. let!(:workflow) do
  1451. create(:core_workflow,
  1452. object: 'Ticket',
  1453. condition_selected: {
  1454. 'ticket.title': {
  1455. operator: 'is any of',
  1456. value: ['a', 'b', ticket_title],
  1457. },
  1458. })
  1459. end
  1460. it 'does match' do
  1461. expect(result[:matched_workflows]).to include(workflow.id)
  1462. end
  1463. end
  1464. context 'when mismatch' do
  1465. let!(:workflow) do
  1466. create(:core_workflow,
  1467. object: 'Ticket',
  1468. condition_selected: {
  1469. 'ticket.title': {
  1470. operator: 'is any of',
  1471. value: ['xxx'],
  1472. },
  1473. })
  1474. end
  1475. it 'does not match' do
  1476. expect(result[:matched_workflows]).not_to include(workflow.id)
  1477. end
  1478. end
  1479. context 'when empty' do
  1480. before do
  1481. ticket.update!(title: '')
  1482. end
  1483. let!(:workflow) do
  1484. create(:core_workflow,
  1485. object: 'Ticket',
  1486. condition_selected: {
  1487. 'ticket.title': {
  1488. operator: 'is any of',
  1489. value: ['xxx'],
  1490. },
  1491. })
  1492. end
  1493. it 'does not match' do
  1494. expect(result[:matched_workflows]).not_to include(workflow.id)
  1495. end
  1496. end
  1497. end
  1498. describe '.perform - Condition - is none of' do
  1499. let(:payload) do
  1500. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1501. end
  1502. context 'when match' do
  1503. let!(:workflow) do
  1504. create(:core_workflow,
  1505. object: 'Ticket',
  1506. condition_selected: {
  1507. 'ticket.title': {
  1508. operator: 'is none of',
  1509. value: %w[a b c],
  1510. },
  1511. })
  1512. end
  1513. it 'does match' do
  1514. expect(result[:matched_workflows]).to include(workflow.id)
  1515. end
  1516. end
  1517. context 'when mismatch' do
  1518. let!(:workflow) do
  1519. create(:core_workflow,
  1520. object: 'Ticket',
  1521. condition_selected: {
  1522. 'ticket.title': {
  1523. operator: 'is none of',
  1524. value: [ticket.title],
  1525. },
  1526. })
  1527. end
  1528. it 'does not match' do
  1529. expect(result[:matched_workflows]).not_to include(workflow.id)
  1530. end
  1531. end
  1532. context 'when empty' do
  1533. let!(:workflow) do
  1534. create(:core_workflow,
  1535. object: 'Ticket',
  1536. condition_selected: {
  1537. 'ticket.title': {
  1538. operator: 'is none of',
  1539. value: [ticket.title],
  1540. },
  1541. })
  1542. end
  1543. it 'does match' do
  1544. ticket.update!(title: '')
  1545. expect(result[:matched_workflows]).to include(workflow.id)
  1546. end
  1547. end
  1548. end
  1549. describe '.perform - Condition - is' do
  1550. let(:payload) do
  1551. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1552. end
  1553. context 'when match' do
  1554. let!(:workflow) do
  1555. create(:core_workflow,
  1556. object: 'Ticket',
  1557. condition_selected: {
  1558. 'ticket.group_id': {
  1559. operator: 'is',
  1560. value: [ticket.group.id.to_s],
  1561. },
  1562. })
  1563. end
  1564. it 'does match' do
  1565. expect(result[:matched_workflows]).to include(workflow.id)
  1566. end
  1567. end
  1568. context 'when mismatch' do
  1569. let!(:workflow) do
  1570. create(:core_workflow,
  1571. object: 'Ticket',
  1572. condition_selected: {
  1573. 'ticket.group_id': {
  1574. operator: 'is',
  1575. value: [Group.first.id],
  1576. },
  1577. })
  1578. end
  1579. it 'does not match' do
  1580. expect(result[:matched_workflows]).not_to include(workflow.id)
  1581. end
  1582. end
  1583. context 'with new external data source field', db_adapter: :postgresql, db_strategy: :reset do
  1584. let!(:external_data_source_attribute) do
  1585. attribute = create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  1586. name: 'external_data_source_attribute')
  1587. ObjectManager::Attribute.migration_execute
  1588. attribute
  1589. end
  1590. let(:condition_field_name) { "ticket.#{external_data_source_attribute.name}" }
  1591. let(:additional_ticket_attributes) do
  1592. {
  1593. external_data_source_attribute.name => {
  1594. value: 123,
  1595. label: 'Example',
  1596. }
  1597. }
  1598. end
  1599. context 'when match' do
  1600. let!(:workflow) do
  1601. create(:core_workflow,
  1602. object: 'Ticket',
  1603. condition_selected: {
  1604. condition_field_name => {
  1605. operator: 'is',
  1606. value: [
  1607. {
  1608. value: 123,
  1609. label: 'Example',
  1610. }
  1611. ],
  1612. },
  1613. })
  1614. end
  1615. it 'does match' do
  1616. expect(result[:matched_workflows]).to include(workflow.id)
  1617. end
  1618. end
  1619. context 'when mismatch' do
  1620. let!(:workflow) do
  1621. create(:core_workflow,
  1622. object: 'Ticket',
  1623. condition_selected: {
  1624. condition_field_name => {
  1625. operator: 'is',
  1626. value: [
  1627. {
  1628. value: 986,
  1629. label: 'Example',
  1630. }
  1631. ],
  1632. },
  1633. })
  1634. end
  1635. it 'does not match' do
  1636. expect(result[:matched_workflows]).not_to include(workflow.id)
  1637. end
  1638. end
  1639. end
  1640. end
  1641. describe '.perform - Condition - is not' do
  1642. let(:payload) do
  1643. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1644. end
  1645. context 'when match' do
  1646. let!(:workflow) do
  1647. create(:core_workflow,
  1648. object: 'Ticket',
  1649. condition_selected: {
  1650. 'ticket.group_id': {
  1651. operator: 'is not',
  1652. value: [Group.first.id.to_s],
  1653. },
  1654. })
  1655. end
  1656. it 'does match' do
  1657. expect(result[:matched_workflows]).to include(workflow.id)
  1658. end
  1659. end
  1660. context 'when mismatch' do
  1661. let!(:workflow) do
  1662. create(:core_workflow,
  1663. object: 'Ticket',
  1664. condition_selected: {
  1665. 'ticket.group_id': {
  1666. operator: 'is not',
  1667. value: [ticket.group.id.to_s],
  1668. },
  1669. })
  1670. end
  1671. it 'does not match' do
  1672. expect(result[:matched_workflows]).not_to include(workflow.id)
  1673. end
  1674. end
  1675. context 'with new external data source field', db_adapter: :postgresql, db_strategy: :reset do
  1676. let!(:external_data_source_attribute) do
  1677. attribute = create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  1678. name: 'external_data_source_attribute')
  1679. ObjectManager::Attribute.migration_execute
  1680. attribute
  1681. end
  1682. let(:condition_field_name) { "ticket.#{external_data_source_attribute.name}" }
  1683. let(:additional_ticket_attributes) do
  1684. {
  1685. external_data_source_attribute.name => {
  1686. value: 123,
  1687. label: 'Example',
  1688. }
  1689. }
  1690. end
  1691. context 'when match' do
  1692. let!(:workflow) do
  1693. create(:core_workflow,
  1694. object: 'Ticket',
  1695. condition_selected: {
  1696. condition_field_name => {
  1697. operator: 'is not',
  1698. value: [
  1699. {
  1700. value: 986,
  1701. label: 'Example',
  1702. }
  1703. ],
  1704. },
  1705. })
  1706. end
  1707. it 'does match' do
  1708. expect(result[:matched_workflows]).to include(workflow.id)
  1709. end
  1710. end
  1711. context 'when mismatch' do
  1712. let!(:workflow) do
  1713. create(:core_workflow,
  1714. object: 'Ticket',
  1715. condition_selected: {
  1716. condition_field_name => {
  1717. operator: 'is not',
  1718. value: [
  1719. {
  1720. value: 123,
  1721. label: 'Example',
  1722. }
  1723. ],
  1724. },
  1725. })
  1726. end
  1727. it 'does not match' do
  1728. expect(result[:matched_workflows]).not_to include(workflow.id)
  1729. end
  1730. end
  1731. end
  1732. end
  1733. describe '.perform - Condition - is set' do
  1734. let(:payload) do
  1735. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1736. end
  1737. context 'when match' do
  1738. let!(:workflow) do
  1739. create(:core_workflow,
  1740. object: 'Ticket',
  1741. condition_selected: {
  1742. 'ticket.group_id': {
  1743. operator: 'is set',
  1744. value: [],
  1745. },
  1746. })
  1747. end
  1748. it 'does match' do
  1749. expect(result[:matched_workflows]).to include(workflow.id)
  1750. end
  1751. end
  1752. context 'with new external data source field', db_adapter: :postgresql, db_strategy: :reset do
  1753. let!(:external_data_source_attribute) do
  1754. attribute = create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  1755. name: 'external_data_source_attribute')
  1756. ObjectManager::Attribute.migration_execute
  1757. attribute
  1758. end
  1759. let(:condition_field_name) { "ticket.#{external_data_source_attribute.name}" }
  1760. context 'when match' do
  1761. let(:additional_ticket_attributes) do
  1762. {
  1763. external_data_source_attribute.name => {
  1764. value: 123,
  1765. label: 'Example',
  1766. }
  1767. }
  1768. end
  1769. let!(:workflow) do
  1770. create(:core_workflow,
  1771. object: 'Ticket',
  1772. condition_selected: {
  1773. condition_field_name => {
  1774. operator: 'is set',
  1775. value: [],
  1776. },
  1777. })
  1778. end
  1779. it 'does match' do
  1780. expect(result[:matched_workflows]).to include(workflow.id)
  1781. end
  1782. end
  1783. context 'when mismatch' do
  1784. let!(:workflow) do
  1785. create(:core_workflow,
  1786. object: 'Ticket',
  1787. condition_selected: {
  1788. condition_field_name => {
  1789. operator: 'is set',
  1790. value: [],
  1791. },
  1792. })
  1793. end
  1794. it 'does not match' do
  1795. expect(result[:matched_workflows]).not_to include(workflow.id)
  1796. end
  1797. end
  1798. end
  1799. end
  1800. describe '.perform - Condition - not set' do
  1801. let(:payload) do
  1802. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1803. end
  1804. context 'with new external data source field', db_adapter: :postgresql, db_strategy: :reset do
  1805. let!(:external_data_source_attribute) do
  1806. attribute = create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  1807. name: 'external_data_source_attribute')
  1808. ObjectManager::Attribute.migration_execute
  1809. attribute
  1810. end
  1811. let(:condition_field_name) { "ticket.#{external_data_source_attribute.name}" }
  1812. context 'when match' do
  1813. let!(:workflow) do
  1814. create(:core_workflow,
  1815. object: 'Ticket',
  1816. condition_selected: {
  1817. condition_field_name => {
  1818. operator: 'not set',
  1819. value: [],
  1820. },
  1821. })
  1822. end
  1823. it 'does match' do
  1824. expect(result[:matched_workflows]).to include(workflow.id)
  1825. end
  1826. end
  1827. context 'when mismatch' do
  1828. let(:additional_ticket_attributes) do
  1829. {
  1830. external_data_source_attribute.name => {
  1831. value: 123,
  1832. label: 'Example',
  1833. }
  1834. }
  1835. end
  1836. let!(:workflow) do
  1837. create(:core_workflow,
  1838. object: 'Ticket',
  1839. condition_selected: {
  1840. condition_field_name => {
  1841. operator: 'not set',
  1842. value: [],
  1843. },
  1844. })
  1845. end
  1846. it 'does not match' do
  1847. expect(result[:matched_workflows]).not_to include(workflow.id)
  1848. end
  1849. end
  1850. end
  1851. end
  1852. describe '.perform - Condition - changed to' do
  1853. let(:payload) do
  1854. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit', 'last_changed_attribute' => 'group_id')
  1855. end
  1856. context 'when match' do
  1857. let!(:workflow) do
  1858. create(:core_workflow,
  1859. object: 'Ticket',
  1860. condition_selected: {
  1861. 'ticket.group_id': {
  1862. operator: 'changed to',
  1863. value: [ticket.group.id.to_s],
  1864. },
  1865. })
  1866. end
  1867. it 'does match' do
  1868. expect(result[:matched_workflows]).to include(workflow.id)
  1869. end
  1870. end
  1871. context 'when mismatch' do
  1872. let!(:workflow) do
  1873. create(:core_workflow,
  1874. object: 'Ticket',
  1875. condition_selected: {
  1876. 'ticket.group_id': {
  1877. operator: 'changed to',
  1878. value: [Group.first.id],
  1879. },
  1880. })
  1881. end
  1882. it 'does not match' do
  1883. expect(result[:matched_workflows]).not_to include(workflow.id)
  1884. end
  1885. end
  1886. context 'with new external data source field', db_adapter: :postgresql, db_strategy: :reset do
  1887. let(:payload) do
  1888. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit', 'last_changed_attribute' => external_data_source_attribute.name)
  1889. end
  1890. let!(:external_data_source_attribute) do
  1891. attribute = create(:object_manager_attribute_autocompletion_ajax_external_data_source,
  1892. name: 'external_data_source_attribute')
  1893. ObjectManager::Attribute.migration_execute
  1894. attribute
  1895. end
  1896. let(:condition_field_name) { "ticket.#{external_data_source_attribute.name}" }
  1897. let(:additional_ticket_attributes) do
  1898. {
  1899. external_data_source_attribute.name => {
  1900. value: 123,
  1901. label: 'Example',
  1902. }
  1903. }
  1904. end
  1905. context 'when match' do
  1906. let!(:workflow) do
  1907. create(:core_workflow,
  1908. object: 'Ticket',
  1909. condition_selected: {
  1910. condition_field_name => {
  1911. operator: 'changed to',
  1912. value: [
  1913. {
  1914. value: 123,
  1915. label: 'Example',
  1916. }
  1917. ],
  1918. },
  1919. })
  1920. end
  1921. it 'does match' do
  1922. expect(result[:matched_workflows]).to include(workflow.id)
  1923. end
  1924. end
  1925. context 'when mismatch' do
  1926. let!(:workflow) do
  1927. create(:core_workflow,
  1928. object: 'Ticket',
  1929. condition_selected: {
  1930. condition_field_name => {
  1931. operator: 'changed to',
  1932. value: [
  1933. {
  1934. value: 986,
  1935. label: 'Example',
  1936. }
  1937. ],
  1938. },
  1939. })
  1940. end
  1941. it 'does not match' do
  1942. expect(result[:matched_workflows]).not_to include(workflow.id)
  1943. end
  1944. end
  1945. end
  1946. end
  1947. describe 'New ticket organization condition in core workflow not working for is specific usage #4750' do
  1948. let(:ticket_customer) { create(:customer, :with_org) }
  1949. let!(:workflow) do
  1950. create(:core_workflow,
  1951. object: 'Ticket',
  1952. condition_selected: {
  1953. 'ticket.organization_id': {
  1954. operator: 'is',
  1955. value: [ticket.customer.organization_id.to_s],
  1956. },
  1957. })
  1958. end
  1959. context 'when agent' do
  1960. let(:payload) do
  1961. base_payload.merge('params' => { 'customer_id' => ticket.customer_id })
  1962. end
  1963. it 'does match' do
  1964. expect(result[:matched_workflows]).to include(workflow.id)
  1965. end
  1966. end
  1967. context 'when customer' do
  1968. let!(:action_user) { ticket.customer } # rubocop:disable RSpec/LetSetup
  1969. it 'does match' do
  1970. expect(result[:matched_workflows]).to include(workflow.id)
  1971. end
  1972. end
  1973. end
  1974. describe 'Core Workflow: Readded options are not usable in conditions #4763' do
  1975. before do
  1976. workflow_1 && workflow_2
  1977. end
  1978. context 'when single value' do
  1979. let(:workflow_1) do
  1980. create(:core_workflow,
  1981. object: 'Ticket',
  1982. perform: {
  1983. 'ticket.priority_id': {
  1984. operator: 'remove_option',
  1985. remove_option: Ticket::Priority.pluck(:id).map(&:to_s),
  1986. },
  1987. })
  1988. end
  1989. let(:workflow_2) do
  1990. create(:core_workflow,
  1991. object: 'Ticket',
  1992. perform: {
  1993. 'ticket.priority_id': {
  1994. operator: 'add_option',
  1995. add_option: Ticket::Priority.pluck(:id).map(&:to_s),
  1996. },
  1997. })
  1998. end
  1999. let!(:workflow_3) do
  2000. create(:core_workflow,
  2001. object: 'Ticket',
  2002. condition_selected: {
  2003. 'ticket.priority_id': {
  2004. operator: 'is',
  2005. value: Ticket::Priority.where(name: '3 high').pluck(:id).map(&:to_s),
  2006. },
  2007. })
  2008. end
  2009. let(:payload) do
  2010. base_payload.merge('params' => { 'priority_id' => Ticket::Priority.find_by(name: '3 high').id.to_s })
  2011. end
  2012. it 'does match' do
  2013. expect(result[:matched_workflows]).to include(workflow_3.id)
  2014. end
  2015. end
  2016. context 'when multiple value', db_strategy: :reset do
  2017. let(:workflow_1) do
  2018. create(:core_workflow,
  2019. object: 'Ticket',
  2020. perform: {
  2021. "ticket.#{field_name}": {
  2022. operator: 'remove_option',
  2023. remove_option: ['key_1'],
  2024. },
  2025. })
  2026. end
  2027. let(:workflow_2) do
  2028. create(:core_workflow,
  2029. object: 'Ticket',
  2030. perform: {
  2031. "ticket.#{field_name}": {
  2032. operator: 'add_option',
  2033. add_option: ['key_1'],
  2034. },
  2035. })
  2036. end
  2037. let!(:workflow_3) do
  2038. create(:core_workflow,
  2039. object: 'Ticket',
  2040. condition_selected: {
  2041. "ticket.#{field_name}": {
  2042. operator: 'is',
  2043. value: ['key_1'],
  2044. },
  2045. })
  2046. end
  2047. let(:payload) do
  2048. base_payload.merge('params' => { field_name => ['key_1'] })
  2049. end
  2050. let(:field_name) { SecureRandom.uuid }
  2051. before do
  2052. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  2053. ObjectManager::Attribute.migration_execute
  2054. end
  2055. it 'does match' do
  2056. expect(result[:matched_workflows]).to include(workflow_3.id)
  2057. end
  2058. end
  2059. end
  2060. end