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