ticket_trigger_test.rb 174 KB

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