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