MediaWiki REL1_30
ExifRotationTest.php
Go to the documentation of this file.
1<?php
11
12 protected function setUp() {
13 parent::setUp();
14 $this->checkPHPExtension( 'exif' );
15
16 $this->handler = new BitmapHandler();
17
18 $this->setMwGlobals( [
19 'wgShowEXIF' => true,
20 'wgEnableAutoRotation' => true,
21 ] );
22 }
23
27 protected function createsThumbnails() {
28 return true;
29 }
30
34 public function testMetadata( $name, $type, $info ) {
35 if ( !$this->handler->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
50 public function testMetadataAutoRotate( $name, $type, $info ) {
51 $this->setMwGlobals( 'wgEnableAutoRotation', null );
52 $this->setMwGlobals( 'wgUseImageMagick', true );
53 $this->setMwGlobals( 'wgUseImageResize', true );
54
55 $file = $this->dataFile( $name, $type );
56 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
57 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
58 }
59
64 public function testRotationRendering( $name, $type, $info, $thumbs ) {
65 if ( !$this->handler->canRotate() ) {
66 $this->markTestSkipped( "This test needs a rasterizer that can auto-rotate." );
67 }
68 foreach ( $thumbs as $size => $out ) {
69 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
70 $params = [
71 'width' => $matches[1],
72 ];
73 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
74 $params = [
75 'width' => $matches[1],
76 'height' => $matches[2]
77 ];
78 } else {
79 throw new MWException( 'bogus test data format ' . $size );
80 }
81
82 $file = $this->dataFile( $name, $type );
83 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
84
85 $this->assertEquals(
86 $out[0],
87 $thumb->getWidth(),
88 "$name: thumb reported width check for $size"
89 );
90 $this->assertEquals(
91 $out[1],
92 $thumb->getHeight(),
93 "$name: thumb reported height check for $size"
94 );
95
96 $gis = getimagesize( $thumb->getLocalCopyPath() );
97 if ( $out[0] > $info['width'] ) {
98 // Physical image won't be scaled bigger than the original.
99 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
100 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
101 } else {
102 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
103 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
104 }
105 }
106 }
107
108 public static function provideFiles() {
109 return [
110 [
111 'landscape-plain.jpg',
112 'image/jpeg',
113 [
114 'width' => 1024,
115 'height' => 768,
116 ],
117 [
118 '800x600px' => [ 800, 600 ],
119 '9999x800px' => [ 1067, 800 ],
120 '800px' => [ 800, 600 ],
121 '600px' => [ 600, 450 ],
122 ]
123 ],
124 [
125 'portrait-rotated.jpg',
126 'image/jpeg',
127 [
128 'width' => 768, // as rotated
129 'height' => 1024, // as rotated
130 ],
131 [
132 '800x600px' => [ 450, 600 ],
133 '9999x800px' => [ 600, 800 ],
134 '800px' => [ 800, 1067 ],
135 '600px' => [ 600, 800 ],
136 ]
137 ]
138 ];
139 }
140
145 public function testMetadataNoAutoRotate( $name, $type, $info ) {
146 $this->setMwGlobals( 'wgEnableAutoRotation', false );
147
148 $file = $this->dataFile( $name, $type );
149 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
150 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
151 }
152
157 public function testMetadataAutoRotateUnsupported( $name, $type, $info ) {
158 $this->setMwGlobals( 'wgEnableAutoRotation', null );
159 $this->setMwGlobals( 'wgUseImageResize', false );
160
161 $file = $this->dataFile( $name, $type );
162 $this->assertEquals( $info['width'], $file->getWidth(), "$name: width check" );
163 $this->assertEquals( $info['height'], $file->getHeight(), "$name: height check" );
164 }
165
170 public function testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs ) {
171 $this->setMwGlobals( 'wgEnableAutoRotation', false );
172
173 foreach ( $thumbs as $size => $out ) {
174 if ( preg_match( '/^(\d+)px$/', $size, $matches ) ) {
175 $params = [
176 'width' => $matches[1],
177 ];
178 } elseif ( preg_match( '/^(\d+)x(\d+)px$/', $size, $matches ) ) {
179 $params = [
180 'width' => $matches[1],
181 'height' => $matches[2]
182 ];
183 } else {
184 throw new MWException( 'bogus test data format ' . $size );
185 }
186
187 $file = $this->dataFile( $name, $type );
188 $thumb = $file->transform( $params, File::RENDER_NOW | File::RENDER_FORCE );
189
190 $this->assertEquals(
191 $out[0],
192 $thumb->getWidth(),
193 "$name: thumb reported width check for $size"
194 );
195 $this->assertEquals(
196 $out[1],
197 $thumb->getHeight(),
198 "$name: thumb reported height check for $size"
199 );
200
201 $gis = getimagesize( $thumb->getLocalCopyPath() );
202 if ( $out[0] > $info['width'] ) {
203 // Physical image won't be scaled bigger than the original.
204 $this->assertEquals( $info['width'], $gis[0], "$name: thumb actual width check for $size" );
205 $this->assertEquals( $info['height'], $gis[1], "$name: thumb actual height check for $size" );
206 } else {
207 $this->assertEquals( $out[0], $gis[0], "$name: thumb actual width check for $size" );
208 $this->assertEquals( $out[1], $gis[1], "$name: thumb actual height check for $size" );
209 }
210 }
211 }
212
213 public static function provideFilesNoAutoRotate() {
214 return [
215 [
216 'landscape-plain.jpg',
217 'image/jpeg',
218 [
219 'width' => 1024,
220 'height' => 768,
221 ],
222 [
223 '800x600px' => [ 800, 600 ],
224 '9999x800px' => [ 1067, 800 ],
225 '800px' => [ 800, 600 ],
226 '600px' => [ 600, 450 ],
227 ]
228 ],
229 [
230 'portrait-rotated.jpg',
231 'image/jpeg',
232 [
233 'width' => 1024, // since not rotated
234 'height' => 768, // since not rotated
235 ],
236 [
237 '800x600px' => [ 800, 600 ],
238 '9999x800px' => [ 1067, 800 ],
239 '800px' => [ 800, 600 ],
240 '600px' => [ 600, 450 ],
241 ]
242 ]
243 ];
244 }
245
246 const TEST_WIDTH = 100;
247 const TEST_HEIGHT = 200;
248
252 public function testBitmapExtractPreRotationDimensions( $rotation, $expected ) {
253 $result = $this->handler->extractPreRotationDimensions( [
254 'physicalWidth' => self::TEST_WIDTH,
255 'physicalHeight' => self::TEST_HEIGHT,
256 ], $rotation );
257 $this->assertEquals( $expected, $result );
258 }
259
261 return [
262 [
263 0,
265 ],
266 [
267 90,
269 ],
270 [
271 180,
273 ],
274 [
275 270,
277 ],
278 ];
279 }
280}
Generic handler for bitmap images.
Definition Bitmap.php:29
Tests related to auto rotation.
testMetadataAutoRotate( $name, $type, $info)
Same as before, but with auto-rotation set to auto.
testBitmapExtractPreRotationDimensions( $rotation, $expected)
provideBitmapExtractPreRotationDimensions
testRotationRenderingNoAutoRotate( $name, $type, $info, $thumbs)
provideFilesNoAutoRotate
static provideBitmapExtractPreRotationDimensions()
static provideFilesNoAutoRotate()
testMetadataNoAutoRotate( $name, $type, $info)
Same as before, but with auto-rotation disabled.
testMetadata( $name, $type, $info)
provideFiles
testRotationRendering( $name, $type, $info, $thumbs)
provideFiles
testMetadataAutoRotateUnsupported( $name, $type, $info)
Same as before, but with auto-rotation set to auto and an image scaler that doesn't support it.
createsThumbnails()
Mark this test as creating thumbnail files.
const RENDER_NOW
Force rendering in the current process.
Definition File.php:59
const RENDER_FORCE
Force rendering even if thumbnail already exist and using RENDER_NOW I.e.
Definition File.php:64
MediaWiki exception.
Specificly for testing Media handlers.
dataFile( $name, $type=null)
Utility function: Get a new file object for a file on disk but not actually in db.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
checkPHPExtension( $extName)
Check if $extName is a loaded PHP extension, will skip the test whenever it is not loaded.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable modifiable after all normalizations have been except for the $wgMaxImageArea check set to true or false to override the $wgMaxImageArea check result gives extension the possibility to transform it themselves set to a MediaTransformOutput the error message to be returned in an array you should do so by altering $wgNamespaceProtection and $wgNamespaceContentModels outside the handler
Definition hooks.txt:928
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:862
$params