692a5f9c56208754bb1fb1b9eb16224ddde7a11e
4 * Tests related to auto rotation
6 class ExifRotationTest
extends MediaWikiTestCase
{
8 protected function setUp() {
10 $this->handler
= new BitmapHandler();
11 $filePath = __DIR__
. '/../../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 protected function tearDown() {
37 global $wgShowEXIF, $wgEnableAutoRotation;
38 $wgShowEXIF = $this->show
;
39 $wgEnableAutoRotation = $this->oldAuto
;
46 * @dataProvider provideFiles
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 provideFiles
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 /* Utility function */
98 private function dataFile( $name, $type ) {
99 return new UnregisteredLocalFile( false, $this->repo
,
100 "mwstore://localtesting/data/$name", $type );
103 public static function provideFiles() {
106 'landscape-plain.jpg',
113 '800x600px' => array( 800, 600 ),
114 '9999x800px' => array( 1067, 800 ),
115 '800px' => array( 800, 600 ),
116 '600px' => array( 600, 450 ),
120 'portrait-rotated.jpg',
123 'width' => 768, // as rotated
124 'height' => 1024, // as rotated
127 '800x600px' => array( 450, 600 ),
128 '9999x800px' => array( 600, 800 ),
129 '800px' => array( 800, 1067 ),
130 '600px' => array( 600, 800 ),
137 * Same as before, but with auto-rotation disabled.
138 * @dataProvider provideFilesNoAutoRotate
140 function testMetadataNoAutoRotate( $name, $type, $info ) {
141 global $wgEnableAutoRotation;
142 $wgEnableAutoRotation = false;
144 $file = $this->dataFile( $name, $type );
145 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
146 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
148 $wgEnableAutoRotation = true;
153 * @dataProvider provideFilesNoAutoRotate
155 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
156 global $wgEnableAutoRotation;
157 $wgEnableAutoRotation = false;
159 foreach( $thumbs as $size => $out ) {
160 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
162 'width' => $matches[1],
164 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
166 'width' => $matches[1],
167 'height' => $matches[2]
170 throw new MWException('bogus test data format ' . $size);
173 $file = $this->dataFile( $name, $type );
174 $thumb = $file->transform( $params, File
::RENDER_NOW | File
::RENDER_FORCE
);
176 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
177 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
179 $gis = getimagesize( $thumb->getLocalCopyPath() );
180 if ($out[0] > $info['width']) {
181 // Physical image won't be scaled bigger than the original.
182 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
183 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
185 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
186 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
189 $wgEnableAutoRotation = true;
192 public static function provideFilesNoAutoRotate() {
195 'landscape-plain.jpg',
202 '800x600px' => array( 800, 600 ),
203 '9999x800px' => array( 1067, 800 ),
204 '800px' => array( 800, 600 ),
205 '600px' => array( 600, 450 ),
209 'portrait-rotated.jpg',
212 'width' => 1024, // since not rotated
213 'height' => 768, // since not rotated
216 '800x600px' => array( 800, 600 ),
217 '9999x800px' => array( 1067, 800 ),
218 '800px' => array( 800, 600 ),
219 '600px' => array( 600, 450 ),
226 const TEST_WIDTH
= 100;
227 const TEST_HEIGHT
= 200;
230 * @dataProvider provideBitmapExtractPreRotationDimensions
232 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
233 $result = $this->handler
->extractPreRotationDimensions( array(
234 'physicalWidth' => self
::TEST_WIDTH
,
235 'physicalHeight' => self
::TEST_HEIGHT
,
237 $this->assertEquals( $expected, $result );
240 function provideBitmapExtractPreRotationDimensions() {
244 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
248 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)
252 array( self
::TEST_WIDTH
, self
::TEST_HEIGHT
)
256 array( self
::TEST_HEIGHT
, self
::TEST_WIDTH
)