RT 4.0.22
[freeside.git] / rt / t / web / attachment_encoding.t
1
2 use strict;
3 use warnings;
4
5 use RT::Test tests => 32;
6 my ( $baseurl, $m ) = RT::Test->started_ok;
7 ok $m->login, 'logged in as root';
8
9 use File::Spec;
10
11 my $subject  = Encode::decode("UTF-8",'标题');
12 my $content  = Encode::decode("UTF-8",'测试');
13 my $filename = Encode::decode("UTF-8",'附件.txt');
14
15 diag 'test without attachments' if $ENV{TEST_VERBOSE};
16
17 {
18     $m->get_ok( $baseurl . '/Ticket/Create.html?Queue=1' );
19
20     $m->form_name('TicketModify');
21     $m->submit_form(
22         form_number => 3,
23         fields      => { Subject => $subject, Content => $content },
24     );
25     $m->content_like( qr/Ticket \d+ created/i, 'created the ticket' );
26     $m->follow_link_ok( { text => 'with headers' },
27         '-> /Ticket/Attachment/WithHeaders/...' );
28     $m->content_contains( $subject, "has subject $subject" );
29     $m->content_contains( $content, "has content $content" );
30
31     my ( $id ) = $m->uri =~ /(\d+)$/;
32     ok( $id, 'found attachment id' );
33     my $attachment = RT::Attachment->new( $RT::SystemUser );
34     ok($attachment->Load($id), "load att $id");
35     # let make original encoding to gbk
36     ok( $attachment->SetHeader( 'X-RT-Original-Encoding' => 'gbk' ),
37         'set original encoding to gbk' );
38     $m->get( $m->uri );
39     $m->content_contains( $subject, "has subject $subject" );
40     $m->content_contains( $content, "has content $content" );
41 }
42
43 diag 'test with attachemnts' if $ENV{TEST_VERBOSE};
44
45 {
46
47     my $file =
48       File::Spec->catfile( RT::Test->temp_directory, Encode::encode("UTF-8",$filename) );
49     open( my $fh, '>', $file ) or die $!;
50     binmode $fh, ':utf8';
51     print $fh $filename;
52     close $fh;
53
54     $m->get_ok( $baseurl . '/Ticket/Create.html?Queue=1' );
55
56     $m->form_name('TicketModify');
57     $m->submit_form(
58         form_number => 3,
59         fields => { Subject => $subject, Content => $content, Attach => $file },
60     );
61     $m->content_like( qr/Ticket \d+ created/i, 'created the ticket' );
62     $m->content_contains( $filename, 'attached filename' );
63     $m->content_lacks( Encode::encode("UTF-8",$filename), 'no double encoded attached filename' );
64     $m->follow_link_ok( { text => 'with headers' },
65         '-> /Ticket/Attachment/WithHeaders/...' );
66
67     # subject is in the parent attachment, so there is no 标题
68     $m->content_lacks( $subject, "does not have content $subject" );
69     $m->content_contains( $content, "has content $content" );
70
71     my ( $id ) = $m->uri =~ /(\d+)$/;
72     ok( $id, 'found attachment id' );
73     my $attachment = RT::Attachment->new( $RT::SystemUser );
74     ok($attachment->Load($id), "load att $id");
75     # let make original encoding to gbk
76     ok( $attachment->SetHeader( 'X-RT-Original-Encoding' => 'gbk' ),
77         'set original encoding to gbk' );
78     $m->get( $m->uri );
79     $m->content_lacks( $subject, "does not have content $subject" );
80     $m->content_contains( $content, "has content $content" );
81
82
83     $m->back;
84     $m->back;
85     $m->follow_link_ok( { text => "Download $filename" },
86         '-> /Ticket/Attachment/...' );
87     $m->content_contains( $filename, "has file content $filename" );
88
89     ( $id ) = $m->uri =~ /(\d+)\D+$/;
90     ok( $id, 'found attachment id' );
91     $attachment = RT::Attachment->new( $RT::SystemUser );
92     ok($attachment->Load($id), "load att $id");
93
94     # let make original encoding to gbk
95     ok( $attachment->SetHeader( 'X-RT-Original-Encoding' => 'gbk' ),
96         'set original encoding to gbk' );
97     $m->get( $m->uri );
98     $m->content_contains( $filename, "has content $filename" );
99
100     unlink $file;
101 }
102