From 9ae93d74fd1345fbc1163444683676c4c25d4848 Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Mon, 25 Jul 2016 19:46:13 -0700 Subject: [PATCH] Override MediaHandlers in tests using MediaWikiServices Change-Id: Ie39fd9243e7817191b87179f792d932f7b96de0c --- includes/media/MediaHandlerFactory.php | 34 +++++++++----- tests/TestsAutoLoader.php | 1 + .../GlobalFunctions/wfThumbIsStandardTest.php | 4 +- .../phpunit/includes/parser/NewParserTest.php | 27 +++++------ .../mocks/media/MockMediaHandlerFactory.php | 47 +++++++++++++++++++ 5 files changed, 83 insertions(+), 30 deletions(-) create mode 100644 tests/phpunit/mocks/media/MockMediaHandlerFactory.php diff --git a/includes/media/MediaHandlerFactory.php b/includes/media/MediaHandlerFactory.php index 7e41242823..c6b4e8dbdc 100644 --- a/includes/media/MediaHandlerFactory.php +++ b/includes/media/MediaHandlerFactory.php @@ -33,26 +33,38 @@ class MediaHandlerFactory { */ 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; } } diff --git a/tests/TestsAutoLoader.php b/tests/TestsAutoLoader.php index 2bb1d2ef21..ef540a8f48 100644 --- a/tests/TestsAutoLoader.php +++ b/tests/TestsAutoLoader.php @@ -134,6 +134,7 @@ $wgAutoloadClasses += [ '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", diff --git a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php index a61b328045..9d9815b798 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php @@ -18,9 +18,6 @@ class WfThumbIsStandardTest extends MediaWikiTestCase { [ 300, 225 ], [ 800, 600 ], ], - 'wgMediaHandlers' => [ - 'unknown/unknown' => 'MockBitmapHandler', - ], ] ); } @@ -95,6 +92,7 @@ class WfThumbIsStandardTest extends MediaWikiTestCase { * @dataProvider provideThumbParams */ public function testIsStandard( $message, $expected, $params ) { + $this->setService( 'MediaHandlerFactory', new MockMediaHandlerFactory() ); $this->assertSame( $expected, wfThumbIsStandard( new FakeDimensionFile( [ 2000, 1800 ] ), $params ), diff --git a/tests/phpunit/includes/parser/NewParserTest.php b/tests/phpunit/includes/parser/NewParserTest.php index c56626f47b..e7abd15e8b 100644 --- a/tests/phpunit/includes/parser/NewParserTest.php +++ b/tests/phpunit/includes/parser/NewParserTest.php @@ -1,4 +1,7 @@ $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'; @@ -177,6 +164,13 @@ class NewParserTest extends MediaWikiTestCase { 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() { @@ -196,6 +190,7 @@ class NewParserTest extends MediaWikiTestCase { // Restore message cache (temporary pages and $wgUseDatabaseMessages) MessageCache::destroyInstance(); + MediaWikiServices::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' ); parent::tearDown(); diff --git a/tests/phpunit/mocks/media/MockMediaHandlerFactory.php b/tests/phpunit/mocks/media/MockMediaHandlerFactory.php new file mode 100644 index 0000000000..0bcc6eba45 --- /dev/null +++ b/tests/phpunit/mocks/media/MockMediaHandlerFactory.php @@ -0,0 +1,47 @@ + 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; + } + +} -- 2.20.1