Debian Codegraph @

tests/phpunit/includes/media/GIFHandlerTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * @group Media
5 */
6 class GIFHandlerTest extends MediaWikiMediaTestCase {
7
8 /** @var GIFHandler */
9 protected $handler;
10
11 protected function setUp(): void {
12 parent::setUp();
13
14 $this->handler = new GIFHandler();
15 }
16
17 /**
18 * @return array Unserialized metadata array for GIFHandler::BROKEN_FILE
19 */
20 private function brokenFile(): array {
21 return [ '_error' => 0 ];
22 }
23
24 /**
25 * @covers \GIFHandler::getSizeAndMetadata
26 */
27 public function testInvalidFile() {
28 $res = $this->handler->getSizeAndMetadata( null, $this->filePath . '/README' );
29 $this->assertEquals( $this->brokenFile(), $res['metadata'] );
30 }
31
32 /**
33 * @param string $filename Basename of the file to check
34 * @param bool $expected Expected result.
35 * @dataProvider provideIsAnimated
36 * @covers \GIFHandler::isAnimatedImage
37 */
38 public function testIsAnimated( $filename, $expected ) {
39 $file = $this->dataFile( $filename, 'image/gif' );
40 $actual = $this->handler->isAnimatedImage( $file );
41 $this->assertEquals( $expected, $actual );
42 }
43
44 public static function provideIsAnimated() {
45 return [
46 [ 'animated.gif', true ],
47 [ 'nonanimated.gif', false ],
48 ];
49 }
50
51 /**
52 * @param string $filename
53 * @param int $expected Total image area
54 * @dataProvider provideGetImageArea
55 * @covers \GIFHandler::getImageArea
56 */
57 public function testGetImageArea( $filename, $expected ) {
58 $file = $this->dataFile( $filename, 'image/gif' );
59 $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() );
60 $this->assertEquals( $expected, $actual );
61 }
62
63 public static function provideGetImageArea() {
64 return [
65 [ 'animated.gif', 5400 ],
66 [ 'nonanimated.gif', 1350 ],
67 ];
68 }
69
70 /**
71 * @param string $metadata Serialized metadata
72 * @param int $expected One of the class constants of GIFHandler
73 * @dataProvider provideIsFileMetadataValid
74 * @covers \GIFHandler::isFileMetadataValid
75 */
76 public function testIsFileMetadataValid( $metadata, $expected ) {
77 $file = $this->getMockFileWithMetadata( $metadata );
78 $actual = $this->handler->isFileMetadataValid( $file );
79 $this->assertEquals( $expected, $actual );
80 }
81
82 public static function provideIsFileMetadataValid() {
83 return [
84 [ '0', GIFHandler::METADATA_GOOD ],
85 [ '', GIFHandler::METADATA_BAD ],
86 [ 'a:0:{}', GIFHandler::METADATA_BAD ],
87 [ 'Something invalid!', GIFHandler::METADATA_BAD ],
88 [
89 'a:4:{s:10:"frameCount";i:1;s:6:"looped";b:0;s:8:"duration";d:0.1000000000000000055511151231257827021181583404541015625;s:8:"metadata";a:2:{s:14:"GIFFileComment";a:1:{i:0;s:35:"GIF test file ⁕ Created with GIMP";}s:15:"_MW_GIF_VERSION";i:1;}}',
90 GIFHandler::METADATA_GOOD
91 ],
92 ];
93 // phpcs:enable
94 }
95
96 /**
97 * @param string $filename
98 * @param array $expected Unserialized metadata
99 * @dataProvider provideGetSizeAndMetadata
100 * @covers \GIFHandler::getSizeAndMetadata
101 */
102 public function testGetSizeAndMetadata( $filename, $expected ) {
103 $file = $this->dataFile( $filename, 'image/gif' );
104 $actual = $this->handler->getSizeAndMetadata( $file, "$this->filePath/$filename" );
105 $this->assertEquals( $expected, $actual );
106 }
107
108 public static function provideGetSizeAndMetadata() {
109 return [
110 [
111 'nonanimated.gif',
112 [
113 'width' => 45,
114 'height' => 30,
115 'bits' => 1,
116 'metadata' => [
117 'frameCount' => 1,
118 'looped' => false,
119 'duration' => 0.1,
120 'metadata' => [
121 'GIFFileComment' => [ 'GIF test file ⁕ Created with GIMP' ],
122 '_MW_GIF_VERSION' => 1,
123 ],
124 ],
125 ]
126 ],
127 [
128 'animated-xmp.gif',
129 [
130 'width' => 45,
131 'height' => 30,
132 'bits' => 1,
133 'metadata' => [
134 'frameCount' => 4,
135 'looped' => true,
136 'duration' => 2.4,
137 'metadata' => [
138 'Artist' => 'Bawolff',
139 'ImageDescription' => [
140 'x-default' => 'A file to test GIF',
141 '_type' => 'lang',
142 ],
143 'SublocationDest' => 'The interwebs',
144 'GIFFileComment' => [
145 0 => 'GIƒ·test·file',
146 ],
147 '_MW_GIF_VERSION' => 1,
148 ],
149 ],
150 ],
151
152 ],
153 ];
154 // phpcs:enable
155 }
156
157 /**
158 * @param string $filename
159 * @param string $expected Serialized array
160 * @dataProvider provideGetIndependentMetaArray
161 * @covers \GIFHandler::getCommonMetaArray
162 */
163 public function testGetIndependentMetaArray( $filename, $expected ) {
164 $file = $this->dataFile( $filename, 'image/gif' );
165 $actual = $this->handler->getCommonMetaArray( $file );
166 $this->assertEquals( $expected, $actual );
167 }
168
169 public static function provideGetIndependentMetaArray() {
170 return [
171 [ 'nonanimated.gif', [
172 'GIFFileComment' => [
173 'GIF test file ⁕ Created with GIMP',
174 ],
175 ] ],
176 [ 'animated-xmp.gif',
177 [
178 'Artist' => 'Bawolff',
179 'ImageDescription' => [
180 'x-default' => 'A file to test GIF',
181 '_type' => 'lang',
182 ],
183 'SublocationDest' => 'The interwebs',
184 'GIFFileComment' =>
185 [
186 'GIƒ·test·file',
187 ],
188 ]
189 ],
190 ];
191 }
192
193 /**
194 * @param string $filename
195 * @param float $expectedLength
196 * @dataProvider provideGetLength
197 * @covers \GIFHandler::getLength
198 */
199 public function testGetLength( $filename, $expectedLength ) {
200 $file = $this->dataFile( $filename, 'image/gif' );
201 $actualLength = $file->getLength();
202 $this->assertEqualsWithDelta( $expectedLength, $actualLength, 0.00001 );
203 }
204
205 public static function provideGetLength() {
206 return [
207 [ 'animated.gif', 2.4 ],
208 [ 'animated-xmp.gif', 2.4 ],
209 [ 'nonanimated', 0.0 ],
210 [ 'Bishzilla_blink.gif', 1.4 ],
211 ];
212 }
213 }