Merge "Override MediaHandlers in tests using MediaWikiServices"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 4 Aug 2016 09:27:33 +0000 (09:27 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 4 Aug 2016 09:27:33 +0000 (09:27 +0000)
includes/media/MediaHandlerFactory.php
tests/TestsAutoLoader.php
tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php
tests/phpunit/includes/parser/NewParserTest.php
tests/phpunit/mocks/media/MockMediaHandlerFactory.php [new file with mode: 0644]

index 7e41242..c6b4e8d 100644 (file)
@@ -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;
        }
 }
index 2bb1d2e..ef540a8 100644 (file)
@@ -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",
index a61b328..9d9815b 100644 (file)
@@ -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 ),
index c56626f..e7abd15 100644 (file)
@@ -1,4 +1,7 @@
 <?php
+
+use MediaWiki\MediaWikiServices;
+
 /**
  * Although marked as a stub, can work independently.
  *
@@ -127,22 +130,6 @@ class NewParserTest extends MediaWikiTestCase {
                        $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';
@@ -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 (file)
index 0000000..0bcc6eb
--- /dev/null
@@ -0,0 +1,47 @@
+<?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;
+       }
+
+}