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