wiki.techinc.nl/tests/phpunit/includes/media/BitmapMetadataHandlerTest.php

164 lines
4.8 KiB
PHP
Raw Normal View History

<?php
class BitmapMetadataHandlerTest extends MediaWikiTestCase {
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
protected function setUp() {
parent::setUp();
$this->setMwGlobals( 'wgShowEXIF', false );
$this->filePath = __DIR__ . '/../../data/media/';
}
/**
* Test if having conflicting metadata values from different
* types of metadata, that the right one takes precedence.
*
* Basically the file has IPTC and XMP metadata, the
* IPTC should override the XMP, except for the multilingual
* translation (to en) where XMP should win.
* @covers BitmapMetadataHandler::Jpeg
*/
public function testMultilingualCascade() {
$this->checkPHPExtension( 'exif' );
$this->checkPHPExtension( 'xml' );
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
$this->setMwGlobals( 'wgShowEXIF', true );
$meta = BitmapMetadataHandler::Jpeg( $this->filePath .
'/Xmp-exif-multilingual_test.jpg' );
$expected = array(
'x-default' => 'right(iptc)',
'en' => 'right translation',
'_type' => 'lang'
);
$this->assertArrayHasKey( 'ImageDescription', $meta,
'Did not extract any ImageDescription info?!' );
$this->assertEquals( $expected, $meta['ImageDescription'] );
}
/**
* Test for jpeg comments are being handled by
* BitmapMetadataHandler correctly.
*
* There's more extensive tests of comment extraction in
* JpegMetadataExtractorTests.php
* @covers BitmapMetadataHandler::Jpeg
*/
public function testJpegComment() {
$meta = BitmapMetadataHandler::Jpeg( $this->filePath .
'jpeg-comment-utf.jpg' );
$this->assertEquals( 'UTF-8 JPEG Comment — ¼',
$meta['JPEGFileComment'][0] );
}
/**
* Make sure a bad iptc block doesn't stop the other metadata
* from being extracted.
* @covers BitmapMetadataHandler::Jpeg
*/
public function testBadIPTC() {
$meta = BitmapMetadataHandler::Jpeg( $this->filePath .
'iptc-invalid-psir.jpg' );
$this->assertEquals( 'Created with GIMP', $meta['JPEGFileComment'][0] );
}
/**
* @covers BitmapMetadataHandler::Jpeg
*/
public function testIPTCDates() {
$meta = BitmapMetadataHandler::Jpeg( $this->filePath .
'iptc-timetest.jpg' );
$this->assertEquals( '2020:07:14 01:36:05', $meta['DateTimeDigitized'] );
$this->assertEquals( '1997:03:02 00:01:02', $meta['DateTimeOriginal'] );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
/**
* File has an invalid time (+ one valid but really weird time)
* that shouldn't be included
* @covers BitmapMetadataHandler::Jpeg
*/
public function testIPTCDatesInvalid() {
$meta = BitmapMetadataHandler::Jpeg( $this->filePath .
'iptc-timetest-invalid.jpg' );
$this->assertEquals( '1845:03:02 00:01:02', $meta['DateTimeOriginal'] );
$this->assertFalse( isset( $meta['DateTimeDigitized'] ) );
}
/**
* XMP data should take priority over iptc data
* when hash has been updated, but not when
* the hash is wrong.
* @covers BitmapMetadataHandler::addMetadata
* @covers BitmapMetadataHandler::getMetadataArray
*/
public function testMerging() {
$merger = new BitmapMetadataHandler();
$merger->addMetadata( array( 'foo' => 'xmp' ), 'xmp-general' );
$merger->addMetadata( array( 'bar' => 'xmp' ), 'xmp-general' );
$merger->addMetadata( array( 'baz' => 'xmp' ), 'xmp-general' );
$merger->addMetadata( array( 'fred' => 'xmp' ), 'xmp-general' );
$merger->addMetadata( array( 'foo' => 'iptc (hash)' ), 'iptc-good-hash' );
$merger->addMetadata( array( 'bar' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
$merger->addMetadata( array( 'baz' => 'iptc (bad hash)' ), 'iptc-bad-hash' );
$merger->addMetadata( array( 'fred' => 'iptc (no hash)' ), 'iptc-no-hash' );
$merger->addMetadata( array( 'baz' => 'exif' ), 'exif' );
$actual = $merger->getMetadataArray();
$expected = array(
'foo' => 'xmp',
'bar' => 'iptc (bad hash)',
'baz' => 'exif',
'fred' => 'xmp',
);
$this->assertEquals( $expected, $actual );
}
/**
* @covers BitmapMetadataHandler::png
*/
public function testPNGXMP() {
if ( !extension_loaded( 'xml' ) ) {
$this->markTestSkipped( "This test needs the xml extension." );
}
$handler = new BitmapMetadataHandler();
$result = $handler->png( $this->filePath . 'xmp.png' );
$expected = array(
'frameCount' => 0,
'loopCount' => 1,
'duration' => 0,
'bitDepth' => 1,
'colorType' => 'index-coloured',
'metadata' => array(
'SerialNumber' => '123456789',
'_MW_PNG_VERSION' => 1,
),
);
$this->assertEquals( $expected, $result );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
/**
* @covers BitmapMetadataHandler::png
*/
public function testPNGNative() {
$handler = new BitmapMetadataHandler();
$result = $handler->png( $this->filePath . 'Png-native-test.png' );
$expected = 'http://example.com/url';
$this->assertEquals( $expected, $result['metadata']['Identifier']['x-default'] );
}
Clean and repair many phpunit tests (+ fix implied configuration) This commit depends on the introduction of MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4. Various tests already set their globals, but forgot to restore them afterwards, or forgot to call the parent setUp, tearDown... Either way they won't have to anymore with setMwGlobals. Consistent use of function characteristics: * protected function setUp * protected function tearDown * public static function (provide..) (Matching the function signature with PHPUnit/Framework/TestCase.php) Replaces: * public function (setUp|tearDown)\( * protected function $1( * \tfunction (setUp|tearDown)\( * \tprotected function $1( * \tfunction (data|provide)\( * \tpublic static function $1\( Also renamed a few "data#", "provider#" and "provides#" functions to "provide#" for consistency. This also removes confusion where the /media tests had a few private methods called dataFile(), which were sometimes expected to be data providers. Fixes: TimestampTest often failed due to a previous test setting a different language (it tests "1 hour ago" so need to make sure it is set to English). MWNamespaceTest became a lot cleaner now that it executes with a known context. Though the now-redundant code that was removed didn't work anyway because wgContentNamespaces isn't keyed by namespace id, it had them was values... FileBackendTest: * Fixed: "PHP Fatal: Using $this when not in object context" HttpTest * Added comment about: "PHP Fatal: Call to protected MWHttpRequest::__construct()" (too much unrelated code to fix in this commit) ExternalStoreTest * Add an assertTrue as well, without it the test is useless because regardless of whether wgExternalStores is true or false it only uses it if it is an array. Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-08 10:56:20 +00:00
/**
* @covers BitmapMetadataHandler::getTiffByteOrder
*/
public function testTiffByteOrder() {
$handler = new BitmapMetadataHandler();
$res = $handler->getTiffByteOrder( $this->filePath . 'test.tiff' );
$this->assertEquals( 'LE', $res );
}
}