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