Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / media / GIFTest.php
blobaaa3ac4ded8e62e8749ccdcf6e9ca7127b08f3b6
1 <?php
3 /**
4 * @group Media
5 */
6 class GIFHandlerTest extends MediaWikiMediaTestCase {
8 /** @var GIFHandler */
9 protected $handler;
11 protected function setUp() {
12 parent::setUp();
14 $this->handler = new GIFHandler();
17 /**
18 * @covers GIFHandler::getMetadata
20 public function testInvalidFile() {
21 $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
22 $this->assertEquals( GIFHandler::BROKEN_FILE, $res );
25 /**
26 * @param string $filename Basename of the file to check
27 * @param bool $expected Expected result.
28 * @dataProvider provideIsAnimated
29 * @covers GIFHandler::isAnimatedImage
31 public function testIsAnimanted( $filename, $expected ) {
32 $file = $this->dataFile( $filename, 'image/gif' );
33 $actual = $this->handler->isAnimatedImage( $file );
34 $this->assertEquals( $expected, $actual );
37 public static function provideIsAnimated() {
38 return [
39 [ 'animated.gif', true ],
40 [ 'nonanimated.gif', false ],
44 /**
45 * @param string $filename
46 * @param int $expected Total image area
47 * @dataProvider provideGetImageArea
48 * @covers GIFHandler::getImageArea
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 );
56 public static function provideGetImageArea() {
57 return [
58 [ 'animated.gif', 5400 ],
59 [ 'nonanimated.gif', 1350 ],
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
69 public function testIsMetadataValid( $metadata, $expected ) {
70 $actual = $this->handler->isMetadataValid( null, $metadata );
71 $this->assertEquals( $expected, $actual );
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
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
85 // @codingStandardsIgnoreEnd
89 /**
90 * @param string $filename
91 * @param string $expected Serialized array
92 * @dataProvider provideGetMetadata
93 * @covers GIFHandler::getMetadata
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 ) );
101 public static function provideGetMetadata() {
102 return [
103 // @codingStandardsIgnoreStart Ignore Generic.Files.LineLength.TooLong
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;}}'
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;}}'
112 // @codingStandardsIgnoreEnd
117 * @param string $filename
118 * @param string $expected Serialized array
119 * @dataProvider provideGetIndependentMetaArray
120 * @covers GIFHandler::getCommonMetaArray
122 public function testGetIndependentMetaArray( $filename, $expected ) {
123 $file = $this->dataFile( $filename, 'image/gif' );
124 $actual = $this->handler->getCommonMetaArray( $file );
125 $this->assertEquals( $expected, $actual );
128 public static function provideGetIndependentMetaArray() {
129 return [
130 [ 'nonanimated.gif', [
131 'GIFFileComment' => [
132 'GIF test file ⁕ Created with GIMP',
134 ] ],
135 [ 'animated-xmp.gif',
137 'Artist' => 'Bawolff',
138 'ImageDescription' => [
139 'x-default' => 'A file to test GIF',
140 '_type' => 'lang',
142 'SublocationDest' => 'The interwebs',
143 'GIFFileComment' =>
145 'GIƒ·test·file',
153 * @param string $filename
154 * @param float $expectedLength
155 * @dataProvider provideGetLength
157 public function testGetLength( $filename, $expectedLength ) {
158 $file = $this->dataFile( $filename, 'image/gif' );
159 $actualLength = $file->getLength();
160 $this->assertEquals( $expectedLength, $actualLength, '', 0.00001 );
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 ],