ticket_trigger_test.rb 146 KB

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