MediaWiki  1.30.0
MimeAnalyzerTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @group Media
4  * @covers MimeAnalyzer
5  */
6 class MimeMagicTest extends PHPUnit_Framework_TestCase {
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 
40  function testImproveTypeFromExtension( $ext, $oldMime, $expectedMime ) {
41  $actualMime = $this->mimeAnalyzer->improveTypeFromExtension( $oldMime, $ext );
42  $this->assertEquals( $expectedMime, $actualMime );
43  }
44 
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 
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 
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 
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 
97  $file = __DIR__ . '/../../../data/media/say-test-with-id3.mp3';
98  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
99  $this->assertEquals( 'audio/mpeg', $actualType );
100  }
101 
106  $file = __DIR__ . '/../../../data/media/say-test-mpeg1.mp3';
107  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
108  $this->assertEquals( 'audio/mpeg', $actualType );
109  }
110 
115  $file = __DIR__ . '/../../../data/media/say-test-mpeg2.mp3';
116  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
117  $this->assertEquals( 'audio/mpeg', $actualType );
118  }
119 
124  $file = __DIR__ . '/../../../data/media/say-test-mpeg2.5.mp3';
125  $actualType = $this->doGuessMimeType( [ $file, 'mp3' ] );
126  $this->assertEquals( 'audio/mpeg', $actualType );
127  }
128 }
MEDIATYPE_AUDIO
const MEDIATYPE_AUDIO
Definition: defines.php:32
MimeMagicTest\testImproveTypeFromExtension
testImproveTypeFromExtension( $ext, $oldMime, $expectedMime)
providerImproveTypeFromExtension
Definition: MimeAnalyzerTest.php:40
MimeMagicTest\testMP3WithID3Recognize
testMP3WithID3Recognize()
Test to make sure that MP3 with id3 tag is recognized.
Definition: MimeAnalyzerTest.php:96
MimeAnalyzer
Implements functions related to MIME types such as detection and mapping to file extension.
Definition: MimeAnalyzer.php:30
MimeMagicTest
Definition: MimeAnalyzerTest.php:6
MimeMagicTest\testMP3NoID3RecognizeMPEG2_5
testMP3NoID3RecognizeMPEG2_5()
Test to make sure that MP3 without id3 tag is recognized (MPEG-2.5 sample rates)
Definition: MimeAnalyzerTest.php:123
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
MimeMagicTest\testMP3NoID3RecognizeMPEG2
testMP3NoID3RecognizeMPEG2()
Test to make sure that MP3 without id3 tag is recognized (MPEG-2 sample rates)
Definition: MimeAnalyzerTest.php:114
$IP
$IP
Definition: update.php:3
MimeMagicTest\testOpusRecognize
testOpusRecognize()
Test to make sure that Opus audio files don't trigger MEDIATYPE_MULTIMEDIA (bug T151352)
Definition: MimeAnalyzerTest.php:78
MimeMagicTest\doGuessMimeType
doGuessMimeType(array $parameters=[])
Definition: MimeAnalyzerTest.php:27
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MimeMagicTest\setUp
setUp()
Definition: MimeAnalyzerTest.php:10
MimeMagicTest\$mimeAnalyzer
MimeAnalyzer $mimeAnalyzer
Definition: MimeAnalyzerTest.php:8
MimeMagicTest\providerImproveTypeFromExtension
providerImproveTypeFromExtension()
Definition: MimeAnalyzerTest.php:45
$ext
$ext
Definition: NoLocalSettings.php:25
MimeMagicTest\testOggRecognize
testOggRecognize()
Test to make sure that encoder=ffmpeg2theora doesn't trigger MEDIATYPE_VIDEO (T65584)
Definition: MimeAnalyzerTest.php:68
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MimeMagicTest\testMP3NoID3RecognizeMPEG1
testMP3NoID3RecognizeMPEG1()
Test to make sure that MP3 without id3 tag is recognized (MPEG-1 sample rates)
Definition: MimeAnalyzerTest.php:105
MimeMagicTest\testMP3AsAudio
testMP3AsAudio()
Test to make sure that mp3 files are detected as audio type.
Definition: MimeAnalyzerTest.php:87