summaryrefslogtreecommitdiff
path: root/rt/t/ticket/merge.t
blob: 7c72fe08f074fa4b74dd43b5be9416ca559b91ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/perl

use strict;
use warnings;


use RT;
use RT::Test tests => '29';


# validate that when merging two tickets, the comments from both tickets
# are integrated into the new ticket
{
    my $queue = RT::Queue->new($RT::SystemUser);
    my ($id,$msg) = $queue->Create(Name => 'MergeTest-'.rand(25));
    ok ($id,$msg);

    my $t1 = RT::Ticket->new($RT::SystemUser);
    my ($tid,$transid, $t1msg) =$t1->Create(
        Queue => $queue->Name,
        Subject => 'Merge test. orig',
    );
    ok ($tid, $t1msg);
    ($id, $msg) = $t1->Comment(Content => 'This is a Comment on the original');
    ok($id,$msg);

    my $txns = $t1->Transactions;
    my $Comments = 0;
    while (my $txn = $txns->Next) {
    $Comments++ if ($txn->Type eq 'Comment');
    }
    is($Comments,1, "our first ticket has only one Comment");

    my $t2 = RT::Ticket->new($RT::SystemUser);
    my ($t2id,$t2transid, $t2msg) =$t2->Create ( Queue => $queue->Name, Subject => 'Merge test. duplicate');
    ok ($t2id, $t2msg);



    ($id, $msg) = $t2->Comment(Content => 'This is a commet on the duplicate');
    ok($id,$msg);


    $txns = $t2->Transactions;
     $Comments = 0;
    while (my $txn = $txns->Next) {
        $Comments++ if ($txn->Type eq 'Comment');
    }
    is($Comments,1, "our second ticket has only one Comment");

    ($id, $msg) = $t1->Comment(Content => 'This is a second  Comment on the original');
    ok($id,$msg);

    $txns = $t1->Transactions;
    $Comments = 0;
    while (my $txn = $txns->Next) {
        $Comments++ if ($txn->Type eq 'Comment');
    }
    is($Comments,2, "our first ticket now has two Comments");

    ($id,$msg) = $t2->MergeInto($t1->id);

    ok($id,$msg);
    $txns = $t1->Transactions;
    $Comments = 0;
    while (my $txn = $txns->Next) {
        $Comments++ if ($txn->Type eq 'Comment');
    }
    is($Comments,3, "our first ticket now has three Comments - we merged safely");
}

# when you try to merge duplicate links on postgres, eveyrything goes to hell due to referential integrity constraints.
{
    my $t = RT::Ticket->new($RT::SystemUser);
    $t->Create(Subject => 'Main', Queue => 'general');

    ok ($t->id);
    my $t2 = RT::Ticket->new($RT::SystemUser);
    $t2->Create(Subject => 'Second', Queue => 'general');
    ok ($t2->id);

    my $t3 = RT::Ticket->new($RT::SystemUser);
    $t3->Create(Subject => 'Third', Queue => 'general');

    ok ($t3->id);

    my ($id,$val);
    ($id,$val) = $t->AddLink(Type => 'DependsOn', Target => $t3->id);
    ok($id,$val);
    ($id,$val) = $t2->AddLink(Type => 'DependsOn', Target => $t3->id);
    ok($id,$val);

    ($id,$val) = $t->MergeInto($t2->id);
    ok($id,$val);
}

my $user = RT::Test->load_or_create_user(
    Name => 'a user', Password => 'password',
);
ok $user && $user->id, 'loaded or created user';

# check rights
{
    RT::Test->set_rights(
        { Principal => 'Everyone', Right => [qw(SeeQueue ShowTicket CreateTicket OwnTicket TakeTicket)] },
        { Principal => 'Owner',    Right => [qw(ModifyTicket)] },
    );

    my $t = RT::Ticket->new(RT::CurrentUser->new($user));
    $t->Create(Subject => 'Main', Queue => 'general');
    ok ($t->id, "Created ticket");

    my $t2 = RT::Ticket->new(RT::CurrentUser->new($user));
    $t2->Create(Subject => 'Second', Queue => 'general');
    ok ($t2->id, "Created ticket");

    foreach my $ticket ( $t, $t2 ) {
        ok( !$ticket->CurrentUserHasRight('ModifyTicket'), "can not modify" );
    }

    my ($status,$msg) = $t->MergeInto($t2->id);
    ok(!$status, "Can not merge: $msg");
    
    ($status, $msg) = $t->SetOwner( $user->id );
    ok( $status, "User took ticket");
    ok( $t->CurrentUserHasRight('ModifyTicket'), "can modify after take" );

    ($status,$msg) = $t->MergeInto($t2->id);
    ok(!$status, "Can not merge: $msg");

    ($status, $msg) = $t2->SetOwner( $user->id );
    ok( $status, "User took ticket");
    ok( $t2->CurrentUserHasRight('ModifyTicket'), "can modify after take" );

    ($status,$msg) = $t->MergeInto($t2->id);
    ok($status, "Merged tickets: $msg");
}