first pass RT4 merge, RT#13852
[freeside.git] / rt / t / mail / charsets-outgoing.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Encode;
5
6 use RT::Test tests => 78;
7
8 my %string = (
9     ru => {
10         test      => "\x{442}\x{435}\x{441}\x{442}",
11         autoreply => "\x{410}\x{432}\x{442}\x{43e}\x{43e}\x{442}\x{432}\x{435}\x{442}",
12         support   => "\x{43f}\x{43e}\x{434}\x{434}\x{435}\x{440}\x{436}\x{43a}\x{430}",
13     },
14     latin1 => {
15         test      => Encode::decode('latin1', "t\xE9st"),
16         autoreply => Encode::decode('latin1', "a\xFCtoreply"),
17         support   => Encode::decode('latin1', "supp\xF5rt"),
18     },
19 );
20
21 my $queue = RT::Test->load_or_create_queue(
22     Name              => 'Regression',
23     CorrespondAddress => 'rt-recipient@example.com',
24     CommentAddress    => 'rt-recipient@example.com',
25 );
26 ok $queue && $queue->id, 'loaded or created queue';
27
28 diag "make sure queue has no subject tag";
29 {
30     my ($status, $msg) = $queue->SetSubjectTag( undef );
31     ok $status, "set subject tag for the queue" or diag "error: $msg";
32 }
33
34 diag "set intial simple autoreply template";
35 {
36     my $template = RT::Template->new( RT->SystemUser );
37     $template->Load('Autoreply');
38     ok $template->id, "loaded autoreply tempalte";
39
40     my ($status, $msg) = $template->SetContent(
41         "Subject: Autreply { \$Ticket->Subject }\n"
42         ."\n"
43         ."hi there it's an autoreply.\n"
44         ."\n"
45     );
46     ok $status, "changed content of the template"
47         or diag "error: $msg";
48 }
49
50 diag "basic test of autoreply";
51 {
52     my $ticket = RT::Ticket->new( RT->SystemUser );
53     $ticket->Create(
54         Queue => $queue->id,
55         Subject => 'test',
56         Requestor => 'root@localhost',
57     );
58     my @mails = RT::Test->fetch_caught_mails;
59     ok @mails, "got some outgoing emails";
60 }
61
62 diag "non-ascii Subject with ascii prefix set in the template";
63 foreach my $set ( 'ru', 'latin1' ) {
64     my $ticket = RT::Ticket->new( RT->SystemUser );
65     $ticket->Create(
66         Queue => $queue->id,
67         Subject => $string{$set}{test},
68         Requestor => 'root@localhost',
69     );
70     my @mails = RT::Test->fetch_caught_mails;
71     ok @mails, "got some outgoing emails";
72
73     my $status = 1;
74     foreach my $mail ( @mails ) {
75         my $entity = parse_mail( $mail );
76         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
77         $subject =~ /$string{$set}{test}/
78             or do { $status = 0; diag "wrong subject: $subject" };
79     }
80     ok $status, "all mails have correct data";
81 }
82
83 foreach my $tag_set ( 'ru', 'latin1' ) {
84
85 diag "set non-ascii subject tag for the queue";
86 {
87     my ($status, $msg) = $queue->SetSubjectTag( $string{$tag_set}{support} );
88     ok $status, "set subject tag for the queue" or diag "error: $msg";
89 }
90
91 diag "ascii subject with non-ascii subject tag";
92 {
93     my $ticket = RT::Ticket->new( RT->SystemUser );
94     $ticket->Create(
95         Queue => $queue->id,
96         Subject => 'test',
97         Requestor => 'root@localhost',
98     );
99     my @mails = RT::Test->fetch_caught_mails;
100     ok @mails, "got some outgoing emails";
101
102     my $status = 1;
103     foreach my $mail ( @mails ) {
104         my $entity = parse_mail( $mail );
105         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
106         $subject =~ /$string{$tag_set}{support}/
107             or do { $status = 0; diag "wrong subject: $subject" };
108     }
109     ok $status, "all mails have correct data";
110 }
111
112 diag "non-ascii subject with non-ascii subject tag";
113 foreach my $set ( 'ru', 'latin1' ) {
114     my $ticket = RT::Ticket->new( RT->SystemUser );
115     $ticket->Create(
116         Queue => $queue->id,
117         Subject => $string{$set}{test},
118         Requestor => 'root@localhost',
119     );
120     my @mails = RT::Test->fetch_caught_mails;
121     ok @mails, "got some outgoing emails";
122
123     my $status = 1;
124     foreach my $mail ( @mails ) {
125         my $entity = parse_mail( $mail );
126         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
127         $subject =~ /$string{$tag_set}{support}/
128             or do { $status = 0; diag "wrong subject: $subject" };
129         $subject =~ /$string{$set}{test}/
130             or do { $status = 0; diag "wrong subject: $subject" };
131     }
132     ok $status, "all mails have correct data";
133 }
134
135 } # subject tag
136
137 diag "return back the empty subject tag";
138 {
139     my ($status, $msg) = $queue->SetSubjectTag( undef );
140     ok $status, "set subject tag for the queue" or diag "error: $msg";
141 }
142
143
144 foreach my $prefix_set ( 'ru', 'latin1' ) {
145
146 diag "add non-ascii subject prefix in the autoreply template";
147 {
148     my $template = RT::Template->new( RT->SystemUser );
149     $template->Load('Autoreply');
150     ok $template->id, "loaded autoreply tempalte";
151
152     my ($status, $msg) = $template->SetContent(
153         "Subject: $string{$prefix_set}{autoreply} { \$Ticket->Subject }\n"
154         ."\n"
155         ."hi there it's an autoreply.\n"
156         ."\n"
157     );
158     ok $status, "changed content of the template" or diag "error: $msg";
159 }
160
161 diag "ascii subject with non-ascii subject prefix in template";
162 {
163     my $ticket = RT::Ticket->new( RT->SystemUser );
164     $ticket->Create(
165         Queue => $queue->id,
166         Subject => 'test',
167         Requestor => 'root@localhost',
168     );
169     my @mails = RT::Test->fetch_caught_mails;
170     ok @mails, "got some outgoing emails";
171
172     my $status = 1;
173     foreach my $mail ( @mails ) {
174         my $entity = parse_mail( $mail );
175         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
176         $subject =~ /$string{$prefix_set}{autoreply}/
177             or do { $status = 0; diag "wrong subject: $subject" };
178     }
179     ok $status, "all mails have correct data";
180 }
181
182 diag "non-ascii subject with non-ascii subject prefix in template";
183 foreach my $set ( 'ru', 'latin1' ) {
184     my $ticket = RT::Ticket->new( RT->SystemUser );
185     $ticket->Create(
186         Queue => $queue->id,
187         Subject => $string{$set}{test},
188         Requestor => 'root@localhost',
189     );
190     my @mails = RT::Test->fetch_caught_mails;
191     ok @mails, "got some outgoing emails";
192
193     my $status = 1;
194     foreach my $mail ( @mails ) {
195         my $entity = parse_mail( $mail );
196         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
197         $subject =~ /$string{$prefix_set}{autoreply}/
198             or do { $status = 0; diag "wrong subject: $subject" };
199         $subject =~ /$string{$set}{test}/
200             or do { $status = 0; diag "wrong subject: $subject" };
201     }
202     ok $status, "all mails have correct data";
203 }
204
205 foreach my $tag_set ( 'ru', 'latin1' ) {
206 diag "set non-ascii subject tag for the queue";
207 {
208     my ($status, $msg) = $queue->SetSubjectTag( $string{$tag_set}{support} );
209     ok $status, "set subject tag for the queue" or diag "error: $msg";
210 }
211
212 diag "non-ascii subject, non-ascii prefix in template and non-ascii tag";
213 foreach my $set ( 'ru', 'latin1' ) {
214     my $ticket = RT::Ticket->new( RT->SystemUser );
215     $ticket->Create(
216         Queue => $queue->id,
217         Subject => $string{$set}{test},
218         Requestor => 'root@localhost',
219     );
220     my @mails = RT::Test->fetch_caught_mails;
221     ok @mails, "got some outgoing emails";
222
223     my $status = 1;
224     foreach my $mail ( @mails ) {
225         my $entity = parse_mail( $mail );
226         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
227         $subject =~ /$string{$prefix_set}{autoreply}/
228             or do { $status = 0; diag "wrong subject: $subject" };
229         $subject =~ /$string{$tag_set}{support}/
230             or do { $status = 0; diag "wrong subject: $subject" };
231         $subject =~ /$string{$set}{test}/
232             or do { $status = 0; diag "wrong subject: $subject" };
233     }
234     ok $status, "all mails have correct data";
235 }
236
237 } # subject tag
238
239 diag "flush subject tag of the queue";
240 {
241     my ($status, $msg) = $queue->SetSubjectTag( undef );
242     ok $status, "set subject tag for the queue" or diag "error: $msg";
243 }
244
245 } # prefix set
246
247
248 diag "don't change subject via template";
249 # clean DB has autoreply that always changes subject in template,
250 # we should test situation when subject is not changed from template
251 {
252     my $template = RT::Template->new( RT->SystemUser );
253     $template->Load('Autoreply');
254     ok $template->id, "loaded autoreply tempalte";
255
256     my ($status, $msg) = $template->SetContent(
257         "\n"
258         ."\n"
259         ."hi there it's an autoreply.\n"
260         ."\n"
261     );
262     ok $status, "changed content of the template" or diag "error: $msg";
263 }
264
265 diag "non-ascii Subject without changes in template";
266 foreach my $set ( 'ru', 'latin1' ) {
267     my $ticket = RT::Ticket->new( RT->SystemUser );
268     $ticket->Create(
269         Queue => $queue->id,
270         Subject => $string{$set}{test},
271         Requestor => 'root@localhost',
272     );
273     my @mails = RT::Test->fetch_caught_mails;
274     ok @mails, "got some outgoing emails";
275
276     my $status = 1;
277     foreach my $mail ( @mails ) {
278         my $entity = parse_mail( $mail );
279         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
280         $subject =~ /$string{$set}{test}/
281             or do { $status = 0; diag "wrong subject: $subject" };
282     }
283     ok $status, "all mails have correct data";
284 }
285
286 foreach my $tag_set ( 'ru', 'latin1' ) {
287 diag "set non-ascii subject tag for the queue";
288 {
289     my ($status, $msg) = $queue->SetSubjectTag( $string{$tag_set}{support} );
290     ok $status, "set subject tag for the queue" or diag "error: $msg";
291 }
292
293 diag "non-ascii Subject without changes in template and with non-ascii subject tag";
294 foreach my $set ( 'ru', 'latin1' ) {
295     my $ticket = RT::Ticket->new( RT->SystemUser );
296     $ticket->Create(
297         Queue => $queue->id,
298         Subject => $string{$set}{test},
299         Requestor => 'root@localhost',
300     );
301     my @mails = RT::Test->fetch_caught_mails;
302     ok @mails, "got some outgoing emails";
303
304     my $status = 1;
305     foreach my $mail ( @mails ) {
306         my $entity = parse_mail( $mail );
307         my $subject = Encode::decode_utf8( $entity->head->get('Subject') );
308         $subject =~ /$string{$set}{test}/
309             or do { $status = 0; diag "wrong subject: $subject" };
310         $subject =~ /$string{$tag_set}{support}/
311             or do { $status = 0; diag "wrong subject: $subject" };
312     }
313     ok $status, "all mails have correct data";
314 }
315
316 } # subject tag set
317