From b403432c767a50668ffa97b4baeaab677ff04462 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Tue, 29 May 2018 17:42:14 +0200 Subject: [PATCH] Initial support of recursive triggers for tickets - issue #2035. --- .rubocop.yml | 1 + app/models/channel/email_parser.rb | 4 + app/models/ticket.rb | 181 +- app/models/transaction.rb | 5 +- app/models/transaction/trigger.rb | 104 +- ...001_issue_2035_recursive_ticket_trigger.rb | 79 + db/seeds/settings.rb | 71 + test/unit/email_process_auto_response_test.rb | 667 ++- ...rigger_extended_recursive_disabled_test.rb | 677 +++ test/unit/ticket_trigger_extended_test.rb | 718 +++ .../ticket_trigger_recursive_disabled_test.rb | 4293 +++++++++++++++++ test/unit/ticket_trigger_test.rb | 86 +- 12 files changed, 6739 insertions(+), 147 deletions(-) create mode 100644 db/migrate/20180529000001_issue_2035_recursive_ticket_trigger.rb create mode 100644 test/unit/ticket_trigger_extended_recursive_disabled_test.rb create mode 100644 test/unit/ticket_trigger_extended_test.rb create mode 100644 test/unit/ticket_trigger_recursive_disabled_test.rb diff --git a/.rubocop.yml b/.rubocop.yml index cdd47ae2f..292590f30 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -202,6 +202,7 @@ Lint/InterpolationCheck: - "test/unit/notification_factory_renderer_test.rb" - "test/unit/notification_factory_template_test.rb" - "test/unit/ticket_trigger_test.rb" + - "test/unit/ticket_trigger_recursive_disabled_test.rb" # RSpec tests Style/NumericPredicate: diff --git a/app/models/channel/email_parser.rb b/app/models/channel/email_parser.rb index 49a45451c..e98a66bbb 100644 --- a/app/models/channel/email_parser.rb +++ b/app/models/channel/email_parser.rb @@ -677,6 +677,10 @@ returns end end + ticket.reload + article.reload + session_user.reload + # run postmaster post filter filters = {} Setting.where(area: 'Postmaster::PostFilter').order(:name).each do |setting| diff --git a/app/models/ticket.rb b/app/models/ticket.rb index 2dd8db5d9..a1f4777ef 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -1046,11 +1046,11 @@ perform changes on ticket tags = value['value'].split(/,/) if value['operator'] == 'add' tags.each do |tag| - tag_add(tag) + tag_add(tag, current_user_id || 1) end elsif value['operator'] == 'remove' tags.each do |tag| - tag_remove(tag) + tag_remove(tag, current_user_id || 1) end else logger.error "Unknown #{attribute} operator #{value['operator']}" @@ -1085,6 +1085,183 @@ perform changes on ticket end return if !changed save + + true + end + +=begin + +perform active triggers on ticket + + Ticket.perform_triggers(ticket, article, item, options) + +=end + + def self.perform_triggers(ticket, article, item, options = {}) + recursive = Setting.get('ticket_trigger_recursive') + type = options[:type] || item[:type] + local_options = options.clone + local_options[:type] = type + local_options[:reset_user_id] = true + local_options[:disable] = ['Transaction::Notification'] + local_options[:trigger_ids] ||= {} + local_options[:trigger_ids][ticket.id] ||= [] + local_options[:loop_count] ||= 0 + local_options[:loop_count] += 1 + + ticket_trigger_recursive_max_loop = Setting.get('ticket_trigger_recursive_max_loop')&.to_i || 10 + if local_options[:loop_count] > ticket_trigger_recursive_max_loop + message = "Stopped perform_triggers for this object (Ticket/#{ticket.id}), because loop count was #{local_options[:loop_count]}!" + logger.info { message } + return [false, message] + end + + triggers = if Rails.configuration.db_case_sensitive + ::Trigger.where(active: true).order('LOWER(name)') + else + ::Trigger.where(active: true).order(:name) + end + return [true, 'No triggers active'] if triggers.blank? + + # check if notification should be send because of customer emails + send_notification = true + if local_options[:send_notification] == false + send_notification = false + elsif item[:article_id] + article = Ticket::Article.lookup(id: item[:article_id]) + if article&.preferences && article.preferences['send-auto-response'] == false + send_notification = false + end + end + + Transaction.execute(local_options) do + triggers.each do |trigger| + condition = trigger.condition + + # check if one article attribute is used + one_has_changed_done = false + article_selector = false + trigger.condition.each_key do |key| + (object_name, attribute) = key.split('.', 2) + next if object_name != 'article' + next if attribute == 'id' + article_selector = true + end + if article && article_selector + one_has_changed_done = true + end + if article && type == 'update' + one_has_changed_done = true + end + + # check ticket "has changed" options + has_changed_done = true + condition.each do |key, value| + next if value.blank? + next if value['operator'].blank? + next if !value['operator']['has changed'] + + # remove condition item, because it has changed + (object_name, attribute) = key.split('.', 2) + next if object_name != 'ticket' + next if item[:changes].blank? + next if !item[:changes].key?(attribute) + condition.delete(key) + one_has_changed_done = true + end + + # check if we have not matching "has changed" attributes + condition.each_value do |value| + next if value.blank? + next if value['operator'].blank? + next if !value['operator']['has changed'] + has_changed_done = false + break + end + + # check ticket action + if condition['ticket.action'] + next if condition['ticket.action']['operator'] == 'is' && condition['ticket.action']['value'] != type + next if condition['ticket.action']['operator'] != 'is' && condition['ticket.action']['value'] == type + condition.delete('ticket.action') + end + next if !has_changed_done + + # check in min one attribute of condition has changed on update + one_has_changed_condition = false + if type == 'update' + + # verify if ticket condition exists + condition.each_key do |key| + (object_name, attribute) = key.split('.', 2) + next if object_name != 'ticket' + one_has_changed_condition = true + next if item[:changes].blank? + next if !item[:changes].key?(attribute) + one_has_changed_done = true + break + end + next if one_has_changed_condition && !one_has_changed_done + end + + # check if ticket selector is matching + condition['ticket.id'] = { + operator: 'is', + value: ticket.id, + } + next if article_selector && !article + + # check if article selector is matching + if article_selector + condition['article.id'] = { + operator: 'is', + value: article.id, + } + end + + # verify is condition is matching + ticket_count, tickets = Ticket.selectors(condition, 1) + + next if ticket_count.blank? + next if ticket_count.zero? + next if tickets.first.id != ticket.id + user_id = ticket.updated_by_id + if article + user_id = article.updated_by_id + end + + if recursive == false && local_options[:loop_count] > 1 + message = "Do not execute recursive triggers per default until Zammad 3.0. With Zammad 3.0 and higher the following trigger is executed '#{trigger.name}' on Ticket:#{ticket.id}. Please review your current triggers and change them if needed." + logger.info { message } + return [true, message] + end + + local_send_notification = true + if article && send_notification == false && trigger.perform['notification.email'] && trigger.perform['notification.email']['recipient'] + recipient = trigger.perform['notification.email']['recipient'] + local_send_notification = false + local_options[:send_notification] = false + if recipient.include?('ticket_customer') || recipient.include?('article_last_sender') + logger.info { "Skip trigger (#{trigger.name}/#{trigger.id}) because sender do not want to get auto responder for object (Ticket/#{ticket.id}/Article/#{article.id})" } + next + end + end + + if local_options[:trigger_ids][ticket.id].include?(trigger.id) + logger.info { "Skip trigger (#{trigger.name}/#{trigger.id}) because was already executed for this object (Ticket:#{ticket.id}/Loop:#{local_options[:loop_count]})" } + next + end + local_options[:trigger_ids][ticket.id].push trigger.id + logger.info { "Execute trigger (#{trigger.name}/#{trigger.id}) for this object (Ticket:#{ticket.id}/Loop:#{local_options[:loop_count]})" } + + ticket.perform_changes(trigger.perform, 'trigger', item, user_id) + + if recursive == true + Observer::Transaction.commit(local_options) + end + end + end + [true, ticket, local_options] end =begin diff --git a/app/models/transaction.rb b/app/models/transaction.rb index 25b94fa55..bbf0ea4b1 100644 --- a/app/models/transaction.rb +++ b/app/models/transaction.rb @@ -13,10 +13,7 @@ class Transaction if options[:interface_handle] ApplicationHandleInfo.current = original_interface_handle end - Observer::Transaction.commit( - disable_notification: options[:disable_notification], - disable: options[:disable], - ) + Observer::Transaction.commit(options) PushMessages.finish end end diff --git a/app/models/transaction/trigger.rb b/app/models/transaction/trigger.rb index 1eb7c7edd..0cda993b2 100644 --- a/app/models/transaction/trigger.rb +++ b/app/models/transaction/trigger.rb @@ -29,13 +29,6 @@ class Transaction::Trigger return if @item[:object] != 'Ticket' - triggers = if Rails.configuration.db_case_sensitive - ::Trigger.where(active: true).order('LOWER(name)') - else - ::Trigger.where(active: true).order(:name) - end - return if triggers.blank? - ticket = Ticket.lookup(id: @item[:object_id]) return if !ticket if @item[:article_id] @@ -44,103 +37,8 @@ class Transaction::Trigger original_user_id = UserInfo.current_user_id - Transaction.execute(reset_user_id: true, disable: ['Transaction::Trigger', 'Transaction::Notification']) do - triggers.each do |trigger| - condition = trigger.condition + Ticket.perform_triggers(ticket, article, @item, @params) - # check if one article attribute is used - one_has_changed_done = false - article_selector = false - trigger.condition.each_key do |key| - (object_name, attribute) = key.split('.', 2) - next if object_name != 'article' - next if attribute == 'id' - article_selector = true - end - if article && article_selector - one_has_changed_done = true - end - if article && @item[:type] == 'update' - one_has_changed_done = true - end - - # check ticket "has changed" options - has_changed_done = true - condition.each do |key, value| - next if value.blank? - next if value['operator'].blank? - next if !value['operator']['has changed'] - - # remove condition item, because it has changed - (object_name, attribute) = key.split('.', 2) - next if object_name != 'ticket' - next if @item[:changes].blank? - next if !@item[:changes].key?(attribute) - condition.delete(key) - one_has_changed_done = true - end - - # check if we have not matching "has changed" attributes - condition.each_value do |value| - next if value.blank? - next if value['operator'].blank? - next if !value['operator']['has changed'] - has_changed_done = false - break - end - - # check ticket action - if condition['ticket.action'] - next if condition['ticket.action']['operator'] == 'is' && condition['ticket.action']['value'] != @item[:type] - next if condition['ticket.action']['operator'] != 'is' && condition['ticket.action']['value'] == @item[:type] - condition.delete('ticket.action') - end - next if !has_changed_done - - # check in min one attribute of condition has changed on update - one_has_changed_condition = false - if @item[:type] == 'update' - - # verify if ticket condition exists - condition.each_key do |key| - (object_name, attribute) = key.split('.', 2) - next if object_name != 'ticket' - one_has_changed_condition = true - next if @item[:changes].blank? - next if !@item[:changes].key?(attribute) - one_has_changed_done = true - break - end - next if one_has_changed_condition && !one_has_changed_done - end - - # check if ticket selector is matching - condition['ticket.id'] = { - operator: 'is', - value: ticket.id, - } - next if article_selector && !article - - # check if article selector is matching - if article_selector - condition['article.id'] = { - operator: 'is', - value: article.id, - } - end - - # verify is condition is matching - ticket_count, tickets = Ticket.selectors(condition, 1) - next if ticket_count.blank? - next if ticket_count.zero? - next if tickets.first.id != ticket.id - user_id = ticket.updated_by_id - if article - user_id = article.updated_by_id - end - ticket.perform_changes(trigger.perform, 'trigger', @item, user_id) - end - end UserInfo.current_user_id = original_user_id end diff --git a/db/migrate/20180529000001_issue_2035_recursive_ticket_trigger.rb b/db/migrate/20180529000001_issue_2035_recursive_ticket_trigger.rb new file mode 100644 index 000000000..ea4a5e91a --- /dev/null +++ b/db/migrate/20180529000001_issue_2035_recursive_ticket_trigger.rb @@ -0,0 +1,79 @@ +class Issue2035RecursiveTicketTrigger < ActiveRecord::Migration[5.1] + def change + + # return if it's a new setup + return if !Setting.find_by(name: 'system_init_done') + + Setting.create_if_not_exists( + title: 'Recursive Ticket Triggers', + name: 'ticket_trigger_recursive', + area: 'Ticket::Core', + description: 'Activate the recursive processing of ticket triggers.', + options: { + form: [ + { + display: 'Recursive Ticket Triggers', + null: true, + name: 'ticket_trigger_recursive', + tag: 'boolean', + options: { + true => 'yes', + false => 'no', + }, + }, + ], + }, + state: false, + preferences: { + permission: ['admin.ticket'], + hidden: true, + }, + frontend: false + ) + Setting.create_if_not_exists( + title: 'Recursive Ticket Triggers Loop Max.', + name: 'ticket_trigger_recursive_max_loop', + area: 'Ticket::Core', + description: 'Maximum number of recursively executed triggers.', + options: { + form: [ + { + display: 'Recursive Ticket Triggers', + null: true, + name: 'ticket_trigger_recursive_max_loop', + tag: 'select', + options: { + 1 => ' 1', + 2 => ' 2', + 3 => ' 3', + 4 => ' 4', + 5 => ' 5', + 6 => ' 6', + 7 => ' 7', + 8 => ' 8', + 9 => ' 9', + 10 => '10', + 11 => '11', + 12 => '12', + 13 => '13', + 14 => '14', + 15 => '15', + 16 => '16', + 17 => '17', + 18 => '18', + 19 => '19', + 20 => '20', + }, + }, + ], + }, + state: 10, + preferences: { + permission: ['admin.ticket'], + hidden: true, + }, + frontend: false + ) + + end +end diff --git a/db/seeds/settings.rb b/db/seeds/settings.rb index 95f6efa6a..b7275a8b1 100644 --- a/db/seeds/settings.rb +++ b/db/seeds/settings.rb @@ -1978,6 +1978,77 @@ Setting.create_if_not_exists( frontend: false ) +Setting.create_if_not_exists( + title: 'Recursive Ticket Triggers', + name: 'ticket_trigger_recursive', + area: 'Ticket::Core', + description: 'Activate the recursive processing of ticket triggers.', + options: { + form: [ + { + display: 'Recursive Ticket Triggers', + null: true, + name: 'ticket_trigger_recursive', + tag: 'boolean', + options: { + true => 'yes', + false => 'no', + }, + }, + ], + }, + state: false, + preferences: { + permission: ['admin.ticket'], + hidden: true, + }, + frontend: false +) +Setting.create_if_not_exists( + title: 'Recursive Ticket Triggers Loop Max.', + name: 'ticket_trigger_recursive_max_loop', + area: 'Ticket::Core', + description: 'Maximum number of recursively executed triggers.', + options: { + form: [ + { + display: 'Recursive Ticket Triggers', + null: true, + name: 'ticket_trigger_recursive_max_loop', + tag: 'select', + options: { + 1 => ' 1', + 2 => ' 2', + 3 => ' 3', + 4 => ' 4', + 5 => ' 5', + 6 => ' 6', + 7 => ' 7', + 8 => ' 8', + 9 => ' 9', + 10 => '10', + 11 => '11', + 12 => '12', + 13 => '13', + 14 => '14', + 15 => '15', + 16 => '16', + 17 => '17', + 18 => '18', + 19 => '19', + 20 => '20', + }, + }, + ], + }, + state: 10, + preferences: { + permission: ['admin.ticket'], + hidden: true, + }, + frontend: false +) + Setting.create_if_not_exists( title: 'Enable Ticket creation', name: 'customer_ticket_create', diff --git a/test/unit/email_process_auto_response_test.rb b/test/unit/email_process_auto_response_test.rb index c22bf9b84..e24eea9cb 100644 --- a/test/unit/email_process_auto_response_test.rb +++ b/test/unit/email_process_auto_response_test.rb @@ -2,10 +2,10 @@ require 'test_helper' class EmailProcessAutoResponseTest < ActiveSupport::TestCase - test 'process auto reply check' do + test 'process auto reply check - 1' do roles = Role.where(name: 'Agent') - agent1 = User.create_or_update( + agent1 = User.create!( login: 'ticket-auto-responder-agent1@example.com', firstname: 'AutoReponder', lastname: 'Agent1', @@ -18,9 +18,13 @@ class EmailProcessAutoResponseTest < ActiveSupport::TestCase created_by_id: 1, ) - Trigger.create_or_update( - name: 'auto reply', + Trigger.create!( + name: '002 auto reply', condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, 'ticket.state_id' => { 'operator' => 'is', 'value' => Ticket::State.lookup(name: 'new').id.to_s, @@ -37,7 +41,7 @@ class EmailProcessAutoResponseTest < ActiveSupport::TestCase }, 'ticket.tags' => { 'operator' => 'add', - 'value' => 'aa, kk', + 'value' => 'aa, kk, auto-reply', }, }, disable_notification: true, @@ -188,8 +192,8 @@ test" assert_equal(1, article_p.ticket.articles.count) # add an agent notification - Trigger.create_or_update( - name: 'additional agent notification', + Trigger.create!( + name: '001 additional agent notification', condition: { 'ticket.state_id' => { 'operator' => 'is', @@ -207,7 +211,7 @@ test" }, 'ticket.tags' => { 'operator' => 'add', - 'value' => 'aa, kk', + 'value' => 'aa, kk, agent-notification', }, }, disable_notification: true, @@ -226,20 +230,663 @@ Some Text" ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(3, tags.count) assert_equal(2, article_p.ticket.articles.count) article_customer = article_p.ticket.articles.first assert_equal('me@example.com', article_customer.from) assert_equal('customer@example.com', article_customer.to) assert_equal('Customer', article_customer.sender.name) assert_equal('email', article_customer.type.name) - article_notification = article_p.ticket.articles.last + article_notification = article_p.ticket.articles[1] assert_match(/New Ticket add. info/, article_notification.subject) assert_no_match(/me@example.com/, article_notification.to) assert_match(/#{agent1.email}/, article_notification.to) assert_equal('System', article_notification.sender.name) assert_equal('email', article_notification.type.name) - Trigger.destroy_all + Setting.set('ticket_trigger_recursive', true) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(3, tags.count) + assert_equal(2, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles.first + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', false) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert(tags.include?('auto-reply')) + assert_equal(3, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles[0] + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + article_auto_reply = article_p.ticket.articles[2] + assert_match(/Thanks for your inquiry/, article_auto_reply.subject) + assert_match(/me@example.com/, article_auto_reply.to) + assert_equal('System', article_auto_reply.sender.name) + assert_equal('email', article_auto_reply.type.name) + + Setting.set('ticket_trigger_recursive', true) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert(tags.include?('auto-reply')) + assert_equal(3, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles[0] + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + article_auto_reply = article_p.ticket.articles[2] + assert_match(/Thanks for your inquiry/, article_auto_reply.subject) + assert_match(/me@example.com/, article_auto_reply.to) + assert_equal('System', article_auto_reply.sender.name) + assert_equal('email', article_auto_reply.type.name) + + end + + test 'process auto reply check - 2' do + + roles = Role.where(name: 'Agent') + agent1 = User.create!( + login: 'ticket-auto-responder-agent1@example.com', + firstname: 'AutoReponder', + lastname: 'Agent1', + email: 'ticket-auto-responder-agent1@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: Group.all, + updated_by_id: 1, + created_by_id: 1, + ) + + Trigger.create!( + name: '001 auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk, auto-reply', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + assert_equal(2, article_p.ticket.articles.count) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +X-Loop: yes + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + assert_equal(1, article_p.ticket.articles.count) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +Precedence: Bulk + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +Auto-Submitted: auto-generated + +Some Text" + Scheduler.worker(true) + assert_equal(1, article_p.ticket.articles.count) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +X-Auto-Response-Suppress: All + + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + assert_equal(1, article_p.ticket.articles.count) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +List-Unsubscribe: + +Some Text" + + fqdn = Setting.get('fqdn') + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +Message-ID: <1234@#{fqdn}> + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + assert_equal(1, article_p.ticket.articles.count) + + fqdn = Setting.get('fqdn') + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +Message-ID: <1234@not_matching.#{fqdn}> + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + assert_equal(2, article_p.ticket.articles.count) + + email_raw_string = "Return-Path: +X-Original-To: sales@znuny.com +Received: from mail-qk0-f170.example.com (mail-qk0-f170.example.com [209.1.1.1]) + by arber.znuny.com (Postfix) with ESMTPS id C3AED5FE2E + for ; Mon, 22 Aug 2016 19:03:15 +0200 (CEST) +Received: by mail-qk0-f170.example.com with SMTP id t7so87721720qkh.1 + for ; Mon, 22 Aug 2016 10:03:15 -0700 (PDT) +DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=XX.XX; s=example; + h=to:from:date:message-id:subject:mime-version:precedence + :auto-submitted:content-transfer-encoding:content-disposition; + bh=SL5tTVvGdxsKjLic38irxzlP439P3jixJH0QTG1HJ5I=; + b=CIk3PLELgjOCagyiFFbd6rlb8ZRDGYRUrg5Dntxa7e5X+PT4cgL+IE13N9TFkK8ZUJ + GohlaPLGiBymIYLTtYMKUpcf22oiX8ZgGiSu1aEMC1Gsa1ZDf+vpy4kd4+7EecRT3IWF + 4RafQxeaqe67budhQpO1Z6UAel6BdJj0xguKM= +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=1e100.net; s=20130820; + h=x-gm-message-state:to:from:date:message-id:subject:mime-version + :precedence:auto-submitted:content-transfer-encoding + :content-disposition; + bh=SL5tTVvGdxsKjLic38irxzlP439P3jixJH0QTG1HJ5I=; + b=PYULo3xigc4O/cuNZ79OathQ5HDMFWWIwUxz6CHbpXDQR5k3EPy/skJU1992hVz9Rl + xiGwScBCkMqOjlxHjQSWhFJIxNtdvMk4m0bixBZ79IEvRuQa9cEbqjf6efnV58br5ftQ + 2osHrtQczoSqLE/d61/o102RfQ0avVyX8XNJik0iepg8MiCY7LTOE9hrbnuDDLxgQecH + rMEfkR7bafcUj1YEto5Vd7uV11cVZYx8UIQqVAVbfygv8dTSFeOzz3NyM0M41rRexfYH + 79Yi5i7z/Wk6q2427wkJ3FIR1B7VQVQEmcq/Texbch+gAXPGBNPUHdg2WHt7NXGktrHL + d3DA== +X-Gm-Message-State: AE9vXwMCTnihGiG/tc7xNNlhFLcEK6DPp7otypJg5e4alD3xGK2R707BP29druIi/mcdNyaHg1vP5lSZ8EvrwvOF8iA0HNFhECGjBTJ40YrSJAR8E89xVwxFv/er+U3vEpqmPmt+hL4QhxK/+D2gKOcHSxku +X-Received: by 10.1.1.1 with SMTP id 17mr25015996qkf.279.1471885393931; + Mon, 22 Aug 2016 10:03:13 -0700 (PDT) +To: sales@znuny.com +From: \"XXX\" +Date: Mon, 22 Aug 2016 10:03:13 -0700 +Message-ID: +Subject: XX PieroXXway - vacation response RE: Callback Request: XX XX [Ticket#1118974] +MIME-Version: 1.0 +Precedence: bulk +X-Autoreply: yes +Auto-Submitted: auto-replied +Content-Type: text/html; charset=UTF-8 +Content-Transfer-Encoding: quoted-printable +Content-Disposition: inline + +test" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + assert_equal(1, article_p.ticket.articles.count) + + # add an agent notification + Trigger.create!( + name: '002 additional agent notification', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}', + 'recipient' => 'ticket_agents', + 'subject' => 'New Ticket add. info (#{ticket.title})!', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk, agent-notification', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +X-Loop: yes + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(3, tags.count) + assert_equal(2, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles.first + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', true) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(3, tags.count) + assert_equal(2, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles.first + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', false) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert(tags.include?('auto-reply')) + assert_equal(3, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles[0] + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_auto_reply = article_p.ticket.articles[1] + assert_match(/Thanks for your inquiry/, article_auto_reply.subject) + assert_match(/me@example.com/, article_auto_reply.to) + assert_equal('System', article_auto_reply.sender.name) + assert_equal('email', article_auto_reply.type.name) + article_notification = article_p.ticket.articles[2] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', true) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + tags = ticket_p.tag_list + assert_equal('new', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert(tags.include?('auto-reply')) + assert_equal(3, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles[0] + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_auto_reply = article_p.ticket.articles[1] + assert_match(/Thanks for your inquiry/, article_auto_reply.subject) + assert_match(/me@example.com/, article_auto_reply.to) + assert_equal('System', article_auto_reply.sender.name) + assert_equal('email', article_auto_reply.type.name) + article_notification = article_p.ticket.articles[2] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + end + + test 'process auto reply check - recursive' do + + roles = Role.where(name: 'Agent') + agent1 = User.create!( + login: 'ticket-auto-responder-agent1@example.com', + firstname: 'AutoReponder', + lastname: 'Agent1', + email: 'ticket-auto-responder-agent1@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: Group.all, + updated_by_id: 1, + created_by_id: 1, + ) + + Trigger.create!( + name: '001 auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk, auto-reply', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # add an agent notification + Trigger.create!( + name: '002 additional agent notification', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}', + 'recipient' => 'ticket_agents', + 'subject' => 'New Ticket add. info (#{ticket.title})!', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk, agent-notification', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject +X-Loop: yes + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('open', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(3, tags.count) + assert_equal(2, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles.first + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', true) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(false, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('open', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(3, tags.count) + assert_equal(2, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles.first + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', false) + + email_raw_string = "From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text" + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + + tags = ticket_p.tag_list + assert_equal('open', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert_equal(2, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles[0] + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + + Setting.set('ticket_trigger_recursive', true) + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal(true, mail['x-zammad-send-auto-response'.to_sym]) + Scheduler.worker(true) + tags = ticket_p.tag_list + assert_equal('open', ticket_p.state.name) + assert_equal('3 high', ticket_p.priority.name) + assert(tags.include?('aa')) + assert(tags.include?('kk')) + assert(tags.include?('agent-notification')) + assert(tags.include?('auto-reply')) + assert_equal(3, article_p.ticket.articles.count) + article_customer = article_p.ticket.articles[0] + assert_equal('me@example.com', article_customer.from) + assert_equal('customer@example.com', article_customer.to) + assert_equal('Customer', article_customer.sender.name) + assert_equal('email', article_customer.type.name) + article_notification = article_p.ticket.articles[1] + assert_match(/New Ticket add. info/, article_notification.subject) + assert_no_match(/me@example.com/, article_notification.to) + assert_match(/#{agent1.email}/, article_notification.to) + assert_equal('System', article_notification.sender.name) + assert_equal('email', article_notification.type.name) + article_auto_reply = article_p.ticket.articles[2] + assert_match(/Thanks for your inquiry/, article_auto_reply.subject) + assert_match(/me@example.com/, article_auto_reply.to) + assert_equal('System', article_auto_reply.sender.name) + assert_equal('email', article_auto_reply.type.name) end diff --git a/test/unit/ticket_trigger_extended_recursive_disabled_test.rb b/test/unit/ticket_trigger_extended_recursive_disabled_test.rb new file mode 100644 index 000000000..8729166fe --- /dev/null +++ b/test/unit/ticket_trigger_extended_recursive_disabled_test.rb @@ -0,0 +1,677 @@ +require 'test_helper' + +class TicketTriggerExtendedRecursiveDisabledTest < ActiveSupport::TestCase + + setup do + Setting.set('ticket_trigger_recursive', false) + end + + test 'recursive trigger' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('closed', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + end + + test 'recursive trigger - loop test' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set prio to 1 low', + condition: { + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set prio to 3 high', + condition: { + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('2 normal', ticket_p.priority.name) + assert_equal('open', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + end + + test 'recursive trigger - 2 trigger will not trigger next trigger' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set state to open', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('new', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + + end + + test 'recursive trigger - 2 trigger will trigger next trigger - case 1' do + trigger1 = Trigger.create!( + name: '1) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set state to open', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('open', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + + end + + test 'recursive trigger - 2 trigger will trigger next trigger - case 2' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set state to open', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('2 normal', ticket_p.priority.name) + assert_equal('open', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + + end + + test 'trigger based move and verify correct agent notifications' do + + group1 = Group.create!( + name: 'Group 1', + active: true, + email_address: EmailAddress.first, + created_by_id: 1, + updated_by_id: 1, + ) + group2 = Group.create!( + name: 'Group 2', + active: true, + email_address: EmailAddress.first, + created_by_id: 1, + updated_by_id: 1, + ) + group3 = Group.create!( + name: 'Group 3', + active: true, + email_address: EmailAddress.first, + created_by_id: 1, + updated_by_id: 1, + ) + roles = Role.where(name: 'Agent') + user1 = User.create!( + login: 'trigger1@example.org', + firstname: 'trigger1', + lastname: 'trigger1', + email: 'trigger1@example.org', + password: 'some_pass', + active: true, + groups: [group1], + roles: roles, + created_by_id: 1, + updated_by_id: 1, + ) + user2 = User.create!( + login: 'trigger2@example.org', + firstname: 'trigger2', + lastname: 'trigger2', + email: 'trigger2@example.org', + password: 'some_pass', + active: true, + groups: [group2], + roles: roles, + created_by_id: 1, + updated_by_id: 1, + ) + + # trigger, move ticket created in group1 into group3 and then into group2 + trigger1 = Trigger.create_or_update( + name: '1 dispatch', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.group_id' => { + 'operator' => 'is', + 'value' => group3.id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'ticket.group_id' => { + 'value' => group2.id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger2 = Trigger.create_or_update( + name: '2 dispatch', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'ticket.group_id' => { + 'value' => group3.id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: '123', + group: group1, + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1) + + assert_equal(ticket1.title, '123') + assert_equal(ticket1.group.name, group1.name) + assert_equal(ticket1.state.name, 'new') + + article_inbound1 = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + # verfiy if agent1 got no notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user1, 'email'), ticket1.id) + + # verfiy if agent2 got no notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id) + + Observer::Transaction.commit + Scheduler.worker(true) + + ticket1.reload + assert_equal('123', ticket1.title) + assert_equal(group3.name, ticket1.group.name) + assert_equal('new', ticket1.state.name) + assert_equal('2 normal', ticket1.priority.name) + assert_equal(1, ticket1.articles.count) + + # verfiy if agent1 got no notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user1, 'email'), ticket1.id) + + # verfiy if agent2 got notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id) + + end + + test 'recursive trigger loop check' do + trigger0 = Trigger.create!( + name: '000', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger1 = Trigger.create!( + name: '001', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger2 = Trigger.create!( + name: '002', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + group1 = Group.find_by(name: 'Users') + ticket1 = Ticket.create!( + title: '123', + group: group1, + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1) + + assert_equal(ticket1.title, '123') + assert_equal(ticket1.group.name, group1.name) + assert_equal(ticket1.state.name, 'new') + + article_inbound1 = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + Scheduler.worker(true) + + ticket1.reload + assert_equal('123', ticket1.title) + assert_equal('new', ticket1.state.name) + assert_equal('3 high', ticket1.priority.name) + assert_equal(1, ticket1.articles.count) + + end + +end diff --git a/test/unit/ticket_trigger_extended_test.rb b/test/unit/ticket_trigger_extended_test.rb new file mode 100644 index 000000000..a8c99ab1a --- /dev/null +++ b/test/unit/ticket_trigger_extended_test.rb @@ -0,0 +1,718 @@ +require 'test_helper' + +class TicketTriggerExtendedTest < ActiveSupport::TestCase + + setup do + Setting.set('ticket_trigger_recursive', true) + end + + test 'recursive trigger' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('closed', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + end + + test 'recursive trigger - loop test' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set prio to 1 low', + condition: { + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set prio to 3 high', + condition: { + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('2 normal', ticket_p.priority.name) + assert_equal('open', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + end + + test 'recursive trigger - 2 trigger will not trigger next trigger' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set state to open', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('new', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + + end + + test 'recursive trigger - 2 trigger will trigger next trigger - case 1' do + trigger1 = Trigger.create!( + name: '1) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set state to open', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('closed', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + + end + + test 'recursive trigger - 2 trigger will trigger next trigger - case 2' do + trigger1 = Trigger.create!( + name: '1) set prio to 3 high', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create!( + name: '2) set state to closed', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create!( + name: '3) set state to open', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'open').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + email_raw_string = 'From: me@example.com +To: customer@example.com +Subject: some new subject + +Some Text' + + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, email_raw_string) + + assert_equal('some new subject', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('3 high', ticket_p.priority.name) + assert_equal('closed', ticket_p.state.name) + + assert_equal(1, ticket_p.articles.count, 'ticket1.articles verify') + + end + + test 'trigger based move and verify correct agent notifications' do + + group1 = Group.create!( + name: 'Group 1', + active: true, + email_address: EmailAddress.first, + created_by_id: 1, + updated_by_id: 1, + ) + group2 = Group.create!( + name: 'Group 2', + active: true, + email_address: EmailAddress.first, + created_by_id: 1, + updated_by_id: 1, + ) + group3 = Group.create!( + name: 'Group 3', + active: true, + email_address: EmailAddress.first, + created_by_id: 1, + updated_by_id: 1, + ) + roles = Role.where(name: 'Agent') + user1 = User.create!( + login: 'trigger1@example.org', + firstname: 'trigger1', + lastname: 'trigger1', + email: 'trigger1@example.org', + password: 'some_pass', + active: true, + groups: [group1], + roles: roles, + created_by_id: 1, + updated_by_id: 1, + ) + user2 = User.create!( + login: 'trigger2@example.org', + firstname: 'trigger2', + lastname: 'trigger2', + email: 'trigger2@example.org', + password: 'some_pass', + active: true, + groups: [group2], + roles: roles, + created_by_id: 1, + updated_by_id: 1, + ) + + # trigger, move ticket created in group1 into group3 and then into group2 + trigger1 = Trigger.create_or_update( + name: '1 dispatch', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.group_id' => { + 'operator' => 'is', + 'value' => group3.id.to_s, + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'ticket.group_id' => { + 'value' => group2.id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger2 = Trigger.create_or_update( + name: '2 dispatch', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'ticket.group_id' => { + 'value' => group3.id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: '123', + group: group1, + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1) + + assert_equal(ticket1.title, '123') + assert_equal(ticket1.group.name, group1.name) + assert_equal(ticket1.state.name, 'new') + + article_inbound1 = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + # verfiy if agent1 got no notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user1, 'email'), ticket1.id) + + # verfiy if agent2 got no notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id) + + Observer::Transaction.commit + Scheduler.worker(true) + + ticket1.reload + assert_equal('123', ticket1.title) + assert_equal(group2.name, ticket1.group.name) + assert_equal('new', ticket1.state.name) + assert_equal('2 normal', ticket1.priority.name) + assert_equal(1, ticket1.articles.count) + + # verfiy if agent1 got no notifcation + assert_equal(0, NotificationFactory::Mailer.already_sent?(ticket1, user1, 'email'), ticket1.id) + + # verfiy if agent2 got notifcation + assert_equal(1, NotificationFactory::Mailer.already_sent?(ticket1, user2, 'email'), ticket1.id) + + end + + test 'recursive trigger loop check' do + Setting.set('ticket_trigger_recursive_max_loop', 2) + trigger0 = Trigger.create!( + name: '000', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + }, + perform: { + 'ticket.state_id' => { + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger1 = Trigger.create!( + name: '001', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '1 low').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger2 = Trigger.create!( + name: '002', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '2 normal').id.to_s, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + group1 = Group.find_by(name: 'Users') + ticket1 = Ticket.create!( + title: '123', + group: group1, + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1) + + assert_equal(ticket1.title, '123') + assert_equal(ticket1.group.name, group1.name) + assert_equal(ticket1.state.name, 'new') + + article_inbound1 = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + Scheduler.worker(true) + + ticket1.reload + assert_equal('123', ticket1.title) + assert_equal('new', ticket1.state.name) + assert_equal('1 low', ticket1.priority.name) + assert_equal(1, ticket1.articles.count) + + Setting.set('ticket_trigger_recursive_max_loop', 3) + + ticket1 = Ticket.create!( + title: '123', + group: group1, + customer_id: 2, + state: Ticket::State.lookup(name: 'new'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket1) + + assert_equal(ticket1.title, '123') + assert_equal(ticket1.group.name, group1.name) + assert_equal(ticket1.state.name, 'new') + + article_inbound1 = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + Scheduler.worker(true) + + ticket1.reload + assert_equal('123', ticket1.title) + assert_equal('closed', ticket1.state.name) + assert_equal('1 low', ticket1.priority.name) + assert_equal(1, ticket1.articles.count) + + end + +end diff --git a/test/unit/ticket_trigger_recursive_disabled_test.rb b/test/unit/ticket_trigger_recursive_disabled_test.rb new file mode 100644 index 000000000..658a04d65 --- /dev/null +++ b/test/unit/ticket_trigger_recursive_disabled_test.rb @@ -0,0 +1,4293 @@ +require 'test_helper' + +class TicketTriggerRecursiveDisabledTest < ActiveSupport::TestCase + + setup do + Setting.set('ticket_trigger_recursive', false) + end + + test '1 basic' do + trigger1 = Trigger.create_or_update( + name: 'aaa loop check', + condition: { + 'article.subject' => { + 'operator' => 'contains', + 'value' => 'Thanks for your inquiry', + }, + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'should_not_loop', + }, + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create_or_update( + name: 'auto tag 1', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'remove', + 'value' => 'kk', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger4 = Trigger.create_or_update( + name: 'auto tag 2', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'abc', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger5 = Trigger.create_or_update( + name: 'not matching', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + } + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger6 = Trigger.create_or_update( + name: 'zzz last', + condition: { + 'article.subject' => { + 'operator' => 'contains', + 'value' => 'some subject 1234', + }, + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'article_create_trigger', + }, + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry - 1234 check (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk abc], ticket1.tag_list) + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('nicole.braun@zammad.org', article1.to) + assert_match('Thanks for your inquiry (some title äöüß)!', article1.subject) + assert_match('Braun
some <b>title</b>', article1.body) + assert_match('> some message <b>note</b>
> new line', article1.body) + assert_equal('text/html', article1.content_type) + + ticket1.priority = Ticket::Priority.lookup(name: '2 normal') + ticket1.save! + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk abc], ticket1.tag_list) + + ticket1.state = Ticket::State.lookup(name: 'open') + ticket1.save! + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('open', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk abc], ticket1.tag_list) + + ticket1.state = Ticket::State.lookup(name: 'new') + ticket1.save! + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa abc], ticket1.tag_list) + + ticket2 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + state: Ticket::State.lookup(name: 'open'), + priority: Ticket::Priority.lookup(name: '2 normal'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('some title äöüß', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('open', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(0, ticket2.articles.count, 'ticket2.articles verify') + assert_equal([], ticket2.tag_list) + + Observer::Transaction.commit + + ticket2.reload + assert_equal('some title äöüß', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('open', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(0, ticket2.articles.count, 'ticket2.articles verify') + assert_equal([], ticket2.tag_list) + + ticket3 = Ticket.create!( + title: "some title\n äöüß3", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket3, 'ticket3 created') + + Ticket::Article.create!( + ticket_id: ticket3.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('some title äöüß3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(1, ticket3.articles.count, 'ticket3.articles verify') + assert_equal([], ticket3.tag_list) + + Observer::Transaction.commit + + ticket3.reload + assert_equal('some title äöüß3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(3, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa kk abc article_create_trigger], ticket3.tag_list) + article3 = ticket3.articles[1] + assert_match('Zammad ', article3.from) + assert_match('nicole.braun@zammad.org', article3.to) + assert_match('Thanks for your inquiry (some title äöüß3)!', article3.subject) + assert_match('Braun
some <b>title</b>', article3.body) + assert_match('> some message note
> new line', article3.body) + assert_no_match('> some message <b>note</b>
> new line', article3.body) + assert_equal('text/html', article3.content_type) + article3 = ticket3.articles[2] + assert_match('Zammad ', article3.from) + assert_match('nicole.braun@zammad.org', article3.to) + assert_match('Thanks for your inquiry - 1234 check (some title äöüß3)!', article3.subject) + assert_equal('text/html', article3.content_type) + + Ticket::Article.create!( + ticket_id: ticket3.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject - not 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket3.reload + assert_equal('some title äöüß3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(4, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa abc article_create_trigger], ticket3.tag_list) + + Ticket::Article.create!( + ticket_id: ticket3.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject NOT 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket3.reload + assert_equal('some title äöüß3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(5, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa abc article_create_trigger], ticket3.tag_list) + + Ticket::Article.create!( + ticket_id: ticket3.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket3.reload + assert_equal('some title äöüß3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(7, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa abc article_create_trigger], ticket3.tag_list) + end + + test '2 actions - create' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'dasdasdasd', + 'recipient' => 'ticket_customer', + 'subject' => 'asdasdas', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(0, ticket1.articles.count, 'ticket1.articles verify') + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('nicole.braun@zammad.org', article1.to) + assert_match('asdasdas', article1.subject) + assert_match('dasdasdasd', article1.body) + assert_equal('text/html', article1.content_type) + + ticket1.priority = Ticket::Priority.lookup(name: '2 normal') + ticket1.save! + + Observer::Transaction.commit + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + + ticket1.state = Ticket::State.lookup(name: 'open') + ticket1.save! + + Observer::Transaction.commit + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('open', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + + ticket1.state = Ticket::State.lookup(name: 'new') + ticket1.save! + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + end + + test '2 actions - update' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'dasdasdasd', + 'recipient' => 'ticket_customer', + 'subject' => 'asdasdas', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(0, ticket1.articles.count, 'ticket1.articles verify') + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(0, ticket1.articles.count, 'ticket1.articles verify') + + ticket1.priority = Ticket::Priority.lookup(name: '2 normal') + ticket1.save! + + Observer::Transaction.commit + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(0, ticket1.articles.count, 'ticket1.articles verify') + + ticket1.state = Ticket::State.lookup(name: 'open') + ticket1.save! + + Observer::Transaction.commit + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('open', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(0, ticket1.articles.count, 'ticket1.articles verify') + + ticket1.state = Ticket::State.lookup(name: 'new') + ticket1.save! + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + end + + test '3 auto replys' do + roles = Role.where(name: 'Customer') + customer1 = User.create_or_update( + login: 'postmaster@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'postmaster@example.com', + password: 'customerpw', + active: true, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + customer2 = User.create_or_update( + login: 'ticket-auto-reply-customer2@example.com', + firstname: 'Trigger', + lastname: 'Customer2', + email: 'ticket-auto-reply-customer2@example.com', + password: 'customerpw', + active: true, + organization_id: nil, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + + trigger1 = Trigger.create_or_update( + name: 'auto reply - new ticket', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is not', + 'value' => Ticket::State.lookup(name: 'closed').id, + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => [ + Ticket::Article::Type.lookup(name: 'email').id, + Ticket::Article::Type.lookup(name: 'phone').id, + Ticket::Article::Type.lookup(name: 'web').id, + ], + }, + }, + perform: { + 'notification.email' => { + 'body' => '

Your request (Ticket##{ticket.number}) has been received and will be reviewed by our support staff.

+
+

To provide additional information, please reply to this email or click on the following link: +#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +

+
+

Zammad, your customer support system

', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create_or_update( + name: 'auto reply (on follow up of tickets)', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + 'article.sender_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id, + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => [ + Ticket::Article::Type.lookup(name: 'email').id, + Ticket::Article::Type.lookup(name: 'phone').id, + Ticket::Article::Type.lookup(name: 'web').id, + ], + }, + }, + perform: { + 'notification.email' => { + 'body' => '

Your follow up for (#{config.ticket_hook}##{ticket.number}) has been received and will be reviewed by our support staff.

+
+

To provide additional information, please reply to this email or click on the following link: +#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +

+
+

Zammad, your customer support system

', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your follow up (#{ticket.title})', + }, + }, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create_or_update( + name: 'not matching', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'closed').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => '2some text
#{ticket.customer.lastname}
#{ticket.title}', + 'recipient' => 'ticket_customer', + 'subject' => '2Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # process mail without Precedence header + content = IO.binread('test/fixtures/ticket_trigger/mail1.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('aaäöüßad asd', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('new', ticket_p.state.name) + assert_equal(2, ticket_p.articles.count) + article_p = ticket_p.articles.last + assert_match('Thanks for your inquiry (aaäöüßad asd)', article_p.subject) + assert_match('Zammad ', article_p.from) + assert_no_match('config\.', article_p.body) + assert_match('http://zammad.example.com', article_p.body) + assert_no_match('ticket.', article_p.body) + assert_match(ticket_p.number, article_p.body) + assert_equal('text/html', article_p.content_type) + + ticket_p.priority = Ticket::Priority.lookup(name: '2 normal') + ticket_p.save! + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify') + + article_p = Ticket::Article.create!( + ticket_id: ticket_p.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(3, ticket_p.articles.count, 'ticket_p.articles verify') + + article_p = Ticket::Article.create!( + ticket_id: ticket_p.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(4, ticket_p.articles.count, 'ticket_p.articles verify') + + article_p = Ticket::Article.create!( + ticket_id: ticket_p.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(6, ticket_p.articles.count, 'ticket_p.articles verify') + + article_p = ticket_p.articles.last + assert_match('Thanks for your follow up (aaäöüßad asd)', article_p.subject) + assert_match('Zammad ', article_p.from) + assert_no_match('config\.', article_p.body) + assert_match('http://zammad.example.com', article_p.body) + assert_no_match('ticket.', article_p.body) + assert_match(ticket_p.number, article_p.body) + assert_equal('text/html', article_p.content_type) + + ticket_p.state = Ticket::State.lookup(name: 'open') + ticket_p.save! + article_p = Ticket::Article.create!( + ticket_id: ticket_p.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('open', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(8, ticket_p.articles.count, 'ticket_p.articles verify') + + article_p = ticket_p.articles.last + assert_match('Thanks for your follow up (aaäöüßad asd)', article_p.subject) + assert_match('Zammad ', article_p.from) + assert_no_match('config\.', article_p.body) + assert_match('http://zammad.example.com', article_p.body) + assert_no_match('ticket.', article_p.body) + assert_match(ticket_p.number, article_p.body) + assert_equal('text/html', article_p.content_type) + + # process mail without Precedence header + content = IO.binread('test/fixtures/ticket_trigger/mail1.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('new', ticket_p.state.name) + assert_equal(2, ticket_p.articles.count) + + # process mail with Precedence header (no auto response) + content = IO.binread('test/fixtures/ticket_trigger/mail2.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('new', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count) + + # process mail with abuse@ (no auto response) + content = IO.binread('test/fixtures/ticket_trigger/mail3.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('new', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count) + end + + test '4 has changed' do + roles = Role.where(name: 'Customer') + customer1 = User.create_or_update( + login: 'postmaster@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'postmaster@example.com', + password: 'customerpw', + active: true, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + customer2 = User.create_or_update( + login: 'ticket-has-changed-customer2@example.com', + firstname: 'Trigger', + lastname: 'Customer2', + email: 'ticket-has-changed-customer2@example.com', + password: 'customerpw', + active: true, + organization_id: nil, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent1 = User.create_or_update( + login: 'agent-has-changed@example.com', + firstname: 'Has Changed', + lastname: 'Agent1', + email: 'agent-has-changed@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + trigger1 = Trigger.create_or_update( + name: 'owner update - to customer', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + } + }, + perform: { + 'notification.email' => { + 'body' => '

The owner of ticket (Ticket##{ticket.number}) has changed.

+
+

To provide additional information, please reply to this email or click on the following link: +#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +

+
+

Zammad, your customer support system

', + 'recipient' => 'ticket_customer', + 'subject' => 'Owner has changed (#{ticket.title})', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # process mail without Precedence header + content = IO.binread('test/fixtures/ticket_trigger/mail1.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('aaäöüßad asd', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('new', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count) + article_p = ticket_p.articles.last + + Observer::Transaction.commit + + ticket_p.owner = agent1 + ticket_p.save! + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('2 normal', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify') + + #p ticket_p.articles.last.inspect + article_p = ticket_p.articles.last + assert_match('Owner has changed', article_p.subject) + assert_match('Zammad ', article_p.from) + assert_match('martin@example.com', article_p.to) + assert_no_match('config\.', article_p.body) + assert_match('http://zammad.example.com', article_p.body) + assert_no_match('ticket.', article_p.body) + assert_match(ticket_p.number, article_p.body) + assert_equal('text/html', article_p.content_type) + + trigger1 = Trigger.create_or_update( + name: 'owner update - to customer', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + }, + perform: { + 'notification.email' => { + 'body' => '

The owner of ticket (Ticket##{ticket.number}) has changed.

+
+

To provide additional information, please reply to this email or click on the following link: +#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +

+
+

Zammad, your customer support system

', + 'recipient' => 'ticket_customer', + 'subject' => 'Owner has changed (#{ticket.title})', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # process mail without Precedence header + content = IO.binread('test/fixtures/ticket_trigger/mail1.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('aaäöüßad asd', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('new', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count) + article_p = ticket_p.articles.last + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.priority = Ticket::Priority.lookup(name: '1 low') + ticket_p.save! + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.priority = Ticket::Priority.lookup(name: '3 high') + ticket_p.save! + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.owner = agent1 + ticket_p.save! + + Observer::Transaction.commit + + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('3 high', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify') + + #p ticket_p.articles.last.inspect + article_p = ticket_p.articles.last + assert_match('Owner has changed', article_p.subject) + assert_match('Zammad ', article_p.from) + assert_match('martin@example.com', article_p.to) + assert_no_match('config\.', article_p.body) + assert_match('http://zammad.example.com', article_p.body) + assert_no_match('ticket.', article_p.body) + assert_match(ticket_p.number, article_p.body) + assert_equal('text/html', article_p.content_type) + + # should trigger + trigger1 = Trigger.create_or_update( + name: 'owner update - to customer', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.priority_id' => { + 'operator' => 'is', + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.action' => { + 'operator' => 'is not', + 'value' => 'create', + }, + }, + perform: { + 'notification.email' => { + 'body' => '

The owner of ticket (Ticket##{ticket.number}) has changed.

+
+

To provide additional information, please reply to this email or click on the following link: +#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +

+
+

Zammad, your customer support system

', + 'recipient' => 'ticket_customer', + 'subject' => 'Owner has changed (#{ticket.title})', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # process mail without Precedence header + content = IO.binread('test/fixtures/ticket_trigger/mail1.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal('aaäöüßad asd', ticket_p.title) + assert_equal('Users', ticket_p.group.name) + assert_equal('new', ticket_p.state.name) + assert_equal(1, ticket_p.articles.count) + article_p = ticket_p.articles.last + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.priority = Ticket::Priority.lookup(name: '1 low') + ticket_p.save! + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.priority = Ticket::Priority.lookup(name: '3 high') + ticket_p.save! + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.owner = agent1 + ticket_p.save! + + Observer::Transaction.commit + assert_equal('aaäöüßad asd', ticket_p.title, 'ticket_p.title verify') + assert_equal('Users', ticket_p.group.name, 'ticket_p.group verify') + assert_equal('new', ticket_p.state.name, 'ticket_p.state verify') + assert_equal('3 high', ticket_p.priority.name, 'ticket_p.priority verify') + assert_equal(2, ticket_p.articles.count, 'ticket_p.articles verify') + + #p ticket_p.articles.last.inspect + article_p = ticket_p.articles.last + assert_match('Owner has changed', article_p.subject) + assert_match('Zammad ', article_p.from) + assert_match('martin@example.com', article_p.to) + assert_no_match('config\.', article_p.body) + assert_match('http://zammad.example.com', article_p.body) + assert_no_match('ticket.', article_p.body) + assert_match(ticket_p.number, article_p.body) + assert_equal('text/html', article_p.content_type) + + # should not trigger + trigger1 = Trigger.create_or_update( + name: 'owner update - to customer', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + }, + perform: { + 'notification.email' => { + 'body' => '

The owner of ticket (Ticket##{ticket.number}) has changed.

+
+

To provide additional information, please reply to this email or click on the following link: +#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +

+
+

Zammad, your customer support system

', + 'recipient' => 'ticket_customer', + 'subject' => 'Owner has changed (#{ticket.title})', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # process mail without Precedence header + content = IO.binread('test/fixtures/ticket_trigger/mail1.box') + ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + + assert_equal(1, ticket_p.articles.count) + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + + ticket_p.owner = agent1 + ticket_p.save! + + Observer::Transaction.commit + assert_equal(1, ticket_p.articles.count) + end + + test '5 notify owner' do + trigger1 = Trigger.create_or_update( + name: 'aaa notify mail', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.all.pluck(:id), + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_owner', + 'subject' => 'CC NOTE (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + owner: agent, + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + assert_equal(1, ticket1.articles.count) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal(3, ticket1.articles.count) + + trigger1 = Trigger.create_or_update( + name: 'aaa notify mail 2', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.all.pluck(:id), + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_owner', + 'subject' => 'CC NOTE (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal(6, ticket1.articles.count) + end + + test '6 owner auto assignment' do + trigger1 = Trigger.create_or_update( + name: 'aaa auto assignment', + condition: { + 'ticket.owner_id' => { + 'operator' => 'is', + 'pre_condition' => 'not_set', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + }, + perform: { + 'ticket.owner_id' => { + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + #owner: agent, + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + ticket1.owner_id = 1 + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + end + + test '6.1 owner auto assignment based on organization' do + trigger1 = Trigger.create_or_update( + name: 'aaa auto assignment', + condition: { + 'ticket.organization_id' => { + 'operator' => 'is not', + 'pre_condition' => 'not_set', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + }, + perform: { + 'ticket.owner_id' => { + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + roles = Role.where(name: 'Agent') + groups = Group.where(name: 'Users') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer = User.create_or_update( + login: 'customer@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer@example.com', + password: 'customerpw', + vip: true, + active: true, + roles: roles, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + group: Group.lookup(name: 'Users'), + customer: customer, + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + ticket1.update!(customer: User.lookup(email: 'nicole.braun@zammad.org') ) + + UserInfo.current_user_id = agent.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + end + + test '6.2 owner auto assignment based on organization' do + trigger1 = Trigger.create_or_update( + name: 'aaa auto assignment', + condition: { + 'ticket.organization_id' => { + 'operator' => 'is', + 'pre_condition' => 'not_set', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + }, + perform: { + 'ticket.owner_id' => { + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer = User.create_or_update( + login: 'customer@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer@example.com', + password: 'customerpw', + vip: true, + active: true, + roles: roles, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + ticket1.update!(customer: customer ) + + UserInfo.current_user_id = agent.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + end + + test '7 owner auto assignment' do + trigger1 = Trigger.create_or_update( + name: 'aaa auto assignment', + condition: { + 'ticket.owner_id' => { + 'operator' => 'is', + 'pre_condition' => 'not_set', + 'value' => '', + 'value_completion' => '', + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Type.find_by(name: 'note'), + }, + 'article.sender_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Sender.find_by(name: 'Agent'), + }, + }, + perform: { + 'ticket.owner_id' => { + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent1 = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + agent2 = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent2', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + #owner: agent, + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + ticket1.owner_id = 1 + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent2.id + ticket1.owner_id = agent2.id + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent2.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(4, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + end + + test '8 owner auto assignment' do + trigger1 = Trigger.create_or_update( + name: 'aaa auto assignment', + condition: { + 'ticket.owner_id' => { + 'operator' => 'is', + 'pre_condition' => 'not_set', + 'value' => '', + 'value_completion' => '', + }, + 'ticket.priority_id' => { + 'operator' => 'has changed', + 'pre_condition' => '', + 'value' => '2', + 'value_completion' => '', + }, + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + }, + perform: { + 'ticket.owner_id' => { + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + #owner: agent, + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'update', + message_id: 'some@id', + content_type: 'text/html', + body: 'update', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + ) + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + ticket1.priority = Ticket::Priority.find_by(name: '1 low') + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('1 low', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + ticket1.owner_id = 1 + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('1 low', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + ticket1.owner_id = agent.id + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('1 low', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + end + + test '9 vip priority set' do + trigger1 = Trigger.create_or_update( + name: 'aaa vip priority', + condition: { + 'customer.vip' => { + 'operator' => 'is', + 'value' => true, + }, + }, + perform: { + 'ticket.priority_id' => { + 'value' => Ticket::Priority.find_by(name: '3 high').id, + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer = User.create_or_update( + login: 'customer@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer@example.com', + password: 'customerpw', + vip: true, + active: true, + roles: roles, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + owner: agent, + customer: customer, + group: Group.lookup(name: 'Users'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + customer.vip = false + customer.save! + + ticket2 = Ticket.create!( + title: 'test 123', + owner: agent, + customer: customer, + group: Group.lookup(name: 'Users'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket2.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('test 123', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal(agent.id, ticket2.owner_id, 'ticket2.owner_id verify') + assert_equal(customer.id, ticket2.customer_id, 'ticket2.customer_id verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(1, ticket2.articles.count, 'ticket2.articles verify') + assert_equal([], ticket2.tag_list) + + Observer::Transaction.commit + + ticket2.reload + assert_equal('test 123', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal(agent.id, ticket2.owner_id, 'ticket2.owner_id verify') + assert_equal(customer.id, ticket2.customer_id, 'ticket2.customer_id verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(1, ticket2.articles.count, 'ticket2.articles verify') + assert_equal([], ticket2.tag_list) + + end + + test '10 owner auto assignment notify to customer' do + trigger1 = Trigger.create_or_update( + name: 'aaa auto assignment', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + 'pre_condition' => '', + 'value' => '2', + 'value_completion' => '', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'NEW OWNER (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent1 = User.create_or_update( + login: 'agent1@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent1@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + agent2 = User.create_or_update( + login: 'agent2@example.com', + firstname: 'Trigger', + lastname: 'Agent2', + email: 'agent2@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + ticket1.owner_id = agent1.id + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + ticket1.owner_id = agent1.id + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent1.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent1.id + ticket1.owner_id = agent2.id + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent2.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + end + + test '11 notify to customer on public note' do + trigger1 = Trigger.create_or_update( + name: 'aaa notify to customer on public note', + condition: { + 'article.internal' => { + 'operator' => 'is', + 'value' => 'false', + }, + 'article.sender_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Sender.lookup(name: 'Agent').id, + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => [ + Ticket::Article::Type.lookup(name: 'note').id, + ], + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'UPDATE (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer = User.create_or_update( + login: 'customer@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer@example.com', + password: 'customerpw', + vip: true, + active: true, + roles: roles, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + owner: agent, + customer: customer, + group: Group.lookup(name: 'Users'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: true, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(5, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + ticket1.priority = Ticket::Priority.find_by(name: '3 high') + ticket1.save! + article = Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: true, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(6, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + article.internal = false + article.save! + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(6, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: true, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(7, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + end + + test '12 notify on owner change' do + trigger1 = Trigger.create_or_update( + name: 'aaa notify to customer on public note', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + 'pre_condition' => 'current_user.id', + 'value' => '', + 'value_completion' => '', + } + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'UPDATE (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + Trigger.create_or_update( + name: 'auto reply (on new tickets)', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is not', + 'value' => Ticket::State.lookup(name: 'closed').id, + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => [ + Ticket::Article::Type.lookup(name: 'email').id, + Ticket::Article::Type.lookup(name: 'phone').id, + Ticket::Article::Type.lookup(name: 'web').id, + ], + }, + 'article.sender_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id, + }, + }, + perform: { + 'notification.email' => { + 'body' => '
Your request (#{config.ticket_hook}#{ticket.number}) has been received and will be reviewed by our support staff.
+
+
To provide additional information, please reply to this email or click on the following link (for initial login, please request a new password): + #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +
+
+
Your #{config.product_name} Team
+
+
Zammad, your customer support system
', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})', + }, + }, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + Trigger.create_or_update( + name: 'auto reply (on follow up of tickets)', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'update', + }, + 'article.sender_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id, + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => [ + Ticket::Article::Type.lookup(name: 'email').id, + Ticket::Article::Type.lookup(name: 'phone').id, + Ticket::Article::Type.lookup(name: 'web').id, + ], + }, + }, + perform: { + 'notification.email' => { + 'body' => '
Your follow up for (#{config.ticket_hook}#{ticket.number}) has been received and will be reviewed by our support staff.
+
+
To provide additional information, please reply to this email or click on the following link: + #{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id} +
+
+
Your #{config.product_name} Team
+
+
Zammad, your customer support system
', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your follow up (#{ticket.title})', + }, + }, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + groups = Group.where(name: 'Users') + roles = Role.where(name: 'Agent') + agent = User.create_or_update( + login: 'agent@example.com', + firstname: 'Trigger', + lastname: 'Agent1', + email: 'agent@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_by_id: 1, + created_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer = User.create_or_update( + login: 'customer@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer@example.com', + password: 'customerpw', + vip: true, + active: true, + roles: roles, + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 123', + #owner: agent, + customer: customer, + group: Group.lookup(name: 'Users'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'web'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + ticket1.owner_id = agent.id + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'web'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(agent.id, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(5, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + UserInfo.current_user_id = agent.id + ticket1.owner_id = 1 + ticket1.save! + Observer::Transaction.commit + UserInfo.current_user_id = nil + + ticket1.reload + assert_equal('test 123', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal(1, ticket1.owner_id, 'ticket1.owner_id verify') + assert_equal(customer.id, ticket1.customer_id, 'ticket1.customer_id verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(6, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + end + + test '1 empty condition should not create errors' do + assert_raises(Exception) do + trigger_empty = Trigger.create_or_update( + name: 'aaa loop check', + condition: { + 'ticket.number' => { + 'operator' => 'contains', + 'value' => '', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + end + end + + test 'article_last_sender trigger -> reply_to' do + trigger = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient+from@example.com', + reply_to: 'some_recipient+reply_to@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('new', ticket1.state.name, 'ticket1.state new') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + auto_response = ticket1.articles.last + assert_match('Zammad ', auto_response.from) + assert_match('some_recipient+reply_to@example.com', auto_response.to) + end + + test 'article_last_sender trigger -> from' do + trigger = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender+from@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('new', ticket1.state.name, 'ticket1.state new') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + auto_response = ticket1.articles.last + assert_match('Zammad ', auto_response.from) + assert_match('some_sender+from@example.com', auto_response.to) + end + + test 'article_last_sender trigger -> origin_by_id' do + trigger = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer1 = User.create_or_update( + login: 'customer+origin_by_id@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer+origin_by_id@example.com', + password: 'customerpw', + active: true, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + origin_by_id: customer1.id, + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('new', ticket1.state.name, 'ticket1.state new') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + auto_response = ticket1.articles.last + assert_match('Zammad ', auto_response.from) + assert_match('customer+origin_by_id@example.com', auto_response.to) + end + + test 'article_last_sender trigger -> created_by_id' do + trigger = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + roles = Role.where(name: 'Customer') + customer1 = User.create_or_update( + login: 'customer+created_by_id@example.com', + firstname: 'Trigger', + lastname: 'Customer1', + email: 'customer+created_by_id@example.com', + password: 'customerpw', + active: true, + roles: roles, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: customer1.id, + created_by_id: customer1.id, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('new', ticket1.state.name, 'ticket1.state new') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + auto_response = ticket1.articles.last + assert_match('Zammad ', auto_response.from) + assert_match('customer+created_by_id@example.com', auto_response.to) + end + + test 'multiple recipients owner_id, article_last_sender(reply_to) trigger' do + trigger = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => %w[ticket_owner article_last_sender], + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + admin = User.create_or_update( + login: 'admin+owner_recipient@example.com', + firstname: 'Role', + lastname: "Admin#{name}", + email: 'admin+owner_recipient@example.com', + password: 'adminpw', + active: true, + roles: Role.where(name: %w[Admin Agent]), + groups: Group.where(name: 'Users'), + updated_by_id: 1, + created_by_id: 1, + ) + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + owner_id: admin.id, + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient+from@example.com', + reply_to: 'some_recipient+reply_to@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('new', ticket1.state.name, 'ticket1.state new') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + auto_response = ticket1.articles.last + assert_match('Zammad ', auto_response.from) + assert_match('some_recipient+reply_to@example.com', auto_response.to) + assert_match('admin+owner_recipient@example.com', auto_response.to) + end + + test 'article_last_sender trigger -> invalid reply_to' do + trigger = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient+from@example.com', + reply_to: 'Blub blub blub some_recipient+reply_to@example', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('new', ticket1.state.name, 'ticket1.state new') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + end + + test '2 loop check' do + trigger1 = Trigger.create_or_update( + name: 'aaa loop check', + condition: { + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.all.pluck(:id), + }, + 'article.sender_id' => { + 'operator' => 'is', + 'value' => Ticket::Article::Sender.lookup(name: 'Customer').id, + }, + 'article.type_id' => { + 'operator' => 'is', + 'value' => [ + Ticket::Article::Type.lookup(name: 'email').id, + Ticket::Article::Type.lookup(name: 'phone').id, + Ticket::Article::Type.lookup(name: 'web').id, + ], + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'loop try 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + ticket1.reload + assert_equal(1, ticket1.articles.count) + + Observer::Transaction.commit + ticket1.reload + assert_equal(2, ticket1.articles.count) + + ticket1.priority = Ticket::Priority.lookup(name: '2 normal') + ticket1.save! + + Observer::Transaction.commit + ticket1.reload + assert_equal(2, ticket1.articles.count) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(4, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[2].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[3].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(6, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[4].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[5].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(8, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[6].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[7].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(10, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[8].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[9].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(12, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[10].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[11].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(14, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[12].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[13].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(16, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[14].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[15].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(18, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[16].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[17].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(20, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[18].from) + assert_equal('nicole.braun@zammad.org', ticket1.articles[19].to) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(21, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[20].from) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_loop_sender@example.com', + to: 'some_loop_recipient@example.com', + subject: 'some subject 1234', + message_id: 'some@id', + content_type: 'text/html', + body: 'some message note
new line', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + Observer::Transaction.commit + ticket1.reload + assert_equal(22, ticket1.articles.count) + assert_equal('some_loop_sender@example.com', ticket1.articles[21].from) + + end + + test '3 invalid condition' do + trigger1 = Trigger.create_or_update( + name: 'aaa loop check', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'xxx', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + trigger1.update_column(:condition, { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.first_response_at' => { + 'operator' => 'before (absolute)', + 'value' => 'invalid invalid 4', + }, + }) + assert_equal('invalid invalid 4', trigger1.condition['ticket.first_response_at']['value']) + + trigger2 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + } + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + 'ticket.priority_id' => { + 'value' => Ticket::Priority.lookup(name: '3 high').id.to_s, + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk], ticket1.tag_list) + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('nicole.braun@zammad.org', article1.to) + assert_match('Thanks for your inquiry (some title äöüß)!', article1.subject) + assert_match('Braun
some <b>title</b>', article1.body) + assert_match('> some message <b>note</b>
> new line', article1.body) + assert_equal('text/html', article1.content_type) + + end + + test '4 tag based auto response' do + trigger1 = Trigger.create_or_update( + name: '100 add tag if sender 1', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'article.from' => { + 'operator' => 'contains', + 'value' => 'sender1', + }, + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'sender1', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger2 = Trigger.create_or_update( + name: '200 add tag if sender 2', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'article.from' => { + 'operator' => 'contains', + 'value' => 'sender2', + }, + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'sender2', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + trigger3 = Trigger.create_or_update( + name: '300 auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + 'ticket.tags' => { + #'operator' => 'contains one not', + 'operator' => 'contains all not', + 'value' => 'sender1, sender2', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'sender1@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + Observer::Transaction.commit + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[sender1], ticket1.tag_list) + + ticket2 = Ticket.create!( + title: 'test 2', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket2.id, + from: 'sender2@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket2.reload + assert_equal('test 2', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(1, ticket2.articles.count, 'ticket2.articles verify') + assert_equal([], ticket2.tag_list) + + Observer::Transaction.commit + + ticket2.reload + assert_equal('test 2', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(1, ticket2.articles.count, 'ticket2.articles verify') + assert_equal(%w[sender2], ticket2.tag_list) + + ticket3 = Ticket.create!( + title: 'test 3', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + assert(ticket3, 'ticket3 created') + Ticket::Article.create!( + ticket_id: ticket3.id, + from: 'sender0@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: "some message note\nnew line", + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket3.reload + assert_equal('test 3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(1, ticket3.articles.count, 'ticket3.articles verify') + assert_equal([], ticket3.tag_list) + Observer::Transaction.commit + ticket3.reload + assert_equal('test 3', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(2, ticket3.articles.count, 'ticket3.articles verify') + assert_equal([], ticket3.tag_list) + article1 = ticket3.articles.last + + end + + test 'article.body' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + 'article.body' => { + 'operator' => 'contains', + 'value' => 'hello', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note hello ', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk], ticket1.tag_list) + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('nicole.braun@zammad.org', article1.to) + assert_match('Thanks for your inquiry (test 1)!', article1.subject) + assert_match('some message', article1.body) + assert_match('> some message <b>note</b> hello', article1.body) + assert_equal('text/html', article1.content_type) + + ticket2 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket2.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket2.reload + assert_equal('test 1', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(1, ticket2.articles.count, 'ticket2.articles verify') + assert_equal([], ticket2.tag_list) + + Observer::Transaction.commit + + ticket2.reload + assert_equal('test 1', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal(1, ticket2.articles.count, 'ticket2.articles verify') + assert_equal(%w[], ticket2.tag_list) + + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + 'article.body' => { + 'operator' => 'contains not', + 'value' => 'hello', + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + 'ticket.tags' => { + 'operator' => 'add', + 'value' => 'aa, kk', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket3 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket3.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note hello ', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket3.reload + assert_equal('test 1', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal('2 normal', ticket3.priority.name, 'ticket3.priority verify') + assert_equal(1, ticket3.articles.count, 'ticket3.articles verify') + assert_equal([], ticket3.tag_list) + + Observer::Transaction.commit + + ticket3.reload + assert_equal('test 1', ticket3.title, 'ticket3.title verify') + assert_equal('Users', ticket3.group.name, 'ticket3.group verify') + assert_equal('new', ticket3.state.name, 'ticket3.state verify') + assert_equal(1, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[], ticket3.tag_list) + + ticket4 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + Ticket::Article.create!( + ticket_id: ticket4.id, + from: 'some_sender@example.com', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note 2', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Agent'), + type: Ticket::Article::Type.find_by(name: 'note'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket4.reload + assert_equal('test 1', ticket4.title, 'ticket4.title verify') + assert_equal('Users', ticket4.group.name, 'ticket4.group verify') + assert_equal('new', ticket4.state.name, 'ticket4.state verify') + assert_equal('2 normal', ticket4.priority.name, 'ticket4.priority verify') + assert_equal(1, ticket4.articles.count, 'ticket4.articles verify') + assert_equal([], ticket4.tag_list) + + Observer::Transaction.commit + + ticket4.reload + assert_equal('test 1', ticket4.title, 'ticket4.title verify') + assert_equal('Users', ticket4.group.name, 'ticket4.group verify') + assert_equal('new', ticket4.state.name, 'ticket4.state verify') + assert_equal(2, ticket4.articles.count, 'ticket4.articles verify') + assert_equal(%w[aa kk], ticket4.tag_list) + article4 = ticket4.articles.last + assert_match('Zammad ', article4.from) + assert_match('nicole.braun@zammad.org', article4.to) + assert_match('Thanks for your inquiry (test 1)!', article4.subject) + assert_match('some message', article4.body) + assert_match('> some message <b>note</b> 2', article4.body) + assert_equal('text/html', article4.content_type) + + end + + test 'change owner' do + roles = Role.where(name: 'Agent') + groups = Group.where(name: 'Users') + agent1 = User.create_or_update( + login: 'agent-has-changed@example.com', + firstname: 'Has Changed', + lastname: 'Agent1', + email: 'agent-has-changed@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + + agent2 = User.create_or_update( + login: 'agent-has-changed2@example.com', + firstname: 'Has Changed', + lastname: 'Agent2', + email: 'agent-has-changed2@example.com', + password: 'agentpw', + active: true, + roles: roles, + groups: groups, + updated_at: '2015-02-05 16:37:00', + updated_by_id: 1, + created_by_id: 1, + ) + + # multi tag trigger with changed owner + trigger1 = Trigger.create_or_update( + name: 'change owner', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + }, + 'ticket.tags' => { + 'operator' => 'contains one not', + 'value' => 'nosendmail test123' + } + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => '123' + }, + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry - 1234 check (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + # single tag trigger with changed owner + trigger2 = Trigger.create_or_update( + name: 'change owner', + condition: { + 'ticket.owner_id' => { + 'operator' => 'has changed', + }, + 'ticket.tags' => { + 'operator' => 'contains one not', + 'value' => 'nosendmail2', + } + }, + perform: { + 'ticket.tags' => { + 'operator' => 'add', + 'value' => '123' + }, + 'notification.email' => { + 'body' => 'some lala', + 'recipient' => 'ticket_customer', + 'subject' => 'Thanks for your inquiry - 1234 check (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(0, ticket1.articles.count, 'ticket1.articles verify') + assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent') + assert_equal([], ticket1.tag_list, 'ticket1.tag_list') + + ticket2 = Ticket.create!( + title: "some title\n äöüß", + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + + assert_equal('some title äöüß', ticket2.title, 'ticket1.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(0, ticket2.articles.count, 'ticket2.articles verify') + assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent') + assert_equal([], ticket1.tag_list, 'ticket2.tag_list') + + # control test - should pass + # create common object tag + tag_object = Tag::Object.create_or_update(name: 'Ticket') + + # add tag + ticket1.tag_add('thisisthebestjob', agent1.id) + + # change owner + ticket1.owner_id = agent1.id + ticket1.save! + + Observer::Transaction.commit + + # this will add a tag by trigger + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket2.title verify') + assert_equal('Users', ticket1.group.name, 'ticket2.group verify') + assert_equal('new', ticket1.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket2.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket2.articles verify') # articles.count must be 1 if the tag is added + assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent') + assert_equal(%w[thisisthebestjob 123], ticket1.tag_list, 'ticket2.tag_list') + + # add tag nosendmail (to test the bug) + ticket1.tag_add('nosendmail', agent2.id) + + # change owner + ticket1.owner_id = agent2.id + ticket1.save! + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') # articles.count must be 0 if adding tag is skipped + assert_equal('agent-has-changed2@example.com', agent2.login, 'verify agent') + assert_equal(%w[thisisthebestjob 123 nosendmail], ticket1.tag_list, 'ticket1.tag_list') + + # add tag (to test the bug) + ticket1.tag_add('test123', agent2.id) + + # change owner + ticket1.owner_id = agent1.id + ticket1.save! + + Observer::Transaction.commit + + ticket1.reload + assert_equal('some title äöüß', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') # articles.count must be 0 if adding tag is skipped + assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent') + assert_equal(%w[thisisthebestjob 123 nosendmail test123], ticket1.tag_list, 'ticket1.tag_list') + + # add tag single tag 'nosendmail2' (to test the bug) + ticket2.tag_add('nosendmail2', agent1.id) + + # change owner + ticket2.owner_id = agent1.id + ticket2.save! + + Observer::Transaction.commit + + ticket2.reload + assert_equal('some title äöüß', ticket2.title, 'ticket2.title verify') + assert_equal('Users', ticket2.group.name, 'ticket2.group verify') + assert_equal('new', ticket2.state.name, 'ticket2.state verify') + assert_equal('2 normal', ticket2.priority.name, 'ticket2.priority verify') + assert_equal(0, ticket2.articles.count, 'ticket2.articles verify') # articles.count must be 0 if adding tag is skipped + assert_equal('agent-has-changed@example.com', agent1.login, 'verify agent') + assert_equal(['nosendmail2'], ticket2.tag_list, 'ticket2.tag_list') + + end + + test 'trigger auto reply with umlaut in form' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1 = Ticket.create!( + title: 'test 1', + group: Group.lookup(name: 'Users'), + customer: User.lookup(email: 'nicole.braun@zammad.org'), + updated_by_id: 1, + created_by_id: 1, + ) + + Ticket::Article.create!( + ticket_id: ticket1.id, + from: 'Sabine Schütz ', + to: 'some_recipient@example.com', + subject: 'some subject', + message_id: 'some@id', + body: 'some message note hello ', + internal: false, + sender: Ticket::Article::Sender.find_by(name: 'Customer'), + type: Ticket::Article::Type.find_by(name: 'email'), + updated_by_id: 1, + created_by_id: 1, + ) + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') + assert_equal(1, ticket1.articles.count, 'ticket1.articles verify') + assert_equal('Sabine Schütz ', ticket1.articles.first.from, 'ticket1.articles.first.from verify') + assert_equal([], ticket1.tag_list) + + Observer::Transaction.commit + + ticket1.reload + assert_equal('test 1', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('some_sender@example.com', article1.to) + assert_match('Thanks for your inquiry (test 1)!', article1.subject) + assert_match('some message', article1.body) + assert_match('> some message <b>note</b> hello', article1.body) + assert_equal('text/html', article1.content_type) + + end + + test 'trigger auto reply with 2 sender addresses in form' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1, article1, user, mail = Channel::EmailParser.new.process({}, IO.binread('test/fixtures/mail65.box')) + + assert_equal('aaäöüßad asd', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('smith@example.com', article1.to) + assert_match('Thanks for your inquiry (aaäöüßad asd)!', article1.subject) + assert_match('some text

aaäöüßad asd', article1.body) + assert_equal('text/html', article1.content_type) + + end + + test 'make sure attachments should be attached with content id' do + trigger1 = Trigger.create_or_update( + name: 'auto reply', + condition: { + 'ticket.action' => { + 'operator' => 'is', + 'value' => 'create', + }, + 'ticket.state_id' => { + 'operator' => 'is', + 'value' => Ticket::State.lookup(name: 'new').id.to_s, + }, + }, + perform: { + 'notification.email' => { + 'body' => 'some text
#{ticket.customer.lastname}
#{ticket.title}
#{article.body}
', + 'recipient' => 'article_last_sender', + 'subject' => 'Thanks for your inquiry (#{ticket.title})!', + }, + }, + disable_notification: true, + active: true, + created_by_id: 1, + updated_by_id: 1, + ) + + ticket1, article1, user, mail = Channel::EmailParser.new.process({}, IO.binread('test/fixtures/mail65.box')) + + assert_equal('aaäöüßad asd', ticket1.title, 'ticket1.title verify') + assert_equal('Users', ticket1.group.name, 'ticket1.group verify') + assert_equal('new', ticket1.state.name, 'ticket1.state verify') + assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('smith@example.com', article1.to) + assert_match('Thanks for your inquiry (aaäöüßad asd)!', article1.subject) + assert_match(/.+cid:.+?@zammad.example.com.+/, article1.body) + assert_equal(1, article1.attachments.count) + assert_equal('789', article1.attachments[0].size) + assert_equal('text/html', article1.content_type) + end +end diff --git a/test/unit/ticket_trigger_test.rb b/test/unit/ticket_trigger_test.rb index d19fd0da6..c9088db92 100644 --- a/test/unit/ticket_trigger_test.rb +++ b/test/unit/ticket_trigger_test.rb @@ -1,6 +1,11 @@ require 'test_helper' class TicketTriggerTest < ActiveSupport::TestCase + + setup do + Setting.set('ticket_trigger_recursive', true) + end + test '1 basic' do trigger1 = Trigger.create_or_update( name: 'aaa loop check', @@ -18,7 +23,7 @@ class TicketTriggerTest < ActiveSupport::TestCase 'notification.email' => { 'body' => 'some lala', 'recipient' => 'ticket_customer', - 'subject' => 'Thanks for your inquiry - loop check (#{ticket.title})!', + 'subject' => 'Thanks for your inquiry - loop check - only once (#{ticket.title})!', }, }, disable_notification: true, @@ -186,9 +191,10 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket1.group.name, 'ticket1.group verify') assert_equal('new', ticket1.state.name, 'ticket1.state verify') assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') - assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') - assert_equal(%w[aa kk abc], ticket1.tag_list) - article1 = ticket1.articles.last + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk should_not_loop abc], ticket1.tag_list) + + article1 = ticket1.articles.second assert_match('Zammad ', article1.from) assert_match('nicole.braun@zammad.org', article1.to) assert_match('Thanks for your inquiry (some title äöüß)!', article1.subject) @@ -196,6 +202,13 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_match('> some message <b>note</b>
> new line', article1.body) assert_equal('text/html', article1.content_type) + article1 = ticket1.articles.last + assert_match('Zammad ', article1.from) + assert_match('nicole.braun@zammad.org', article1.to) + assert_match('Thanks for your inquiry - loop check - only once (some title äöüß)!', article1.subject) + assert_match('some lala', article1.body) + assert_equal('text/html', article1.content_type) + ticket1.priority = Ticket::Priority.lookup(name: '2 normal') ticket1.save! Observer::Transaction.commit @@ -205,8 +218,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket1.group.name, 'ticket1.group verify') assert_equal('new', ticket1.state.name, 'ticket1.state verify') assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') - assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') - assert_equal(%w[aa kk abc], ticket1.tag_list) + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk should_not_loop abc], ticket1.tag_list) ticket1.state = Ticket::State.lookup(name: 'open') ticket1.save! @@ -218,8 +231,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket1.group.name, 'ticket1.group verify') assert_equal('open', ticket1.state.name, 'ticket1.state verify') assert_equal('2 normal', ticket1.priority.name, 'ticket1.priority verify') - assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') - assert_equal(%w[aa kk abc], ticket1.tag_list) + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa kk should_not_loop abc], ticket1.tag_list) ticket1.state = Ticket::State.lookup(name: 'new') ticket1.save! @@ -231,8 +244,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket1.group.name, 'ticket1.group verify') assert_equal('new', ticket1.state.name, 'ticket1.state verify') assert_equal('3 high', ticket1.priority.name, 'ticket1.priority verify') - assert_equal(2, ticket1.articles.count, 'ticket1.articles verify') - assert_equal(%w[aa abc], ticket1.tag_list) + assert_equal(3, ticket1.articles.count, 'ticket1.articles verify') + assert_equal(%w[aa should_not_loop abc], ticket1.tag_list) ticket2 = Ticket.create!( title: "some title\n äöüß", @@ -299,8 +312,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket3.group.name, 'ticket3.group verify') assert_equal('new', ticket3.state.name, 'ticket3.state verify') assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') - assert_equal(3, ticket3.articles.count, 'ticket3.articles verify') - assert_equal(%w[aa kk abc article_create_trigger], ticket3.tag_list) + assert_equal(4, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa kk should_not_loop abc article_create_trigger], ticket3.tag_list) article3 = ticket3.articles[1] assert_match('Zammad ', article3.from) assert_match('nicole.braun@zammad.org', article3.to) @@ -312,8 +325,14 @@ class TicketTriggerTest < ActiveSupport::TestCase article3 = ticket3.articles[2] assert_match('Zammad ', article3.from) assert_match('nicole.braun@zammad.org', article3.to) - assert_match('Thanks for your inquiry - 1234 check (some title äöüß3)!', article3.subject) + assert_match('Thanks for your inquiry - loop check - only once (some title', article3.subject) + assert_match('some lala', article3.body) assert_equal('text/html', article3.content_type) + article4 = ticket3.articles[3] + assert_match('Zammad ', article4.from) + assert_match('nicole.braun@zammad.org', article4.to) + assert_match('Thanks for your inquiry - 1234 check (some title äöüß3)!', article4.subject) + assert_equal('text/html', article4.content_type) Ticket::Article.create!( ticket_id: ticket3.id, @@ -337,8 +356,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket3.group.name, 'ticket3.group verify') assert_equal('new', ticket3.state.name, 'ticket3.state verify') assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') - assert_equal(4, ticket3.articles.count, 'ticket3.articles verify') - assert_equal(%w[aa abc article_create_trigger], ticket3.tag_list) + assert_equal(5, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa should_not_loop abc article_create_trigger], ticket3.tag_list) Ticket::Article.create!( ticket_id: ticket3.id, @@ -362,8 +381,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket3.group.name, 'ticket3.group verify') assert_equal('new', ticket3.state.name, 'ticket3.state verify') assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') - assert_equal(5, ticket3.articles.count, 'ticket3.articles verify') - assert_equal(%w[aa abc article_create_trigger], ticket3.tag_list) + assert_equal(6, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa should_not_loop abc article_create_trigger], ticket3.tag_list) Ticket::Article.create!( ticket_id: ticket3.id, @@ -387,8 +406,8 @@ class TicketTriggerTest < ActiveSupport::TestCase assert_equal('Users', ticket3.group.name, 'ticket3.group verify') assert_equal('new', ticket3.state.name, 'ticket3.state verify') assert_equal('3 high', ticket3.priority.name, 'ticket3.priority verify') - assert_equal(7, ticket3.articles.count, 'ticket3.articles verify') - assert_equal(%w[aa abc article_create_trigger], ticket3.tag_list) + assert_equal(9, ticket3.articles.count, 'ticket3.articles verify') + assert_equal(%w[aa should_not_loop abc article_create_trigger], ticket3.tag_list) end test '2 actions - create' do @@ -829,24 +848,35 @@ class TicketTriggerTest < ActiveSupport::TestCase # process mail without Precedence header content = IO.binread('test/fixtures/ticket_trigger/mail1.box') - ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + ticket_p1, article_p1, user_p1, mail = Channel::EmailParser.new.process({}, content) - assert_equal('new', ticket_p.state.name) - assert_equal(2, ticket_p.articles.count) + assert_not_equal(ticket_p.id, ticket_p1.id) + assert_equal('new', ticket_p1.state.name) + assert_equal(2, ticket_p1.articles.count) # process mail with Precedence header (no auto response) content = IO.binread('test/fixtures/ticket_trigger/mail2.box') - ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + ticket_p2, article_p2, user_p2, mail = Channel::EmailParser.new.process({}, content) - assert_equal('new', ticket_p.state.name) - assert_equal(1, ticket_p.articles.count) + assert_not_equal(ticket_p.id, ticket_p1.id) + assert_not_equal(ticket_p.id, ticket_p2.id) + assert_not_equal(ticket_p1.id, ticket_p2.id) + assert_equal('new', ticket_p2.state.name) + assert_equal(1, ticket_p2.articles.count) # process mail with abuse@ (no auto response) content = IO.binread('test/fixtures/ticket_trigger/mail3.box') - ticket_p, article_p, user_p, mail = Channel::EmailParser.new.process({}, content) + ticket_p3, article_p3, user_p3, mail = Channel::EmailParser.new.process({}, content) - assert_equal('new', ticket_p.state.name) - assert_equal(1, ticket_p.articles.count) + assert_not_equal(ticket_p.id, ticket_p1.id) + assert_not_equal(ticket_p.id, ticket_p2.id) + assert_not_equal(ticket_p.id, ticket_p3.id) + assert_not_equal(ticket_p1.id, ticket_p2.id) + assert_not_equal(ticket_p1.id, ticket_p3.id) + assert_not_equal(ticket_p2.id, ticket_p1.id) + assert_not_equal(ticket_p2.id, ticket_p3.id) + assert_equal('new', ticket_p3.state.name) + assert_equal(1, ticket_p3.articles.count) end test '4 has changed' do