aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Mullie <git@mullie.eu>2017-07-11 16:35:49 +0200
committerBrion Vibber <brion@pobox.com>2017-08-13 12:04:37 -0400
commitd6bc1b25899834be9b87e64ffb69007fb89dc52c (patch)
treeeb87178f33e811235c8cccfcba325c9355b57a1e
parent700e49ddddcb9b791f5edbb5dd569b18c3a7f2eb (diff)
downloadmediawikicore-d6bc1b25899834be9b87e64ffb69007fb89dc52c.tar.gz
mediawikicore-d6bc1b25899834be9b87e64ffb69007fb89dc52c.zip
Remove test code that depends on extension
And allow extensions to add their own media handlers. I'm not too happy with the introduction of another global, but didn't like the alternatives either: * Add some hook to MockMediaHandlerFactory that would allow extensions to add their own stuff in. * Use another hook (like ParserTestTables or ParserTestGlobals) and then override the service with a new instance - seemed too hacky The good thing about this is that it lets us kill off a class. I'm other to other suggestions in case I missed something. Bug: T169258 Depends-On: I5875621c58597426ad5242bf3d07714555c439b5 Change-Id: I1c2e903fb235395a8de8e0f7bf65ce07739d2930
-rw-r--r--RELEASE-NOTES-1.302
-rw-r--r--includes/DefaultSettings.php17
-rw-r--r--tests/common/TestsAutoLoader.php2
-rw-r--r--tests/parser/ParserTestRunner.php5
-rw-r--r--tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php7
-rw-r--r--tests/phpunit/includes/media/FakeDimensionFile.php9
-rw-r--r--tests/phpunit/mocks/media/MockMediaHandlerFactory.php51
-rw-r--r--tests/phpunit/mocks/media/MockOggHandler.php105
8 files changed, 35 insertions, 163 deletions
diff --git a/RELEASE-NOTES-1.30 b/RELEASE-NOTES-1.30
index 9c4bcf044c92..e8d9b48a3bf8 100644
--- a/RELEASE-NOTES-1.30
+++ b/RELEASE-NOTES-1.30
@@ -53,6 +53,8 @@ section).
'watchlistunwatchlinks' preference option is enabled). With JavaScript
enabled, these links toggle so the user can also re-watch pages that have
just been unwatched.
+* Added $wgParserTestMediaHandlers, where mock media handlers can be passed to
+ MediaHandlerFactory for parser tests.
=== Languages updated in 1.30 ===
diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php
index 7fa55bc5d7f4..4e162f6ef59f 100644
--- a/includes/DefaultSettings.php
+++ b/includes/DefaultSettings.php
@@ -960,6 +960,23 @@ $wgTrustedMediaFormats = [
$wgMediaHandlers = [];
/**
+ * Media handler overrides for parser tests (they don't need to generate actual
+ * thumbnails, so a mock will do)
+ */
+$wgParserTestMediaHandlers = [
+ 'image/jpeg' => 'MockBitmapHandler',
+ 'image/png' => 'MockBitmapHandler',
+ 'image/gif' => 'MockBitmapHandler',
+ 'image/tiff' => 'MockBitmapHandler',
+ 'image/webp' => 'MockBitmapHandler',
+ 'image/x-ms-bmp' => 'MockBitmapHandler',
+ 'image/x-bmp' => 'MockBitmapHandler',
+ 'image/x-xcf' => 'MockBitmapHandler',
+ 'image/svg+xml' => 'MockSvgHandler',
+ 'image/vnd.djvu' => 'MockDjVuHandler',
+];
+
+/**
* Plugins for page content model handling.
* Each entry in the array maps a model id to a class name or callback
* that creates an instance of the appropriate ContentHandler subclass.
diff --git a/tests/common/TestsAutoLoader.php b/tests/common/TestsAutoLoader.php
index 9b9ea6d7d390..8f752df68be6 100644
--- a/tests/common/TestsAutoLoader.php
+++ b/tests/common/TestsAutoLoader.php
@@ -158,8 +158,6 @@ $wgAutoloadClasses += [
'MockImageHandler' => "$testDir/phpunit/mocks/media/MockImageHandler.php",
'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",
'MockChangesListFilter' => "$testDir/phpunit/mocks/MockChangesListFilter.php",
'MockChangesListFilterGroup' => "$testDir/phpunit/mocks/MockChangesListFilterGroup.php",
'MockWebRequest' => "$testDir/phpunit/mocks/MockWebRequest.php",
diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php
index c2f396be8d22..1d533442031a 100644
--- a/tests/parser/ParserTestRunner.php
+++ b/tests/parser/ParserTestRunner.php
@@ -341,8 +341,9 @@ class ParserTestRunner {
MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' );
MediaWikiServices::getInstance()->redefineService(
'MediaHandlerFactory',
- function () {
- return new MockMediaHandlerFactory();
+ function ( MediaWikiServices $services ) {
+ $handlers = $services->getMainConfig()->get( 'ParserTestMediaHandlers' );
+ return new MediaHandlerFactory( $handlers );
}
);
$teardown[] = function () {
diff --git a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php
index 9d9815b79845..bdba6a355e24 100644
--- a/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php
+++ b/tests/phpunit/includes/GlobalFunctions/wfThumbIsStandardTest.php
@@ -1,5 +1,7 @@
<?php
+use MediaWiki\MediaWikiServices;
+
/**
* @group GlobalFunctions
* @covers ::wfThumbIsStandard
@@ -92,10 +94,11 @@ class WfThumbIsStandardTest extends MediaWikiTestCase {
* @dataProvider provideThumbParams
*/
public function testIsStandard( $message, $expected, $params ) {
- $this->setService( 'MediaHandlerFactory', new MockMediaHandlerFactory() );
+ $handlers = MediaWikiServices::getInstance()->getMainConfig()->get( 'ParserTestMediaHandlers' );
+ $this->setService( 'MediaHandlerFactory', new MediaHandlerFactory( $handlers ) );
$this->assertSame(
$expected,
- wfThumbIsStandard( new FakeDimensionFile( [ 2000, 1800 ] ), $params ),
+ wfThumbIsStandard( new FakeDimensionFile( [ 2000, 1800 ], 'image/jpeg' ), $params ),
$message
);
}
diff --git a/tests/phpunit/includes/media/FakeDimensionFile.php b/tests/phpunit/includes/media/FakeDimensionFile.php
index 4b8f213eaa72..81e820e0d6d0 100644
--- a/tests/phpunit/includes/media/FakeDimensionFile.php
+++ b/tests/phpunit/includes/media/FakeDimensionFile.php
@@ -5,12 +5,15 @@
*/
class FakeDimensionFile extends File {
public $mustRender = false;
+ public $mime;
+ public $dimensions;
- public function __construct( $dimensions ) {
+ public function __construct( $dimensions, $mime = 'unknown/unknown' ) {
parent::__construct( Title::makeTitle( NS_FILE, 'Test' ),
new NullRepo( null ) );
$this->dimensions = $dimensions;
+ $this->mime = $mime;
}
public function getWidth( $page = 1 ) {
@@ -28,4 +31,8 @@ class FakeDimensionFile extends File {
public function getPath() {
return '';
}
+
+ public function getMimeType() {
+ return $this->mime;
+ }
}
diff --git a/tests/phpunit/mocks/media/MockMediaHandlerFactory.php b/tests/phpunit/mocks/media/MockMediaHandlerFactory.php
deleted file mode 100644
index 54d46b02714e..000000000000
--- a/tests/phpunit/mocks/media/MockMediaHandlerFactory.php
+++ /dev/null
@@ -1,51 +0,0 @@
-<?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,
- ];
-
- public function __construct() {
- // override parent
- }
-
- protected function getHandlerClass( $type ) {
- if ( isset( self::$overrides[$type] ) ) {
- return self::$overrides[$type];
- }
-
- return MockBitmapHandler::class;
- }
-
-}
diff --git a/tests/phpunit/mocks/media/MockOggHandler.php b/tests/phpunit/mocks/media/MockOggHandler.php
deleted file mode 100644
index bb686fd84685..000000000000
--- a/tests/phpunit/mocks/media/MockOggHandler.php
+++ /dev/null
@@ -1,105 +0,0 @@
-<?php
-/**
- * Fake handler for Ogg videos.
- *
- * 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
- */
-
-class MockOggHandler extends OggHandlerTMH {
- function doTransform( $file, $dstPath, $dstUrl, $params, $flags = 0 ) {
- # Important or height handling is wrong.
- if ( !$this->normaliseParams( $file, $params ) ) {
- return new TransformParameterError( $params );
- }
-
- $srcWidth = $file->getWidth();
- $srcHeight = $file->getHeight();
-
- // Audio should not be transformed by size, give it a default width and height
- if ( $this->isAudio( $file ) ) {
- $srcWidth = 220;
- $srcHeight = 23;
- }
-
- $params['width'] = isset( $params['width'] ) ? $params['width'] : $srcWidth;
-
- // if height overtakes width use height as max:
- $targetWidth = $params['width'];
- $targetHeight = $srcWidth == 0 ? $srcHeight : round( $params['width'] * $srcHeight / $srcWidth );
- if ( isset( $params['height'] ) && $targetHeight > $params['height'] ) {
- $targetHeight = $params['height'];
- $targetWidth = round( $params['height'] * $srcWidth / $srcHeight );
- }
- $options = [
- 'file' => $file,
- 'length' => $this->getLength( $file ),
- 'offset' => $this->getOffset( $file ),
- 'width' => $targetWidth,
- 'height' => $targetHeight,
- 'isVideo' => !$this->isAudio( $file ),
- 'thumbtime' => isset(
- $params['thumbtime']
- ) ? $params['thumbtime'] : intval( $file->getLength() / 2 ),
- 'start' => isset( $params['start'] ) ? $params['start'] : false,
- 'end' => isset( $params['end'] ) ? $params['end'] : false,
- 'fillwindow' => isset( $params['fillwindow'] ) ? $params['fillwindow'] : false,
- 'disablecontrols' => isset( $params['disablecontrols'] ) ? $params['disablecontrols'] : false
- ];
-
- // No thumbs for audio
- if ( !$options['isVideo'] ) {
- return new TimedMediaTransformOutput( $options );
- }
-
- // Setup pointer to thumb arguments
- $options[ 'thumbUrl' ] = $dstUrl;
- $options[ 'dstPath' ] = $dstPath;
- $options[ 'path' ] = $dstPath;
-
- return new TimedMediaTransformOutput( $options );
- }
-
- function getLength( $file ) {
- if ( $this->isAudio( $file ) ) {
- return 0.99875;
- }
- return 4.3666666666667;
- }
-
- function getBitRate( $file ) {
- if ( $this->isAudio( $file ) ) {
- return 41107;
- }
- return 590013;
- }
-
- function getWebType( $file ) {
- if ( $this->isAudio( $file ) ) {
- return "audio/ogg; codecs=\"vorbis\"";
- }
- return "video/ogg; codecs=\"theora\"";
- }
-
- function getFramerate( $file ) {
- if ( $this->isAudio( $file ) ) {
- return 0;
- }
- return 30;
- }
-}