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