Debian Codegraph @

tests/phpunit/unit/includes/libs/mime/MimeAnalyzerTest.php

raw symbols SCIP
1 <?php
2
3 namespace Wikimedia\Tests\Mime;
4
5 use MediaWikiCoversValidator;
6 use PHPUnit\Framework\TestCase;
7 use ReflectionClass;
8 use Wikimedia\Mime\MimeAnalyzer;
9 use Wikimedia\TestingAccessWrapper;
10
11 /**
12 * @group Media
13 * @group Mime
14 * @covers \Wikimedia\Mime\MimeAnalyzer
15 */
16 class MimeAnalyzerTest extends TestCase {
17
18 use MediaWikiCoversValidator;
19
20 /** @var MimeAnalyzer */
21 private $mimeAnalyzer;
22
23 protected function setUp(): void {
24 parent::setUp();
25 $this->mimeAnalyzer = $this->createMimeAnalyzer();
26 }
27
28 private function createMimeAnalyzer() {
29 return new MimeAnalyzer( [
30 'infoFile' => MimeAnalyzer::USE_INTERNAL,
31 'typeFile' => MimeAnalyzer::USE_INTERNAL,
32 'xmlTypes' => [
33 'http://www.w3.org/2000/svg:svg' => 'image/svg+xml',
34 'svg' => 'image/svg+xml',
35 'http://www.lysator.liu.se/~alla/dia/:diagram' => 'application/x-dia-diagram',
36 'http://www.w3.org/1999/xhtml:html' => 'text/html', // application/xhtml+xml?
37 'html' => 'text/html', // application/xhtml+xml?
38 ]
39 ] );
40 }
41
42 private function doGuessMimeType( array $parameters = [] ) {
43 $class = new ReflectionClass( get_class( $this->mimeAnalyzer ) );
44 $method = $class->getMethod( 'doGuessMimeType' );
45 return $method->invokeArgs( $this->mimeAnalyzer, $parameters );
46 }
47
48 /**
49 * @dataProvider providerImproveTypeFromExtension
50 * @param string $ext File extension (no leading dot)
51 * @param string $oldMime Initially detected MIME
52 * @param string|null $expectedMime MIME type after taking extension into account
53 */
54 public function testImproveTypeFromExtension( $ext, $oldMime, $expectedMime ) {
55 $actualMime = $this->mimeAnalyzer->improveTypeFromExtension( $oldMime, $ext );
56 $this->assertEquals( $expectedMime, $actualMime );
57 }
58
59 public static function providerImproveTypeFromExtension() {
60 return [
61 [ 'gif', 'image/gif', 'image/gif' ],
62 [ 'gif', 'unknown/unknown', 'unknown/unknown' ],
63 [ 'wrl', 'unknown/unknown', 'model/vrml' ],
64 [ 'txt', 'text/plain', 'text/plain' ],
65 [ 'csv', 'text/plain', 'text/csv' ],
66 [ 'tsv', 'text/plain', 'text/tab-separated-values' ],
67 [ 'js', 'text/javascript', 'application/javascript' ],
68 [ 'js', 'application/x-javascript', 'application/javascript' ],
69 [ 'json', 'text/plain', 'application/json' ],
70 [ 'foo', 'application/x-opc+zip', 'application/zip' ],
71 [ 'docx', 'application/x-opc+zip',
72 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' ],
73 [ 'djvu', 'image/x-djvu', 'image/vnd.djvu' ],
74 [ 'wav', 'audio/wav', 'audio/wav' ],
75 [ 'odt', 'application/vnd.oasis.opendocument',
76 'application/vnd.oasis.opendocument.text' ],
77
78 // XXX: It's probably wrong (as in: confusing and error-prone) for
79 // ::improveTypeFromExtension to return null (T253483).
80 // This test case exists to ensure that any changes to the existing
81 // behavior are intentional.
82 [ 'no_such_extension', 'unknown/unknown', null ],
83 ];
84 }
85
86 public static function provideGetMediaType() {
87 // Make sure encoder=ffmpeg2theora don't trigger MEDIATYPE_VIDEO (T65584)
88 yield 'Recognize ogg' => [ 'say-test.ogg', 'application/ogg', MEDIATYPE_AUDIO ];
89
90 // Make sure Opus audio files don't trigger MEDIATYPE_MULTIMEDIA (T151352)
91 yield 'Recognize Opus' => [ 'say-test.opus', 'application/ogg', MEDIATYPE_AUDIO ];
92
93 // Make sure mp3 files are detected as audio type
94 yield 'Recognize mp3' => [ 'say-test-with-id3.mp3', null, MEDIATYPE_AUDIO ];
95
96 yield 'Unknown Extension' => [ 'unknown_extension', null, MEDIATYPE_UNKNOWN ];
97 }
98
99 /**
100 * @dataProvider provideGetMediaType
101 */
102 public function testGetMediaType( $file, $mime, $expectType ) {
103 $file = __DIR__ . '/../../../../data/media/' . $file;
104 $this->assertEquals(
105 $expectType,
106 $this->mimeAnalyzer->getMediaType( $file, $mime )
107 );
108 }
109
110 public static function provideDoGuessMimeType() {
111 // Make sure MP3 with id3 tag is recognized
112 yield 'Recognize mp3 with id3' => [ 'say-test-with-id3.mp3', 'mp3', 'audio/mpeg' ];
113
114 // Make sure MP3 without id3 tag is recognized (MPEG-1 sample rates)
115 yield 'Recognize mp3 no id3, MPEG-1' => [ 'say-test-mpeg1.mp3', 'mp3', 'audio/mpeg' ];
116
117 // Make sure MP3 without id3 tag is recognized (MPEG-2 sample rates)
118 yield 'Recognize mp3 no id3, MPEG-2' => [ 'say-test-mpeg2.mp3', 'mp3', 'audio/mpeg' ];
119
120 // Make sure MP3 without id3 tag is recognized (MPEG-2.5 sample rates)
121 yield 'Recognize mp3 no id3, MPEG-2.5' => [ 'say-test-mpeg2.5.mp3', 'mp3', 'audio/mpeg' ];
122
123 // A ZIP file embedded in the middle of a .doc file is still a Word Document
124 yield 'ZIP in DOC' => [ 'zip-in-doc.doc', 'doc', 'application/msword' ];
125
126 yield 'Jpeg2000, lossless' => [ 'jpeg2000-lossless.jp2', 'jp2', 'image/jp2' ];
127
128 yield 'Jpeg2000, part 2' => [ 'jpeg2000-profile.jpf', 'jpf', 'image/jpx' ];
129 }
130
131 /**
132 * @dataProvider provideDoGuessMimeType
133 */
134 public function testDoGuessMimeType( $file, $ext, $expectType ) {
135 $file = __DIR__ . '/../../../../data/media/' . $file;
136 $this->assertEquals(
137 $expectType,
138 $this->doGuessMimeType( [ $file, $ext ] )
139 );
140 }
141
142 public static function provideDetectZipTypeFromFile() {
143 return [
144 '[Content_Type].xml at end (T291750)' => [
145 'type-at-end.docx',
146 'application/x-opc+zip'
147 ],
148 'Typical ODT gives fake generic type' => [
149 'lo6-empty.odt',
150 'application/vnd.oasis.opendocument'
151 ],
152 'Ye olde GIFAR vulnerability' => [
153 'gifar.gif',
154 'application/java',
155 ],
156
157 // todo : Add test case for ZIP file with .zip extension
158 ];
159 }
160
161 /**
162 * @dataProvider provideDetectZipTypeFromFile
163 * @param string $fileName
164 * @param string $expected
165 */
166 public function testDetectZipTypeFromFile( $fileName, $expected ) {
167 $file = fopen( __DIR__ . '/../../../../data/media/' . $fileName, 'r' );
168 $this->assertEquals(
169 $expected,
170 $this->mimeAnalyzer->detectZipTypeFromFile( $file )
171 );
172 }
173
174 public static function providePngZipConfusion() {
175 return [
176 [
177 'An invalid ZIP file due to the signature being too close to the ' .
178 'end to accommodate an EOCDR',
179 'zip-sig-near-end.png',
180 'image/png',
181 ],
182 [
183 'An invalid ZIP file due to the comment length running beyond the ' .
184 'end of the file',
185 'zip-comment-overflow.png',
186 'image/png',
187 ],
188 [
189 'A ZIP file similar to the above, but without either of those two ' .
190 'problems. Not a valid ZIP file, but it passes MimeAnalyzer\'s ' .
191 'definition of a ZIP file. This is mostly a double check of the ' .
192 'above two tests.',
193 'zip-kind-of-valid.png',
194 'application/zip',
195 ],
196 [
197 'As above with non-zero comment length',
198 'zip-kind-of-valid-2.png',
199 'application/zip',
200 ],
201 [
202 'Ye olde GIFAR vulnerability',
203 'gifar.gif',
204 'application/java'
205 ]
206 ];
207 }
208
209 /**
210 * @dataProvider providePngZipConfusion
211 */
212 public function testPngZipConfusion( $description, $fileName, $expectedType ) {
213 $file = __DIR__ . '/../../../../data/media/' . $fileName;
214 $actualType = $this->doGuessMimeType( [ $file, 'png' ] );
215 $this->assertEquals( $expectedType, $actualType, $description );
216 }
217
218 /**
219 * The empty string is not a MIME type and should not be mapped to a file extension.
220 */
221 public function testNoEmptyStringMimeType() {
222 $this->assertSame( [], $this->mimeAnalyzer->getExtensionsFromMimeType( '' ) );
223 }
224
225 public function testAddExtraTypes() {
226 $mime = new MimeAnalyzer( [
227 'infoFile' => MimeAnalyzer::USE_INTERNAL,
228 'typeFile' => MimeAnalyzer::USE_INTERNAL,
229 'xmlTypes' => [],
230 'initCallback' => static function ( $instance ) {
231 $instance->addExtraTypes( 'fake/mime fake_extension' );
232 $instance->addExtraInfo( 'fake/mime [OFFICE]' );
233 $instance->mExtToMime[ 'no_such_extension' ] = 'fake/mime';
234 },
235 ] );
236 $this->assertSame( [ 'fake/mime' ], $mime->getMimeTypesFromExtension( 'fake_extension' ) );
237 $this->assertSame( 'fake/mime', $mime->getMimeTypeFromExtensionOrNull( 'no_such_extension' ) );
238
239 $mimeAccess = TestingAccessWrapper::newFromObject( $mime );
240 $this->assertSame( MEDIATYPE_OFFICE, $mimeAccess->findMediaType( '.fake_extension' ) );
241 }
242
243 public function testGetMimeTypesFromNoExtension() {
244 $this->assertSame( [], $this->mimeAnalyzer->getMimeTypesFromExtension( 'no_such_extension' ) );
245 }
246
247 public static function provideFileExtensions() {
248 yield 'ttf file extension should output font/sfnt' => [ 'ttf', 'font/sfnt' ];
249 yield 'ttf file extension should output application/font-sfnt' => [ 'ttf', 'application/font-sfnt' ];
250 yield 'woff file extension should output font/woff' => [ 'woff', 'font/woff' ];
251 yield 'woff file extension should output application/font/woff' => [ 'woff', 'application/font-woff' ];
252 yield 'woff2 file extension should output font/woff' => [ 'woff2', 'font/woff2' ];
253 yield 'woff2 file extension should output application/font/woff' => [ 'woff2', 'application/font-woff2' ];
254 yield 'webm file extension should return video/webm' => [ 'webm', 'video/webm' ];
255 yield 'webm file extension should return audio/webm' => [ 'webm', 'audio/webm' ];
256 }
257
258 /**
259 * @dataProvider provideFileExtensions
260 */
261 public function testGetMimeTypesFromExtension( $inputFileExtension, $expectedOutput ) {
262 $actualOutput = $this->mimeAnalyzer->getMimeTypesFromExtension( $inputFileExtension );
263 $this->assertContains( $expectedOutput, $actualOutput );
264 }
265
266 public static function provideFileExtensionsForMimeType() {
267 yield 'font/sfnt should output ttf and otf file extensionss' => [ 'font/sfnt', [ 'ttf', 'otf' ] ];
268 yield 'application/font-sfnt should output ttf file extension' => [ 'application/font-sfnt', [ 'ttf' ] ];
269 yield 'font/woff should output woff file extension' => [ 'font/woff', [ 'woff' ] ];
270 yield 'application/font-woff should output woff file extension' => [ 'application/font-woff', [ 'woff' ] ];
271 yield 'font/woff2 should output woff2 file extension' => [ 'font/woff2', [ 'woff2' ] ];
272 yield 'application/font-woff2 should output woff2 file extension' => [ 'application/font-woff2', [ 'woff2' ] ];
273 yield 'text/sgml should output sgml file extension' => [ 'text/sgml', [ 'sgml', 'sgm' ] ];
274 yield 'text/javascript should output js file extension' => [ 'text/javascript', [ 'js' ] ];
275 }
276
277 /**
278 * @dataProvider provideFileExtensionsForMimeType
279 */
280 public function testGetExtensionsFromMimeType( $inputMimeType, $expectedOutput ) {
281 $actualOutput = $this->mimeAnalyzer->getExtensionsFromMimeType( $inputMimeType );
282 $this->assertSame( $expectedOutput, $actualOutput );
283 }
284
285 public function testGetMimeTypeFromExtensionOrNull() {
286 $this->assertSame( 'video/webm', $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( 'webm' ) );
287 $this->assertNull( $this->mimeAnalyzer->getMimeTypeFromExtensionOrNull( 'no_such_extension' ) );
288 }
289
290 public function testGetExtensionsFromFakeMimeType() {
291 $this->assertSame( [], $this->mimeAnalyzer->getExtensionsFromMimeType( 'fake/mime' ) );
292 }
293
294 public function testGetExtensionFromMimeTypeOrNull() {
295 $this->assertSame( 'sgml', $this->mimeAnalyzer->getExtensionFromMimeTypeOrNull( 'text/sgml' ) );
296 $this->assertNull( $this->mimeAnalyzer->getExtensionFromMimeTypeOrNull( 'fake/mime' ) );
297 }
298
299 public function testGetMediaTypes() {
300 $mimeAnalyzer = $this->createMimeAnalyzer();
301 $mediaTypes = $mimeAnalyzer->getMediaTypes();
302
303 $this->assertIsArray( $mediaTypes );
304 $this->assertNotEmpty( $mediaTypes );
305
306 $this->assertContains( 'BITMAP', $mediaTypes );
307 }
308
309 public function testGetMediaTypeForNullCase() {
310 $mimeAnalyzer = $this->createMimeAnalyzer();
311
312 // Test case when both $mime and $path are null
313 $this->assertEquals( MEDIATYPE_UNKNOWN, $mimeAnalyzer->getMediaType() );
314 }
315
316 public function testIsMatchingExtension() {
317 $analyzer = $this->createMimeAnalyzer();
318
319 // Passing an unknown MIME type
320 $this->assertNull( $analyzer->isMatchingExtension( 'application/x-custom', 'jpg' ) );
321 }
322
323 public function testIsValidMajorMimeTypeTrue() {
324 $analyzer = $this->createMimeAnalyzer();
325
326 $this->assertTrue( $analyzer->isValidMajorMimeType( 'image' ) );
327 }
328
329 public function testIsValidMajorMimeTypeFalse() {
330 $analyzer = $this->createMimeAnalyzer();
331
332 $this->assertFalse( $analyzer->isValidMajorMimeType( 'font' ) );
333 }
334 }