RT 4.0.22
[freeside.git] / rt / t / mail / wrong_mime_charset.t
1 use strict;
2 use warnings;
3 use RT::Test nodb => 1, tests => 6;
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 TODO: {
24         local $TODO =
25 'need a better approach of encoding converter, should be fixed in 4.2';
26
27 # this is a weird behavior for different perl versions, 5.12 warns twice,
28 # which is correct since we do the encoding thing twice, for Subject
29 # and Data respectively.
30 # but 5.8 and 5.10 warns only once.
31 ok( @warnings == 1 || @warnings == 2, "1 or 2 warnings are ok" );
32 ok( @warnings == 1 || ( @warnings == 2 && $warnings[1] eq $warnings[0] ),
33     'if there are 2 warnings, they should be same' );
34
35 like(
36     $warnings[0],
37     qr/\QEncoding error: "\x{fffd}" does not map to iso-8859-1/,
38 "We can't encode something into the wrong encoding without Encode complaining"
39 );
40
41 my $subject = Encode::decode( 'iso-8859-1', $mime->head->get('Subject') );
42 chomp $subject;
43 is( $subject, $test_string, 'subject is set to iso-8859-1' );
44 my $body = Encode::decode( 'iso-8859-1', $mime->stringify_body );
45 chomp $body;
46 is( $body, $test_string, 'body is set to iso-8859-1' );
47 }