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