merged master
[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->handler = new BitmapHandler();
11 $filePath = __DIR__ . '/../../data/media';
12
13 $tmpDir = $this->getNewTempDirectory();
14
15 $this->repo = new FSRepo( array(
16 'name' => 'temp',
17 'url' => 'http://localhost/thumbtest',
18 'backend' => new FSFileBackend( array(
19 'name' => 'localtesting',
20 'lockManager' => 'nullLockManager',
21 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
22 ) )
23 ) );
24 if ( !wfDl( 'exif' ) ) {
25 $this->markTestSkipped( "This test needs the exif extension." );
26 }
27 global $wgShowEXIF;
28 $this->show = $wgShowEXIF;
29 $wgShowEXIF = true;
30
31 global $wgEnableAutoRotation;
32 $this->oldAuto = $wgEnableAutoRotation;
33 $wgEnableAutoRotation = true;
34 }
35
36 public function tearDown() {
37 global $wgShowEXIF, $wgEnableAutoRotation;
38 $wgShowEXIF = $this->show;
39 $wgEnableAutoRotation = $this->oldAuto;
40
41 parent::tearDown();
42 }
43
44 /**
45 *
46 * @dataProvider providerFiles
47 */
48 function testMetadata( $name, $type, $info ) {
49 if ( !BitmapHandler::canRotate() ) {
50 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
51 }
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" );
55 }
56
57 /**
58 *
59 * @dataProvider providerFiles
60 */
61 function testRotationRendering( $name, $type, $info, $thumbs ) {
62 if ( !BitmapHandler::canRotate() ) {
63 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
64 }
65 foreach( $thumbs as $size => $out ) {
66 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
67 $params = array(
68 'width' => $matches[1],
69 );
70 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
71 $params = array(
72 'width' => $matches[1],
73 'height' => $matches[2]
74 );
75 } else {
76 throw new MWException('bogus test data format ' . $size);
77 }
78
79 $file = $this->dataFile( $name, $type );
80 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
81
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" );
84
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");
90 } else {
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");
93 }
94 }
95 }
96
97 private function dataFile( $name, $type ) {
98 return new UnregisteredLocalFile( false, $this->repo,
99 "mwstore://localtesting/data/$name", $type );
100 }
101
102 function providerFiles() {
103 return array(
104 array(
105 'landscape-plain.jpg',
106 'image/jpeg',
107 array(
108 'width' => 1024,
109 'height' => 768,
110 ),
111 array(
112 '800x600px' => array( 800, 600 ),
113 '9999x800px' => array( 1067, 800 ),
114 '800px' => array( 800, 600 ),
115 '600px' => array( 600, 450 ),
116 )
117 ),
118 array(
119 'portrait-rotated.jpg',
120 'image/jpeg',
121 array(
122 'width' => 768, // as rotated
123 'height' => 1024, // as rotated
124 ),
125 array(
126 '800x600px' => array( 450, 600 ),
127 '9999x800px' => array( 600, 800 ),
128 '800px' => array( 800, 1067 ),
129 '600px' => array( 600, 800 ),
130 )
131 )
132 );
133 }
134
135 /**
136 * Same as before, but with auto-rotation disabled.
137 * @dataProvider providerFilesNoAutoRotate
138 */
139 function testMetadataNoAutoRotate( $name, $type, $info ) {
140 global $wgEnableAutoRotation;
141 $wgEnableAutoRotation = false;
142
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" );
146
147 $wgEnableAutoRotation = true;
148 }
149
150 /**
151 *
152 * @dataProvider providerFilesNoAutoRotate
153 */
154 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
155 global $wgEnableAutoRotation;
156 $wgEnableAutoRotation = false;
157
158 foreach( $thumbs as $size => $out ) {
159 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
160 $params = array(
161 'width' => $matches[1],
162 );
163 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
164 $params = array(
165 'width' => $matches[1],
166 'height' => $matches[2]
167 );
168 } else {
169 throw new MWException('bogus test data format ' . $size);
170 }
171
172 $file = $this->dataFile( $name, $type );
173 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
174
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" );
177
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");
183 } else {
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");
186 }
187 }
188 $wgEnableAutoRotation = true;
189 }
190
191 function providerFilesNoAutoRotate() {
192 return array(
193 array(
194 'landscape-plain.jpg',
195 'image/jpeg',
196 array(
197 'width' => 1024,
198 'height' => 768,
199 ),
200 array(
201 '800x600px' => array( 800, 600 ),
202 '9999x800px' => array( 1067, 800 ),
203 '800px' => array( 800, 600 ),
204 '600px' => array( 600, 450 ),
205 )
206 ),
207 array(
208 'portrait-rotated.jpg',
209 'image/jpeg',
210 array(
211 'width' => 1024, // since not rotated
212 'height' => 768, // since not rotated
213 ),
214 array(
215 '800x600px' => array( 800, 600 ),
216 '9999x800px' => array( 1067, 800 ),
217 '800px' => array( 800, 600 ),
218 '600px' => array( 600, 450 ),
219 )
220 )
221 );
222 }
223
224
225 const TEST_WIDTH = 100;
226 const TEST_HEIGHT = 200;
227
228 /**
229 * @dataProvider provideBitmapExtractPreRotationDimensions
230 */
231 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
232 $result = $this->handler->extractPreRotationDimensions( array(
233 'physicalWidth' => self::TEST_WIDTH,
234 'physicalHeight' => self::TEST_HEIGHT,
235 ), $rotation );
236 $this->assertEquals( $expected, $result );
237 }
238
239 function provideBitmapExtractPreRotationDimensions() {
240 return array(
241 array(
242 0,
243 array( self::TEST_WIDTH, self::TEST_HEIGHT )
244 ),
245 array(
246 90,
247 array( self::TEST_HEIGHT, self::TEST_WIDTH )
248 ),
249 array(
250 180,
251 array( self::TEST_WIDTH, self::TEST_HEIGHT )
252 ),
253 array(
254 270,
255 array( self::TEST_HEIGHT, self::TEST_WIDTH )
256 ),
257 );
258 }
259 }
260