Merge "Fixed dependencies for jquery.collapsibleTabs"
[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 protected 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 protected function tearDown() {
37 global $wgShowEXIF, $wgEnableAutoRotation;
38 $wgShowEXIF = $this->show;
39 $wgEnableAutoRotation = $this->oldAuto;
40
41 parent::tearDown();
42 }
43
44 /**
45 *
46 * @dataProvider provideFiles
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 provideFiles
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 /* Utility function */
98 private function dataFile( $name, $type ) {
99 return new UnregisteredLocalFile( false, $this->repo,
100 "mwstore://localtesting/data/$name", $type );
101 }
102
103 public static function provideFiles() {
104 return array(
105 array(
106 'landscape-plain.jpg',
107 'image/jpeg',
108 array(
109 'width' => 1024,
110 'height' => 768,
111 ),
112 array(
113 '800x600px' => array( 800, 600 ),
114 '9999x800px' => array( 1067, 800 ),
115 '800px' => array( 800, 600 ),
116 '600px' => array( 600, 450 ),
117 )
118 ),
119 array(
120 'portrait-rotated.jpg',
121 'image/jpeg',
122 array(
123 'width' => 768, // as rotated
124 'height' => 1024, // as rotated
125 ),
126 array(
127 '800x600px' => array( 450, 600 ),
128 '9999x800px' => array( 600, 800 ),
129 '800px' => array( 800, 1067 ),
130 '600px' => array( 600, 800 ),
131 )
132 )
133 );
134 }
135
136 /**
137 * Same as before, but with auto-rotation disabled.
138 * @dataProvider provideFilesNoAutoRotate
139 */
140 function testMetadataNoAutoRotate( $name, $type, $info ) {
141 global $wgEnableAutoRotation;
142 $wgEnableAutoRotation = false;
143
144 $file = $this->dataFile( $name, $type );
145 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
146 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
147
148 $wgEnableAutoRotation = true;
149 }
150
151 /**
152 *
153 * @dataProvider provideFilesNoAutoRotate
154 */
155 function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
156 global $wgEnableAutoRotation;
157 $wgEnableAutoRotation = false;
158
159 foreach( $thumbs as $size => $out ) {
160 if( preg_match('/^(\d+)px$/', $size, $matches ) ) {
161 $params = array(
162 'width' => $matches[1],
163 );
164 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
165 $params = array(
166 'width' => $matches[1],
167 'height' => $matches[2]
168 );
169 } else {
170 throw new MWException('bogus test data format ' . $size);
171 }
172
173 $file = $this->dataFile( $name, $type );
174 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
175
176 $this->assertEquals( $out[0], $thumb->getWidth(), "$name: thumb reported width check for $size" );
177 $this->assertEquals( $out[1], $thumb->getHeight(), "$name: thumb reported height check for $size" );
178
179 $gis = getimagesize( $thumb->getLocalCopyPath() );
180 if ($out[0] > $info['width']) {
181 // Physical image won't be scaled bigger than the original.
182 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size");
183 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size");
184 } else {
185 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size");
186 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size");
187 }
188 }
189 $wgEnableAutoRotation = true;
190 }
191
192 public static function provideFilesNoAutoRotate() {
193 return array(
194 array(
195 'landscape-plain.jpg',
196 'image/jpeg',
197 array(
198 'width' => 1024,
199 'height' => 768,
200 ),
201 array(
202 '800x600px' => array( 800, 600 ),
203 '9999x800px' => array( 1067, 800 ),
204 '800px' => array( 800, 600 ),
205 '600px' => array( 600, 450 ),
206 )
207 ),
208 array(
209 'portrait-rotated.jpg',
210 'image/jpeg',
211 array(
212 'width' => 1024, // since not rotated
213 'height' => 768, // since not rotated
214 ),
215 array(
216 '800x600px' => array( 800, 600 ),
217 '9999x800px' => array( 1067, 800 ),
218 '800px' => array( 800, 600 ),
219 '600px' => array( 600, 450 ),
220 )
221 )
222 );
223 }
224
225
226 const TEST_WIDTH = 100;
227 const TEST_HEIGHT = 200;
228
229 /**
230 * @dataProvider provideBitmapExtractPreRotationDimensions
231 */
232 function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
233 $result = $this->handler->extractPreRotationDimensions( array(
234 'physicalWidth' => self::TEST_WIDTH,
235 'physicalHeight' => self::TEST_HEIGHT,
236 ), $rotation );
237 $this->assertEquals( $expected, $result );
238 }
239
240 function provideBitmapExtractPreRotationDimensions() {
241 return array(
242 array(
243 0,
244 array( self::TEST_WIDTH, self::TEST_HEIGHT )
245 ),
246 array(
247 90,
248 array( self::TEST_HEIGHT, self::TEST_WIDTH )
249 ),
250 array(
251 180,
252 array( self::TEST_WIDTH, self::TEST_HEIGHT )
253 ),
254 array(
255 270,
256 array( self::TEST_HEIGHT, self::TEST_WIDTH )
257 ),
258 );
259 }
260 }
261