*/
private $handlers;
+ protected function getHandlerClass( $type ) {
+ global $wgMediaHandlers;
+ if ( isset( $wgMediaHandlers[$type] ) ) {
+ return $wgMediaHandlers[$type];
+ } else {
+ return false;
+ }
+ }
+
/**
* @param string $type mimetype
* @return bool|MediaHandler
*/
public function getHandler( $type ) {
- global $wgMediaHandlers;
- if ( !isset( $wgMediaHandlers[$type] ) ) {
- wfDebug( __METHOD__ . ": no handler found for $type.\n" );
-
- return false;
+ if ( isset( $this->handlers[$type] ) ) {
+ return $this->handlers[$type];
}
- $class = $wgMediaHandlers[$type];
- if ( !isset( $this->handlers[$class] ) ) {
- $this->handlers[$class] = new $class;
- if ( !$this->handlers[$class]->isEnabled() ) {
+
+ $class = $this->getHandlerClass( $type );
+ if ( $class !== false ) {
+ /** @var MediaHandler $handler */
+ $handler = new $class;
+ if ( !$handler->isEnabled() ) {
wfDebug( __METHOD__ . ": $class is not enabled\n" );
- $this->handlers[$class] = false;
+ $handler = false;
}
+ } else {
+ wfDebug( __METHOD__ . ": no handler found for $type.\n" );
+ $handler = false;
}
- return $this->handlers[$class];
+ $this->handlers[$type] = $handler;
+ return $handler;
}
}
'MockSvgHandler' => "$testDir/phpunit/mocks/media/MockSvgHandler.php",
'MockDjVuHandler' => "$testDir/phpunit/mocks/media/MockDjVuHandler.php",
'MockOggHandler' => "$testDir/phpunit/mocks/media/MockOggHandler.php",
+ 'MockMediaHandlerFactory' => "$testDir/phpunit/mocks/media/MockMediaHandlerFactory.php",
'MockWebRequest' => "$testDir/phpunit/mocks/MockWebRequest.php",
'MediaWiki\\Session\\DummySessionBackend'
=> "$testDir/phpunit/mocks/session/DummySessionBackend.php",
[ 300, 225 ],
[ 800, 600 ],
],
- 'wgMediaHandlers' => [
- 'unknown/unknown' => 'MockBitmapHandler',
- ],
] );
}
* @dataProvider provideThumbParams
*/
public function testIsStandard( $message, $expected, $params ) {
+ $this->setService( 'MediaHandlerFactory', new MockMediaHandlerFactory() );
$this->assertSame(
$expected,
wfThumbIsStandard( new FakeDimensionFile( [ 2000, 1800 ] ), $params ),
<?php
+
+use MediaWiki\MediaWikiServices;
+
/**
* Although marked as a stub, can work independently.
*
$tmpGlobals['wgStyleDirectory'] = "$IP/skins";
}
- # Replace all media handlers with a mock. We do not need to generate
- # actual thumbnails to do parser testing, we only care about receiving
- # a ThumbnailImage properly initialized.
- global $wgMediaHandlers;
- foreach ( $wgMediaHandlers as $type => $handler ) {
- $tmpGlobals['wgMediaHandlers'][$type] = 'MockBitmapHandler';
- }
- // Vector images have to be handled slightly differently
- $tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
-
- // DjVu images have to be handled slightly differently
- $tmpGlobals['wgMediaHandlers']['image/vnd.djvu'] = 'MockDjVuHandler';
-
- // Ogg video/audio increasingly more differently
- $tmpGlobals['wgMediaHandlers']['application/ogg'] = 'MockOggHandler';
-
$tmpHooks = $wgHooks;
$tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
$tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
$wgContLang->resetNamespaces(); # reset namespace cache
ParserTest::resetTitleServices();
+ MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' );
+ MediaWikiServices::getInstance()->redefineService(
+ 'MediaHandlerFactory',
+ function() {
+ return new MockMediaHandlerFactory();
+ }
+ );
}
protected function tearDown() {
// Restore message cache (temporary pages and $wgUseDatabaseMessages)
MessageCache::destroyInstance();
+ MediaWikiServices::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
parent::tearDown();
--- /dev/null
+<?php
+/**
+ * Media-handling base classes and generic functionality.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Media
+ */
+
+/**
+ * Replace all media handlers with a mock. We do not need to generate
+ * actual thumbnails to do parser testing, we only care about receiving
+ * a ThumbnailImage properly initialized.
+ *
+ * @since 1.28
+ */
+class MockMediaHandlerFactory extends MediaHandlerFactory {
+
+ private static $overrides = [
+ 'image/svg+xml' => MockSvgHandler::class,
+ 'image/vnd.djvu' => MockDjVuHandler::class,
+ 'application/ogg' => MockOggHandler::class,
+ ];
+
+ protected function getHandlerClass( $type ) {
+ if ( isset( self::$overrides[$type] ) ) {
+ return self::$overrides[$type];
+ }
+
+ return MockBitmapHandler::class;
+ }
+
+}