diff --git a/app/models/ticket.rb b/app/models/ticket.rb index f529c3c93..c29bae12f 100644 --- a/app/models/ticket.rb +++ b/app/models/ticket.rb @@ -298,6 +298,16 @@ returns # add history to both + # reassign links to the new ticket + Link.where( + link_object_source_id: Link::Object.find_by(name: 'Ticket').id, + link_object_source_value: id, + ).update_all(link_object_source_value: data[:ticket_id]) + Link.where( + link_object_target_id: Link::Object.find_by(name: 'Ticket').id, + link_object_target_value: id, + ).update_all(link_object_target_value: data[:ticket_id]) + # link tickets Link.add( link_type: 'parent', diff --git a/spec/factories/link.rb b/spec/factories/link.rb new file mode 100644 index 000000000..a9fb7195a --- /dev/null +++ b/spec/factories/link.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :link do + link_type_id { Link::Type.find_by(name: 'normal').id } + link_object_source_id { Link::Object.find_by(name: 'Ticket').id } + link_object_target_id { Link::Object.find_by(name: 'Ticket').id } + end +end diff --git a/spec/factories/ticket.rb b/spec/factories/ticket.rb new file mode 100644 index 000000000..a9161d77f --- /dev/null +++ b/spec/factories/ticket.rb @@ -0,0 +1,11 @@ +FactoryGirl.define do + factory :ticket do + title 'Test Ticket' + group { Group.lookup(name: 'Users') } + customer { FactoryGirl.create(:customer_user) } + state { Ticket::State.lookup(name: 'new') } + priority { Ticket::Priority.lookup(name: '2 normal') } + updated_by_id 1 + created_by_id 1 + end +end diff --git a/spec/factories/user.rb b/spec/factories/user.rb index 599125ec3..53becc371 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -18,6 +18,10 @@ FactoryGirl.define do created_by_id 1 end + factory :customer_user, parent: :user do + role_ids { Role.signup_role_ids.sort } + end + factory :user_login_failed, parent: :user do login_failed { (Setting.get('password_max_login_failed').to_i || 10) + 1 } end diff --git a/spec/models/ticket_spec.rb b/spec/models/ticket_spec.rb new file mode 100644 index 000000000..0c3cf3006 --- /dev/null +++ b/spec/models/ticket_spec.rb @@ -0,0 +1,35 @@ +require 'rails_helper' + +RSpec.describe Ticket do + + describe '.merge_to' do + + it 'reassigns all links to the target ticket after merge' do + source_ticket = create(:ticket) + target_ticket = create(:ticket) + + important_ticket1 = create(:ticket) + important_ticket2 = create(:ticket) + important_ticket3 = create(:ticket) + + create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket1.id) + create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket2.id) + create(:link, link_object_source_value: source_ticket.id, link_object_target_value: important_ticket3.id) + + source_ticket.merge_to( + ticket_id: target_ticket.id, + user_id: 1, + ) + + links = Link.list( + link_object: 'Ticket', + link_object_value: target_ticket.id, + ) + + expected_ticket_ids = [source_ticket.id, important_ticket1.id, important_ticket2.id, important_ticket3.id ] + check_ticket_ids = links.collect { |link| link['link_object_value'] } + + expect(check_ticket_ids).to match_array(expected_ticket_ids) + end + end +end