Added new ref touch and tests.

This commit is contained in:
Martin Edenhofer 2015-03-08 22:33:17 +01:00
parent cbfbc225fa
commit 6db57a6509
7 changed files with 365 additions and 1 deletions

View file

@ -0,0 +1,31 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::Organization::RefObjectTouch < ActiveRecord::Observer
observe 'organization'
def after_create(record)
ref_object_touch(record)
end
def after_update(record)
ref_object_touch(record)
end
def after_destroy(record)
ref_object_touch(record)
end
def ref_object_touch(record)
# return if we run import mode
return if Setting.get('import_mode')
# touch organizations tickets
Ticket.select('id').where( :organization_id => record.id ).each {|ticket|
ticket.touch
}
# touch current members
record.member_ids.uniq.each {|user_id|
User.find(user_id).touch
}
end
end

View file

@ -17,9 +17,29 @@ class Observer::Ticket::RefObjectTouch < ActiveRecord::Observer
# return if we run import mode
return if Setting.get('import_mode')
# touch old customer if changed
cutomer_id_changed = record.changes['customer_id']
if cutomer_id_changed && cutomer_id_changed[0] != cutomer_id_changed[1]
if cutomer_id_changed[0]
User.find( cutomer_id_changed[0] ).touch
end
end
# touch new/current customer
if record.customer
record.customer.touch
end
# touch old organization if changed
organization_id_changed = record.changes['organization_id']
if organization_id_changed && organization_id_changed[0] != organization_id_changed[1]
if organization_id_changed[0]
Organization.find( organization_id_changed[0] ).touch
end
end
# touch new/current organization
if record.organization
record.organization.touch
end

View file

@ -0,0 +1,60 @@
# Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
class Observer::User::RefObjectTouch < ActiveRecord::Observer
observe 'user'
def after_create(record)
ref_object_touch(record)
end
def after_update(record)
ref_object_touch(record)
end
def after_destroy(record)
ref_object_touch(record)
end
def ref_object_touch(record)
# return if we run import mode
return if Setting.get('import_mode')
# touch customers tickets
Ticket.select('id').where( :customer_id => record.id ).each {|ticket|
ticket.touch
}
# touch old organization if changed
member_ids = []
organization_id_changed = record.changes['organization_id']
if organization_id_changed && organization_id_changed[0] != organization_id_changed[1]
if organization_id_changed[0]
organization = Organization.find( organization_id_changed[0] )
organization.touch
member_ids = organization.member_ids
# touch customers tickets
Ticket.select('id').where( :organization_id => organization.id ).each {|ticket|
ticket.touch
}
end
end
# touch new/current organization
if record.organization
record.organization.touch
member_ids += record.organization.member_ids
# touch customers tickets
Ticket.select('id').where( :organization_id => record.organization_id ).each {|ticket|
ticket.touch
}
end
# touch old/current customer
member_ids.uniq.each {|user_id|
if user_id != record.id
User.find(user_id).touch
end
}
end
end

View file

@ -42,7 +42,9 @@ module Zammad
'observer::_ticket::_escalation_calculation',
'observer::_ticket::_ref_object_touch',
'observer::_tag::_ticket_history',
'observer::_user::_geo'
'observer::_user::_ref_object_touch',
'observer::_user::_geo',
'observer::_organization::_ref_object_touch'
# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.

View file

@ -0,0 +1,128 @@
# encoding: utf-8
require 'test_helper'
class OrganizationRefObjectTouchTest < ActiveSupport::TestCase
# create base
groups = Group.where( :name => 'Users' )
roles = Role.where( :name => 'Agent' )
agent1 = User.create_or_update(
:login => 'organization-ref-object-update-agent1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:email => 'organization-ref-object-update-agent1@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,
)
roles = Role.where( :name => 'Customer' )
organization1 = Organization.create_if_not_exists(
:name => 'Ref Object Update Org 1',
:updated_at => '2015-02-05 16:37:00',
:updated_by_id => 1,
:created_by_id => 1,
)
organization2 = Organization.create_if_not_exists(
:name => 'Ref Object Update Org 2',
:updated_at => '2015-02-05 16:37:00',
:updated_by_id => 1,
:created_by_id => 1,
)
customer1 = User.create_or_update(
:login => 'organization-ref-object-update-customer1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:email => 'organization-ref-object-update-customer1@example.com',
:password => 'customerpw',
:active => true,
:organization_id => organization1.id,
:roles => roles,
:updated_at => '2015-02-05 16:37:00',
:updated_by_id => 1,
:created_by_id => 1,
)
customer2 = User.create_or_update(
:login => 'organization-ref-object-update-customer2@example.com',
:firstname => 'Notification',
:lastname => 'Agent2',
:email => 'organization-ref-object-update-customer2@example.com',
:password => 'customerpw',
:active => true,
:organization_id => organization2.id,
:roles => roles,
:updated_at => '2015-02-05 16:37:00',
:updated_by_id => 1,
:created_by_id => 1,
)
test 'a - check if ticket and customer has been updated' do
ticket = Ticket.create(
:title => "some title1\n äöüß",
:group => Group.lookup( :name => 'Users'),
:customer_id => customer1.id,
:owner_id => agent1.id,
:state => Ticket::State.lookup( :name => 'new' ),
:priority => Ticket::Priority.lookup( :name => '2 normal' ),
:updated_by_id => 1,
:created_by_id => 1,
)
assert( ticket, "ticket created" )
assert_equal( ticket.customer.id, customer1.id )
assert_equal( ticket.organization.id, organization1.id )
sleep 5
organization1.name = 'Ref Object Update Org 1/1'
organization1.save
# check if ticket and customer has been touched
ticket = Ticket.find(ticket.id)
if ticket.updated_at > 2.second.ago
assert( true, "ticket.updated_at has been updated" )
else
assert( false, "ticket.updated_at has not been updated" )
end
customer1 = User.find(customer1.id)
if customer1.updated_at > 2.second.ago
assert( true, "customer1.updated_at has been updated" )
else
assert( false, "customer1.updated_at has not been updated" )
end
sleep 4
customer2.organization_id = organization1.id
customer2.save
# check if customer1, ticket and organization has been touched
customer1 = User.find(customer1.id)
if customer1.updated_at > 2.second.ago
assert( true, "customer1.updated_at has been updated" )
else
assert( false, "customer1.updated_at has not been updated" )
end
ticket = Ticket.find(ticket.id)
if ticket.updated_at > 2.second.ago
assert( true, "ticket.updated_at has been updated" )
else
assert( false, "ticket.updated_at has not been updated" )
end
organization1 = Organization.find(organization1.id)
if organization1.updated_at > 2.second.ago
assert( true, "organization1.updated_at has been updated" )
else
assert( false, "organization1.updated_at has not been updated" )
end
delete = ticket.destroy
assert( delete, "ticket destroy" )
end
end

View file

@ -0,0 +1,123 @@
# encoding: utf-8
require 'test_helper'
class UserRefObjectTouchTest < ActiveSupport::TestCase
# create base
groups = Group.where( :name => 'Users' )
roles = Role.where( :name => 'Agent' )
agent1 = User.create_or_update(
:login => 'user-ref-object-update-agent1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:email => 'user-ref-object-update-agent1@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,
)
roles = Role.where( :name => 'Customer' )
organization1 = Organization.create_if_not_exists(
:name => 'Ref Object Update Org',
:updated_at => '2015-02-05 16:37:00',
:updated_by_id => 1,
:created_by_id => 1,
)
customer1 = User.create_or_update(
:login => 'user-ref-object-update-customer1@example.com',
:firstname => 'Notification',
:lastname => 'Agent1',
:email => 'user-ref-object-update-customer1@example.com',
:password => 'customerpw',
:active => true,
:organization_id => organization1.id,
:roles => roles,
:updated_at => '2015-02-05 16:37:00',
:updated_by_id => 1,
:created_by_id => 1,
)
customer2 = User.create_or_update(
:login => 'user-ref-object-update-customer2@example.com',
:firstname => 'Notification',
:lastname => 'Agent2',
:email => 'user-ref-object-update-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,
)
test 'a - check if ticket and organization has been updated' do
ticket = Ticket.create(
:title => "some title1\n äöüß",
:group => Group.lookup( :name => 'Users'),
:customer_id => customer1.id,
:owner_id => agent1.id,
:state => Ticket::State.lookup( :name => 'new' ),
:priority => Ticket::Priority.lookup( :name => '2 normal' ),
:updated_by_id => 1,
:created_by_id => 1,
)
assert( ticket, "ticket created" )
assert_equal( ticket.customer.id, customer1.id )
assert_equal( ticket.organization.id, organization1.id )
sleep 5
customer1.firstname = 'firstname customer1'
customer1.save
# check if ticket and organization has been touched
ticket = Ticket.find(ticket.id)
if ticket.updated_at > 2.second.ago
assert( true, "ticket.updated_at has been updated" )
else
assert( false, "ticket.updated_at has not been updated" )
end
organization1 = Organization.find(organization1.id)
if organization1.updated_at > 2.second.ago
assert( true, "organization1.updated_at has been updated" )
else
assert( false, "organization1.updated_at has not been updated" )
end
sleep 4
ticket.customer_id = customer2.id
ticket.save
# check if customer1, customer2 and organization has been touched
customer1 = User.find(customer1.id)
if customer1.updated_at > 2.second.ago
assert( true, "customer1.updated_at has been updated" )
else
assert( false, "customer1.updated_at has not been updated" )
end
customer2 = User.find(customer2.id)
if customer2.updated_at > 2.second.ago
assert( true, "customer2.updated_at has been updated" )
else
assert( false, "customer2.updated_at has not been updated" )
end
organization1 = Organization.find(organization1.id)
if organization1.updated_at > 2.second.ago
assert( true, "organization1.updated_at has been updated" )
else
assert( false, "organization1.updated_at has not been updated" )
end
delete = ticket.destroy
assert( delete, "ticket destroy" )
end
end