first pass RT4 merge, RT#13852
[freeside.git] / rt / t / mail / threading.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use utf8;
5
6 use RT::Test tests => 22;
7 RT->Config->Set( NotifyActor => 1 );
8
9 my $queue = RT::Test->load_or_create_queue(
10     Name              => 'General',
11     CorrespondAddress => 'rt-recipient@example.com',
12     CommentAddress    => 'rt-recipient@example.com',
13 );
14 ok $queue && $queue->id, 'loaded or created queue';
15
16 my $user = RT::Test->load_or_create_user(
17     Name         => 'root',
18     EmailAddress => 'root@localhost',
19 );
20 ok $user && $user->id, 'loaded or created user';
21
22 {
23     my $mail = <<EOF;
24 From: root\@localhost
25 Subject: a ticket
26 Message-ID: <some-message-id>
27
28 Foob!
29 EOF
30     my ($status, $id) = RT::Test->send_via_mailgate($mail);
31     ok $id, "created a ticket";
32
33     my @mail = RT::Test->fetch_caught_mails;
34     is scalar @mail, 1, "autoreply";
35     like $mail[0], qr{^In-Reply-To:\s*<some-message-id>$}mi;
36     like $mail[0], qr{^References:\s*<RT-Ticket-\Q$id\E\@example\.com>}mi;
37
38     my $ticket = RT::Ticket->new( RT->SystemUser );
39     $ticket->Load( $id );
40     ok $ticket->id, "loaded ticket";
41
42     ($status, my ($msg)) = $ticket->Correspond( Content => 'boo' );
43     ok $status, "replied to the ticket";
44
45     @mail = RT::Test->fetch_caught_mails;
46     is scalar @mail, 1, "reply";
47     like $mail[0], qr{^References:\s*<RT-Ticket-\Q$id\E\@example\.com>$}mi,
48         "no context, so only pseudo header is referenced";
49 }
50
51 {
52     my ($ticket) = RT::Test->create_ticket(
53         Queue => $queue->id,
54         Requestor => $user->EmailAddress
55     );
56     my $id = $ticket->id;
57     ok $id, "created a ticket";
58
59     my @mail = RT::Test->fetch_caught_mails;
60     is scalar @mail, 1, "autoreply";
61     like $mail[0], qr{^References:\s*<RT-Ticket-\Q$id\E\@example\.com>}mi;
62 }
63
64 {
65     my $scrip = RT::Scrip->new(RT->SystemUser);
66     my ($status, $msg) = $scrip->Create(
67         Description => "Notify requestor on status change",
68         ScripCondition => 'On Status Change',
69         ScripAction    => 'Notify Requestors',
70         Template       => 'Transaction',
71         Stage          => 'TransactionCreate',
72         Queue          => 0,
73     );
74     ok($status, "Scrip created");
75
76     my ($ticket) = RT::Test->create_ticket(
77         Queue => $queue->id,
78         Requestor => $user->EmailAddress,
79     );
80     my $id = $ticket->id;
81     ok $id, "created a ticket";
82
83     RT::Test->fetch_caught_mails;
84     ($status, $msg) = $ticket->SetStatus('open');
85     ok $status, "changed status";
86
87     my @mail = RT::Test->fetch_caught_mails;
88     is scalar @mail, 1, "status change notification";
89     like $mail[0], qr{^References:\s*<RT-Ticket-\Q$id\E\@example\.com>}mi;
90 }