4350cbbad3b39fc52e632e4faa4a9522eb334d7e
[lhc/web/wiklou.git] / tests / phpunit / includes / media / GIFTest.php
1 <?php
2 class GIFHandlerTest extends MediaWikiTestCase {
3
4 /** @var FSFileBackend */
5 protected $backend;
6 /** @var GIFHandler */
7 protected $handler;
8 /** @var FSRepo */
9 protected $repo;
10 /** @var string */
11 protected $filePath;
12
13 protected function setUp() {
14 parent::setUp();
15
16 $this->filePath = __DIR__ . '/../../data/media';
17 $this->backend = new FSFileBackend( array(
18 'name' => 'localtesting',
19 'lockManager' => 'nullLockManager',
20 'containerPaths' => array( 'data' => $this->filePath )
21 ) );
22 $this->repo = new FSRepo( array(
23 'name' => 'temp',
24 'url' => 'http://localhost/thumbtest',
25 'backend' => $this->backend
26 ) );
27 $this->handler = new GIFHandler();
28 }
29
30 /**
31 * @covers GIFHandler::getMetadata
32 */
33 public function testInvalidFile() {
34 $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
35 $this->assertEquals( GIFHandler::BROKEN_FILE, $res );
36 }
37
38 /**
39 * @param $filename String basename of the file to check
40 * @param $expected boolean Expected result.
41 * @dataProvider provideIsAnimated
42 * @covers GIFHandler::isAnimatedImage
43 */
44 public function testIsAnimanted( $filename, $expected ) {
45 $file = $this->dataFile( $filename, 'image/gif' );
46 $actual = $this->handler->isAnimatedImage( $file );
47 $this->assertEquals( $expected, $actual );
48 }
49
50 public static function provideIsAnimated() {
51 return array(
52 array( 'animated.gif', true ),
53 array( 'nonanimated.gif', false ),
54 );
55 }
56
57 /**
58 * @param $filename String
59 * @param $expected Integer Total image area
60 * @dataProvider provideGetImageArea
61 * @covers GIFHandler::getImageArea
62 */
63 public function testGetImageArea( $filename, $expected ) {
64 $file = $this->dataFile( $filename, 'image/gif' );
65 $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() );
66 $this->assertEquals( $expected, $actual );
67 }
68
69 public static function provideGetImageArea() {
70 return array(
71 array( 'animated.gif', 5400 ),
72 array( 'nonanimated.gif', 1350 ),
73 );
74 }
75
76 /**
77 * @param $metadata String Serialized metadata
78 * @param $expected Integer One of the class constants of GIFHandler
79 * @dataProvider provideIsMetadataValid
80 * @covers GIFHandler::isMetadataValid
81 */
82 public function testIsMetadataValid( $metadata, $expected ) {
83 $actual = $this->handler->isMetadataValid( null, $metadata );
84 $this->assertEquals( $expected, $actual );
85 }
86
87 public static function provideIsMetadataValid() {
88 return array(
89 array( GIFHandler::BROKEN_FILE, GIFHandler::METADATA_GOOD ),
90 array( '', GIFHandler::METADATA_BAD ),
91 array( null, GIFHandler::METADATA_BAD ),
92 array( 'Something invalid!', GIFHandler::METADATA_BAD ),
93 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 ),
94 );
95 }
96
97 /**
98 * @param $filename String
99 * @param $expected String Serialized array
100 * @dataProvider provideGetMetadata
101 * @covers GIFHandler::getMetadata
102 */
103 public function testGetMetadata( $filename, $expected ) {
104 $file = $this->dataFile( $filename, 'image/gif' );
105 $actual = $this->handler->getMetadata( $file, "$this->filePath/$filename" );
106 $this->assertEquals( unserialize( $expected ), unserialize( $actual ) );
107 }
108
109 public static function provideGetMetadata() {
110 return array(
111 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;}}' ),
112 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;}}' ),
113 );
114 }
115
116 /**
117 * @param $filename String
118 * @param $expected String 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 function provideGetIndependentMetaArray() {
129 return array(
130 array( 'nonanimated.gif', array(
131 'GIFFileComment' => array(
132 'GIF test file ⁕ Created with GIMP',
133 ),
134 ) ),
135 array( 'animated-xmp.gif',
136 array(
137 'Artist' => 'Bawolff',
138 'ImageDescription' => array(
139 'x-default' => 'A file to test GIF',
140 '_type' => 'lang',
141 ),
142 'SublocationDest' => 'The interwebs',
143 'GIFFileComment' =>
144 array(
145 'GIƒ·test·file',
146 ),
147 )
148 ),
149 );
150 }
151
152 private function dataFile( $name, $type ) {
153 return new UnregisteredLocalFile( false, $this->repo,
154 "mwstore://localtesting/data/$name", $type );
155 }
156 }