2
require_once 'MIME/Type.php';
4
class MIME_TypeTest extends PHPUnit_Framework_TestCase
6
public function testParseParameterPearError()
9
$mt->parse(new PEAR_Error('this is an error'));
10
$this->assertEquals('', $mt->media);
13
public function testParse()
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);
21
'description' => array('Hello there!', ''),
22
'asd' => array('fgh', '')
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);
34
public function testParseAgain()
36
$mt = new MIME_Type();
37
$mt->parse('application/ogg;description=Hello there!;asd=fgh');
38
$this->assertEquals(2, count($mt->parameters));
40
$mt->parse('text/plain;hello=there!');
41
$this->assertEquals(1, count($mt->parameters));
44
public function testHasParameters()
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'));
53
public function testGetParameters()
57
MIME_Type::getParameters('text/plain')
59
//rest is tested in testParse()
62
public function testStripParameters()
66
MIME_Type::stripParameters('text/plain')
70
MIME_Type::stripParameters('text/plain;asd=def')
74
MIME_Type::stripParameters('text/plain;asd=def;ghj=jkl')
78
public function testStripComments()
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));
85
public function testStripCommentsEscaped()
89
'def', MIME_Type::stripComments('(\)abc)def(\))', $comment)
91
$this->assertEquals(')abc )', $comment);
94
public function testStripCommentsEscapedString()
98
'foo', MIME_Type::stripComments('\\foo(abc)', $comment)
100
$this->assertEquals('abc', $comment);
103
public function testStripCommentsQuoted()
105
$this->assertEquals('def', MIME_Type::stripComments('(a"bc)def")def', $null));
106
$this->assertEquals('(abc)def', MIME_Type::stripComments('"(abc)def"', $null));
109
public function testStripCommentsParameterComment()
114
MIME_Type::stripComments('(abc)def(ghi)', $comment)
116
$this->assertEquals('abc ghi', $comment);
119
public function testGetMedia()
121
$this->assertEquals('text', MIME_Type::getMedia('text/plain'));
122
$this->assertEquals('application', MIME_Type::getMedia('application/ogg'));
123
$this->assertEquals('*', MIME_Type::getMedia('*/*'));
126
public function testGetSubType()
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'));
134
public function testGet()
136
$mt = new MIME_Type('text/xml');
137
$this->assertEquals('text/xml', $mt->get());
139
$mt = new MIME_Type('text/xml; this="is"; a="parameter" (with a comment)');
141
'text/xml; this="is"; a="parameter" (with a comment)',
146
public function testIsExperimental()
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'));
153
public function testIsVendor()
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'));
160
public function testIsWildcard()
162
$this->assertTrue(MIME_Type::isWildcard('*/*'));
163
$this->assertTrue(MIME_Type::isWildcard('image/*'));
164
$this->assertFalse(MIME_Type::isWildcard('text/plain'));
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'));
173
public function testWildcardMatchNoWildcard()
175
$this->assertFalse(MIME_Type::wildcardMatch('image/foo', 'image/png'));
178
public function testAddParameter()
180
$mt = new MIME_Type('image/png; foo=bar');
181
$mt->addParameter('baz', 'val', 'this is a comment');
183
$this->assertContains('foo=', $res);
184
$this->assertContains('bar', $res);
186
$this->assertContains('baz=', $res);
187
$this->assertContains('val', $res);
188
$this->assertContains('(this is a comment)', $res);
191
public function testRemoveParameter()
193
$mt = new MIME_Type('image/png; foo=bar');
194
$mt->addParameter('baz', 'val', 'this is a comment');
195
$mt->removeParameter('foo');
197
$this->assertNotContains('foo=', $res);
198
$this->assertNotContains('bar', $res);
199
$this->assertContains('baz=', $res);
202
public function testAutoDetect()
204
$dir = dirname(__FILE__) . '/files/';
207
MIME_Type::autoDetect($dir . 'example.png')
209
$this->assertInstanceOf('MIME_Type', $mt);
210
$this->assertEquals('image', $mt->media);
211
$this->assertEquals('png', $mt->subType);
214
MIME_Type::autoDetect($dir . 'example.jpg')
216
$this->assertInstanceOf('MIME_Type', $mt);
217
$this->assertEquals('image', $mt->media);
218
$this->assertEquals('jpeg', $mt->subType);
221
public function testAutoDetectNonexistingFile()
223
$res = MIME_Type::autoDetect('/this/file/does/not/exist');
224
$this->assertInstanceOf('PEAR_Error', $res);
225
$this->assertContains('doesn\'t exist', $res->getMessage());
228
public function testAutoDetectFinfo()
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);
240
public function testAutoDetectFinfoMagic()
242
$mt = new MIME_Type();
243
$mt->magicFile = dirname(__FILE__) . '/TypeTest.magic';
244
$mt->useMimeContentType = false;
245
$mt->useFileCmd = false;
246
$mt->useExtension = false;
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);
254
public function testAutoDetectFinfoNonExistingMagic()
256
$mt = new MIME_Type();
257
$mt->magicFile = dirname(__FILE__) . '/magicdoesnotexist';
258
$mt->useMimeContentType = false;
259
$mt->useFileCmd = false;
260
$mt->useExtension = false;
262
$type = $mt->autoDetect(dirname(__FILE__) . '/files/example.php');
263
$this->assertInstanceOf('PEAR_Error', $type);
266
public function testAutoDetectMimeContentType()
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);
277
public function testAutoDetectFileCommand()
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);
288
public function testAutoDetectFileCommandMagic()
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);
300
public function testAutoDetectExtension()
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);
311
public function testAutoDetectError()
313
$mt = new MIME_Type();
314
$mt->useFinfo = false;
315
$mt->useMimeContentType = false;
316
$mt->useFileCmd = false;
317
$mt->useExtension = false;
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);
325
public function test_fileAutoDetectNoFileCommand()
327
$cmd = &PEAR::getStaticProperty('MIME_Type', 'fileCmd');
328
$cmd = 'thiscommanddoesnotexist';
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());
336
public function testComments()
338
$type = new MIME_Type('(UTF-8 Plain Text) text / plain ; charset = utf-8');
340
'text/plain; charset="utf-8"', $type->get()
343
$type = new MIME_Type('text (Text) / plain ; charset = utf-8');
345
'text/plain; charset="utf-8"', $type->get()
348
$type = new MIME_Type('text / (Plain) plain ; charset = utf-8');
350
'text/plain; charset="utf-8"', $type->get()
353
$type = new MIME_Type('text / plain (Plain Text) ; charset = utf-8');
355
'text/plain; charset="utf-8"', $type->get()
358
$type = new MIME_Type('text / plain ; (Charset=utf-8) charset = utf-8');
360
'text/plain; charset="utf-8" (Charset=utf-8)', $type->get()
363
$type = new MIME_Type('text / plain ; charset (Charset) = utf-8');
365
'text/plain; charset="utf-8" (Charset)', $type->get()
368
$type = new MIME_Type('text / plain ; charset = (UTF8) utf-8');
370
'text/plain; charset="utf-8" (UTF8)', $type->get()
373
$type = new MIME_Type('text / plain ; charset = utf-8 (UTF-8 Plain Text)');
375
'text/plain; charset="utf-8" (UTF-8 Plain Text)', $type->get()
378
$type = new MIME_Type('application/x-foobar;description="bbgh(kdur"');
380
'application/x-foobar; description="bbgh(kdur"', $type->get()
383
$type = new MIME_Type('application/x-foobar;description="a \"quoted string\""');
385
'application/x-foobar; description="a \"quoted string\""', $type->get()
391
public function test_handleDetectionParamPearError()
393
$err = new PEAR_Error('test');
394
$ret = MIME_Type::_handleDetection($err, false);
395
$this->assertInstanceOf('PEAR_Error', $ret);
398
public function test_handleDetectionEmptyType()
400
$ret = MIME_Type::_handleDetection('', false);
401
$this->assertInstanceOf('PEAR_Error', $ret);
403
$ret = MIME_Type::_handleDetection(false, false);
404
$this->assertInstanceOf('PEAR_Error', $ret);