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