1<?php
2/**
3 * Copyright 2015-2017 Horde LLC (http://www.horde.org/)
4 *
5 * @category   Horde
6 * @copyright  2015-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_Headers_ContentParam_ContentType class.
14 *
15 * @author     Michael Slusarz <slusarz@horde.org>
16 * @category   Horde
17 * @copyright  2015-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_Headers_ContentTypeTest
24extends PHPUnit_Framework_TestCase
25{
26    /**
27     * @dataProvider parsingOfInputProvider
28     */
29    public function testParsingOfInput($input, $expected_val, $expected_params)
30    {
31        $ob = new Horde_Mime_Headers_ContentParam_ContentType(
32            'Content-Type',
33            $input
34        );
35
36        $this->assertEquals(
37            $expected_val,
38            $ob->value
39        );
40
41        $params = $ob->params;
42        ksort($params);
43
44        $this->assertEquals(
45            $expected_params,
46            $params
47        );
48    }
49
50    public function parsingOfInputProvider()
51    {
52        return array(
53            array(
54                'text/plain',
55                'text/plain',
56                array()
57            ),
58            array(
59                '    TEXT/PLAIN',
60                'text/plain',
61                array()
62            ),
63            array(
64                ' modEL/hTmL   ',
65                'model/html',
66                array()
67            ),
68            array(
69                'bogus/foo',
70                'x-bogus/foo',
71                array()
72            ),
73            array(
74                " message/RFC822   ;   Filename=\"foo\";\n BAR=33 ; foo  = 22",
75                'message/rfc822',
76                array(
77                    'bar' => '33',
78                    'filename' => 'foo',
79                    'foo' => '22'
80                )
81            ),
82            array(
83                'foo',
84                Horde_Mime_Headers_ContentParam_ContentType::DEFAULT_CONTENT_TYPE,
85                array()
86            )
87        );
88    }
89
90    public function testClone()
91    {
92        $ob = new Horde_Mime_Headers_ContentParam_ContentType(
93            'Content-Type',
94            'text/plain; foo=bar;'
95        );
96
97        $ob2 = clone $ob;
98
99        $ob->setContentParamValue('image/jpeg');
100        $ob['foo'] = 123;
101
102        $this->assertEquals(
103            'text/plain',
104            $ob2->value
105        );
106        $this->assertEquals(
107            array('foo' => 'bar'),
108            $ob2->params
109        );
110    }
111
112    public function testSerialize()
113    {
114        $ob = new Horde_Mime_Headers_ContentParam_ContentType(
115            'Content-Type',
116            'text/plain; foo=bar;'
117        );
118
119        $ob2 = unserialize(serialize($ob));
120
121        $this->assertEquals(
122            'text/plain',
123            $ob2->value
124        );
125        $this->assertEquals(
126            array('foo' => 'bar'),
127            $ob2->params
128        );
129    }
130
131    public function testStaticCreateMethod()
132    {
133        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
134
135        $this->assertEquals(
136            $ob::DEFAULT_CONTENT_TYPE,
137            $ob->value
138        );
139
140        $this->assertEmpty($ob->params);
141    }
142
143    public function testParamsReturnsACopyOfArray()
144    {
145        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
146        $ob['foo'] = 'bar';
147
148        $params = $ob->params;
149
150        $params['foo'] = '123';
151
152        $this->assertEquals(
153            array('foo' => 'bar'),
154            $ob->params
155        );
156    }
157
158    /**
159     * @dataProvider parsingContentTypeValueProvider
160     */
161    public function testParsingContentTypeValue($value, $primary, $sub)
162    {
163        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
164        $ob->setContentParamValue($value);
165
166        $this->assertEquals(
167            $primary,
168            $ob->ptype
169        );
170        $this->assertEquals(
171            $sub,
172            $ob->stype
173        );
174    }
175
176    public function parsingContentTypeValueProvider()
177    {
178        return array(
179            array(
180                'text/plain',
181                'text',
182                'plain'
183            ),
184            array(
185                'TEXT/HTML',
186                'text',
187                'html'
188            ),
189            array(
190                'foo/bar',
191                'x-foo',
192                'bar'
193            ),
194            array(
195                'text/plain; charset=utf-8',
196                'text',
197                'plain'
198            )
199        );
200    }
201
202    /**
203     * @dataProvider typeCharsetPropertyProvider
204     */
205    public function testTypeCharsetProperty($value, $charset, $expected)
206    {
207        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
208        $ob->setContentParamValue($value);
209        $ob['charset'] = $charset;
210
211        $this->assertEquals(
212            $expected,
213            $ob->type_charset
214        );
215    }
216
217    public function typeCharsetPropertyProvider()
218    {
219        return array(
220            array(
221                'text/plain',
222                'utf-8',
223                'text/plain; charset=utf-8'
224            ),
225            array(
226                'text/html',
227                'utf-8',
228                'text/html; charset=utf-8'
229            ),
230            array(
231                'image/jpeg',
232                'utf-8',
233                'image/jpeg'
234            )
235        );
236    }
237
238    /**
239     * @dataProvider multipartPartsHaveBoundary
240     */
241    public function testMultipartPartsHaveBoundary($value, $has_boundary)
242    {
243        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
244        $ob->setContentParamValue($value);
245
246        if ($has_boundary) {
247            $this->assertArrayHasKey('boundary', $ob->params);
248        } else {
249            $this->assertArrayNotHasKey('boundary', $ob->params);
250        }
251    }
252
253    public function multipartPartsHaveBoundary()
254    {
255        return array(
256            array(
257                'text/plain',
258                false
259            ),
260            array(
261                'image/jpeg',
262                false
263            ),
264            array(
265                'multipart/mixed',
266                true
267            )
268        );
269    }
270
271    /**
272     * @dataProvider charsetIsLowercaseProvider
273     */
274    public function testCharsetIsLowercase($charset, $expected)
275    {
276        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
277        $ob->setContentParamValue('text/plain');
278        $ob['charset'] = $charset;
279
280        $this->assertEquals(
281            $expected,
282            $ob['charset']
283        );
284    }
285
286    public function charsetIsLowercaseProvider()
287    {
288        return array(
289            array(
290                'utf-8',
291                'utf-8'
292            ),
293            array(
294                'ISO-8859-1',
295                'iso-8859-1'
296            ),
297            array(
298                'US-ASCII',
299                null
300            )
301        );
302    }
303
304    public function testMultipartCantUnsetBoundary()
305    {
306        $ob = Horde_Mime_Headers_ContentParam_ContentType::create();
307        $ob->setContentParamValue('multipart/mixed');
308
309        $this->assertNotEmpty($ob['boundary']);
310
311        unset($ob['boundary']);
312
313        $this->assertNotEmpty($ob['boundary']);
314    }
315
316    /**
317     * @dataProvider isDefaultProvider
318     */
319    public function testIsDefault($value, $is_default)
320    {
321        $ob = new Horde_Mime_Headers_ContentParam_ContentType(
322            'Content-Type',
323            $value
324        );
325
326        if ($is_default) {
327            $this->assertTrue($ob->isDefault());
328        } else {
329            $this->assertFalse($ob->isDefault());
330        }
331    }
332
333    public function isDefaultProvider()
334    {
335        return array(
336            array(
337                'text/plain',
338                true
339            ),
340            array(
341                'text/plain; charset=us-ascii',
342                true
343            ),
344            array(
345                'text/plain; charset=us-ascii; foo=bar',
346                false
347            ),
348            array(
349                'text/plain; charset=utf-8',
350                false
351            ),
352            array(
353                'text/html',
354                false
355            ),
356            array(
357                'text/html; charset=us-ascii',
358                false
359            ),
360            array(
361                'image/jpeg',
362                false
363            ),
364            array(
365                'image/jpeg; charset=utf-8',
366                false
367            )
368        );
369    }
370
371}
372