Debian Codegraph @

tests/phpunit/includes/media/WebPHandlerTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * @covers \WebPHandler
5 */
6 class WebPHandlerTest extends MediaWikiIntegrationTestCase {
7
8 /**
9 * @dataProvider provideTestExtractMetaData
10 */
11 public function testExtractMetaData( $header, $expectedResult ) {
12 $tempFileName = $this->getNewTempFile();
13
14 // Put header into file
15 file_put_contents( $tempFileName, $header );
16
17 $this->assertEquals( $expectedResult, WebPHandler::extractMetadata( $tempFileName ) );
18 }
19
20 public static function provideTestExtractMetaData() {
21 return [
22 // Files from https://developers.google.com/speed/webp/gallery2
23 [ "\x52\x49\x46\x46\x90\x68\x01\x00\x57\x45\x42\x50\x56\x50\x38\x4C\x83\x68\x01\x00\x2F\x8F\x01\x4B\x10\x8D\x38\x6C\xDB\x46\x92\xE0\xE0\x82\x7B\x6C",
24 [ 'compression' => 'lossless', 'width' => 400, 'height' => 301 ] ],
25 [ "\x52\x49\x46\x46\x64\x5B\x00\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x8F\x01\x00\x2C\x01\x00\x41\x4C\x50\x48\xE5\x0E",
26 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 400, 'height' => 301 ] ],
27 [ "\x52\x49\x46\x46\xA8\x72\x00\x00\x57\x45\x42\x50\x56\x50\x38\x4C\x9B\x72\x00\x00\x2F\x81\x81\x62\x10\x8D\x40\x8C\x24\x39\x6E\x73\x73\x38\x01\x96",
28 [ 'compression' => 'lossless', 'width' => 386, 'height' => 395 ] ],
29 [ "\x52\x49\x46\x46\xE0\x42\x00\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x81\x01\x00\x8A\x01\x00\x41\x4C\x50\x48\x56\x10",
30 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 386, 'height' => 395 ] ],
31 [ "\x52\x49\x46\x46\x70\x61\x02\x00\x57\x45\x42\x50\x56\x50\x38\x4C\x63\x61\x02\x00\x2F\x1F\xC3\x95\x10\x8D\xC8\x72\xDB\xC8\x92\x24\xD8\x91\xD9\x91",
32 [ 'compression' => 'lossless', 'width' => 800, 'height' => 600 ] ],
33 [ "\x52\x49\x46\x46\x1C\x1D\x01\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x1F\x03\x00\x57\x02\x00\x41\x4C\x50\x48\x25\x8B",
34 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 800, 'height' => 600 ] ],
35 [ "\x52\x49\x46\x46\xFA\xC5\x00\x00\x57\x45\x42\x50\x56\x50\x38\x4C\xEE\xC5\x00\x00\x2F\xA4\x81\x28\x10\x8D\x40\x68\x24\xC9\x91\xA4\xAE\xF3\x97\x75",
36 [ 'compression' => 'lossless', 'width' => 421, 'height' => 163 ] ],
37 [ "\x52\x49\x46\x46\xF6\x5D\x00\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\xA4\x01\x00\xA2\x00\x00\x41\x4C\x50\x48\x38\x1A",
38 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 421, 'height' => 163 ] ],
39 [ "\x52\x49\x46\x46\xC4\x96\x01\x00\x57\x45\x42\x50\x56\x50\x38\x4C\xB8\x96\x01\x00\x2F\x2B\xC1\x4A\x10\x11\x87\x6D\xDB\x48\x12\xFC\x60\xB0\x83\x24",
40 [ 'compression' => 'lossless', 'width' => 300, 'height' => 300 ] ],
41 [ "\x52\x49\x46\x46\x0A\x11\x01\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x10\x00\x00\x00\x2B\x01\x00\x2B\x01\x00\x41\x4C\x50\x48\x67\x6E",
42 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 300, 'height' => 300 ] ],
43
44 // Lossy files from https://developers.google.com/speed/webp/gallery1
45 [ "\x52\x49\x46\x46\x68\x76\x00\x00\x57\x45\x42\x50\x56\x50\x38\x20\x5C\x76\x00\x00\xD2\xBE\x01\x9D\x01\x2A\x26\x02\x70\x01\x3E\xD5\x4E\x97\x43\xA2",
46 [ 'compression' => 'lossy', 'width' => 550, 'height' => 368 ] ],
47 [ "\x52\x49\x46\x46\xB0\xEC\x00\x00\x57\x45\x42\x50\x56\x50\x38\x20\xA4\xEC\x00\x00\xB2\x4B\x02\x9D\x01\x2A\x26\x02\x94\x01\x3E\xD1\x50\x96\x46\x26",
48 [ 'compression' => 'lossy', 'width' => 550, 'height' => 404 ] ],
49 [ "\x52\x49\x46\x46\x7A\x19\x03\x00\x57\x45\x42\x50\x56\x50\x38\x20\x6E\x19\x03\x00\xB2\xF8\x09\x9D\x01\x2A\x00\x05\xD0\x02\x3E\xAD\x46\x99\x4A\xA5",
50 [ 'compression' => 'lossy', 'width' => 1280, 'height' => 720 ] ],
51 [ "\x52\x49\x46\x46\x44\xB3\x02\x00\x57\x45\x42\x50\x56\x50\x38\x20\x38\xB3\x02\x00\x52\x57\x06\x9D\x01\x2A\x00\x04\x04\x03\x3E\xA5\x44\x96\x49\x26",
52 [ 'compression' => 'lossy', 'width' => 1024, 'height' => 772 ] ],
53 [ "\x52\x49\x46\x46\x02\x43\x01\x00\x57\x45\x42\x50\x56\x50\x38\x20\xF6\x42\x01\x00\x12\xC0\x05\x9D\x01\x2A\x00\x04\xF0\x02\x3E\x79\x34\x93\x47\xA4",
54 [ 'compression' => 'lossy', 'width' => 1024, 'height' => 752 ] ],
55
56 // Animated file from https://groups.google.com/a/chromium.org/d/topic/blink-dev/Y8tRC4mdQz8/discussion
57 [ "\x52\x49\x46\x46\xD0\x0B\x02\x00\x57\x45\x42\x50\x56\x50\x38\x58\x0A\x00\x00\x00\x12\x00\x00\x00\x3F\x01\x00\x3F\x01\x00\x41\x4E",
58 [ 'compression' => 'unknown', 'animated' => true, 'transparency' => true, 'width' => 320, 'height' => 320 ] ],
59
60 // Error cases
61 [ '', false ],
62 [ ' ', false ],
63 [ 'RIFF ', false ],
64 [ 'RIFF1234WEBP ', false ],
65 [ 'RIFF1234WEBPVP8 ', false ],
66 [ 'RIFF1234WEBPVP8L ', false ],
67 ];
68 // phpcs:enable
69 }
70
71 /**
72 * @dataProvider provideTestWithFileExtractMetaData
73 */
74 public function testWithFileExtractMetaData( $filename, $expectedResult ) {
75 $this->assertEquals( $expectedResult, WebPHandler::extractMetadata( $filename ) );
76 }
77
78 public static function provideTestWithFileExtractMetaData() {
79 return [
80 [ __DIR__ . '/../../data/media/2_webp_ll.webp',
81 [
82 'compression' => 'lossless',
83 'width' => 386,
84 'height' => 395
85 ]
86 ],
87 [ __DIR__ . '/../../data/media/2_webp_a.webp',
88 [
89 'compression' => 'lossy',
90 'animated' => false,
91 'transparency' => true,
92 'width' => 386,
93 'height' => 395
94 ]
95 ],
96 ];
97 }
98
99 /**
100 * @dataProvider provideTestGetSizeAndMetadata
101 */
102 public function testGetSizeAndMetadata( $path, $expectedResult ) {
103 $handler = new WebPHandler();
104 $this->assertEquals( $expectedResult, $handler->getSizeAndMetadata( null, $path ) );
105 }
106
107 public static function provideTestGetSizeAndMetadata() {
108 return [
109 // Public domain files from https://developers.google.com/speed/webp/gallery2
110 [
111 __DIR__ . '/../../data/media/2_webp_a.webp',
112 [
113 'width' => 386,
114 'height' => 395,
115 'metadata' => [
116 'compression' => 'lossy',
117 'animated' => false,
118 'transparency' => true,
119 'width' => 386,
120 'height' => 395,
121 'metadata' => [
122 '_MW_WEBP_VERSION' => 2,
123 ],
124 ],
125 ]
126 ],
127 [
128 __DIR__ . '/../../data/media/2_webp_ll.webp',
129 [
130 'width' => 386,
131 'height' => 395,
132 'metadata' => [
133 'compression' => 'lossless',
134 'width' => 386,
135 'height' => 395,
136 'metadata' => [
137 '_MW_WEBP_VERSION' => 2,
138 ],
139 ],
140 ]
141 ],
142 [
143 __DIR__ . '/../../data/media/webp_animated.webp',
144 [
145 'width' => 300,
146 'height' => 225,
147 'metadata' => [
148 'compression' => 'unknown',
149 'animated' => true,
150 'transparency' => true,
151 'width' => 300,
152 'height' => 225,
153 'metadata' => [
154 '_MW_WEBP_VERSION' => 2,
155 ],
156 ],
157 ]
158 ],
159 [
160 __DIR__ . '/../../data/media/exif.webp',
161 [
162 'width' => 40,
163 'height' => 10,
164 'metadata' => [
165 'compression' => 'lossy',
166 'animated' => false,
167 'transparency' => false,
168 'width' => 40,
169 'height' => 10,
170 'metadata' => [
171 '_MW_WEBP_VERSION' => 2,
172 ],
173 'media-metadata' => [
174 'GPSLatitude' => 88.51805555555555,
175 'GPSLongitude' => -21.12357,
176 'GPSAltitude' => -3.1415926530119025,
177 'GPSDOP' => '5/1',
178 'GPSVersionID' => '2.2.0.0'
179 ]
180 ]
181 ]
182 ],
183 // Using non-standard "Exif\0\0" prefix
184 [
185 __DIR__ . '/../../data/media/exif-prefix.webp',
186 [
187 'width' => 40,
188 'height' => 10,
189 'metadata' => [
190 'compression' => 'lossy',
191 'animated' => false,
192 'transparency' => false,
193 'width' => 40,
194 'height' => 10,
195 'metadata' => [
196 '_MW_WEBP_VERSION' => 2,
197 ],
198 'media-metadata' => [
199 'GPSLatitude' => 88.51805555555555,
200 'GPSLongitude' => -21.12357,
201 'GPSAltitude' => -3.1415926530119025,
202 'GPSDOP' => '5/1',
203 'GPSVersionID' => '2.2.0.0'
204 ]
205 ]
206 ]
207 ],
208 // Using standard "xmp " fourcc
209 [
210 __DIR__ . '/../../data/media/xmp.webp',
211 [
212 'width' => 420,
213 'height' => 300,
214 'metadata' => [
215 'compression' => 'lossy',
216 'animated' => false,
217 'transparency' => false,
218 'width' => 420,
219 'height' => 300,
220 'metadata' => [
221 '_MW_WEBP_VERSION' => 2,
222 ],
223 'media-metadata' => [
224 'ImageDescription' => [
225 'x-default' => 'An example image',
226 'en' => 'right translation',
227 '_type' => 'lang'
228 ]
229 ]
230 ]
231 ]
232 ],
233 // Using the "xmp\0" fourcc (not standard "xmp ").
234 [
235 __DIR__ . '/../../data/media/xmp-null.webp',
236 [
237 'width' => 420,
238 'height' => 300,
239 'metadata' => [
240 'compression' => 'lossy',
241 'animated' => false,
242 'transparency' => false,
243 'width' => 420,
244 'height' => 300,
245 'metadata' => [
246 '_MW_WEBP_VERSION' => 2,
247 ],
248 'media-metadata' => [
249 'ImageDescription' => [
250 'x-default' => 'Image with XMPnull byte fourcc',
251 'en' => 'right translation',
252 '_type' => 'lang'
253 ]
254 ]
255 ]
256 ]
257 ],
258 // Containing both XMP and Exif
259 [
260 __DIR__ . '/../../data/media/xmp-exif.webp',
261 [
262 'width' => 420,
263 'height' => 300,
264 'metadata' => [
265 'compression' => 'lossy',
266 'animated' => false,
267 'transparency' => false,
268 'width' => 420,
269 'height' => 300,
270 'metadata' => [
271 '_MW_WEBP_VERSION' => 2,
272 ],
273 'media-metadata' => [
274 'ImageDescription' => [
275 'x-default' => 'right(iptc)',
276 'en' => 'right translation',
277 '_type' => 'lang'
278 ],
279 'XResolution' => '72/1',
280 'YResolution' => '72/1',
281 'ResolutionUnit' => 2,
282 'YCbCrPositioning' => 1,
283 ]
284 ]
285 ]
286 ],
287 // Lossless red square at max resolution to confirm height decoding
288 [
289 __DIR__ . '/../../data/media/webp_ll_large.webp',
290 [
291 'width' => 16383,
292 'height' => 16383,
293 'metadata' => [
294 'compression' => 'lossless',
295 'width' => 16383,
296 'height' => 16383,
297 'metadata' => [
298 '_MW_WEBP_VERSION' => 2,
299 ],
300 ],
301 ]
302 ],
303
304 // Error cases
305 [
306 __FILE__,
307 [ 'metadata' => [ '_error' => '0' ] ],
308 ],
309 ];
310 }
311
312 /**
313 * Tests the WebP MIME detection. This should really be a separate test, but sticking it
314 * here for now.
315 *
316 * @dataProvider provideTestGetMimeType
317 */
318 public function testGuessMimeType( $path ) {
319 $mime = $this->getServiceContainer()->getMimeAnalyzer();
320 $this->assertEquals( 'image/webp', $mime->guessMimeType( $path, false ) );
321 }
322
323 public static function provideTestGetMimeType() {
324 return [
325 // Public domain files from https://developers.google.com/speed/webp/gallery2
326 [ __DIR__ . '/../../data/media/2_webp_a.webp' ],
327 [ __DIR__ . '/../../data/media/2_webp_ll.webp' ],
328 [ __DIR__ . '/../../data/media/webp_animated.webp' ],
329 ];
330 }
331 }
332
333 /* Python code to extract a header and convert to PHP format:
334 * print '"%s"' % ''.implode( '\\x%02X' % ord(c) for c in urllib.urlopen(url).read(36) )
335 */