Merge "Add an interface for getting "standard" file metadata."
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Tue, 29 Oct 2013 18:23:10 +0000 (18:23 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 29 Oct 2013 18:23:10 +0000 (18:23 +0000)
1  2 
tests/phpunit/includes/media/GIFTest.php
tests/phpunit/includes/media/JpegTest.php
tests/phpunit/includes/media/PNGTest.php
tests/phpunit/includes/media/SVGMetadataExtractorTest.php

@@@ -1,15 -1,6 +1,15 @@@
  <?php
  class GIFHandlerTest extends MediaWikiTestCase {
  
 +      /** @var FSFileBackend */
 +      protected $backend;
 +      /** @var GIFHandler */
 +      protected $handler;
 +      /** @var FSRepo */
 +      protected $repo;
 +      /** @var string */
 +      protected $filePath;
 +
        protected function setUp() {
                parent::setUp();
  
@@@ -27,9 -18,6 +27,9 @@@
                $this->handler = new GIFHandler();
        }
  
 +      /**
 +       * @covers GIFHandler::getMetadata
 +       */
        public function testInvalidFile() {
                $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
                $this->assertEquals( GIFHandler::BROKEN_FILE, $res );
@@@ -39,7 -27,6 +39,7 @@@
         * @param $filename String basename of the file to check
         * @param $expected boolean Expected result.
         * @dataProvider provideIsAnimated
 +       * @covers GIFHandler::isAnimatedImage
         */
        public function testIsAnimanted( $filename, $expected ) {
                $file = $this->dataFile( $filename, 'image/gif' );
@@@ -58,7 -45,6 +58,7 @@@
         * @param $filename String
         * @param $expected Integer Total image area
         * @dataProvider provideGetImageArea
 +       * @covers GIFHandler::getImageArea
         */
        public function testGetImageArea( $filename, $expected ) {
                $file = $this->dataFile( $filename, 'image/gif' );
@@@ -77,7 -63,6 +77,7 @@@
         * @param $metadata String Serialized metadata
         * @param $expected Integer One of the class constants of GIFHandler
         * @dataProvider provideIsMetadataValid
 +       * @covers GIFHandler::isMetadataValid
         */
        public function testIsMetadataValid( $metadata, $expected ) {
                $actual = $this->handler->isMetadataValid( null, $metadata );
@@@ -98,7 -83,6 +98,7 @@@
         * @param $filename String
         * @param $expected String Serialized array
         * @dataProvider provideGetMetadata
 +       * @covers GIFHandler::getMetadata
         */
        public function testGetMetadata( $filename, $expected ) {
                $file = $this->dataFile( $filename, 'image/gif' );
                );
        }
  
+       /**
+        * @param $filename String
+        * @param $expected String Serialized array
+        * @dataProvider provideGetIndependentMetaArray
+        */
+       public function testGetIndependentMetaArray( $filename, $expected ) {
+               $file = $this->dataFile( $filename, 'image/gif' );
+               $actual = $this->handler->getCommonMetaArray( $file );
+               $this->assertEquals( $expected, $actual );
+       }
+       public function provideGetIndependentMetaArray() {
+               return array(
+                       array( 'nonanimated.gif', array(
+                               'GIFFileComment' => array(
+                                       'GIF test file ⁕ Created with GIMP',
+                               ),
+                       ) ),
+                       array( 'animated-xmp.gif',
+                               array(
+                                       'Artist' => 'Bawolff',
+                                       'ImageDescription' => array(
+                                               'x-default' => 'A file to test GIF',
+                                               '_type' => 'lang',
+                                       ),
+                                       'SublocationDest' => 'The interwebs',
+                                       'GIFFileComment' =>
+                                       array(
+                                               'GIƒ·test·file',
+                                       ),
+                               )
+                       ),
+               );
+       }
        private function dataFile( $name, $type ) {
                return new UnregisteredLocalFile( false, $this->repo,
                        "mwstore://localtesting/data/$name", $type );
@@@ -1,11 -1,6 +1,11 @@@
  <?php
 +/**
 + * @covers JpegHandler
 + */
  class JpegTest extends MediaWikiTestCase {
  
 +      protected $filePath;
 +
        protected function setUp() {
                parent::setUp();
                if ( !extension_loaded( 'exif' ) ) {
  
  
                $this->setMwGlobals( 'wgShowEXIF', true );
+               $this->backend = new FSFileBackend( array(
+                       'name' => 'localtesting',
+                       'lockManager' => 'nullLockManager',
+                       'containerPaths' => array( 'data' => $this->filePath )
+               ) );
+               $this->repo = new FSRepo( array(
+                       'name' => 'temp',
+                       'url' => 'http://localhost/thumbtest',
+                       'backend' => $this->backend
+               ) );
+               $this->handler = new JpegHandler;
        }
  
        public function testInvalidFile() {
-               $jpeg = new JpegHandler;
-               $res = $jpeg->getMetadata( null, $this->filePath . 'README' );
+               $file = $this->dataFile( 'README', 'image/jpeg' );
+               $res = $this->handler->getMetadata( $file, $this->filePath . 'README' );
                $this->assertEquals( ExifBitmapHandler::BROKEN_FILE, $res );
        }
  
        public function testJpegMetadataExtraction() {
-               $h = new JpegHandler;
-               $res = $h->getMetadata( null, $this->filePath . 'test.jpg' );
+               $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
+               $res = $this->handler->getMetadata( $file, $this->filePath . 'test.jpg' );
                $expected = 'a:7:{s:16:"ImageDescription";s:9:"Test file";s:11:"XResolution";s:4:"72/1";s:11:"YResolution";s:4:"72/1";s:14:"ResolutionUnit";i:2;s:16:"YCbCrPositioning";i:1;s:15:"JPEGFileComment";a:1:{i:0;s:17:"Created with GIMP";}s:22:"MEDIAWIKI_EXIF_VERSION";i:2;}';
  
                // Unserialize in case serialization format ever changes.
                $this->assertEquals( unserialize( $expected ), unserialize( $res ) );
        }
+       public function testGetIndependentMetaArray() {
+               $file = $this->dataFile( 'test.jpg', 'image/jpeg' );
+               $res = $this->handler->getCommonMetaArray( $file );
+               $expected = array(
+                       'ImageDescription' => 'Test file',
+                       'XResolution' => '72/1',
+                       'YResolution' => '72/1',
+                       'ResolutionUnit' => 2,
+                       'YCbCrPositioning' => 1,
+                       'JPEGFileComment' => array(
+                               'Created with GIMP',
+                       ),
+               );
+               $this->assertEquals( $res, $expected );
+       }
+       private function dataFile( $name, $type ) {
+               return new UnregisteredLocalFile( false, $this->repo,
+                       "mwstore://localtesting/data/$name", $type );
+       }
  }
@@@ -1,15 -1,6 +1,15 @@@
  <?php
  class PNGHandlerTest extends MediaWikiTestCase {
  
 +      /** @var PNGHandler */
 +      protected $handler;
 +      /** @var FSRepo */
 +      protected $repo;
 +      /** @var FSFileBackend */
 +      protected $backend;
 +      /** @var string */
 +      protected $filePath;
 +
        protected function setUp() {
                parent::setUp();
  
@@@ -27,9 -18,6 +27,9 @@@
                $this->handler = new PNGHandler();
        }
  
 +      /**
 +       * @covers PNGHandler::getMetadata
 +       */
        public function testInvalidFile() {
                $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
                $this->assertEquals( PNGHandler::BROKEN_FILE, $res );
@@@ -39,7 -27,6 +39,7 @@@
         * @param $filename String basename of the file to check
         * @param $expected boolean Expected result.
         * @dataProvider provideIsAnimated
 +       * @covers PNGHandler::isAnimatedImage
         */
        public function testIsAnimanted( $filename, $expected ) {
                $file = $this->dataFile( $filename, 'image/png' );
@@@ -58,7 -45,6 +58,7 @@@
         * @param $filename String
         * @param $expected Integer Total image area
         * @dataProvider provideGetImageArea
 +       * @covers PNGHandler::getImageArea
         */
        public function testGetImageArea( $filename, $expected ) {
                $file = $this->dataFile( $filename, 'image/png' );
@@@ -79,7 -65,6 +79,7 @@@
         * @param $metadata String Serialized metadata
         * @param $expected Integer One of the class constants of PNGHandler
         * @dataProvider provideIsMetadataValid
 +       * @covers PNGHandler::isMetadataValid
         */
        public function testIsMetadataValid( $metadata, $expected ) {
                $actual = $this->handler->isMetadataValid( null, $metadata );
         * @param $filename String
         * @param $expected String Serialized array
         * @dataProvider provideGetMetadata
 +       * @covers PNGHandler::getMetadata
         */
        public function testGetMetadata( $filename, $expected ) {
                $file = $this->dataFile( $filename, 'image/png' );
                );
        }
  
+       /**
+        * @param $filename String
+        * @param $expected Array Expected standard metadata
+        * @dataProvider provideGetIndependentMetaArray
+        */
+       public function testGetIndependentMetaArray( $filename, $expected ) {
+               $file = $this->dataFile( $filename, 'image/png' );
+               $actual = $this->handler->getCommonMetaArray( $file );
+               $this->assertEquals( $expected, $actual );
+       }
+       public function provideGetIndependentMetaArray() {
+               return array(
+                       array( 'rgb-na-png.png', array() ),
+                       array( 'xmp.png',
+                               array(
+                                       'SerialNumber' => '123456789',
+                               )
+                       ),
+               );
+       }
        private function dataFile( $name, $type ) {
                return new UnregisteredLocalFile( false, $this->repo,
                        "mwstore://localtesting/data/$name", $type );
@@@ -1,8 -1,5 +1,8 @@@
  <?php
  
 +/**
 + * @covers SVGMetadataExtractor
 + */
  class SVGMetadataExtractorTest extends MediaWikiTestCase {
  
        protected function setUp() {
        /**
         * @dataProvider provideSvgFiles
         */
 -      function testGetMetadata( $infile, $expected ) {
 +      public function testGetMetadata( $infile, $expected ) {
                $this->assertMetadata( $infile, $expected );
        }
  
        /**
         * @dataProvider provideSvgFilesWithXMLMetadata
         */
 -      function testGetXMLMetadata( $infile, $expected ) {
 +      public function testGetXMLMetadata( $infile, $expected ) {
                $r = new XMLReader();
                if ( !method_exists( $r, 'readInnerXML' ) ) {
                        $this->markTestSkipped( 'XMLReader::readInnerXML() does not exist (libxml >2.6.20 needed).' );
                                        'originalWidth' => '385',
                                        'originalHeight' => '385.0004883',
                                )
+                       ),
+                       array(
+                               "$base/Tux.svg",
+                               array(
+                                       'width' => 512,
+                                       'height' => 594,
+                                       'originalWidth' => '100%',
+                                       'originalHeight' => '100%',
+                                       'title' => 'Tux',
+                                       'description' => 'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg',
+                               )
                        )
                );
        }