Merge "Make unused variable optional in ChangesList::insertDiffHist"
[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 array( '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;}}', GIFHandler::METADATA_GOOD ),
82 // @codingStandardsIgnoreEnd
83 ];
84 }
85
86 /**
87 * @param string $filename
88 * @param string $expected Serialized array
89 * @dataProvider provideGetMetadata
90 * @covers GIFHandler::getMetadata
91 */
92 public function testGetMetadata( $filename, $expected ) {
93 $file = $this->dataFile( $filename, 'image/gif' );
94 $actual = $this->handler->getMetadata( $file, "$this->filePath/$filename" );
95 $this->assertEquals( unserialize( $expected ), unserialize( $actual ) );
96 }
97
98 public static function provideGetMetadata() {
99 return [
100 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
101 array( 'nonanimated.gif', '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;}}' ),
102 array( 'animated-xmp.gif', '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;}}' ),
103 // @codingStandardsIgnoreEnd
104 ];
105 }
106
107 /**
108 * @param string $filename
109 * @param string $expected Serialized array
110 * @dataProvider provideGetIndependentMetaArray
111 * @covers GIFHandler::getCommonMetaArray
112 */
113 public function testGetIndependentMetaArray( $filename, $expected ) {
114 $file = $this->dataFile( $filename, 'image/gif' );
115 $actual = $this->handler->getCommonMetaArray( $file );
116 $this->assertEquals( $expected, $actual );
117 }
118
119 public static function provideGetIndependentMetaArray() {
120 return [
121 [ 'nonanimated.gif', [
122 'GIFFileComment' => [
123 'GIF test file ⁕ Created with GIMP',
124 ],
125 ] ],
126 [ 'animated-xmp.gif',
127 [
128 'Artist' => 'Bawolff',
129 'ImageDescription' => [
130 'x-default' => 'A file to test GIF',
131 '_type' => 'lang',
132 ],
133 'SublocationDest' => 'The interwebs',
134 'GIFFileComment' =>
135 [
136 'GIƒ·test·file',
137 ],
138 ]
139 ],
140 ];
141 }
142
143 /**
144 * @param $filename string
145 * @param $expectedLength float
146 * @dataProvider provideGetLength
147 */
148 public function testGetLength( $filename, $expectedLength ) {
149 $file = $this->dataFile( $filename, 'image/gif' );
150 $actualLength = $file->getLength();
151 $this->assertEquals( $expectedLength, $actualLength, '', 0.00001 );
152 }
153
154 public function provideGetLength() {
155 return [
156 [ 'animated.gif', 2.4 ],
157 [ 'animated-xmp.gif', 2.4 ],
158 [ 'nonanimated', 0.0 ],
159 [ 'Bishzilla_blink.gif', 1.4 ],
160 ];
161 }
162 }