Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / mime / MimeAnalyzerTest.php
1 <?php
2 /**
3 * @group Media
4 * @covers MimeAnalyzer
5 */
6 class MimeAnalyzerTest extends PHPUnit\Framework\TestCase {
7
8 use MediaWikiCoversValidator;
9
10 /** @var MimeAnalyzer */
11 private $mimeAnalyzer;
12
13 function setUp() {
14 global $IP;
15
16 $this->mimeAnalyzer = new MimeAnalyzer( [
17 'infoFile' => $IP . "/includes/libs/mime/mime.info",
18 'typeFile' => $IP . "/includes/libs/mime/mime.types",
19 'xmlTypes' => [
20 'http://www.w3.org/2000/svg:svg' => 'image/svg+xml',
21 'svg' => 'image/svg+xml',
22 'http://www.lysator.liu.se/~alla/dia/:diagram' => 'application/x-dia-diagram',
23 'http://www.w3.org/1999/xhtml:html' => 'text/html', // application/xhtml+xml?
24 'html' => 'text/html', // application/xhtml+xml?
25 ]
26 ] );
27 parent::setUp();
28 }
29
30 function doGuessMimeType( array $parameters = [] ) {
31 $class = new ReflectionClass( get_class( $this->mimeAnalyzer ) );
32 $method = $class->getMethod( 'doGuessMimeType' );
33 $method->setAccessible( true );
34 return $method->invokeArgs( $this->mimeAnalyzer, $parameters );
35 }
36
37 /**
38 * @dataProvider providerImproveTypeFromExtension
39 * @param string $ext File extension (no leading dot)
40 * @param string $oldMime Initially detected MIME
41 * @param string $expectedMime MIME type after taking extension into account
42 */
43 function testImproveTypeFromExtension( $ext, $oldMime, $expectedMime ) {
44 $actualMime = $this->mimeAnalyzer->improveTypeFromExtension( $oldMime, $ext );
45 $this->assertEquals( $expectedMime, $actualMime );
46 }
47
48 function providerImproveTypeFromExtension() {
49 return [
50 [ 'gif', 'image/gif', 'image/gif' ],
51 [ 'gif', 'unknown/unknown', 'unknown/unknown' ],
52 [ 'wrl', 'unknown/unknown', 'model/vrml' ],
53 [ 'txt', 'text/plain', 'text/plain' ],
54 [ 'csv', 'text/plain', 'text/csv' ],
55 [ 'tsv', 'text/plain', 'text/tab-separated-values' ],
56 [ 'js', 'text/javascript', 'application/javascript' ],
57 [ 'js', 'application/x-javascript', 'application/javascript' ],
58 [ 'json', 'text/plain', 'application/json' ],
59 [ 'foo', 'application/x-opc+zip', 'application/zip' ],
60 [ 'docx', 'application/x-opc+zip',
61 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ],
62 [ 'djvu', 'image/x-djvu', 'image/vnd.djvu' ],
63 [ 'wav', 'audio/wav', 'audio/wav' ],
64 ];
65 }
66
67 /**
68 * Test to make sure that encoder=ffmpeg2theora doesn't trigger
69 * MEDIATYPE_VIDEO (T65584)
70 */
71 function testOggRecognize() {
72 $oggFile = __DIR__ . '/../../../data/media/say-test.ogg';
73 $actualType = $this->mimeAnalyzer->getMediaType( $oggFile, 'application/ogg' );
74 $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
75 }
76
77 /**
78 * Test to make sure that Opus audio files don't trigger
79 * MEDIATYPE_MULTIMEDIA (bug T151352)
80 */
81 function testOpusRecognize() {
82 $oggFile = __DIR__ . '/../../../data/media/say-test.opus';
83 $actualType = $this->mimeAnalyzer->getMediaType( $oggFile, 'application/ogg' );
84 $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
85 }
86
87 /**
88 * Test to make sure that mp3 files are detected as audio type
89 */
90 function testMP3AsAudio() {
91 $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
92 $actualType = $this->mimeAnalyzer->getMediaType( $file );
93 $this->assertEquals( MEDIATYPE_AUDIO, $actualType );
94 }
95
96 /**
97 * Test to make sure that MP3 with id3 tag is recognized
98 */
99 function testMP3WithID3Recognize() {
100 $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
101 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
102 $this->assertEquals( 'audio/mpeg', $actualType );
103 }
104
105 /**
106 * Test to make sure that MP3 without id3 tag is recognized (MPEG-1 sample rates)
107 */
108 function testMP3NoID3RecognizeMPEG1() {
109 $file = __DIR__ . '/../../../data/media/say-test-mpeg1.mp3';
110 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
111 $this->assertEquals( 'audio/mpeg', $actualType );
112 }
113
114 /**
115 * Test to make sure that MP3 without id3 tag is recognized (MPEG-2 sample rates)
116 */
117 function testMP3NoID3RecognizeMPEG2() {
118 $file = __DIR__ . '/../../../data/media/say-test-mpeg2.mp3';
119 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
120 $this->assertEquals( 'audio/mpeg', $actualType );
121 }
122
123 /**
124 * Test to make sure that MP3 without id3 tag is recognized (MPEG-2.5 sample rates)
125 */
126 function testMP3NoID3RecognizeMPEG2_5() {
127 $file = __DIR__ . '/../../../data/media/say-test-mpeg2.5.mp3';
128 $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
129 $this->assertEquals( 'audio/mpeg', $actualType );
130 }
131
132 /**
133 * A ZIP file embedded in the middle of a .doc file is still a Word Document.
134 */
135 function testZipInDoc() {
136 $file = __DIR__ . '/../../../data/media/zip-in-doc.doc';
137 $actualType = $this->doGuessMimeType( [ $file, 'doc' ] );
138 $this->assertEquals( 'application/msword', $actualType );
139 }
140
141 /**
142 * @covers MimeAnalyzer::detectZipType
143 * @dataProvider provideOpendocumentsformatHeaders
144 */
145 function testDetectZipTypeRecognizesOpendocuments( $expected, $header ) {
146 $this->assertEquals(
147 $expected,
148 $this->mimeAnalyzer->detectZipType( $header )
149 );
150 }
151
152 /**
153 * An ODF file is a ZIP file of multiple files. The first one being
154 * 'mimetype' and is not compressed.
155 */
156 function provideOpendocumentsformatHeaders() {
157 $thirtychars = str_repeat( 0, 30 );
158 return [
159 'Database front end document header based on ODF 1.2' => [
160 'application/vnd.oasis.opendocument.base',
161 $thirtychars . 'mimetypeapplication/vnd.oasis.opendocument.basePK',
162 ],
163 ];
164 }
165
166 function providePngZipConfusion() {
167 return [
168 [
169 'An invalid ZIP file due to the signature being too close to the ' .
170 'end to accomodate an EOCDR',
171 'zip-sig-near-end.png',
172 'image/png',
173 ],
174 [
175 'An invalid ZIP file due to the comment length running beyond the ' .
176 'end of the file',
177 'zip-comment-overflow.png',
178 'image/png',
179 ],
180 [
181 'A ZIP file similar to the above, but without either of those two ' .
182 'problems. Not a valid ZIP file, but it passes MimeAnalyzer\'s ' .
183 'definition of a ZIP file. This is mostly a sanity check of the ' .
184 'above two tests.',
185 'zip-kind-of-valid.png',
186 'application/zip',
187 ],
188 [
189 'As above with non-zero comment length',
190 'zip-kind-of-valid-2.png',
191 'application/zip',
192 ],
193 ];
194 }
195
196 /** @dataProvider providePngZipConfusion */
197 function testPngZipConfusion( $description, $fileName, $expectedType ) {
198 $file = __DIR__ . '/../../../data/media/' . $fileName;
199 $actualType = $this->doGuessMimeType( [ $file, 'png' ] );
200 $this->assertEquals( $expectedType, $actualType, $description );
201 }
202 }