Remove wfDl() and cleanup everything it touched
[lhc/web/wiklou.git] / tests / phpunit / includes / media / ExifRotationTest.php
1 <?php
2 /**
3 * Tests related to auto rotation.
4 *
5 * @group medium
6 */
7 class ExifRotationTest extends MediaWikiTestCase {
8
9 protected function setUp() {
10 parent::setUp();
11 if ( !extension_loaded( 'exif' ) ) {
12 $this->markTestSkipped( "This test needs the exif extension." );
13 }
14
15 $this->handler = new BitmapHandler();
16 $filePath = __DIR__ . '/../../data/media';
17
18 $tmpDir = $this->getNewTempDirectory();
19
20 $this->repo = new FSRepo( array(
21 'name' => 'temp',
22 'url' => 'http://localhost/thumbtest',
23 'backend' => new FSFileBackend( array(
24 'name' => 'localtesting',
25 'lockManager' => 'nullLockManager',
26 'containerPaths' => array( 'temp-thumb' => $tmpDir, 'data' => $filePath )
27 ) )
28 ) );
29
30 $this->setMwGlobals( array(
31 'wgShowEXIF' => true,
32 'wgEnableAutoRotation' => true,
33 ) );
34 }
35
36 /**
37 *
38 * @dataProvider provideFiles
39 */
40 function testMetadata( $name, $type, $info ) {
41 if ( !BitmapHandler::canRotate() ) {
42 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
43 }
44 $file = $this->dataFile( $name, $type );
45 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
46 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
47 }
48
49 /**
50 *
51 * @dataProvider provideFiles
52 */
53 function testRotationRendering( $name, $type, $info, $thumbs ) {
54 if ( !BitmapHandler::canRotate() ) {
55 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
56 }
57 foreach ( $thumbs as $size => $out ) {
58 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
59 $params = array(
60 'width' => $matches[1],
61 );
62 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
63 $params = array(
64 'width' => $matches[1],
65 'height' => $matches[2]
66 );
67 } else {
68 throw new MWException( 'bogus test data format ' . $size );
69 }
70
71 $file = $this->dataFile( $name, $type );
72 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
73
74 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
75 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
76
77 $gis = getimagesize( $thumb->getLocalCopyPath() );
78 if ( $out[0] > $info['width'] ) {
79 // Physical image won't be scaled bigger than the original.
80 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
81 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
82 } else {
83 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
84 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
85 }
86 }
87 }
88
89 /* Utility function */
90 private function dataFile( $name, $type ) {
91 return new UnregisteredLocalFile( false, $this->repo,
92 "mwstore://localtesting/data/$name", $type );
93 }
94
95 public static function provideFiles() {
96 return array(
97 array(
98 'landscape-plain.jpg',
99 'image/jpeg',
100 array(
101 'width' => 1024,
102 'height' => 768,
103 ),
104 array(
105 '800x600px' => array( 800, 600 ),
106 '9999x800px' => array( 1067, 800 ),
107 '800px' => array( 800, 600 ),
108 '600px' => array( 600, 450 ),
109 )
110 ),
111 array(
112 'portrait-rotated.jpg',
113 'image/jpeg',
114 array(
115 'width' => 768, // as rotated
116 'height' => 1024, // as rotated
117 ),
118 array(
119 '800x600px' => array( 450, 600 ),
120 '9999x800px' => array( 600, 800 ),
121 '800px' => array( 800, 1067 ),
122 '600px' => array( 600, 800 ),
123 )
124 )
125 );
126 }
127
128 /**
129 * Same as before, but with auto-rotation disabled.
130 * @dataProvider provideFilesNoAutoRotate
131 */
132 function testMetadataNoAutoRotate( $name, $type, $info ) {
133 $this->setMwGlobals( 'wgEnableAutoRotation', false );
134
135 $file = $this->dataFile( $name, $type );
136 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
137 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
138 }
139
140 /**
141 *
142 * @dataProvider provideFilesNoAutoRotate
143 */
144 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
145 $this->setMwGlobals( '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->dataFile( $name, $type );
162 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
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->getLocalCopyPath() );
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 }
178
179 public static function provideFilesNoAutoRotate() {
180 return array(
181 array(
182 'landscape-plain.jpg',
183 'image/jpeg',
184 array(
185 'width' => 1024,
186 'height' => 768,
187 ),
188 array(
189 '800x600px' => array( 800, 600 ),
190 '9999x800px' => array( 1067, 800 ),
191 '800px' => array( 800, 600 ),
192 '600px' => array( 600, 450 ),
193 )
194 ),
195 array(
196 'portrait-rotated.jpg',
197 'image/jpeg',
198 array(
199 'width' => 1024, // since not rotated
200 'height' => 768, // since not rotated
201 ),
202 array(
203 '800x600px' => array( 800, 600 ),
204 '9999x800px' => array( 1067, 800 ),
205 '800px' => array( 800, 600 ),
206 '600px' => array( 600, 450 ),
207 )
208 )
209 );
210 }
211
212
213 const TEST_WIDTH = 100;
214 const TEST_HEIGHT = 200;
215
216 /**
217 * @dataProvider provideBitmapExtractPreRotationDimensions
218 */
219 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
220 $result = $this->handler->extractPreRotationDimensions( array(
221 'physicalWidth' => self::TEST_WIDTH,
222 'physicalHeight' => self::TEST_HEIGHT,
223 ), $rotation );
224 $this->assertEquals( $expected, $result );
225 }
226
227 public static function provideBitmapExtractPreRotationDimensions() {
228 return array(
229 array(
230 0,
231 array( self::TEST_WIDTH, self::TEST_HEIGHT )
232 ),
233 array(
234 90,
235 array( self::TEST_HEIGHT, self::TEST_WIDTH )
236 ),
237 array(
238 180,
239 array( self::TEST_WIDTH, self::TEST_HEIGHT )
240 ),
241 array(
242 270,
243 array( self::TEST_HEIGHT, self::TEST_WIDTH )
244 ),
245 );
246 }
247 }