4 * Tests related to auto rotation
6 class ExifRotationTest
extends MediaWikiTestCase
{
10 $this->handler
= new BitmapHandler();
11 $filePath = dirname( __FILE__
) . '/../../data/media';
12 $tmpDir = wfTempDir() . '/exif-test-' . time() . '-' . mt_rand();
13 $this->backend
= new FSFileBackend( array(
14 'name' => 'localtesting',
15 'lockManager' => 'nullLockManager',
16 'containerPaths' => array( 'images-thumb' => $tmpDir, 'data' => $filePath )
18 $this->repo
= new FSRepo( array(
20 'url' => 'http://localhost/thumbtest',
21 'backend' => $this->backend
23 if ( !wfDl( 'exif' ) ) {
24 $this->markTestSkipped( "This test needs the exif extension." );
27 $this->show
= $wgShowEXIF;
30 global $wgEnableAutoRotation;
31 $this->oldAuto
= $wgEnableAutoRotation;
32 $wgEnableAutoRotation = true;
34 public function tearDown() {
35 global $wgShowEXIF, $wgEnableAutoRotation;
36 $wgShowEXIF = $this->show
;
37 $wgEnableAutoRotation = $this->oldAuto
;
42 * @dataProvider providerFiles
44 function testMetadata( $name, $type, $info ) {
45 if ( !BitmapHandler
::canRotate() ) {
46 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
48 $file = $this->dataFile( $name, $type );
49 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
50 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
55 * @dataProvider providerFiles
57 function testRotationRendering( $name, $type, $info, $thumbs ) {
58 if ( !BitmapHandler
::canRotate() ) {
59 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
61 foreach( $thumbs as $size => $out ) {
62 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
64 'width' => $matches[1],
66 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
68 'width' => $matches[1],
69 'height' => $matches[2]
72 throw new MWException('bogus test data format ' . $size);
75 $file = $this->dataFile( $name, $type );
76 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
78 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
79 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
81 $gis = getimagesize( $thumb->getLocalCopyPath() );
82 if ($out[0] > $info['width']) {
83 // Physical image won't be scaled bigger than the original.
84 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
85 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
87 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
88 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
93 private function dataFile( $name, $type ) {
94 return new UnregisteredLocalFile( false, $this->repo
,
95 "mwstore://localtesting/data/$name", $type );
98 function providerFiles() {
101 'landscape-plain.jpg',
108 '800x600px' => array( 800, 600 ),
109 '9999x800px' => array( 1067, 800 ),
110 '800px' => array( 800, 600 ),
111 '600px' => array( 600, 450 ),
115 'portrait-rotated.jpg',
118 'width' => 768, // as rotated
119 'height' => 1024, // as rotated
122 '800x600px' => array( 450, 600 ),
123 '9999x800px' => array( 600, 800 ),
124 '800px' => array( 800, 1067 ),
125 '600px' => array( 600, 800 ),
132 * Same as before, but with auto-rotation disabled.
133 * @dataProvider providerFilesNoAutoRotate
135 function testMetadataNoAutoRotate( $name, $type, $info ) {
136 global $wgEnableAutoRotation;
137 $wgEnableAutoRotation = false;
139 $file = $this->dataFile( $name, $type );
140 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
141 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
143 $wgEnableAutoRotation = true;
148 * @dataProvider providerFilesNoAutoRotate
150 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
151 global $wgEnableAutoRotation;
152 $wgEnableAutoRotation = false;
154 foreach( $thumbs as $size => $out ) {
155 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
157 'width' => $matches[1],
159 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
161 'width' => $matches[1],
162 'height' => $matches[2]
165 throw new MWException('bogus test data format ' . $size);
168 $file = $this->dataFile( $name, $type );
169 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
171 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
172 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
174 $gis = getimagesize( $thumb->getLocalCopyPath() );
175 if ($out[0] > $info['width']) {
176 // Physical image won't be scaled bigger than the original.
177 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
178 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
180 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
181 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
184 $wgEnableAutoRotation = true;
187 function providerFilesNoAutoRotate() {
190 'landscape-plain.jpg',
197 '800x600px' => array( 800, 600 ),
198 '9999x800px' => array( 1067, 800 ),
199 '800px' => array( 800, 600 ),
200 '600px' => array( 600, 450 ),
204 'portrait-rotated.jpg',
207 'width' => 1024, // since not rotated
208 'height' => 768, // since not rotated
211 '800x600px' => array( 800, 600 ),
212 '9999x800px' => array( 1067, 800 ),
213 '800px' => array( 800, 600 ),
214 '600px' => array( 600, 450 ),
221 const TEST_WIDTH
= 100;
222 const TEST_HEIGHT
= 200;
225 * @dataProvider provideBitmapExtractPreRotationDimensions
227 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
228 $result = $this->handler
->extractPreRotationDimensions( array(
229 'physicalWidth' => self
::TEST_WIDTH
,
230 'physicalHeight' => self
::TEST_HEIGHT
,
232 $this->assertEquals( $expected, $result );
235 function provideBitmapExtractPreRotationDimensions() {
239 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
243 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
247 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
251 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)