Fix separated login link so that create account and login are always next to each...
[lhc/web/wiklou.git] / tests / phpunit / includes / media / XMPTest.php
1 <?php
2 class XMPTest extends MediaWikiTestCase {
3
4 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 dataXMPParse
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 );
26 }
27
28 public function dataXMPParse() {
29 $xmpPath = dirname( __FILE__ ) . '/../../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 );
56 foreach( $xmpFiles as $file ) {
57 $xmp = file_get_contents( $xmpPath . $file[0] . '.xmp' );
58 // I'm not sure if this is the best way to handle getting the
59 // result array, but it seems kind of big to put directly in the test
60 // file.
61 $result = null;
62 include( $xmpPath . $file[0] . '.result.php' );
63 $data[] = array( $xmp, $result, '[' . $file[0] . '.xmp] ' . $file[1] );
64 }
65 return $data;
66 }
67
68 /** Test ExtendedXMP block support. (Used when the XMP has to be split
69 * over multiple jpeg segments, due to 64k size limit on jpeg segments.
70 *
71 * @todo This is based on what the standard says. Need to find a real
72 * world example file to double check the support for this is right.
73 */
74 function testExtendedXMP() {
75 $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/';
76 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
77 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
78
79 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
80 $length = pack( 'N', strlen( $extendedXMP ) );
81 $offset = pack( 'N', 0 );
82 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
83
84 $reader = new XMPReader();
85 $reader->parse( $standardXMP );
86 $reader->parseExtended( $extendedPacket );
87 $actual = $reader->getResults();
88
89 $expected = array( 'xmp-exif' =>
90 array(
91 'DigitalZoomRatio' => '0/10',
92 'Flash' => 9,
93 'FNumber' => '2/10',
94 )
95 );
96
97 $this->assertEquals( $expected, $actual );
98 }
99
100 /**
101 * This test has an extended XMP block with a wrong guid (md5sum)
102 * and thus should only return the StandardXMP, not the ExtendedXMP.
103 */
104 function testExtendedXMPWithWrongGUID() {
105 $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/';
106 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
107 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
108
109 $md5sum = '28C74E0AC2D796886759006FBE2E57B9'; // Note last digit.
110 $length = pack( 'N', strlen( $extendedXMP ) );
111 $offset = pack( 'N', 0 );
112 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
113
114 $reader = new XMPReader();
115 $reader->parse( $standardXMP );
116 $reader->parseExtended( $extendedPacket );
117 $actual = $reader->getResults();
118
119 $expected = array( 'xmp-exif' =>
120 array(
121 'DigitalZoomRatio' => '0/10',
122 'Flash' => 9,
123 )
124 );
125
126 $this->assertEquals( $expected, $actual );
127 }
128 /**
129 * Have a high offset to simulate a missing packet,
130 * which should cause it to ignore the ExtendedXMP packet.
131 */
132 function testExtendedXMPMissingPacket() {
133 $xmpPath = dirname( __FILE__ ) . '/../../data/xmp/';
134 $standardXMP = file_get_contents( $xmpPath . 'xmpExt.xmp' );
135 $extendedXMP = file_get_contents( $xmpPath . 'xmpExt2.xmp' );
136
137 $md5sum = '28C74E0AC2D796886759006FBE2E57B7'; // of xmpExt2.xmp
138 $length = pack( 'N', strlen( $extendedXMP ) );
139 $offset = pack( 'N', 2048 );
140 $extendedPacket = $md5sum . $length . $offset . $extendedXMP;
141
142 $reader = new XMPReader();
143 $reader->parse( $standardXMP );
144 $reader->parseExtended( $extendedPacket );
145 $actual = $reader->getResults();
146
147 $expected = array( 'xmp-exif' =>
148 array(
149 'DigitalZoomRatio' => '0/10',
150 'Flash' => 9,
151 )
152 );
153
154 $this->assertEquals( $expected, $actual );
155 }
156
157 }