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