Merge "DumpUploads: output local path instead of mwstore path"
[lhc/web/wiklou.git] / tests / phpunit / includes / media / GIFTest.php
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() {
12 parent::setUp();
13
14 $this->handler = new GIFHandler();
15 }
16
17 /**
18 * @covers GIFHandler::getMetadata
19 */
20 public function testInvalidFile() {
21 $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
22 $this->assertEquals( GIFHandler::BROKEN_FILE, $res );
23 }
24
25 /**
26 * @param string $filename Basename of the file to check
27 * @param bool $expected Expected result.
28 * @dataProvider provideIsAnimated
29 * @covers GIFHandler::isAnimatedImage
30 */
31 public function testIsAnimanted( $filename, $expected ) {
32 $file = $this->dataFile( $filename, 'image/gif' );
33 $actual = $this->handler->isAnimatedImage( $file );
34 $this->assertEquals( $expected, $actual );
35 }
36
37 public static function provideIsAnimated() {
38 return [
39 [ 'animated.gif', true ],
40 [ 'nonanimated.gif', false ],
41 ];
42 }
43
44 /**
45 * @param string $filename
46 * @param int $expected Total image area
47 * @dataProvider provideGetImageArea
48 * @covers GIFHandler::getImageArea
49 */
50 public function testGetImageArea( $filename, $expected ) {
51 $file = $this->dataFile( $filename, 'image/gif' );
52 $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() );
53 $this->assertEquals( $expected, $actual );
54 }
55
56 public static function provideGetImageArea() {
57 return [
58 [ 'animated.gif', 5400 ],
59 [ 'nonanimated.gif', 1350 ],
60 ];
61 }
62
63 /**
64 * @param string $metadata Serialized metadata
65 * @param int $expected One of the class constants of GIFHandler
66 * @dataProvider provideIsMetadataValid
67 * @covers GIFHandler::isMetadataValid
68 */
69 public function testIsMetadataValid( $metadata, $expected ) {
70 $actual = $this->handler->isMetadataValid( null, $metadata );
71 $this->assertEquals( $expected, $actual );
72 }
73
74 public static function provideIsMetadataValid() {
75 return [
76 [ GIFHandler::BROKEN_FILE, GIFHandler::METADATA_GOOD ],
77 [ '', GIFHandler::METADATA_BAD ],
78 [ null, GIFHandler::METADATA_BAD ],
79 [ 'Something invalid!', GIFHandler::METADATA_BAD ],
80 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
81 [
82 '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;}}',
83 GIFHandler::METADATA_GOOD
84 ],
85 // @codingStandardsIgnoreEnd
86 ];
87 }
88
89 /**
90 * @param string $filename
91 * @param string $expected Serialized array
92 * @dataProvider provideGetMetadata
93 * @covers GIFHandler::getMetadata
94 */
95 public function testGetMetadata( $filename, $expected ) {
96 $file = $this->dataFile( $filename, 'image/gif' );
97 $actual = $this->handler->getMetadata( $file, "$this->filePath/$filename" );
98 $this->assertEquals( unserialize( $expected ), unserialize( $actual ) );
99 }
100
101 public static function provideGetMetadata() {
102 return [
103 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
104 [
105 'nonanimated.gif',
106 '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;}}'
107 ],
108 [
109 'animated-xmp.gif',
110 'a:4:{s:10:"frameCount";i:4;s:6:"looped";b:1;s:8:"duration";d:2.399999999999999911182158029987476766109466552734375;s:8:"metadata";a:5:{s:6:"Artist";s:7:"Bawolff";s:16:"ImageDescription";a:2:{s:9:"x-default";s:18:"A file to test GIF";s:5:"_type";s:4:"lang";}s:15:"SublocationDest";s:13:"The interwebs";s:14:"GIFFileComment";a:1:{i:0;s:16:"GIƒ·test·file";}s:15:"_MW_GIF_VERSION";i:1;}}'
111 ],
112 // @codingStandardsIgnoreEnd
113 ];
114 }
115
116 /**
117 * @param string $filename
118 * @param string $expected Serialized array
119 * @dataProvider provideGetIndependentMetaArray
120 * @covers GIFHandler::getCommonMetaArray
121 */
122 public function testGetIndependentMetaArray( $filename, $expected ) {
123 $file = $this->dataFile( $filename, 'image/gif' );
124 $actual = $this->handler->getCommonMetaArray( $file );
125 $this->assertEquals( $expected, $actual );
126 }
127
128 public static function provideGetIndependentMetaArray() {
129 return [
130 [ 'nonanimated.gif', [
131 'GIFFileComment' => [
132 'GIF test file ⁕ Created with GIMP',
133 ],
134 ] ],
135 [ 'animated-xmp.gif',
136 [
137 'Artist' => 'Bawolff',
138 'ImageDescription' => [
139 'x-default' => 'A file to test GIF',
140 '_type' => 'lang',
141 ],
142 'SublocationDest' => 'The interwebs',
143 'GIFFileComment' =>
144 [
145 'GIƒ·test·file',
146 ],
147 ]
148 ],
149 ];
150 }
151
152 /**
153 * @param string $filename
154 * @param float $expectedLength
155 * @dataProvider provideGetLength
156 */
157 public function testGetLength( $filename, $expectedLength ) {
158 $file = $this->dataFile( $filename, 'image/gif' );
159 $actualLength = $file->getLength();
160 $this->assertEquals( $expectedLength, $actualLength, '', 0.00001 );
161 }
162
163 public function provideGetLength() {
164 return [
165 [ 'animated.gif', 2.4 ],
166 [ 'animated-xmp.gif', 2.4 ],
167 [ 'nonanimated', 0.0 ],
168 [ 'Bishzilla_blink.gif', 1.4 ],
169 ];
170 }
171 }