MediaWiki  1.33.0
MimeAnalyzerTest.php
Go to the documentation of this file.
1 <?php
6 class MimeAnalyzerTest extends PHPUnit\Framework\TestCase {
7 
8  use MediaWikiCoversValidator;
9 
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 
43  function testImproveTypeFromExtension( $ext, $oldMime, $expectedMime ) {
44  $actualMime = $this->mimeAnalyzer->improveTypeFromExtension( $oldMime, $ext );
45  $this->assertEquals( $expectedMime, $actualMime );
46  }
47 
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 
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 
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 
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 
100  $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
101  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
102  $this->assertEquals( 'audio/mpeg', $actualType );
103  }
104 
109  $file = __DIR__ . '/../../../data/media/say-test-mpeg1.mp3';
110  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
111  $this->assertEquals( 'audio/mpeg', $actualType );
112  }
113 
118  $file = __DIR__ . '/../../../data/media/say-test-mpeg2.mp3';
119  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
120  $this->assertEquals( 'audio/mpeg', $actualType );
121  }
122 
127  $file = __DIR__ . '/../../../data/media/say-test-mpeg2.5.mp3';
128  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
129  $this->assertEquals( 'audio/mpeg', $actualType );
130  }
131 
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 }
MimeAnalyzerTest\testMP3AsAudio
testMP3AsAudio()
Test to make sure that mp3 files are detected as audio type.
Definition: MimeAnalyzerTest.php:90
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Definition: router.php:42
MEDIATYPE_AUDIO
const MEDIATYPE_AUDIO
Definition: defines.php:32
MimeAnalyzer
Implements functions related to MIME types such as detection and mapping to file extension.
Definition: MimeAnalyzer.php:30
MimeAnalyzerTest
Media MimeAnalyzer.
Definition: MimeAnalyzerTest.php:6
MimeAnalyzerTest\doGuessMimeType
doGuessMimeType(array $parameters=[])
Definition: MimeAnalyzerTest.php:30
MimeAnalyzerTest\testMP3NoID3RecognizeMPEG2
testMP3NoID3RecognizeMPEG2()
Test to make sure that MP3 without id3 tag is recognized (MPEG-2 sample rates)
Definition: MimeAnalyzerTest.php:117
MimeAnalyzerTest\setUp
setUp()
Definition: MimeAnalyzerTest.php:13
MimeAnalyzerTest\$mimeAnalyzer
MimeAnalyzer $mimeAnalyzer
Definition: MimeAnalyzerTest.php:11
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$IP
$IP
Definition: update.php:3
MimeAnalyzerTest\providerImproveTypeFromExtension
providerImproveTypeFromExtension()
Definition: MimeAnalyzerTest.php:48
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
MimeAnalyzerTest\testImproveTypeFromExtension
testImproveTypeFromExtension( $ext, $oldMime, $expectedMime)
providerImproveTypeFromExtension
Definition: MimeAnalyzerTest.php:43
MimeAnalyzerTest\testOpusRecognize
testOpusRecognize()
Test to make sure that Opus audio files don't trigger MEDIATYPE_MULTIMEDIA (bug T151352)
Definition: MimeAnalyzerTest.php:81
MimeAnalyzerTest\testMP3WithID3Recognize
testMP3WithID3Recognize()
Test to make sure that MP3 with id3 tag is recognized.
Definition: MimeAnalyzerTest.php:99
MimeAnalyzerTest\testMP3NoID3RecognizeMPEG2_5
testMP3NoID3RecognizeMPEG2_5()
Test to make sure that MP3 without id3 tag is recognized (MPEG-2.5 sample rates)
Definition: MimeAnalyzerTest.php:126
$ext
if(!is_readable( $file)) $ext
Definition: router.php:48
MimeAnalyzerTest\testMP3NoID3RecognizeMPEG1
testMP3NoID3RecognizeMPEG1()
Test to make sure that MP3 without id3 tag is recognized (MPEG-1 sample rates)
Definition: MimeAnalyzerTest.php:108
MimeAnalyzerTest\testOggRecognize
testOggRecognize()
Test to make sure that encoder=ffmpeg2theora doesn't trigger MEDIATYPE_VIDEO (T65584)
Definition: MimeAnalyzerTest.php:71
MimeAnalyzerTest\testZipInDoc
testZipInDoc()
A ZIP file embedded in the middle of a .doc file is still a Word Document.
Definition: MimeAnalyzerTest.php:135