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