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