4 * Tests related to auto rotation
6 class ExifRotationTest
extends MediaWikiTestCase
{
10 $this->filePath
= dirname( __FILE__
) . '/../../data/media/';
11 $this->handler
= new BitmapHandler();
12 $this->repo
= new FSRepo(array(
14 'directory' => wfTempDir() . '/exif-test-' . time(),
15 'url' => 'http://localhost/thumbtest'
17 if ( !wfDl( 'exif' ) ) {
18 $this->markTestSkipped( "This test needs the exif extension." );
21 $this->show
= $wgShowEXIF;
24 global $wgEnableAutoRotation;
25 $this->oldAuto
= $wgEnableAutoRotation;
26 $wgEnableAutoRotation = true;
28 public function tearDown() {
29 global $wgShowEXIF, $wgEnableAutoRotation;
30 $wgShowEXIF = $this->show
;
31 $wgEnableAutoRotation = $this->oldAuto
;
36 * @dataProvider providerFiles
38 function testMetadata( $name, $type, $info ) {
39 if ( !BitmapHandler
::canRotate() ) {
40 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
42 $file = UnregisteredLocalFile
::newFromPath( $this->filePath
. $name, $type );
43 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
44 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
49 * @dataProvider providerFiles
51 function testRotationRendering( $name, $type, $info, $thumbs ) {
52 if ( !BitmapHandler
::canRotate() ) {
53 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
55 foreach( $thumbs as $size => $out ) {
56 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
58 'width' => $matches[1],
60 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
62 'width' => $matches[1],
63 'height' => $matches[2]
66 throw new MWException('bogus test data format ' . $size);
69 $file = $this->localFile( $name, $type );
70 $thumb = $file->transform( $params, File
::RENDER_NOW
);
72 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
73 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
75 $gis = getimagesize( $thumb->getPath() );
76 if ($out[0] > $info['width']) {
77 // Physical image won't be scaled bigger than the original.
78 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
79 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
81 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
82 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
87 private function localFile( $name, $type ) {
88 return new UnregisteredLocalFile( false, $this->repo
, $this->filePath
. $name, $type );
91 function providerFiles() {
94 'landscape-plain.jpg',
101 '800x600px' => array( 800, 600 ),
102 '9999x800px' => array( 1067, 800 ),
103 '800px' => array( 800, 600 ),
104 '600px' => array( 600, 450 ),
108 'portrait-rotated.jpg',
111 'width' => 768, // as rotated
112 'height' => 1024, // as rotated
115 '800x600px' => array( 450, 600 ),
116 '9999x800px' => array( 600, 800 ),
117 '800px' => array( 800, 1067 ),
118 '600px' => array( 600, 800 ),
125 * Same as before, but with auto-rotation disabled.
126 * @dataProvider providerFilesNoAutoRotate
128 function testMetadataNoAutoRotate( $name, $type, $info ) {
129 global $wgEnableAutoRotation;
130 $wgEnableAutoRotation = false;
132 $file = UnregisteredLocalFile
::newFromPath( $this->filePath
. $name, $type );
133 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
134 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
136 $wgEnableAutoRotation = true;
141 * @dataProvider providerFilesNoAutoRotate
143 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
144 global $wgEnableAutoRotation;
145 $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->localFile( $name, $type );
162 $thumb = $file->transform( $params, File
::RENDER_NOW
);
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->getPath() );
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");
177 $wgEnableAutoRotation = true;
180 function providerFilesNoAutoRotate() {
183 'landscape-plain.jpg',
190 '800x600px' => array( 800, 600 ),
191 '9999x800px' => array( 1067, 800 ),
192 '800px' => array( 800, 600 ),
193 '600px' => array( 600, 450 ),
197 'portrait-rotated.jpg',
200 'width' => 1024, // since not rotated
201 'height' => 768, // since not rotated
204 '800x600px' => array( 800, 600 ),
205 '9999x800px' => array( 1067, 800 ),
206 '800px' => array( 800, 600 ),
207 '600px' => array( 600, 450 ),
214 const TEST_WIDTH
= 100;
215 const TEST_HEIGHT
= 200;
218 * @dataProvider provideBitmapExtractPreRotationDimensions
220 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
221 $result = $this->handler
->extractPreRotationDimensions( array(
222 'physicalWidth' => self
::TEST_WIDTH
,
223 'physicalHeight' => self
::TEST_HEIGHT
,
225 $this->assertEquals( $expected, $result );
228 function provideBitmapExtractPreRotationDimensions() {
232 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
236 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
240 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
244 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)