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