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