Merge "Use $wgUser in ResourceLoaderUserGroupsModule when possible."
[lhc/web/wiklou.git] / tests / phpunit / includes / media / ExifRotationTest.php
index 87aed29..1e2d1bb 100644 (file)
@@ -7,23 +7,38 @@ class ExifRotationTest extends MediaWikiTestCase {
 
        function setUp() {
                parent::setUp();
-               $this->filePath = dirname( __FILE__ ) . '/../../data/media/';
                $this->handler = new BitmapHandler();
-               $this->repo = new FSRepo(array(
-                       'name' => 'temp',
-                       'directory' => wfTempDir() . '/exif-test-' . time(),
-                       'url' => 'http://localhost/thumbtest'
-               ));
+               $filePath = dirname( __FILE__ ) . '/../../data/media';
+
+               $tmpDir = $this->getNewTempDirectory();
+
+               $this->repo = new FSRepo( array(
+                       '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;
+               global $wgShowEXIF, $wgEnableAutoRotation;
                $wgShowEXIF = $this->show;
+               $wgEnableAutoRotation = $this->oldAuto;
+
+               parent::tearDown();
        }
 
        /**
@@ -31,7 +46,10 @@ class ExifRotationTest extends MediaWikiTestCase {
         * @dataProvider providerFiles
         */
        function testMetadata( $name, $type, $info ) {
-               $file = UnregisteredLocalFile::newFromPath( $this->filePath . $name, $type );
+               if ( !BitmapHandler::canRotate() ) {
+                       $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
+               }
+               $file = $this->dataFile( $name, $type );
                $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
                $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
        }
@@ -41,6 +59,9 @@ class ExifRotationTest extends MediaWikiTestCase {
         * @dataProvider providerFiles
         */
        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 ) ) {
                                $params = array(
@@ -55,13 +76,13 @@ class ExifRotationTest extends MediaWikiTestCase {
                                throw new MWException('bogus test data format ' . $size);
                        }
 
-                       $file = $this->localFile( $name, $type );
-                       $thumb = $file->transform( $params, File::RENDER_NOW );
+                       $file = $this->dataFile( $name, $type );
+                       $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
 
                        $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
                        $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
 
-                       $gis = getimagesize( $thumb->getPath() );
+                       $gis = getimagesize( $thumb->getLocalCopyPath() );
                        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");
@@ -73,8 +94,9 @@ class ExifRotationTest extends MediaWikiTestCase {
                }
        }
 
-       private function localFile( $name, $type ) {
-               return new UnregisteredLocalFile( false, $this->repo, $this->filePath . $name, $type );
+       private function dataFile( $name, $type ) {
+               return new UnregisteredLocalFile( false, $this->repo,
+                       "mwstore://localtesting/data/$name", $type );
        }
 
        function providerFiles() {
@@ -109,6 +131,95 @@ class ExifRotationTest extends MediaWikiTestCase {
                        )
                );
        }
+
+       /**
+        * Same as before, but with auto-rotation disabled.
+        * @dataProvider providerFilesNoAutoRotate
+        */
+       function testMetadataNoAutoRotate( $name, $type, $info ) {
+               global $wgEnableAutoRotation;
+               $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
+        */
+       function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
+               global $wgEnableAutoRotation;
+               $wgEnableAutoRotation = false;
+
+               foreach( $thumbs as $size => $out ) {
+                       if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
+                               $params = array(
+                                       'width' => $matches[1],
+                               );
+                       } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
+                               $params = array(
+                                       'width' => $matches[1],
+                                       'height' => $matches[2]
+                               );
+                       } else {
+                               throw new MWException('bogus test data format ' . $size);
+                       }
+
+                       $file = $this->dataFile( $name, $type );
+                       $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
+
+                       $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
+                       $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
+
+                       $gis = getimagesize( $thumb->getLocalCopyPath() );
+                       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");
+                       } 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");
+                       }
+               }
+               $wgEnableAutoRotation = true;
+       }
+
+       function providerFilesNoAutoRotate() {
+               return array(
+                       array(
+                               'landscape-plain.jpg',
+                               'image/jpeg',
+                               array(
+                                       'width' => 1024,
+                                       'height' => 768,
+                               ),
+                               array(
+                                       '800x600px' => array( 800, 600 ),
+                                       '9999x800px' => array( 1067, 800 ),
+                                       '800px' => array( 800, 600 ),
+                                       '600px' => array( 600, 450 ),
+                               )
+                       ),
+                       array(
+                               'portrait-rotated.jpg',
+                               'image/jpeg',
+                               array(
+                                       'width' => 1024, // since not rotated
+                                       'height' => 768, // since not rotated
+                               ),
+                               array(
+                                       '800x600px' => array( 800, 600 ),
+                                       '9999x800px' => array( 1067, 800 ),
+                                       '800px' => array( 800, 600 ),
+                                       '600px' => array( 600, 450 ),
+                               )
+                       )
+               );
+       }
        
        
        const TEST_WIDTH = 100;
@@ -142,7 +253,7 @@ class ExifRotationTest extends MediaWikiTestCase {
                        array(
                                270,
                                array( self::TEST_HEIGHT, self::TEST_WIDTH ) 
-                       ),                      
+                       ),
                );
        }
 }