default to a session cookie instead of setting an explicit timeout, weird timezone...
[freeside.git] / rt / t / mail / html-outgoing.t
1 use strict;
2 use warnings;
3 use RT::Test tests => undef;
4
5 use RT::Test::Email;
6 use Test::Warn;
7
8 my $root = RT::User->new(RT->SystemUser);
9 $root->Load('root');
10
11 # Set root as admincc
12 my $q = RT::Queue->new(RT->SystemUser);
13 $q->Load('General');
14 my ($ok, $msg) = $q->AddWatcher( Type => 'AdminCc', PrincipalId => $root->Id );
15 ok($ok, "Added root as a watcher on the General queue");
16
17 # Create a couple users to test notifications
18 my %users;
19 for my $user_name (qw(enduser tech)) {
20     my $user = $users{$user_name} = RT::User->new(RT->SystemUser);
21     $user->Create( Name => ucfirst($user_name),
22                    Privileged => 1,
23                    EmailAddress => $user_name.'@example.com');
24     my ($val, $msg);
25     ($val, $msg) = $user->PrincipalObj->GrantRight(Object =>$q, Right => $_)
26         for qw(ModifyTicket OwnTicket ShowTicket);
27 }
28
29 my $t = RT::Ticket->new(RT->SystemUser);
30 my ($tid, $ttrans, $tmsg);
31
32 diag "Autoreply and AdminCc (Transaction)";
33 mail_ok {
34     ($tid, $ttrans, $tmsg) = 
35         $t->Create(Subject => "The internet is broken",
36                    Owner => 'Tech', Requestor => 'Enduser',
37                    Queue => 'General');
38 } { from    => qr/The default queue/,
39     to      => 'enduser@example.com',
40     subject => qr/\Q[example.com #1] AutoReply: The internet is broken\E/,
41     body    => parts_regex(
42         'trouble ticket regarding \*?The internet is broken\*?',
43         'trouble ticket regarding <b>The internet is broken</b>'
44     ),
45     'Content-Type' => qr{multipart},
46 },{ from    => qr/RT System/,
47     bcc     => 'root@localhost',
48     subject => qr/\Q[example.com #1] The internet is broken\E/,
49     body    => parts_regex(
50         'Request (\[\d+\])?1(\s*[(<]http://localhost:\d+/Ticket/Display\.html\?id=1[)>])?\s*was acted upon by RT_System',
51         'Request <a href="http://localhost:\d+/Ticket/Display\.html\?id=1">1</a> was acted upon by RT_System\.</b>'
52     ),
53     'Content-Type' => qr{multipart},
54 };
55
56 diag "Admin Correspondence and Correspondence";
57 mail_ok {
58     ($ok, $tmsg) = $t->Correspond(
59         MIMEObj => HTML::Mason::Commands::MakeMIMEEntity(
60             Body => '<p>This is a test of <b>HTML</b> correspondence.</p>',
61             Type => 'text/html',
62         ),
63     );
64 } { from    => qr/RT System/,
65     bcc     => 'root@localhost',
66     subject => qr/\Q[example.com #1] The internet is broken\E/,
67     body    => parts_regex(
68         'Ticket URL: (?:\[\d+\])?http://localhost:\d+/Ticket/Display\.html\?id=1.+?'.
69         'This is a test of \*?HTML\*? correspondence\.',
70         'Ticket URL: <a href="(http://localhost:\d+/Ticket/Display\.html\?id=1)">\1</a>.+?'.
71         '<p>This is a test of <b>HTML</b> correspondence\.</p>'
72     ),
73     'Content-Type' => qr{multipart},
74 },{ from    => qr/RT System/,
75     to      => 'enduser@example.com',
76     subject => qr/\Q[example.com #1] The internet is broken\E/,
77     body    => parts_regex(
78         'This is a test of \*?HTML\*? correspondence\.',
79         '<p>This is a test of <b>HTML</b> correspondence\.</p>'
80     ),
81     'Content-Type' => qr{multipart},
82 };
83
84 SKIP: {
85     skip "Only fails on core HTMLFormatter", 9
86         unless RT->Config->Get("HTMLFormatter") eq "core";
87     require HTML::FormatText::WithLinks::AndTables;
88     skip "Only fails with older verions of HTML::FormatText::WithLinks::AndTables", 9
89         unless $HTML::FormatText::WithLinks::AndTables::VERSION < 0.03;
90     diag "Failing HTML -> Text conversion";
91     warnings_like {
92         my $body = '<table><tr><td><table><tr><td>Foo</td></tr></table></td></tr></table>';
93         mail_ok {
94             ($ok, $tmsg) = $t->Correspond(
95                 MIMEObj => HTML::Mason::Commands::MakeMIMEEntity(
96                     Body => $body,
97                     Type => 'text/html',
98                 ),
99             );
100         } { from    => qr/RT System/,
101             bcc     => 'root@localhost',
102             subject => qr/\Q[example.com #1] The internet is broken\E/,
103             body    => qr{Ticket URL: <a href="(http://localhost:\d+/Ticket/Display\.html\?id=1)">\1</a>.+?$body}s,
104             'Content-Type' => qr{text/html},  # TODO
105         },{ from    => qr/RT System/,
106             to      => 'enduser@example.com',
107             subject => qr/\Q[example.com #1] The internet is broken\E/,
108             body    => qr{$body},
109             'Content-Type' => qr{text/html},  # TODO
110         };
111     } [(qr/uninitialized value/, qr/Failed to downgrade HTML/)x3];
112 }
113
114
115 diag "Admin Comment in HTML";
116 mail_ok {
117     ($ok, $tmsg) = $t->Comment(
118         MIMEObj => HTML::Mason::Commands::MakeMIMEEntity(
119             Body => '<p>Comment test, <em>please!</em></p>',
120             Type => 'text/html',
121         ),
122     );
123 } { from    => qr/RT System/,
124     bcc     => 'root@localhost',
125     subject => qr/\Q[example.com #1] [Comment] The internet is broken\E/,
126     body    => parts_regex(
127         'This is a comment about (\[\d+\])?ticket.1(\s*[(<]http://localhost:\d+/Ticket/Display\.html\?id=1[)>])?\..+?'.
128         'It is not sent to the Requestor\(s\):.+?'.
129         'Comment test, _?please!_?',
130
131         '<p>This is a comment about <a href="http://localhost:\d+/Ticket/Display\.html\?id=1">ticket 1</a>\. '.
132         'It is not sent to the Requestor\(s\):</p>.+?'.
133         '<p>Comment test, <em>please!</em></p>',
134     ),
135     'Content-Type' => qr{multipart},
136 };
137
138
139 diag "Resolved in HTML templates";
140 mail_ok {
141     ($ok, $tmsg) = $t->SetStatus('resolved');
142 } { from    => qr/RT System/,
143     to      => 'enduser@example.com',
144     subject => qr/\Q[example.com #1] Resolved: The internet is broken\E/,
145     body    => parts_regex(
146         'According to our records, your request has been resolved\.',
147         '<p>According to our records, your request has been resolved\.',
148     ),
149     'Content-Type' => qr{multipart},
150 };
151
152
153 diag "Status changes in HTML";
154 my $scrip = RT::Scrip->new(RT->SystemUser);
155 my ($sval, $smsg) =$scrip->Create(
156     ScripCondition => 'On Status Change',
157     ScripAction => 'Notify Requestors',
158     Template => 'Status Change in HTML',
159     Queue => $q->Id,
160     Description => 'Tell requestors about status changes'
161 );
162 ok ($sval, $smsg);
163 ok ($scrip->Id, "Created the scrip");
164 ok ($scrip->TemplateObj->Id, "Created the scrip template");
165 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
166 ok ($scrip->ActionObj->Id, "Created the scrip action");
167
168 mail_ok {
169     ($ok, $tmsg) = $t->SetStatus('stalled');
170 } { from    => qr/RT System/,
171     to      => 'enduser@example.com',
172     subject => qr/\Q[example.com #1] Status Changed to: stalled\E/,
173     body    => parts_regex(
174         'http://localhost:\d+/Ticket/Display\.html\?id=1.+?',
175         '<a href="(http://localhost:\d+/Ticket/Display\.html\?id=1)">\1</a>'
176     ),
177     'Content-Type' => qr{multipart},
178 };
179
180 done_testing;
181
182 sub parts_regex {
183     my ($text, $html) = @_;
184
185     my $pattern = 'Content-Type: text/plain.+?' . $text . '.+?' .
186                   'Content-Type: text/html.+?'  . $html;
187
188     return qr/$pattern/s;
189 }
190