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