~ubuntu-branches/ubuntu/trusty/php-mime-type/trusty

« back to all changes in this revision

Viewing changes to MIME_Type-1.3.1/tests/TypeTest.php

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2012-02-26 14:49:59 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120226144959-15su7r6j5injbj2g
Tags: 1.3.1-1
* New upstream version.
* Added a debian/gbp.conf.
* Switching to source format 3.0 (quilt).
* Switching to pkg-php-tools and dh 8 sequencer.
* Rewrote debian/copyright using machine-readable format, now using the LGPL
version 3 (upstream and maintainer relicense).
* Standards-Version: 3.9.3 (no change but the above).
* Fixed VCS fields (Closes: #638523).
* Now using PKG-PHP-PEAR team as maintainer.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
require_once 'MIME/Type.php';
 
3
 
 
4
class MIME_TypeTest extends PHPUnit_Framework_TestCase
 
5
{
 
6
    public function testParseParameterPearError()
 
7
    {
 
8
        $mt = new MIME_Type();
 
9
        $mt->parse(new PEAR_Error('this is an error'));
 
10
        $this->assertEquals('', $mt->media);
 
11
    }
 
12
 
 
13
    public function testParse()
 
14
    {
 
15
        $mt = new MIME_Type();
 
16
        $mt->parse('application/ogg;description=Hello there!;asd=fgh');
 
17
        $this->assertEquals('application', $mt->media);
 
18
        $this->assertEquals('ogg'        , $mt->subType);
 
19
 
 
20
        $params = array(
 
21
            'description' => array('Hello there!', ''),
 
22
            'asd' => array('fgh', '')
 
23
        );
 
24
        $this->assertEquals(2, count($mt->parameters));
 
25
        foreach ($params as $name => $param) {
 
26
            $this->assertTrue(isset($mt->parameters[$name]));
 
27
            $this->assertInstanceOf('MIME_Type_Parameter', $mt->parameters[$name]);
 
28
            $this->assertEquals($name,     $mt->parameters[$name]->name);
 
29
            $this->assertEquals($param[0], $mt->parameters[$name]->value);
 
30
            $this->assertEquals($param[1], $mt->parameters[$name]->comment);
 
31
        }
 
32
    }
 
33
 
 
34
    public function testParseAgain()
 
35
    {
 
36
        $mt = new MIME_Type();
 
37
        $mt->parse('application/ogg;description=Hello there!;asd=fgh');
 
38
        $this->assertEquals(2, count($mt->parameters));
 
39
 
 
40
        $mt->parse('text/plain;hello=there!');
 
41
        $this->assertEquals(1, count($mt->parameters));
 
42
    }
 
43
 
 
44
    public function testHasParameters()
 
45
    {
 
46
        $this->assertFalse(MIME_Type::hasParameters('text/plain'));
 
47
        $this->assertFalse(MIME_Type::hasParameters('text/*'));
 
48
        $this->assertFalse(MIME_Type::hasParameters('*/*'));
 
49
        $this->assertTrue(MIME_Type::hasParameters('text/xml;description=test'));
 
50
        $this->assertTrue(MIME_Type::hasParameters('text/xml;one=test;two=three'));
 
51
    }
 
52
 
 
53
    public function testGetParameters()
 
54
    {
 
55
        $this->assertEquals(
 
56
            array(),
 
57
            MIME_Type::getParameters('text/plain')
 
58
        );
 
59
        //rest is tested in testParse()
 
60
    }
 
61
 
 
62
    public function testStripParameters()
 
63
    {
 
64
        $this->assertEquals(
 
65
            'text/plain',
 
66
            MIME_Type::stripParameters('text/plain')
 
67
        );
 
68
        $this->assertEquals(
 
69
            'text/plain',
 
70
            MIME_Type::stripParameters('text/plain;asd=def')
 
71
        );
 
72
        $this->assertEquals(
 
73
            'text/plain',
 
74
            MIME_Type::stripParameters('text/plain;asd=def;ghj=jkl')
 
75
        );
 
76
    }
 
77
 
 
78
    public function testStripComments()
 
79
    {
 
80
        $this->assertEquals('def', MIME_Type::stripComments('(abc)def(ghi)', $null));
 
81
        $this->assertEquals('def', MIME_Type::stripComments('(abc)def', $null));
 
82
        $this->assertEquals('def', MIME_Type::stripComments('def(ghi)', $null));
 
83
    }
 
84
 
 
85
    public function testStripCommentsEscaped()
 
86
    {
 
87
        $comment = '';
 
88
        $this->assertEquals(
 
89
            'def', MIME_Type::stripComments('(\)abc)def(\))', $comment)
 
90
        );
 
91
        $this->assertEquals(')abc )', $comment);
 
92
    }
 
93
 
 
94
    public function testStripCommentsEscapedString()
 
95
    {
 
96
        $comment = false;
 
97
        $this->assertEquals(
 
98
            'foo', MIME_Type::stripComments('\\foo(abc)', $comment)
 
99
        );
 
100
        $this->assertEquals('abc', $comment);
 
101
    }
 
102
 
 
103
    public function testStripCommentsQuoted()
 
104
    {
 
105
        $this->assertEquals('def', MIME_Type::stripComments('(a"bc)def")def', $null));
 
106
        $this->assertEquals('(abc)def', MIME_Type::stripComments('"(abc)def"', $null));
 
107
    }
 
108
 
 
109
    public function testStripCommentsParameterComment()
 
110
    {
 
111
        $comment = '';
 
112
        $this->assertEquals(
 
113
            'def',
 
114
            MIME_Type::stripComments('(abc)def(ghi)', $comment)
 
115
        );
 
116
        $this->assertEquals('abc ghi', $comment);
 
117
    }
 
118
 
 
119
    public function testGetMedia()
 
120
    {
 
121
        $this->assertEquals('text', MIME_Type::getMedia('text/plain'));
 
122
        $this->assertEquals('application', MIME_Type::getMedia('application/ogg'));
 
123
        $this->assertEquals('*', MIME_Type::getMedia('*/*'));
 
124
    }
 
125
 
 
126
    public function testGetSubType()
 
127
    {
 
128
        $this->assertEquals('plain', MIME_Type::getSubType('text/plain'));
 
129
        $this->assertEquals('ogg', MIME_Type::getSubType('application/ogg'));
 
130
        $this->assertEquals('*', MIME_Type::getSubType('*/*'));
 
131
        $this->assertEquals('plain', MIME_Type::getSubType('text/plain;a=b'));
 
132
    }
 
133
 
 
134
    public function testGet()
 
135
    {
 
136
        $mt = new MIME_Type('text/xml');
 
137
        $this->assertEquals('text/xml', $mt->get());
 
138
 
 
139
        $mt = new MIME_Type('text/xml; this="is"; a="parameter" (with a comment)');
 
140
        $this->assertEquals(
 
141
            'text/xml; this="is"; a="parameter" (with a comment)',
 
142
            $mt->get()
 
143
        );
 
144
    }
 
145
 
 
146
    public function testIsExperimental()
 
147
    {
 
148
        $this->assertTrue(MIME_Type::isExperimental('text/x-test'));
 
149
        $this->assertTrue(MIME_Type::isExperimental('image/X-test'));
 
150
        $this->assertFalse(MIME_Type::isExperimental('text/plain'));
 
151
    }
 
152
 
 
153
    public function testIsVendor()
 
154
    {
 
155
        $this->assertTrue(MIME_Type::isVendor('application/vnd.openoffice'));
 
156
        $this->assertFalse(MIME_Type::isVendor('application/vendor.openoffice'));
 
157
        $this->assertFalse(MIME_Type::isVendor('vnd/fsck'));
 
158
    }
 
159
 
 
160
    public function testIsWildcard()
 
161
    {
 
162
        $this->assertTrue(MIME_Type::isWildcard('*/*'));
 
163
        $this->assertTrue(MIME_Type::isWildcard('image/*'));
 
164
        $this->assertFalse(MIME_Type::isWildcard('text/plain'));
 
165
    }
 
166
 
 
167
    public function testWildcardMatch() {
 
168
        $this->assertTrue(MIME_Type::wildcardMatch('*/*', 'image/png'));
 
169
        $this->assertTrue(MIME_Type::wildcardMatch('image/*', 'image/png'));
 
170
        $this->assertFalse(MIME_Type::wildcardMatch('image/*', 'text/plain'));
 
171
    }
 
172
 
 
173
    public function testWildcardMatchNoWildcard()
 
174
    {
 
175
        $this->assertFalse(MIME_Type::wildcardMatch('image/foo', 'image/png'));
 
176
    }
 
177
 
 
178
    public function testAddParameter()
 
179
    {
 
180
        $mt = new MIME_Type('image/png; foo=bar');
 
181
        $mt->addParameter('baz', 'val', 'this is a comment');
 
182
        $res = $mt->get();
 
183
        $this->assertContains('foo=', $res);
 
184
        $this->assertContains('bar', $res);
 
185
 
 
186
        $this->assertContains('baz=', $res);
 
187
        $this->assertContains('val', $res);
 
188
        $this->assertContains('(this is a comment)', $res);
 
189
    }
 
190
 
 
191
    public function testRemoveParameter()
 
192
    {
 
193
        $mt = new MIME_Type('image/png; foo=bar');
 
194
        $mt->addParameter('baz', 'val', 'this is a comment');
 
195
        $mt->removeParameter('foo');
 
196
        $res = $mt->get();
 
197
        $this->assertNotContains('foo=', $res);
 
198
        $this->assertNotContains('bar', $res);
 
199
        $this->assertContains('baz=', $res);
 
200
    }
 
201
 
 
202
    public function testAutoDetect()
 
203
    {
 
204
        $dir = dirname(__FILE__) . '/files/';
 
205
 
 
206
        $mt = new MIME_Type(
 
207
            MIME_Type::autoDetect($dir . 'example.png')
 
208
        );
 
209
        $this->assertInstanceOf('MIME_Type', $mt);
 
210
        $this->assertEquals('image', $mt->media);
 
211
        $this->assertEquals('png', $mt->subType);
 
212
 
 
213
        $mt = new MIME_Type(
 
214
            MIME_Type::autoDetect($dir . 'example.jpg')
 
215
        );
 
216
        $this->assertInstanceOf('MIME_Type', $mt);
 
217
        $this->assertEquals('image', $mt->media);
 
218
        $this->assertEquals('jpeg', $mt->subType);
 
219
    }
 
220
 
 
221
    public function testAutoDetectNonexistingFile()
 
222
    {
 
223
        $res = MIME_Type::autoDetect('/this/file/does/not/exist');
 
224
        $this->assertInstanceOf('PEAR_Error', $res);
 
225
        $this->assertContains('doesn\'t exist', $res->getMessage());
 
226
    }
 
227
 
 
228
    public function testAutoDetectFinfo()
 
229
    {
 
230
        $mt = new MIME_Type();
 
231
        $mt->useMimeContentType = false;
 
232
        $mt->useFileCmd = false;
 
233
        $mt->useExtension = false;
 
234
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg');
 
235
        $this->assertNotInstanceOf('PEAR_Error', $type);
 
236
        $this->assertEquals('image', $mt->media);
 
237
        $this->assertEquals('jpeg', $mt->subType);
 
238
    }
 
239
 
 
240
    public function testAutoDetectFinfoMagic()
 
241
    {
 
242
        $mt = new MIME_Type();
 
243
        $mt->magicFile = dirname(__FILE__) . '/TypeTest.magic';
 
244
        $mt->useMimeContentType = false;
 
245
        $mt->useFileCmd = false;
 
246
        $mt->useExtension = false;
 
247
 
 
248
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php');
 
249
        $this->assertNotInstanceOf('PEAR_Error', $type);
 
250
        $this->assertEquals('text', $mt->media);
 
251
        $this->assertEquals('x-unittest', $mt->subType);
 
252
    }
 
253
 
 
254
    public function testAutoDetectFinfoNonExistingMagic()
 
255
    {
 
256
        $mt = new MIME_Type();
 
257
        $mt->magicFile = dirname(__FILE__) . '/magicdoesnotexist';
 
258
        $mt->useMimeContentType = false;
 
259
        $mt->useFileCmd = false;
 
260
        $mt->useExtension = false;
 
261
 
 
262
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php');
 
263
        $this->assertInstanceOf('PEAR_Error', $type);
 
264
    }
 
265
 
 
266
    public function testAutoDetectMimeContentType()
 
267
    {
 
268
        $mt = new MIME_Type();
 
269
        $mt->useFinfo = false;
 
270
        $mt->useFileCmd = false;
 
271
        $mt->useExtension = false;
 
272
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg');
 
273
        $this->assertEquals('image', $mt->media);
 
274
        $this->assertEquals('jpeg', $mt->subType);
 
275
    }
 
276
 
 
277
    public function testAutoDetectFileCommand()
 
278
    {
 
279
        $mt = new MIME_Type();
 
280
        $mt->useFinfo = false;
 
281
        $mt->useMimeContentType = false;
 
282
        $mt->useExtension = false;
 
283
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg');
 
284
        $this->assertEquals('image', $mt->media);
 
285
        $this->assertEquals('jpeg', $mt->subType);
 
286
    }
 
287
 
 
288
    public function testAutoDetectFileCommandMagic()
 
289
    {
 
290
        $mt = new MIME_Type();
 
291
        $mt->magicFile = dirname(__FILE__) . '/TypeTest.magic';
 
292
        $mt->useFinfo = false;
 
293
        $mt->useMimeContentType = false;
 
294
        $mt->useExtension = false;
 
295
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php');
 
296
        $this->assertEquals('text', $mt->media);
 
297
        $this->assertEquals('x-unittest', $mt->subType);
 
298
    }
 
299
 
 
300
    public function testAutoDetectExtension()
 
301
    {
 
302
        $mt = new MIME_Type();
 
303
        $mt->useFinfo = false;
 
304
        $mt->useMimeContentType = false;
 
305
        $mt->useFileCmd = false;
 
306
        $type = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg');
 
307
        $this->assertEquals('image', $mt->media);
 
308
        $this->assertEquals('jpeg', $mt->subType);
 
309
    }
 
310
 
 
311
    public function testAutoDetectError()
 
312
    {
 
313
        $mt = new MIME_Type();
 
314
        $mt->useFinfo = false;
 
315
        $mt->useMimeContentType = false;
 
316
        $mt->useFileCmd = false;
 
317
        $mt->useExtension = false;
 
318
 
 
319
        $res = $mt->autoDetect(dirname(__FILE__) . '/files/example.jpg');
 
320
        $this->assertInstanceOf('PEAR_Error', $res);
 
321
        $this->assertEquals('', $mt->media);
 
322
        $this->assertEquals('', $mt->subType);
 
323
    }
 
324
 
 
325
    public function test_fileAutoDetectNoFileCommand()
 
326
    {
 
327
        $cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
 
328
        $cmd = 'thiscommanddoesnotexist';
 
329
 
 
330
        require_once 'System/Command.php';
 
331
        $res = MIME_Type::_fileAutoDetect(dirname(__FILE__) . '/files/example.jpg');
 
332
        $this->assertInstanceOf('PEAR_Error', $res);
 
333
        $this->assertContains('thiscommanddoesnotexist', $res->getMessage());
 
334
    }
 
335
 
 
336
    public function testComments()
 
337
    {
 
338
        $type = new MIME_Type('(UTF-8 Plain Text) text / plain ; charset = utf-8');
 
339
        $this->assertEquals(
 
340
            'text/plain; charset="utf-8"', $type->get()
 
341
        );
 
342
 
 
343
        $type = new MIME_Type('text (Text) / plain ; charset = utf-8');
 
344
        $this->assertEquals(
 
345
            'text/plain; charset="utf-8"', $type->get()
 
346
        );
 
347
 
 
348
        $type = new MIME_Type('text / (Plain) plain ; charset = utf-8');
 
349
        $this->assertEquals(
 
350
            'text/plain; charset="utf-8"', $type->get()
 
351
        );
 
352
 
 
353
        $type = new MIME_Type('text / plain (Plain Text) ; charset = utf-8');
 
354
        $this->assertEquals(
 
355
            'text/plain; charset="utf-8"', $type->get()
 
356
        );
 
357
 
 
358
        $type = new MIME_Type('text / plain ; (Charset=utf-8) charset = utf-8');
 
359
        $this->assertEquals(
 
360
            'text/plain; charset="utf-8" (Charset=utf-8)', $type->get()
 
361
        );
 
362
 
 
363
        $type = new MIME_Type('text / plain ; charset (Charset) = utf-8');
 
364
        $this->assertEquals(
 
365
            'text/plain; charset="utf-8" (Charset)', $type->get()
 
366
        );
 
367
 
 
368
        $type = new MIME_Type('text / plain ; charset = (UTF8) utf-8');
 
369
        $this->assertEquals(
 
370
            'text/plain; charset="utf-8" (UTF8)', $type->get()
 
371
        );
 
372
 
 
373
        $type = new MIME_Type('text / plain ; charset = utf-8 (UTF-8 Plain Text)');
 
374
        $this->assertEquals(
 
375
            'text/plain; charset="utf-8" (UTF-8 Plain Text)', $type->get()
 
376
        );
 
377
 
 
378
        $type = new MIME_Type('application/x-foobar;description="bbgh(kdur"');
 
379
        $this->assertEquals(
 
380
            'application/x-foobar; description="bbgh(kdur"', $type->get()
 
381
        );
 
382
 
 
383
        $type = new MIME_Type('application/x-foobar;description="a \"quoted string\""');
 
384
        $this->assertEquals(
 
385
            'application/x-foobar; description="a \"quoted string\""', $type->get()
 
386
        );
 
387
 
 
388
    }
 
389
 
 
390
 
 
391
    public function test_handleDetectionParamPearError()
 
392
    {
 
393
        $err = new PEAR_Error('test');
 
394
        $ret = MIME_Type::_handleDetection($err, false);
 
395
        $this->assertInstanceOf('PEAR_Error', $ret);
 
396
    }
 
397
 
 
398
    public function test_handleDetectionEmptyType()
 
399
    {
 
400
        $ret = MIME_Type::_handleDetection('', false);
 
401
        $this->assertInstanceOf('PEAR_Error', $ret);
 
402
 
 
403
        $ret = MIME_Type::_handleDetection(false, false);
 
404
        $this->assertInstanceOf('PEAR_Error', $ret);
 
405
    }
 
406
}
 
407
?>