Remove old maintenance script importTextFile.php
[mediawiki.git] / tests / phpunit / includes / media / ExifRotationTest.php
blob247e352c6549ea6fc024359b1cdc8a5f702a48f5
1 <?php
2 /**
3 * Tests related to auto rotation.
5 * @group Media
6 * @group medium
8 * @todo covers tags
9 */
10 class ExifRotationTest extends MediaWikiMediaTestCase {
12 protected function setUp() {
13 parent::setUp();
14 $this->checkPHPExtension( 'exif' );
16 $this->handler = new BitmapHandler();
18 $this->setMwGlobals( array(
19 'wgShowEXIF' => true,
20 'wgEnableAutoRotation' => true,
21 ) );
24 /**
25 * Mark this test as creating thumbnail files.
27 protected function createsThumbnails() {
28 return true;
31 /**
32 * @dataProvider provideFiles
34 public function testMetadata( $name, $type, $info ) {
35 if ( !BitmapHandler::canRotate() ) {
36 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
38 $file = $this->dataFile( $name, $type );
39 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
40 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
43 /**
45 * @dataProvider provideFiles
47 public function testRotationRendering( $name, $type, $info, $thumbs ) {
48 if ( !BitmapHandler::canRotate() ) {
49 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
51 foreach ( $thumbs as $size => $out ) {
52 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
53 $params = array(
54 'width' => $matches[1],
56 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
57 $params = array(
58 'width' => $matches[1],
59 'height' => $matches[2]
61 } else {
62 throw new MWException( 'bogus test data format ' . $size );
65 $file = $this->dataFile( $name, $type );
66 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
68 $this->assertEquals(
69 $out[0],
70 $thumb->getWidth(),
71 "$name: thumb reported width check for $size"
73 $this->assertEquals(
74 $out[1],
75 $thumb->getHeight(),
76 "$name: thumb reported height check for $size"
79 $gis = getimagesize( $thumb->getLocalCopyPath() );
80 if ( $out[0] > $info['width'] ) {
81 // Physical image won't be scaled bigger than the original.
82 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
83 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
84 } else {
85 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
86 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
91 public static function provideFiles() {
92 return array(
93 array(
94 'landscape-plain.jpg',
95 'image/jpeg',
96 array(
97 'width' => 1024,
98 'height' => 768,
100 array(
101 '800x600px' => array( 800, 600 ),
102 '9999x800px' => array( 1067, 800 ),
103 '800px' => array( 800, 600 ),
104 '600px' => array( 600, 450 ),
107 array(
108 'portrait-rotated.jpg',
109 'image/jpeg',
110 array(
111 'width' => 768, // as rotated
112 'height' => 1024, // as rotated
114 array(
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 provideFilesNoAutoRotate
128 public function testMetadataNoAutoRotate( $name, $type, $info ) {
129 $this->setMwGlobals( 'wgEnableAutoRotation', false );
131 $file = $this->dataFile( $name, $type );
132 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
133 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
138 * @dataProvider provideFilesNoAutoRotate
140 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
141 $this->setMwGlobals( 'wgEnableAutoRotation', false );
143 foreach ( $thumbs as $size => $out ) {
144 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
145 $params = array(
146 'width' => $matches[1],
148 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
149 $params = array(
150 'width' => $matches[1],
151 'height' => $matches[2]
153 } else {
154 throw new MWException( 'bogus test data format ' . $size );
157 $file = $this->dataFile( $name, $type );
158 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
160 $this->assertEquals(
161 $out[0],
162 $thumb->getWidth(),
163 "$name: thumb reported width check for $size"
165 $this->assertEquals(
166 $out[1],
167 $thumb->getHeight(),
168 "$name: thumb reported height check for $size"
171 $gis = getimagesize( $thumb->getLocalCopyPath() );
172 if ( $out[0] > $info['width'] ) {
173 // Physical image won't be scaled bigger than the original.
174 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
175 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
176 } else {
177 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
178 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
183 public static function provideFilesNoAutoRotate() {
184 return array(
185 array(
186 'landscape-plain.jpg',
187 'image/jpeg',
188 array(
189 'width' => 1024,
190 'height' => 768,
192 array(
193 '800x600px' => array( 800, 600 ),
194 '9999x800px' => array( 1067, 800 ),
195 '800px' => array( 800, 600 ),
196 '600px' => array( 600, 450 ),
199 array(
200 'portrait-rotated.jpg',
201 'image/jpeg',
202 array(
203 'width' => 1024, // since not rotated
204 'height' => 768, // since not rotated
206 array(
207 '800x600px' => array( 800, 600 ),
208 '9999x800px' => array( 1067, 800 ),
209 '800px' => array( 800, 600 ),
210 '600px' => array( 600, 450 ),
216 const TEST_WIDTH = 100;
217 const TEST_HEIGHT = 200;
220 * @dataProvider provideBitmapExtractPreRotationDimensions
222 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
223 $result = $this->handler->extractPreRotationDimensions( array(
224 'physicalWidth' => self::TEST_WIDTH,
225 'physicalHeight' => self::TEST_HEIGHT,
226 ), $rotation );
227 $this->assertEquals( $expected, $result );
230 public static function provideBitmapExtractPreRotationDimensions() {
231 return array(
232 array(
234 array( self::TEST_WIDTH, self::TEST_HEIGHT )
236 array(
238 array( self::TEST_HEIGHT, self::TEST_WIDTH )
240 array(
241 180,
242 array( self::TEST_WIDTH, self::TEST_HEIGHT )
244 array(
245 270,
246 array( self::TEST_HEIGHT, self::TEST_WIDTH )