core_workflow_spec.rb 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe CoreWorkflow, mariadb: true, type: :model do
  4. let(:group) { create(:group) }
  5. let!(:ticket) { create(:ticket, state: Ticket::State.find_by(name: 'pending reminder'), pending_time: 5.days.from_now, group: group) }
  6. let!(:base_payload) do
  7. {
  8. 'event' => 'core_workflow',
  9. 'request_id' => 'default',
  10. 'class_name' => 'Ticket',
  11. 'screen' => 'create_middle',
  12. 'params' => {},
  13. }
  14. end
  15. let(:payload) { base_payload }
  16. let!(:action_user) { create(:agent, groups: [ticket.group]) }
  17. let(:result) { described_class.perform(payload: payload, user: action_user) }
  18. describe '.perform - No assets' do
  19. let(:result) { described_class.perform(payload: payload, user: action_user, assets: false) }
  20. it 'does not contain assets' do
  21. expect(result[:assets]).to be_blank
  22. end
  23. end
  24. describe '.perform - Default - Group' do
  25. let!(:group_change) { create(:group) }
  26. let!(:group_create) { create(:group) }
  27. describe 'for agent with full permissions on screen create_middle' do
  28. let(:action_user) { create(:agent) }
  29. before do
  30. action_user.group_names_access_map = {
  31. group_create.name => ['full'],
  32. group_change.name => ['change'],
  33. }
  34. end
  35. it 'does show group_create for agent with all permissions' do
  36. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  37. end
  38. it 'does not show group_change for agent with all permissions' do
  39. expect(result[:restrict_values]['group_id']).not_to include(group_change.id.to_s)
  40. end
  41. end
  42. describe 'for agent with full permissions on screen edit' do
  43. let(:payload) do
  44. base_payload.merge('screen' => 'edit')
  45. end
  46. let(:action_user) { create(:agent) }
  47. before do
  48. action_user.group_names_access_map = {
  49. group_create.name => ['full'],
  50. group_change.name => ['change'],
  51. }
  52. end
  53. it 'does show group_create for agent with all permissions' do
  54. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  55. end
  56. it 'does show group_change for agent with all permissions' do
  57. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  58. end
  59. end
  60. describe 'for agent with change permissions on screen create_middle' do
  61. let(:action_user) { create(:agent) }
  62. before do
  63. action_user.group_names_access_map = {
  64. group_create.name => ['change'],
  65. group_change.name => ['change'],
  66. }
  67. end
  68. it 'does not show group_create for agent with change permissions' do
  69. expect(result[:restrict_values]['group_id']).not_to include(group_create.id.to_s)
  70. end
  71. it 'does not show group_change for agent with change permissions' do
  72. expect(result[:restrict_values]['group_id']).not_to include(group_change.id.to_s)
  73. end
  74. end
  75. describe 'for agent with change permissions on screen edit' do
  76. let(:payload) do
  77. base_payload.merge('screen' => 'edit')
  78. end
  79. let(:action_user) { create(:agent) }
  80. before do
  81. action_user.group_names_access_map = {
  82. group_create.name => ['change'],
  83. group_change.name => ['change'],
  84. }
  85. end
  86. it 'does show group_create for agent with change permissions' do
  87. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  88. end
  89. it 'does show group_change for agent with change permissions' do
  90. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  91. end
  92. end
  93. describe 'for customer on screen create_middle' do
  94. let(:action_user) { create(:customer) }
  95. it 'does show group_create for customer' do
  96. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  97. end
  98. it 'does show group_change for customer' do
  99. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  100. end
  101. end
  102. describe 'for customer on screen edit' do
  103. let(:payload) do
  104. base_payload.merge('screen' => 'edit')
  105. end
  106. let(:action_user) { create(:customer) }
  107. it 'does show group_create for customer' do
  108. expect(result[:restrict_values]['group_id']).to include(group_create.id.to_s)
  109. end
  110. it 'does show group_change for customer' do
  111. expect(result[:restrict_values]['group_id']).to include(group_change.id.to_s)
  112. end
  113. end
  114. end
  115. describe '.perform - Default - Owner' do
  116. before do
  117. another_group = create(:group)
  118. action_user.group_names_access_map = {
  119. ticket.group.name => ['full'],
  120. another_group.name => ['full'],
  121. }
  122. end
  123. it 'does not show any owners for no group' do
  124. expect(result[:restrict_values]['owner_id']).to eq([''])
  125. end
  126. describe 'on group' do
  127. let(:payload) do
  128. base_payload.merge('params' => { 'group_id' => ticket.group.id })
  129. end
  130. it 'does show ticket agent' do
  131. expect(result[:restrict_values]['owner_id']).to eq(['', action_user.id.to_s])
  132. end
  133. end
  134. describe 'on group save' do
  135. let(:payload) do
  136. base_payload.merge('request_id' => 'ChecksCoreWorkflow.validate_workflows', 'params' => { 'group_id' => ticket.group.id })
  137. end
  138. it 'does show ticket agent and system user' do
  139. expect(result[:restrict_values]['owner_id']).to eq(['', '1', action_user.id.to_s])
  140. end
  141. end
  142. end
  143. describe '.perform - Default - Bulk Owner' do
  144. let(:payload) do
  145. base_payload.merge('screen' => 'overview_bulk')
  146. end
  147. it 'does not show any owners for no group' do
  148. expect(result[:restrict_values]['owner_id']).to eq([''])
  149. end
  150. describe 'on ticket ids' do
  151. let(:payload) do
  152. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => ticket.id.to_s })
  153. end
  154. it 'does show ticket agent' do
  155. expect(result[:restrict_values]['owner_id']).to eq(['', action_user.id.to_s])
  156. end
  157. end
  158. describe 'on ticket ids with no group overlap' do
  159. let(:ticket2) { create(:ticket) }
  160. let(:payload) do
  161. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => "#{ticket.id},#{ticket2.id}" })
  162. end
  163. it 'does not show ticket agent' do
  164. expect(result[:restrict_values]['owner_id']).to eq([''])
  165. end
  166. end
  167. describe 'on ticket ids with group overlap' do
  168. let(:ticket2) { create(:ticket, group: ticket.group) }
  169. let(:payload) do
  170. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => "#{ticket.id},#{ticket2.id}" })
  171. end
  172. it 'does show ticket agent' do
  173. expect(result[:restrict_values]['owner_id']).to eq(['', action_user.id.to_s])
  174. end
  175. end
  176. describe 'Ticket owner selection is not updated if owner selection should be empty #3809' do
  177. let(:group_no_owners) { create(:group) }
  178. let(:ticket2) { create(:ticket, group: group_no_owners) }
  179. let(:payload) do
  180. base_payload.merge('screen' => 'overview_bulk', 'params' => { 'ticket_ids' => ticket2.id.to_s })
  181. end
  182. before do
  183. action_user.group_names_access_map = {
  184. group_no_owners.name => %w[create read change overview],
  185. }
  186. end
  187. it 'does not show any owners for group with no full permitted users' do
  188. expect(result[:restrict_values]['owner_id']).to eq([''])
  189. end
  190. end
  191. end
  192. describe '.perform - Default - State' do
  193. it 'does show state type new for create_middle' do
  194. expect(result[:restrict_values]['state_id']).to include(Ticket::State.find_by(name: 'new').id.to_s)
  195. end
  196. describe 'on edit' do
  197. let(:payload) do
  198. base_payload.merge('screen' => 'edit')
  199. end
  200. it 'does not show state type new' do
  201. expect(result[:restrict_values]['state_id']).not_to include(Ticket::State.find_by(name: 'new').id.to_s)
  202. end
  203. end
  204. it 'does show empty value for create_middle' do
  205. expect(result[:restrict_values]['state_id']).to include('')
  206. end
  207. context 'with customer user' do
  208. let(:action_user) { create(:customer) }
  209. it 'does not show empty value for create_middle' do
  210. expect(result[:restrict_values]['state_id']).not_to include('')
  211. end
  212. end
  213. end
  214. describe '.perform - Default - Priority' do
  215. let(:prio_invalid) { create(:ticket_priority, active: false) }
  216. it 'does show valid priority' do
  217. expect(result[:restrict_values]['priority_id']).to include(Ticket::Priority.find_by(name: '3 high').id.to_s)
  218. end
  219. it 'does not show invalid priority' do
  220. expect(result[:restrict_values]['priority_id']).not_to include(prio_invalid.id.to_s)
  221. end
  222. end
  223. describe '.perform - Default - Customer setting customer_ticket_create_group_ids' do
  224. let(:action_user) { create(:customer) }
  225. let!(:group1) { create(:group) }
  226. let!(:group2) { create(:group) }
  227. let!(:group3) { create(:group) }
  228. it 'does show group 1' do
  229. expect(result[:restrict_values]['group_id']).to include(group1.id.to_s)
  230. end
  231. context 'with customer_ticket_create_group_ids set' do
  232. before do
  233. Setting.set('customer_ticket_create_group_ids', [group2.id.to_s, group3.id.to_s])
  234. end
  235. it 'does not show group 1' do
  236. expect(result[:restrict_values]['group_id']).not_to include(group1.id.to_s)
  237. end
  238. end
  239. end
  240. describe '.perform - Default - #3721 - Fields are falsey displayed as mandatory if they contain historic screen values', db_strategy: :reset do
  241. let(:field_name) { SecureRandom.uuid }
  242. let(:screens) do
  243. {
  244. create_middle: {
  245. 'ticket.agent' => {
  246. shown: true,
  247. },
  248. },
  249. }
  250. end
  251. before do
  252. create(:object_manager_attribute_text, object_name: 'Ticket', name: field_name, display: field_name, screens: screens)
  253. ObjectManager::Attribute.migration_execute
  254. end
  255. it 'does show the field as optional because it has no required value' do
  256. expect(result[:mandatory][field_name]).to be(false)
  257. end
  258. end
  259. describe '.perform - Default - Restrict values for multiselect fields', db_strategy: :reset do
  260. let(:field_name) { SecureRandom.uuid }
  261. before do
  262. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  263. ObjectManager::Attribute.migration_execute
  264. end
  265. context 'without saved values' do
  266. it 'does return the correct list of selectable values' do
  267. expect(result[:restrict_values][field_name]).to eq(['', 'key_1', 'key_2', 'key_3'])
  268. end
  269. end
  270. context 'with saved values' do
  271. let(:payload) do
  272. base_payload.merge('params' => {
  273. 'id' => ticket.id,
  274. })
  275. end
  276. before do
  277. ticket.reload.update(field_name.to_sym => %w[key_2 key_3])
  278. end
  279. it 'does return the correct list of selectable values' do
  280. expect(result[:restrict_values][field_name]).to eq(['', 'key_1', 'key_2', 'key_3'])
  281. end
  282. end
  283. end
  284. describe '.perform - Custom - Pending Time' do
  285. it 'does not show pending time for non pending state' do
  286. expect(result[:visibility]['pending_time']).to eq('remove')
  287. end
  288. describe 'for ticket id with no state change' do
  289. let(:payload) do
  290. base_payload.merge('params' => {
  291. 'id' => ticket.id,
  292. })
  293. end
  294. it 'does show pending time for pending ticket' do
  295. expect(result[:visibility]['pending_time']).to eq('show')
  296. end
  297. end
  298. describe 'for ticket id with state change' do
  299. let(:payload) do
  300. base_payload.merge('params' => {
  301. 'id' => ticket.id,
  302. 'state_id' => Ticket::State.find_by(name: 'open').id.to_s,
  303. })
  304. end
  305. it 'does not show pending time for pending ticket' do
  306. expect(result[:visibility]['pending_time']).to eq('remove')
  307. end
  308. end
  309. end
  310. describe '.perform - Custom - Admin SLA' do
  311. let(:payload) do
  312. base_payload.merge(
  313. 'screen' => 'edit',
  314. 'class_name' => 'Sla',
  315. )
  316. end
  317. it 'does set first_response_time_in_text optional' do
  318. expect(result[:mandatory]['first_response_time_in_text']).to be(false)
  319. end
  320. it 'does set update_time_in_text optional' do
  321. expect(result[:mandatory]['update_time_in_text']).to be(false)
  322. end
  323. it 'does set solution_time_in_text optional' do
  324. expect(result[:mandatory]['solution_time_in_text']).to be(false)
  325. end
  326. describe 'on first_response_time_enabled' do
  327. let(:payload) do
  328. base_payload.merge(
  329. 'screen' => 'edit',
  330. 'class_name' => 'Sla',
  331. 'params' => { 'first_response_time_enabled' => 'true' }
  332. )
  333. end
  334. it 'does set first_response_time_in_text mandatory' do
  335. expect(result[:mandatory]['first_response_time_in_text']).to be(true)
  336. end
  337. it 'does set update_time_in_text optional' do
  338. expect(result[:mandatory]['update_time_in_text']).to be(false)
  339. end
  340. it 'does set solution_time_in_text optional' do
  341. expect(result[:mandatory]['solution_time_in_text']).to be(false)
  342. end
  343. end
  344. describe 'on update_time_enabled' do
  345. let(:payload) do
  346. base_payload.merge(
  347. 'screen' => 'edit',
  348. 'class_name' => 'Sla',
  349. 'params' => { 'update_time_enabled' => 'true', 'update_type' => 'update' }
  350. )
  351. end
  352. it 'does set first_response_time_in_text optional' do
  353. expect(result[:mandatory]['first_response_time_in_text']).to be(false)
  354. end
  355. it 'does set update_time_in_text mandatory' do
  356. expect(result[:mandatory]['update_time_in_text']).to be(true)
  357. end
  358. it 'does set solution_time_in_text optional' do
  359. expect(result[:mandatory]['solution_time_in_text']).to be(false)
  360. end
  361. end
  362. describe 'on solution_time_enabled' do
  363. let(:payload) do
  364. base_payload.merge(
  365. 'screen' => 'edit',
  366. 'class_name' => 'Sla',
  367. 'params' => { 'solution_time_enabled' => 'true' }
  368. )
  369. end
  370. it 'does set first_response_time_in_text optional' do
  371. expect(result[:mandatory]['first_response_time_in_text']).to be(false)
  372. end
  373. it 'does set update_time_in_text optional' do
  374. expect(result[:mandatory]['update_time_in_text']).to be(false)
  375. end
  376. it 'does set solution_time_in_text mandatory' do
  377. expect(result[:mandatory]['solution_time_in_text']).to be(true)
  378. end
  379. end
  380. end
  381. describe '.perform - Custom - Admin CoreWorkflow' do
  382. let(:payload) do
  383. base_payload.merge(
  384. 'screen' => 'edit',
  385. 'class_name' => 'CoreWorkflow',
  386. )
  387. end
  388. it 'does not show screens for empty object' do
  389. expect(result[:restrict_values]['preferences::screen']).to eq([''])
  390. end
  391. it 'does not show invalid objects' do
  392. expect(result[:restrict_values]['object']).not_to include('CoreWorkflow')
  393. end
  394. describe 'on object Ticket' do
  395. let(:payload) do
  396. base_payload.merge(
  397. 'screen' => 'edit',
  398. 'class_name' => 'CoreWorkflow',
  399. 'params' => { 'object' => 'Ticket' },
  400. )
  401. end
  402. it 'does show screen create_middle' do
  403. expect(result[:restrict_values]['preferences::screen']).to include('create_middle')
  404. end
  405. it 'does show screen edit' do
  406. expect(result[:restrict_values]['preferences::screen']).to include('edit')
  407. end
  408. end
  409. describe 'on saved object Ticket' do
  410. let(:workflow) { create(:core_workflow, object: 'Ticket') }
  411. let(:payload) do
  412. base_payload.merge(
  413. 'screen' => 'edit',
  414. 'class_name' => 'CoreWorkflow',
  415. 'params' => { 'id' => workflow.id },
  416. )
  417. end
  418. it 'does show screen create_middle' do
  419. expect(result[:restrict_values]['preferences::screen']).to include('create_middle')
  420. end
  421. it 'does show screen edit' do
  422. expect(result[:restrict_values]['preferences::screen']).to include('edit')
  423. end
  424. end
  425. end
  426. describe '.perform - Condition - owner_id not set' do
  427. let!(:workflow) do
  428. create(:core_workflow,
  429. object: 'Ticket',
  430. condition_selected: {
  431. 'ticket.owner_id': {
  432. operator: 'not_set',
  433. },
  434. })
  435. end
  436. it 'does match' do
  437. expect(result[:matched_workflows]).to include(workflow.id)
  438. end
  439. describe 'for owner id 1' do
  440. let(:payload) do
  441. base_payload.merge(
  442. 'params' => { 'owner_id' => '1' },
  443. )
  444. end
  445. it 'does match' do
  446. expect(result[:matched_workflows]).to include(workflow.id)
  447. end
  448. end
  449. end
  450. describe '.perform - Condition - session.role_ids' do
  451. let!(:workflow) do
  452. create(:core_workflow,
  453. object: 'Ticket',
  454. condition_selected: {
  455. 'session.role_ids': {
  456. operator: 'is',
  457. value: [ Role.find_by(name: 'Agent').id.to_s ],
  458. },
  459. })
  460. end
  461. it 'does match' do
  462. expect(result[:matched_workflows]).to include(workflow.id)
  463. end
  464. describe 'for customer' do
  465. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  466. it 'does not match' do
  467. expect(result[:matched_workflows]).not_to include(workflow.id)
  468. end
  469. end
  470. end
  471. describe '.perform - Condition - session.group_ids_full' do
  472. let!(:workflow) do
  473. create(:core_workflow,
  474. object: 'Ticket',
  475. condition_selected: {
  476. 'session.group_ids_full': {
  477. operator: 'is',
  478. value: [ ticket.group.id.to_s ],
  479. },
  480. })
  481. end
  482. it 'does match' do
  483. expect(result[:matched_workflows]).to include(workflow.id)
  484. end
  485. describe 'for customer' do
  486. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  487. it 'does not match' do
  488. expect(result[:matched_workflows]).not_to include(workflow.id)
  489. end
  490. end
  491. end
  492. describe '.perform - Condition - session.group_ids_change' do
  493. let!(:workflow) do
  494. create(:core_workflow,
  495. object: 'Ticket',
  496. condition_selected: {
  497. 'session.group_ids_change': {
  498. operator: 'is',
  499. value: [ ticket.group.id.to_s ],
  500. },
  501. })
  502. end
  503. it 'does match' do
  504. expect(result[:matched_workflows]).to include(workflow.id)
  505. end
  506. describe 'for customer' do
  507. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  508. it 'does not match' do
  509. expect(result[:matched_workflows]).not_to include(workflow.id)
  510. end
  511. end
  512. end
  513. describe '.perform - Condition - session.group_ids_read' do
  514. let!(:workflow) do
  515. create(:core_workflow,
  516. object: 'Ticket',
  517. condition_selected: {
  518. 'session.group_ids_read': {
  519. operator: 'is',
  520. value: [ ticket.group.id.to_s ],
  521. },
  522. })
  523. end
  524. it 'does match' do
  525. expect(result[:matched_workflows]).to include(workflow.id)
  526. end
  527. describe 'for customer' do
  528. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  529. it 'does not match' do
  530. expect(result[:matched_workflows]).not_to include(workflow.id)
  531. end
  532. end
  533. end
  534. describe '.perform - Condition - session.group_ids_overview' do
  535. let!(:workflow) do
  536. create(:core_workflow,
  537. object: 'Ticket',
  538. condition_selected: {
  539. 'session.group_ids_overview': {
  540. operator: 'is',
  541. value: [ ticket.group.id.to_s ],
  542. },
  543. })
  544. end
  545. it 'does match' do
  546. expect(result[:matched_workflows]).to include(workflow.id)
  547. end
  548. describe 'for customer' do
  549. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  550. it 'does not match' do
  551. expect(result[:matched_workflows]).not_to include(workflow.id)
  552. end
  553. end
  554. end
  555. describe '.perform - Condition - session.group_ids_create' do
  556. let!(:workflow) do
  557. create(:core_workflow,
  558. object: 'Ticket',
  559. condition_selected: {
  560. 'session.group_ids_create': {
  561. operator: 'is',
  562. value: [ ticket.group.id.to_s ],
  563. },
  564. })
  565. end
  566. it 'does match' do
  567. expect(result[:matched_workflows]).to include(workflow.id)
  568. end
  569. describe 'for customer' do
  570. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  571. it 'does not match' do
  572. expect(result[:matched_workflows]).not_to include(workflow.id)
  573. end
  574. end
  575. end
  576. describe '.perform - Condition - session.permission_ids' do
  577. let!(:workflow) do
  578. create(:core_workflow,
  579. object: 'Ticket',
  580. condition_selected: {
  581. 'session.permission_ids': {
  582. operator: 'is',
  583. value: [ Permission.find_by(name: 'ticket.agent').id.to_s ],
  584. },
  585. })
  586. end
  587. it 'does match' do
  588. expect(result[:matched_workflows]).to include(workflow.id)
  589. end
  590. describe 'for customer' do
  591. let!(:action_user) { create(:customer) } # rubocop:disable RSpec/LetSetup
  592. it 'does not match' do
  593. expect(result[:matched_workflows]).not_to include(workflow.id)
  594. end
  595. end
  596. end
  597. describe '.perform - Condition - Regex match' do
  598. let(:payload) do
  599. base_payload.merge(
  600. 'params' => { 'title' => 'workflow ticket' },
  601. )
  602. end
  603. let!(:workflow) do
  604. create(:core_workflow,
  605. object: 'Ticket',
  606. condition_selected: {
  607. 'ticket.title': {
  608. operator: 'regex match',
  609. value: [ '^workflow' ],
  610. },
  611. })
  612. end
  613. it 'does match' do
  614. expect(result[:matched_workflows]).to include(workflow.id)
  615. end
  616. describe 'for invalid regex' do
  617. let!(:workflow) do
  618. create(:core_workflow,
  619. object: 'Ticket',
  620. condition_selected: {
  621. 'ticket.title': {
  622. operator: 'regex match',
  623. value: [ '^workfluw' ],
  624. },
  625. })
  626. end
  627. it 'does not match' do
  628. expect(result[:matched_workflows]).not_to include(workflow.id)
  629. end
  630. end
  631. end
  632. describe '.perform - Condition - Regex mismatch' do
  633. let(:payload) do
  634. base_payload.merge(
  635. 'params' => { 'title' => 'workflow ticket' },
  636. )
  637. end
  638. let!(:workflow) do
  639. create(:core_workflow,
  640. object: 'Ticket',
  641. condition_selected: {
  642. 'ticket.title': {
  643. operator: 'regex mismatch',
  644. value: [ '^workfluw' ],
  645. },
  646. })
  647. end
  648. it 'does match' do
  649. expect(result[:matched_workflows]).to include(workflow.id)
  650. end
  651. describe 'for invalid regex' do
  652. let!(:workflow) do
  653. create(:core_workflow,
  654. object: 'Ticket',
  655. condition_selected: {
  656. 'ticket.title': {
  657. operator: 'regex mismatch',
  658. value: [ '^workflow' ],
  659. },
  660. })
  661. end
  662. it 'does not match' do
  663. expect(result[:matched_workflows]).not_to include(workflow.id)
  664. end
  665. end
  666. end
  667. describe '.perform - Condition - Contains', db_strategy: :reset do
  668. let(:field_name) { SecureRandom.uuid }
  669. let!(:workflow) do
  670. create(:core_workflow,
  671. object: 'Ticket',
  672. condition_selected: {
  673. "ticket.#{field_name}": {
  674. operator: 'contains',
  675. value: %w[key_1 key_2],
  676. },
  677. })
  678. end
  679. before do
  680. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  681. ObjectManager::Attribute.migration_execute
  682. end
  683. context 'when empty' do
  684. let(:payload) do
  685. base_payload.merge(
  686. 'params' => { field_name => [] },
  687. )
  688. end
  689. it 'does not match' do
  690. expect(result[:matched_workflows]).not_to include(workflow.id)
  691. end
  692. end
  693. context 'when same value' do
  694. let(:payload) do
  695. base_payload.merge(
  696. 'params' => { field_name => %w[key_1 key_2] },
  697. )
  698. end
  699. it 'does match' do
  700. expect(result[:matched_workflows]).to include(workflow.id)
  701. end
  702. end
  703. context 'when 50% value' do
  704. let(:payload) do
  705. base_payload.merge(
  706. 'params' => { field_name => ['key_1'] },
  707. )
  708. end
  709. it 'does match' do
  710. expect(result[:matched_workflows]).to include(workflow.id)
  711. end
  712. end
  713. context 'when value differs' do
  714. let(:payload) do
  715. base_payload.merge(
  716. 'params' => { field_name => ['key_3'] },
  717. )
  718. end
  719. it 'does not match' do
  720. expect(result[:matched_workflows]).not_to include(workflow.id)
  721. end
  722. end
  723. end
  724. describe '.perform - Condition - Contains not', db_strategy: :reset do
  725. let(:field_name) { SecureRandom.uuid }
  726. let!(:workflow) do
  727. create(:core_workflow,
  728. object: 'Ticket',
  729. condition_selected: {
  730. "ticket.#{field_name}": {
  731. operator: 'contains not',
  732. value: %w[key_1 key_2],
  733. },
  734. })
  735. end
  736. before do
  737. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  738. ObjectManager::Attribute.migration_execute
  739. end
  740. context 'when empty' do
  741. let(:payload) do
  742. base_payload.merge(
  743. 'params' => { field_name => [] },
  744. )
  745. end
  746. it 'does match' do
  747. expect(result[:matched_workflows]).to include(workflow.id)
  748. end
  749. end
  750. context 'when same value' do
  751. let(:payload) do
  752. base_payload.merge(
  753. 'params' => { field_name => %w[key_1 key_2] },
  754. )
  755. end
  756. it 'does not match' do
  757. expect(result[:matched_workflows]).not_to include(workflow.id)
  758. end
  759. end
  760. context 'when 50% value' do
  761. let(:payload) do
  762. base_payload.merge(
  763. 'params' => { field_name => ['key_1'] },
  764. )
  765. end
  766. it 'does not match' do
  767. expect(result[:matched_workflows]).not_to include(workflow.id)
  768. end
  769. end
  770. context 'when value differs' do
  771. let(:payload) do
  772. base_payload.merge(
  773. 'params' => { field_name => ['key_3'] },
  774. )
  775. end
  776. it 'does match' do
  777. expect(result[:matched_workflows]).to include(workflow.id)
  778. end
  779. end
  780. end
  781. describe '.perform - Condition - Contains all', db_strategy: :reset do
  782. let(:field_name) { SecureRandom.uuid }
  783. let!(:workflow) do
  784. create(:core_workflow,
  785. object: 'Ticket',
  786. condition_selected: {
  787. "ticket.#{field_name}": {
  788. operator: 'contains all',
  789. value: %w[key_1 key_2],
  790. },
  791. })
  792. end
  793. before do
  794. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  795. ObjectManager::Attribute.migration_execute
  796. end
  797. context 'when empty' do
  798. let(:payload) do
  799. base_payload.merge(
  800. 'params' => { field_name => [] },
  801. )
  802. end
  803. it 'does not match' do
  804. expect(result[:matched_workflows]).not_to include(workflow.id)
  805. end
  806. end
  807. context 'when same value' do
  808. let(:payload) do
  809. base_payload.merge(
  810. 'params' => { field_name => %w[key_1 key_2] },
  811. )
  812. end
  813. it 'does match' do
  814. expect(result[:matched_workflows]).to include(workflow.id)
  815. end
  816. end
  817. context 'when 50% value' do
  818. let(:payload) do
  819. base_payload.merge(
  820. 'params' => { field_name => ['key_1'] },
  821. )
  822. end
  823. it 'does not match' do
  824. expect(result[:matched_workflows]).not_to include(workflow.id)
  825. end
  826. end
  827. context 'when value differs' do
  828. let(:payload) do
  829. base_payload.merge(
  830. 'params' => { field_name => ['key_3'] },
  831. )
  832. end
  833. it 'does not match' do
  834. expect(result[:matched_workflows]).not_to include(workflow.id)
  835. end
  836. end
  837. end
  838. describe '.perform - Condition - Contains all not', db_strategy: :reset do
  839. let(:field_name) { SecureRandom.uuid }
  840. let!(:workflow) do
  841. create(:core_workflow,
  842. object: 'Ticket',
  843. condition_selected: {
  844. "ticket.#{field_name}": {
  845. operator: 'contains all not',
  846. value: %w[key_1 key_2],
  847. },
  848. })
  849. end
  850. before do
  851. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  852. ObjectManager::Attribute.migration_execute
  853. end
  854. context 'when empty' do
  855. let(:payload) do
  856. base_payload.merge(
  857. 'params' => { field_name => [] },
  858. )
  859. end
  860. it 'does match' do
  861. expect(result[:matched_workflows]).to include(workflow.id)
  862. end
  863. end
  864. context 'when same value' do
  865. let(:payload) do
  866. base_payload.merge(
  867. 'params' => { field_name => %w[key_1 key_2] },
  868. )
  869. end
  870. it 'does not match' do
  871. expect(result[:matched_workflows]).not_to include(workflow.id)
  872. end
  873. end
  874. context 'when 50% value' do
  875. let(:payload) do
  876. base_payload.merge(
  877. 'params' => { field_name => ['key_1'] },
  878. )
  879. end
  880. it 'does not match' do
  881. expect(result[:matched_workflows]).not_to include(workflow.id)
  882. end
  883. end
  884. context 'when value differs' do
  885. let(:payload) do
  886. base_payload.merge(
  887. 'params' => { field_name => ['key_3'] },
  888. )
  889. end
  890. it 'does match' do
  891. expect(result[:matched_workflows]).to include(workflow.id)
  892. end
  893. end
  894. end
  895. describe '.perform - Stop after match' do
  896. let(:stop_after_match) { false }
  897. before do
  898. create(:core_workflow,
  899. object: 'Ticket',
  900. perform: {
  901. 'ticket.priority_id': {
  902. operator: 'hide',
  903. hide: 'true'
  904. },
  905. })
  906. create(:core_workflow,
  907. object: 'Ticket',
  908. perform: {
  909. 'ticket.priority_id': {
  910. operator: 'show',
  911. show: 'true'
  912. },
  913. },
  914. stop_after_match: stop_after_match)
  915. create(:core_workflow,
  916. object: 'Ticket',
  917. perform: {
  918. 'ticket.priority_id': {
  919. operator: 'hide',
  920. hide: 'true'
  921. },
  922. })
  923. end
  924. it 'does not stop' do
  925. expect(result[:visibility]['priority_id']).to eq('hide')
  926. end
  927. describe 'with stop_after_match' do
  928. let(:stop_after_match) { true }
  929. it 'does stop' do
  930. expect(result[:visibility]['priority_id']).to eq('show')
  931. end
  932. end
  933. end
  934. describe '.perform - Condition - Custom module' do
  935. let(:modules) { ['CoreWorkflow::Custom::Testa', 'CoreWorkflow::Custom::Testb', 'CoreWorkflow::Custom::Testc'] }
  936. let(:custom_class_false) do
  937. Class.new(CoreWorkflow::Custom::Backend) do
  938. def selected_attribute_match?
  939. false
  940. end
  941. end
  942. end
  943. let(:custom_class_true) do
  944. Class.new(CoreWorkflow::Custom::Backend) do
  945. def selected_attribute_match?
  946. true
  947. end
  948. end
  949. end
  950. let!(:workflow) do
  951. create(:core_workflow,
  952. object: 'Ticket',
  953. condition_selected: {
  954. 'custom.module': {
  955. operator: operator,
  956. value: modules,
  957. },
  958. })
  959. end
  960. describe 'with "match all modules" false' do
  961. let(:operator) { 'match all modules' }
  962. before do
  963. stub_const 'CoreWorkflow::Custom::Testa', custom_class_false
  964. stub_const 'CoreWorkflow::Custom::Testb', custom_class_false
  965. stub_const 'CoreWorkflow::Custom::Testc', custom_class_false
  966. end
  967. it 'does not match' do
  968. expect(result[:matched_workflows]).not_to include(workflow.id)
  969. end
  970. end
  971. describe 'with "match all modules" true' do
  972. let(:operator) { 'match all modules' }
  973. before do
  974. stub_const 'CoreWorkflow::Custom::Testa', custom_class_true
  975. stub_const 'CoreWorkflow::Custom::Testb', custom_class_true
  976. stub_const 'CoreWorkflow::Custom::Testc', custom_class_true
  977. end
  978. it 'does match' do
  979. expect(result[:matched_workflows]).to include(workflow.id)
  980. end
  981. end
  982. describe 'with "match all modules" blank' do
  983. let(:modules) { [] }
  984. let(:operator) { 'match all modules' }
  985. it 'does match' do
  986. expect(result[:matched_workflows]).to include(workflow.id)
  987. end
  988. end
  989. describe 'with "match one module" true' do
  990. let(:operator) { 'match one module' }
  991. before do
  992. stub_const 'CoreWorkflow::Custom::Testa', custom_class_false
  993. stub_const 'CoreWorkflow::Custom::Testb', custom_class_false
  994. stub_const 'CoreWorkflow::Custom::Testc', custom_class_true
  995. end
  996. it 'does match' do
  997. expect(result[:matched_workflows]).to include(workflow.id)
  998. end
  999. end
  1000. describe 'with "match one module" false' do
  1001. let(:operator) { 'match one module' }
  1002. before do
  1003. stub_const 'CoreWorkflow::Custom::Testa', custom_class_false
  1004. stub_const 'CoreWorkflow::Custom::Testb', custom_class_false
  1005. stub_const 'CoreWorkflow::Custom::Testc', custom_class_false
  1006. end
  1007. it 'does not match' do
  1008. expect(result[:matched_workflows]).not_to include(workflow.id)
  1009. end
  1010. end
  1011. describe 'with "match one module" blank' do
  1012. let(:modules) { [] }
  1013. let(:operator) { 'match one module' }
  1014. it 'does match' do
  1015. expect(result[:matched_workflows]).to include(workflow.id)
  1016. end
  1017. end
  1018. describe 'with "match no modules" true' do
  1019. let(:operator) { 'match no modules' }
  1020. before do
  1021. stub_const 'CoreWorkflow::Custom::Testa', custom_class_false
  1022. stub_const 'CoreWorkflow::Custom::Testb', custom_class_false
  1023. stub_const 'CoreWorkflow::Custom::Testc', custom_class_false
  1024. end
  1025. it 'does match' do
  1026. expect(result[:matched_workflows]).to include(workflow.id)
  1027. end
  1028. end
  1029. describe 'with "match no modules" false' do
  1030. let(:operator) { 'match no modules' }
  1031. before do
  1032. stub_const 'CoreWorkflow::Custom::Testa', custom_class_true
  1033. stub_const 'CoreWorkflow::Custom::Testb', custom_class_true
  1034. stub_const 'CoreWorkflow::Custom::Testc', custom_class_true
  1035. end
  1036. it 'does not match' do
  1037. expect(result[:matched_workflows]).not_to include(workflow.id)
  1038. end
  1039. end
  1040. describe 'with "match no modules" blank' do
  1041. let(:modules) { [] }
  1042. let(:operator) { 'match no modules' }
  1043. it 'does match' do
  1044. expect(result[:matched_workflows]).to include(workflow.id)
  1045. end
  1046. end
  1047. end
  1048. describe '.perform - Select' do
  1049. let!(:workflow1) do
  1050. create(:core_workflow,
  1051. object: 'Ticket',
  1052. perform: {
  1053. 'ticket.group_id': {
  1054. operator: 'select',
  1055. select: [ticket.group.id.to_s]
  1056. },
  1057. })
  1058. end
  1059. let!(:workflow2) do
  1060. create(:core_workflow,
  1061. object: 'Ticket',
  1062. condition_selected: {
  1063. 'ticket.group_id': {
  1064. operator: 'is',
  1065. value: ticket.group.id.to_s
  1066. },
  1067. },
  1068. perform: {
  1069. 'ticket.owner_id': {
  1070. operator: 'select',
  1071. select: [action_user.id.to_s]
  1072. },
  1073. })
  1074. end
  1075. it 'does match workflows' do
  1076. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  1077. end
  1078. it 'does select group' do
  1079. expect(result[:select]['group_id']).to eq(ticket.group.id.to_s)
  1080. end
  1081. it 'does select owner (recursion)' do
  1082. expect(result[:select]['owner_id']).to eq(action_user.id.to_s)
  1083. end
  1084. it 'does rerun 2 times (group select + owner select)' do
  1085. expect(result[:rerun_count]).to eq(2)
  1086. end
  1087. end
  1088. describe '.perform - Auto Select' do
  1089. let!(:workflow1) do
  1090. create(:core_workflow,
  1091. object: 'Ticket',
  1092. perform: {
  1093. 'ticket.group_id': {
  1094. operator: 'auto_select',
  1095. auto_select: true
  1096. },
  1097. })
  1098. end
  1099. let!(:workflow2) do
  1100. create(:core_workflow,
  1101. object: 'Ticket',
  1102. condition_selected: {
  1103. 'ticket.group_id': {
  1104. operator: 'is',
  1105. value: ticket.group.id.to_s
  1106. },
  1107. },
  1108. perform: {
  1109. 'ticket.owner_id': {
  1110. operator: 'auto_select',
  1111. auto_select: true
  1112. },
  1113. })
  1114. end
  1115. it 'does match workflows' do
  1116. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  1117. end
  1118. it 'does select group' do
  1119. expect(result[:select]['group_id']).to eq(ticket.group.id.to_s)
  1120. end
  1121. it 'does select owner (recursion)' do
  1122. expect(result[:select]['owner_id']).to eq(action_user.id.to_s)
  1123. end
  1124. it 'does rerun 2 times (group select + owner select)' do
  1125. expect(result[:rerun_count]).to eq(2)
  1126. end
  1127. describe 'with owner' do
  1128. let(:payload) do
  1129. base_payload.merge('params' => {
  1130. 'group_id' => ticket.group.id.to_s,
  1131. 'owner_id' => action_user.id.to_s,
  1132. })
  1133. end
  1134. it 'does not select owner' do
  1135. expect(result[:select]['owner_id']).to be_nil
  1136. end
  1137. it 'does rerun 0 times' do
  1138. expect(result[:rerun_count]).to eq(0)
  1139. end
  1140. end
  1141. end
  1142. describe '.perform - Fill in' do
  1143. let!(:workflow1) do
  1144. create(:core_workflow,
  1145. object: 'Ticket',
  1146. perform: {
  1147. 'ticket.group_id': {
  1148. operator: 'select',
  1149. select: [ticket.group.id.to_s]
  1150. },
  1151. })
  1152. end
  1153. let!(:workflow2) do
  1154. create(:core_workflow,
  1155. object: 'Ticket',
  1156. condition_selected: {
  1157. 'ticket.group_id': {
  1158. operator: 'is',
  1159. value: ticket.group.id.to_s
  1160. },
  1161. },
  1162. perform: {
  1163. 'ticket.title': {
  1164. operator: 'fill_in',
  1165. fill_in: 'hello'
  1166. },
  1167. })
  1168. end
  1169. it 'does match workflows' do
  1170. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  1171. end
  1172. it 'does select group' do
  1173. expect(result[:select]['group_id']).to eq(ticket.group.id.to_s)
  1174. end
  1175. it 'does fill in title' do
  1176. expect(result[:fill_in]['title']).to eq('hello')
  1177. end
  1178. it 'does rerun 1 time (group select + title fill in)' do
  1179. expect(result[:rerun_count]).to eq(1)
  1180. end
  1181. end
  1182. describe '.perform - Fill in empty' do
  1183. let!(:workflow1) do
  1184. create(:core_workflow,
  1185. object: 'Ticket',
  1186. perform: {
  1187. 'ticket.group_id': {
  1188. operator: 'select',
  1189. select: [ticket.group.id.to_s]
  1190. },
  1191. })
  1192. end
  1193. let!(:workflow2) do
  1194. create(:core_workflow,
  1195. object: 'Ticket',
  1196. condition_selected: {
  1197. 'ticket.group_id': {
  1198. operator: 'is',
  1199. value: ticket.group.id.to_s
  1200. },
  1201. },
  1202. perform: {
  1203. 'ticket.title': {
  1204. operator: 'fill_in_empty',
  1205. fill_in_empty: 'hello'
  1206. },
  1207. })
  1208. end
  1209. it 'does match workflows' do
  1210. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  1211. end
  1212. it 'does select group' do
  1213. expect(result[:select]['group_id']).to eq(ticket.group.id.to_s)
  1214. end
  1215. it 'does fill in title' do
  1216. expect(result[:fill_in]['title']).to eq('hello')
  1217. end
  1218. it 'does rerun 1 time (group select + title fill in)' do
  1219. expect(result[:rerun_count]).to eq(1)
  1220. end
  1221. describe 'with title' do
  1222. let(:payload) do
  1223. base_payload.merge('params' => {
  1224. 'title' => 'ha!',
  1225. })
  1226. end
  1227. it 'does not fill in title' do
  1228. expect(result[:fill_in]['title']).to be_nil
  1229. end
  1230. it 'does rerun 1 times (group select)' do
  1231. expect(result[:rerun_count]).to eq(1)
  1232. end
  1233. end
  1234. end
  1235. describe '.perform - Rerun attributes default cache bug' do
  1236. before do
  1237. create(:core_workflow,
  1238. object: 'Ticket',
  1239. perform: {
  1240. 'ticket.group_id': {
  1241. operator: 'select',
  1242. select: [ticket.group.id.to_s]
  1243. },
  1244. })
  1245. create(:core_workflow,
  1246. object: 'Ticket',
  1247. condition_selected: {
  1248. 'ticket.group_id': {
  1249. operator: 'is_set',
  1250. },
  1251. },
  1252. perform: {
  1253. 'ticket.owner_id': {
  1254. operator: 'select',
  1255. select: [action_user.id.to_s]
  1256. },
  1257. })
  1258. create(:core_workflow,
  1259. object: 'Ticket',
  1260. condition_selected: {
  1261. 'ticket.owner_id': {
  1262. operator: 'not_set',
  1263. },
  1264. },
  1265. perform: {
  1266. 'ticket.priority_id': {
  1267. operator: 'hide',
  1268. hide: 'true'
  1269. },
  1270. })
  1271. end
  1272. it 'does not hide priority id' do
  1273. expect(result[:visibility]['priority_id']).to eq('show')
  1274. end
  1275. end
  1276. describe '.perform - Clean up params after restrict values removed selected value by set_fixed_to' do
  1277. let(:payload) do
  1278. base_payload.merge('params' => {
  1279. 'owner_id' => action_user.id,
  1280. })
  1281. end
  1282. before do
  1283. create(:core_workflow,
  1284. object: 'Ticket',
  1285. perform: {
  1286. 'ticket.group_id': {
  1287. operator: 'select',
  1288. select: [ticket.group.id.to_s]
  1289. },
  1290. })
  1291. create(:core_workflow,
  1292. object: 'Ticket',
  1293. condition_selected: {
  1294. 'ticket.group_id': {
  1295. operator: 'is_set',
  1296. },
  1297. },
  1298. perform: {
  1299. 'ticket.owner_id': {
  1300. operator: 'set_fixed_to',
  1301. set_fixed_to: ['']
  1302. },
  1303. })
  1304. create(:core_workflow,
  1305. object: 'Ticket',
  1306. condition_selected: {
  1307. 'ticket.owner_id': {
  1308. operator: 'is_set',
  1309. },
  1310. },
  1311. perform: {
  1312. 'ticket.priority_id': {
  1313. operator: 'hide',
  1314. hide: 'true'
  1315. },
  1316. })
  1317. end
  1318. it 'does not allow owner_id' do
  1319. expect(result[:restrict_values]['owner_id']).not_to include(action_user.id)
  1320. end
  1321. it 'does not hide priority id' do
  1322. expect(result[:visibility]['priority_id']).to eq('show')
  1323. end
  1324. end
  1325. describe '.perform - Clean up params after restrict values removed selected value by remove_option' do
  1326. let(:payload) do
  1327. base_payload.merge('params' => {
  1328. 'owner_id' => action_user.id,
  1329. })
  1330. end
  1331. before do
  1332. create(:core_workflow,
  1333. object: 'Ticket',
  1334. perform: {
  1335. 'ticket.group_id': {
  1336. operator: 'select',
  1337. select: [ticket.group.id.to_s]
  1338. },
  1339. })
  1340. create(:core_workflow,
  1341. object: 'Ticket',
  1342. condition_selected: {
  1343. 'ticket.group_id': {
  1344. operator: 'is_set',
  1345. },
  1346. },
  1347. perform: {
  1348. 'ticket.owner_id': {
  1349. operator: 'remove_option',
  1350. remove_option: [action_user.id]
  1351. },
  1352. })
  1353. create(:core_workflow,
  1354. object: 'Ticket',
  1355. condition_selected: {
  1356. 'ticket.owner_id': {
  1357. operator: 'is_set',
  1358. },
  1359. },
  1360. perform: {
  1361. 'ticket.priority_id': {
  1362. operator: 'hide',
  1363. hide: 'true'
  1364. },
  1365. })
  1366. end
  1367. it 'does not allow owner_id' do
  1368. expect(result[:restrict_values]['owner_id']).not_to include(action_user.id)
  1369. end
  1370. it 'does not hide priority id' do
  1371. expect(result[:visibility]['priority_id']).to eq('show')
  1372. end
  1373. end
  1374. describe '.perform - Clean up params after restrict values removed selected value by default attributes' do
  1375. let(:payload) do
  1376. base_payload.merge('params' => {
  1377. 'owner_id' => action_user.id,
  1378. })
  1379. end
  1380. before do
  1381. create(:core_workflow,
  1382. object: 'Ticket',
  1383. condition_selected: {
  1384. 'ticket.owner_id': {
  1385. operator: 'is_set',
  1386. },
  1387. },
  1388. perform: {
  1389. 'ticket.priority_id': {
  1390. operator: 'hide',
  1391. hide: 'true'
  1392. },
  1393. })
  1394. end
  1395. it 'does not allow owner_id' do
  1396. expect(result[:restrict_values]['owner_id']).not_to include(action_user.id)
  1397. end
  1398. it 'does not hide priority id' do
  1399. expect(result[:visibility]['priority_id']).to eq('show')
  1400. end
  1401. end
  1402. describe '.perform - Default - auto selection based on only_shown_if_selectable' do
  1403. it 'does auto select group' do
  1404. expect(result[:select]['group_id']).not_to be_nil
  1405. end
  1406. it 'does auto hide group' do
  1407. expect(result[:visibility]['group_id']).to eq('hide')
  1408. end
  1409. end
  1410. describe '.perform - One field and two perform actions' do
  1411. before do
  1412. create(:core_workflow,
  1413. object: 'Ticket',
  1414. perform: {
  1415. 'ticket.owner_id': {
  1416. operator: %w[select set_optional],
  1417. select: [action_user.id.to_s],
  1418. set_optional: 'true',
  1419. },
  1420. })
  1421. end
  1422. it 'does auto select owner' do
  1423. expect(result[:select]['owner_id']).to eq(action_user.id.to_s)
  1424. end
  1425. it 'does set owner optional' do
  1426. expect(result[:mandatory]['owner_id']).to be(false)
  1427. end
  1428. end
  1429. describe '.perform - Hide mobile based on user login' do
  1430. let(:base_payload) do
  1431. {
  1432. 'event' => 'core_workflow',
  1433. 'request_id' => 'default',
  1434. 'class_name' => 'User',
  1435. 'screen' => 'create',
  1436. 'params' => {
  1437. 'login' => 'nicole.special@zammad.org',
  1438. },
  1439. }
  1440. end
  1441. before do
  1442. create(:core_workflow,
  1443. object: 'User',
  1444. condition_selected: { 'user.login'=>{ 'operator' => 'is', 'value' => 'nicole.special@zammad.org' } },
  1445. perform: { 'user.mobile'=>{ 'operator' => 'hide', 'hide' => 'true' } },)
  1446. end
  1447. it 'does hide mobile for user' do
  1448. expect(result[:visibility]['mobile']).to eq('hide')
  1449. end
  1450. end
  1451. describe '.perform - Condition - group active is true' do
  1452. let(:payload) do
  1453. base_payload.merge('params' => {
  1454. 'group_id' => Group.first.id,
  1455. })
  1456. end
  1457. let!(:workflow) do
  1458. create(:core_workflow,
  1459. object: 'Ticket',
  1460. condition_selected: { 'group.active'=>{ 'operator' => 'is', 'value' => true } })
  1461. end
  1462. it 'does match' do
  1463. expect(result[:matched_workflows]).to include(workflow.id)
  1464. end
  1465. end
  1466. describe '.perform - Condition - group.assignment_timeout (Integer) matches' do
  1467. let(:group) { create(:group, assignment_timeout: 10) }
  1468. let(:payload) do
  1469. base_payload.merge('params' => {
  1470. 'group_id' => group.id,
  1471. })
  1472. end
  1473. before do
  1474. create(:core_workflow,
  1475. object: 'Ticket',
  1476. condition_selected: { 'group.assignment_timeout'=>{ 'operator' => 'is', 'value' => 10 } },
  1477. perform: { 'ticket.priority_id'=>{ 'operator' => 'hide', 'hide' => 'true' } },)
  1478. end
  1479. it 'does match' do
  1480. expect(result[:visibility]['priority_id']).to eq('hide')
  1481. end
  1482. end
  1483. describe '.perform - Readonly' do
  1484. let!(:workflow1) do
  1485. create(:core_workflow,
  1486. object: 'Ticket',
  1487. perform: {
  1488. 'ticket.group_id': {
  1489. operator: 'set_readonly',
  1490. set_readonly: 'true'
  1491. },
  1492. })
  1493. end
  1494. it 'does match workflow' do
  1495. expect(result[:matched_workflows]).to include(workflow1.id)
  1496. end
  1497. it 'does set group readonly' do
  1498. expect(result[:readonly]['group_id']).to be(true)
  1499. end
  1500. context 'when readonly unset' do
  1501. let!(:workflow2) do
  1502. create(:core_workflow,
  1503. object: 'Ticket',
  1504. perform: {
  1505. 'ticket.group_id': {
  1506. operator: 'unset_readonly',
  1507. unset_readonly: 'true'
  1508. },
  1509. })
  1510. end
  1511. it 'does match workflows' do
  1512. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  1513. end
  1514. it 'does set group readonly' do
  1515. expect(result[:readonly]['group_id']).to be(false)
  1516. end
  1517. end
  1518. end
  1519. describe 'Core Workflow "is not" operator is working unexpected #3752' do
  1520. let(:approval_role) { create(:role) }
  1521. let!(:workflow) do
  1522. create(:core_workflow,
  1523. object: 'Ticket',
  1524. condition_selected: {
  1525. 'session.role_ids': {
  1526. operator: 'is_not',
  1527. value: [ approval_role.id.to_s ]
  1528. },
  1529. })
  1530. end
  1531. context 'when not action user has approval role' do
  1532. let(:action_user) { create(:agent, roles: [Role.find_by(name: 'Agent'), approval_role]) }
  1533. it 'does not match' do
  1534. expect(result[:matched_workflows]).not_to include(workflow.id)
  1535. end
  1536. end
  1537. context 'when action user has not approval role' do
  1538. let(:action_user) { create(:agent) }
  1539. it 'does match' do
  1540. expect(result[:matched_workflows]).to include(workflow.id)
  1541. end
  1542. end
  1543. end
  1544. describe 'Saved conditions break on selections without reloading #3758', db_strategy: :reset do
  1545. let(:field_name) { SecureRandom.uuid }
  1546. let(:screens) do
  1547. {
  1548. edit: {
  1549. 'ticket.agent' => {
  1550. shown: true,
  1551. },
  1552. },
  1553. }
  1554. end
  1555. let!(:workflow) do
  1556. create(:core_workflow,
  1557. object: 'Ticket',
  1558. condition_saved: {
  1559. "ticket.#{field_name}": {
  1560. operator: 'is_not',
  1561. value: 'true',
  1562. },
  1563. })
  1564. end
  1565. let(:payload) do
  1566. base_payload.merge('params' => { 'id' => ticket.id }, 'screen' => 'edit')
  1567. end
  1568. before do
  1569. create(:object_manager_attribute_boolean, object_name: 'Ticket', name: field_name, display: field_name, screens: screens)
  1570. ObjectManager::Attribute.migration_execute
  1571. end
  1572. it 'does match the workflow because saved value is false' do
  1573. expect(result[:matched_workflows]).to include(workflow.id)
  1574. end
  1575. context 'when params contain boolean field true' do
  1576. let(:payload) do
  1577. base_payload.merge('params' => { 'id' => ticket.id, field_name => true }, 'screen' => 'edit')
  1578. end
  1579. it 'does match the workflow because saved value is false' do
  1580. expect(result[:matched_workflows]).to include(workflow.id)
  1581. end
  1582. end
  1583. end
  1584. describe 'Core Workflow: Add organization condition attributes for object User #3779' do
  1585. let(:organization) { create(:organization, note: 'hello') }
  1586. let!(:base_payload) do
  1587. {
  1588. 'event' => 'core_workflow',
  1589. 'request_id' => 'default',
  1590. 'class_name' => 'User',
  1591. 'screen' => 'create',
  1592. 'params' => {},
  1593. }
  1594. end
  1595. let!(:workflow) do
  1596. create(:core_workflow,
  1597. object: 'User',
  1598. condition_selected: {
  1599. 'organization.note': {
  1600. operator: 'is',
  1601. value: 'hello',
  1602. },
  1603. })
  1604. end
  1605. context 'when new user has no organization' do
  1606. it 'does not match the workflow' do
  1607. expect(result[:matched_workflows]).not_to include(workflow.id)
  1608. end
  1609. end
  1610. context 'when new user is part of the organization' do
  1611. let(:payload) do
  1612. base_payload.merge('params' => { 'organization_id' => organization.id.to_s })
  1613. end
  1614. it 'does match the workflow' do
  1615. expect(result[:matched_workflows]).to include(workflow.id)
  1616. end
  1617. end
  1618. end
  1619. describe 'Ticket owner selection is not updated if owner selection should be empty #3809' do
  1620. let(:group_no_owners) { create(:group) }
  1621. let(:payload) do
  1622. base_payload.merge('params' => { 'group_id' => group_no_owners.id })
  1623. end
  1624. before do
  1625. action_user.group_names_access_map = {
  1626. group_no_owners.name => %w[create read change overview],
  1627. }
  1628. end
  1629. it 'does not show any owners because no one has full permissions' do
  1630. expect(result[:restrict_values]['owner_id']).to eq([''])
  1631. end
  1632. end
  1633. describe 'If selected value is not part of the restriction of set_fixed_to it should recalculate it with the new value #3822', db_strategy: :reset do
  1634. let(:field_name1) { SecureRandom.uuid }
  1635. let(:screens) do
  1636. {
  1637. 'create_middle' => {
  1638. 'ticket.agent' => {
  1639. 'shown' => false,
  1640. 'required' => false,
  1641. }
  1642. }
  1643. }
  1644. end
  1645. let!(:workflow1) do
  1646. create(:core_workflow,
  1647. object: 'Ticket',
  1648. perform: { "ticket.#{field_name1}" => { 'operator' => 'set_fixed_to', 'set_fixed_to' => ['key_3'] } })
  1649. end
  1650. let!(:workflow2) do
  1651. create(:core_workflow,
  1652. object: 'Ticket',
  1653. condition_selected: {
  1654. "ticket.#{field_name1}": {
  1655. operator: 'is',
  1656. value: 'key_3',
  1657. },
  1658. })
  1659. end
  1660. before do
  1661. create(:object_manager_attribute_select, name: field_name1, display: field_name1, screens: screens)
  1662. ObjectManager::Attribute.migration_execute
  1663. end
  1664. it 'does select key_3 as new param value and based on this executes workflow 2' do
  1665. expect(result[:matched_workflows]).to include(workflow1.id, workflow2.id)
  1666. end
  1667. end
  1668. describe 'Add clear selection action or has changed condition #3821' do
  1669. let!(:workflow_has_changed) do
  1670. create(:core_workflow,
  1671. object: 'Ticket',
  1672. condition_selected: {
  1673. 'ticket.priority_id': {
  1674. operator: 'has_changed',
  1675. },
  1676. })
  1677. end
  1678. let!(:workflow_changed_to) do
  1679. create(:core_workflow,
  1680. object: 'Ticket',
  1681. condition_selected: {
  1682. 'ticket.priority_id': {
  1683. operator: 'changed_to',
  1684. value: [ Ticket::Priority.find_by(name: '3 high').id.to_s ]
  1685. },
  1686. })
  1687. end
  1688. context 'when priority changed' do
  1689. let(:payload) do
  1690. base_payload.merge('last_changed_attribute' => 'priority_id', 'params' => { 'priority_id' => Ticket::Priority.find_by(name: '3 high').id.to_s })
  1691. end
  1692. it 'does match on condition has changed' do
  1693. expect(result[:matched_workflows]).to include(workflow_has_changed.id)
  1694. end
  1695. it 'does match on condition changed to' do
  1696. expect(result[:matched_workflows]).to include(workflow_changed_to.id)
  1697. end
  1698. end
  1699. context 'when nothing changed' do
  1700. it 'does not match on condition has changed' do
  1701. expect(result[:matched_workflows]).not_to include(workflow_has_changed.id)
  1702. end
  1703. it 'does not match on condition changed to' do
  1704. expect(result[:matched_workflows]).not_to include(workflow_changed_to.id)
  1705. end
  1706. end
  1707. context 'when state changed' do
  1708. let(:payload) do
  1709. base_payload.merge('last_changed_attribute' => 'state_id')
  1710. end
  1711. it 'does not match on condition has changed' do
  1712. expect(result[:matched_workflows]).not_to include(workflow_has_changed.id)
  1713. end
  1714. it 'does not match on condition changed to' do
  1715. expect(result[:matched_workflows]).not_to include(workflow_changed_to.id)
  1716. end
  1717. end
  1718. end
  1719. describe 'Wrong core workflow execution because of missing relation defaults #4541' do
  1720. let!(:workflow) do
  1721. create(:core_workflow,
  1722. object: 'Ticket',
  1723. condition_selected: {
  1724. 'ticket.priority_id': {
  1725. operator: 'is',
  1726. value: [ Ticket::Priority.find_by(name: '1 low').id.to_s ]
  1727. },
  1728. })
  1729. end
  1730. before do
  1731. Ticket::Priority.find_by(name: '2 normal').update(note: 'Test')
  1732. workflow
  1733. end
  1734. it 'does not execute the core workflow because the default priority is 2 normal and not 1 low' do
  1735. expect(result[:matched_workflows]).not_to include(workflow.id)
  1736. end
  1737. end
  1738. describe 'Core Workflow: Readded options are not usable in conditions #4763' do
  1739. before do
  1740. workflow_1 && workflow_2
  1741. end
  1742. context 'when single value' do
  1743. let(:workflow_1) do
  1744. create(:core_workflow,
  1745. object: 'Ticket',
  1746. perform: {
  1747. 'ticket.priority_id': {
  1748. operator: 'remove_option',
  1749. remove_option: Ticket::Priority.pluck(:id).map(&:to_s),
  1750. },
  1751. })
  1752. end
  1753. let(:workflow_2) do
  1754. create(:core_workflow,
  1755. object: 'Ticket',
  1756. perform: {
  1757. 'ticket.priority_id': {
  1758. operator: 'add_option',
  1759. add_option: Ticket::Priority.pluck(:id).map(&:to_s),
  1760. },
  1761. })
  1762. end
  1763. let!(:workflow_3) do
  1764. create(:core_workflow,
  1765. object: 'Ticket',
  1766. condition_selected: {
  1767. 'ticket.priority_id': {
  1768. operator: 'is',
  1769. value: Ticket::Priority.where(name: '3 high').pluck(:id).map(&:to_s),
  1770. },
  1771. })
  1772. end
  1773. let(:payload) do
  1774. base_payload.merge('params' => { 'priority_id' => Ticket::Priority.find_by(name: '3 high').id.to_s })
  1775. end
  1776. it 'does match' do
  1777. expect(result[:matched_workflows]).to include(workflow_3.id)
  1778. end
  1779. end
  1780. context 'when multiple value', db_strategy: :reset do
  1781. let(:workflow_1) do
  1782. create(:core_workflow,
  1783. object: 'Ticket',
  1784. perform: {
  1785. "ticket.#{field_name}": {
  1786. operator: 'remove_option',
  1787. remove_option: ['key_1'],
  1788. },
  1789. })
  1790. end
  1791. let(:workflow_2) do
  1792. create(:core_workflow,
  1793. object: 'Ticket',
  1794. perform: {
  1795. "ticket.#{field_name}": {
  1796. operator: 'add_option',
  1797. add_option: ['key_1'],
  1798. },
  1799. })
  1800. end
  1801. let!(:workflow_3) do
  1802. create(:core_workflow,
  1803. object: 'Ticket',
  1804. condition_selected: {
  1805. "ticket.#{field_name}": {
  1806. operator: 'is',
  1807. value: ['key_1'],
  1808. },
  1809. })
  1810. end
  1811. let(:payload) do
  1812. base_payload.merge('params' => { field_name => ['key_1'] })
  1813. end
  1814. let(:field_name) { SecureRandom.uuid }
  1815. before do
  1816. create(:object_manager_attribute_multiselect, name: field_name, display: field_name)
  1817. ObjectManager::Attribute.migration_execute
  1818. end
  1819. it 'does match' do
  1820. expect(result[:matched_workflows]).to include(workflow_3.id)
  1821. end
  1822. end
  1823. end
  1824. end