Aleksander Machniak
2015-06-27 a7e552b5a436375e233e7d4df4ed6cfc9e58d435
commit | author | age
b37954 1 <?php
AM 2
3 /**
4  * Test class to test rcube_mime class
5  *
6  * @package Tests
7  */
8 class Framework_Mime extends PHPUnit_Framework_TestCase
9 {
10
11     /**
12      * Test decoding of single e-mail address strings
13      * Uses rcube_mime::decode_address_list()
14      */
15     function test_decode_single_address()
16     {
17         $headers = array(
18             0  => 'test@domain.tld',
19             1  => '<test@domain.tld>',
20             2  => 'Test <test@domain.tld>',
21             3  => 'Test Test <test@domain.tld>',
22             4  => 'Test Test<test@domain.tld>',
23             5  => '"Test Test" <test@domain.tld>',
24             6  => '"Test Test"<test@domain.tld>',
25             7  => '"Test \\" Test" <test@domain.tld>',
26             8  => '"Test<Test" <test@domain.tld>',
27             9  => '=?ISO-8859-1?B?VGVzdAo=?= <test@domain.tld>',
28             10 => '=?ISO-8859-1?B?VGVzdAo=?=<test@domain.tld>', // #1487068
29             // comments in address (#1487673)
30             11 => 'Test (comment) <test@domain.tld>',
31             12 => '"Test" (comment) <test@domain.tld>',
32             13 => '"Test (comment)" (comment) <test@domain.tld>',
33             14 => '(comment) <test@domain.tld>',
34             15 => 'Test <test@(comment)domain.tld>',
35             16 => 'Test Test ((comment)) <test@domain.tld>',
36             17 => 'test@domain.tld (comment)',
37             18 => '"Test,Test" <test@domain.tld>',
38             // 1487939
39             19 => 'Test <"test test"@domain.tld>',
40             20 => '<"test test"@domain.tld>',
41             21 => '"test test"@domain.tld',
42             // invalid (#1489092)
43             22 => '"John Doe @ SomeBusinessName" <MAILER-DAEMON>',
44             23 => '=?UTF-8?B?IlRlc3QsVGVzdCI=?= <test@domain.tld>',
55d9d5 45             // invalid, but we do our best to parse correctly
AM 46             24 => '"email@test.com" <>',
dbf70c 47             // valid with redundant quoting (#1490040)
AM 48             25 => '"user"@"domain.tld"',
b37954 49         );
AM 50
51         $results = array(
52             0  => array(1, '', 'test@domain.tld'),
53             1  => array(1, '', 'test@domain.tld'),
54             2  => array(1, 'Test', 'test@domain.tld'),
55             3  => array(1, 'Test Test', 'test@domain.tld'),
56             4  => array(1, 'Test Test', 'test@domain.tld'),
57             5  => array(1, 'Test Test', 'test@domain.tld'),
58             6  => array(1, 'Test Test', 'test@domain.tld'),
59             7  => array(1, 'Test " Test', 'test@domain.tld'),
60             8  => array(1, 'Test<Test', 'test@domain.tld'),
61             9  => array(1, 'Test', 'test@domain.tld'),
62             10 => array(1, 'Test', 'test@domain.tld'),
63             11 => array(1, 'Test', 'test@domain.tld'),
64             12 => array(1, 'Test', 'test@domain.tld'),
65             13 => array(1, 'Test (comment)', 'test@domain.tld'),
66             14 => array(1, '', 'test@domain.tld'),
67             15 => array(1, 'Test', 'test@domain.tld'),
68             16 => array(1, 'Test Test', 'test@domain.tld'),
69             17 => array(1, '', 'test@domain.tld'),
70             18 => array(1, 'Test,Test', 'test@domain.tld'),
71             19 => array(1, 'Test', '"test test"@domain.tld'),
72             20 => array(1, '', '"test test"@domain.tld'),
73             21 => array(1, '', '"test test"@domain.tld'),
74             // invalid (#1489092)
75             22 => array(1, 'John Doe @ SomeBusinessName', 'MAILER-DAEMON'),
76             23 => array(1, 'Test,Test', 'test@domain.tld'),
55d9d5 77             24 => array(1, '', 'email@test.com'),
dbf70c 78             25 => array(1, '', 'user@domain.tld'),
b37954 79         );
AM 80
81         foreach ($headers as $idx => $header) {
82             $res = rcube_mime::decode_address_list($header);
83
84             $this->assertEquals($results[$idx][0], count($res), "Rows number in result for header: " . $header);
85             $this->assertEquals($results[$idx][1], $res[1]['name'], "Name part decoding for header: " . $header);
86             $this->assertEquals($results[$idx][2], $res[1]['mailto'], "Email part decoding for header: " . $header);
87         }
88     }
89
90     /**
91      * Test decoding of header values
92      * Uses rcube_mime::decode_mime_string()
93      */
94     function test_header_decode_qp()
95     {
96         $test = array(
97             // #1488232: invalid character "?"
98             'quoted-printable (1)' => array(
99                 'in'  => '=?utf-8?Q?Certifica=C3=A7=C3=A3??=',
100                 'out' => 'Certifica=C3=A7=C3=A3?',
101             ),
102             'quoted-printable (2)' => array(
103                 'in'  => '=?utf-8?Q?Certifica=?= =?utf-8?Q?C3=A7=C3=A3?=',
104                 'out' => 'Certifica=C3=A7=C3=A3',
105             ),
106             'quoted-printable (3)' => array(
107                 'in'  => '=?utf-8?Q??= =?utf-8?Q??=',
108                 'out' => '',
109             ),
110             'quoted-printable (4)' => array(
111                 'in'  => '=?utf-8?Q??= a =?utf-8?Q??=',
112                 'out' => ' a ',
113             ),
114             'quoted-printable (5)' => array(
115                 'in'  => '=?utf-8?Q?a?= =?utf-8?Q?b?=',
116                 'out' => 'ab',
117             ),
118             'quoted-printable (6)' => array(
119                 'in'  => '=?utf-8?Q?   ?= =?utf-8?Q?a?=',
120                 'out' => '   a',
121             ),
122             'quoted-printable (7)' => array(
123                 'in'  => '=?utf-8?Q?___?= =?utf-8?Q?a?=',
124                 'out' => '   a',
125             ),
126         );
127
128         foreach ($test as $idx => $item) {
129             $res = rcube_mime::decode_mime_string($item['in'], 'UTF-8');
130             $res = quoted_printable_encode($res);
131
132             $this->assertEquals($item['out'], $res, "Header decoding for: " . $idx);
133         }
134     }
135
136     /**
137      * Test format=flowed unfolding
138      */
139     function test_format_flowed()
140     {
141         $raw = file_get_contents(TESTS_DIR . 'src/format-flowed-unfolded.txt');
142         $flowed = file_get_contents(TESTS_DIR . 'src/format-flowed.txt');
143
144         $this->assertEquals($flowed, rcube_mime::format_flowed($raw, 80), "Test correct folding and space-stuffing");
145     }
146
147     /**
148      * Test format=flowed unfolding
149      */
150     function test_unfold_flowed()
151     {
152         $flowed = file_get_contents(TESTS_DIR . 'src/format-flowed.txt');
153         $unfolded = file_get_contents(TESTS_DIR . 'src/format-flowed-unfolded.txt');
154
155         $this->assertEquals($unfolded, rcube_mime::unfold_flowed($flowed), "Test correct unfolding of quoted lines");
156     }
157
158     /**
ac9392 159      * Test format=flowed unfolding (#1490284)
AM 160      */
161     function test_unfold_flowed2()
162     {
163         $flowed   = "> culpa qui officia deserunt mollit anim id est laborum.\r\n"
164                     ."> \r\n"
165                     ."Sed ut perspiciatis unde omnis iste natus error \r\nsit voluptatem";
166         $unfolded = "> culpa qui officia deserunt mollit anim id est laborum.\r\n"
167                     ."> \r\n"
168                     ."Sed ut perspiciatis unde omnis iste natus error sit voluptatem";
169
170         $this->assertEquals($unfolded, rcube_mime::unfold_flowed($flowed), "Test correct unfolding of quoted lines [2]");
171     }
172
173     /**
b37954 174      * Test wordwrap()
AM 175      */
176     function test_wordwrap()
177     {
178         $samples = array(
179             array(
180                 array("aaaa aaaa\n           aaaa"),
181                 "aaaa aaaa\n           aaaa",
182             ),
183             array(
184                 array("123456789 123456789 123456789 123", 29),
185                 "123456789 123456789 123456789\n123",
186             ),
187             array(
188                 array("123456789   3456789 123456789", 29),
189                 "123456789   3456789 123456789",
190             ),
191             array(
192                 array("123456789 123456789 123456789   123", 29),
193                 "123456789 123456789 123456789\n  123",
194             ),
195             array(
196                 array("abc", 1, "\n", true),
197                 "a\nb\nc",
198             ),
199             array(
200                 array("ąść", 1, "\n", true, 'UTF-8'),
201                 "ą\nś\nć",
202             ),
203             array(
204                 array(">abc\n>def", 2, "\n", true),
205                 ">abc\n>def",
206             ),
207             array(
208                 array("abc def", 3, "-"),
209                 "abc-def",
210             ),
211             array(
212                 array("----------------------------------------------------------------------------------------\nabc                        def123456789012345", 76),
213                 "----------------------------------------------------------------------------------------\nabc                        def123456789012345",
214             ),
215             array(
216                 array("-------\nabc def", 5),
217                 "-------\nabc\ndef",
218             ),
219             array(
220                 array("http://xx.xxx.xx.xxx:8080/addressbooks/roundcubexxxxx%40xxxxxxxxxxxxxxxxxxxxxxx.xx.xx/testing/", 70),
221                 "http://xx.xxx.xx.xxx:8080/addressbooks/roundcubexxxxx%40xxxxxxxxxxxxxxxxxxxxxxx.xx.xx/testing/",
222             ),
223             array(
224                 array("this-is-just-some-blabla-to-make-this-more-than-seventy-five-characters-in-a-row -- this line should be wrapped", 20, "\n"),
225                 "this-is-just-some-blabla-to-make-this-more-than-seventy-five-characters-in-a-row\n-- this line should\nbe wrapped",
226             ),
227         );
228
229         foreach ($samples as $sample) {
230             $this->assertEquals($sample[1], call_user_func_array(array('rcube_mime', 'wordwrap'), $sample[0]), "Test text wrapping");
231         }
232     }
233
a7e552 234     /**
AM 235      * Test parse_message()
236      */
237     function test_parse_message()
238     {
239         $file   = file_get_contents(__DIR__ . '/../src/html.msg');
240         $result = rcube_mime::parse_message($file);
241
242         $this->assertInstanceOf('rcube_message_part', $result);
243         $this->assertSame('multipart/alternative', $result->mimetype);
244         $this->assertSame('1.0', $result->headers['mime-version']);
245         $this->assertSame('=_68eeaf4ab95b5312965e45c33362338e', $result->ctype_parameters['boundary']);
246         $this->assertSame('1',              $result->parts[0]->mime_id);
247         $this->assertSame(12,               $result->parts[0]->size);
248         $this->assertSame('text/plain',     $result->parts[0]->mimetype);
249         $this->assertSame("this is test",   $result->parts[0]->body);
250         $this->assertSame('2',              $result->parts[1]->mime_id);
251         $this->assertSame(0,                $result->parts[1]->size);
252         $this->assertSame('multipart/related', $result->parts[1]->mimetype);
253         $this->assertCount(2,               $result->parts[1]->parts);
254         $this->assertSame('2.1',            $result->parts[1]->parts[0]->mime_id);
255         $this->assertSame(257,              $result->parts[1]->parts[0]->size);
256         $this->assertSame('text/html',      $result->parts[1]->parts[0]->mimetype);
257         $this->assertSame('UTF-8',          $result->parts[1]->parts[0]->charset);
258         $this->assertRegExp('/<html>/',     $result->parts[1]->parts[0]->body);
259         $this->assertSame('2.2',            $result->parts[1]->parts[1]->mime_id);
260         $this->assertSame(793,              $result->parts[1]->parts[1]->size);
261         $this->assertSame('image/jpeg',     $result->parts[1]->parts[1]->mimetype);
262         $this->assertSame('base64',          $result->parts[1]->parts[1]->encoding);
263         $this->assertSame('inline',          $result->parts[1]->parts[1]->disposition);
264         $this->assertSame('photo-mini.jpg', $result->parts[1]->parts[1]->filename);
265     }
b37954 266 }