4 * Tests related to auto rotation
6 class ExifRotationTest
extends MediaWikiTestCase
{
10 $this->handler
= new BitmapHandler();
11 $filePath = dirname( __FILE__
) . '/../../data/media';
13 $tmpDir = $this->getNewTempDirectory();
15 $this->repo
= new FSRepo( array(
17 'url' => 'http://localhost/thumbtest',
18 'backend' => new FSFileBackend( array(
19 'name' => 'localtesting',
20 'lockManager' => 'nullLockManager',
21 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
24 if ( !wfDl( 'exif' ) ) {
25 $this->markTestSkipped( "This test needs the exif extension." );
28 $this->show
= $wgShowEXIF;
31 global $wgEnableAutoRotation;
32 $this->oldAuto
= $wgEnableAutoRotation;
33 $wgEnableAutoRotation = true;
36 public function tearDown() {
37 global $wgShowEXIF, $wgEnableAutoRotation;
38 $wgShowEXIF = $this->show
;
39 $wgEnableAutoRotation = $this->oldAuto
;
46 * @dataProvider providerFiles
48 function testMetadata( $name, $type, $info ) {
49 if ( !BitmapHandler
::canRotate() ) {
50 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
52 $file = $this->dataFile( $name, $type );
53 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
54 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
59 * @dataProvider providerFiles
61 function testRotationRendering( $name, $type, $info, $thumbs ) {
62 if ( !BitmapHandler
::canRotate() ) {
63 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
65 foreach( $thumbs as $size => $out ) {
66 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
68 'width' => $matches[1],
70 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
72 'width' => $matches[1],
73 'height' => $matches[2]
76 throw new MWException('bogus test data format ' . $size);
79 $file = $this->dataFile( $name, $type );
80 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
82 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
83 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
85 $gis = getimagesize( $thumb->getLocalCopyPath() );
86 if ($out[0] > $info['width']) {
87 // Physical image won't be scaled bigger than the original.
88 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
89 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
91 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
92 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
97 private function dataFile( $name, $type ) {
98 return new UnregisteredLocalFile( false, $this->repo
,
99 "mwstore://localtesting/data/$name", $type );
102 function providerFiles() {
105 'landscape-plain.jpg',
112 '800x600px' => array( 800, 600 ),
113 '9999x800px' => array( 1067, 800 ),
114 '800px' => array( 800, 600 ),
115 '600px' => array( 600, 450 ),
119 'portrait-rotated.jpg',
122 'width' => 768, // as rotated
123 'height' => 1024, // as rotated
126 '800x600px' => array( 450, 600 ),
127 '9999x800px' => array( 600, 800 ),
128 '800px' => array( 800, 1067 ),
129 '600px' => array( 600, 800 ),
136 * Same as before, but with auto-rotation disabled.
137 * @dataProvider providerFilesNoAutoRotate
139 function testMetadataNoAutoRotate( $name, $type, $info ) {
140 global $wgEnableAutoRotation;
141 $wgEnableAutoRotation = false;
143 $file = $this->dataFile( $name, $type );
144 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
145 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
147 $wgEnableAutoRotation = true;
152 * @dataProvider providerFilesNoAutoRotate
154 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
155 global $wgEnableAutoRotation;
156 $wgEnableAutoRotation = false;
158 foreach( $thumbs as $size => $out ) {
159 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
161 'width' => $matches[1],
163 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
165 'width' => $matches[1],
166 'height' => $matches[2]
169 throw new MWException('bogus test data format ' . $size);
172 $file = $this->dataFile( $name, $type );
173 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
175 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
176 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
178 $gis = getimagesize( $thumb->getLocalCopyPath() );
179 if ($out[0] > $info['width']) {
180 // Physical image won't be scaled bigger than the original.
181 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
182 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
184 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
185 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
188 $wgEnableAutoRotation = true;
191 function providerFilesNoAutoRotate() {
194 'landscape-plain.jpg',
201 '800x600px' => array( 800, 600 ),
202 '9999x800px' => array( 1067, 800 ),
203 '800px' => array( 800, 600 ),
204 '600px' => array( 600, 450 ),
208 'portrait-rotated.jpg',
211 'width' => 1024, // since not rotated
212 'height' => 768, // since not rotated
215 '800x600px' => array( 800, 600 ),
216 '9999x800px' => array( 1067, 800 ),
217 '800px' => array( 800, 600 ),
218 '600px' => array( 600, 450 ),
225 const TEST_WIDTH
= 100;
226 const TEST_HEIGHT
= 200;
229 * @dataProvider provideBitmapExtractPreRotationDimensions
231 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
232 $result = $this->handler
->extractPreRotationDimensions( array(
233 'physicalWidth' => self
::TEST_WIDTH
,
234 'physicalHeight' => self
::TEST_HEIGHT
,
236 $this->assertEquals( $expected, $result );
239 function provideBitmapExtractPreRotationDimensions() {
243 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
247 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
251 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
255 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)