MediaWiki REL1_32
SVGMetadataExtractorTest.php
Go to the documentation of this file.
1<?php
2
8
12 public function testGetMetadata( $infile, $expected ) {
13 $this->assertMetadata( $infile, $expected );
14 }
15
19 public function testGetXMLMetadata( $infile, $expected ) {
20 $r = new XMLReader();
21 if ( !method_exists( $r, 'readInnerXML' ) ) {
22 $this->markTestSkipped( 'XMLReader::readInnerXML() does not exist (libxml >2.6.20 needed).' );
23
24 return;
25 }
26 $this->assertMetadata( $infile, $expected );
27 }
28
32 public function testScaleSVGUnit( $inUnit, $expected ) {
33 $this->assertEquals(
34 $expected,
35 SVGReader::scaleSVGUnit( $inUnit ),
36 'SVG unit conversion and scaling failure'
37 );
38 }
39
40 function assertMetadata( $infile, $expected ) {
41 try {
42 $data = SVGMetadataExtractor::getMetadata( $infile );
43 $this->assertEquals( $expected, $data, 'SVG metadata extraction test' );
44 } catch ( MWException $e ) {
45 if ( $expected === false ) {
46 $this->assertTrue( true, 'SVG metadata extracted test (expected failure)' );
47 } else {
48 throw $e;
49 }
50 }
51 }
52
53 public static function provideSvgFiles() {
54 $base = __DIR__ . '/../../data/media';
55
56 return [
57 [
58 "$base/Wikimedia-logo.svg",
59 [
60 'width' => 1024,
61 'height' => 1024,
62 'originalWidth' => '1024',
63 'originalHeight' => '1024',
64 'translations' => [],
65 ]
66 ],
67 [
68 "$base/QA_icon.svg",
69 [
70 'width' => 60,
71 'height' => 60,
72 'originalWidth' => '60',
73 'originalHeight' => '60',
74 'translations' => [],
75 ]
76 ],
77 [
78 "$base/Gtk-media-play-ltr.svg",
79 [
80 'width' => 60,
81 'height' => 60,
82 'originalWidth' => '60.0000000',
83 'originalHeight' => '60.0000000',
84 'translations' => [],
85 ]
86 ],
87 [
88 "$base/Toll_Texas_1.svg",
89 // This file triggered T33719, needs entity expansion in the xmlns checks
90 [
91 'width' => 385,
92 'height' => 385,
93 'originalWidth' => '385',
94 'originalHeight' => '385.0004883',
95 'translations' => [],
96 ]
97 ],
98 [
99 "$base/Tux.svg",
100 [
101 'width' => 512,
102 'height' => 594,
103 'originalWidth' => '100%',
104 'originalHeight' => '100%',
105 'title' => 'Tux',
106 'translations' => [],
107 'description' => 'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg',
108 ]
109 ],
110 [
111 "$base/Speech_bubbles.svg",
112 [
113 'width' => 627,
114 'height' => 461,
115 'originalWidth' => '17.7cm',
116 'originalHeight' => '13cm',
117 'translations' => [
121 'tlh-ca' => SVGReader::LANG_FULL_MATCH,
123 ],
124 ]
125 ],
126 [
127 "$base/Soccer_ball_animated.svg",
128 [
129 'width' => 150,
130 'height' => 150,
131 'originalWidth' => '150',
132 'originalHeight' => '150',
133 'animated' => true,
134 'translations' => []
135 ],
136 ],
137 [
138 "$base/comma_separated_viewbox.svg",
139 [
140 'width' => 512,
141 'height' => 594,
142 'originalWidth' => '100%',
143 'originalHeight' => '100%',
144 'translations' => []
145 ],
146 ],
147 ];
148 }
149
150 public static function provideSvgFilesWithXMLMetadata() {
151 $base = __DIR__ . '/../../data/media';
152 // phpcs:disable Generic.Files.LineLength
153 $metadata = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
154 <ns4:Work xmlns:ns4="http://creativecommons.org/ns#" rdf:about="">
155 <ns5:format xmlns:ns5="http://purl.org/dc/elements/1.1/">image/svg+xml</ns5:format>
156 <ns5:type xmlns:ns5="http://purl.org/dc/elements/1.1/" rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
157 </ns4:Work>
158 </rdf:RDF>';
159 // phpcs:enable
160
161 $metadata = str_replace( "\r", '', $metadata ); // Windows compat
162 return [
163 [
164 "$base/US_states_by_total_state_tax_revenue.svg",
165 [
166 'height' => 593,
167 'metadata' => $metadata,
168 'width' => 959,
169 'originalWidth' => '958.69',
170 'originalHeight' => '592.78998',
171 'translations' => [],
172 ]
173 ],
174 ];
175 }
176
177 public static function provideSvgUnits() {
178 return [
179 [ '1' , 1 ],
180 [ '1.1' , 1.1 ],
181 [ '0.1' , 0.1 ],
182 [ '.1' , 0.1 ],
183 [ '1e2' , 100 ],
184 [ '1E2' , 100 ],
185 [ '+1' , 1 ],
186 [ '-1' , -1 ],
187 [ '-1.1' , -1.1 ],
188 [ '1e+2' , 100 ],
189 [ '1e-2' , 0.01 ],
190 [ '10px' , 10 ],
191 [ '10pt' , 10 * 1.25 ],
192 [ '10pc' , 10 * 15 ],
193 [ '10mm' , 10 * 3.543307 ],
194 [ '10cm' , 10 * 35.43307 ],
195 [ '10in' , 10 * 90 ],
196 [ '10em' , 10 * 16 ],
197 [ '10ex' , 10 * 12 ],
198 [ '10%' , 51.2 ],
199 [ '10 px' , 10 ],
200 // Invalid values
201 [ '1e1.1', 10 ],
202 [ '10bp', 10 ],
203 [ 'p10', null ],
204 ];
205 }
206}
MediaWiki exception.
Media SVGMetadataExtractor.
testGetMetadata( $infile, $expected)
provideSvgFiles
testScaleSVGUnit( $inUnit, $expected)
provideSvgUnits
testGetXMLMetadata( $infile, $expected)
provideSvgFilesWithXMLMetadata
static getMetadata( $filename)
static scaleSVGUnit( $length, $viewportSize=512)
Return a rounded pixel equivalent for a labeled CSS/SVG length.
returning false will NOT prevent logging $e
Definition hooks.txt:2226