RT 4.2.11, ticket#13852
[freeside.git] / rt / t / mail / wrong_mime_charset.t
1 use strict;
2 use warnings;
3 use RT::Test nodb => 1, tests => undef;
4
5 use_ok('RT::I18N');
6 my $test_string    = Encode::decode("UTF-8", 'À');
7 my $encoded_string = Encode::encode( 'iso-8859-1', $test_string );
8 my $mime           = MIME::Entity->build(
9     "Subject" => $encoded_string,
10     "Data"    => [$encoded_string],
11 );
12
13 # set the wrong charset mime in purpose
14 $mime->head->mime_attr( "Content-Type.charset" => 'utf8' );
15
16 my @warnings;
17 local $SIG{__WARN__} = sub {
18     push @warnings, "@_";
19 };
20
21 RT::I18N::SetMIMEEntityToEncoding( $mime, 'iso-8859-1' );
22
23 # this is a weird behavior for different perl versions, 5.12 warns twice,
24 # which is correct since we do the encoding thing twice, for Subject
25 # and Data respectively.
26 # but 5.8 and 5.10 warns only once.
27 ok( @warnings == 1 || @warnings == 2, "1 or 2 warnings are ok" );
28 ok( @warnings == 1 || ( @warnings == 2 && $warnings[1] eq $warnings[0] ),
29     'if there are 2 warnings, they should be same' );
30
31 like(
32     $warnings[0],
33     qr/\QEncoding error: "\x{fffd}" does not map to iso-8859-1/,
34 "We can't encode something into the wrong encoding without Encode complaining"
35 );
36
37 my $subject = Encode::decode( 'iso-8859-1', $mime->head->get('Subject') );
38 chomp $subject;
39 is( $subject, $test_string, 'subject is set to iso-8859-1' );
40 my $body = Encode::decode( 'iso-8859-1', $mime->stringify_body );
41 chomp $body;
42 is( $body, $test_string, 'body is set to iso-8859-1' );
43
44 done_testing;