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