Pass phpcs-strict on some test files (8/11)
[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 * @todo covers tags
8 */
9 class ExifRotationTest extends MediaWikiTestCase {
10
11 protected function setUp() {
12 parent::setUp();
13 $this->checkPHPExtension( 'exif' );
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 'wikiId' => wfWikiId(),
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 * @dataProvider provideFiles
38 */
39 public function testMetadata( $name, $type, $info ) {
40 if ( !BitmapHandler::canRotate() ) {
41 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
42 }
43 $file = $this->dataFile( $name, $type );
44 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
45 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
46 }
47
48 /**
49 *
50 * @dataProvider provideFiles
51 */
52 public function testRotationRendering( $name, $type, $info, $thumbs ) {
53 if ( !BitmapHandler::canRotate() ) {
54 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
55 }
56 foreach ( $thumbs as $size => $out ) {
57 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
58 $params = array(
59 'width' => $matches[1],
60 );
61 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
62 $params = array(
63 'width' => $matches[1],
64 'height' => $matches[2]
65 );
66 } else {
67 throw new MWException( 'bogus test data format ' . $size );
68 }
69
70 $file = $this->dataFile( $name, $type );
71 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
72
73 $this->assertEquals(
74 $out[0],
75 $thumb->getWidth(),
76 "$name: thumb reported width check for $size"
77 );
78 $this->assertEquals(
79 $out[1],
80 $thumb->getHeight(),
81 "$name: thumb reported height check for $size"
82 );
83
84 $gis = getimagesize( $thumb->getLocalCopyPath() );
85 if ( $out[0] > $info['width'] ) {
86 // Physical image won't be scaled bigger than the original.
87 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
88 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
89 } else {
90 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
91 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
92 }
93 }
94 }
95
96 /* Utility function */
97 private function dataFile( $name, $type ) {
98 return new UnregisteredLocalFile( false, $this->repo,
99 "mwstore://localtesting/data/$name", $type );
100 }
101
102 public static function provideFiles() {
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 provideFilesNoAutoRotate
138 */
139 public function testMetadataNoAutoRotate( $name, $type, $info ) {
140 $this->setMwGlobals( 'wgEnableAutoRotation', false );
141
142 $file = $this->dataFile( $name, $type );
143 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
144 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
145 }
146
147 /**
148 *
149 * @dataProvider provideFilesNoAutoRotate
150 */
151 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
152 $this->setMwGlobals( 'wgEnableAutoRotation', false );
153
154 foreach ( $thumbs as $size => $out ) {
155 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
156 $params = array(
157 'width' => $matches[1],
158 );
159 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
160 $params = array(
161 'width' => $matches[1],
162 'height' => $matches[2]
163 );
164 } else {
165 throw new MWException( 'bogus test data format ' . $size );
166 }
167
168 $file = $this->dataFile( $name, $type );
169 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
170
171 $this->assertEquals(
172 $out[0],
173 $thumb->getWidth(),
174 "$name: thumb reported width check for $size"
175 );
176 $this->assertEquals(
177 $out[1],
178 $thumb->getHeight(),
179 "$name: thumb reported height check for $size"
180 );
181
182 $gis = getimagesize( $thumb->getLocalCopyPath() );
183 if ( $out[0] > $info['width'] ) {
184 // Physical image won't be scaled bigger than the original.
185 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
186 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
187 } else {
188 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
189 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
190 }
191 }
192 }
193
194 public static function provideFilesNoAutoRotate() {
195 return array(
196 array(
197 'landscape-plain.jpg',
198 'image/jpeg',
199 array(
200 'width' => 1024,
201 'height' => 768,
202 ),
203 array(
204 '800x600px' => array( 800, 600 ),
205 '9999x800px' => array( 1067, 800 ),
206 '800px' => array( 800, 600 ),
207 '600px' => array( 600, 450 ),
208 )
209 ),
210 array(
211 'portrait-rotated.jpg',
212 'image/jpeg',
213 array(
214 'width' => 1024, // since not rotated
215 'height' => 768, // since not rotated
216 ),
217 array(
218 '800x600px' => array( 800, 600 ),
219 '9999x800px' => array( 1067, 800 ),
220 '800px' => array( 800, 600 ),
221 '600px' => array( 600, 450 ),
222 )
223 )
224 );
225 }
226
227 const TEST_WIDTH = 100;
228 const TEST_HEIGHT = 200;
229
230 /**
231 * @dataProvider provideBitmapExtractPreRotationDimensions
232 */
233 public 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 public static 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 }