fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / web / ticket_forward.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5 use File::Spec;
6 my $att_file = File::Spec->catfile( RT::Test->temp_directory, 'attachment' );
7 open my $att_fh, '>', $att_file or die $!;
8 print $att_fh "this is an attachment";
9 close $att_fh;
10 my $att_name = ( File::Spec->splitpath($att_file) )[-1];
11
12 my ( $baseurl, $m ) = RT::Test->started_ok;
13 ok $m->login, 'logged in as root';
14
15 # Create a ticket with content and an attachment
16 $m->get_ok( $baseurl . '/Ticket/Create.html?Queue=1' );
17
18 $m->submit_form(
19     form_name => 'TicketCreate',
20     fields    => {
21         Subject => 'test forward',
22         Content => 'this is content',
23         Attach  => $att_file,
24     },
25 );
26 $m->content_like( qr/Ticket \d+ created/i, 'created the ticket' );
27 RT::Test->clean_caught_mails;
28
29 diag "Forward Ticket" if $ENV{TEST_VERBOSE};
30 {
31     $m->follow_link_ok(
32         { id => 'page-actions-forward' },
33         'follow 1st Forward to forward ticket'
34     );
35
36     $m->submit_form(
37         form_name => 'ForwardMessage',
38         fields    => {
39             To  => '"Foo" <rt-foo@example.com>, rt-too@example.com',
40             Cc  => 'rt-cc@example.com',
41             Bcc => 'root',
42         },
43         button => 'ForwardAndReturn'
44     );
45     $m->content_contains(
46         'Forwarded Ticket to Foo &lt;rt-foo@example.com&gt;, &lt;rt-too@example.com&gt;, &lt;rt-cc@example.com&gt;, root &#40;Enoch Root&#41;',
47         'txn msg' );
48     my ($mail) = RT::Test->fetch_caught_mails;
49     like( $mail, qr!Subject: test forward!,           'Subject field' );
50     like( $mail, qr!To: .*?rt-foo\@example.com!i,     'To field' );
51     like( $mail, qr!To: .*?rt-too\@example.com!i,     'To field' );
52     like( $mail, qr!Cc: rt-cc\@example.com!i,         'Cc field' );
53     like( $mail, qr!Bcc: root\@localhost!i,  'Bcc field' );
54     like( $mail, qr!This is a forward of ticket!,     'content' );
55     like( $mail, qr!this is an attachment!,           'att content' );
56     like( $mail, qr!$att_name!,                       'att file name' );
57 }
58
59 diag "Forward Transaction" if $ENV{TEST_VERBOSE};
60 {
61     $m->follow_link_ok( { text => 'Forward', n => 2 }, 'follow 2nd Forward' );
62     $m->submit_form(
63         form_name => 'ForwardMessage',
64         fields    => {
65             To  => 'rt-to@example.com, rt-too@example.com',
66             Cc  => 'rt-cc@example.com',
67             Bcc => 'root'
68         },
69         button => 'ForwardAndReturn'
70     );
71     $m->content_like(
72 qr/Forwarded .*?Transaction #\d+.*? to &lt;rt-to\@example\.com&gt;, &lt;rt-too\@example\.com&gt;, &lt;rt-cc\@example\.com&gt;, root &#40;Enoch Root&#41;/,
73         'txn msg'
74     );
75     my ($mail) = RT::Test->fetch_caught_mails;
76     like( $mail, qr!Subject: test forward!,            'Subject field' );
77     like( $mail, qr!To: .*rt-to\@example.com!i,        'To field' );
78     like( $mail, qr!To: .*rt-too\@example.com!i,       'To field' );
79     like( $mail, qr!Cc: rt-cc\@example.com!i,          'Cc field' );
80     like( $mail, qr!Bcc: root\@localhost!i,   'Bcc field' );
81     like( $mail, qr!This is a forward of transaction!, 'content' );
82     like( $mail, qr!$att_name!,                        'att file name' );
83     like( $mail, qr!this is an attachment!,            'att content' );
84 }
85
86 diag "Forward Ticket without content" if $ENV{TEST_VERBOSE};
87 {
88     my $ticket = RT::Test->create_ticket(
89         Subject => 'test forward without content',
90         Queue   => 1,
91     );
92     $m->get_ok( $baseurl . '/Ticket/Forward.html?id=' . $ticket->id );
93     $m->submit_form(
94         form_name => 'ForwardMessage',
95         fields    => { To => 'rt-test@example.com', },
96         button    => 'ForwardAndReturn'
97     );
98     my ($mail) = RT::Test->fetch_caught_mails;
99     like( $mail, qr/Subject: \[example\.com #\d\] Fwd: test forward without content/, 'Subject field' );
100     like( $mail, qr/To: rt-test\@example\.com/,             'To field' );
101     like( $mail, qr/This is a forward of ticket #\d/,       'content' );
102 }
103
104 diag "Forward Transaction with attachments but empty content" if $ENV{TEST_VERBOSE};
105 {
106     # Create a ticket without content but with a non-text/plain attachment
107     $m->get_ok( $baseurl . '/Ticket/Create.html?Queue=1' );
108
109     $m->form_name('TicketCreate');
110     my $attach = $m->current_form->find_input('Attach');
111     $attach->filename('awesome.pátch');
112     $attach->headers('Content-Type' => 'text/x-diff');
113     $m->set_fields(
114         Subject => 'test forward, empty content but attachments',
115         Attach  => $att_file, # from up top
116     );
117     $m->click('AddMoreAttach');
118     $m->form_name('TicketCreate');
119     $attach = $m->current_form->find_input('Attach');
120     $attach->filename("bpslogo.png");
121     $attach->headers('Content-Type' => 'image/png');
122     $m->set_fields(
123         Attach  => RT::Test::get_relocatable_file('bpslogo.png', '..', 'data'), # an image!
124     );
125     $m->submit;
126     $m->content_like( qr/Ticket \d+ created/i,  'created the ticket' );
127     $m->content_like( qr/awesome.p\%C3\%A1tch/, 'uploaded patch file' );
128     $m->content_like( qr/text\/x-diff/,     'uploaded patch file content type' );
129     $m->content_like( qr/bpslogo\.png/,     'uploaded image file' );
130     $m->content_like( qr/image\/png/,       'uploaded image file content type' );
131     RT::Test->clean_caught_mails;
132
133     $m->follow_link_ok( { text => 'Forward', n => 2 }, 'follow 2nd Forward' );
134     $m->submit_form(
135         form_name => 'ForwardMessage',
136         fields    => {
137             To  => 'rt-test@example.com',
138         },
139         button => 'ForwardAndReturn'
140     );
141     $m->content_like( qr/Forwarded .*?Transaction #\d+.*? to &lt;rt-test\@example\.com&gt;/, 'txn msg' );
142     my ($mail) = RT::Test->fetch_caught_mails;
143     like( $mail, qr/Subject: test forward, empty content but attachments/, 'Subject field' );
144     like( $mail, qr/To: rt-test\@example.com/,         'To field' );
145     like( $mail, qr/This is a forward of transaction/, 'content' );
146     like( $mail, qr/filename\*\=\"UTF\-8\'\'awesome.p\%C3\%A1tch\"/, 'att file name' );
147     like( $mail, qr/this is an attachment/,            'att content' );
148     like( $mail, qr/text\/x-diff/,                     'att content type' );
149     like( $mail, qr/bpslogo\.png/,                     'att image file name' );
150     like( $mail, qr/image\/png/,                       'att image content type' );
151 }
152
153 diag "Forward Transaction with attachments but no 'content' part" if $ENV{TEST_VERBOSE};
154 {
155     my $mime = MIME::Entity->build(
156         From    => '"Tést" <test@example.com>',
157         Subject => 'attachments for everyone',
158         Type    => 'multipart/mixed',
159     );
160
161     $mime->attach(
162         Path        => $att_file,
163         Type        => 'text/x-diff',
164         Filename    => 'awesome.patch',
165         Disposition => 'attachment',
166     );
167     
168     $mime->attach(
169         Path        => RT::Test::get_relocatable_file('bpslogo.png', '..', 'data'),
170         Type        => 'image/png',
171         Filename    => 'bpslogo.png',
172         Encoding    => 'base64',
173         Disposition => 'attachment',
174     );
175
176     my $ticket = RT::Test->create_ticket(
177         Queue   => 1,
178         Subject => 'test forward, attachments but no "content"',
179         MIMEObj => $mime,
180     );
181
182     $m->get_ok( $baseurl . '/Ticket/Display.html?id=' . $ticket->Id );
183     $m->content_like( qr/awesome\.patch/,   'uploaded patch file' );
184     $m->content_like( qr/text\/x-diff/,     'uploaded patch file content type' );
185     $m->content_like( qr/bpslogo\.png/,     'uploaded image file' );
186     $m->content_like( qr/image\/png/,       'uploaded image file content type' );
187     RT::Test->clean_caught_mails;
188
189     # Forward txn
190     $m->follow_link_ok( { text => 'Forward', n => 2 }, 'follow 2nd Forward' );
191     $m->submit_form(
192         form_name => 'ForwardMessage',
193         fields    => {
194             To  => 'rt-test@example.com',
195         },
196         button => 'ForwardAndReturn'
197     );
198     $m->content_like( qr/Forwarded .*?Transaction #\d+.*? to &lt;rt-test\@example\.com&gt;/, 'txn msg' );
199
200     # Forward ticket
201     $m->follow_link_ok( { text => 'Forward', n => 1 }, 'follow 1st Forward' );
202     $m->submit_form(
203         form_name => 'ForwardMessage',
204         fields    => {
205             To  => 'rt-test@example.com',
206         },
207         button => 'ForwardAndReturn'
208     );
209     $m->content_like( qr/Forwarded Ticket to &lt;rt-test\@example\.com&gt;/, 'txn msg' );
210
211     my ($forward_txn, $forward_ticket) = RT::Test->fetch_caught_mails;
212     my $tag = qr/\[example\.com #\d+\] Fwd:/;
213     like( $forward_txn, qr/Subject: $tag attachments for everyone/, 'Subject field is from txn' );
214     like( $forward_txn, qr/This is a forward of transaction/, 'forward description' );
215     like( $forward_ticket, qr/Subject: $tag test forward, attachments but no "content"/, 'Subject field is from ticket' );
216     like( $forward_ticket, qr/This is a forward of ticket/, 'forward description' );
217     like( $forward_ticket, qr/From: \=\?UTF-8\?.* \<test\@example\.com\>/i );
218
219     for my $mail ($forward_txn, $forward_ticket) {
220         like( $mail, qr/To: rt-test\@example.com/,         'To field' );
221         like( $mail, qr/awesome\.patch/,                   'att file name' );
222         like( $mail, qr/this is an attachment/,            'att content' );
223         like( $mail, qr/text\/x-diff/,                     'att content type' );
224         like( $mail, qr/bpslogo\.png/,                     'att image file name' );
225         like( $mail, qr/image\/png/,                       'att image content type' );
226     }
227 }
228 RT::Test->clean_caught_mails;
229
230 diag "Forward Ticket Template with a Subject: line" if $ENV{TEST_VERBOSE};
231 {
232
233     require RT::Template;
234     my $template = RT::Template->new($RT::SystemUser);
235     $template->Load('Forward Ticket');
236
237     # prepend a Subject: line
238     $template->SetContent("Subject: OVERRIDING SUBJECT\n\n" . $template->Content);
239
240     my $ticket = RT::Test->create_ticket(
241         Subject => 'test ticket',
242         Queue   => 1,
243     );
244
245     $m->goto_ticket($ticket->Id);
246
247     $m->follow_link_ok(
248         { id => 'page-actions-forward' },
249         'follow 1st Forward to forward ticket'
250     );
251
252     $m->submit_form(
253         form_name => 'ForwardMessage',
254         fields    => {
255             To => 'rt-to@example.com',
256         },
257         button => 'ForwardAndReturn'
258     );
259
260     my ($mail) = RT::Test->fetch_caught_mails;
261     like($mail, qr/Subject: \[example.com #\d+\] OVERRIDING SUBJECT/);
262 }
263
264 diag "Forward Transaction with non-ascii subject" if $ENV{TEST_VERBOSE};
265 {
266     $m->follow_link_ok( { text => 'Forward', n => 2 }, 'follow 2nd Forward' );
267     my $subject = Encode::decode("UTF-8", 'test non-ascii äöü');
268     $m->submit_form(
269         form_name => 'ForwardMessage',
270         fields    => {
271             Subject => $subject,
272             To  => 'rt-to@example.com',
273         },
274         button => 'ForwardAndReturn'
275     );
276     my ($mail) = RT::Test->fetch_caught_mails;
277     if ( $mail =~ /Subject: (.+)/ ) {
278         like( Encode::decode("UTF-8", RT::I18N::DecodeMIMEWordsToUTF8( $1, 'Subject' )), qr/$subject/, 'non-ascii subject' );
279     }
280     $m->content_contains( $subject, 'non-ascii subject got displayed correctly' );
281 }
282
283 undef $m;
284 done_testing;