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