ticket_trigger_test.rb 173 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684
  1. require 'test_helper'
  2. class TicketTriggerTest < ActiveSupport::TestCase
  3. setup do
  4. Setting.set('ticket_trigger_recursive', true)
  5. end
  6. test '1 basic' do
  7. Trigger.create!(
  8. name: 'aaa loop check',
  9. condition: {
  10. 'article.subject' => {
  11. 'operator' => 'contains',
  12. 'value' => 'Thanks for your inquiry',
  13. },
  14. },
  15. perform: {
  16. 'ticket.tags' => {
  17. 'operator' => 'add',
  18. 'value' => 'should_not_loop',
  19. },
  20. 'notification.email' => {
  21. 'body' => 'some lala',
  22. 'recipient' => 'ticket_customer',
  23. 'subject' => 'Thanks for your inquiry - loop check - only once (#{ticket.title})!',
  24. },
  25. },
  26. disable_notification: true,
  27. active: true,
  28. created_by_id: 1,
  29. updated_by_id: 1,
  30. )
  31. Trigger.create!(
  32. name: 'auto reply',
  33. condition: {
  34. 'ticket.action' => {
  35. 'operator' => 'is',
  36. 'value' => 'create',
  37. },
  38. 'ticket.state_id' => {
  39. 'operator' => 'is',
  40. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  41. }
  42. },
  43. perform: {
  44. 'notification.email' => {
  45. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  46. 'recipient' => 'ticket_customer',
  47. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  48. },
  49. 'ticket.priority_id' => {
  50. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  51. },
  52. 'ticket.tags' => {
  53. 'operator' => 'add',
  54. 'value' => 'aa, kk',
  55. },
  56. },
  57. disable_notification: true,
  58. active: true,
  59. created_by_id: 1,
  60. updated_by_id: 1,
  61. )
  62. Trigger.create!(
  63. name: 'auto tag 1',
  64. condition: {
  65. 'ticket.action' => {
  66. 'operator' => 'is',
  67. 'value' => 'update',
  68. },
  69. 'ticket.state_id' => {
  70. 'operator' => 'is',
  71. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  72. }
  73. },
  74. perform: {
  75. 'ticket.priority_id' => {
  76. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  77. },
  78. 'ticket.tags' => {
  79. 'operator' => 'remove',
  80. 'value' => 'kk',
  81. },
  82. },
  83. disable_notification: true,
  84. active: true,
  85. created_by_id: 1,
  86. updated_by_id: 1,
  87. )
  88. Trigger.create!(
  89. name: 'auto tag 2',
  90. condition: {
  91. 'ticket.state_id' => {
  92. 'operator' => 'is',
  93. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  94. }
  95. },
  96. perform: {
  97. 'ticket.tags' => {
  98. 'operator' => 'add',
  99. 'value' => 'abc',
  100. },
  101. },
  102. disable_notification: true,
  103. active: true,
  104. created_by_id: 1,
  105. updated_by_id: 1,
  106. )
  107. Trigger.create!(
  108. name: 'not matching',
  109. condition: {
  110. 'ticket.state_id' => {
  111. 'operator' => 'is',
  112. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  113. }
  114. },
  115. perform: {
  116. 'ticket.priority_id' => {
  117. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  118. },
  119. },
  120. disable_notification: true,
  121. active: true,
  122. created_by_id: 1,
  123. updated_by_id: 1,
  124. )
  125. Trigger.create!(
  126. name: 'zzz last',
  127. condition: {
  128. 'article.subject' => {
  129. 'operator' => 'contains',
  130. 'value' => 'some subject 1234',
  131. },
  132. },
  133. perform: {
  134. 'ticket.tags' => {
  135. 'operator' => 'add',
  136. 'value' => 'article_create_trigger',
  137. },
  138. 'notification.email' => {
  139. 'body' => 'some lala',
  140. 'recipient' => 'ticket_customer',
  141. 'subject' => 'Thanks for your inquiry - 1234 check (#{ticket.title})!',
  142. },
  143. },
  144. disable_notification: true,
  145. active: true,
  146. created_by_id: 1,
  147. updated_by_id: 1,
  148. )
  149. ticket1 = Ticket.create!(
  150. title: "some <b>title</b>\n äöüß",
  151. group: Group.lookup(name: 'Users'),
  152. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  153. updated_by_id: 1,
  154. created_by_id: 1,
  155. )
  156. Ticket::Article.create!(
  157. ticket_id: ticket1.id,
  158. from: 'some_sender@example.com',
  159. to: 'some_recipient@example.com',
  160. subject: 'some subject',
  161. message_id: 'some@id',
  162. body: "some message <b>note</b>\nnew line",
  163. internal: false,
  164. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  165. type: Ticket::Article::Type.find_by(name: 'note'),
  166. updated_by_id: 1,
  167. created_by_id: 1,
  168. )
  169. ticket1.reload
  170. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  171. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  172. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  173. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  174. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  175. assert_equal([], ticket1.tag_list)
  176. TransactionDispatcher.commit
  177. ticket1.reload
  178. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  179. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  180. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  181. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  182. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  183. assert_equal(%w[aa kk should_not_loop abc], ticket1.tag_list)
  184. article1 = ticket1.articles.second
  185. assert_match('Zammad <zammad@localhost>', article1.from)
  186. assert_match('nicole.braun@zammad.org', article1.to)
  187. assert_match('Thanks for your inquiry (some <b>title</b> äöüß)!', article1.subject)
  188. assert_match('Braun<br>some &lt;b&gt;title&lt;/b&gt;', article1.body)
  189. assert_match('&gt; some message &lt;b&gt;note&lt;/b&gt;<br>&gt; new line', article1.body)
  190. assert_equal('text/html', article1.content_type)
  191. article1 = ticket1.articles.last
  192. assert_match('Zammad <zammad@localhost>', article1.from)
  193. assert_match('nicole.braun@zammad.org', article1.to)
  194. assert_match('Thanks for your inquiry - loop check - only once (some <b>title</b> äöüß)!', article1.subject)
  195. assert_match('some lala', article1.body)
  196. assert_equal('text/html', article1.content_type)
  197. ticket1.priority = Ticket::Priority.lookup(name: '2 normal')
  198. ticket1.save!
  199. TransactionDispatcher.commit
  200. ticket1.reload
  201. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  202. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  203. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  204. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  205. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  206. assert_equal(%w[aa kk should_not_loop abc], ticket1.tag_list)
  207. ticket1.state = Ticket::State.lookup(name: 'open')
  208. ticket1.save!
  209. TransactionDispatcher.commit
  210. ticket1.reload
  211. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  212. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  213. assert_equal('open', ticket1.state.name, 'ticket1.state verify')
  214. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  215. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  216. assert_equal(%w[aa kk should_not_loop abc], ticket1.tag_list)
  217. ticket1.state = Ticket::State.lookup(name: 'new')
  218. ticket1.save!
  219. TransactionDispatcher.commit
  220. ticket1.reload
  221. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  222. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  223. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  224. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  225. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  226. assert_equal(%w[aa should_not_loop abc], ticket1.tag_list)
  227. ticket2 = Ticket.create!(
  228. title: "some title\n äöüß",
  229. group: Group.lookup(name: 'Users'),
  230. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  231. state: Ticket::State.lookup(name: 'open'),
  232. priority: Ticket::Priority.lookup(name: '2 normal'),
  233. updated_by_id: 1,
  234. created_by_id: 1,
  235. )
  236. assert_equal('some title äöüß', ticket2.title, 'ticket2.title verify')
  237. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  238. assert_equal('open', ticket2.state.name, 'ticket2.state verify')
  239. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  240. assert_equal(0, ticket2.articles.count, 'ticket2.articles verify')
  241. assert_equal([], ticket2.tag_list)
  242. TransactionDispatcher.commit
  243. ticket2.reload
  244. assert_equal('some title äöüß', ticket2.title, 'ticket2.title verify')
  245. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  246. assert_equal('open', ticket2.state.name, 'ticket2.state verify')
  247. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  248. assert_equal(0, ticket2.articles.count, 'ticket2.articles verify')
  249. assert_equal([], ticket2.tag_list)
  250. ticket3 = Ticket.create!(
  251. title: "some <b>title</b>\n äöüß3",
  252. group: Group.lookup(name: 'Users'),
  253. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  254. updated_by_id: 1,
  255. created_by_id: 1,
  256. )
  257. assert(ticket3, 'ticket3 created')
  258. Ticket::Article.create!(
  259. ticket_id: ticket3.id,
  260. from: 'some_sender@example.com',
  261. to: 'some_recipient@example.com',
  262. subject: 'some subject 1234',
  263. message_id: 'some@id',
  264. content_type: 'text/html',
  265. body: 'some message <b>note</b><br>new line',
  266. internal: false,
  267. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  268. type: Ticket::Article::Type.find_by(name: 'note'),
  269. updated_by_id: 1,
  270. created_by_id: 1,
  271. )
  272. assert_equal('some <b>title</b> äöüß3', ticket3.title, 'ticket3.title verify')
  273. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  274. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  275. assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify')
  276. assert_equal(1, ticket3.articles.count, 'ticket3.articles verify')
  277. assert_equal([], ticket3.tag_list)
  278. TransactionDispatcher.commit
  279. ticket3.reload
  280. assert_equal('some <b>title</b> äöüß3', ticket3.title, 'ticket3.title verify')
  281. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  282. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  283. assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify')
  284. assert_equal(4, ticket3.articles.count, 'ticket3.articles verify')
  285. assert_equal(%w[aa kk should_not_loop abc article_create_trigger], ticket3.tag_list)
  286. article3 = ticket3.articles[1]
  287. assert_match('Zammad <zammad@localhost>', article3.from)
  288. assert_match('nicole.braun@zammad.org', article3.to)
  289. assert_match('Thanks for your inquiry (some <b>title</b> äöüß3)!', article3.subject)
  290. assert_match('Braun<br>some &lt;b&gt;title&lt;/b&gt;', article3.body)
  291. assert_match('&gt; some message note<br>&gt; new line', article3.body)
  292. assert_no_match('&gt; some message &lt;b&gt;note&lt;/b&gt;<br>&gt; new line', article3.body)
  293. assert_equal('text/html', article3.content_type)
  294. article3 = ticket3.articles[2]
  295. assert_match('Zammad <zammad@localhost>', article3.from)
  296. assert_match('nicole.braun@zammad.org', article3.to)
  297. assert_match('Thanks for your inquiry - loop check - only once (some <b>title</b>', article3.subject)
  298. assert_match('some lala', article3.body)
  299. assert_equal('text/html', article3.content_type)
  300. article4 = ticket3.articles[3]
  301. assert_match('Zammad <zammad@localhost>', article4.from)
  302. assert_match('nicole.braun@zammad.org', article4.to)
  303. assert_match('Thanks for your inquiry - 1234 check (some <b>title</b> äöüß3)!', article4.subject)
  304. assert_equal('text/html', article4.content_type)
  305. Ticket::Article.create!(
  306. ticket_id: ticket3.id,
  307. from: 'some_sender@example.com',
  308. to: 'some_recipient@example.com',
  309. subject: 'some subject - not 1234',
  310. message_id: 'some@id',
  311. content_type: 'text/html',
  312. body: 'some message <b>note</b><br>new line',
  313. internal: false,
  314. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  315. type: Ticket::Article::Type.find_by(name: 'note'),
  316. updated_by_id: 1,
  317. created_by_id: 1,
  318. )
  319. TransactionDispatcher.commit
  320. ticket3.reload
  321. assert_equal('some <b>title</b> äöüß3', ticket3.title, 'ticket3.title verify')
  322. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  323. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  324. assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify')
  325. assert_equal(5, ticket3.articles.count, 'ticket3.articles verify')
  326. assert_equal(%w[aa should_not_loop abc article_create_trigger], ticket3.tag_list)
  327. Ticket::Article.create!(
  328. ticket_id: ticket3.id,
  329. from: 'some_sender@example.com',
  330. to: 'some_recipient@example.com',
  331. subject: 'some subject NOT 1234',
  332. message_id: 'some@id',
  333. content_type: 'text/html',
  334. body: 'some message <b>note</b><br>new line',
  335. internal: false,
  336. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  337. type: Ticket::Article::Type.find_by(name: 'note'),
  338. updated_by_id: 1,
  339. created_by_id: 1,
  340. )
  341. TransactionDispatcher.commit
  342. ticket3.reload
  343. assert_equal('some <b>title</b> äöüß3', ticket3.title, 'ticket3.title verify')
  344. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  345. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  346. assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify')
  347. assert_equal(6, ticket3.articles.count, 'ticket3.articles verify')
  348. assert_equal(%w[aa should_not_loop abc article_create_trigger], ticket3.tag_list)
  349. Ticket::Article.create!(
  350. ticket_id: ticket3.id,
  351. from: 'some_sender@example.com',
  352. to: 'some_recipient@example.com',
  353. subject: 'some subject 1234',
  354. message_id: 'some@id',
  355. content_type: 'text/html',
  356. body: 'some message <b>note</b><br>new line',
  357. internal: false,
  358. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  359. type: Ticket::Article::Type.find_by(name: 'note'),
  360. updated_by_id: 1,
  361. created_by_id: 1,
  362. )
  363. TransactionDispatcher.commit
  364. ticket3.reload
  365. assert_equal('some <b>title</b> äöüß3', ticket3.title, 'ticket3.title verify')
  366. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  367. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  368. assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify')
  369. assert_equal(9, ticket3.articles.count, 'ticket3.articles verify')
  370. assert_equal(%w[aa should_not_loop abc article_create_trigger], ticket3.tag_list)
  371. end
  372. test '2 actions - create' do
  373. Trigger.create!(
  374. name: 'auto reply',
  375. condition: {
  376. 'ticket.action' => {
  377. 'operator' => 'is',
  378. 'value' => 'create',
  379. },
  380. 'ticket.state_id' => {
  381. 'operator' => 'is',
  382. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  383. }
  384. },
  385. perform: {
  386. 'notification.email' => {
  387. 'body' => 'dasdasdasd',
  388. 'recipient' => 'ticket_customer',
  389. 'subject' => 'asdasdas',
  390. },
  391. 'ticket.priority_id' => {
  392. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  393. },
  394. },
  395. disable_notification: true,
  396. active: true,
  397. created_by_id: 1,
  398. updated_by_id: 1,
  399. )
  400. ticket1 = Ticket.create!(
  401. title: "some title\n äöüß",
  402. group: Group.lookup(name: 'Users'),
  403. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  404. updated_by_id: 1,
  405. created_by_id: 1,
  406. )
  407. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  408. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  409. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  410. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  411. assert_equal(0, ticket1.articles.count, 'ticket1.articles verify')
  412. TransactionDispatcher.commit
  413. ticket1.reload
  414. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  415. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  416. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  417. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  418. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  419. article1 = ticket1.articles.last
  420. assert_match('Zammad <zammad@localhost>', article1.from)
  421. assert_match('nicole.braun@zammad.org', article1.to)
  422. assert_match('asdasdas', article1.subject)
  423. assert_match('dasdasdasd', article1.body)
  424. assert_equal('text/html', article1.content_type)
  425. ticket1.priority = Ticket::Priority.lookup(name: '2 normal')
  426. ticket1.save!
  427. TransactionDispatcher.commit
  428. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  429. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  430. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  431. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  432. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  433. ticket1.state = Ticket::State.lookup(name: 'open')
  434. ticket1.save!
  435. TransactionDispatcher.commit
  436. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  437. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  438. assert_equal('open', ticket1.state.name, 'ticket1.state verify')
  439. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  440. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  441. ticket1.state = Ticket::State.lookup(name: 'new')
  442. ticket1.save!
  443. TransactionDispatcher.commit
  444. ticket1.reload
  445. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  446. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  447. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  448. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  449. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  450. end
  451. test '2 actions - update' do
  452. Trigger.create!(
  453. name: 'auto reply',
  454. condition: {
  455. 'ticket.action' => {
  456. 'operator' => 'is',
  457. 'value' => 'update',
  458. },
  459. 'ticket.state_id' => {
  460. 'operator' => 'is',
  461. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  462. }
  463. },
  464. perform: {
  465. 'notification.email' => {
  466. 'body' => 'dasdasdasd',
  467. 'recipient' => 'ticket_customer',
  468. 'subject' => 'asdasdas',
  469. },
  470. 'ticket.priority_id' => {
  471. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  472. },
  473. },
  474. disable_notification: true,
  475. active: true,
  476. created_by_id: 1,
  477. updated_by_id: 1,
  478. )
  479. ticket1 = Ticket.create!(
  480. title: "some title\n äöüß",
  481. group: Group.lookup(name: 'Users'),
  482. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  483. updated_by_id: 1,
  484. created_by_id: 1,
  485. )
  486. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  487. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  488. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  489. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  490. assert_equal(0, ticket1.articles.count, 'ticket1.articles verify')
  491. TransactionDispatcher.commit
  492. ticket1.reload
  493. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  494. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  495. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  496. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  497. assert_equal(0, ticket1.articles.count, 'ticket1.articles verify')
  498. ticket1.priority = Ticket::Priority.lookup(name: '2 normal')
  499. ticket1.save!
  500. TransactionDispatcher.commit
  501. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  502. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  503. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  504. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  505. assert_equal(0, ticket1.articles.count, 'ticket1.articles verify')
  506. ticket1.state = Ticket::State.lookup(name: 'open')
  507. ticket1.save!
  508. TransactionDispatcher.commit
  509. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  510. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  511. assert_equal('open', ticket1.state.name, 'ticket1.state verify')
  512. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  513. assert_equal(0, ticket1.articles.count, 'ticket1.articles verify')
  514. ticket1.state = Ticket::State.lookup(name: 'new')
  515. ticket1.save!
  516. TransactionDispatcher.commit
  517. ticket1.reload
  518. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  519. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  520. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  521. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  522. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  523. end
  524. test '3 auto replys' do
  525. roles = Role.where(name: 'Customer')
  526. User.create!(
  527. login: 'postmaster@example.com',
  528. firstname: 'Trigger',
  529. lastname: 'Customer1',
  530. email: 'postmaster@example.com',
  531. password: 'customerpw',
  532. active: true,
  533. roles: roles,
  534. updated_at: '2015-02-05 16:37:00',
  535. updated_by_id: 1,
  536. created_by_id: 1,
  537. )
  538. User.create!(
  539. login: 'ticket-auto-reply-customer2@example.com',
  540. firstname: 'Trigger',
  541. lastname: 'Customer2',
  542. email: 'ticket-auto-reply-customer2@example.com',
  543. password: 'customerpw',
  544. active: true,
  545. organization_id: nil,
  546. roles: roles,
  547. updated_at: '2015-02-05 16:37:00',
  548. updated_by_id: 1,
  549. created_by_id: 1,
  550. )
  551. Trigger.create!(
  552. name: 'auto reply - new ticket',
  553. condition: {
  554. 'ticket.action' => {
  555. 'operator' => 'is',
  556. 'value' => 'create',
  557. },
  558. 'ticket.state_id' => {
  559. 'operator' => 'is not',
  560. 'value' => Ticket::State.lookup(name: 'closed').id,
  561. },
  562. 'article.type_id' => {
  563. 'operator' => 'is',
  564. 'value' => [
  565. Ticket::Article::Type.lookup(name: 'email').id,
  566. Ticket::Article::Type.lookup(name: 'phone').id,
  567. Ticket::Article::Type.lookup(name: 'web').id,
  568. ],
  569. },
  570. },
  571. perform: {
  572. 'notification.email' => {
  573. 'body' => '<p>Your request (Ticket##{ticket.number}) has been received and will be reviewed by our support staff.<p>
  574. <br/>
  575. <p>To provide additional information, please reply to this email or click on the following link:
  576. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  577. </p>
  578. <br/>
  579. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  580. 'recipient' => 'ticket_customer',
  581. 'subject' => 'Thanks for your inquiry (#{ticket.title})',
  582. },
  583. },
  584. disable_notification: true,
  585. active: true,
  586. created_by_id: 1,
  587. updated_by_id: 1,
  588. )
  589. Trigger.create!(
  590. name: 'auto reply (on follow-up of tickets)',
  591. condition: {
  592. 'ticket.action' => {
  593. 'operator' => 'is',
  594. 'value' => 'update',
  595. },
  596. 'article.sender_id' => {
  597. 'operator' => 'is',
  598. 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id,
  599. },
  600. 'article.type_id' => {
  601. 'operator' => 'is',
  602. 'value' => [
  603. Ticket::Article::Type.lookup(name: 'email').id,
  604. Ticket::Article::Type.lookup(name: 'phone').id,
  605. Ticket::Article::Type.lookup(name: 'web').id,
  606. ],
  607. },
  608. },
  609. perform: {
  610. 'notification.email' => {
  611. 'body' => '<p>Your follow-up for (#{config.ticket_hook}##{ticket.number}) has been received and will be reviewed by our support staff.<p>
  612. <br/>
  613. <p>To provide additional information, please reply to this email or click on the following link:
  614. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  615. </p>
  616. <br/>
  617. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  618. 'recipient' => 'ticket_customer',
  619. 'subject' => 'Thanks for your follow-up (#{ticket.title})',
  620. },
  621. },
  622. active: true,
  623. created_by_id: 1,
  624. updated_by_id: 1,
  625. )
  626. Trigger.create!(
  627. name: 'not matching',
  628. condition: {
  629. 'ticket.action' => {
  630. 'operator' => 'is',
  631. 'value' => 'create',
  632. },
  633. 'ticket.state_id' => {
  634. 'operator' => 'is',
  635. 'value' => Ticket::State.lookup(name: 'closed').id.to_s,
  636. }
  637. },
  638. perform: {
  639. 'notification.email' => {
  640. 'body' => '2some text<br>#{ticket.customer.lastname}<br>#{ticket.title}',
  641. 'recipient' => 'ticket_customer',
  642. 'subject' => '2Thanks for your inquiry (#{ticket.title})!',
  643. },
  644. },
  645. disable_notification: true,
  646. active: true,
  647. created_by_id: 1,
  648. updated_by_id: 1,
  649. )
  650. # process mail without Precedence header
  651. content = File.read(Rails.root.join('test/data/ticket_trigger/mail1.box'))
  652. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, content)
  653. assert_equal('aaäöüßad asd', ticket_p.title)
  654. assert_equal('Users', ticket_p.group.name)
  655. assert_equal('new', ticket_p.state.name)
  656. assert_equal(2, ticket_p.articles.count)
  657. article_p = ticket_p.articles.last
  658. assert_match('Thanks for your inquiry (aaäöüßad asd)', article_p.subject)
  659. assert_match('Zammad <zammad@localhost>', article_p.from)
  660. assert_no_match('config\.', article_p.body)
  661. assert_match('http://zammad.example.com', article_p.body)
  662. assert_no_match('ticket.', article_p.body)
  663. assert_match(ticket_p.number, article_p.body)
  664. assert_equal('text/html', article_p.content_type)
  665. ticket_p.priority = Ticket::Priority.lookup(name: '2 normal')
  666. ticket_p.save!
  667. TransactionDispatcher.commit
  668. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  669. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  670. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  671. assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify')
  672. assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify')
  673. Ticket::Article.create!(
  674. ticket_id: ticket_p.id,
  675. from: 'some_sender@example.com',
  676. to: 'some_recipient@example.com',
  677. subject: 'some subject',
  678. message_id: 'some@id',
  679. body: 'some message note',
  680. internal: false,
  681. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  682. type: Ticket::Article::Type.find_by(name: 'note'),
  683. updated_by_id: 1,
  684. created_by_id: 1,
  685. )
  686. TransactionDispatcher.commit
  687. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  688. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  689. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  690. assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify')
  691. assert_equal(3, ticket_p.articles.count, 'ticket_p.articles verify')
  692. Ticket::Article.create!(
  693. ticket_id: ticket_p.id,
  694. from: 'some_sender@example.com',
  695. to: 'some_recipient@example.com',
  696. subject: 'some subject',
  697. message_id: 'some@id',
  698. body: 'some message note',
  699. internal: false,
  700. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  701. type: Ticket::Article::Type.find_by(name: 'email'),
  702. updated_by_id: 1,
  703. created_by_id: 1,
  704. )
  705. TransactionDispatcher.commit
  706. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  707. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  708. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  709. assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify')
  710. assert_equal(4, ticket_p.articles.count, 'ticket_p.articles verify')
  711. Ticket::Article.create!(
  712. ticket_id: ticket_p.id,
  713. from: 'some_sender@example.com',
  714. to: 'some_recipient@example.com',
  715. subject: 'some subject',
  716. message_id: 'some@id',
  717. body: 'some message note',
  718. internal: false,
  719. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  720. type: Ticket::Article::Type.find_by(name: 'email'),
  721. updated_by_id: 1,
  722. created_by_id: 1,
  723. )
  724. TransactionDispatcher.commit
  725. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  726. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  727. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  728. assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify')
  729. assert_equal(6, ticket_p.articles.count, 'ticket_p.articles verify')
  730. article_p = ticket_p.articles.last
  731. assert_match('Thanks for your follow-up (aaäöüßad asd)', article_p.subject)
  732. assert_match('Zammad <zammad@localhost>', article_p.from)
  733. assert_no_match('config\.', article_p.body)
  734. assert_match('http://zammad.example.com', article_p.body)
  735. assert_no_match('ticket.', article_p.body)
  736. assert_match(ticket_p.number, article_p.body)
  737. assert_equal('text/html', article_p.content_type)
  738. ticket_p.state = Ticket::State.lookup(name: 'open')
  739. ticket_p.save!
  740. Ticket::Article.create!(
  741. ticket_id: ticket_p.id,
  742. from: 'some_sender@example.com',
  743. to: 'some_recipient@example.com',
  744. subject: 'some subject',
  745. message_id: 'some@id',
  746. body: 'some message note',
  747. internal: false,
  748. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  749. type: Ticket::Article::Type.find_by(name: 'email'),
  750. updated_by_id: 1,
  751. created_by_id: 1,
  752. )
  753. TransactionDispatcher.commit
  754. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  755. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  756. assert_equal('open', ticket_p.state.name, 'ticket_p.state verify')
  757. assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify')
  758. assert_equal(8, ticket_p.articles.count, 'ticket_p.articles verify')
  759. article_p = ticket_p.articles.last
  760. assert_match('Thanks for your follow-up (aaäöüßad asd)', article_p.subject)
  761. assert_match('Zammad <zammad@localhost>', article_p.from)
  762. assert_no_match('config\.', article_p.body)
  763. assert_match('http://zammad.example.com', article_p.body)
  764. assert_no_match('ticket.', article_p.body)
  765. assert_match(ticket_p.number, article_p.body)
  766. assert_equal('text/html', article_p.content_type)
  767. # process mail without Precedence header
  768. content = File.read(Rails.root.join('test/data/ticket_trigger/mail1.box'))
  769. ticket_p1, _article_p1, _user_p1, _mail = Channel::EmailParser.new.process({}, content)
  770. assert_not_equal(ticket_p.id, ticket_p1.id)
  771. assert_equal('new', ticket_p1.state.name)
  772. assert_equal(2, ticket_p1.articles.count)
  773. # process mail with Precedence header (no auto response)
  774. content = File.read(Rails.root.join('test/data/ticket_trigger/mail2.box'))
  775. ticket_p2, _article_p2, _user_p2, _mail = Channel::EmailParser.new.process({}, content)
  776. assert_not_equal(ticket_p.id, ticket_p1.id)
  777. assert_not_equal(ticket_p.id, ticket_p2.id)
  778. assert_not_equal(ticket_p1.id, ticket_p2.id)
  779. assert_equal('new', ticket_p2.state.name)
  780. assert_equal(1, ticket_p2.articles.count)
  781. # process mail with abuse@ (no auto response)
  782. content = File.read(Rails.root.join('test/data/ticket_trigger/mail3.box'))
  783. ticket_p3, _article_p3, _user_p3, _mail = Channel::EmailParser.new.process({}, content)
  784. assert_not_equal(ticket_p.id, ticket_p1.id)
  785. assert_not_equal(ticket_p.id, ticket_p2.id)
  786. assert_not_equal(ticket_p.id, ticket_p3.id)
  787. assert_not_equal(ticket_p1.id, ticket_p2.id)
  788. assert_not_equal(ticket_p1.id, ticket_p3.id)
  789. assert_not_equal(ticket_p2.id, ticket_p1.id)
  790. assert_not_equal(ticket_p2.id, ticket_p3.id)
  791. assert_equal('new', ticket_p3.state.name)
  792. assert_equal(1, ticket_p3.articles.count)
  793. end
  794. test '4 has changed' do
  795. roles = Role.where(name: 'Customer')
  796. User.create!(
  797. login: 'postmaster@example.com',
  798. firstname: 'Trigger',
  799. lastname: 'Customer1',
  800. email: 'postmaster@example.com',
  801. password: 'customerpw',
  802. active: true,
  803. roles: roles,
  804. updated_at: '2015-02-05 16:37:00',
  805. updated_by_id: 1,
  806. created_by_id: 1,
  807. )
  808. User.create!(
  809. login: 'ticket-has-changed-customer2@example.com',
  810. firstname: 'Trigger',
  811. lastname: 'Customer2',
  812. email: 'ticket-has-changed-customer2@example.com',
  813. password: 'customerpw',
  814. active: true,
  815. organization_id: nil,
  816. roles: roles,
  817. updated_at: '2015-02-05 16:37:00',
  818. updated_by_id: 1,
  819. created_by_id: 1,
  820. )
  821. groups = Group.where(name: 'Users')
  822. roles = Role.where(name: 'Agent')
  823. agent1 = User.create!(
  824. login: 'agent-has-changed@example.com',
  825. firstname: 'Has Changed',
  826. lastname: 'Agent1',
  827. email: 'agent-has-changed@example.com',
  828. password: 'agentpw',
  829. active: true,
  830. roles: roles,
  831. groups: groups,
  832. updated_at: '2015-02-05 16:37:00',
  833. updated_by_id: 1,
  834. created_by_id: 1,
  835. )
  836. Trigger.create!(
  837. name: 'owner update - to customer',
  838. condition: {
  839. 'ticket.owner_id' => {
  840. 'operator' => 'has changed',
  841. 'pre_condition' => 'current_user.id',
  842. 'value' => '',
  843. 'value_completion' => '',
  844. }
  845. },
  846. perform: {
  847. 'notification.email' => {
  848. 'body' => '<p>The owner of ticket (Ticket##{ticket.number}) has changed.<p>
  849. <br/>
  850. <p>To provide additional information, please reply to this email or click on the following link:
  851. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  852. </p>
  853. <br/>
  854. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  855. 'recipient' => 'ticket_customer',
  856. 'subject' => 'Owner has changed (#{ticket.title})',
  857. },
  858. },
  859. disable_notification: true,
  860. active: true,
  861. created_by_id: 1,
  862. updated_by_id: 1,
  863. )
  864. # process mail without Precedence header
  865. content = File.read(Rails.root.join('test/data/ticket_trigger/mail1.box'))
  866. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, content)
  867. assert_equal('aaäöüßad asd', ticket_p.title)
  868. assert_equal('Users', ticket_p.group.name)
  869. assert_equal('new', ticket_p.state.name)
  870. assert_equal(1, ticket_p.articles.count)
  871. ticket_p.articles.last
  872. TransactionDispatcher.commit
  873. ticket_p.owner = agent1
  874. ticket_p.save!
  875. TransactionDispatcher.commit
  876. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  877. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  878. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  879. assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify')
  880. assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify')
  881. #p ticket_p.articles.last.inspect
  882. article_p = ticket_p.articles.last
  883. assert_match('Owner has changed', article_p.subject)
  884. assert_match('Zammad <zammad@localhost>', article_p.from)
  885. assert_match('martin@example.com', article_p.to)
  886. assert_no_match('config\.', article_p.body)
  887. assert_match('http://zammad.example.com', article_p.body)
  888. assert_no_match('ticket.', article_p.body)
  889. assert_match(ticket_p.number, article_p.body)
  890. assert_equal('text/html', article_p.content_type)
  891. Trigger.create_or_update(
  892. name: 'owner update - to customer',
  893. condition: {
  894. 'ticket.owner_id' => {
  895. 'operator' => 'has changed',
  896. 'pre_condition' => 'current_user.id',
  897. 'value' => '',
  898. 'value_completion' => '',
  899. },
  900. 'ticket.priority_id' => {
  901. 'operator' => 'is',
  902. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  903. },
  904. },
  905. perform: {
  906. 'notification.email' => {
  907. 'body' => '<p>The owner of ticket (Ticket##{ticket.number}) has changed.<p>
  908. <br/>
  909. <p>To provide additional information, please reply to this email or click on the following link:
  910. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  911. </p>
  912. <br/>
  913. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  914. 'recipient' => 'ticket_customer',
  915. 'subject' => 'Owner has changed (#{ticket.title})',
  916. },
  917. },
  918. disable_notification: true,
  919. active: true,
  920. created_by_id: 1,
  921. updated_by_id: 1,
  922. )
  923. # process mail without Precedence header
  924. content = File.read(Rails.root.join('test/data/ticket_trigger/mail1.box'))
  925. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, content)
  926. assert_equal('aaäöüßad asd', ticket_p.title)
  927. assert_equal('Users', ticket_p.group.name)
  928. assert_equal('new', ticket_p.state.name)
  929. assert_equal(1, ticket_p.articles.count)
  930. TransactionDispatcher.commit
  931. assert_equal(1, ticket_p.articles.count)
  932. ticket_p.priority = Ticket::Priority.lookup(name: '1 low')
  933. ticket_p.save!
  934. TransactionDispatcher.commit
  935. assert_equal(1, ticket_p.articles.count)
  936. ticket_p.priority = Ticket::Priority.lookup(name: '3 high')
  937. ticket_p.save!
  938. TransactionDispatcher.commit
  939. assert_equal(1, ticket_p.articles.count)
  940. ticket_p.owner = agent1
  941. ticket_p.save!
  942. TransactionDispatcher.commit
  943. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  944. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  945. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  946. assert_equal('3 high', ticket_p.priority.name, 'ticket_p.priority verify')
  947. assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify')
  948. #p ticket_p.articles.last.inspect
  949. article_p = ticket_p.articles.last
  950. assert_match('Owner has changed', article_p.subject)
  951. assert_match('Zammad <zammad@localhost>', article_p.from)
  952. assert_match('martin@example.com', article_p.to)
  953. assert_no_match('config\.', article_p.body)
  954. assert_match('http://zammad.example.com', article_p.body)
  955. assert_no_match('ticket.', article_p.body)
  956. assert_match(ticket_p.number, article_p.body)
  957. assert_equal('text/html', article_p.content_type)
  958. # should trigger
  959. Trigger.create_or_update(
  960. name: 'owner update - to customer',
  961. condition: {
  962. 'ticket.owner_id' => {
  963. 'operator' => 'has changed',
  964. 'pre_condition' => 'current_user.id',
  965. 'value' => '',
  966. 'value_completion' => '',
  967. },
  968. 'ticket.priority_id' => {
  969. 'operator' => 'is',
  970. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  971. },
  972. 'ticket.action' => {
  973. 'operator' => 'is not',
  974. 'value' => 'create',
  975. },
  976. },
  977. perform: {
  978. 'notification.email' => {
  979. 'body' => '<p>The owner of ticket (Ticket##{ticket.number}) has changed.<p>
  980. <br/>
  981. <p>To provide additional information, please reply to this email or click on the following link:
  982. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  983. </p>
  984. <br/>
  985. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  986. 'recipient' => 'ticket_customer',
  987. 'subject' => 'Owner has changed (#{ticket.title})',
  988. },
  989. },
  990. disable_notification: true,
  991. active: true,
  992. created_by_id: 1,
  993. updated_by_id: 1,
  994. )
  995. # process mail without Precedence header
  996. content = File.read(Rails.root.join('test/data/ticket_trigger/mail1.box'))
  997. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, content)
  998. assert_equal('aaäöüßad asd', ticket_p.title)
  999. assert_equal('Users', ticket_p.group.name)
  1000. assert_equal('new', ticket_p.state.name)
  1001. assert_equal(1, ticket_p.articles.count)
  1002. TransactionDispatcher.commit
  1003. assert_equal(1, ticket_p.articles.count)
  1004. ticket_p.priority = Ticket::Priority.lookup(name: '1 low')
  1005. ticket_p.save!
  1006. TransactionDispatcher.commit
  1007. assert_equal(1, ticket_p.articles.count)
  1008. ticket_p.priority = Ticket::Priority.lookup(name: '3 high')
  1009. ticket_p.save!
  1010. TransactionDispatcher.commit
  1011. assert_equal(1, ticket_p.articles.count)
  1012. ticket_p.owner = agent1
  1013. ticket_p.save!
  1014. TransactionDispatcher.commit
  1015. assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify')
  1016. assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify')
  1017. assert_equal('new', ticket_p.state.name, 'ticket_p.state verify')
  1018. assert_equal('3 high', ticket_p.priority.name, 'ticket_p.priority verify')
  1019. assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify')
  1020. #p ticket_p.articles.last.inspect
  1021. article_p = ticket_p.articles.last
  1022. assert_match('Owner has changed', article_p.subject)
  1023. assert_match('Zammad <zammad@localhost>', article_p.from)
  1024. assert_match('martin@example.com', article_p.to)
  1025. assert_no_match('config\.', article_p.body)
  1026. assert_match('http://zammad.example.com', article_p.body)
  1027. assert_no_match('ticket.', article_p.body)
  1028. assert_match(ticket_p.number, article_p.body)
  1029. assert_equal('text/html', article_p.content_type)
  1030. # should not trigger
  1031. Trigger.create_or_update(
  1032. name: 'owner update - to customer',
  1033. condition: {
  1034. 'ticket.owner_id' => {
  1035. 'operator' => 'has changed',
  1036. 'pre_condition' => 'current_user.id',
  1037. 'value' => '',
  1038. 'value_completion' => '',
  1039. },
  1040. 'ticket.action' => {
  1041. 'operator' => 'is',
  1042. 'value' => 'create',
  1043. },
  1044. },
  1045. perform: {
  1046. 'notification.email' => {
  1047. 'body' => '<p>The owner of ticket (Ticket##{ticket.number}) has changed.<p>
  1048. <br/>
  1049. <p>To provide additional information, please reply to this email or click on the following link:
  1050. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  1051. </p>
  1052. <br/>
  1053. <p><i><a href="http://zammad.com">Zammad</a>, your customer support system</i></p>',
  1054. 'recipient' => 'ticket_customer',
  1055. 'subject' => 'Owner has changed (#{ticket.title})',
  1056. },
  1057. },
  1058. disable_notification: true,
  1059. active: true,
  1060. created_by_id: 1,
  1061. updated_by_id: 1,
  1062. )
  1063. # process mail without Precedence header
  1064. content = File.read(Rails.root.join('test/data/ticket_trigger/mail1.box'))
  1065. ticket_p, _article_p, _user_p, _mail = Channel::EmailParser.new.process({}, content)
  1066. assert_equal(1, ticket_p.articles.count)
  1067. TransactionDispatcher.commit
  1068. assert_equal(1, ticket_p.articles.count)
  1069. ticket_p.owner = agent1
  1070. ticket_p.save!
  1071. TransactionDispatcher.commit
  1072. assert_equal(1, ticket_p.articles.count)
  1073. end
  1074. test '5 notify owner' do
  1075. Trigger.create!(
  1076. name: 'aaa notify mail',
  1077. condition: {
  1078. 'ticket.state_id' => {
  1079. 'operator' => 'is',
  1080. 'value' => Ticket::State.all.pluck(:id),
  1081. },
  1082. 'ticket.action' => {
  1083. 'operator' => 'is',
  1084. 'value' => 'update',
  1085. },
  1086. },
  1087. perform: {
  1088. 'notification.email' => {
  1089. 'body' => 'some lala',
  1090. 'recipient' => 'ticket_owner',
  1091. 'subject' => 'CC NOTE (#{ticket.title})!',
  1092. },
  1093. },
  1094. disable_notification: true,
  1095. active: true,
  1096. created_by_id: 1,
  1097. updated_by_id: 1,
  1098. )
  1099. groups = Group.where(name: 'Users')
  1100. roles = Role.where(name: 'Agent')
  1101. agent = User.create!(
  1102. login: 'agent@example.com',
  1103. firstname: 'Trigger',
  1104. lastname: 'Agent1',
  1105. email: 'agent@example.com',
  1106. password: 'agentpw',
  1107. active: true,
  1108. roles: roles,
  1109. groups: groups,
  1110. updated_by_id: 1,
  1111. created_by_id: 1,
  1112. )
  1113. ticket1 = Ticket.create!(
  1114. title: 'test 123',
  1115. owner: agent,
  1116. group: Group.lookup(name: 'Users'),
  1117. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  1118. updated_by_id: 1,
  1119. created_by_id: 1,
  1120. )
  1121. Ticket::Article.create!(
  1122. ticket_id: ticket1.id,
  1123. from: 'some_sender@example.com',
  1124. to: 'some_recipient@example.com',
  1125. subject: 'some subject',
  1126. message_id: 'some@id',
  1127. body: "some message <b>note</b>\nnew line",
  1128. internal: false,
  1129. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1130. type: Ticket::Article::Type.find_by(name: 'note'),
  1131. updated_by_id: 1,
  1132. created_by_id: 1,
  1133. )
  1134. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1135. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1136. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1137. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1138. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1139. assert_equal([], ticket1.tag_list)
  1140. TransactionDispatcher.commit
  1141. assert_equal(1, ticket1.articles.count)
  1142. Ticket::Article.create!(
  1143. ticket_id: ticket1.id,
  1144. from: 'some_sender@example.com',
  1145. to: 'some_recipient@example.com',
  1146. subject: 'update',
  1147. message_id: 'some@id',
  1148. content_type: 'text/html',
  1149. body: 'update',
  1150. internal: false,
  1151. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1152. type: Ticket::Article::Type.find_by(name: 'note'),
  1153. updated_by_id: 1,
  1154. created_by_id: 1,
  1155. )
  1156. TransactionDispatcher.commit
  1157. assert_equal(3, ticket1.articles.count)
  1158. Trigger.create!(
  1159. name: 'aaa notify mail 2',
  1160. condition: {
  1161. 'ticket.state_id' => {
  1162. 'operator' => 'is',
  1163. 'value' => Ticket::State.all.pluck(:id),
  1164. },
  1165. 'ticket.action' => {
  1166. 'operator' => 'is',
  1167. 'value' => 'update',
  1168. },
  1169. },
  1170. perform: {
  1171. 'notification.email' => {
  1172. 'body' => 'some lala',
  1173. 'recipient' => 'ticket_owner',
  1174. 'subject' => 'CC NOTE (#{ticket.title})!',
  1175. },
  1176. },
  1177. disable_notification: true,
  1178. active: true,
  1179. created_by_id: 1,
  1180. updated_by_id: 1,
  1181. )
  1182. Ticket::Article.create!(
  1183. ticket_id: ticket1.id,
  1184. from: 'some_sender@example.com',
  1185. to: 'some_recipient@example.com',
  1186. subject: 'update',
  1187. message_id: 'some@id',
  1188. content_type: 'text/html',
  1189. body: 'update',
  1190. internal: false,
  1191. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1192. type: Ticket::Article::Type.find_by(name: 'note'),
  1193. updated_by_id: 1,
  1194. created_by_id: 1,
  1195. )
  1196. TransactionDispatcher.commit
  1197. assert_equal(6, ticket1.articles.count)
  1198. end
  1199. test '6 owner auto assignment' do
  1200. Trigger.create!(
  1201. name: 'aaa auto assignment',
  1202. condition: {
  1203. 'ticket.owner_id' => {
  1204. 'operator' => 'is',
  1205. 'pre_condition' => 'not_set',
  1206. 'value' => '',
  1207. 'value_completion' => '',
  1208. },
  1209. 'ticket.action' => {
  1210. 'operator' => 'is',
  1211. 'value' => 'update',
  1212. },
  1213. },
  1214. perform: {
  1215. 'ticket.owner_id' => {
  1216. 'pre_condition' => 'current_user.id',
  1217. 'value' => '',
  1218. 'value_completion' => '',
  1219. },
  1220. },
  1221. disable_notification: true,
  1222. active: true,
  1223. created_by_id: 1,
  1224. updated_by_id: 1,
  1225. )
  1226. groups = Group.where(name: 'Users')
  1227. roles = Role.where(name: 'Agent')
  1228. agent = User.create!(
  1229. login: 'agent@example.com',
  1230. firstname: 'Trigger',
  1231. lastname: 'Agent1',
  1232. email: 'agent@example.com',
  1233. password: 'agentpw',
  1234. active: true,
  1235. roles: roles,
  1236. groups: groups,
  1237. updated_by_id: 1,
  1238. created_by_id: 1,
  1239. )
  1240. ticket1 = Ticket.create!(
  1241. title: 'test 123',
  1242. #owner: agent,
  1243. group: Group.lookup(name: 'Users'),
  1244. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  1245. updated_by_id: 1,
  1246. created_by_id: 1,
  1247. )
  1248. Ticket::Article.create!(
  1249. ticket_id: ticket1.id,
  1250. from: 'some_sender@example.com',
  1251. to: 'some_recipient@example.com',
  1252. subject: 'some subject',
  1253. message_id: 'some@id',
  1254. body: "some message <b>note</b>\nnew line",
  1255. internal: false,
  1256. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1257. type: Ticket::Article::Type.find_by(name: 'note'),
  1258. updated_by_id: 1,
  1259. created_by_id: 1,
  1260. )
  1261. TransactionDispatcher.commit
  1262. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1263. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1264. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1265. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1266. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1267. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1268. assert_equal([], ticket1.tag_list)
  1269. UserInfo.current_user_id = agent.id
  1270. Ticket::Article.create!(
  1271. ticket_id: ticket1.id,
  1272. from: 'some_sender@example.com',
  1273. to: 'some_recipient@example.com',
  1274. subject: 'update',
  1275. message_id: 'some@id',
  1276. content_type: 'text/html',
  1277. body: 'update',
  1278. internal: false,
  1279. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1280. type: Ticket::Article::Type.find_by(name: 'note'),
  1281. )
  1282. TransactionDispatcher.commit
  1283. UserInfo.current_user_id = nil
  1284. ticket1.reload
  1285. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1286. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1287. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1288. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1289. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1290. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1291. assert_equal([], ticket1.tag_list)
  1292. UserInfo.current_user_id = agent.id
  1293. ticket1.owner_id = 1
  1294. ticket1.save!
  1295. TransactionDispatcher.commit
  1296. UserInfo.current_user_id = nil
  1297. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1298. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1299. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1300. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1301. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1302. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1303. assert_equal([], ticket1.tag_list)
  1304. end
  1305. test '6.1 owner auto assignment based on organization' do
  1306. Trigger.create!(
  1307. name: 'aaa auto assignment',
  1308. condition: {
  1309. 'ticket.organization_id' => {
  1310. 'operator' => 'is not',
  1311. 'pre_condition' => 'not_set',
  1312. 'value' => '',
  1313. 'value_completion' => '',
  1314. },
  1315. 'ticket.action' => {
  1316. 'operator' => 'is',
  1317. 'value' => 'update',
  1318. },
  1319. },
  1320. perform: {
  1321. 'ticket.owner_id' => {
  1322. 'pre_condition' => 'current_user.id',
  1323. 'value' => '',
  1324. 'value_completion' => '',
  1325. },
  1326. },
  1327. disable_notification: true,
  1328. active: true,
  1329. created_by_id: 1,
  1330. updated_by_id: 1,
  1331. )
  1332. roles = Role.where(name: 'Agent')
  1333. groups = Group.where(name: 'Users')
  1334. agent = User.create!(
  1335. login: 'agent@example.com',
  1336. firstname: 'Trigger',
  1337. lastname: 'Agent1',
  1338. email: 'agent@example.com',
  1339. password: 'agentpw',
  1340. active: true,
  1341. roles: roles,
  1342. groups: groups,
  1343. updated_by_id: 1,
  1344. created_by_id: 1,
  1345. )
  1346. roles = Role.where(name: 'Customer')
  1347. customer = User.create!(
  1348. login: 'customer@example.com',
  1349. firstname: 'Trigger',
  1350. lastname: 'Customer1',
  1351. email: 'customer@example.com',
  1352. password: 'customerpw',
  1353. vip: true,
  1354. active: true,
  1355. roles: roles,
  1356. updated_by_id: 1,
  1357. created_by_id: 1,
  1358. )
  1359. ticket1 = Ticket.create!(
  1360. title: 'test 123',
  1361. group: Group.lookup(name: 'Users'),
  1362. customer: customer,
  1363. updated_by_id: 1,
  1364. created_by_id: 1,
  1365. )
  1366. Ticket::Article.create!(
  1367. ticket_id: ticket1.id,
  1368. from: 'some_sender@example.com',
  1369. to: 'some_recipient@example.com',
  1370. subject: 'some subject',
  1371. message_id: 'some@id',
  1372. body: "some message <b>note</b>\nnew line",
  1373. internal: false,
  1374. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1375. type: Ticket::Article::Type.find_by(name: 'note'),
  1376. updated_by_id: 1,
  1377. created_by_id: 1,
  1378. )
  1379. TransactionDispatcher.commit
  1380. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1381. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1382. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1383. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1384. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1385. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1386. assert_equal([], ticket1.tag_list)
  1387. ticket1.update!(customer: User.lookup(email: 'nicole.braun@zammad.org') )
  1388. UserInfo.current_user_id = agent.id
  1389. Ticket::Article.create!(
  1390. ticket_id: ticket1.id,
  1391. from: 'some_sender@example.com',
  1392. to: 'some_recipient@example.com',
  1393. subject: 'update',
  1394. message_id: 'some@id',
  1395. content_type: 'text/html',
  1396. body: 'update',
  1397. internal: false,
  1398. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1399. type: Ticket::Article::Type.find_by(name: 'note'),
  1400. )
  1401. TransactionDispatcher.commit
  1402. UserInfo.current_user_id = nil
  1403. ticket1.reload
  1404. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1405. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1406. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1407. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1408. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1409. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1410. assert_equal([], ticket1.tag_list)
  1411. end
  1412. test '6.2 owner auto assignment based on organization' do
  1413. Trigger.create!(
  1414. name: 'aaa auto assignment',
  1415. condition: {
  1416. 'ticket.organization_id' => {
  1417. 'operator' => 'is',
  1418. 'pre_condition' => 'not_set',
  1419. 'value' => '',
  1420. 'value_completion' => '',
  1421. },
  1422. 'ticket.action' => {
  1423. 'operator' => 'is',
  1424. 'value' => 'update',
  1425. },
  1426. },
  1427. perform: {
  1428. 'ticket.owner_id' => {
  1429. 'pre_condition' => 'current_user.id',
  1430. 'value' => '',
  1431. 'value_completion' => '',
  1432. },
  1433. },
  1434. disable_notification: true,
  1435. active: true,
  1436. created_by_id: 1,
  1437. updated_by_id: 1,
  1438. )
  1439. groups = Group.where(name: 'Users')
  1440. roles = Role.where(name: 'Agent')
  1441. agent = User.create!(
  1442. login: 'agent@example.com',
  1443. firstname: 'Trigger',
  1444. lastname: 'Agent1',
  1445. email: 'agent@example.com',
  1446. password: 'agentpw',
  1447. active: true,
  1448. roles: roles,
  1449. groups: groups,
  1450. updated_by_id: 1,
  1451. created_by_id: 1,
  1452. )
  1453. roles = Role.where(name: 'Customer')
  1454. customer = User.create!(
  1455. login: 'customer@example.com',
  1456. firstname: 'Trigger',
  1457. lastname: 'Customer1',
  1458. email: 'customer@example.com',
  1459. password: 'customerpw',
  1460. vip: true,
  1461. active: true,
  1462. roles: roles,
  1463. updated_by_id: 1,
  1464. created_by_id: 1,
  1465. )
  1466. ticket1 = Ticket.create!(
  1467. title: 'test 123',
  1468. group: Group.lookup(name: 'Users'),
  1469. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  1470. updated_by_id: 1,
  1471. created_by_id: 1,
  1472. )
  1473. Ticket::Article.create!(
  1474. ticket_id: ticket1.id,
  1475. from: 'some_sender@example.com',
  1476. to: 'some_recipient@example.com',
  1477. subject: 'some subject',
  1478. message_id: 'some@id',
  1479. body: "some message <b>note</b>\nnew line",
  1480. internal: false,
  1481. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1482. type: Ticket::Article::Type.find_by(name: 'note'),
  1483. updated_by_id: 1,
  1484. created_by_id: 1,
  1485. )
  1486. TransactionDispatcher.commit
  1487. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1488. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1489. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1490. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1491. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1492. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1493. assert_equal([], ticket1.tag_list)
  1494. ticket1.update!(customer: customer)
  1495. UserInfo.current_user_id = agent.id
  1496. Ticket::Article.create!(
  1497. ticket_id: ticket1.id,
  1498. from: 'some_sender@example.com',
  1499. to: 'some_recipient@example.com',
  1500. subject: 'update',
  1501. message_id: 'some@id',
  1502. content_type: 'text/html',
  1503. body: 'update',
  1504. internal: false,
  1505. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1506. type: Ticket::Article::Type.find_by(name: 'note'),
  1507. )
  1508. TransactionDispatcher.commit
  1509. UserInfo.current_user_id = nil
  1510. ticket1.reload
  1511. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1512. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1513. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1514. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1515. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1516. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1517. assert_equal([], ticket1.tag_list)
  1518. end
  1519. test '7 owner auto assignment' do
  1520. Trigger.create!(
  1521. name: 'aaa auto assignment',
  1522. condition: {
  1523. 'ticket.owner_id' => {
  1524. 'operator' => 'is',
  1525. 'pre_condition' => 'not_set',
  1526. 'value' => '',
  1527. 'value_completion' => '',
  1528. },
  1529. 'article.type_id' => {
  1530. 'operator' => 'is',
  1531. 'value' => Ticket::Article::Type.find_by(name: 'note'),
  1532. },
  1533. 'article.sender_id' => {
  1534. 'operator' => 'is',
  1535. 'value' => Ticket::Article::Sender.find_by(name: 'Agent'),
  1536. },
  1537. },
  1538. perform: {
  1539. 'ticket.owner_id' => {
  1540. 'pre_condition' => 'current_user.id',
  1541. 'value' => '',
  1542. 'value_completion' => '',
  1543. },
  1544. },
  1545. disable_notification: true,
  1546. active: true,
  1547. created_by_id: 1,
  1548. updated_by_id: 1,
  1549. )
  1550. groups = Group.where(name: 'Users')
  1551. roles = Role.where(name: 'Agent')
  1552. agent1 = User.create!(
  1553. login: 'agent@example.com',
  1554. firstname: 'Trigger',
  1555. lastname: 'Agent1',
  1556. email: 'agent@example.com',
  1557. password: 'agentpw',
  1558. active: true,
  1559. roles: roles,
  1560. groups: groups,
  1561. updated_by_id: 1,
  1562. created_by_id: 1,
  1563. )
  1564. agent2 = User.create!(
  1565. login: 'agent2@example.com',
  1566. firstname: 'Trigger',
  1567. lastname: 'Agent2',
  1568. email: 'agent2@example.com',
  1569. password: 'agentpw',
  1570. active: true,
  1571. roles: roles,
  1572. groups: groups,
  1573. updated_by_id: 1,
  1574. created_by_id: 1,
  1575. )
  1576. ticket1 = Ticket.create!(
  1577. title: 'test 123',
  1578. #owner: agent,
  1579. group: Group.lookup(name: 'Users'),
  1580. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  1581. updated_by_id: 1,
  1582. created_by_id: 1,
  1583. )
  1584. Ticket::Article.create!(
  1585. ticket_id: ticket1.id,
  1586. from: 'some_sender@example.com',
  1587. to: 'some_recipient@example.com',
  1588. subject: 'some subject',
  1589. message_id: 'some@id',
  1590. body: "some message <b>note</b>\nnew line",
  1591. internal: false,
  1592. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1593. type: Ticket::Article::Type.find_by(name: 'note'),
  1594. updated_by_id: 1,
  1595. created_by_id: 1,
  1596. )
  1597. TransactionDispatcher.commit
  1598. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1599. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1600. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1601. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1602. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1603. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1604. assert_equal([], ticket1.tag_list)
  1605. UserInfo.current_user_id = agent1.id
  1606. Ticket::Article.create!(
  1607. ticket_id: ticket1.id,
  1608. from: 'some_sender@example.com',
  1609. to: 'some_recipient@example.com',
  1610. subject: 'update',
  1611. message_id: 'some@id',
  1612. content_type: 'text/html',
  1613. body: 'update',
  1614. internal: false,
  1615. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1616. type: Ticket::Article::Type.find_by(name: 'note'),
  1617. )
  1618. TransactionDispatcher.commit
  1619. UserInfo.current_user_id = nil
  1620. ticket1.reload
  1621. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1622. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1623. assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1624. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1625. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1626. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1627. assert_equal([], ticket1.tag_list)
  1628. UserInfo.current_user_id = agent1.id
  1629. ticket1.owner_id = 1
  1630. ticket1.save!
  1631. TransactionDispatcher.commit
  1632. UserInfo.current_user_id = nil
  1633. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1634. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1635. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1636. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1637. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1638. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1639. assert_equal([], ticket1.tag_list)
  1640. UserInfo.current_user_id = agent1.id
  1641. Ticket::Article.create!(
  1642. ticket_id: ticket1.id,
  1643. from: 'some_sender@example.com',
  1644. to: 'some_recipient@example.com',
  1645. subject: 'update',
  1646. message_id: 'some@id',
  1647. content_type: 'text/html',
  1648. body: 'update',
  1649. internal: false,
  1650. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  1651. type: Ticket::Article::Type.find_by(name: 'note'),
  1652. )
  1653. TransactionDispatcher.commit
  1654. UserInfo.current_user_id = nil
  1655. ticket1.reload
  1656. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1657. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1658. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1659. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1660. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1661. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  1662. assert_equal([], ticket1.tag_list)
  1663. UserInfo.current_user_id = agent2.id
  1664. ticket1.owner_id = agent2.id
  1665. ticket1.save!
  1666. TransactionDispatcher.commit
  1667. UserInfo.current_user_id = nil
  1668. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1669. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1670. assert_equal(agent2.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1671. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1672. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1673. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  1674. assert_equal([], ticket1.tag_list)
  1675. UserInfo.current_user_id = agent1.id
  1676. Ticket::Article.create!(
  1677. ticket_id: ticket1.id,
  1678. from: 'some_sender@example.com',
  1679. to: 'some_recipient@example.com',
  1680. subject: 'update',
  1681. message_id: 'some@id',
  1682. content_type: 'text/html',
  1683. body: 'update',
  1684. internal: false,
  1685. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1686. type: Ticket::Article::Type.find_by(name: 'note'),
  1687. )
  1688. TransactionDispatcher.commit
  1689. UserInfo.current_user_id = nil
  1690. ticket1.reload
  1691. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1692. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1693. assert_equal(agent2.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1694. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1695. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1696. assert_equal(4, ticket1.articles.count, 'ticket1.articles verify')
  1697. assert_equal([], ticket1.tag_list)
  1698. end
  1699. test '8 owner auto assignment' do
  1700. Trigger.create!(
  1701. name: 'aaa auto assignment',
  1702. condition: {
  1703. 'ticket.owner_id' => {
  1704. 'operator' => 'is',
  1705. 'pre_condition' => 'not_set',
  1706. 'value' => '',
  1707. 'value_completion' => '',
  1708. },
  1709. 'ticket.priority_id' => {
  1710. 'operator' => 'has changed',
  1711. 'pre_condition' => '',
  1712. 'value' => '2',
  1713. 'value_completion' => '',
  1714. },
  1715. 'ticket.action' => {
  1716. 'operator' => 'is',
  1717. 'value' => 'update',
  1718. },
  1719. },
  1720. perform: {
  1721. 'ticket.owner_id' => {
  1722. 'pre_condition' => 'current_user.id',
  1723. 'value' => '',
  1724. 'value_completion' => '',
  1725. },
  1726. },
  1727. disable_notification: true,
  1728. active: true,
  1729. created_by_id: 1,
  1730. updated_by_id: 1,
  1731. )
  1732. groups = Group.where(name: 'Users')
  1733. roles = Role.where(name: 'Agent')
  1734. agent = User.create!(
  1735. login: 'agent@example.com',
  1736. firstname: 'Trigger',
  1737. lastname: 'Agent1',
  1738. email: 'agent@example.com',
  1739. password: 'agentpw',
  1740. active: true,
  1741. roles: roles,
  1742. groups: groups,
  1743. updated_by_id: 1,
  1744. created_by_id: 1,
  1745. )
  1746. ticket1 = Ticket.create!(
  1747. title: 'test 123',
  1748. #owner: agent,
  1749. group: Group.lookup(name: 'Users'),
  1750. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  1751. updated_by_id: 1,
  1752. created_by_id: 1,
  1753. )
  1754. Ticket::Article.create!(
  1755. ticket_id: ticket1.id,
  1756. from: 'some_sender@example.com',
  1757. to: 'some_recipient@example.com',
  1758. subject: 'some subject',
  1759. message_id: 'some@id',
  1760. body: "some message <b>note</b>\nnew line",
  1761. internal: false,
  1762. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1763. type: Ticket::Article::Type.find_by(name: 'note'),
  1764. updated_by_id: 1,
  1765. created_by_id: 1,
  1766. )
  1767. TransactionDispatcher.commit
  1768. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1769. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1770. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1771. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1772. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1773. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1774. assert_equal([], ticket1.tag_list)
  1775. UserInfo.current_user_id = agent.id
  1776. Ticket::Article.create!(
  1777. ticket_id: ticket1.id,
  1778. from: 'some_sender@example.com',
  1779. to: 'some_recipient@example.com',
  1780. subject: 'update',
  1781. message_id: 'some@id',
  1782. content_type: 'text/html',
  1783. body: 'update',
  1784. internal: false,
  1785. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1786. type: Ticket::Article::Type.find_by(name: 'note'),
  1787. )
  1788. TransactionDispatcher.commit
  1789. UserInfo.current_user_id = nil
  1790. ticket1.reload
  1791. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1792. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1793. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1794. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1795. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1796. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1797. assert_equal([], ticket1.tag_list)
  1798. UserInfo.current_user_id = agent.id
  1799. ticket1.priority = Ticket::Priority.find_by(name: '1 low')
  1800. ticket1.save!
  1801. TransactionDispatcher.commit
  1802. UserInfo.current_user_id = nil
  1803. ticket1.reload
  1804. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1805. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1806. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1807. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1808. assert_equal('1 low', ticket1.priority.name, 'ticket1.priority verify')
  1809. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1810. assert_equal([], ticket1.tag_list)
  1811. UserInfo.current_user_id = agent.id
  1812. ticket1.owner_id = 1
  1813. ticket1.save!
  1814. TransactionDispatcher.commit
  1815. UserInfo.current_user_id = nil
  1816. ticket1.reload
  1817. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1818. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1819. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  1820. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1821. assert_equal('1 low', ticket1.priority.name, 'ticket1.priority verify')
  1822. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1823. assert_equal([], ticket1.tag_list)
  1824. UserInfo.current_user_id = agent.id
  1825. ticket1.owner_id = agent.id
  1826. ticket1.save!
  1827. TransactionDispatcher.commit
  1828. UserInfo.current_user_id = nil
  1829. ticket1.reload
  1830. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1831. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1832. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1833. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1834. assert_equal('1 low', ticket1.priority.name, 'ticket1.priority verify')
  1835. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  1836. assert_equal([], ticket1.tag_list)
  1837. end
  1838. test '9 vip priority set' do
  1839. Trigger.create!(
  1840. name: 'aaa vip priority',
  1841. condition: {
  1842. 'customer.vip' => {
  1843. 'operator' => 'is',
  1844. 'value' => true,
  1845. },
  1846. },
  1847. perform: {
  1848. 'ticket.priority_id' => {
  1849. 'value' => Ticket::Priority.find_by(name: '3 high').id,
  1850. },
  1851. },
  1852. disable_notification: true,
  1853. active: true,
  1854. created_by_id: 1,
  1855. updated_by_id: 1,
  1856. )
  1857. groups = Group.where(name: 'Users')
  1858. roles = Role.where(name: 'Agent')
  1859. agent = User.create!(
  1860. login: 'agent@example.com',
  1861. firstname: 'Trigger',
  1862. lastname: 'Agent1',
  1863. email: 'agent@example.com',
  1864. password: 'agentpw',
  1865. active: true,
  1866. roles: roles,
  1867. groups: groups,
  1868. updated_by_id: 1,
  1869. created_by_id: 1,
  1870. )
  1871. roles = Role.where(name: 'Customer')
  1872. customer = User.create!(
  1873. login: 'customer@example.com',
  1874. firstname: 'Trigger',
  1875. lastname: 'Customer1',
  1876. email: 'customer@example.com',
  1877. password: 'customerpw',
  1878. vip: true,
  1879. active: true,
  1880. roles: roles,
  1881. updated_by_id: 1,
  1882. created_by_id: 1,
  1883. )
  1884. ticket1 = Ticket.create!(
  1885. title: 'test 123',
  1886. owner: agent,
  1887. customer: customer,
  1888. group: Group.lookup(name: 'Users'),
  1889. updated_by_id: 1,
  1890. created_by_id: 1,
  1891. )
  1892. Ticket::Article.create!(
  1893. ticket_id: ticket1.id,
  1894. from: 'some_sender@example.com',
  1895. to: 'some_recipient@example.com',
  1896. subject: 'some subject',
  1897. message_id: 'some@id',
  1898. body: "some message <b>note</b>\nnew line",
  1899. internal: false,
  1900. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1901. type: Ticket::Article::Type.find_by(name: 'note'),
  1902. updated_by_id: 1,
  1903. created_by_id: 1,
  1904. )
  1905. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1906. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1907. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1908. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  1909. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1910. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  1911. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1912. assert_equal([], ticket1.tag_list)
  1913. TransactionDispatcher.commit
  1914. ticket1.reload
  1915. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  1916. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  1917. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  1918. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  1919. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  1920. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  1921. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  1922. assert_equal([], ticket1.tag_list)
  1923. customer.vip = false
  1924. customer.save!
  1925. ticket2 = Ticket.create!(
  1926. title: 'test 123',
  1927. owner: agent,
  1928. customer: customer,
  1929. group: Group.lookup(name: 'Users'),
  1930. updated_by_id: 1,
  1931. created_by_id: 1,
  1932. )
  1933. Ticket::Article.create!(
  1934. ticket_id: ticket2.id,
  1935. from: 'some_sender@example.com',
  1936. to: 'some_recipient@example.com',
  1937. subject: 'some subject',
  1938. message_id: 'some@id',
  1939. body: "some message <b>note</b>\nnew line",
  1940. internal: false,
  1941. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  1942. type: Ticket::Article::Type.find_by(name: 'note'),
  1943. updated_by_id: 1,
  1944. created_by_id: 1,
  1945. )
  1946. assert_equal('test 123', ticket2.title, 'ticket2.title verify')
  1947. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  1948. assert_equal(agent.id, ticket2.owner_id, 'ticket2.owner_id verify')
  1949. assert_equal(customer.id, ticket2.customer_id, 'ticket2.customer_id verify')
  1950. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  1951. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  1952. assert_equal(1, ticket2.articles.count, 'ticket2.articles verify')
  1953. assert_equal([], ticket2.tag_list)
  1954. TransactionDispatcher.commit
  1955. ticket2.reload
  1956. assert_equal('test 123', ticket2.title, 'ticket2.title verify')
  1957. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  1958. assert_equal(agent.id, ticket2.owner_id, 'ticket2.owner_id verify')
  1959. assert_equal(customer.id, ticket2.customer_id, 'ticket2.customer_id verify')
  1960. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  1961. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  1962. assert_equal(1, ticket2.articles.count, 'ticket2.articles verify')
  1963. assert_equal([], ticket2.tag_list)
  1964. end
  1965. test '10 owner auto assignment notify to customer' do
  1966. Trigger.create!(
  1967. name: 'aaa auto assignment',
  1968. condition: {
  1969. 'ticket.owner_id' => {
  1970. 'operator' => 'has changed',
  1971. 'pre_condition' => '',
  1972. 'value' => '2',
  1973. 'value_completion' => '',
  1974. },
  1975. },
  1976. perform: {
  1977. 'notification.email' => {
  1978. 'body' => 'some lala',
  1979. 'recipient' => 'ticket_customer',
  1980. 'subject' => 'NEW OWNER (#{ticket.title})!',
  1981. },
  1982. },
  1983. disable_notification: true,
  1984. active: true,
  1985. created_by_id: 1,
  1986. updated_by_id: 1,
  1987. )
  1988. groups = Group.where(name: 'Users')
  1989. roles = Role.where(name: 'Agent')
  1990. agent1 = User.create!(
  1991. login: 'agent1@example.com',
  1992. firstname: 'Trigger',
  1993. lastname: 'Agent1',
  1994. email: 'agent1@example.com',
  1995. password: 'agentpw',
  1996. active: true,
  1997. roles: roles,
  1998. groups: groups,
  1999. updated_by_id: 1,
  2000. created_by_id: 1,
  2001. )
  2002. agent2 = User.create!(
  2003. login: 'agent2@example.com',
  2004. firstname: 'Trigger',
  2005. lastname: 'Agent2',
  2006. email: 'agent2@example.com',
  2007. password: 'agentpw',
  2008. active: true,
  2009. roles: roles,
  2010. groups: groups,
  2011. updated_by_id: 1,
  2012. created_by_id: 1,
  2013. )
  2014. ticket1 = Ticket.create!(
  2015. title: 'test 123',
  2016. group: Group.lookup(name: 'Users'),
  2017. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2018. updated_by_id: 1,
  2019. created_by_id: 1,
  2020. )
  2021. Ticket::Article.create!(
  2022. ticket_id: ticket1.id,
  2023. from: 'some_sender@example.com',
  2024. to: 'some_recipient@example.com',
  2025. subject: 'some subject',
  2026. message_id: 'some@id',
  2027. body: "some message <b>note</b>\nnew line",
  2028. internal: false,
  2029. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2030. type: Ticket::Article::Type.find_by(name: 'note'),
  2031. updated_by_id: 1,
  2032. created_by_id: 1,
  2033. )
  2034. TransactionDispatcher.commit
  2035. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2036. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2037. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  2038. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2039. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2040. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  2041. assert_equal([], ticket1.tag_list)
  2042. UserInfo.current_user_id = agent1.id
  2043. ticket1.owner_id = agent1.id
  2044. ticket1.save!
  2045. TransactionDispatcher.commit
  2046. UserInfo.current_user_id = nil
  2047. ticket1.reload
  2048. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2049. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2050. assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2051. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2052. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2053. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2054. assert_equal([], ticket1.tag_list)
  2055. UserInfo.current_user_id = agent1.id
  2056. ticket1.owner_id = agent1.id
  2057. ticket1.save!
  2058. TransactionDispatcher.commit
  2059. UserInfo.current_user_id = nil
  2060. ticket1.reload
  2061. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2062. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2063. assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2064. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2065. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2066. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2067. assert_equal([], ticket1.tag_list)
  2068. UserInfo.current_user_id = agent1.id
  2069. ticket1.owner_id = agent2.id
  2070. ticket1.save!
  2071. TransactionDispatcher.commit
  2072. UserInfo.current_user_id = nil
  2073. ticket1.reload
  2074. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2075. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2076. assert_equal(agent2.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2077. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2078. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2079. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  2080. assert_equal([], ticket1.tag_list)
  2081. end
  2082. test '11 notify to customer on public note' do
  2083. Trigger.create!(
  2084. name: 'aaa notify to customer on public note',
  2085. condition: {
  2086. 'article.internal' => {
  2087. 'operator' => 'is',
  2088. 'value' => 'false',
  2089. },
  2090. 'article.sender_id' => {
  2091. 'operator' => 'is',
  2092. 'value' => Ticket::Article::Sender.lookup(name: 'Agent').id,
  2093. },
  2094. 'article.type_id' => {
  2095. 'operator' => 'is',
  2096. 'value' => [
  2097. Ticket::Article::Type.lookup(name: 'note').id,
  2098. ],
  2099. },
  2100. },
  2101. perform: {
  2102. 'notification.email' => {
  2103. 'body' => 'some lala',
  2104. 'recipient' => 'ticket_customer',
  2105. 'subject' => 'UPDATE (#{ticket.title})!',
  2106. },
  2107. },
  2108. disable_notification: true,
  2109. active: true,
  2110. created_by_id: 1,
  2111. updated_by_id: 1,
  2112. )
  2113. groups = Group.where(name: 'Users')
  2114. roles = Role.where(name: 'Agent')
  2115. agent = User.create!(
  2116. login: 'agent@example.com',
  2117. firstname: 'Trigger',
  2118. lastname: 'Agent1',
  2119. email: 'agent@example.com',
  2120. password: 'agentpw',
  2121. active: true,
  2122. roles: roles,
  2123. groups: groups,
  2124. updated_by_id: 1,
  2125. created_by_id: 1,
  2126. )
  2127. roles = Role.where(name: 'Customer')
  2128. customer = User.create!(
  2129. login: 'customer@example.com',
  2130. firstname: 'Trigger',
  2131. lastname: 'Customer1',
  2132. email: 'customer@example.com',
  2133. password: 'customerpw',
  2134. vip: true,
  2135. active: true,
  2136. roles: roles,
  2137. updated_by_id: 1,
  2138. created_by_id: 1,
  2139. )
  2140. ticket1 = Ticket.create!(
  2141. title: 'test 123',
  2142. owner: agent,
  2143. customer: customer,
  2144. group: Group.lookup(name: 'Users'),
  2145. updated_by_id: 1,
  2146. created_by_id: 1,
  2147. )
  2148. Ticket::Article.create!(
  2149. ticket_id: ticket1.id,
  2150. from: 'some_sender@example.com',
  2151. to: 'some_recipient@example.com',
  2152. subject: 'some subject',
  2153. message_id: 'some@id',
  2154. body: "some message <b>note</b>\nnew line",
  2155. internal: false,
  2156. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2157. type: Ticket::Article::Type.find_by(name: 'note'),
  2158. updated_by_id: 1,
  2159. created_by_id: 1,
  2160. )
  2161. TransactionDispatcher.commit
  2162. ticket1.reload
  2163. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2164. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2165. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2166. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2167. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2168. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2169. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2170. assert_equal([], ticket1.tag_list)
  2171. Ticket::Article.create!(
  2172. ticket_id: ticket1.id,
  2173. from: 'some_sender@example.com',
  2174. to: 'some_recipient@example.com',
  2175. subject: 'some subject',
  2176. message_id: 'some@id',
  2177. body: "some message <b>note</b>\nnew line",
  2178. internal: true,
  2179. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2180. type: Ticket::Article::Type.find_by(name: 'note'),
  2181. updated_by_id: 1,
  2182. created_by_id: 1,
  2183. )
  2184. TransactionDispatcher.commit
  2185. ticket1.reload
  2186. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2187. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2188. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2189. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2190. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2191. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2192. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  2193. assert_equal([], ticket1.tag_list)
  2194. Ticket::Article.create!(
  2195. ticket_id: ticket1.id,
  2196. from: 'some_sender@example.com',
  2197. to: 'some_recipient@example.com',
  2198. subject: 'some subject',
  2199. message_id: 'some@id',
  2200. body: "some message <b>note</b>\nnew line",
  2201. internal: false,
  2202. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2203. type: Ticket::Article::Type.find_by(name: 'note'),
  2204. updated_by_id: 1,
  2205. created_by_id: 1,
  2206. )
  2207. TransactionDispatcher.commit
  2208. ticket1.reload
  2209. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2210. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2211. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2212. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2213. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2214. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2215. assert_equal(5, ticket1.articles.count, 'ticket1.articles verify')
  2216. assert_equal([], ticket1.tag_list)
  2217. ticket1.priority = Ticket::Priority.find_by(name: '3 high')
  2218. ticket1.save!
  2219. article = Ticket::Article.create!(
  2220. ticket_id: ticket1.id,
  2221. from: 'some_sender@example.com',
  2222. to: 'some_recipient@example.com',
  2223. subject: 'some subject',
  2224. message_id: 'some@id',
  2225. body: "some message <b>note</b>\nnew line",
  2226. internal: true,
  2227. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2228. type: Ticket::Article::Type.find_by(name: 'note'),
  2229. updated_by_id: 1,
  2230. created_by_id: 1,
  2231. )
  2232. TransactionDispatcher.commit
  2233. ticket1.reload
  2234. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2235. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2236. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2237. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2238. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2239. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  2240. assert_equal(6, ticket1.articles.count, 'ticket1.articles verify')
  2241. assert_equal([], ticket1.tag_list)
  2242. article.internal = false
  2243. article.save!
  2244. TransactionDispatcher.commit
  2245. ticket1.reload
  2246. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2247. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2248. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2249. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2250. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2251. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  2252. assert_equal(6, ticket1.articles.count, 'ticket1.articles verify')
  2253. assert_equal([], ticket1.tag_list)
  2254. Ticket::Article.create!(
  2255. ticket_id: ticket1.id,
  2256. from: 'some_sender@example.com',
  2257. to: 'some_recipient@example.com',
  2258. subject: 'some subject',
  2259. message_id: 'some@id',
  2260. body: "some message <b>note</b>\nnew line",
  2261. internal: true,
  2262. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2263. type: Ticket::Article::Type.find_by(name: 'note'),
  2264. updated_by_id: 1,
  2265. created_by_id: 1,
  2266. )
  2267. TransactionDispatcher.commit
  2268. ticket1.reload
  2269. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2270. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2271. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2272. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2273. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2274. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  2275. assert_equal(7, ticket1.articles.count, 'ticket1.articles verify')
  2276. assert_equal([], ticket1.tag_list)
  2277. end
  2278. test '12 notify on owner change' do
  2279. Trigger.create!(
  2280. name: 'aaa notify to customer on public note',
  2281. condition: {
  2282. 'ticket.owner_id' => {
  2283. 'operator' => 'has changed',
  2284. 'pre_condition' => 'current_user.id',
  2285. 'value' => '',
  2286. 'value_completion' => '',
  2287. }
  2288. },
  2289. perform: {
  2290. 'notification.email' => {
  2291. 'body' => 'some lala',
  2292. 'recipient' => 'ticket_customer',
  2293. 'subject' => 'UPDATE (#{ticket.title})!',
  2294. },
  2295. },
  2296. disable_notification: true,
  2297. active: true,
  2298. created_by_id: 1,
  2299. updated_by_id: 1,
  2300. )
  2301. Trigger.create!(
  2302. name: 'auto reply (on new tickets)',
  2303. condition: {
  2304. 'ticket.action' => {
  2305. 'operator' => 'is',
  2306. 'value' => 'create',
  2307. },
  2308. 'ticket.state_id' => {
  2309. 'operator' => 'is not',
  2310. 'value' => Ticket::State.lookup(name: 'closed').id,
  2311. },
  2312. 'article.type_id' => {
  2313. 'operator' => 'is',
  2314. 'value' => [
  2315. Ticket::Article::Type.lookup(name: 'email').id,
  2316. Ticket::Article::Type.lookup(name: 'phone').id,
  2317. Ticket::Article::Type.lookup(name: 'web').id,
  2318. ],
  2319. },
  2320. 'article.sender_id' => {
  2321. 'operator' => 'is',
  2322. 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id,
  2323. },
  2324. },
  2325. perform: {
  2326. 'notification.email' => {
  2327. 'body' => '<div>Your request <b>(#{config.ticket_hook}#{ticket.number})</b> has been received and will be reviewed by our support staff.</div>
  2328. <br/>
  2329. <div>To provide additional information, please reply to this email or click on the following link (for initial login, please request a new password):
  2330. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  2331. </div>
  2332. <br/>
  2333. <div>Your #{config.product_name} Team</div>
  2334. <br/>
  2335. <div><i><a href="https://zammad.com">Zammad</a>, your customer support system</i></div>',
  2336. 'recipient' => 'ticket_customer',
  2337. 'subject' => 'Thanks for your inquiry (#{ticket.title})',
  2338. },
  2339. },
  2340. active: true,
  2341. created_by_id: 1,
  2342. updated_by_id: 1,
  2343. )
  2344. Trigger.create!(
  2345. name: 'auto reply (on follow-up of tickets)',
  2346. condition: {
  2347. 'ticket.action' => {
  2348. 'operator' => 'is',
  2349. 'value' => 'update',
  2350. },
  2351. 'article.sender_id' => {
  2352. 'operator' => 'is',
  2353. 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id,
  2354. },
  2355. 'article.type_id' => {
  2356. 'operator' => 'is',
  2357. 'value' => [
  2358. Ticket::Article::Type.lookup(name: 'email').id,
  2359. Ticket::Article::Type.lookup(name: 'phone').id,
  2360. Ticket::Article::Type.lookup(name: 'web').id,
  2361. ],
  2362. },
  2363. },
  2364. perform: {
  2365. 'notification.email' => {
  2366. 'body' => '<div>Your follow-up for <b>(#{config.ticket_hook}#{ticket.number})</b> has been received and will be reviewed by our support staff.</div>
  2367. <br/>
  2368. <div>To provide additional information, please reply to this email or click on the following link:
  2369. <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}">#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}</a>
  2370. </div>
  2371. <br/>
  2372. <div>Your #{config.product_name} Team</div>
  2373. <br/>
  2374. <div><i><a href="https://zammad.com">Zammad</a>, your customer support system</i></div>',
  2375. 'recipient' => 'ticket_customer',
  2376. 'subject' => 'Thanks for your follow-up (#{ticket.title})',
  2377. },
  2378. },
  2379. active: true,
  2380. created_by_id: 1,
  2381. updated_by_id: 1,
  2382. )
  2383. groups = Group.where(name: 'Users')
  2384. roles = Role.where(name: 'Agent')
  2385. agent = User.create!(
  2386. login: 'agent@example.com',
  2387. firstname: 'Trigger',
  2388. lastname: 'Agent1',
  2389. email: 'agent@example.com',
  2390. password: 'agentpw',
  2391. active: true,
  2392. roles: roles,
  2393. groups: groups,
  2394. updated_by_id: 1,
  2395. created_by_id: 1,
  2396. )
  2397. roles = Role.where(name: 'Customer')
  2398. customer = User.create!(
  2399. login: 'customer@example.com',
  2400. firstname: 'Trigger',
  2401. lastname: 'Customer1',
  2402. email: 'customer@example.com',
  2403. password: 'customerpw',
  2404. vip: true,
  2405. active: true,
  2406. roles: roles,
  2407. updated_by_id: 1,
  2408. created_by_id: 1,
  2409. )
  2410. ticket1 = Ticket.create!(
  2411. title: 'test 123',
  2412. #owner: agent,
  2413. customer: customer,
  2414. group: Group.lookup(name: 'Users'),
  2415. updated_by_id: 1,
  2416. created_by_id: 1,
  2417. )
  2418. Ticket::Article.create!(
  2419. ticket_id: ticket1.id,
  2420. from: 'some_sender@example.com',
  2421. to: 'some_recipient@example.com',
  2422. subject: 'some subject',
  2423. message_id: 'some@id',
  2424. body: "some message <b>note</b>\nnew line",
  2425. internal: false,
  2426. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2427. type: Ticket::Article::Type.find_by(name: 'web'),
  2428. updated_by_id: 1,
  2429. created_by_id: 1,
  2430. )
  2431. TransactionDispatcher.commit
  2432. ticket1.reload
  2433. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2434. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2435. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  2436. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2437. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2438. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2439. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2440. assert_equal([], ticket1.tag_list)
  2441. UserInfo.current_user_id = agent.id
  2442. ticket1.owner_id = agent.id
  2443. ticket1.save!
  2444. TransactionDispatcher.commit
  2445. UserInfo.current_user_id = nil
  2446. ticket1.reload
  2447. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2448. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2449. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2450. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2451. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2452. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2453. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  2454. assert_equal([], ticket1.tag_list)
  2455. Ticket::Article.create!(
  2456. ticket_id: ticket1.id,
  2457. from: 'some_sender@example.com',
  2458. to: 'some_recipient@example.com',
  2459. subject: 'some subject',
  2460. message_id: 'some@id',
  2461. body: "some message <b>note</b>\nnew line",
  2462. internal: false,
  2463. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2464. type: Ticket::Article::Type.find_by(name: 'web'),
  2465. updated_by_id: 1,
  2466. created_by_id: 1,
  2467. )
  2468. TransactionDispatcher.commit
  2469. ticket1.reload
  2470. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2471. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2472. assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify')
  2473. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2474. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2475. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2476. assert_equal(5, ticket1.articles.count, 'ticket1.articles verify')
  2477. assert_equal([], ticket1.tag_list)
  2478. UserInfo.current_user_id = agent.id
  2479. ticket1.owner_id = 1
  2480. ticket1.save!
  2481. TransactionDispatcher.commit
  2482. UserInfo.current_user_id = nil
  2483. ticket1.reload
  2484. assert_equal('test 123', ticket1.title, 'ticket1.title verify')
  2485. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  2486. assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify')
  2487. assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify')
  2488. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  2489. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  2490. assert_equal(6, ticket1.articles.count, 'ticket1.articles verify')
  2491. assert_equal([], ticket1.tag_list)
  2492. end
  2493. test '1 empty condition should not create errors' do
  2494. assert_raises(Exception) do
  2495. Trigger.create!(
  2496. name: 'aaa loop check',
  2497. condition: {
  2498. 'ticket.number' => {
  2499. 'operator' => 'contains',
  2500. 'value' => '',
  2501. },
  2502. },
  2503. perform: {
  2504. 'notification.email' => {
  2505. 'body' => 'some lala',
  2506. 'recipient' => 'ticket_customer',
  2507. 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!',
  2508. },
  2509. },
  2510. disable_notification: true,
  2511. active: true,
  2512. created_by_id: 1,
  2513. updated_by_id: 1,
  2514. )
  2515. end
  2516. end
  2517. test 'article_last_sender trigger -> reply_to' do
  2518. Trigger.create!(
  2519. name: 'auto reply',
  2520. condition: {
  2521. 'ticket.action' => {
  2522. 'operator' => 'is',
  2523. 'value' => 'create',
  2524. },
  2525. 'ticket.state_id' => {
  2526. 'operator' => 'is',
  2527. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  2528. }
  2529. },
  2530. perform: {
  2531. 'notification.email' => {
  2532. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  2533. 'recipient' => 'article_last_sender',
  2534. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  2535. },
  2536. },
  2537. disable_notification: true,
  2538. active: true,
  2539. created_by_id: 1,
  2540. updated_by_id: 1,
  2541. )
  2542. ticket1 = Ticket.create!(
  2543. title: "some <b>title</b>\n äöüß",
  2544. group: Group.lookup(name: 'Users'),
  2545. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2546. updated_by_id: 1,
  2547. created_by_id: 1,
  2548. )
  2549. Ticket::Article.create!(
  2550. ticket_id: ticket1.id,
  2551. from: 'some_sender@example.com',
  2552. to: 'some_recipient+from@example.com',
  2553. reply_to: 'some_recipient+reply_to@example.com',
  2554. subject: 'some subject',
  2555. message_id: 'some@id',
  2556. body: "some message <b>note</b>\nnew line",
  2557. internal: false,
  2558. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2559. type: Ticket::Article::Type.find_by(name: 'note'),
  2560. updated_by_id: 1,
  2561. created_by_id: 1,
  2562. )
  2563. TransactionDispatcher.commit
  2564. ticket1.reload
  2565. assert_equal('new', ticket1.state.name, 'ticket1.state new')
  2566. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2567. auto_response = ticket1.articles.last
  2568. assert_match('Zammad <zammad@localhost>', auto_response.from)
  2569. assert_match('some_recipient+reply_to@example.com', auto_response.to)
  2570. end
  2571. test 'article_last_sender trigger -> from' do
  2572. Trigger.create!(
  2573. name: 'auto reply',
  2574. condition: {
  2575. 'ticket.action' => {
  2576. 'operator' => 'is',
  2577. 'value' => 'create',
  2578. },
  2579. 'ticket.state_id' => {
  2580. 'operator' => 'is',
  2581. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  2582. }
  2583. },
  2584. perform: {
  2585. 'notification.email' => {
  2586. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  2587. 'recipient' => 'article_last_sender',
  2588. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  2589. },
  2590. },
  2591. disable_notification: true,
  2592. active: true,
  2593. created_by_id: 1,
  2594. updated_by_id: 1,
  2595. )
  2596. ticket1 = Ticket.create!(
  2597. title: "some <b>title</b>\n äöüß",
  2598. group: Group.lookup(name: 'Users'),
  2599. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2600. updated_by_id: 1,
  2601. created_by_id: 1,
  2602. )
  2603. Ticket::Article.create!(
  2604. ticket_id: ticket1.id,
  2605. from: 'some_sender+from@example.com',
  2606. to: 'some_recipient@example.com',
  2607. subject: 'some subject',
  2608. message_id: 'some@id',
  2609. body: "some message <b>note</b>\nnew line",
  2610. internal: false,
  2611. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2612. type: Ticket::Article::Type.find_by(name: 'email'),
  2613. updated_by_id: 1,
  2614. created_by_id: 1,
  2615. )
  2616. TransactionDispatcher.commit
  2617. ticket1.reload
  2618. assert_equal('new', ticket1.state.name, 'ticket1.state new')
  2619. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2620. auto_response = ticket1.articles.last
  2621. assert_match('Zammad <zammad@localhost>', auto_response.from)
  2622. assert_match('some_sender+from@example.com', auto_response.to)
  2623. end
  2624. test 'article_last_sender trigger -> origin_by_id' do
  2625. Trigger.create!(
  2626. name: 'auto reply',
  2627. condition: {
  2628. 'ticket.action' => {
  2629. 'operator' => 'is',
  2630. 'value' => 'create',
  2631. },
  2632. 'ticket.state_id' => {
  2633. 'operator' => 'is',
  2634. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  2635. }
  2636. },
  2637. perform: {
  2638. 'notification.email' => {
  2639. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  2640. 'recipient' => 'article_last_sender',
  2641. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  2642. },
  2643. },
  2644. disable_notification: true,
  2645. active: true,
  2646. created_by_id: 1,
  2647. updated_by_id: 1,
  2648. )
  2649. roles = Role.where(name: 'Customer')
  2650. customer1 = User.create!(
  2651. login: 'customer+origin_by_id@example.com',
  2652. firstname: 'Trigger',
  2653. lastname: 'Customer1',
  2654. email: 'customer+origin_by_id@example.com',
  2655. password: 'customerpw',
  2656. active: true,
  2657. roles: roles,
  2658. updated_at: '2015-02-05 16:37:00',
  2659. updated_by_id: 1,
  2660. created_by_id: 1,
  2661. )
  2662. ticket1 = Ticket.create!(
  2663. title: "some <b>title</b>\n äöüß",
  2664. group: Group.lookup(name: 'Users'),
  2665. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2666. updated_by_id: 1,
  2667. created_by_id: 1,
  2668. )
  2669. Ticket::Article.create!(
  2670. ticket_id: ticket1.id,
  2671. to: 'some_recipient@example.com',
  2672. subject: 'some subject',
  2673. message_id: 'some@id',
  2674. body: "some message <b>note</b>\nnew line",
  2675. internal: false,
  2676. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2677. type: Ticket::Article::Type.find_by(name: 'email'),
  2678. origin_by_id: customer1.id,
  2679. updated_by_id: 1,
  2680. created_by_id: 1,
  2681. )
  2682. TransactionDispatcher.commit
  2683. ticket1.reload
  2684. assert_equal('new', ticket1.state.name, 'ticket1.state new')
  2685. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2686. auto_response = ticket1.articles.last
  2687. assert_match('Zammad <zammad@localhost>', auto_response.from)
  2688. assert_match('customer+origin_by_id@example.com', auto_response.to)
  2689. end
  2690. test 'article_last_sender trigger -> created_by_id' do
  2691. Trigger.create!(
  2692. name: 'auto reply',
  2693. condition: {
  2694. 'ticket.action' => {
  2695. 'operator' => 'is',
  2696. 'value' => 'create',
  2697. },
  2698. 'ticket.state_id' => {
  2699. 'operator' => 'is',
  2700. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  2701. }
  2702. },
  2703. perform: {
  2704. 'notification.email' => {
  2705. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  2706. 'recipient' => 'article_last_sender',
  2707. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  2708. },
  2709. },
  2710. disable_notification: true,
  2711. active: true,
  2712. created_by_id: 1,
  2713. updated_by_id: 1,
  2714. )
  2715. roles = Role.where(name: 'Customer')
  2716. customer1 = User.create!(
  2717. login: 'customer+created_by_id@example.com',
  2718. firstname: 'Trigger',
  2719. lastname: 'Customer1',
  2720. email: 'customer+created_by_id@example.com',
  2721. password: 'customerpw',
  2722. active: true,
  2723. roles: roles,
  2724. updated_at: '2015-02-05 16:37:00',
  2725. updated_by_id: 1,
  2726. created_by_id: 1,
  2727. )
  2728. ticket1 = Ticket.create!(
  2729. title: "some <b>title</b>\n äöüß",
  2730. group: Group.lookup(name: 'Users'),
  2731. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2732. updated_by_id: 1,
  2733. created_by_id: 1,
  2734. )
  2735. Ticket::Article.create!(
  2736. ticket_id: ticket1.id,
  2737. to: 'some_recipient@example.com',
  2738. subject: 'some subject',
  2739. message_id: 'some@id',
  2740. body: "some message <b>note</b>\nnew line",
  2741. internal: false,
  2742. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2743. type: Ticket::Article::Type.find_by(name: 'email'),
  2744. updated_by_id: customer1.id,
  2745. created_by_id: customer1.id,
  2746. )
  2747. TransactionDispatcher.commit
  2748. ticket1.reload
  2749. assert_equal('new', ticket1.state.name, 'ticket1.state new')
  2750. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2751. auto_response = ticket1.articles.last
  2752. assert_match('Zammad <zammad@localhost>', auto_response.from)
  2753. assert_match('customer+created_by_id@example.com', auto_response.to)
  2754. end
  2755. test 'multiple recipients owner_id, article_last_sender(reply_to) trigger' do
  2756. Trigger.create!(
  2757. name: 'auto reply',
  2758. condition: {
  2759. 'ticket.action' => {
  2760. 'operator' => 'is',
  2761. 'value' => 'create',
  2762. },
  2763. 'ticket.state_id' => {
  2764. 'operator' => 'is',
  2765. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  2766. }
  2767. },
  2768. perform: {
  2769. 'notification.email' => {
  2770. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  2771. 'recipient' => %w[ticket_owner article_last_sender],
  2772. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  2773. },
  2774. },
  2775. disable_notification: true,
  2776. active: true,
  2777. created_by_id: 1,
  2778. updated_by_id: 1,
  2779. )
  2780. admin = User.create!(
  2781. login: 'admin+owner_recipient@example.com',
  2782. firstname: 'Role',
  2783. lastname: "Admin#{name}",
  2784. email: 'admin+owner_recipient@example.com',
  2785. password: 'adminpw',
  2786. active: true,
  2787. roles: Role.where(name: %w[Admin Agent]),
  2788. groups: Group.where(name: 'Users'),
  2789. updated_by_id: 1,
  2790. created_by_id: 1,
  2791. )
  2792. ticket1 = Ticket.create!(
  2793. title: "some <b>title</b>\n äöüß",
  2794. group: Group.lookup(name: 'Users'),
  2795. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2796. owner_id: admin.id,
  2797. updated_by_id: 1,
  2798. created_by_id: 1,
  2799. )
  2800. Ticket::Article.create!(
  2801. ticket_id: ticket1.id,
  2802. from: 'some_sender@example.com',
  2803. to: 'some_recipient+from@example.com',
  2804. reply_to: 'some_recipient+reply_to@example.com',
  2805. subject: 'some subject',
  2806. message_id: 'some@id',
  2807. body: "some message <b>note</b>\nnew line",
  2808. internal: false,
  2809. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2810. type: Ticket::Article::Type.find_by(name: 'note'),
  2811. updated_by_id: 1,
  2812. created_by_id: 1,
  2813. )
  2814. TransactionDispatcher.commit
  2815. ticket1.reload
  2816. assert_equal('new', ticket1.state.name, 'ticket1.state new')
  2817. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  2818. auto_response = ticket1.articles.last
  2819. assert_match('Zammad <zammad@localhost>', auto_response.from)
  2820. assert_match('some_recipient+reply_to@example.com', auto_response.to)
  2821. assert_match('admin+owner_recipient@example.com', auto_response.to)
  2822. end
  2823. test 'article_last_sender trigger -> invalid reply_to' do
  2824. Trigger.create!(
  2825. name: 'auto reply',
  2826. condition: {
  2827. 'ticket.action' => {
  2828. 'operator' => 'is',
  2829. 'value' => 'create',
  2830. },
  2831. 'ticket.state_id' => {
  2832. 'operator' => 'is',
  2833. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  2834. }
  2835. },
  2836. perform: {
  2837. 'notification.email' => {
  2838. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  2839. 'recipient' => 'article_last_sender',
  2840. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  2841. },
  2842. },
  2843. disable_notification: true,
  2844. active: true,
  2845. created_by_id: 1,
  2846. updated_by_id: 1,
  2847. )
  2848. ticket1 = Ticket.create!(
  2849. title: "some <b>title</b>\n äöüß",
  2850. group: Group.lookup(name: 'Users'),
  2851. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2852. updated_by_id: 1,
  2853. created_by_id: 1,
  2854. )
  2855. Ticket::Article.create!(
  2856. ticket_id: ticket1.id,
  2857. from: 'some_sender@example.com',
  2858. to: 'some_recipient+from@example.com',
  2859. reply_to: 'Blub blub blub some_recipient+reply_to@example',
  2860. subject: 'some subject',
  2861. message_id: 'some@id',
  2862. body: "some message <b>note</b>\nnew line",
  2863. internal: false,
  2864. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  2865. type: Ticket::Article::Type.find_by(name: 'note'),
  2866. updated_by_id: 1,
  2867. created_by_id: 1,
  2868. )
  2869. TransactionDispatcher.commit
  2870. ticket1.reload
  2871. assert_equal('new', ticket1.state.name, 'ticket1.state new')
  2872. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  2873. end
  2874. test '2 loop check' do
  2875. Trigger.create!(
  2876. name: 'aaa loop check',
  2877. condition: {
  2878. 'ticket.state_id' => {
  2879. 'operator' => 'is',
  2880. 'value' => Ticket::State.all.pluck(:id),
  2881. },
  2882. 'article.sender_id' => {
  2883. 'operator' => 'is',
  2884. 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id,
  2885. },
  2886. 'article.type_id' => {
  2887. 'operator' => 'is',
  2888. 'value' => [
  2889. Ticket::Article::Type.lookup(name: 'email').id,
  2890. Ticket::Article::Type.lookup(name: 'phone').id,
  2891. Ticket::Article::Type.lookup(name: 'web').id,
  2892. ],
  2893. },
  2894. },
  2895. perform: {
  2896. 'notification.email' => {
  2897. 'body' => 'some lala',
  2898. 'recipient' => 'ticket_customer',
  2899. 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!',
  2900. },
  2901. },
  2902. disable_notification: true,
  2903. active: true,
  2904. created_by_id: 1,
  2905. updated_by_id: 1,
  2906. )
  2907. ticket1 = Ticket.create!(
  2908. title: 'loop try 1',
  2909. group: Group.lookup(name: 'Users'),
  2910. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  2911. updated_by_id: 1,
  2912. created_by_id: 1,
  2913. )
  2914. Ticket::Article.create!(
  2915. ticket_id: ticket1.id,
  2916. from: 'some_loop_sender@example.com',
  2917. to: 'some_loop_recipient@example.com',
  2918. subject: 'some subject',
  2919. message_id: 'some@id',
  2920. body: "some message <b>note</b>\nnew line",
  2921. internal: false,
  2922. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2923. type: Ticket::Article::Type.find_by(name: 'email'),
  2924. updated_by_id: 1,
  2925. created_by_id: 1,
  2926. )
  2927. ticket1.reload
  2928. assert_equal(1, ticket1.articles.count)
  2929. TransactionDispatcher.commit
  2930. ticket1.reload
  2931. assert_equal(2, ticket1.articles.count)
  2932. ticket1.priority = Ticket::Priority.lookup(name: '2 normal')
  2933. ticket1.save!
  2934. TransactionDispatcher.commit
  2935. ticket1.reload
  2936. assert_equal(2, ticket1.articles.count)
  2937. Ticket::Article.create!(
  2938. ticket_id: ticket1.id,
  2939. from: 'some_loop_sender@example.com',
  2940. to: 'some_loop_recipient@example.com',
  2941. subject: 'some subject 1234',
  2942. message_id: 'some@id',
  2943. content_type: 'text/html',
  2944. body: 'some message <b>note</b><br>new line',
  2945. internal: false,
  2946. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2947. type: Ticket::Article::Type.find_by(name: 'email'),
  2948. updated_by_id: 1,
  2949. created_by_id: 1,
  2950. )
  2951. TransactionDispatcher.commit
  2952. ticket1.reload
  2953. assert_equal(4, ticket1.articles.count)
  2954. assert_equal('some_loop_sender@example.com', ticket1.articles[2].from)
  2955. assert_equal('nicole.braun@zammad.org', ticket1.articles[3].to)
  2956. Ticket::Article.create!(
  2957. ticket_id: ticket1.id,
  2958. from: 'some_loop_sender@example.com',
  2959. to: 'some_loop_recipient@example.com',
  2960. subject: 'some subject 1234',
  2961. message_id: 'some@id',
  2962. content_type: 'text/html',
  2963. body: 'some message <b>note</b><br>new line',
  2964. internal: false,
  2965. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2966. type: Ticket::Article::Type.find_by(name: 'email'),
  2967. updated_by_id: 1,
  2968. created_by_id: 1,
  2969. )
  2970. TransactionDispatcher.commit
  2971. ticket1.reload
  2972. assert_equal(6, ticket1.articles.count)
  2973. assert_equal('some_loop_sender@example.com', ticket1.articles[4].from)
  2974. assert_equal('nicole.braun@zammad.org', ticket1.articles[5].to)
  2975. Ticket::Article.create!(
  2976. ticket_id: ticket1.id,
  2977. from: 'some_loop_sender@example.com',
  2978. to: 'some_loop_recipient@example.com',
  2979. subject: 'some subject 1234',
  2980. message_id: 'some@id',
  2981. content_type: 'text/html',
  2982. body: 'some message <b>note</b><br>new line',
  2983. internal: false,
  2984. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  2985. type: Ticket::Article::Type.find_by(name: 'email'),
  2986. updated_by_id: 1,
  2987. created_by_id: 1,
  2988. )
  2989. TransactionDispatcher.commit
  2990. ticket1.reload
  2991. assert_equal(8, ticket1.articles.count)
  2992. assert_equal('some_loop_sender@example.com', ticket1.articles[6].from)
  2993. assert_equal('nicole.braun@zammad.org', ticket1.articles[7].to)
  2994. Ticket::Article.create!(
  2995. ticket_id: ticket1.id,
  2996. from: 'some_loop_sender@example.com',
  2997. to: 'some_loop_recipient@example.com',
  2998. subject: 'some subject 1234',
  2999. message_id: 'some@id',
  3000. content_type: 'text/html',
  3001. body: 'some message <b>note</b><br>new line',
  3002. internal: false,
  3003. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3004. type: Ticket::Article::Type.find_by(name: 'email'),
  3005. updated_by_id: 1,
  3006. created_by_id: 1,
  3007. )
  3008. TransactionDispatcher.commit
  3009. ticket1.reload
  3010. assert_equal(10, ticket1.articles.count)
  3011. assert_equal('some_loop_sender@example.com', ticket1.articles[8].from)
  3012. assert_equal('nicole.braun@zammad.org', ticket1.articles[9].to)
  3013. Ticket::Article.create!(
  3014. ticket_id: ticket1.id,
  3015. from: 'some_loop_sender@example.com',
  3016. to: 'some_loop_recipient@example.com',
  3017. subject: 'some subject 1234',
  3018. message_id: 'some@id',
  3019. content_type: 'text/html',
  3020. body: 'some message <b>note</b><br>new line',
  3021. internal: false,
  3022. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3023. type: Ticket::Article::Type.find_by(name: 'email'),
  3024. updated_by_id: 1,
  3025. created_by_id: 1,
  3026. )
  3027. TransactionDispatcher.commit
  3028. ticket1.reload
  3029. assert_equal(12, ticket1.articles.count)
  3030. assert_equal('some_loop_sender@example.com', ticket1.articles[10].from)
  3031. assert_equal('nicole.braun@zammad.org', ticket1.articles[11].to)
  3032. Ticket::Article.create!(
  3033. ticket_id: ticket1.id,
  3034. from: 'some_loop_sender@example.com',
  3035. to: 'some_loop_recipient@example.com',
  3036. subject: 'some subject 1234',
  3037. message_id: 'some@id',
  3038. content_type: 'text/html',
  3039. body: 'some message <b>note</b><br>new line',
  3040. internal: false,
  3041. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3042. type: Ticket::Article::Type.find_by(name: 'email'),
  3043. updated_by_id: 1,
  3044. created_by_id: 1,
  3045. )
  3046. TransactionDispatcher.commit
  3047. ticket1.reload
  3048. assert_equal(14, ticket1.articles.count)
  3049. assert_equal('some_loop_sender@example.com', ticket1.articles[12].from)
  3050. assert_equal('nicole.braun@zammad.org', ticket1.articles[13].to)
  3051. Ticket::Article.create!(
  3052. ticket_id: ticket1.id,
  3053. from: 'some_loop_sender@example.com',
  3054. to: 'some_loop_recipient@example.com',
  3055. subject: 'some subject 1234',
  3056. message_id: 'some@id',
  3057. content_type: 'text/html',
  3058. body: 'some message <b>note</b><br>new line',
  3059. internal: false,
  3060. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3061. type: Ticket::Article::Type.find_by(name: 'email'),
  3062. updated_by_id: 1,
  3063. created_by_id: 1,
  3064. )
  3065. TransactionDispatcher.commit
  3066. ticket1.reload
  3067. assert_equal(16, ticket1.articles.count)
  3068. assert_equal('some_loop_sender@example.com', ticket1.articles[14].from)
  3069. assert_equal('nicole.braun@zammad.org', ticket1.articles[15].to)
  3070. Ticket::Article.create!(
  3071. ticket_id: ticket1.id,
  3072. from: 'some_loop_sender@example.com',
  3073. to: 'some_loop_recipient@example.com',
  3074. subject: 'some subject 1234',
  3075. message_id: 'some@id',
  3076. content_type: 'text/html',
  3077. body: 'some message <b>note</b><br>new line',
  3078. internal: false,
  3079. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3080. type: Ticket::Article::Type.find_by(name: 'email'),
  3081. updated_by_id: 1,
  3082. created_by_id: 1,
  3083. )
  3084. TransactionDispatcher.commit
  3085. ticket1.reload
  3086. assert_equal(18, ticket1.articles.count)
  3087. assert_equal('some_loop_sender@example.com', ticket1.articles[16].from)
  3088. assert_equal('nicole.braun@zammad.org', ticket1.articles[17].to)
  3089. Ticket::Article.create!(
  3090. ticket_id: ticket1.id,
  3091. from: 'some_loop_sender@example.com',
  3092. to: 'some_loop_recipient@example.com',
  3093. subject: 'some subject 1234',
  3094. message_id: 'some@id',
  3095. content_type: 'text/html',
  3096. body: 'some message <b>note</b><br>new line',
  3097. internal: false,
  3098. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3099. type: Ticket::Article::Type.find_by(name: 'email'),
  3100. updated_by_id: 1,
  3101. created_by_id: 1,
  3102. )
  3103. TransactionDispatcher.commit
  3104. ticket1.reload
  3105. assert_equal(20, ticket1.articles.count)
  3106. assert_equal('some_loop_sender@example.com', ticket1.articles[18].from)
  3107. assert_equal('nicole.braun@zammad.org', ticket1.articles[19].to)
  3108. Ticket::Article.create!(
  3109. ticket_id: ticket1.id,
  3110. from: 'some_loop_sender@example.com',
  3111. to: 'some_loop_recipient@example.com',
  3112. subject: 'some subject 1234',
  3113. message_id: 'some@id',
  3114. content_type: 'text/html',
  3115. body: 'some message <b>note</b><br>new line',
  3116. internal: false,
  3117. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3118. type: Ticket::Article::Type.find_by(name: 'email'),
  3119. updated_by_id: 1,
  3120. created_by_id: 1,
  3121. )
  3122. TransactionDispatcher.commit
  3123. ticket1.reload
  3124. assert_equal(21, ticket1.articles.count)
  3125. assert_equal('some_loop_sender@example.com', ticket1.articles[20].from)
  3126. Ticket::Article.create!(
  3127. ticket_id: ticket1.id,
  3128. from: 'some_loop_sender@example.com',
  3129. to: 'some_loop_recipient@example.com',
  3130. subject: 'some subject 1234',
  3131. message_id: 'some@id',
  3132. content_type: 'text/html',
  3133. body: 'some message <b>note</b><br>new line',
  3134. internal: false,
  3135. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3136. type: Ticket::Article::Type.find_by(name: 'email'),
  3137. updated_by_id: 1,
  3138. created_by_id: 1,
  3139. )
  3140. TransactionDispatcher.commit
  3141. ticket1.reload
  3142. assert_equal(22, ticket1.articles.count)
  3143. assert_equal('some_loop_sender@example.com', ticket1.articles[21].from)
  3144. end
  3145. test '3 invalid condition' do
  3146. trigger1 = Trigger.create!(
  3147. name: 'aaa loop check',
  3148. condition: {
  3149. 'ticket.action' => {
  3150. 'operator' => 'is',
  3151. 'value' => 'create',
  3152. },
  3153. },
  3154. perform: {
  3155. 'ticket.tags' => {
  3156. 'operator' => 'add',
  3157. 'value' => 'xxx',
  3158. },
  3159. },
  3160. disable_notification: true,
  3161. active: true,
  3162. created_by_id: 1,
  3163. updated_by_id: 1,
  3164. )
  3165. trigger1.update_column(:condition, {
  3166. 'ticket.action' => {
  3167. 'operator' => 'is',
  3168. 'value' => 'create',
  3169. },
  3170. 'ticket.first_response_at' => {
  3171. 'operator' => 'before (absolute)',
  3172. 'value' => 'invalid invalid 4',
  3173. },
  3174. })
  3175. assert_equal('invalid invalid 4', trigger1.condition['ticket.first_response_at']['value'])
  3176. Trigger.create!(
  3177. name: 'auto reply',
  3178. condition: {
  3179. 'ticket.action' => {
  3180. 'operator' => 'is',
  3181. 'value' => 'create',
  3182. },
  3183. 'ticket.state_id' => {
  3184. 'operator' => 'is',
  3185. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3186. }
  3187. },
  3188. perform: {
  3189. 'notification.email' => {
  3190. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  3191. 'recipient' => 'ticket_customer',
  3192. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3193. },
  3194. 'ticket.priority_id' => {
  3195. 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s,
  3196. },
  3197. 'ticket.tags' => {
  3198. 'operator' => 'add',
  3199. 'value' => 'aa, kk',
  3200. },
  3201. },
  3202. disable_notification: true,
  3203. active: true,
  3204. created_by_id: 1,
  3205. updated_by_id: 1,
  3206. )
  3207. ticket1 = Ticket.create!(
  3208. title: "some <b>title</b>\n äöüß",
  3209. group: Group.lookup(name: 'Users'),
  3210. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3211. updated_by_id: 1,
  3212. created_by_id: 1,
  3213. )
  3214. Ticket::Article.create!(
  3215. ticket_id: ticket1.id,
  3216. from: 'some_sender@example.com',
  3217. to: 'some_recipient@example.com',
  3218. subject: 'some subject',
  3219. message_id: 'some@id',
  3220. body: "some message <b>note</b>\nnew line",
  3221. internal: false,
  3222. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  3223. type: Ticket::Article::Type.find_by(name: 'note'),
  3224. updated_by_id: 1,
  3225. created_by_id: 1,
  3226. )
  3227. ticket1.reload
  3228. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  3229. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3230. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3231. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3232. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  3233. assert_equal([], ticket1.tag_list)
  3234. TransactionDispatcher.commit
  3235. ticket1.reload
  3236. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  3237. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3238. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3239. assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify')
  3240. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  3241. assert_equal(%w[aa kk], ticket1.tag_list)
  3242. article1 = ticket1.articles.last
  3243. assert_match('Zammad <zammad@localhost>', article1.from)
  3244. assert_match('nicole.braun@zammad.org', article1.to)
  3245. assert_match('Thanks for your inquiry (some <b>title</b> äöüß)!', article1.subject)
  3246. assert_match('Braun<br>some &lt;b&gt;title&lt;/b&gt;', article1.body)
  3247. assert_match('&gt; some message &lt;b&gt;note&lt;/b&gt;<br>&gt; new line', article1.body)
  3248. assert_equal('text/html', article1.content_type)
  3249. end
  3250. test '4 tag based auto response' do
  3251. Trigger.create!(
  3252. name: '100 add tag if sender 1',
  3253. condition: {
  3254. 'ticket.action' => {
  3255. 'operator' => 'is',
  3256. 'value' => 'create',
  3257. },
  3258. 'article.from' => {
  3259. 'operator' => 'contains',
  3260. 'value' => 'sender1',
  3261. },
  3262. },
  3263. perform: {
  3264. 'ticket.tags' => {
  3265. 'operator' => 'add',
  3266. 'value' => 'sender1',
  3267. },
  3268. },
  3269. disable_notification: true,
  3270. active: true,
  3271. created_by_id: 1,
  3272. updated_by_id: 1,
  3273. )
  3274. Trigger.create!(
  3275. name: '200 add tag if sender 2',
  3276. condition: {
  3277. 'ticket.action' => {
  3278. 'operator' => 'is',
  3279. 'value' => 'create',
  3280. },
  3281. 'article.from' => {
  3282. 'operator' => 'contains',
  3283. 'value' => 'sender2',
  3284. },
  3285. },
  3286. perform: {
  3287. 'ticket.tags' => {
  3288. 'operator' => 'add',
  3289. 'value' => 'sender2',
  3290. },
  3291. },
  3292. disable_notification: true,
  3293. active: true,
  3294. created_by_id: 1,
  3295. updated_by_id: 1,
  3296. )
  3297. Trigger.create!(
  3298. name: '300 auto reply',
  3299. condition: {
  3300. 'ticket.action' => {
  3301. 'operator' => 'is',
  3302. 'value' => 'create',
  3303. },
  3304. 'ticket.state_id' => {
  3305. 'operator' => 'is',
  3306. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3307. },
  3308. 'ticket.tags' => {
  3309. #'operator' => 'contains one not',
  3310. 'operator' => 'contains all not',
  3311. 'value' => 'sender1, sender2',
  3312. },
  3313. },
  3314. perform: {
  3315. 'notification.email' => {
  3316. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  3317. 'recipient' => 'ticket_customer',
  3318. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3319. },
  3320. },
  3321. disable_notification: true,
  3322. active: true,
  3323. created_by_id: 1,
  3324. updated_by_id: 1,
  3325. )
  3326. ticket1 = Ticket.create!(
  3327. title: 'test 1',
  3328. group: Group.lookup(name: 'Users'),
  3329. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3330. updated_by_id: 1,
  3331. created_by_id: 1,
  3332. )
  3333. Ticket::Article.create!(
  3334. ticket_id: ticket1.id,
  3335. from: 'sender1@example.com',
  3336. to: 'some_recipient@example.com',
  3337. subject: 'some subject',
  3338. message_id: 'some@id',
  3339. body: "some message <b>note</b>\nnew line",
  3340. internal: false,
  3341. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3342. type: Ticket::Article::Type.find_by(name: 'email'),
  3343. updated_by_id: 1,
  3344. created_by_id: 1,
  3345. )
  3346. ticket1.reload
  3347. assert_equal('test 1', ticket1.title, 'ticket1.title verify')
  3348. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3349. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3350. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3351. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  3352. assert_equal([], ticket1.tag_list)
  3353. TransactionDispatcher.commit
  3354. ticket1.reload
  3355. assert_equal('test 1', ticket1.title, 'ticket1.title verify')
  3356. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3357. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3358. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3359. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  3360. assert_equal(%w[sender1], ticket1.tag_list)
  3361. ticket2 = Ticket.create!(
  3362. title: 'test 2',
  3363. group: Group.lookup(name: 'Users'),
  3364. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3365. updated_by_id: 1,
  3366. created_by_id: 1,
  3367. )
  3368. Ticket::Article.create!(
  3369. ticket_id: ticket2.id,
  3370. from: 'sender2@example.com',
  3371. to: 'some_recipient@example.com',
  3372. subject: 'some subject',
  3373. message_id: 'some@id',
  3374. body: "some message <b>note</b>\nnew line",
  3375. internal: false,
  3376. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3377. type: Ticket::Article::Type.find_by(name: 'email'),
  3378. updated_by_id: 1,
  3379. created_by_id: 1,
  3380. )
  3381. ticket2.reload
  3382. assert_equal('test 2', ticket2.title, 'ticket2.title verify')
  3383. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  3384. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  3385. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  3386. assert_equal(1, ticket2.articles.count, 'ticket2.articles verify')
  3387. assert_equal([], ticket2.tag_list)
  3388. TransactionDispatcher.commit
  3389. ticket2.reload
  3390. assert_equal('test 2', ticket2.title, 'ticket2.title verify')
  3391. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  3392. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  3393. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  3394. assert_equal(1, ticket2.articles.count, 'ticket2.articles verify')
  3395. assert_equal(%w[sender2], ticket2.tag_list)
  3396. ticket3 = Ticket.create!(
  3397. title: 'test 3',
  3398. group: Group.lookup(name: 'Users'),
  3399. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3400. updated_by_id: 1,
  3401. created_by_id: 1,
  3402. )
  3403. assert(ticket3, 'ticket3 created')
  3404. Ticket::Article.create!(
  3405. ticket_id: ticket3.id,
  3406. from: 'sender0@example.com',
  3407. to: 'some_recipient@example.com',
  3408. subject: 'some subject',
  3409. message_id: 'some@id',
  3410. body: "some message <b>note</b>\nnew line",
  3411. internal: false,
  3412. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3413. type: Ticket::Article::Type.find_by(name: 'email'),
  3414. updated_by_id: 1,
  3415. created_by_id: 1,
  3416. )
  3417. ticket3.reload
  3418. assert_equal('test 3', ticket3.title, 'ticket3.title verify')
  3419. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  3420. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  3421. assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify')
  3422. assert_equal(1, ticket3.articles.count, 'ticket3.articles verify')
  3423. assert_equal([], ticket3.tag_list)
  3424. TransactionDispatcher.commit
  3425. ticket3.reload
  3426. assert_equal('test 3', ticket3.title, 'ticket3.title verify')
  3427. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  3428. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  3429. assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify')
  3430. assert_equal(2, ticket3.articles.count, 'ticket3.articles verify')
  3431. assert_equal([], ticket3.tag_list)
  3432. ticket3.articles.last
  3433. end
  3434. test 'article.body' do
  3435. Trigger.create!(
  3436. name: 'auto reply',
  3437. condition: {
  3438. 'ticket.action' => {
  3439. 'operator' => 'is',
  3440. 'value' => 'create',
  3441. },
  3442. 'ticket.state_id' => {
  3443. 'operator' => 'is',
  3444. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3445. },
  3446. 'article.body' => {
  3447. 'operator' => 'contains',
  3448. 'value' => 'hello',
  3449. },
  3450. },
  3451. perform: {
  3452. 'notification.email' => {
  3453. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  3454. 'recipient' => 'ticket_customer',
  3455. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3456. },
  3457. 'ticket.tags' => {
  3458. 'operator' => 'add',
  3459. 'value' => 'aa, kk',
  3460. },
  3461. },
  3462. disable_notification: true,
  3463. active: true,
  3464. created_by_id: 1,
  3465. updated_by_id: 1,
  3466. )
  3467. ticket1 = Ticket.create!(
  3468. title: 'test 1',
  3469. group: Group.lookup(name: 'Users'),
  3470. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3471. updated_by_id: 1,
  3472. created_by_id: 1,
  3473. )
  3474. Ticket::Article.create!(
  3475. ticket_id: ticket1.id,
  3476. from: 'some_sender@example.com',
  3477. to: 'some_recipient@example.com',
  3478. subject: 'some subject',
  3479. message_id: 'some@id',
  3480. body: 'some message <b>note</b> hello ',
  3481. internal: false,
  3482. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  3483. type: Ticket::Article::Type.find_by(name: 'note'),
  3484. updated_by_id: 1,
  3485. created_by_id: 1,
  3486. )
  3487. ticket1.reload
  3488. assert_equal('test 1', ticket1.title, 'ticket1.title verify')
  3489. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3490. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3491. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3492. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  3493. assert_equal([], ticket1.tag_list)
  3494. TransactionDispatcher.commit
  3495. ticket1.reload
  3496. assert_equal('test 1', ticket1.title, 'ticket1.title verify')
  3497. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3498. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3499. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  3500. assert_equal(%w[aa kk], ticket1.tag_list)
  3501. article1 = ticket1.articles.last
  3502. assert_match('Zammad <zammad@localhost>', article1.from)
  3503. assert_match('nicole.braun@zammad.org', article1.to)
  3504. assert_match('Thanks for your inquiry (test 1)!', article1.subject)
  3505. assert_match('some message', article1.body)
  3506. assert_match('&gt; some message &lt;b&gt;note&lt;/b&gt; hello', article1.body)
  3507. assert_equal('text/html', article1.content_type)
  3508. ticket2 = Ticket.create!(
  3509. title: 'test 1',
  3510. group: Group.lookup(name: 'Users'),
  3511. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3512. updated_by_id: 1,
  3513. created_by_id: 1,
  3514. )
  3515. Ticket::Article.create!(
  3516. ticket_id: ticket2.id,
  3517. from: 'some_sender@example.com',
  3518. to: 'some_recipient@example.com',
  3519. subject: 'some subject',
  3520. message_id: 'some@id',
  3521. body: 'some message <b>note</b>',
  3522. internal: false,
  3523. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  3524. type: Ticket::Article::Type.find_by(name: 'note'),
  3525. updated_by_id: 1,
  3526. created_by_id: 1,
  3527. )
  3528. ticket2.reload
  3529. assert_equal('test 1', ticket2.title, 'ticket2.title verify')
  3530. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  3531. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  3532. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  3533. assert_equal(1, ticket2.articles.count, 'ticket2.articles verify')
  3534. assert_equal([], ticket2.tag_list)
  3535. TransactionDispatcher.commit
  3536. ticket2.reload
  3537. assert_equal('test 1', ticket2.title, 'ticket2.title verify')
  3538. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  3539. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  3540. assert_equal(1, ticket2.articles.count, 'ticket2.articles verify')
  3541. assert_equal(%w[], ticket2.tag_list)
  3542. Trigger.create_or_update(
  3543. name: 'auto reply',
  3544. condition: {
  3545. 'ticket.action' => {
  3546. 'operator' => 'is',
  3547. 'value' => 'create',
  3548. },
  3549. 'ticket.state_id' => {
  3550. 'operator' => 'is',
  3551. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3552. },
  3553. 'article.body' => {
  3554. 'operator' => 'contains not',
  3555. 'value' => 'hello',
  3556. },
  3557. },
  3558. perform: {
  3559. 'notification.email' => {
  3560. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  3561. 'recipient' => 'ticket_customer',
  3562. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3563. },
  3564. 'ticket.tags' => {
  3565. 'operator' => 'add',
  3566. 'value' => 'aa, kk',
  3567. },
  3568. },
  3569. disable_notification: true,
  3570. active: true,
  3571. created_by_id: 1,
  3572. updated_by_id: 1,
  3573. )
  3574. ticket3 = Ticket.create!(
  3575. title: 'test 1',
  3576. group: Group.lookup(name: 'Users'),
  3577. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3578. updated_by_id: 1,
  3579. created_by_id: 1,
  3580. )
  3581. Ticket::Article.create!(
  3582. ticket_id: ticket3.id,
  3583. from: 'some_sender@example.com',
  3584. to: 'some_recipient@example.com',
  3585. subject: 'some subject',
  3586. message_id: 'some@id',
  3587. body: 'some message <b>note</b> hello ',
  3588. internal: false,
  3589. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  3590. type: Ticket::Article::Type.find_by(name: 'note'),
  3591. updated_by_id: 1,
  3592. created_by_id: 1,
  3593. )
  3594. ticket3.reload
  3595. assert_equal('test 1', ticket3.title, 'ticket3.title verify')
  3596. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  3597. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  3598. assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify')
  3599. assert_equal(1, ticket3.articles.count, 'ticket3.articles verify')
  3600. assert_equal([], ticket3.tag_list)
  3601. TransactionDispatcher.commit
  3602. ticket3.reload
  3603. assert_equal('test 1', ticket3.title, 'ticket3.title verify')
  3604. assert_equal('Users', ticket3.group.name, 'ticket3.group verify')
  3605. assert_equal('new', ticket3.state.name, 'ticket3.state verify')
  3606. assert_equal(1, ticket3.articles.count, 'ticket3.articles verify')
  3607. assert_equal(%w[], ticket3.tag_list)
  3608. ticket4 = Ticket.create!(
  3609. title: 'test 1',
  3610. group: Group.lookup(name: 'Users'),
  3611. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3612. updated_by_id: 1,
  3613. created_by_id: 1,
  3614. )
  3615. Ticket::Article.create!(
  3616. ticket_id: ticket4.id,
  3617. from: 'some_sender@example.com',
  3618. to: 'some_recipient@example.com',
  3619. subject: 'some subject',
  3620. message_id: 'some@id',
  3621. body: 'some message <b>note</b> 2',
  3622. internal: false,
  3623. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  3624. type: Ticket::Article::Type.find_by(name: 'note'),
  3625. updated_by_id: 1,
  3626. created_by_id: 1,
  3627. )
  3628. ticket4.reload
  3629. assert_equal('test 1', ticket4.title, 'ticket4.title verify')
  3630. assert_equal('Users', ticket4.group.name, 'ticket4.group verify')
  3631. assert_equal('new', ticket4.state.name, 'ticket4.state verify')
  3632. assert_equal('2 normal', ticket4.priority.name, 'ticket4.priority verify')
  3633. assert_equal(1, ticket4.articles.count, 'ticket4.articles verify')
  3634. assert_equal([], ticket4.tag_list)
  3635. TransactionDispatcher.commit
  3636. ticket4.reload
  3637. assert_equal('test 1', ticket4.title, 'ticket4.title verify')
  3638. assert_equal('Users', ticket4.group.name, 'ticket4.group verify')
  3639. assert_equal('new', ticket4.state.name, 'ticket4.state verify')
  3640. assert_equal(2, ticket4.articles.count, 'ticket4.articles verify')
  3641. assert_equal(%w[aa kk], ticket4.tag_list)
  3642. article4 = ticket4.articles.last
  3643. assert_match('Zammad <zammad@localhost>', article4.from)
  3644. assert_match('nicole.braun@zammad.org', article4.to)
  3645. assert_match('Thanks for your inquiry (test 1)!', article4.subject)
  3646. assert_match('some message', article4.body)
  3647. assert_match('&gt; some message &lt;b&gt;note&lt;/b&gt; 2', article4.body)
  3648. assert_equal('text/html', article4.content_type)
  3649. end
  3650. test 'change owner' do
  3651. roles = Role.where(name: 'Agent')
  3652. groups = Group.where(name: 'Users')
  3653. agent1 = User.create!(
  3654. login: 'agent-has-changed@example.com',
  3655. firstname: 'Has Changed',
  3656. lastname: 'Agent1',
  3657. email: 'agent-has-changed@example.com',
  3658. password: 'agentpw',
  3659. active: true,
  3660. roles: roles,
  3661. groups: groups,
  3662. updated_at: '2015-02-05 16:37:00',
  3663. updated_by_id: 1,
  3664. created_by_id: 1,
  3665. )
  3666. agent2 = User.create!(
  3667. login: 'agent-has-changed2@example.com',
  3668. firstname: 'Has Changed',
  3669. lastname: 'Agent2',
  3670. email: 'agent-has-changed2@example.com',
  3671. password: 'agentpw',
  3672. active: true,
  3673. roles: roles,
  3674. groups: groups,
  3675. updated_at: '2015-02-05 16:37:00',
  3676. updated_by_id: 1,
  3677. created_by_id: 1,
  3678. )
  3679. # multi tag trigger with changed owner
  3680. Trigger.create!(
  3681. name: 'change owner',
  3682. condition: {
  3683. 'ticket.owner_id' => {
  3684. 'operator' => 'has changed',
  3685. },
  3686. 'ticket.tags' => {
  3687. 'operator' => 'contains one not',
  3688. 'value' => 'nosendmail test123'
  3689. }
  3690. },
  3691. perform: {
  3692. 'ticket.tags' => {
  3693. 'operator' => 'add',
  3694. 'value' => '123'
  3695. },
  3696. 'notification.email' => {
  3697. 'body' => 'some lala',
  3698. 'recipient' => 'ticket_customer',
  3699. 'subject' => 'Thanks for your inquiry - 1234 check (#{ticket.title})!',
  3700. },
  3701. },
  3702. disable_notification: true,
  3703. active: true,
  3704. created_by_id: 1,
  3705. updated_by_id: 1,
  3706. )
  3707. # single tag trigger with changed owner
  3708. Trigger.create_or_update(
  3709. name: 'change owner',
  3710. condition: {
  3711. 'ticket.owner_id' => {
  3712. 'operator' => 'has changed',
  3713. },
  3714. 'ticket.tags' => {
  3715. 'operator' => 'contains one not',
  3716. 'value' => 'nosendmail2',
  3717. }
  3718. },
  3719. perform: {
  3720. 'ticket.tags' => {
  3721. 'operator' => 'add',
  3722. 'value' => '123'
  3723. },
  3724. 'notification.email' => {
  3725. 'body' => 'some lala',
  3726. 'recipient' => 'ticket_customer',
  3727. 'subject' => 'Thanks for your inquiry - 1234 check (#{ticket.title})!',
  3728. },
  3729. },
  3730. disable_notification: true,
  3731. active: true,
  3732. created_by_id: 1,
  3733. updated_by_id: 1,
  3734. )
  3735. ticket1 = Ticket.create!(
  3736. title: "some title\n äöüß",
  3737. group: Group.lookup(name: 'Users'),
  3738. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3739. updated_by_id: 1,
  3740. created_by_id: 1,
  3741. )
  3742. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  3743. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3744. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3745. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3746. assert_equal(0, ticket1.articles.count, 'ticket1.articles verify')
  3747. assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent')
  3748. assert_equal([], ticket1.tag_list, 'ticket1.tag_list')
  3749. ticket2 = Ticket.create!(
  3750. title: "some title\n äöüß",
  3751. group: Group.lookup(name: 'Users'),
  3752. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3753. updated_by_id: 1,
  3754. created_by_id: 1,
  3755. )
  3756. assert_equal('some title äöüß', ticket2.title, 'ticket1.title verify')
  3757. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  3758. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  3759. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  3760. assert_equal(0, ticket2.articles.count, 'ticket2.articles verify')
  3761. assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent')
  3762. assert_equal([], ticket1.tag_list, 'ticket2.tag_list')
  3763. # control test - should pass
  3764. # create common object tag
  3765. Tag::Object.create_or_update(name: 'Ticket')
  3766. # add tag
  3767. ticket1.tag_add('thisisthebestjob', agent1.id)
  3768. # change owner
  3769. ticket1.owner_id = agent1.id
  3770. ticket1.save!
  3771. TransactionDispatcher.commit
  3772. # this will add a tag by trigger
  3773. ticket1.reload
  3774. assert_equal('some title äöüß', ticket1.title, 'ticket2.title verify')
  3775. assert_equal('Users', ticket1.group.name, 'ticket2.group verify')
  3776. assert_equal('new', ticket1.state.name, 'ticket2.state verify')
  3777. assert_equal('2 normal', ticket1.priority.name, 'ticket2.priority verify')
  3778. assert_equal(1, ticket1.articles.count, 'ticket2.articles verify') # articles.count must be 1 if the tag is added
  3779. assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent')
  3780. assert_equal(%w[thisisthebestjob 123], ticket1.tag_list, 'ticket2.tag_list')
  3781. # add tag nosendmail (to test the bug)
  3782. ticket1.tag_add('nosendmail', agent2.id)
  3783. # change owner
  3784. ticket1.owner_id = agent2.id
  3785. ticket1.save!
  3786. TransactionDispatcher.commit
  3787. ticket1.reload
  3788. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  3789. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3790. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3791. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3792. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') # articles.count must be 0 if adding tag is skipped
  3793. assert_equal('agent-has-changed2@example.com', agent2.login, 'verify agent')
  3794. assert_equal(%w[thisisthebestjob 123 nosendmail], ticket1.tag_list, 'ticket1.tag_list')
  3795. # add tag (to test the bug)
  3796. ticket1.tag_add('test123', agent2.id)
  3797. # change owner
  3798. ticket1.owner_id = agent1.id
  3799. ticket1.save!
  3800. TransactionDispatcher.commit
  3801. ticket1.reload
  3802. assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify')
  3803. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3804. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3805. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3806. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') # articles.count must be 0 if adding tag is skipped
  3807. assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent')
  3808. assert_equal(%w[thisisthebestjob 123 nosendmail test123], ticket1.tag_list, 'ticket1.tag_list')
  3809. # add tag single tag 'nosendmail2' (to test the bug)
  3810. ticket2.tag_add('nosendmail2', agent1.id)
  3811. # change owner
  3812. ticket2.owner_id = agent1.id
  3813. ticket2.save!
  3814. TransactionDispatcher.commit
  3815. ticket2.reload
  3816. assert_equal('some title äöüß', ticket2.title, 'ticket2.title verify')
  3817. assert_equal('Users', ticket2.group.name, 'ticket2.group verify')
  3818. assert_equal('new', ticket2.state.name, 'ticket2.state verify')
  3819. assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify')
  3820. assert_equal(0, ticket2.articles.count, 'ticket2.articles verify') # articles.count must be 0 if adding tag is skipped
  3821. assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent')
  3822. assert_equal(['nosendmail2'], ticket2.tag_list, 'ticket2.tag_list')
  3823. end
  3824. test 'trigger auto reply with umlaut in form' do
  3825. Trigger.create!(
  3826. name: 'auto reply',
  3827. condition: {
  3828. 'ticket.action' => {
  3829. 'operator' => 'is',
  3830. 'value' => 'create',
  3831. },
  3832. 'ticket.state_id' => {
  3833. 'operator' => 'is',
  3834. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3835. },
  3836. },
  3837. perform: {
  3838. 'notification.email' => {
  3839. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  3840. 'recipient' => 'article_last_sender',
  3841. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3842. },
  3843. },
  3844. disable_notification: true,
  3845. active: true,
  3846. created_by_id: 1,
  3847. updated_by_id: 1,
  3848. )
  3849. ticket1 = Ticket.create!(
  3850. title: 'test 1',
  3851. group: Group.lookup(name: 'Users'),
  3852. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  3853. updated_by_id: 1,
  3854. created_by_id: 1,
  3855. )
  3856. Ticket::Article.create!(
  3857. ticket_id: ticket1.id,
  3858. from: 'Sabine Schütz <some_sender@example.com>',
  3859. to: 'some_recipient@example.com',
  3860. subject: 'some subject',
  3861. message_id: 'some@id',
  3862. body: 'some message <b>note</b> hello ',
  3863. internal: false,
  3864. sender: Ticket::Article::Sender.find_by(name: 'Customer'),
  3865. type: Ticket::Article::Type.find_by(name: 'email'),
  3866. updated_by_id: 1,
  3867. created_by_id: 1,
  3868. )
  3869. ticket1.reload
  3870. assert_equal('test 1', ticket1.title, 'ticket1.title verify')
  3871. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3872. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3873. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  3874. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  3875. assert_equal('Sabine Schütz <some_sender@example.com>', ticket1.articles.first.from, 'ticket1.articles.first.from verify')
  3876. assert_equal([], ticket1.tag_list)
  3877. TransactionDispatcher.commit
  3878. ticket1.reload
  3879. assert_equal('test 1', ticket1.title, 'ticket1.title verify')
  3880. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3881. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3882. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  3883. article1 = ticket1.articles.last
  3884. assert_match('Zammad <zammad@localhost>', article1.from)
  3885. assert_match('some_sender@example.com', article1.to)
  3886. assert_match('Thanks for your inquiry (test 1)!', article1.subject)
  3887. assert_match('some message', article1.body)
  3888. assert_match('&gt; some message &lt;b&gt;note&lt;/b&gt; hello', article1.body)
  3889. assert_equal('text/html', article1.content_type)
  3890. end
  3891. test 'trigger auto reply with 2 sender addresses in form' do
  3892. Trigger.create!(
  3893. name: 'auto reply',
  3894. condition: {
  3895. 'ticket.action' => {
  3896. 'operator' => 'is',
  3897. 'value' => 'create',
  3898. },
  3899. 'ticket.state_id' => {
  3900. 'operator' => 'is',
  3901. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3902. },
  3903. },
  3904. perform: {
  3905. 'notification.email' => {
  3906. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}',
  3907. 'recipient' => 'article_last_sender',
  3908. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3909. },
  3910. },
  3911. disable_notification: true,
  3912. active: true,
  3913. created_by_id: 1,
  3914. updated_by_id: 1,
  3915. )
  3916. ticket1, _article1, _user, _mail = Channel::EmailParser.new.process({}, File.read(Rails.root.join('test/data/mail/mail065.box')))
  3917. assert_equal('aaäöüßad asd', ticket1.title, 'ticket1.title verify')
  3918. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3919. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3920. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  3921. article1 = ticket1.articles.last
  3922. assert_match('Zammad <zammad@localhost>', article1.from)
  3923. assert_match('smith@example.com', article1.to)
  3924. assert_match('Thanks for your inquiry (aaäöüßad asd)!', article1.subject)
  3925. assert_match('some text<br><br>aaäöüßad asd', article1.body)
  3926. assert_equal('text/html', article1.content_type)
  3927. end
  3928. test 'make sure attachments should be attached with content id' do
  3929. Trigger.create!(
  3930. name: 'auto reply',
  3931. condition: {
  3932. 'ticket.action' => {
  3933. 'operator' => 'is',
  3934. 'value' => 'create',
  3935. },
  3936. 'ticket.state_id' => {
  3937. 'operator' => 'is',
  3938. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  3939. },
  3940. },
  3941. perform: {
  3942. 'notification.email' => {
  3943. 'body' => 'some text<br>#{ticket.customer.lastname}<br>#{ticket.title}<br>#{article.body}<br><img tabindex="0" style="width: 192px; height: 192px" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCADAAMADAREAAhEBAxEB/8QAHgABAAICAwEBAQAAAAAAAAAAAAcICQoFBgsDAQT/xAA7EAAABwEAAQMCAgYJAgcAAAAAAQIDBAUGBwgJERITIQoUFRciMXa1FiMyNzg5QVF3JLIYGSc1QkVh/8QAHQEBAAICAwEBAAAAAAAAAAAAAAQFAwYCBwgBCf/EAEURAAICAgEDAgMEBAkLBAMAAAECAAMEEQUGEiETMQciQQgUMlEjYXF2FTM1QnJzgZGzFjY3OFJiobGytLUXGILBJTRD/9oADAMBAAIRAxEAPwDU/G4SPARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARARLM+HPi5uvNDyU5T4086cbiaXp+iTU/piTGflwM3TxYsiyv9PZx4xk+5WZ+nhzLSelk/qnGjLJslLNKT++Atjk6Wquy1z/uo">',
  3944. 'recipient' => 'article_last_sender',
  3945. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3946. },
  3947. },
  3948. disable_notification: true,
  3949. active: true,
  3950. created_by_id: 1,
  3951. updated_by_id: 1,
  3952. )
  3953. ticket1, _article1, _user, _mail = Channel::EmailParser.new.process({}, File.read(Rails.root.join('test/data/mail/mail065.box')))
  3954. assert_equal('aaäöüßad asd', ticket1.title, 'ticket1.title verify')
  3955. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  3956. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  3957. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  3958. article1 = ticket1.articles.last
  3959. assert_match('Zammad <zammad@localhost>', article1.from)
  3960. assert_match('smith@example.com', article1.to)
  3961. assert_match('Thanks for your inquiry (aaäöüßad asd)!', article1.subject)
  3962. assert_match(%r{.+cid:.+?@zammad.example.com.+}, article1.body)
  3963. assert_equal(1, article1.attachments.count)
  3964. assert_equal('789', article1.attachments[0].size)
  3965. assert_equal('text/html', article1.content_type)
  3966. end
  3967. # Issue #1316 - 'organization is not X' conditions break triggers
  3968. test 'NOT IN predicates handle NULL values' do
  3969. customer = User.create!(
  3970. email: 'issue_1316_test_user@zammad.org',
  3971. created_by_id: 1,
  3972. updated_by_id: 1,
  3973. )
  3974. Trigger.create!(
  3975. name: 'auto reply (condition: organization-is-not)',
  3976. condition: {
  3977. 'ticket.organization_id' => {
  3978. 'operator' => 'is not',
  3979. 'value' => Organization.first.id.to_s,
  3980. },
  3981. },
  3982. perform: {
  3983. 'notification.email' => {
  3984. 'body' => 'Lorem ipsum dolor',
  3985. 'recipient' => 'ticket_customer',
  3986. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  3987. },
  3988. },
  3989. active: true,
  3990. created_by_id: 1,
  3991. updated_by_id: 1,
  3992. )
  3993. ticket = Ticket.create!(
  3994. title: "some <b>title</b>\n äöüß",
  3995. group: Group.lookup(name: 'Users'),
  3996. customer: customer,
  3997. updated_by_id: 1,
  3998. created_by_id: 1,
  3999. )
  4000. assert_nil(customer.organization_id)
  4001. assert_equal(0, ticket.reload.articles.count, 'ticket.articles verify')
  4002. TransactionDispatcher.commit
  4003. assert_equal(1, ticket.reload.articles.count, 'ticket.articles verify')
  4004. autoreply = ticket.articles.first
  4005. assert_equal('Zammad <zammad@localhost>', autoreply.from)
  4006. assert_equal(customer.email, autoreply.to)
  4007. assert_equal("Thanks for your inquiry (#{ticket.title})!", autoreply.subject)
  4008. assert_equal('Lorem ipsum dolor', autoreply.body)
  4009. assert_equal('text/html', autoreply.content_type)
  4010. end
  4011. test 'trigger tags and auto responder when there is an article body contains matched values' do
  4012. Trigger.create!(
  4013. name: 'detect message body',
  4014. condition: {
  4015. 'article.body' => {
  4016. 'operator' => 'contains',
  4017. 'value' => 'some message',
  4018. },
  4019. },
  4020. perform: {
  4021. 'ticket.tags' => {
  4022. 'operator' => 'add',
  4023. 'value' => 'tag1, tag2',
  4024. },
  4025. 'notification.email' => {
  4026. 'body' => 'some lala',
  4027. 'recipient' => 'ticket_customer',
  4028. 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!',
  4029. },
  4030. },
  4031. disable_notification: true,
  4032. active: true,
  4033. created_by_id: 1,
  4034. updated_by_id: 1,
  4035. )
  4036. ticket1 = Ticket.create!(
  4037. title: "some <b>title</b>\n äöüß",
  4038. group: Group.lookup(name: 'Users'),
  4039. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  4040. updated_by_id: 1,
  4041. created_by_id: 1,
  4042. )
  4043. article1 = Ticket::Article.create!(
  4044. ticket_id: ticket1.id,
  4045. from: 'some_sender@example.com',
  4046. to: 'some_recipient@example.com',
  4047. subject: 'some subject',
  4048. message_id: 'some@id',
  4049. body: "some message <b>note</b>\nnew line",
  4050. internal: false,
  4051. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  4052. type: Ticket::Article::Type.find_by(name: 'note'),
  4053. updated_by_id: 1,
  4054. created_by_id: 1,
  4055. )
  4056. ticket1.reload
  4057. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  4058. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  4059. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  4060. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  4061. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  4062. assert_equal([], ticket1.tag_list)
  4063. TransactionDispatcher.commit
  4064. ticket1.reload
  4065. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  4066. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  4067. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  4068. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  4069. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  4070. assert_equal(%w[tag1 tag2], ticket1.tag_list)
  4071. assert_match('- ', article1.from)
  4072. assert_match('some_recipient@example.com', article1.to)
  4073. assert_match('some subject', article1.subject)
  4074. assert_match("some message <b>note</b>\nnew line", article1.body)
  4075. assert_equal('text/plain', article1.content_type)
  4076. end
  4077. test 'trigger note and auto responder (correct order) when there is an article body contains matched values' do
  4078. Trigger.create!(
  4079. name: 'detect message body',
  4080. condition: {
  4081. 'article.body' => {
  4082. 'operator' => 'contains',
  4083. 'value' => 'some message',
  4084. },
  4085. },
  4086. perform: {
  4087. 'article.note' => {
  4088. 'subject' => 'some subject! #{ticket.id}',
  4089. 'body' => 'I can integrate with 3rd party services at https://my.saas/foo/#{ticket.id}',
  4090. 'internal' => 'true',
  4091. },
  4092. 'notification.email' => {
  4093. 'body' => 'some lala',
  4094. 'recipient' => 'ticket_customer',
  4095. 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!',
  4096. },
  4097. },
  4098. disable_notification: true,
  4099. active: true,
  4100. created_by_id: 1,
  4101. updated_by_id: 1,
  4102. )
  4103. ticket1 = Ticket.create!(
  4104. title: "some <b>title</b>\n äöüß",
  4105. group: Group.lookup(name: 'Users'),
  4106. customer: User.lookup(email: 'nicole.braun@zammad.org'),
  4107. updated_by_id: 1,
  4108. created_by_id: 1,
  4109. )
  4110. article1 = Ticket::Article.create!(
  4111. ticket_id: ticket1.id,
  4112. from: 'some_sender@example.com',
  4113. to: 'some_recipient@example.com',
  4114. subject: 'some subject',
  4115. message_id: 'some@id',
  4116. body: "some message <b>note</b>\nnew line",
  4117. internal: false,
  4118. sender: Ticket::Article::Sender.find_by(name: 'Agent'),
  4119. type: Ticket::Article::Type.find_by(name: 'note'),
  4120. updated_by_id: 1,
  4121. created_by_id: 1,
  4122. )
  4123. ticket1.reload
  4124. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  4125. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  4126. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  4127. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  4128. assert_equal(1, ticket1.articles.count, 'ticket1.articles verify')
  4129. assert_equal([], ticket1.tag_list)
  4130. TransactionDispatcher.commit
  4131. ticket1.reload
  4132. assert_equal('some <b>title</b> äöüß', ticket1.title, 'ticket1.title verify')
  4133. assert_equal('Users', ticket1.group.name, 'ticket1.group verify')
  4134. assert_equal('new', ticket1.state.name, 'ticket1.state verify')
  4135. assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify')
  4136. assert_equal(3, ticket1.articles.count, 'ticket1.articles verify')
  4137. assert_equal([], ticket1.tag_list)
  4138. assert_match('- ', article1.from)
  4139. assert_match('some_recipient@example.com', article1.to)
  4140. assert_match('some subject', article1.subject)
  4141. assert_match("some message <b>note</b>\nnew line", article1.body)
  4142. assert_equal('text/plain', article1.content_type)
  4143. article_note1 = ticket1.articles[1]
  4144. assert_match('- ', article_note1.from)
  4145. assert_nil(article_note1.to)
  4146. assert_match("some subject! #{ticket1.id}", article_note1.subject)
  4147. assert_match("I can integrate with 3rd party services at <a href=\"https://my.saas/foo/#{ticket1.id}\" rel=\"nofollow noreferrer noopener\" target=\"_blank\">https://my.saas/foo/#{ticket1.id}</a>", article_note1.body)
  4148. assert_equal('text/html', article_note1.content_type)
  4149. assert_equal(true, article_note1.internal)
  4150. article_auto_responder1 = ticket1.articles[2]
  4151. assert_match('Zammad <zammad@localhost>', article_auto_responder1.from)
  4152. assert_match('nicole.braun@zammad.org', article_auto_responder1.to)
  4153. assert_match('Thanks for your inquiry - loop check (some <b>title</b> äöüß)!', article_auto_responder1.subject)
  4154. assert_match('some lala', article_auto_responder1.body)
  4155. assert_equal('text/html', article_auto_responder1.content_type)
  4156. end
  4157. test 'validates perform with article.note - should fail because of missing body' do
  4158. assert_raises(Exception) do
  4159. Trigger.create!(
  4160. name: 'some trigger',
  4161. condition: {
  4162. 'article.body' => {
  4163. 'operator' => 'contains',
  4164. 'value' => 'some message',
  4165. },
  4166. },
  4167. perform: {
  4168. 'article.note' => {
  4169. 'subject' => 'some subject!',
  4170. 'internal' => 'true',
  4171. },
  4172. },
  4173. disable_notification: true,
  4174. active: true,
  4175. created_by_id: 1,
  4176. updated_by_id: 1,
  4177. )
  4178. end
  4179. end
  4180. test 'validates perform with notification.email - should fail because of missing recipient' do
  4181. assert_raises(Exception) do
  4182. Trigger.create!(
  4183. name: 'some trigger',
  4184. condition: {
  4185. 'article.body' => {
  4186. 'operator' => 'contains',
  4187. 'value' => 'some message',
  4188. },
  4189. },
  4190. perform: {
  4191. 'notification.email' => {
  4192. 'body' => 'some lala',
  4193. 'recipient' => '',
  4194. 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!',
  4195. },
  4196. },
  4197. disable_notification: true,
  4198. active: true,
  4199. created_by_id: 1,
  4200. updated_by_id: 1,
  4201. )
  4202. end
  4203. end
  4204. test 'validates perform with notification.sms - should fail because of missing recipient' do
  4205. assert_raises(Exception) do
  4206. Trigger.create!(
  4207. name: 'some trigger',
  4208. condition: {
  4209. 'article.body' => {
  4210. 'operator' => 'contains',
  4211. 'value' => 'some message',
  4212. },
  4213. },
  4214. perform: {
  4215. 'notification.sms' => {
  4216. 'body' => 'some lala',
  4217. 'recipient' => '',
  4218. },
  4219. },
  4220. disable_notification: true,
  4221. active: true,
  4222. created_by_id: 1,
  4223. updated_by_id: 1,
  4224. )
  4225. end
  4226. end
  4227. #2399 - Attached images are broken on trigger reply with #{article.body_as_html}
  4228. test 'make sure auto reply using #{article.body_as_html} copies all articles image attachments as well' do
  4229. # make sure that this auto reply trigger only reacts to this particular test in order not to interfer with other auto reply tests
  4230. Trigger.create!(
  4231. name: 'auto reply with HTML quote',
  4232. condition: {
  4233. 'ticket.action' => {
  4234. 'operator' => 'is',
  4235. 'value' => 'create',
  4236. },
  4237. 'ticket.state_id' => {
  4238. 'operator' => 'is',
  4239. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  4240. },
  4241. 'ticket.title' => {
  4242. 'operator' => 'contains',
  4243. 'value' => 'AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545]',
  4244. },
  4245. },
  4246. perform: {
  4247. 'notification.email' => {
  4248. 'body' => '#{article.body_as_html}',
  4249. 'recipient' => 'article_last_sender',
  4250. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  4251. },
  4252. },
  4253. disable_notification: true,
  4254. active: true,
  4255. created_by_id: 1,
  4256. updated_by_id: 1,
  4257. )
  4258. ticket1, _article1, _user, _mail = Channel::EmailParser.new.process({}, File.read(Rails.root.join('test/data/mail/mail048.box')))
  4259. assert_equal('AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545]', ticket1.title, 'ticket1.title verify')
  4260. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  4261. assert_equal(2, ticket1.articles.first.attachments.count)
  4262. article1 = ticket1.articles.last
  4263. assert_match('Thanks for your inquiry (AW: OTRS / Anfrage OTRS Einführung/Präsentation [Ticket#11545])!', article1.subject)
  4264. assert_equal(1, article1.attachments.count)
  4265. assert_equal('50606', article1.attachments[0].size)
  4266. assert_equal('CPG-Reklamationsmitteilung bezügl.01234567895 an Voda-28.03.2017.jpg', article1.attachments[0].filename)
  4267. end
  4268. #2399 - Attached images are broken on trigger reply with #{article.body_as_html}
  4269. test 'make sure auto reply using #{article.body_as_html} does not copy any non-image attachments' do
  4270. # make sure that this auto reply trigger only reacts to this particular test in order not to interfer with other auto reply tests
  4271. Trigger.create!(
  4272. name: 'auto reply with HTML quote',
  4273. condition: {
  4274. 'ticket.action' => {
  4275. 'operator' => 'is',
  4276. 'value' => 'create',
  4277. },
  4278. 'ticket.state_id' => {
  4279. 'operator' => 'is',
  4280. 'value' => Ticket::State.lookup(name: 'new').id.to_s,
  4281. },
  4282. 'ticket.title' => {
  4283. 'operator' => 'contains',
  4284. 'value' => 'Online-apotheke. Günstigster Preis. Ohne Rezepte',
  4285. },
  4286. },
  4287. perform: {
  4288. 'notification.email' => {
  4289. 'body' => '#{article.body_as_html}',
  4290. 'recipient' => 'article_last_sender',
  4291. 'subject' => 'Thanks for your inquiry (#{ticket.title})!',
  4292. },
  4293. },
  4294. disable_notification: true,
  4295. active: true,
  4296. created_by_id: 1,
  4297. updated_by_id: 1,
  4298. )
  4299. ticket1, _article1, _user, _mail = Channel::EmailParser.new.process({}, File.read(Rails.root.join('test/data/mail/mail069.box')))
  4300. assert_equal('Online-apotheke. Günstigster Preis. Ohne Rezepte', ticket1.title, 'ticket1.title verify')
  4301. assert_equal(2, ticket1.articles.count, 'ticket1.articles verify')
  4302. assert_equal(1, ticket1.articles.first.attachments.count)
  4303. article1 = ticket1.articles.last
  4304. assert_match('Thanks for your inquiry (Online-apotheke. Günstigster Preis. Ohne Rezepte)!', article1.subject)
  4305. assert_equal(0, article1.attachments.count)
  4306. end
  4307. end