(follow-up r99910) Make $wgEnableAutoRotation work...
[lhc/web/wiklou.git] / tests / phpunit / includes / media / ExifRotationTest.php
1 <?php
2
3 /**
4 * Tests related to auto rotation
5 */
6 class ExifRotationTest extends MediaWikiTestCase {
7
8 function setUp() {
9 parent::setUp();
10 $this->filePath = dirname( __FILE__ ) . '/../../data/media/';
11 $this->handler = new BitmapHandler();
12 $this->repo = new FSRepo(array(
13 'name' => 'temp',
14 'directory' => wfTempDir() . '/exif-test-' . time(),
15 'url' => 'http://localhost/thumbtest'
16 ));
17 if ( !wfDl( 'exif' ) ) {
18 $this->markTestSkipped( "This test needs the exif extension." );
19 }
20 global $wgShowEXIF;
21 $this->show = $wgShowEXIF;
22 $wgShowEXIF = true;
23
24 global $wgEnableAutoRotation;
25 $this->oldAuto = $wgEnableAutoRotation;
26 $wgEnableAutoRotation = true;
27 }
28 public function tearDown() {
29 global $wgShowEXIF, $wgEnableAutoRotation;
30 $wgShowEXIF = $this->show;
31 $wgEnableAutoRotation = $this->oldAuto;
32 }
33
34 /**
35 *
36 * @dataProvider providerFiles
37 */
38 function testMetadata( $name, $type, $info ) {
39 if ( !BitmapHandler::canRotate() ) {
40 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
41 }
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" );
45 }
46
47 /**
48 *
49 * @dataProvider providerFiles
50 */
51 function testRotationRendering( $name, $type, $info, $thumbs ) {
52 if ( !BitmapHandler::canRotate() ) {
53 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
54 }
55 foreach( $thumbs as $size => $out ) {
56 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
57 $params = array(
58 'width' => $matches[1],
59 );
60 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
61 $params = array(
62 'width' => $matches[1],
63 'height' => $matches[2]
64 );
65 } else {
66 throw new MWException('bogus test data format ' . $size);
67 }
68
69 $file = $this->localFile( $name, $type );
70 $thumb = $file->transform( $params, File::RENDER_NOW );
71
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" );
74
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");
80 } else {
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");
83 }
84 }
85 }
86
87 private function localFile( $name, $type ) {
88 return new UnregisteredLocalFile( false, $this->repo, $this->filePath . $name, $type );
89 }
90
91 function providerFiles() {
92 return array(
93 array(
94 'landscape-plain.jpg',
95 'image/jpeg',
96 array(
97 'width' => 1024,
98 'height' => 768,
99 ),
100 array(
101 '800x600px' => array( 800, 600 ),
102 '9999x800px' => array( 1067, 800 ),
103 '800px' => array( 800, 600 ),
104 '600px' => array( 600, 450 ),
105 )
106 ),
107 array(
108 'portrait-rotated.jpg',
109 'image/jpeg',
110 array(
111 'width' => 768, // as rotated
112 'height' => 1024, // as rotated
113 ),
114 array(
115 '800x600px' => array( 450, 600 ),
116 '9999x800px' => array( 600, 800 ),
117 '800px' => array( 800, 1067 ),
118 '600px' => array( 600, 800 ),
119 )
120 )
121 );
122 }
123
124 /**
125 * Same as before, but with auto-rotation disabled.
126 * @dataProvider providerFilesNoAutoRotate
127 */
128 function testMetadataNoAutoRotate( $name, $type, $info ) {
129 global $wgEnableAutoRotation;
130 $wgEnableAutoRotation = false;
131
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" );
135
136 $wgEnableAutoRotation = true;
137 }
138
139 /**
140 *
141 * @dataProvider providerFilesNoAutoRotate
142 */
143 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
144 global $wgEnableAutoRotation;
145 $wgEnableAutoRotation = false;
146
147 foreach( $thumbs as $size => $out ) {
148 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
149 $params = array(
150 'width' => $matches[1],
151 );
152 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
153 $params = array(
154 'width' => $matches[1],
155 'height' => $matches[2]
156 );
157 } else {
158 throw new MWException('bogus test data format ' . $size);
159 }
160
161 $file = $this->localFile( $name, $type );
162 $thumb = $file->transform( $params, File::RENDER_NOW );
163
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" );
166
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");
172 } else {
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");
175 }
176 }
177 $wgEnableAutoRotation = true;
178 }
179
180 function providerFilesNoAutoRotate() {
181 return array(
182 array(
183 'landscape-plain.jpg',
184 'image/jpeg',
185 array(
186 'width' => 1024,
187 'height' => 768,
188 ),
189 array(
190 '800x600px' => array( 800, 600 ),
191 '9999x800px' => array( 1067, 800 ),
192 '800px' => array( 800, 600 ),
193 '600px' => array( 600, 450 ),
194 )
195 ),
196 array(
197 'portrait-rotated.jpg',
198 'image/jpeg',
199 array(
200 'width' => 1024, // since not rotated
201 'height' => 768, // since not rotated
202 ),
203 array(
204 '800x600px' => array( 800, 600 ),
205 '9999x800px' => array( 1067, 800 ),
206 '800px' => array( 800, 600 ),
207 '600px' => array( 600, 450 ),
208 )
209 )
210 );
211 }
212
213
214 const TEST_WIDTH = 100;
215 const TEST_HEIGHT = 200;
216
217 /**
218 * @dataProvider provideBitmapExtractPreRotationDimensions
219 */
220 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
221 $result = $this->handler->extractPreRotationDimensions( array(
222 'physicalWidth' => self::TEST_WIDTH,
223 'physicalHeight' => self::TEST_HEIGHT,
224 ), $rotation );
225 $this->assertEquals( $expected, $result );
226 }
227
228 function provideBitmapExtractPreRotationDimensions() {
229 return array(
230 array(
231 0,
232 array( self::TEST_WIDTH, self::TEST_HEIGHT )
233 ),
234 array(
235 90,
236 array( self::TEST_HEIGHT, self::TEST_WIDTH )
237 ),
238 array(
239 180,
240 array( self::TEST_WIDTH, self::TEST_HEIGHT )
241 ),
242 array(
243 270,
244 array( self::TEST_HEIGHT, self::TEST_WIDTH )
245 ),
246 );
247 }
248 }
249