Merge changes I9d2b148e,Iccf6ea81
[lhc/web/wiklou.git] / tests / phpunit / includes / media / PNGTest.php
1 <?php
2 class PNGHandlerTest extends MediaWikiTestCase {
3
4 protected function setUp() {
5 parent::setUp();
6
7 $this->filePath = __DIR__ . '/../../data/media';
8 $this->backend = new FSFileBackend( array(
9 'name' => 'localtesting',
10 'lockManager' => 'nullLockManager',
11 'containerPaths' => array( 'data' => $this->filePath )
12 ) );
13 $this->repo = new FSRepo( array(
14 'name' => 'temp',
15 'url' => 'http://localhost/thumbtest',
16 'backend' => $this->backend
17 ) );
18 $this->handler = new PNGHandler();
19 }
20
21 public function testInvalidFile() {
22 $res = $this->handler->getMetadata( null, $this->filePath . '/README' );
23 $this->assertEquals( PNGHandler::BROKEN_FILE, $res );
24 }
25 /**
26 * @param $filename String basename of the file to check
27 * @param $expected boolean Expected result.
28 * @dataProvider provideIsAnimated
29 */
30 public function testIsAnimanted( $filename, $expected ) {
31 $file = $this->dataFile( $filename, 'image/png' );
32 $actual = $this->handler->isAnimatedImage( $file );
33 $this->assertEquals( $expected, $actual );
34 }
35 public static function provideIsAnimated() {
36 return array(
37 array( 'Animated_PNG_example_bouncing_beach_ball.png', true ),
38 array( '1bit-png.png', false ),
39 );
40 }
41
42 /**
43 * @param $filename String
44 * @param $expected Integer Total image area
45 * @dataProvider provideGetImageArea
46 */
47 public function testGetImageArea( $filename, $expected ) {
48 $file = $this->dataFile($filename, 'image/png' );
49 $actual = $this->handler->getImageArea( $file, $file->getWidth(), $file->getHeight() );
50 $this->assertEquals( $expected, $actual );
51 }
52 public static function provideGetImageArea() {
53 return array(
54 array( '1bit-png.png', 2500 ),
55 array( 'greyscale-png.png', 2500 ),
56 array( 'Png-native-test.png', 126000 ),
57 array( 'Animated_PNG_example_bouncing_beach_ball.png', 10000 ),
58 );
59 }
60
61 /**
62 * @param $metadata String Serialized metadata
63 * @param $expected Integer One of the class constants of PNGHandler
64 * @dataProvider provideIsMetadataValid
65 */
66 public function testIsMetadataValid( $metadata, $expected ) {
67 $actual = $this->handler->isMetadataValid( null, $metadata );
68 $this->assertEquals( $expected, $actual );
69 }
70 public static function provideIsMetadataValid() {
71 return array(
72 array( PNGHandler::BROKEN_FILE, PNGHandler::METADATA_GOOD ),
73 array( '', PNGHandler::METADATA_BAD ),
74 array( null, PNGHandler::METADATA_BAD ),
75 array( 'Something invalid!', PNGHandler::METADATA_BAD ),
76 array( 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}', PNGHandler::METADATA_GOOD ),
77 );
78 }
79
80 /**
81 * @param $filename String
82 * @param $expected String Serialized array
83 * @dataProvider provideGetMetadata
84 */
85 public function testGetMetadata( $filename, $expected ) {
86 $file = $this->dataFile( $filename, 'image/png' );
87 $actual = $this->handler->getMetadata( $file, "$this->filePath/$filename" );
88 // $this->assertEquals( unserialize( $expected ), unserialize( $actual ) );
89 $this->assertEquals( ( $expected ), ( $actual ) );
90 }
91 public static function provideGetMetadata() {
92 return array(
93 array( 'rgb-na-png.png', 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:8;s:9:"colorType";s:10:"truecolour";s:8:"metadata";a:1:{s:15:"_MW_PNG_VERSION";i:1;}}' ),
94 array( 'xmp.png', 'a:6:{s:10:"frameCount";i:0;s:9:"loopCount";i:1;s:8:"duration";d:0;s:8:"bitDepth";i:1;s:9:"colorType";s:14:"index-coloured";s:8:"metadata";a:2:{s:12:"SerialNumber";s:9:"123456789";s:15:"_MW_PNG_VERSION";i:1;}}' ),
95 );
96 }
97
98 private function dataFile( $name, $type ) {
99 return new UnregisteredLocalFile( false, $this->repo,
100 "mwstore://localtesting/data/$name", $type );
101 }
102 }