Merge "Chinese Conversion Table Update 2017-6"
[lhc/web/wiklou.git] / tests / phpunit / includes / media / WebPTest.php
1 <?php
2
3 /**
4 * @covers WebPHandler
5 */
6 class WebPHandlerTest extends MediaWikiTestCase {
7 public function setUp() {
8 parent::setUp();
9 // Allocated file for testing
10 $this->tempFileName = tempnam( wfTempDir(), 'WEBP' );
11 }
12 public function tearDown() {
13 parent::tearDown();
14 unlink( $this->tempFileName );
15 }
16 /**
17 * @dataProvider provideTestExtractMetaData
18 */
19 public function testExtractMetaData( $header, $expectedResult ) {
20 // Put header into file
21 file_put_contents( $this->tempFileName, $header );
22
23 $this->assertEquals( $expectedResult, WebPHandler::extractMetadata( $this->tempFileName ) );
24 }
25 public function provideTestExtractMetaData() {
26 // @codingStandardsIgnoreStart Generic.Files.LineLength
27 return [
28 // Files from https://developers.google.com/speed/webp/gallery2
29 [ "\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",
30 [ 'compression' => 'lossless', 'width' => 400, 'height' => 301 ] ],
31 [ "\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",
32 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 400, 'height' => 301 ] ],
33 [ "\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",
34 [ 'compression' => 'lossless', 'width' => 386, 'height' => 395 ] ],
35 [ "\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",
36 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 386, 'height' => 395 ] ],
37 [ "\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",
38 [ 'compression' => 'lossless', 'width' => 800, 'height' => 600 ] ],
39 [ "\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",
40 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 800, 'height' => 600 ] ],
41 [ "\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",
42 [ 'compression' => 'lossless', 'width' => 421, 'height' => 163 ] ],
43 [ "\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",
44 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 421, 'height' => 163 ] ],
45 [ "\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",
46 [ 'compression' => 'lossless', 'width' => 300, 'height' => 300 ] ],
47 [ "\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",
48 [ 'compression' => 'unknown', 'animated' => false, 'transparency' => true, 'width' => 300, 'height' => 300 ] ],
49
50 // Lossy files from https://developers.google.com/speed/webp/gallery1
51 [ "\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",
52 [ 'compression' => 'lossy', 'width' => 550, 'height' => 368 ] ],
53 [ "\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",
54 [ 'compression' => 'lossy', 'width' => 550, 'height' => 404 ] ],
55 [ "\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",
56 [ 'compression' => 'lossy', 'width' => 1280, 'height' => 720 ] ],
57 [ "\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",
58 [ 'compression' => 'lossy', 'width' => 1024, 'height' => 772 ] ],
59 [ "\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",
60 [ 'compression' => 'lossy', 'width' => 1024, 'height' => 752 ] ],
61
62 // Animated file from https://groups.google.com/a/chromium.org/d/topic/blink-dev/Y8tRC4mdQz8/discussion
63 [ "\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",
64 [ 'compression' => 'unknown', 'animated' => true, 'transparency' => true, 'width' => 320, 'height' => 320 ] ],
65
66 // Error cases
67 [ '', false ],
68 [ ' ', false ],
69 [ 'RIFF ', false ],
70 [ 'RIFF1234WEBP ', false ],
71 [ 'RIFF1234WEBPVP8 ', false ],
72 [ 'RIFF1234WEBPVP8L ', false ],
73 ];
74 // @codingStandardsIgnoreEnd
75 }
76
77 /**
78 * @dataProvider provideTestWithFileExtractMetaData
79 */
80 public function testWithFileExtractMetaData( $filename, $expectedResult ) {
81 $this->assertEquals( $expectedResult, WebPHandler::extractMetadata( $filename ) );
82 }
83 public function provideTestWithFileExtractMetaData() {
84 return [
85 [ __DIR__ . '/../../data/media/2_webp_ll.webp',
86 [
87 'compression' => 'lossless',
88 'width' => 386,
89 'height' => 395
90 ]
91 ],
92 [ __DIR__ . '/../../data/media/2_webp_a.webp',
93 [
94 'compression' => 'lossy',
95 'animated' => false,
96 'transparency' => true,
97 'width' => 386,
98 'height' => 395
99 ]
100 ],
101 ];
102 }
103
104 /**
105 * @dataProvider provideTestGetImageSize
106 */
107 public function testGetImageSize( $path, $expectedResult ) {
108 $handler = new WebPHandler();
109 $this->assertEquals( $expectedResult, $handler->getImageSize( null, $path ) );
110 }
111 public function provideTestGetImageSize() {
112 return [
113 // Public domain files from https://developers.google.com/speed/webp/gallery2
114 [ __DIR__ . '/../../data/media/2_webp_a.webp', [ 386, 395 ] ],
115 [ __DIR__ . '/../../data/media/2_webp_ll.webp', [ 386, 395 ] ],
116 [ __DIR__ . '/../../data/media/webp_animated.webp', [ 300, 225 ] ],
117
118 // Error cases
119 [ __FILE__, false ],
120 ];
121 }
122
123 /**
124 * Tests the WebP MIME detection. This should really be a separate test, but sticking it
125 * here for now.
126 *
127 * @dataProvider provideTestGetMimeType
128 */
129 public function testGuessMimeType( $path ) {
130 $mime = MediaWiki\MediaWikiServices::getInstance()->getMimeAnalyzer();
131 $this->assertEquals( 'image/webp', $mime->guessMimeType( $path, false ) );
132 }
133 public function provideTestGetMimeType() {
134 return [
135 // Public domain files from https://developers.google.com/speed/webp/gallery2
136 [ __DIR__ . '/../../data/media/2_webp_a.webp' ],
137 [ __DIR__ . '/../../data/media/2_webp_ll.webp' ],
138 [ __DIR__ . '/../../data/media/webp_animated.webp' ],
139 ];
140 }
141 }
142
143 /* Python code to extract a header and convert to PHP format:
144 * print '"%s"' % ''.implode( '\\x%02X' % ord(c) for c in urllib.urlopen(url).read(36) )
145 */