RT 4.2.11, ticket#13852
[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     diag "Failing HTML -> Text conversion";
88     warnings_like {
89         my $body = '<table><tr><td><table><tr><td>Foo</td></tr></table></td></tr></table>';
90         mail_ok {
91             ($ok, $tmsg) = $t->Correspond(
92                 MIMEObj => HTML::Mason::Commands::MakeMIMEEntity(
93                     Body => $body,
94                     Type => 'text/html',
95                 ),
96             );
97         } { from    => qr/RT System/,
98             bcc     => 'root@localhost',
99             subject => qr/\Q[example.com #1] The internet is broken\E/,
100             body    => qr{Ticket URL: <a href="(http://localhost:\d+/Ticket/Display\.html\?id=1)">\1</a>.+?$body}s,
101             'Content-Type' => qr{text/html},  # TODO
102         },{ from    => qr/RT System/,
103             to      => 'enduser@example.com',
104             subject => qr/\Q[example.com #1] The internet is broken\E/,
105             body    => qr{$body},
106             'Content-Type' => qr{text/html},  # TODO
107         };
108     } [(qr/uninitialized value/, qr/Failed to downgrade HTML/)x3];
109 }
110
111
112 diag "Admin Comment in HTML";
113 mail_ok {
114     ($ok, $tmsg) = $t->Comment(
115         MIMEObj => HTML::Mason::Commands::MakeMIMEEntity(
116             Body => '<p>Comment test, <em>please!</em></p>',
117             Type => 'text/html',
118         ),
119     );
120 } { from    => qr/RT System/,
121     bcc     => 'root@localhost',
122     subject => qr/\Q[example.com #1] [Comment] The internet is broken\E/,
123     body    => parts_regex(
124         'This is a comment about (\[\d+\])?ticket.1(\s*[(<]http://localhost:\d+/Ticket/Display\.html\?id=1[)>])?\..+?'.
125         'It is not sent to the Requestor\(s\):.+?'.
126         'Comment test, _?please!_?',
127
128         '<p>This is a comment about <a href="http://localhost:\d+/Ticket/Display\.html\?id=1">ticket 1</a>\. '.
129         'It is not sent to the Requestor\(s\):</p>.+?'.
130         '<p>Comment test, <em>please!</em></p>',
131     ),
132     'Content-Type' => qr{multipart},
133 };
134
135
136 diag "Resolved in HTML templates";
137 mail_ok {
138     ($ok, $tmsg) = $t->SetStatus('resolved');
139 } { from    => qr/RT System/,
140     to      => 'enduser@example.com',
141     subject => qr/\Q[example.com #1] Resolved: The internet is broken\E/,
142     body    => parts_regex(
143         'According to our records, your request has been resolved\.',
144         '<p>According to our records, your request has been resolved\.',
145     ),
146     'Content-Type' => qr{multipart},
147 };
148
149
150 diag "Status changes in HTML";
151 my $scrip = RT::Scrip->new(RT->SystemUser);
152 my ($sval, $smsg) =$scrip->Create(
153     ScripCondition => 'On Status Change',
154     ScripAction => 'Notify Requestors',
155     Template => 'Status Change in HTML',
156     Queue => $q->Id,
157     Description => 'Tell requestors about status changes'
158 );
159 ok ($sval, $smsg);
160 ok ($scrip->Id, "Created the scrip");
161 ok ($scrip->TemplateObj->Id, "Created the scrip template");
162 ok ($scrip->ConditionObj->Id, "Created the scrip condition");
163 ok ($scrip->ActionObj->Id, "Created the scrip action");
164
165 mail_ok {
166     ($ok, $tmsg) = $t->SetStatus('stalled');
167 } { from    => qr/RT System/,
168     to      => 'enduser@example.com',
169     subject => qr/\Q[example.com #1] Status Changed to: stalled\E/,
170     body    => parts_regex(
171         'http://localhost:\d+/Ticket/Display\.html\?id=1.+?',
172         '<a href="(http://localhost:\d+/Ticket/Display\.html\?id=1)">\1</a>'
173     ),
174     'Content-Type' => qr{multipart},
175 };
176
177 done_testing;
178
179 sub parts_regex {
180     my ($text, $html) = @_;
181
182     my $pattern = 'Content-Type: text/plain.+?' . $text . '.+?' .
183                   'Content-Type: text/html.+?'  . $html;
184
185     return qr/$pattern/s;
186 }
187