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