MediaWiki  master
XMPTest.php
Go to the documentation of this file.
00001 <?php
00002 class XMPTest extends MediaWikiTestCase {
00003 
00004         protected function setUp() {
00005                 parent::setUp();
00006                 if ( !wfDl( 'xml' ) ) {
00007                         $this->markTestSkipped( 'Requires libxml to do XMP parsing' );
00008                 }
00009         }
00010 
00020         public function testXMPParse( $xmp, $expected, $info ) {
00021                 if ( !is_string( $xmp ) || !is_array( $expected ) ) {
00022                         throw new Exception( "Invalid data provided to " . __METHOD__ );
00023                 }
00024                 $reader = new XMPReader;
00025                 $reader->parse( $xmp );
00026                 $this->assertEquals( $expected, $reader->getResults(), $info, 0.0000000001 );
00027         }
00028 
00029         public static function provideXMPParse() {
00030                 $xmpPath = __DIR__ . '/../../data/xmp/' ;
00031                 $data = array();
00032 
00033                 // $xmpFiles format: array of arrays with first arg file base name,
00034                 // with the actual file having .xmp on the end for the xmp
00035                 // and .result.php on the end for a php file containing the result
00036                 // array. Second argument is some info on what's being tested.
00037                 $xmpFiles = array(
00038                         array( '1', 'parseType=Resource test' ),
00039                         array( '2', 'Structure with mixed attribute and element props' ),
00040                         array( '3', 'Extra qualifiers (that should be ignored)' ),
00041                         array( '3-invalid', 'Test ignoring qualifiers that look like normal props' ),
00042                         array( '4', 'Flash as qualifier' ),
00043                         array( '5', 'Flash as qualifier 2' ),
00044                         array( '6', 'Multiple rdf:Description' ),
00045                         array( '7', 'Generic test of several property types' ),
00046                         array( 'flash', 'Test of Flash property' ),
00047                         array( 'invalid-child-not-struct', 'Test child props not in struct or ignored' ),
00048                         array( 'no-recognized-props', 'Test namespace and no recognized props' ),
00049                         array( 'no-namespace', 'Test non-namespaced attributes are ignored' ),
00050                         array( 'bag-for-seq', "Allow bag's instead of seq's. (bug 27105)" ),
00051                         array( 'utf16BE', 'UTF-16BE encoding' ),
00052                         array( 'utf16LE', 'UTF-16LE encoding' ),
00053                         array( 'utf32BE', 'UTF-32BE encoding' ),
00054                         array( 'utf32LE', 'UTF-32LE encoding' ),
00055                         array( 'xmpExt', 'Extended XMP missing second part' ),
00056                         array( 'gps', 'Handling of exif GPS parameters in XMP' ),
00057                  );
00058                 foreach( $xmpFiles as $file ) {
00059                         $xmp = file_get_contents( $xmpPath . $file[0] . '.xmp' );
00060                         // I'm not sure if this is the best way to handle getting the
00061                         // result array, but it seems kind of big to put directly in the test
00062                         // file.
00063                         $result = null;
00064                         include( $xmpPath . $file[0] . '.result.php' );
00065                         $data[] = array( $xmp, $result, '[' . $file[0] . '.xmp] ' . $file[1] );
00066                 }
00067                 return $data;
00068         }
00069 
00076         function testExtendedXMP() {
00077                 $xmpPath = __DIR__ . '/../../data/xmp/';
00078                 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
00079                 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
00080 
00081                 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
00082                 $length = pack( 'N', strlen( $extendedXMP ) );
00083                 $offset = pack( 'N', 0 );
00084                 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
00085 
00086                 $reader = new XMPReader();
00087                 $reader->parse( $standardXMP );
00088                 $reader->parseExtended( $extendedPacket );
00089                 $actual = $reader->getResults();
00090 
00091                 $expected = array( 'xmp-exif' =>
00092                         array(
00093                                 'DigitalZoomRatio' => '0/10',
00094                                 'Flash' => 9,
00095                                 'FNumber' => '2/10',
00096                         )
00097                 );
00098 
00099                 $this->assertEquals( $expected, $actual );
00100         }
00101 
00106         function testExtendedXMPWithWrongGUID() {
00107                 $xmpPath = __DIR__ . '/../../data/xmp/';
00108                 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
00109                 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
00110 
00111                 $md5sum = '28C74E0AC2D796886759006FBE2E57B9'; // Note last digit.
00112                 $length = pack( 'N', strlen( $extendedXMP ) );
00113                 $offset = pack( 'N', 0 );
00114                 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
00115 
00116                 $reader = new XMPReader();
00117                 $reader->parse( $standardXMP );
00118                 $reader->parseExtended( $extendedPacket );
00119                 $actual = $reader->getResults();
00120 
00121                 $expected = array( 'xmp-exif' =>
00122                         array(
00123                                 'DigitalZoomRatio' => '0/10',
00124                                 'Flash' => 9,
00125                         )
00126                 );
00127 
00128                 $this->assertEquals( $expected, $actual );
00129         }
00134         function testExtendedXMPMissingPacket() {
00135                 $xmpPath = __DIR__ . '/../../data/xmp/';
00136                 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
00137                 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
00138 
00139                 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
00140                 $length = pack( 'N', strlen( $extendedXMP ) );
00141                 $offset = pack( 'N', 2048 );
00142                 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
00143 
00144                 $reader = new XMPReader();
00145                 $reader->parse( $standardXMP );
00146                 $reader->parseExtended( $extendedPacket );
00147                 $actual = $reader->getResults();
00148 
00149                 $expected = array( 'xmp-exif' =>
00150                         array(
00151                                 'DigitalZoomRatio' => '0/10',
00152                                 'Flash' => 9,
00153                         )
00154                 );
00155 
00156                 $this->assertEquals( $expected, $actual );
00157         }
00158 
00159 }