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