fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / ticket / merge.t
1
2 use strict;
3 use warnings;
4
5
6 use RT;
7 use RT::Test tests => '44';
8
9
10 # validate that when merging two tickets, the comments from both tickets
11 # are integrated into the new ticket
12 {
13     my $queue = RT::Queue->new(RT->SystemUser);
14     my ($id,$msg) = $queue->Create(Name => 'MergeTest-'.rand(25));
15     ok ($id,$msg);
16
17     my $t1 = RT::Ticket->new(RT->SystemUser);
18     my ($tid,$transid, $t1msg) =$t1->Create(
19         Queue => $queue->Name,
20         Subject => 'Merge test. orig',
21     );
22     ok ($tid, $t1msg);
23     ($id, $msg) = $t1->Comment(Content => 'This is a Comment on the original');
24     ok($id,$msg);
25
26     my $txns = $t1->Transactions;
27     my $Comments = 0;
28     while (my $txn = $txns->Next) {
29     $Comments++ if ($txn->Type eq 'Comment');
30     }
31     is($Comments,1, "our first ticket has only one Comment");
32
33     my $t2 = RT::Ticket->new(RT->SystemUser);
34     my ($t2id,$t2transid, $t2msg) =$t2->Create ( Queue => $queue->Name, Subject => 'Merge test. duplicate');
35     ok ($t2id, $t2msg);
36
37
38
39     ($id, $msg) = $t2->Comment(Content => 'This is a commet on the duplicate');
40     ok($id,$msg);
41
42
43     $txns = $t2->Transactions;
44      $Comments = 0;
45     while (my $txn = $txns->Next) {
46         $Comments++ if ($txn->Type eq 'Comment');
47     }
48     is($Comments,1, "our second ticket has only one Comment");
49
50     ($id, $msg) = $t1->Comment(Content => 'This is a second  Comment on the original');
51     ok($id,$msg);
52
53     $txns = $t1->Transactions;
54     $Comments = 0;
55     while (my $txn = $txns->Next) {
56         $Comments++ if ($txn->Type eq 'Comment');
57     }
58     is($Comments,2, "our first ticket now has two Comments");
59
60     ($id,$msg) = $t2->MergeInto($t1->id);
61
62     ok($id,$msg);
63     $txns = $t1->Transactions;
64     $Comments = 0;
65     while (my $txn = $txns->Next) {
66         $Comments++ if ($txn->Type eq 'Comment');
67     }
68     is($Comments,3, "our first ticket now has three Comments - we merged safely");
69 }
70
71 # when you try to merge duplicate links on postgres, eveyrything goes to hell due to referential integrity constraints.
72 {
73     my $t = RT::Ticket->new(RT->SystemUser);
74     $t->Create(Subject => 'Main', Queue => 'general');
75
76     ok ($t->id);
77     my $t2 = RT::Ticket->new(RT->SystemUser);
78     $t2->Create(Subject => 'Second', Queue => 'general');
79     ok ($t2->id);
80
81     my $t3 = RT::Ticket->new(RT->SystemUser);
82     $t3->Create(Subject => 'Third', Queue => 'general');
83
84     ok ($t3->id);
85
86     my ($id,$val);
87     ($id,$val) = $t->AddLink(Type => 'DependsOn', Target => $t3->id);
88     ok($id,$val);
89     ($id,$val) = $t2->AddLink(Type => 'DependsOn', Target => $t3->id);
90     ok($id,$val);
91
92     ($id,$val) = $t->MergeInto($t2->id);
93     ok($id,$val);
94 }
95
96 my $user = RT::Test->load_or_create_user(
97     Name => 'a user', Password => 'password',
98 );
99 ok $user && $user->id, 'loaded or created user';
100
101 # check rights
102 {
103     RT::Test->set_rights(
104         { Principal => 'Everyone', Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket TakeTicket)] },
105         { Principal => 'Owner',    Right => [qw(ModifyTicket)] },
106     );
107
108     my $t = RT::Ticket->new(RT::CurrentUser->new($user));
109     $t->Create(Subject => 'Main', Queue => 'general');
110     ok ($t->id, "Created ticket");
111
112     my $t2 = RT::Ticket->new(RT::CurrentUser->new($user));
113     $t2->Create(Subject => 'Second', Queue => 'general');
114     ok ($t2->id, "Created ticket");
115
116     foreach my $ticket ( $t, $t2 ) {
117         ok( !$ticket->CurrentUserHasRight('ModifyTicket'), "can not modify" );
118     }
119
120     my ($status,$msg) = $t->MergeInto($t2->id);
121     ok(!$status, "Can not merge: $msg");
122     
123     ($status, $msg) = $t->SetOwner( $user->id );
124     ok( $status, "User took ticket");
125     ok( $t->CurrentUserHasRight('ModifyTicket'), "can modify after take" );
126
127     ($status,$msg) = $t->MergeInto($t2->id);
128     ok(!$status, "Can not merge: $msg");
129
130     ($status, $msg) = $t2->SetOwner( $user->id );
131     ok( $status, "User took ticket");
132     ok( $t2->CurrentUserHasRight('ModifyTicket'), "can modify after take" );
133
134     ($status,$msg) = $t->MergeInto($t2->id);
135     ok($status, "Merged tickets: $msg");
136 }
137
138 # check Time* fields after merge
139 {
140     my @tickets;
141     my @values = (
142         { Worked => 11, Estimated => 17, Left => 6 },
143         { Worked => 7, Estimated => 12, Left => 5 },
144     );
145
146     for my $i (0 .. 1) {
147         my $t = RT::Ticket->new(RT->SystemUser);
148         $t->Create( Queue => 'general');
149         ok ($t->id);
150         push @tickets, $t;
151
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";
157         }
158     }
159
160     my ($status, $msg) = $tickets[1]->MergeInto($tickets[0]->id);
161     ok($status,$msg);
162
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";
167         my $expected = 0;
168         $expected += $_->{ $field } foreach @values;
169         is $t->$method, $expected, "correct value";
170
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;
176         }
177         is $from_history, $expected, "history is correct";
178     }
179 }