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_ContentDisposition 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_ContentDispositionTest
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_ContentDisposition(
32            'Content-Disposition',
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                'inline',
55                'inline',
56                array()
57            ),
58            array(
59                '    INLINE',
60                'inline',
61                array()
62            ),
63            array(
64                'attachment',
65                'attachment',
66                array()
67            ),
68            array(
69                ' AtTaChMeNt   ',
70                'attachment',
71                array()
72            ),
73            array(
74                'bogus',
75                '',
76                array()
77            ),
78            array(
79                " iNLINe   ;   filename=\"foo\";\n bar=33;    size = 22",
80                'inline',
81                array(
82                    'bar' => '33',
83                    'filename' => 'foo',
84                    'size' => 22
85                )
86            ),
87            array(
88                "attachMENT;Filename=\"foo\";bar=33;SIZE=\"22\"",
89                'attachment',
90                array(
91                    'bar' => '33',
92                    'filename' => 'foo',
93                    'size' => 22
94                )
95            )
96        );
97    }
98
99    /**
100     * @dataProvider fullValueProvider
101     */
102    public function testFullValue($value, $params, $expected)
103    {
104        $ob = new Horde_Mime_Headers_ContentParam_ContentDisposition(
105            'Content-Disposition',
106            ''
107        );
108
109        $ob->setContentParamValue($value);
110        foreach ($params as $key => $val) {
111            $ob[$key] = $val;
112        }
113
114        $this->assertEquals(
115            $expected,
116            $ob->full_value
117        );
118    }
119
120    public function fullValueProvider()
121    {
122        return array(
123            array(
124                'attachment',
125                array('foo' => 'bar'),
126                'attachment; foo=bar'
127            ),
128            array(
129                'inline',
130                array(
131                    'Foo' => 'BAR',
132                    'BAZ' => 345
133                ),
134                'inline; Foo=BAR; BAZ=345'
135            ),
136            array(
137                '',
138                array(
139                    'Foo' => 'BAR'
140                ),
141                'attachment; Foo=BAR'
142            ),
143            array(
144                'inline; foo=bar',
145                array(),
146                'inline'
147            )
148        );
149    }
150
151    public function testSerialize()
152    {
153        $ob = new Horde_Mime_Headers_ContentParam_ContentDisposition(
154            'Content-Disposition',
155            'inline; foo=bar;'
156        );
157
158        $ob2 = unserialize(serialize($ob));
159
160        $this->assertEquals(
161            'inline',
162            $ob2->value
163        );
164        $this->assertEquals(
165            array('foo' => 'bar'),
166            $ob2->params
167        );
168    }
169
170    public function testClone()
171    {
172        $ob = new Horde_Mime_Headers_ContentParam_ContentDisposition(
173            'Content-Disposition',
174            'inline; foo=bar;'
175        );
176
177        $ob2 = clone $ob;
178
179        $ob->setContentParamValue('attachment');
180        $ob['foo'] = 123;
181
182        $this->assertEquals(
183            'inline',
184            $ob2->value
185        );
186        $this->assertEquals(
187            array('foo' => 'bar'),
188            $ob2->params
189        );
190    }
191
192    /**
193     * @dataProvider isDefaultProvider
194     */
195    public function testIsDefault($value, $is_default)
196    {
197        $ob = new Horde_Mime_Headers_ContentParam_ContentDisposition(
198            'Content-Disposition',
199            $value
200        );
201
202        if ($is_default) {
203            $this->assertTrue($ob->isDefault());
204        } else {
205            $this->assertFalse($ob->isDefault());
206        }
207    }
208
209    public function isDefaultProvider()
210    {
211        return array(
212            array(
213                '',
214                true
215            ),
216            array(
217                'attachment',
218                false
219            ),
220            array(
221                'attachment; foo=bar',
222                false
223            ),
224            array(
225                'inline',
226                false
227            ),
228            array(
229                'inline; foo=bar',
230                false
231            )
232        );
233    }
234
235}
236