diff options
Diffstat (limited to 'tests/phpunit/includes/media')
19 files changed, 542 insertions, 344 deletions
diff --git a/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php b/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php index 88f87ef9..a0e63a8a 100644 --- a/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php +++ b/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php @@ -1,7 +1,11 @@ <?php class BitmapMetadataHandlerTest extends MediaWikiTestCase { - public function setUp() { + protected function setUp() { + parent::setUp(); + + $this->setMwGlobals( 'wgShowEXIF', false ); + $this->filePath = __DIR__ . '/../../data/media/'; } @@ -12,33 +16,31 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { * Basically the file has IPTC and XMP metadata, the * IPTC should override the XMP, except for the multilingual * translation (to en) where XMP should win. + * @covers BitmapMetadataHandler::Jpeg */ public function testMultilingualCascade() { - if ( !wfDl( 'exif' ) ) { + if ( !extension_loaded( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } - if ( !wfDl( 'xml' ) ) { + if ( !extension_loaded( 'xml' ) ) { $this->markTestSkipped( "This test needs the xml extension." ); } - global $wgShowEXIF; - $oldExif = $wgShowEXIF; - $wgShowEXIF = true; + + $this->setMwGlobals( 'wgShowEXIF', true ); $meta = BitmapMetadataHandler::Jpeg( $this->filePath . '/Xmp-exif-multilingual_test.jpg' ); $expected = array( 'x-default' => 'right(iptc)', - 'en' => 'right translation', - '_type' => 'lang' + 'en' => 'right translation', + '_type' => 'lang' ); - + $this->assertArrayHasKey( 'ImageDescription', $meta, 'Did not extract any ImageDescription info?!' ); $this->assertEquals( $expected, $meta['ImageDescription'] ); - - $wgShowEXIF = $oldExif; } /** @@ -47,6 +49,7 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { * * There's more extensive tests of comment extraction in * JpegMetadataExtractorTests.php + * @covers BitmapMetadataHandler::Jpeg */ public function testJpegComment() { $meta = BitmapMetadataHandler::Jpeg( $this->filePath . @@ -59,6 +62,7 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { /** * Make sure a bad iptc block doesn't stop the other metadata * from being extracted. + * @covers BitmapMetadataHandler::Jpeg */ public function testBadIPTC() { $meta = BitmapMetadataHandler::Jpeg( $this->filePath . @@ -66,6 +70,9 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { $this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] ); } + /** + * @covers BitmapMetadataHandler::Jpeg + */ public function testIPTCDates() { $meta = BitmapMetadataHandler::Jpeg( $this->filePath . 'iptc-timetest.jpg' ); @@ -73,9 +80,11 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { $this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] ); $this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] ); } + /** * File has an invalid time (+ one valid but really weird time) * that shouldn't be included + * @covers BitmapMetadataHandler::Jpeg */ public function testIPTCDatesInvalid() { $meta = BitmapMetadataHandler::Jpeg( $this->filePath . @@ -89,6 +98,8 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { * XMP data should take priority over iptc data * when hash has been updated, but not when * the hash is wrong. + * @covers BitmapMetadataHandler::addMetadata + * @covers BitmapMetadataHandler::getMetadataArray */ public function testMerging() { $merger = new BitmapMetadataHandler(); @@ -112,35 +123,45 @@ class BitmapMetadataHandlerTest extends MediaWikiTestCase { $this->assertEquals( $expected, $actual ); } + /** + * @covers BitmapMetadataHandler::png + */ public function testPNGXMP() { - if ( !wfDl( 'xml' ) ) { + if ( !extension_loaded( 'xml' ) ) { $this->markTestSkipped( "This test needs the xml extension." ); } $handler = new BitmapMetadataHandler(); $result = $handler->png( $this->filePath . 'xmp.png' ); - $expected = array ( + $expected = array( 'frameCount' => 0, 'loopCount' => 1, 'duration' => 0, 'bitDepth' => 1, 'colorType' => 'index-coloured', - 'metadata' => array ( + 'metadata' => array( 'SerialNumber' => '123456789', '_MW_PNG_VERSION' => 1, ), ); - $this->assertEquals( $expected, $result ); + $this->assertEquals( $expected, $result ); } + + /** + * @covers BitmapMetadataHandler::png + */ public function testPNGNative() { $handler = new BitmapMetadataHandler(); $result = $handler->png( $this->filePath . 'Png-native-test.png' ); $expected = 'http://example.com/url'; - $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] ); + $this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] ); } + + /** + * @covers BitmapMetadataHandler::getTiffByteOrder + */ public function testTiffByteOrder() { $handler = new BitmapMetadataHandler(); $res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' ); $this->assertEquals( 'LE', $res ); } - } diff --git a/tests/phpunit/includes/media/BitmapScalingTest.php b/tests/phpunit/includes/media/BitmapScalingTest.php index 11d9dc47..9395b660 100644 --- a/tests/phpunit/includes/media/BitmapScalingTest.php +++ b/tests/phpunit/includes/media/BitmapScalingTest.php @@ -2,36 +2,34 @@ class BitmapScalingTest extends MediaWikiTestCase { - function setUp() { - global $wgMaxImageArea, $wgCustomConvertCommand; - $this->oldMaxImageArea = $wgMaxImageArea; - $this->oldCustomConvertCommand = $wgCustomConvertCommand; - $wgMaxImageArea = 1.25e7; // 3500x3500 - $wgCustomConvertCommand = 'dummy'; // Set so that we don't get client side rendering - } - function tearDown() { - global $wgMaxImageArea, $wgCustomConvertCommand; - $wgMaxImageArea = $this->oldMaxImageArea; - $wgCustomConvertCommand = $this->oldCustomConvertCommand; + protected function setUp() { + parent::setUp(); + + $this->setMwGlobals( array( + 'wgMaxImageArea' => 1.25e7, // 3500x3500 + 'wgCustomConvertCommand' => 'dummy', // Set so that we don't get client side rendering + ) ); } + /** * @dataProvider provideNormaliseParams + * @covers BitmapHandler::normaliseParams */ - function testNormaliseParams( $fileDimensions, $expectedParams, $params, $msg ) { + public function testNormaliseParams( $fileDimensions, $expectedParams, $params, $msg ) { $file = new FakeDimensionFile( $fileDimensions ); $handler = new BitmapHandler; $valid = $handler->normaliseParams( $file, $params ); $this->assertTrue( $valid ); $this->assertEquals( $expectedParams, $params, $msg ); } - - function provideNormaliseParams() { + + public static function provideNormaliseParams() { return array( - /* Regular resize operations */ + /* Regular resize operations */ array( array( 1024, 768 ), - array( - 'width' => 512, 'height' => 384, + array( + 'width' => 512, 'height' => 384, 'physicalWidth' => 512, 'physicalHeight' => 384, 'page' => 1, ), @@ -40,53 +38,53 @@ class BitmapScalingTest extends MediaWikiTestCase { ), array( array( 1024, 768 ), - array( - 'width' => 512, 'height' => 384, + array( + 'width' => 512, 'height' => 384, 'physicalWidth' => 512, 'physicalHeight' => 384, - 'page' => 1, + 'page' => 1, ), array( 'width' => 512, 'height' => 768 ), 'Resizing with height set too high', ), array( array( 1024, 768 ), - array( - 'width' => 512, 'height' => 384, + array( + 'width' => 512, 'height' => 384, 'physicalWidth' => 512, 'physicalHeight' => 384, - 'page' => 1, + 'page' => 1, ), array( 'width' => 1024, 'height' => 384 ), 'Resizing with height set', ), - + /* Very tall images */ array( array( 1000, 100 ), - array( + array( 'width' => 5, 'height' => 1, 'physicalWidth' => 5, 'physicalHeight' => 1, - 'page' => 1, + 'page' => 1, ), array( 'width' => 5 ), 'Very wide image', ), - + array( array( 100, 1000 ), - array( + array( 'width' => 1, 'height' => 10, 'physicalWidth' => 1, 'physicalHeight' => 10, - 'page' => 1, + 'page' => 1, ), array( 'width' => 1 ), 'Very high image', ), array( array( 100, 1000 ), - array( + array( 'width' => 1, 'height' => 5, 'physicalWidth' => 1, 'physicalHeight' => 10, - 'page' => 1, + 'page' => 1, ), array( 'width' => 10, 'height' => 5 ), 'Very high image with height set', @@ -94,58 +92,46 @@ class BitmapScalingTest extends MediaWikiTestCase { /* Max image area */ array( array( 4000, 4000 ), - array( + array( 'width' => 5000, 'height' => 5000, 'physicalWidth' => 4000, 'physicalHeight' => 4000, - 'page' => 1, + 'page' => 1, ), array( 'width' => 5000 ), 'Bigger than max image size but doesn\'t need scaling', ), ); - } - function testTooBigImage() { + } + + /** + * @covers BitmapHandler::doTransform + */ + public function testTooBigImage() { $file = new FakeDimensionFile( array( 4000, 4000 ) ); $handler = new BitmapHandler; $params = array( 'width' => '3700' ); // Still bigger than max size. - $this->assertEquals( 'TransformParameterError', + $this->assertEquals( 'TransformParameterError', get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) ); } - function testTooBigMustRenderImage() { + + /** + * @covers BitmapHandler::doTransform + */ + public function testTooBigMustRenderImage() { $file = new FakeDimensionFile( array( 4000, 4000 ) ); $file->mustRender = true; $handler = new BitmapHandler; $params = array( 'width' => '5000' ); // Still bigger than max size. - $this->assertEquals( 'TransformParameterError', + $this->assertEquals( 'TransformParameterError', get_class( $handler->doTransform( $file, 'dummy path', '', $params ) ) ); } - - function testImageArea() { + + /** + * @covers BitmapHandler::getImageArea + */ + public function testImageArea() { $file = new FakeDimensionFile( array( 7, 9 ) ); $handler = new BitmapHandler; $this->assertEquals( 63, $handler->getImageArea( $file ) ); } } - -class FakeDimensionFile extends File { - public $mustRender = false; - - public function __construct( $dimensions ) { - parent::__construct( Title::makeTitle( NS_FILE, 'Test' ), - new NullRepo( null ) ); - - $this->dimensions = $dimensions; - } - public function getWidth( $page = 1 ) { - return $this->dimensions[0]; - } - public function getHeight( $page = 1 ) { - return $this->dimensions[1]; - } - public function mustRender() { - return $this->mustRender; - } - public function getPath() { - return ''; - } -} diff --git a/tests/phpunit/includes/media/ExifBitmapTest.php b/tests/phpunit/includes/media/ExifBitmapTest.php index b2f6b7ba..a2e0eb62 100644 --- a/tests/phpunit/includes/media/ExifBitmapTest.php +++ b/tests/phpunit/includes/media/ExifBitmapTest.php @@ -2,60 +2,91 @@ class ExifBitmapTest extends MediaWikiTestCase { - public function setUp() { - global $wgShowEXIF; - $this->showExif = $wgShowEXIF; - $wgShowEXIF = true; - $this->handler = new ExifBitmapHandler; - if ( !wfDl( 'exif' ) ) { + /** + * @var ExifBitmapHandler + */ + protected $handler; + + protected function setUp() { + parent::setUp(); + if ( !extension_loaded( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } - } - public function tearDown() { - global $wgShowEXIF; - $wgShowEXIF = $this->showExif; + $this->setMwGlobals( 'wgShowEXIF', true ); + + $this->handler = new ExifBitmapHandler; + } + /** + * @covers ExifBitmapHandler::isMetadataValid + */ public function testIsOldBroken() { $res = $this->handler->isMetadataValid( null, ExifBitmapHandler::OLD_BROKEN_FILE ); $this->assertEquals( ExifBitmapHandler::METADATA_COMPATIBLE, $res ); } + + /** + * @covers ExifBitmapHandler::isMetadataValid + */ public function testIsBrokenFile() { $res = $this->handler->isMetadataValid( null, ExifBitmapHandler::BROKEN_FILE ); $this->assertEquals( ExifBitmapHandler::METADATA_GOOD, $res ); } + + /** + * @covers ExifBitmapHandler::isMetadataValid + */ public function testIsInvalid() { $res = $this->handler->isMetadataValid( null, 'Something Invalid Here.' ); $this->assertEquals( ExifBitmapHandler::METADATA_BAD, $res ); } + + /** + * @covers ExifBitmapHandler::isMetadataValid + */ public function testGoodMetadata() { $meta = 'a:16:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}'; $res = $this->handler->isMetadataValid( null, $meta ); $this->assertEquals( ExifBitmapHandler::METADATA_GOOD, $res ); } + + /** + * @covers ExifBitmapHandler::isMetadataValid + */ public function testIsOldGood() { $meta = 'a:16:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:1;}'; $res = $this->handler->isMetadataValid( null, $meta ); $this->assertEquals( ExifBitmapHandler::METADATA_COMPATIBLE, $res ); } - // Handle metadata from paged tiff handler (gotten via instant commons) - // gracefully. + + /** + * Handle metadata from paged tiff handler (gotten via instant commons) gracefully. + * @covers ExifBitmapHandler::isMetadataValid + */ public function testPagedTiffHandledGracefully() { $meta = 'a:6:{s:9:"page_data";a:1:{i:1;a:5:{s:5:"width";i:643;s:6:"height";i:448;s:5:"alpha";s:4:"true";s:4:"page";i:1;s:6:"pixels";i:288064;}}s:10:"page_count";i:1;s:10:"first_page";i:1;s:9:"last_page";i:1;s:4:"exif";a:9:{s:10:"ImageWidth";i:643;s:11:"ImageLength";i:448;s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:4;s:12:"RowsPerStrip";i:50;s:19:"PlanarConfiguration";i:1;s:22:"MEDIAWIKI_EXIF_VERSION";i:1;}s:21:"TIFF_METADATA_VERSION";s:3:"1.4";}'; $res = $this->handler->isMetadataValid( null, $meta ); $this->assertEquals( ExifBitmapHandler::METADATA_BAD, $res ); } - function testConvertMetadataLatest() { + /** + * @covers ExifBitmapHandler::convertMetadataVersion + */ + public function testConvertMetadataLatest() { $metadata = array( - 'foo' => array( 'First', 'Second', '_type' => 'ol' ), - 'MEDIAWIKI_EXIF_VERSION' => 2 - ); + 'foo' => array( 'First', 'Second', '_type' => 'ol' ), + 'MEDIAWIKI_EXIF_VERSION' => 2 + ); $res = $this->handler->convertMetadataVersion( $metadata, 2 ); $this->assertEquals( $metadata, $res ); } - function testConvertMetadataToOld() { + + /** + * @covers ExifBitmapHandler::convertMetadataVersion + */ + public function testConvertMetadataToOld() { $metadata = array( 'foo' => array( 'First', 'Second', '_type' => 'ol' ), 'bar' => array( 'First', 'Second', '_type' => 'ul' ), @@ -73,9 +104,13 @@ class ExifBitmapTest extends MediaWikiTestCase { $res = $this->handler->convertMetadataVersion( $metadata, 1 ); $this->assertEquals( $expected, $res ); } - function testConvertMetadataSoftware() { + + /** + * @covers ExifBitmapHandler::convertMetadataVersion + */ + public function testConvertMetadataSoftware() { $metadata = array( - 'Software' => array( array('GIMP', '1.1' ) ), + 'Software' => array( array( 'GIMP', '1.1' ) ), 'MEDIAWIKI_EXIF_VERSION' => 2, ); $expected = array( @@ -85,7 +120,11 @@ class ExifBitmapTest extends MediaWikiTestCase { $res = $this->handler->convertMetadataVersion( $metadata, 1 ); $this->assertEquals( $expected, $res ); } - function testConvertMetadataSoftwareNormal() { + + /** + * @covers ExifBitmapHandler::convertMetadataVersion + */ + public function testConvertMetadataSoftwareNormal() { $metadata = array( 'Software' => array( "GIMP 1.2", "vim" ), 'MEDIAWIKI_EXIF_VERSION' => 2, diff --git a/tests/phpunit/includes/media/ExifRotationTest.php b/tests/phpunit/includes/media/ExifRotationTest.php index 6af52dd1..64276d92 100644 --- a/tests/phpunit/includes/media/ExifRotationTest.php +++ b/tests/phpunit/includes/media/ExifRotationTest.php @@ -1,51 +1,44 @@ <?php - /** - * Tests related to auto rotation + * Tests related to auto rotation. + * + * @group medium + * + * @todo covers tags */ class ExifRotationTest extends MediaWikiTestCase { - function setUp() { + protected function setUp() { parent::setUp(); + if ( !extension_loaded( 'exif' ) ) { + $this->markTestSkipped( "This test needs the exif extension." ); + } + $this->handler = new BitmapHandler(); $filePath = __DIR__ . '/../../data/media'; $tmpDir = $this->getNewTempDirectory(); $this->repo = new FSRepo( array( - 'name' => 'temp', - 'url' => 'http://localhost/thumbtest', - 'backend' => new FSFileBackend( array( - 'name' => 'localtesting', - 'lockManager' => 'nullLockManager', + 'name' => 'temp', + 'url' => 'http://localhost/thumbtest', + 'backend' => new FSFileBackend( array( + 'name' => 'localtesting', + 'lockManager' => 'nullLockManager', 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath ) ) ) ) ); - if ( !wfDl( 'exif' ) ) { - $this->markTestSkipped( "This test needs the exif extension." ); - } - global $wgShowEXIF; - $this->show = $wgShowEXIF; - $wgShowEXIF = true; - - global $wgEnableAutoRotation; - $this->oldAuto = $wgEnableAutoRotation; - $wgEnableAutoRotation = true; - } - - public function tearDown() { - global $wgShowEXIF, $wgEnableAutoRotation; - $wgShowEXIF = $this->show; - $wgEnableAutoRotation = $this->oldAuto; - parent::tearDown(); + $this->setMwGlobals( array( + 'wgShowEXIF' => true, + 'wgEnableAutoRotation' => true, + ) ); } /** - * - * @dataProvider providerFiles + * @dataProvider provideFiles */ - function testMetadata( $name, $type, $info ) { + public function testMetadata( $name, $type, $info ) { if ( !BitmapHandler::canRotate() ) { $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." ); } @@ -56,14 +49,14 @@ class ExifRotationTest extends MediaWikiTestCase { /** * - * @dataProvider providerFiles + * @dataProvider provideFiles */ - function testRotationRendering( $name, $type, $info, $thumbs ) { + public function testRotationRendering( $name, $type, $info, $thumbs ) { if ( !BitmapHandler::canRotate() ) { $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." ); } - foreach( $thumbs as $size => $out ) { - if( preg_match('/^(\d+)px$/', $size, $matches ) ) { + foreach ( $thumbs as $size => $out ) { + if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { $params = array( 'width' => $matches[1], ); @@ -73,7 +66,7 @@ class ExifRotationTest extends MediaWikiTestCase { 'height' => $matches[2] ); } else { - throw new MWException('bogus test data format ' . $size); + throw new MWException( 'bogus test data format ' . $size ); } $file = $this->dataFile( $name, $type ); @@ -83,23 +76,24 @@ class ExifRotationTest extends MediaWikiTestCase { $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" ); $gis = getimagesize( $thumb->getLocalCopyPath() ); - if ($out[0] > $info['width']) { + if ( $out[0] > $info['width'] ) { // Physical image won't be scaled bigger than the original. - $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size"); - $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size"); + $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" ); + $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" ); } else { - $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size"); - $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size"); + $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" ); + $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); } } } + /* Utility function */ private function dataFile( $name, $type ) { return new UnregisteredLocalFile( false, $this->repo, "mwstore://localtesting/data/$name", $type ); } - function providerFiles() { + public static function provideFiles() { return array( array( 'landscape-plain.jpg', @@ -134,29 +128,25 @@ class ExifRotationTest extends MediaWikiTestCase { /** * Same as before, but with auto-rotation disabled. - * @dataProvider providerFilesNoAutoRotate + * @dataProvider provideFilesNoAutoRotate */ - function testMetadataNoAutoRotate( $name, $type, $info ) { - global $wgEnableAutoRotation; - $wgEnableAutoRotation = false; + public function testMetadataNoAutoRotate( $name, $type, $info ) { + $this->setMwGlobals( 'wgEnableAutoRotation', false ); $file = $this->dataFile( $name, $type ); $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" ); $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" ); - - $wgEnableAutoRotation = true; } /** * - * @dataProvider providerFilesNoAutoRotate + * @dataProvider provideFilesNoAutoRotate */ - function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) { - global $wgEnableAutoRotation; - $wgEnableAutoRotation = false; + public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) { + $this->setMwGlobals( 'wgEnableAutoRotation', false ); - foreach( $thumbs as $size => $out ) { - if( preg_match('/^(\d+)px$/', $size, $matches ) ) { + foreach ( $thumbs as $size => $out ) { + if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) { $params = array( 'width' => $matches[1], ); @@ -166,7 +156,7 @@ class ExifRotationTest extends MediaWikiTestCase { 'height' => $matches[2] ); } else { - throw new MWException('bogus test data format ' . $size); + throw new MWException( 'bogus test data format ' . $size ); } $file = $this->dataFile( $name, $type ); @@ -176,19 +166,18 @@ class ExifRotationTest extends MediaWikiTestCase { $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" ); $gis = getimagesize( $thumb->getLocalCopyPath() ); - if ($out[0] > $info['width']) { + if ( $out[0] > $info['width'] ) { // Physical image won't be scaled bigger than the original. - $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size"); - $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size"); + $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" ); + $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" ); } else { - $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size"); - $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size"); + $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" ); + $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" ); } } - $wgEnableAutoRotation = true; } - function providerFilesNoAutoRotate() { + public static function provideFilesNoAutoRotate() { return array( array( 'landscape-plain.jpg', @@ -220,41 +209,40 @@ class ExifRotationTest extends MediaWikiTestCase { ) ); } - - + + const TEST_WIDTH = 100; const TEST_HEIGHT = 200; - + /** * @dataProvider provideBitmapExtractPreRotationDimensions */ - function testBitmapExtractPreRotationDimensions( $rotation, $expected ) { + public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) { $result = $this->handler->extractPreRotationDimensions( array( - 'physicalWidth' => self::TEST_WIDTH, - 'physicalHeight' => self::TEST_HEIGHT, - ), $rotation ); + 'physicalWidth' => self::TEST_WIDTH, + 'physicalHeight' => self::TEST_HEIGHT, + ), $rotation ); $this->assertEquals( $expected, $result ); } - - function provideBitmapExtractPreRotationDimensions() { + + public static function provideBitmapExtractPreRotationDimensions() { return array( array( 0, - array( self::TEST_WIDTH, self::TEST_HEIGHT ) + array( self::TEST_WIDTH, self::TEST_HEIGHT ) ), array( 90, - array( self::TEST_HEIGHT, self::TEST_WIDTH ) + array( self::TEST_HEIGHT, self::TEST_WIDTH ) ), array( 180, - array( self::TEST_WIDTH, self::TEST_HEIGHT ) + array( self::TEST_WIDTH, self::TEST_HEIGHT ) ), array( 270, - array( self::TEST_HEIGHT, self::TEST_WIDTH ) + array( self::TEST_HEIGHT, self::TEST_WIDTH ) ), ); } } - diff --git a/tests/phpunit/includes/media/ExifTest.php b/tests/phpunit/includes/media/ExifTest.php index 045777d7..dea36b03 100644 --- a/tests/phpunit/includes/media/ExifTest.php +++ b/tests/phpunit/includes/media/ExifTest.php @@ -1,25 +1,25 @@ <?php class ExifTest extends MediaWikiTestCase { - public function setUp() { - $this->mediaPath = __DIR__ . '/../../data/media/'; + /** @var string */ + protected $mediaPath; - if ( !wfDl( 'exif' ) ) { + protected function setUp() { + parent::setUp(); + if ( !extension_loaded( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } - global $wgShowEXIF; - $this->showExif = $wgShowEXIF; - $wgShowEXIF = true; - } - public function tearDown() { - global $wgShowEXIF; - $wgShowEXIF = $this->showExif; + $this->mediaPath = __DIR__ . '/../../data/media/'; + + + + $this->setMwGlobals( 'wgShowEXIF', true ); } public function testGPSExtraction() { $filename = $this->mediaPath . 'exif-gps.jpg'; - $seg = JpegMetadataExtractor::segmentSplitter( $filename ); + $seg = JpegMetadataExtractor::segmentSplitter( $filename ); $exif = new Exif( $filename, $seg['byteOrder'] ); $data = $exif->getFilteredData(); $expected = array( @@ -34,7 +34,7 @@ class ExifTest extends MediaWikiTestCase { public function testUnicodeUserComment() { $filename = $this->mediaPath . 'exif-user-comment.jpg'; - $seg = JpegMetadataExtractor::segmentSplitter( $filename ); + $seg = JpegMetadataExtractor::segmentSplitter( $filename ); $exif = new Exif( $filename, $seg['byteOrder'] ); $data = $exif->getFilteredData(); @@ -43,6 +43,4 @@ class ExifTest extends MediaWikiTestCase { ); $this->assertEquals( $expected, $data ); } - - } diff --git a/tests/phpunit/includes/media/FakeDimensionFile.php b/tests/phpunit/includes/media/FakeDimensionFile.php new file mode 100644 index 00000000..7926000b --- /dev/null +++ b/tests/phpunit/includes/media/FakeDimensionFile.php @@ -0,0 +1,28 @@ +<?php + +class FakeDimensionFile extends File { + public $mustRender = false; + + public function __construct( $dimensions ) { + parent::__construct( Title::makeTitle( NS_FILE, 'Test' ), + new NullRepo( null ) ); + + $this->dimensions = $dimensions; + } + + public function getWidth( $page = 1 ) { + return $this->dimensions[0]; + } + + public function getHeight( $page = 1 ) { + return $this->dimensions[1]; + } + + public function mustRender() { + return $this->mustRender; + } + + public function getPath() { + return ''; + } +}
\ No newline at end of file diff --git a/tests/phpunit/includes/media/FormatMetadataTest.php b/tests/phpunit/includes/media/FormatMetadataTest.php index 6ade6702..a073e4ca 100644 --- a/tests/phpunit/includes/media/FormatMetadataTest.php +++ b/tests/phpunit/includes/media/FormatMetadataTest.php @@ -1,36 +1,43 @@ <?php + +/** + * @todo covers tags + */ class FormatMetadataTest extends MediaWikiTestCase { - public function setUp() { - if ( !wfDl( 'exif' ) ) { + + /** @var FSFileBackend */ + protected $backend; + /** @var FSRepo */ + protected $repo; + + protected function setUp() { + parent::setUp(); + + if ( !extension_loaded( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } - $filePath = __DIR__ . '/../../data/media'; + $filePath = __DIR__ . '/../../data/media'; $this->backend = new FSFileBackend( array( - 'name' => 'localtesting', - 'lockManager' => 'nullLockManager', + 'name' => 'localtesting', + 'lockManager' => 'nullLockManager', 'containerPaths' => array( 'data' => $filePath ) ) ); $this->repo = new FSRepo( array( - 'name' => 'temp', - 'url' => 'http://localhost/thumbtest', + 'name' => 'temp', + 'url' => 'http://localhost/thumbtest', 'backend' => $this->backend ) ); - global $wgShowEXIF; - $this->show = $wgShowEXIF; - $wgShowEXIF = true; - } - public function tearDown() { - global $wgShowEXIF; - $wgShowEXIF = $this->show; + + $this->setMwGlobals( 'wgShowEXIF', true ); } public function testInvalidDate() { $file = $this->dataFile( 'broken_exif_date.jpg', 'image/jpeg' ); - + // Throws an error if bug hit $meta = $file->formatMetadata(); $this->assertNotEquals( false, $meta, 'Valid metadata extracted' ); - + // Find date exif entry $this->assertArrayHasKey( 'visible', $meta ); $dateIndex = null; @@ -40,7 +47,7 @@ class FormatMetadataTest extends MediaWikiTestCase { } } $this->assertNotNull( $dateIndex, 'Date entry exists in metadata' ); - $this->assertEquals( '0000:01:00 00:02:27', + $this->assertEquals( '0000:01:00 00:02:27', $meta['visible'][$dateIndex]['value'], 'File with invalid date metadata (bug 29471)' ); } diff --git a/tests/phpunit/includes/media/GIFMetadataExtractorTest.php b/tests/phpunit/includes/media/GIFMetadataExtractorTest.php index 650fdd5c..9e3f9244 100644 --- a/tests/phpunit/includes/media/GIFMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/GIFMetadataExtractorTest.php @@ -1,20 +1,25 @@ <?php class GIFMetadataExtractorTest extends MediaWikiTestCase { - public function setUp() { + protected function setUp() { + parent::setUp(); + $this->mediaPath = __DIR__ . '/../../data/media/'; } + /** * Put in a file, and see if the metadata coming out is as expected. * @param $filename String * @param $expected Array The extracted metadata. - * @dataProvider dataGetMetadata + * @dataProvider provideGetMetadata + * @covers GIFMetadataExtractor::getMetadata */ public function testGetMetadata( $filename, $expected ) { $actual = GIFMetadataExtractor::getMetadata( $this->mediaPath . $filename ); $this->assertEquals( $expected, $actual ); } - public function dataGetMetadata() { + + public static function provideGetMetadata() { $xmpNugget = <<<EOF <?xpacket begin='' id='W5M0MpCehiHzreSzNTczkc9d'?> @@ -66,29 +71,35 @@ EOF; $xmpNugget = str_replace( "\r", '', $xmpNugget ); // Windows compat return array( - array( 'nonanimated.gif', array( - 'comment' => array( 'GIF test file ⁕ Created with GIMP' ), - 'duration' => 0.1, - 'frameCount' => 1, - 'looped' => false, - 'xmp' => '', + array( + 'nonanimated.gif', + array( + 'comment' => array( 'GIF test file ⁕ Created with GIMP' ), + 'duration' => 0.1, + 'frameCount' => 1, + 'looped' => false, + 'xmp' => '', ) ), - array( 'animated.gif', array( - 'comment' => array( 'GIF test file . Created with GIMP' ), - 'duration' => 2.4, - 'frameCount' => 4, - 'looped' => true, - 'xmp' => '', + array( + 'animated.gif', + array( + 'comment' => array( 'GIF test file . Created with GIMP' ), + 'duration' => 2.4, + 'frameCount' => 4, + 'looped' => true, + 'xmp' => '', ) ), - array( 'animated-xmp.gif', array( - 'xmp' => $xmpNugget, - 'duration' => 2.4, - 'frameCount' => 4, - 'looped' => true, - 'comment' => array( 'GIƒ·test·file' ), + array( + 'animated-xmp.gif', + array( + 'xmp' => $xmpNugget, + 'duration' => 2.4, + 'frameCount' => 4, + 'looped' => true, + 'comment' => array( 'GIƒ·test·file' ), ) ), ); diff --git a/tests/phpunit/includes/media/GIFTest.php b/tests/phpunit/includes/media/GIFTest.php index 5dcbeee0..c8e729c8 100644 --- a/tests/phpunit/includes/media/GIFTest.php +++ b/tests/phpunit/includes/media/GIFTest.php @@ -1,36 +1,53 @@ <?php class GIFHandlerTest extends MediaWikiTestCase { - public function setUp() { - $this->filePath = __DIR__ . '/../../data/media'; + /** @var FSFileBackend */ + protected $backend; + /** @var GIFHandler */ + protected $handler; + /** @var FSRepo */ + protected $repo; + /** @var string */ + protected $filePath; + + protected function setUp() { + parent::setUp(); + + $this->filePath = __DIR__ . '/../../data/media'; $this->backend = new FSFileBackend( array( - 'name' => 'localtesting', - 'lockManager' => 'nullLockManager', + 'name' => 'localtesting', + 'lockManager' => 'nullLockManager', 'containerPaths' => array( 'data' => $this->filePath ) ) ); $this->repo = new FSRepo( array( - 'name' => 'temp', - 'url' => 'http://localhost/thumbtest', + 'name' => 'temp', + 'url' => 'http://localhost/thumbtest', 'backend' => $this->backend ) ); $this->handler = new GIFHandler(); } + /** + * @covers GIFHandler::getMetadata + */ public function testInvalidFile() { $res = $this->handler->getMetadata( null, $this->filePath . '/README' ); $this->assertEquals( GIFHandler::BROKEN_FILE, $res ); } + /** * @param $filename String basename of the file to check * @param $expected boolean Expected result. - * @dataProvider dataIsAnimated + * @dataProvider provideIsAnimated + * @covers GIFHandler::isAnimatedImage */ public function testIsAnimanted( $filename, $expected ) { $file = $this->dataFile( $filename, 'image/gif' ); $actual = $this->handler->isAnimatedImage( $file ); $this->assertEquals( $expected, $actual ); } - public function dataIsAnimated() { + + public static function provideIsAnimated() { return array( array( 'animated.gif', true ), array( 'nonanimated.gif', false ), @@ -40,14 +57,16 @@ class GIFHandlerTest extends MediaWikiTestCase { /** * @param $filename String * @param $expected Integer Total image area - * @dataProvider dataGetImageArea + * @dataProvider provideGetImageArea + * @covers GIFHandler::getImageArea */ public function testGetImageArea( $filename, $expected ) { $file = $this->dataFile( $filename, 'image/gif' ); $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() ); $this->assertEquals( $expected, $actual ); } - public function dataGetImageArea() { + + public static function provideGetImageArea() { return array( array( 'animated.gif', 5400 ), array( 'nonanimated.gif', 1350 ), @@ -57,13 +76,15 @@ class GIFHandlerTest extends MediaWikiTestCase { /** * @param $metadata String Serialized metadata * @param $expected Integer One of the class constants of GIFHandler - * @dataProvider dataIsMetadataValid + * @dataProvider provideIsMetadataValid + * @covers GIFHandler::isMetadataValid */ public function testIsMetadataValid( $metadata, $expected ) { $actual = $this->handler->isMetadataValid( null, $metadata ); $this->assertEquals( $expected, $actual ); } - public function dataIsMetadataValid() { + + public static function provideIsMetadataValid() { return array( array( GIFHandler::BROKEN_FILE, GIFHandler::METADATA_GOOD ), array( '', GIFHandler::METADATA_BAD ), @@ -76,7 +97,8 @@ class GIFHandlerTest extends MediaWikiTestCase { /** * @param $filename String * @param $expected String Serialized array - * @dataProvider dataGetMetadata + * @dataProvider provideGetMetadata + * @covers GIFHandler::getMetadata */ public function testGetMetadata( $filename, $expected ) { $file = $this->dataFile( $filename, 'image/gif' ); @@ -84,7 +106,7 @@ class GIFHandlerTest extends MediaWikiTestCase { $this->assertEquals( unserialize( $expected ), unserialize( $actual ) ); } - public function dataGetMetadata() { + public static function provideGetMetadata() { return array( array( 'nonanimated.gif', 'a:4:{s:10:"frameCount";i:1;s:6:"looped";b:0;s:8:"duration";d:0.1000000000000000055511151231257827021181583404541015625;s:8:"metadata";a:2:{s:14:"GIFFileComment";a:1:{i:0;s:35:"GIF test file ⁕ Created with GIMP";}s:15:"_MW_GIF_VERSION";i:1;}}' ), array( 'animated-xmp.gif', 'a:4:{s:10:"frameCount";i:4;s:6:"looped";b:1;s:8:"duration";d:2.399999999999999911182158029987476766109466552734375;s:8:"metadata";a:5:{s:6:"Artist";s:7:"Bawolff";s:16:"ImageDescription";a:2:{s:9:"x-default";s:18:"A file to test GIF";s:5:"_type";s:4:"lang";}s:15:"SublocationDest";s:13:"The interwebs";s:14:"GIFFileComment";a:1:{i:0;s:16:"GIƒ·test·file";}s:15:"_MW_GIF_VERSION";i:1;}}' ), diff --git a/tests/phpunit/includes/media/IPTCTest.php b/tests/phpunit/includes/media/IPTCTest.php index ec6deeb8..81c1d287 100644 --- a/tests/phpunit/includes/media/IPTCTest.php +++ b/tests/phpunit/includes/media/IPTCTest.php @@ -1,11 +1,19 @@ <?php + class IPTCTest extends MediaWikiTestCase { + + /** + * @covers IPTC::getCharset + */ public function testRecognizeUtf8() { // utf-8 is the only one used in practise. $res = IPTC::getCharset( "\x1b%G" ); $this->assertEquals( 'UTF-8', $res ); } + /** + * @covers IPTC::Parse + */ public function testIPTCParseNoCharset88591() { // basically IPTC for keyword with value of 0xBC which is 1/4 in iso-8859-1 // This data doesn't specify a charset. We're supposed to guess @@ -14,16 +22,23 @@ class IPTCTest extends MediaWikiTestCase { $res = IPTC::Parse( $iptcData ); $this->assertEquals( array( '¼' ), $res['Keywords'] ); } - /* This one contains a sequence that's valid iso 8859-1 but not valid utf8 */ - /* \xC3 = Ã, \xB8 = ¸ */ + + /** + * @covers IPTC::Parse + */ public function testIPTCParseNoCharset88591b() { + /* This one contains a sequence that's valid iso 8859-1 but not valid utf8 */ + /* \xC3 = Ã, \xB8 = ¸ */ $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x09\x1c\x02\x19\x00\x04\xC3\xC3\xC3\xB8"; $res = IPTC::Parse( $iptcData ); $this->assertEquals( array( 'ÃÃø' ), $res['Keywords'] ); } - /* Same as testIPTCParseNoCharset88591b, but forcing the charset to utf-8. + + /** + * Same as testIPTCParseNoCharset88591b, but forcing the charset to utf-8. * What should happen is the first "\xC3\xC3" should be dropped as invalid, * leaving \xC3\xB8, which is ø + * @covers IPTC::Parse */ public function testIPTCParseForcedUTFButInvalid() { $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x11\x1c\x02\x19\x00\x04\xC3\xC3\xC3\xB8" @@ -31,12 +46,20 @@ class IPTCTest extends MediaWikiTestCase { $res = IPTC::Parse( $iptcData ); $this->assertEquals( array( 'ø' ), $res['Keywords'] ); } + + /** + * @covers IPTC::Parse + */ public function testIPTCParseNoCharsetUTF8() { $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x07\x1c\x02\x19\x00\x02¼"; $res = IPTC::Parse( $iptcData ); $this->assertEquals( array( '¼' ), $res['Keywords'] ); } - // Testing something that has 2 values for keyword + + /** + * Testing something that has 2 values for keyword + * @covers IPTC::Parse + */ public function testIPTCParseMulti() { $iptcData = /* identifier */ "Photoshop 3.0\08BIM\4\4" /* length */ . "\0\0\0\0\0\x0D" @@ -45,11 +68,14 @@ class IPTCTest extends MediaWikiTestCase { $res = IPTC::Parse( $iptcData ); $this->assertEquals( array( '¼', '¼½' ), $res['Keywords'] ); } + + /** + * @covers IPTC::Parse + */ public function testIPTCParseUTF8() { // This has the magic "\x1c\x01\x5A\x00\x03\x1B\x25\x47" which marks content as UTF8. $iptcData = "Photoshop 3.0\08BIM\4\4\0\0\0\0\0\x0F\x1c\x02\x19\x00\x02¼\x1c\x01\x5A\x00\x03\x1B\x25\x47"; $res = IPTC::Parse( $iptcData ); $this->assertEquals( array( '¼' ), $res['Keywords'] ); } - } diff --git a/tests/phpunit/includes/media/JpegMetadataExtractorTest.php b/tests/phpunit/includes/media/JpegMetadataExtractorTest.php index 41d81190..eafc8a2e 100644 --- a/tests/phpunit/includes/media/JpegMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/JpegMetadataExtractorTest.php @@ -5,10 +5,15 @@ * serve as a very good "test". (Adobe photoshop probably creates such files * but it costs money). The implementation of it currently in MediaWiki is based * solely on reading the standard, without any real world test files. + * @todo covers tags */ class JpegMetadataExtractorTest extends MediaWikiTestCase { - public function setUp() { + protected $filePath; + + protected function setUp() { + parent::setUp(); + $this->filePath = __DIR__ . '/../../data/media/'; } @@ -16,26 +21,29 @@ class JpegMetadataExtractorTest extends MediaWikiTestCase { * We also use this test to test padding bytes don't * screw stuff up * - * @param $file filename + * @param string $file filename * - * @dataProvider dataUtf8Comment + * @dataProvider provideUtf8Comment */ public function testUtf8Comment( $file ) { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . $file ); $this->assertEquals( array( 'UTF-8 JPEG Comment — ¼' ), $res['COM'] ); } - public function dataUtf8Comment() { + + public static function provideUtf8Comment() { return array( array( 'jpeg-comment-utf.jpg' ), array( 'jpeg-padding-even.jpg' ), array( 'jpeg-padding-odd.jpg' ), ); } + /** The file is iso-8859-1, but it should get auto converted */ public function testIso88591Comment() { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-iso8859-1.jpg' ); $this->assertEquals( array( 'ISO-8859-1 JPEG Comment - ¼' ), $res['COM'] ); } + /** Comment values that are non-textual (random binary junk) should not be shown. * The example test file has a comment with a 0x5 byte in it which is a control character * and considered binary junk for our purposes. @@ -44,6 +52,7 @@ class JpegMetadataExtractorTest extends MediaWikiTestCase { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-binary.jpg' ); $this->assertEmpty( $res['COM'] ); } + /* Very rarely a file can have multiple comments. * Order of comments is based on order inside the file. */ @@ -51,16 +60,19 @@ class JpegMetadataExtractorTest extends MediaWikiTestCase { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-comment-multiple.jpg' ); $this->assertEquals( array( 'foo', 'bar' ), $res['COM'] ); } + public function testXMPExtraction() { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' ); $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' ); $this->assertEquals( $expected, $res['XMP'] ); } + public function testPSIRExtraction() { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-psir.jpg' ); $expected = '50686f746f73686f7020332e30003842494d04040000000000181c02190004746573741c02190003666f6f1c020000020004'; $this->assertEquals( $expected, bin2hex( $res['PSIR'][0] ) ); } + public function testXMPExtractionAltAppId() { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-xmp-alt.jpg' ); $expected = file_get_contents( $this->filePath . 'jpeg-xmp-psir.xmp' ); @@ -74,18 +86,21 @@ class JpegMetadataExtractorTest extends MediaWikiTestCase { $this->assertEquals( 'iptc-no-hash', $res ); } + public function testIPTCHashComparisionBadHash() { $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-bad-hash.jpg' ); $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] ); $this->assertEquals( 'iptc-bad-hash', $res ); } + public function testIPTCHashComparisionGoodHash() { $segments = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'jpeg-iptc-good-hash.jpg' ); $res = JpegMetadataExtractor::doPSIR( $segments['PSIR'][0] ); $this->assertEquals( 'iptc-good-hash', $res ); } + public function testExifByteOrder() { $res = JpegMetadataExtractor::segmentSplitter( $this->filePath . 'exif-user-comment.jpg' ); $expected = 'BE'; diff --git a/tests/phpunit/includes/media/JpegTest.php b/tests/phpunit/includes/media/JpegTest.php index ea007f90..9af4f1e1 100644 --- a/tests/phpunit/includes/media/JpegTest.php +++ b/tests/phpunit/includes/media/JpegTest.php @@ -1,18 +1,19 @@ <?php +/** + * @todo covers tags + */ class JpegTest extends MediaWikiTestCase { - public function setUp() { - $this->filePath = __DIR__ . '/../../data/media/'; - if ( !wfDl( 'exif' ) ) { + protected function setUp() { + parent::setUp(); + if ( !extension_loaded( 'exif' ) ) { $this->markTestSkipped( "This test needs the exif extension." ); } - global $wgShowEXIF; - $this->show = $wgShowEXIF; - $wgShowEXIF = true; - } - public function tearDown() { - global $wgShowEXIF; - $wgShowEXIF = $this->show; + + $this->filePath = __DIR__ . '/../../data/media/'; + + + $this->setMwGlobals( 'wgShowEXIF', true ); } public function testInvalidFile() { @@ -20,6 +21,7 @@ class JpegTest extends MediaWikiTestCase { $res = $jpeg->getMetadata( null, $this->filePath . 'README' ); $this->assertEquals( ExifBitmapHandler::BROKEN_FILE, $res ); } + public function testJpegMetadataExtraction() { $h = new JpegHandler; $res = $h->getMetadata( null, $this->filePath . 'test.jpg' ); diff --git a/tests/phpunit/includes/media/MediaHandlerTest.php b/tests/phpunit/includes/media/MediaHandlerTest.php index 99df4f80..c28898bb 100644 --- a/tests/phpunit/includes/media/MediaHandlerTest.php +++ b/tests/phpunit/includes/media/MediaHandlerTest.php @@ -1,7 +1,12 @@ <?php class MediaHandlerTest extends MediaWikiTestCase { - function testFitBoxWidth() { + + /** + * @covers MediaHandler::fitBoxWidth + * @todo split into a dataprovider and test method + */ + public function testFitBoxWidth() { $vals = array( array( 'width' => 50, @@ -46,5 +51,3 @@ class MediaHandlerTest extends MediaWikiTestCase { } } } - - diff --git a/tests/phpunit/includes/media/PNGMetadataExtractorTest.php b/tests/phpunit/includes/media/PNGMetadataExtractorTest.php index 1b1b2ec3..939f2cfc 100644 --- a/tests/phpunit/includes/media/PNGMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/PNGMetadataExtractorTest.php @@ -1,13 +1,21 @@ <?php + +/** + * @todo covers tags + */ class PNGMetadataExtractorTest extends MediaWikiTestCase { - function setUp() { + protected function setUp() { + parent::setUp(); $this->filePath = __DIR__ . '/../../data/media/'; } + /** - * Tests zTXt tag (compressed textual metadata) + * Tests zTXt tag (compressed textual metadata) */ - function testPngNativetZtxt() { + public function testPngNativetZtxt() { + $this->checkPHPExtension( 'zlib' ); + $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); $expected = "foo bar baz foo foo foo foof foo foo foo foo"; @@ -22,7 +30,7 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { /** * Test tEXt tag (Uncompressed textual metadata) */ - function testPngNativeText() { + public function testPngNativeText() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); $expected = "Some long image desc"; @@ -39,7 +47,7 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { * tEXt tags must be encoded iso-8859-1 (vs iTXt which are utf-8) * Make sure non-ascii characters get converted properly */ - function testPngNativeTextNonAscii() { + public function testPngNativeTextNonAscii() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); @@ -48,7 +56,6 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { // encoded as just \xA9. $expected = "© 2010 Bawolff"; - $this->assertArrayHasKey( 'text', $meta ); $meta = $meta['text']; $this->assertArrayHasKey( 'Copyright', $meta ); @@ -60,7 +67,9 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { /** * Test extraction of pHYs tags, which can tell what the * actual resolution of the image is (aka in dots per meter). - function testPngPhysTag () { + */ + /* + public function testPngPhysTag() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); @@ -71,11 +80,12 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { $this->assertEquals( '2835/100', $meta['YResolution'] ); $this->assertEquals( 3, $meta['ResolutionUnit'] ); // 3 = cm } + */ /** * Given a normal static PNG, check the animation metadata returned. */ - function testStaticPngAnimationMetadata() { + public function testStaticPngAnimationMetadata() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); @@ -88,7 +98,7 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { * Given an animated APNG image file * check it gets animated metadata right. */ - function testApngAnimationMetadata() { + public function testApngAnimationMetadata() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Animated_PNG_example_bouncing_beach_ball.png' ); @@ -98,44 +108,48 @@ class PNGMetadataExtractorTest extends MediaWikiTestCase { $this->assertEquals( 1.5, $meta['duration'], '', 0.00001 ); } - function testPngBitDepth8() { + public function testPngBitDepth8() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); $this->assertEquals( 8, $meta['bitDepth'] ); } - function testPngBitDepth1() { + + public function testPngBitDepth1() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . '1bit-png.png' ); $this->assertEquals( 1, $meta['bitDepth'] ); } - function testPngIndexColour() { + public function testPngIndexColour() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'Png-native-test.png' ); $this->assertEquals( 'index-coloured', $meta['colorType'] ); } - function testPngRgbColour() { + + public function testPngRgbColour() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'rgb-png.png' ); $this->assertEquals( 'truecolour-alpha', $meta['colorType'] ); } - function testPngRgbNoAlphaColour() { + + public function testPngRgbNoAlphaColour() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'rgb-na-png.png' ); $this->assertEquals( 'truecolour', $meta['colorType'] ); } - function testPngGreyscaleColour() { + + public function testPngGreyscaleColour() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'greyscale-png.png' ); $this->assertEquals( 'greyscale-alpha', $meta['colorType'] ); } - function testPngGreyscaleNoAlphaColour() { + + public function testPngGreyscaleNoAlphaColour() { $meta = PNGMetadataExtractor::getMetadata( $this->filePath . 'greyscale-na-png.png' ); $this->assertEquals( 'greyscale', $meta['colorType'] ); } - } diff --git a/tests/phpunit/includes/media/PNGTest.php b/tests/phpunit/includes/media/PNGTest.php index fe73c9c7..ad4c2493 100644 --- a/tests/phpunit/includes/media/PNGTest.php +++ b/tests/phpunit/includes/media/PNGTest.php @@ -1,36 +1,53 @@ <?php class PNGHandlerTest extends MediaWikiTestCase { - public function setUp() { - $this->filePath = __DIR__ . '/../../data/media'; + /** @var PNGHandler */ + protected $handler; + /** @var FSRepo */ + protected $repo; + /** @var FSFileBackend */ + protected $backend; + /** @var string */ + protected $filePath; + + protected function setUp() { + parent::setUp(); + + $this->filePath = __DIR__ . '/../../data/media'; $this->backend = new FSFileBackend( array( - 'name' => 'localtesting', - 'lockManager' => 'nullLockManager', + 'name' => 'localtesting', + 'lockManager' => 'nullLockManager', 'containerPaths' => array( 'data' => $this->filePath ) ) ); $this->repo = new FSRepo( array( - 'name' => 'temp', - 'url' => 'http://localhost/thumbtest', + 'name' => 'temp', + 'url' => 'http://localhost/thumbtest', 'backend' => $this->backend ) ); $this->handler = new PNGHandler(); } + /** + * @covers PNGHandler::getMetadata + */ public function testInvalidFile() { $res = $this->handler->getMetadata( null, $this->filePath . '/README' ); $this->assertEquals( PNGHandler::BROKEN_FILE, $res ); } + /** * @param $filename String basename of the file to check * @param $expected boolean Expected result. - * @dataProvider dataIsAnimated + * @dataProvider provideIsAnimated + * @covers PNGHandler::isAnimatedImage */ public function testIsAnimanted( $filename, $expected ) { $file = $this->dataFile( $filename, 'image/png' ); $actual = $this->handler->isAnimatedImage( $file ); $this->assertEquals( $expected, $actual ); } - public function dataIsAnimated() { + + public static function provideIsAnimated() { return array( array( 'Animated_PNG_example_bouncing_beach_ball.png', true ), array( '1bit-png.png', false ), @@ -40,14 +57,16 @@ class PNGHandlerTest extends MediaWikiTestCase { /** * @param $filename String * @param $expected Integer Total image area - * @dataProvider dataGetImageArea + * @dataProvider provideGetImageArea + * @covers PNGHandler::getImageArea */ public function testGetImageArea( $filename, $expected ) { - $file = $this->dataFile($filename, 'image/png' ); + $file = $this->dataFile( $filename, 'image/png' ); $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() ); $this->assertEquals( $expected, $actual ); } - public function dataGetImageArea() { + + public static function provideGetImageArea() { return array( array( '1bit-png.png', 2500 ), array( 'greyscale-png.png', 2500 ), @@ -59,13 +78,15 @@ class PNGHandlerTest extends MediaWikiTestCase { /** * @param $metadata String Serialized metadata * @param $expected Integer One of the class constants of PNGHandler - * @dataProvider dataIsMetadataValid + * @dataProvider provideIsMetadataValid + * @covers PNGHandler::isMetadataValid */ public function testIsMetadataValid( $metadata, $expected ) { $actual = $this->handler->isMetadataValid( null, $metadata ); $this->assertEquals( $expected, $actual ); } - public function dataIsMetadataValid() { + + public static function provideIsMetadataValid() { return array( array( PNGHandler::BROKEN_FILE, PNGHandler::METADATA_GOOD ), array( '', PNGHandler::METADATA_BAD ), @@ -78,7 +99,8 @@ class PNGHandlerTest extends MediaWikiTestCase { /** * @param $filename String * @param $expected String Serialized array - * @dataProvider dataGetMetadata + * @dataProvider provideGetMetadata + * @covers PNGHandler::getMetadata */ public function testGetMetadata( $filename, $expected ) { $file = $this->dataFile( $filename, 'image/png' ); @@ -86,10 +108,11 @@ class PNGHandlerTest extends MediaWikiTestCase { // $this->assertEquals( unserialize( $expected ), unserialize( $actual ) ); $this->assertEquals( ( $expected ), ( $actual ) ); } - public function dataGetMetadata() { + + public static function provideGetMetadata() { return array( array( 'rgb-na-png.png', 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}' ), - array( 'xmp.png', 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:1;s:9:"colorType";s:14:"index-coloured";s:8:"metadata";a:2:{s:12:"SerialNumber";s:9:"123456789";s:15:"_MW_PNG_VERSION";i:1;}}' ), + array( 'xmp.png', 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:1;s:9:"colorType";s:14:"index-coloured";s:8:"metadata";a:2:{s:12:"SerialNumber";s:9:"123456789";s:15:"_MW_PNG_VERSION";i:1;}}' ), ); } diff --git a/tests/phpunit/includes/media/SVGMetadataExtractorTest.php b/tests/phpunit/includes/media/SVGMetadataExtractorTest.php index 2116554e..257009b0 100644 --- a/tests/phpunit/includes/media/SVGMetadataExtractorTest.php +++ b/tests/phpunit/includes/media/SVGMetadataExtractorTest.php @@ -1,25 +1,30 @@ <?php +/** + * @todo covers tags + */ class SVGMetadataExtractorTest extends MediaWikiTestCase { - function setUp() { + protected function setUp() { + parent::setUp(); AutoLoader::loadClass( 'SVGMetadataExtractorTest' ); } /** - * @dataProvider providerSvgFiles + * @dataProvider provideSvgFiles */ - function testGetMetadata( $infile, $expected ) { + public function testGetMetadata( $infile, $expected ) { $this->assertMetadata( $infile, $expected ); } - + /** - * @dataProvider providerSvgFilesWithXMLMetadata + * @dataProvider provideSvgFilesWithXMLMetadata */ - function testGetXMLMetadata( $infile, $expected ) { + public function testGetXMLMetadata( $infile, $expected ) { $r = new XMLReader(); - if( !method_exists( $r, 'readInnerXML' ) ) { + if ( !method_exists( $r, 'readInnerXML' ) ) { $this->markTestSkipped( 'XMLReader::readInnerXML() does not exist (libxml >2.6.20 needed).' ); + return; } $this->assertMetadata( $infile, $expected ); @@ -38,8 +43,9 @@ class SVGMetadataExtractorTest extends MediaWikiTestCase { } } - function providerSvgFiles() { + public static function provideSvgFiles() { $base = __DIR__ . '/../../data/media'; + return array( array( "$base/Wikimedia-logo.svg", @@ -81,10 +87,9 @@ class SVGMetadataExtractorTest extends MediaWikiTestCase { ); } - function providerSvgFilesWithXMLMetadata() { + public static function provideSvgFilesWithXMLMetadata() { $base = __DIR__ . '/../../data/media'; - $metadata = - '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> + $metadata = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <ns4:Work xmlns:ns4="http://creativecommons.org/ns#" rdf:about=""> <ns5:format xmlns:ns5="http://purl.org/dc/elements/1.1/">image/svg+xml</ns5:format> <ns5:type xmlns:ns5="http://purl.org/dc/elements/1.1/" rdf:resource="http://purl.org/dc/dcmitype/StillImage"/> @@ -105,4 +110,3 @@ class SVGMetadataExtractorTest extends MediaWikiTestCase { ); } } - diff --git a/tests/phpunit/includes/media/TiffTest.php b/tests/phpunit/includes/media/TiffTest.php index 4c79f66c..8d74b98d 100644 --- a/tests/phpunit/includes/media/TiffTest.php +++ b/tests/phpunit/includes/media/TiffTest.php @@ -1,31 +1,35 @@ <?php class TiffTest extends MediaWikiTestCase { - public function setUp() { - global $wgShowEXIF; - $this->showExif = $wgShowEXIF; - $wgShowEXIF = true; + /** @var TiffHandler */ + protected $handler; + /** @var string */ + protected $filePath; + + protected function setUp() { + parent::setUp(); + if ( !extension_loaded( 'exif' ) ) { + $this->markTestSkipped( "This test needs the exif extension." ); + } + + $this->setMwGlobals( 'wgShowEXIF', true ); + $this->filePath = __DIR__ . '/../../data/media/'; $this->handler = new TiffHandler; } - public function tearDown() { - global $wgShowEXIF; - $wgShowEXIF = $this->showExif; - } - + /** + * @covers TiffHandler::getMetadata + */ public function testInvalidFile() { - if ( !wfDl( 'exif' ) ) { - $this->markTestIncomplete( "This test needs the exif extension." ); - } $res = $this->handler->getMetadata( null, $this->filePath . 'README' ); $this->assertEquals( ExifBitmapHandler::BROKEN_FILE, $res ); } + /** + * @covers TiffHandler::getMetadata + */ public function testTiffMetadataExtraction() { - if ( !wfDl( 'exif' ) ) { - $this->markTestIncomplete( "This test needs the exif extension." ); - } $res = $this->handler->getMetadata( null, $this->filePath . 'test.tiff' ); $expected = 'a:16:{s:10:"ImageWidth";i:20;s:11:"ImageLength";i:20;s:13:"BitsPerSample";a:3:{i:0;i:8;i:1;i:8;i:2;i:8;}s:11:"Compression";i:5;s:25:"PhotometricInterpretation";i:2;s:16:"ImageDescription";s:17:"Created with GIMP";s:12:"StripOffsets";i:8;s:11:"Orientation";i:1;s:15:"SamplesPerPixel";i:3;s:12:"RowsPerStrip";i:64;s:15:"StripByteCounts";i:238;s:11:"XResolution";s:19:"1207959552/16777216";s:11:"YResolution";s:19:"1207959552/16777216";s:19:"PlanarConfiguration";i:1;s:14:"ResolutionUnit";i:2;s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}'; // Re-unserialize in case there are subtle differences between how versions diff --git a/tests/phpunit/includes/media/XMPTest.php b/tests/phpunit/includes/media/XMPTest.php index 8198d3b0..d12e9b00 100644 --- a/tests/phpunit/includes/media/XMPTest.php +++ b/tests/phpunit/includes/media/XMPTest.php @@ -1,8 +1,13 @@ <?php + +/** + * @todo covers tags + */ class XMPTest extends MediaWikiTestCase { - function setUp() { - if ( !wfDl( 'xml' ) ) { + protected function setUp() { + parent::setUp(); + if ( !extension_loaded( 'xml' ) ) { $this->markTestSkipped( 'Requires libxml to do XMP parsing' ); } } @@ -14,7 +19,8 @@ class XMPTest extends MediaWikiTestCase { * @param $expected Array expected result of parsing the xmp. * @param $info String Short sentence on what's being tested. * - * @dataProvider dataXMPParse + * @throws Exception + * @dataProvider provideXMPParse */ public function testXMPParse( $xmp, $expected, $info ) { if ( !is_string( $xmp ) || !is_array( $expected ) ) { @@ -25,8 +31,8 @@ class XMPTest extends MediaWikiTestCase { $this->assertEquals( $expected, $reader->getResults(), $info, 0.0000000001 ); } - public function dataXMPParse() { - $xmpPath = __DIR__ . '/../../data/xmp/' ; + public static function provideXMPParse() { + $xmpPath = __DIR__ . '/../../data/xmp/'; $data = array(); // $xmpFiles format: array of arrays with first arg file base name, @@ -53,16 +59,18 @@ class XMPTest extends MediaWikiTestCase { array( 'utf32LE', 'UTF-32LE encoding' ), array( 'xmpExt', 'Extended XMP missing second part' ), array( 'gps', 'Handling of exif GPS parameters in XMP' ), - ); - foreach( $xmpFiles as $file ) { + ); + + foreach ( $xmpFiles as $file ) { $xmp = file_get_contents( $xmpPath . $file[0] . '.xmp' ); // I'm not sure if this is the best way to handle getting the // result array, but it seems kind of big to put directly in the test // file. $result = null; - include( $xmpPath . $file[0] . '.result.php' ); + include $xmpPath . $file[0] . '.result.php'; $data[] = array( $xmp, $result, '[' . $file[0] . '.xmp] ' . $file[1] ); } + return $data; } @@ -72,7 +80,7 @@ class XMPTest extends MediaWikiTestCase { * @todo This is based on what the standard says. Need to find a real * world example file to double check the support for this is right. */ - function testExtendedXMP() { + public function testExtendedXMP() { $xmpPath = __DIR__ . '/../../data/xmp/'; $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' ); $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' ); @@ -87,8 +95,8 @@ class XMPTest extends MediaWikiTestCase { $reader->parseExtended( $extendedPacket ); $actual = $reader->getResults(); - $expected = array( 'xmp-exif' => - array( + $expected = array( + 'xmp-exif' => array( 'DigitalZoomRatio' => '0/10', 'Flash' => 9, 'FNumber' => '2/10', @@ -102,7 +110,7 @@ class XMPTest extends MediaWikiTestCase { * This test has an extended XMP block with a wrong guid (md5sum) * and thus should only return the StandardXMP, not the ExtendedXMP. */ - function testExtendedXMPWithWrongGUID() { + public function testExtendedXMPWithWrongGUID() { $xmpPath = __DIR__ . '/../../data/xmp/'; $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' ); $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' ); @@ -117,8 +125,8 @@ class XMPTest extends MediaWikiTestCase { $reader->parseExtended( $extendedPacket ); $actual = $reader->getResults(); - $expected = array( 'xmp-exif' => - array( + $expected = array( + 'xmp-exif' => array( 'DigitalZoomRatio' => '0/10', 'Flash' => 9, ) @@ -126,11 +134,12 @@ class XMPTest extends MediaWikiTestCase { $this->assertEquals( $expected, $actual ); } + /** * Have a high offset to simulate a missing packet, * which should cause it to ignore the ExtendedXMP packet. */ - function testExtendedXMPMissingPacket() { + public function testExtendedXMPMissingPacket() { $xmpPath = __DIR__ . '/../../data/xmp/'; $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' ); $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' ); @@ -145,8 +154,8 @@ class XMPTest extends MediaWikiTestCase { $reader->parseExtended( $extendedPacket ); $actual = $reader->getResults(); - $expected = array( 'xmp-exif' => - array( + $expected = array( + 'xmp-exif' => array( 'DigitalZoomRatio' => '0/10', 'Flash' => 9, ) @@ -154,5 +163,4 @@ class XMPTest extends MediaWikiTestCase { $this->assertEquals( $expected, $actual ); } - } diff --git a/tests/phpunit/includes/media/XMPValidateTest.php b/tests/phpunit/includes/media/XMPValidateTest.php index e2bb8d8d..96bf5e47 100644 --- a/tests/phpunit/includes/media/XMPValidateTest.php +++ b/tests/phpunit/includes/media/XMPValidateTest.php @@ -2,15 +2,16 @@ class XMPValidateTest extends MediaWikiTestCase { /** - * @dataProvider providerDate + * @dataProvider provideDates + * @covers XMPValidate::validateDate */ - function testValidateDate( $value, $expected ) { + public function testValidateDate( $value, $expected ) { // The method should modify $value. XMPValidate::validateDate( array(), $value, true ); $this->assertEquals( $expected, $value ); } - function providerDate() { + public static function provideDates() { /* For reference valid date formats are: * YYYY * YYYY-MM @@ -41,7 +42,5 @@ class XMPValidateTest extends MediaWikiTestCase { array( '2001-05-12T15', null ), array( '2001-12T15:13', null ), ); - } - } |