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