PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/phase3/tests/phpunit/includes/media/XMPTest.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 154 lines | 106 code | 18 blank | 30 comment | 3 complexity | 03e1a8ff1422899e7540e2fe3d5d8729 MD5 | raw file
  1. <?php
  2. class XMPTest extends MediaWikiTestCase {
  3. /**
  4. * Put XMP in, compare what comes out...
  5. *
  6. * @param $xmp String the actual xml data.
  7. * @param $expected Array expected result of parsing the xmp.
  8. * @param $info String Short sentence on what's being tested.
  9. *
  10. * @dataProvider dataXMPParse
  11. */
  12. public function testXMPParse( $xmp, $expected, $info ) {
  13. if ( !function_exists( 'xml_parser_create_ns' ) ) {
  14. $this->markIncomplete( 'Requires libxml to do XMP parsing' );
  15. }
  16. if ( !is_string( $xmp ) || !is_array( $expected ) ) {
  17. throw new Exception( "Invalid data provided to " . __METHOD__ );
  18. }
  19. $reader = new XMPReader;
  20. $reader->parse( $xmp );
  21. $this->assertEquals( $expected, $reader->getResults(), $info );
  22. }
  23. public function dataXMPParse() {
  24. $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/' ;
  25. $data = array();
  26. // $xmpFiles format: array of arrays with first arg file base name,
  27. // with the actual file having .xmp on the end for the xmp
  28. // and .result.php on the end for a php file containing the result
  29. // array. Second argument is some info on what's being tested.
  30. $xmpFiles = array(
  31. array( '1', 'parseType=Resource test' ),
  32. array( '2', 'Structure with mixed attribute and element props' ),
  33. array( '3', 'Extra qualifiers (that should be ignored)' ),
  34. array( '3-invalid', 'Test ignoring qualifiers that look like normal props' ),
  35. array( '4', 'Flash as qualifier' ),
  36. array( '5', 'Flash as qualifier 2' ),
  37. array( '6', 'Multiple rdf:Description' ),
  38. array( '7', 'Generic test of several property types' ),
  39. array( 'flash', 'Test of Flash property' ),
  40. array( 'invalid-child-not-struct', 'Test child props not in struct or ignored' ),
  41. array( 'no-recognized-props', 'Test namespace and no recognized props' ),
  42. array( 'no-namespace', 'Test non-namespaced attributes are ignored' ),
  43. array( 'bag-for-seq', "Allow bag's instead of seq's. (bug 27105)" ),
  44. array( 'utf16BE', 'UTF-16BE encoding' ),
  45. array( 'utf16LE', 'UTF-16LE encoding' ),
  46. array( 'utf32BE', 'UTF-32BE encoding' ),
  47. array( 'utf32LE', 'UTF-32LE encoding' ),
  48. array( 'xmpExt', 'Extended XMP missing second part' ),
  49. );
  50. foreach( $xmpFiles as $file ) {
  51. $xmp = file_get_contents( $xmpPath . $file[0] . '.xmp' );
  52. // I'm not sure if this is the best way to handle getting the
  53. // result array, but it seems kind of big to put directly in the test
  54. // file.
  55. $result = null;
  56. include( $xmpPath . $file[0] . '.result.php' );
  57. $data[] = array( $xmp, $result, '[' . $file[0] . '.xmp] ' . $file[1] );
  58. }
  59. return $data;
  60. }
  61. /** Test ExtendedXMP block support. (Used when the XMP has to be split
  62. * over multiple jpeg segments, due to 64k size limit on jpeg segments.
  63. *
  64. * @todo This is based on what the standard says. Need to find a real
  65. * world example file to double check the support for this is right.
  66. */
  67. function testExtendedXMP() {
  68. $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/';
  69. $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
  70. $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
  71. $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
  72. $length = pack( 'N', strlen( $extendedXMP ) );
  73. $offset = pack( 'N', 0 );
  74. $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
  75. $reader = new XMPReader();
  76. $reader->parse( $standardXMP );
  77. $reader->parseExtended( $extendedPacket );
  78. $actual = $reader->getResults();
  79. $expected = array( 'xmp-exif' =>
  80. array(
  81. 'DigitalZoomRatio' => '0/10',
  82. 'Flash' => 9,
  83. 'FNumber' => '2/10',
  84. )
  85. );
  86. $this->assertEquals( $expected, $actual );
  87. }
  88. /**
  89. * This test has an extended XMP block with a wrong guid (md5sum)
  90. * and thus should only return the StandardXMP, not the ExtendedXMP.
  91. */
  92. function testExtendedXMPWithWrongGUID() {
  93. $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/';
  94. $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
  95. $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
  96. $md5sum = '28C74E0AC2D796886759006FBE2E57B9'; // Note last digit.
  97. $length = pack( 'N', strlen( $extendedXMP ) );
  98. $offset = pack( 'N', 0 );
  99. $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
  100. $reader = new XMPReader();
  101. $reader->parse( $standardXMP );
  102. $reader->parseExtended( $extendedPacket );
  103. $actual = $reader->getResults();
  104. $expected = array( 'xmp-exif' =>
  105. array(
  106. 'DigitalZoomRatio' => '0/10',
  107. 'Flash' => 9,
  108. )
  109. );
  110. $this->assertEquals( $expected, $actual );
  111. }
  112. /**
  113. * Have a high offset to simulate a missing packet,
  114. * which should cause it to ignore the ExtendedXMP packet.
  115. */
  116. function testExtendedXMPMissingPacket() {
  117. $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/';
  118. $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
  119. $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
  120. $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
  121. $length = pack( 'N', strlen( $extendedXMP ) );
  122. $offset = pack( 'N', 2048 );
  123. $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
  124. $reader = new XMPReader();
  125. $reader->parse( $standardXMP );
  126. $reader->parseExtended( $extendedPacket );
  127. $actual = $reader->getResults();
  128. $expected = array( 'xmp-exif' =>
  129. array(
  130. 'DigitalZoomRatio' => '0/10',
  131. 'Flash' => 9,
  132. )
  133. );
  134. $this->assertEquals( $expected, $actual );
  135. }
  136. }