7 use RT::Test tests => '44';
10 # validate that when merging two tickets, the comments from both tickets
11 # are integrated into the new ticket
13 my $queue = RT::Queue->new(RT->SystemUser);
14 my ($id,$msg) = $queue->Create(Name => 'MergeTest-'.rand(25));
17 my $t1 = RT::Ticket->new(RT->SystemUser);
18 my ($tid,$transid, $t1msg) =$t1->Create(
19 Queue => $queue->Name,
20 Subject => 'Merge test. orig',
23 ($id, $msg) = $t1->Comment(Content => 'This is a Comment on the original');
26 my $txns = $t1->Transactions;
28 while (my $txn = $txns->Next) {
29 $Comments++ if ($txn->Type eq 'Comment');
31 is($Comments,1, "our first ticket has only one Comment");
33 my $t2 = RT::Ticket->new(RT->SystemUser);
34 my ($t2id,$t2transid, $t2msg) =$t2->Create ( Queue => $queue->Name, Subject => 'Merge test. duplicate');
39 ($id, $msg) = $t2->Comment(Content => 'This is a commet on the duplicate');
43 $txns = $t2->Transactions;
45 while (my $txn = $txns->Next) {
46 $Comments++ if ($txn->Type eq 'Comment');
48 is($Comments,1, "our second ticket has only one Comment");
50 ($id, $msg) = $t1->Comment(Content => 'This is a second Comment on the original');
53 $txns = $t1->Transactions;
55 while (my $txn = $txns->Next) {
56 $Comments++ if ($txn->Type eq 'Comment');
58 is($Comments,2, "our first ticket now has two Comments");
60 ($id,$msg) = $t2->MergeInto($t1->id);
63 $txns = $t1->Transactions;
65 while (my $txn = $txns->Next) {
66 $Comments++ if ($txn->Type eq 'Comment');
68 is($Comments,3, "our first ticket now has three Comments - we merged safely");
71 # when you try to merge duplicate links on postgres, eveyrything goes to hell due to referential integrity constraints.
73 my $t = RT::Ticket->new(RT->SystemUser);
74 $t->Create(Subject => 'Main', Queue => 'general');
77 my $t2 = RT::Ticket->new(RT->SystemUser);
78 $t2->Create(Subject => 'Second', Queue => 'general');
81 my $t3 = RT::Ticket->new(RT->SystemUser);
82 $t3->Create(Subject => 'Third', Queue => 'general');
87 ($id,$val) = $t->AddLink(Type => 'DependsOn', Target => $t3->id);
89 ($id,$val) = $t2->AddLink(Type => 'DependsOn', Target => $t3->id);
92 ($id,$val) = $t->MergeInto($t2->id);
96 my $user = RT::Test->load_or_create_user(
97 Name => 'a user', Password => 'password',
99 ok $user && $user->id, 'loaded or created user';
103 RT::Test->set_rights(
104 { Principal => 'Everyone', Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket TakeTicket)] },
105 { Principal => 'Owner', Right => [qw(ModifyTicket)] },
108 my $t = RT::Ticket->new(RT::CurrentUser->new($user));
109 $t->Create(Subject => 'Main', Queue => 'general');
110 ok ($t->id, "Created ticket");
112 my $t2 = RT::Ticket->new(RT::CurrentUser->new($user));
113 $t2->Create(Subject => 'Second', Queue => 'general');
114 ok ($t2->id, "Created ticket");
116 foreach my $ticket ( $t, $t2 ) {
117 ok( !$ticket->CurrentUserHasRight('ModifyTicket'), "can not modify" );
120 my ($status,$msg) = $t->MergeInto($t2->id);
121 ok(!$status, "Can not merge: $msg");
123 ($status, $msg) = $t->SetOwner( $user->id );
124 ok( $status, "User took ticket");
125 ok( $t->CurrentUserHasRight('ModifyTicket'), "can modify after take" );
127 ($status,$msg) = $t->MergeInto($t2->id);
128 ok(!$status, "Can not merge: $msg");
130 ($status, $msg) = $t2->SetOwner( $user->id );
131 ok( $status, "User took ticket");
132 ok( $t2->CurrentUserHasRight('ModifyTicket'), "can modify after take" );
134 ($status,$msg) = $t->MergeInto($t2->id);
135 ok($status, "Merged tickets: $msg");
138 # check Time* fields after merge
142 { Worked => 11, Estimated => 17, Left => 6 },
143 { Worked => 7, Estimated => 12, Left => 5 },
147 my $t = RT::Ticket->new(RT->SystemUser);
148 $t->Create( Queue => 'general');
152 foreach my $field ( keys %{ $values[ $i ] } ) {
153 my $method = "SetTime$field";
154 my ($status, $msg) = $t->$method( $values[ $i ]{ $field } );
155 ok $status, "changed $field on the ticket"
156 or diag "error: $msg";
160 my ($status, $msg) = $tickets[1]->MergeInto($tickets[0]->id);
163 my $t = RT::Ticket->new(RT->SystemUser);
164 $t->Load( $tickets[0]->id );
165 foreach my $field ( keys %{ $values[0] } ) {
166 my $method = "Time$field";
168 $expected += $_->{ $field } foreach @values;
169 is $t->$method, $expected, "correct value";
171 my $from_history = 0;
172 my $txns = $t->Transactions;
173 while ( my $txn = $txns->Next ) {
174 next unless $txn->Type eq 'Set' && $txn->Field eq $method;
175 $from_history += $txn->NewValue - $txn->OldValue;
177 is $from_history, $expected, "history is correct";