Merge "(bug 43467) Add invert selection for ns in Special:Newpages"
[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 return $data;
69 }
70
71 /** Test ExtendedXMP block support. (Used when the XMP has to be split
72 * over multiple jpeg segments, due to 64k size limit on jpeg segments.
73 *
74 * @todo This is based on what the standard says. Need to find a real
75 * world example file to double check the support for this is right.
76 */
77 function testExtendedXMP() {
78 $xmpPath = __DIR__ . '/../../data/xmp/';
79 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
80 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
81
82 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
83 $length = pack( 'N', strlen( $extendedXMP ) );
84 $offset = pack( 'N', 0 );
85 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
86
87 $reader = new XMPReader();
88 $reader->parse( $standardXMP );
89 $reader->parseExtended( $extendedPacket );
90 $actual = $reader->getResults();
91
92 $expected = array(
93 'xmp-exif' => array(
94 'DigitalZoomRatio' => '0/10',
95 'Flash' => 9,
96 'FNumber' => '2/10',
97 )
98 );
99
100 $this->assertEquals( $expected, $actual );
101 }
102
103 /**
104 * This test has an extended XMP block with a wrong guid (md5sum)
105 * and thus should only return the StandardXMP, not the ExtendedXMP.
106 */
107 function testExtendedXMPWithWrongGUID() {
108 $xmpPath = __DIR__ . '/../../data/xmp/';
109 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
110 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
111
112 $md5sum = '28C74E0AC2D796886759006FBE2E57B9'; // Note last digit.
113 $length = pack( 'N', strlen( $extendedXMP ) );
114 $offset = pack( 'N', 0 );
115 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
116
117 $reader = new XMPReader();
118 $reader->parse( $standardXMP );
119 $reader->parseExtended( $extendedPacket );
120 $actual = $reader->getResults();
121
122 $expected = array(
123 'xmp-exif' => array(
124 'DigitalZoomRatio' => '0/10',
125 'Flash' => 9,
126 )
127 );
128
129 $this->assertEquals( $expected, $actual );
130 }
131
132 /**
133 * Have a high offset to simulate a missing packet,
134 * which should cause it to ignore the ExtendedXMP packet.
135 */
136 function testExtendedXMPMissingPacket() {
137 $xmpPath = __DIR__ . '/../../data/xmp/';
138 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
139 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
140
141 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
142 $length = pack( 'N', strlen( $extendedXMP ) );
143 $offset = pack( 'N', 2048 );
144 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
145
146 $reader = new XMPReader();
147 $reader->parse( $standardXMP );
148 $reader->parseExtended( $extendedPacket );
149 $actual = $reader->getResults();
150
151 $expected = array(
152 'xmp-exif' => array(
153 'DigitalZoomRatio' => '0/10',
154 'Flash' => 9,
155 )
156 );
157
158 $this->assertEquals( $expected, $actual );
159 }
160
161 }