MediaWiki
REL1_21
|
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 00059 foreach ( $xmpFiles as $file ) { 00060 $xmp = file_get_contents( $xmpPath . $file[0] . '.xmp' ); 00061 // I'm not sure if this is the best way to handle getting the 00062 // result array, but it seems kind of big to put directly in the test 00063 // file. 00064 $result = null; 00065 include( $xmpPath . $file[0] . '.result.php' ); 00066 $data[] = array( $xmp, $result, '[' . $file[0] . '.xmp] ' . $file[1] ); 00067 } 00068 return $data; 00069 } 00070 00077 function testExtendedXMP() { 00078 $xmpPath = __DIR__ . '/../../data/xmp/'; 00079 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' ); 00080 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' ); 00081 00082 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp 00083 $length = pack( 'N', strlen( $extendedXMP ) ); 00084 $offset = pack( 'N', 0 ); 00085 $extendedPacket = $md5sum . $length . $offset . $extendedXMP; 00086 00087 $reader = new XMPReader(); 00088 $reader->parse( $standardXMP ); 00089 $reader->parseExtended( $extendedPacket ); 00090 $actual = $reader->getResults(); 00091 00092 $expected = array( 00093 'xmp-exif' => array( 00094 'DigitalZoomRatio' => '0/10', 00095 'Flash' => 9, 00096 'FNumber' => '2/10', 00097 ) 00098 ); 00099 00100 $this->assertEquals( $expected, $actual ); 00101 } 00102 00107 function testExtendedXMPWithWrongGUID() { 00108 $xmpPath = __DIR__ . '/../../data/xmp/'; 00109 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' ); 00110 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' ); 00111 00112 $md5sum = '28C74E0AC2D796886759006FBE2E57B9'; // Note last digit. 00113 $length = pack( 'N', strlen( $extendedXMP ) ); 00114 $offset = pack( 'N', 0 ); 00115 $extendedPacket = $md5sum . $length . $offset . $extendedXMP; 00116 00117 $reader = new XMPReader(); 00118 $reader->parse( $standardXMP ); 00119 $reader->parseExtended( $extendedPacket ); 00120 $actual = $reader->getResults(); 00121 00122 $expected = array( 00123 'xmp-exif' => array( 00124 'DigitalZoomRatio' => '0/10', 00125 'Flash' => 9, 00126 ) 00127 ); 00128 00129 $this->assertEquals( $expected, $actual ); 00130 } 00131 00136 function testExtendedXMPMissingPacket() { 00137 $xmpPath = __DIR__ . '/../../data/xmp/'; 00138 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' ); 00139 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' ); 00140 00141 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp 00142 $length = pack( 'N', strlen( $extendedXMP ) ); 00143 $offset = pack( 'N', 2048 ); 00144 $extendedPacket = $md5sum . $length . $offset . $extendedXMP; 00145 00146 $reader = new XMPReader(); 00147 $reader->parse( $standardXMP ); 00148 $reader->parseExtended( $extendedPacket ); 00149 $actual = $reader->getResults(); 00150 00151 $expected = array( 00152 'xmp-exif' => array( 00153 'DigitalZoomRatio' => '0/10', 00154 'Flash' => 9, 00155 ) 00156 ); 00157 00158 $this->assertEquals( $expected, $actual ); 00159 } 00160 00161 }