Merge "Change 'editfont' default preference to 'monospace'"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / mime / MimeAnalyzerTest.php
1 <?php
2 /*
3 * @group Media
4 * @covers MimeAnalyzer
5 */
6 class MimeMagicTest extends PHPUnit_Framework_TestCase {
7 /** @var MimeAnalyzer */
8 private $mimeAnalyzer;
9
10 function setUp() {
11 global $IP;
12
13 $this->mimeAnalyzer = new MimeAnalyzer( [
14 'infoFile' => $IP . "/includes/libs/mime/mime.info",
15 'typeFile' => $IP . "/includes/libs/mime/mime.types",
16 'xmlTypes' => [
17 'http://www.w3.org/2000/svg:svg' => 'image/svg+xml',
18 'svg' => 'image/svg+xml',
19 'http://www.lysator.liu.se/~alla/dia/:diagram' => 'application/x-dia-diagram',
20 'http://www.w3.org/1999/xhtml:html' => 'text/html', // application/xhtml+xml?
21 'html' => 'text/html', // application/xhtml+xml?
22 ]
23 ] );
24 parent::setUp();
25 }
26
27 function doGuessMimeType( array $parameters = [] ) {
28 $class = new ReflectionClass( get_class( $this->mimeAnalyzer ) );
29 $method = $class->getMethod( 'doGuessMimeType' );
30 $method->setAccessible( true );
31 return $method->invokeArgs( $this->mimeAnalyzer, $parameters );
32 }
33
34 /**
35 * @dataProvider providerImproveTypeFromExtension
36 * @param string $ext File extension (no leading dot)
37 * @param string $oldMime Initially detected MIME
38 * @param string $expectedMime MIME type after taking extension into account
39 */
40 function testImproveTypeFromExtension( $ext, $oldMime, $expectedMime ) {
41 $actualMime = $this->mimeAnalyzer->improveTypeFromExtension( $oldMime, $ext );
42 $this->assertEquals( $expectedMime, $actualMime );
43 }
44
45 function providerImproveTypeFromExtension() {
46 return [
47 [ 'gif', 'image/gif', 'image/gif' ],
48 [ 'gif', 'unknown/unknown', 'unknown/unknown' ],
49 [ 'wrl', 'unknown/unknown', 'model/vrml' ],
50 [ 'txt', 'text/plain', 'text/plain' ],
51 [ 'csv', 'text/plain', 'text/csv' ],
52 [ 'tsv', 'text/plain', 'text/tab-separated-values' ],
53 [ 'js', 'text/javascript', 'application/javascript' ],
54 [ 'js', 'application/x-javascript', 'application/javascript' ],
55 [ 'json', 'text/plain', 'application/json' ],
56 [ 'foo', 'application/x-opc+zip', 'application/zip' ],
57 [ 'docx', 'application/x-opc+zip',
58 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ],
59 [ 'djvu', 'image/x-djvu', 'image/vnd.djvu' ],
60 [ 'wav', 'audio/wav', 'audio/wav' ],
61 ];
62 }
63
64 /**
65 * Test to make sure that encoder=ffmpeg2theora doesn't trigger
66 * MEDIATYPE_VIDEO (T65584)
67 */
68 function testOggRecognize() {
69 $oggFile = __DIR__ . '/../../../data/media/say-test.ogg';
70 $actualType = $this->mimeAnalyzer->getMediaType( $oggFile, 'application/ogg' );
71 $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
72 }
73
74 /**
75 * Test to make sure that Opus audio files don't trigger
76 * MEDIATYPE_MULTIMEDIA (bug T151352)
77 */
78 function testOpusRecognize() {
79 $oggFile = __DIR__ . '/../../../data/media/say-test.opus';
80 $actualType = $this->mimeAnalyzer->getMediaType( $oggFile, 'application/ogg' );
81 $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
82 }
83
84 /**
85 * Test to make sure that mp3 files are detected as audio type
86 */
87 function testMP3AsAudio() {
88 $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
89 $actualType = $this->mimeAnalyzer->getMediaType( $file );
90 $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
91 }
92
93 /**
94 * Test to make sure that MP3 with id3 tag is recognized
95 */
96 function testMP3WithID3Recognize() {
97 $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
98 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
99 $this->assertEquals( 'audio/mpeg', $actualType );
100 }
101
102 /**
103 * Test to make sure that MP3 without id3 tag is recognized (MPEG-1 sample rates)
104 */
105 function testMP3NoID3RecognizeMPEG1() {
106 $file = __DIR__ . '/../../../data/media/say-test-mpeg1.mp3';
107 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
108 $this->assertEquals( 'audio/mpeg', $actualType );
109 }
110
111 /**
112 * Test to make sure that MP3 without id3 tag is recognized (MPEG-2 sample rates)
113 */
114 function testMP3NoID3RecognizeMPEG2() {
115 $file = __DIR__ . '/../../../data/media/say-test-mpeg2.mp3';
116 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
117 $this->assertEquals( 'audio/mpeg', $actualType );
118 }
119
120 /**
121 * Test to make sure that MP3 without id3 tag is recognized (MPEG-2.5 sample rates)
122 */
123 function testMP3NoID3RecognizeMPEG2_5() {
124 $file = __DIR__ . '/../../../data/media/say-test-mpeg2.5.mp3';
125 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
126 $this->assertEquals( 'audio/mpeg', $actualType );
127 }
128 }