3 * Tests related to auto rotation.
7 class ExifRotationTest
extends MediaWikiTestCase
{
9 protected function setUp() {
11 if ( !extension_loaded( 'exif' ) ) {
12 $this->markTestSkipped( "This test needs the exif extension." );
15 $this->handler
= new BitmapHandler();
16 $filePath = __DIR__
. '/../../data/media';
18 $tmpDir = $this->getNewTempDirectory();
20 $this->repo
= new FSRepo( array(
22 'url' => 'http://localhost/thumbtest',
23 'backend' => new FSFileBackend( array(
24 'name' => 'localtesting',
25 'lockManager' => 'nullLockManager',
26 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
30 $this->setMwGlobals( array(
32 'wgEnableAutoRotation' => true,
38 * @dataProvider provideFiles
40 function testMetadata( $name, $type, $info ) {
41 if ( !BitmapHandler
::canRotate() ) {
42 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
44 $file = $this->dataFile( $name, $type );
45 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
46 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
51 * @dataProvider provideFiles
53 function testRotationRendering( $name, $type, $info, $thumbs ) {
54 if ( !BitmapHandler
::canRotate() ) {
55 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
57 foreach ( $thumbs as $size => $out ) {
58 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
60 'width' => $matches[1],
62 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
64 'width' => $matches[1],
65 'height' => $matches[2]
68 throw new MWException( 'bogus test data format ' . $size );
71 $file = $this->dataFile( $name, $type );
72 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
74 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
75 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
77 $gis = getimagesize( $thumb->getLocalCopyPath() );
78 if ( $out[0] > $info['width'] ) {
79 // Physical image won't be scaled bigger than the original.
80 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
81 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
83 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
84 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
89 /* Utility function */
90 private function dataFile( $name, $type ) {
91 return new UnregisteredLocalFile( false, $this->repo
,
92 "mwstore://localtesting/data/$name", $type );
95 public static function provideFiles() {
98 'landscape-plain.jpg',
105 '800x600px' => array( 800, 600 ),
106 '9999x800px' => array( 1067, 800 ),
107 '800px' => array( 800, 600 ),
108 '600px' => array( 600, 450 ),
112 'portrait-rotated.jpg',
115 'width' => 768, // as rotated
116 'height' => 1024, // as rotated
119 '800x600px' => array( 450, 600 ),
120 '9999x800px' => array( 600, 800 ),
121 '800px' => array( 800, 1067 ),
122 '600px' => array( 600, 800 ),
129 * Same as before, but with auto-rotation disabled.
130 * @dataProvider provideFilesNoAutoRotate
132 function testMetadataNoAutoRotate( $name, $type, $info ) {
133 $this->setMwGlobals( 'wgEnableAutoRotation', false );
135 $file = $this->dataFile( $name, $type );
136 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
137 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
142 * @dataProvider provideFilesNoAutoRotate
144 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
145 $this->setMwGlobals( 'wgEnableAutoRotation', false );
147 foreach ( $thumbs as $size => $out ) {
148 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
150 'width' => $matches[1],
152 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
154 'width' => $matches[1],
155 'height' => $matches[2]
158 throw new MWException( 'bogus test data format ' . $size );
161 $file = $this->dataFile( $name, $type );
162 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
164 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
165 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
167 $gis = getimagesize( $thumb->getLocalCopyPath() );
168 if ( $out[0] > $info['width'] ) {
169 // Physical image won't be scaled bigger than the original.
170 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
171 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
173 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
174 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
179 public static function provideFilesNoAutoRotate() {
182 'landscape-plain.jpg',
189 '800x600px' => array( 800, 600 ),
190 '9999x800px' => array( 1067, 800 ),
191 '800px' => array( 800, 600 ),
192 '600px' => array( 600, 450 ),
196 'portrait-rotated.jpg',
199 'width' => 1024, // since not rotated
200 'height' => 768, // since not rotated
203 '800x600px' => array( 800, 600 ),
204 '9999x800px' => array( 1067, 800 ),
205 '800px' => array( 800, 600 ),
206 '600px' => array( 600, 450 ),
213 const TEST_WIDTH
= 100;
214 const TEST_HEIGHT
= 200;
217 * @dataProvider provideBitmapExtractPreRotationDimensions
219 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
220 $result = $this->handler
->extractPreRotationDimensions( array(
221 'physicalWidth' => self
::TEST_WIDTH
,
222 'physicalHeight' => self
::TEST_HEIGHT
,
224 $this->assertEquals( $expected, $result );
227 public static function provideBitmapExtractPreRotationDimensions() {
231 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
235 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
239 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
243 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)