1<?php
2/**
3 * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
4 *
5 * @category   Horde
6 * @copyright  2010-2016 Horde LLC
7 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
8 * @package    Mime
9 * @subpackage UnitTests
10 */
11
12/**
13 * Tests for the Horde_Mime class.
14 *
15 * @author     Michael Slusarz <slusarz@horde.org>
16 * @category   Horde
17 * @copyright  2010-2016 Horde LLC
18 * @internal
19 * @license    http://www.horde.org/licenses/lgpl21 LGPL 2.1
20 * @package    Mime
21 * @subpackage UnitTests
22 */
23class Horde_Mime_MimeTest extends PHPUnit_Framework_TestCase
24{
25    /**
26     * @dataProvider is8bitProvider
27     */
28    public function testIs8bit($data, $expected)
29    {
30        $this->assertEquals(
31            $expected,
32            Horde_Mime::is8bit($data)
33        );
34    }
35
36    public function is8bitProvider()
37    {
38        return array(
39            array('A', false),
40            array('a', false),
41            array('1', false),
42            array('!', false),
43            array("\0", false),
44            array("\10", false),
45            array("\127", false),
46            array("\x80", true),
47            array('ä', true),
48            array('A©B', true),
49            array(' ® ', true),
50            // This string is in Windows-1252
51            array(base64_decode('UnVubmVyc5IgQWxlcnQh='), true)
52        );
53    }
54
55    /**
56     * @dataProvider decodeProvider
57     */
58    public function testDecode($data, $expected)
59    {
60        $this->assertEquals(
61            $expected,
62            Horde_Mime::decode($data)
63        );
64    }
65
66    public function decodeProvider()
67    {
68        return array(
69            array(
70                '=?utf-8?Q?_Fran=C3=A7ois_Xavier=2E_XXXXXX_?= <foo@example.com>',
71                ' François Xavier. XXXXXX  <foo@example.com>'
72            ),
73
74            /* Adapted from Dovecot's
75             * src/lib-mail/test-message-header-decode.c. */
76            array(
77                " \t=?utf-8?q?=c3=a4?=  =?utf-8?q?=c3=a4?=  b  \t\r\n ",
78                " \tää  b  \t\r\n "
79            ),
80            array(
81                "a =?utf-8?q?=c3=a4?= b",
82                "a ä b"
83            ),
84            array(
85                "a =?utf-8?q?=c3=a4?=\t\t\r\n =?utf-8?q?=c3=a4?= b",
86                "a ää b"
87            ),
88            array(
89                "a =?utf-8?q?=c3=a4?=  x  =?utf-8?q?=c3=a4?= b",
90                "a ä  x  ä b"
91            ),
92            array(
93                "a =?utf-8?b?w6TDpCDDpA==?= b",
94                "a ää ä b"
95            ), array(
96                "=?utf-8?b?w6Qgw6Q=?=",
97                "ä ä"
98            ),
99
100            /* Not MIME encoded. */
101            array(
102                '=? required=?',
103                '=? required=?'
104            )
105        );
106    }
107
108    /**
109     * @dataProvider encodeProvider
110     */
111    public function testEncode($data, $charset, $expected)
112    {
113        $this->assertEquals(
114            $expected,
115            Horde_Mime::encode($data, $charset)
116        );
117    }
118
119    public function encodeProvider()
120    {
121        return array(
122            /* Adapted from Dovecot's
123             * src/lib-mail/test-message-header-encode.c. */
124            array(
125                'a b',
126                'utf-8',
127                'a b'
128            ),
129            array(
130                'a bcäde f',
131                'utf-8',
132                'a =?utf-8?b?YmPDpGRl?= f'
133            ),
134            array(
135                'a ää ä b',
136                'utf-8',
137                'a =?utf-8?b?w6TDpCDDpA==?= b'
138            ),
139            array(
140                'ä a ä',
141                'utf-8',
142                '=?utf-8?b?w6Q=?= a =?utf-8?b?w6Q=?='
143            ),
144            array(
145                'ää a ä',
146                'utf-8',
147                // Dovecot: '=?utf-8?b?w6TDpCBhIMOk?='
148                '=?utf-8?b?w6TDpA==?= a =?utf-8?b?w6Q=?='
149            ),
150            array(
151                '=',
152                'utf-8',
153                '='
154            ),
155            array(
156                '?',
157                'utf-8',
158                '?'
159            ),
160            array(
161                'a=?',
162                'utf-8',
163                'a=?'
164            ),
165            array(
166                '=?',
167                'utf-8',
168                // Dovecot: '=?utf-8?q?=3D=3F?='
169                '=?utf-8?b?PT8=?='
170            ),
171            array(
172                '=?x',
173                'utf-8',
174                // Dovecot: '=?utf-8?q?=3D=3Fx?='
175                '=?utf-8?b?PT94?='
176            ),
177            array(
178                "a\n=?",
179                'utf-8',
180                // Dovecot: "a\n\t=?utf-8?q?=3D=3F?="
181                "a\n=?utf-8?b?PT8=?="
182            ),
183            array(
184                "a\t=?",
185                'utf-8',
186                // Dovecot: "a\t=?utf-8?q?=3D=3F?="
187                "a\t=?utf-8?b?PT8=?="
188            ),
189            array(
190                "a =?",
191                'utf-8',
192                // Dovecot: "a =?utf-8?q?=3D=3F?="
193                'a =?utf-8?b?PT8=?='
194            ),
195            array(
196                "foo\001bar",
197                'utf-8',
198                // Dovecot: "=?utf-8?q?foo=01bar?="
199                '=?utf-8?b?Zm9vAWJhcg==?='
200            ),
201            array(
202                "\x01\x02\x03\x04\x05\x06\x07\x08",
203                'utf-8',
204                "=?utf-8?b?AQIDBAUGBwg=?="
205            ),
206
207            /* Null character in encode output. */
208            array(
209                "\x00",
210                'UTF-16LE',
211                '=?utf-16le?b?AAA=?='
212            )
213        );
214    }
215
216}
217