Debian Codegraph @

tests/phpunit/includes/media/SVGReaderTest.php

raw symbols SCIP
1 <?php
2
3 /**
4 * @group Media
5 * @covers \SVGReader
6 */
7 class SVGReaderTest extends \MediaWikiIntegrationTestCase {
8
9 /**
10 * @dataProvider provideSvgFiles
11 */
12 public function testGetMetadata( $infile, $expected ) {
13 $this->assertMetadata( $infile, $expected );
14 }
15
16 /**
17 * @dataProvider provideSvgFilesWithXMLMetadata
18 */
19 public function testGetXMLMetadata( $infile, $expected ) {
20 $r = new XMLReader();
21 $this->assertMetadata( $infile, $expected );
22 }
23
24 /**
25 * @dataProvider provideSvgUnits
26 */
27 public function testScaleSVGUnit( $inUnit, $expected ) {
28 $this->assertEquals(
29 $expected,
30 SVGReader::scaleSVGUnit( $inUnit ),
31 'SVG unit conversion and scaling failure'
32 );
33 }
34
35 private function assertMetadata( $infile, $expected ) {
36 if ( $expected === false ) {
37 $this->expectException( InvalidSVGException::class );
38 }
39 $svgReader = new SVGReader( $infile );
40 $data = $svgReader->getMetadata();
41
42 $this->assertEquals( $expected, $data, 'SVG metadata extraction test' );
43 }
44
45 public static function provideSvgFiles() {
46 $base = __DIR__ . '/../../data/media';
47
48 return [
49 [
50 "$base/Wikimedia-logo.svg",
51 [
52 'width' => 1024,
53 'height' => 1024,
54 'originalWidth' => '1024',
55 'originalHeight' => '1024',
56 'translations' => [],
57 ]
58 ],
59 [
60 "$base/QA_icon.svg",
61 [
62 'width' => 60,
63 'height' => 60,
64 'originalWidth' => '60',
65 'originalHeight' => '60',
66 'translations' => [],
67 ]
68 ],
69 [
70 "$base/Gtk-media-play-ltr.svg",
71 [
72 'width' => 60,
73 'height' => 60,
74 'originalWidth' => '60.0000000',
75 'originalHeight' => '60.0000000',
76 'translations' => [],
77 ]
78 ],
79 [
80 "$base/Toll_Texas_1.svg",
81 // This file triggered T33719, needs entity expansion in the xmlns checks
82 [
83 'width' => 385,
84 'height' => 385,
85 'originalWidth' => '385',
86 'originalHeight' => '385.0004883',
87 'translations' => [],
88 ]
89 ],
90 [
91 "$base/Tux.svg",
92 [
93 'width' => 512,
94 'height' => 594,
95 'originalWidth' => '100%',
96 'originalHeight' => '100%',
97 'title' => 'Tux',
98 'translations' => [],
99 'description' => 'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg',
100 ]
101 ],
102 [
103 "$base/Speech_bubbles.svg",
104 [
105 'width' => 669,
106 'height' => 491,
107 'originalWidth' => '17.7cm',
108 'originalHeight' => '13cm',
109 'translations' => [
110 'de' => SVGReader::LANG_FULL_MATCH,
111 'fr' => SVGReader::LANG_FULL_MATCH,
112 'nl' => SVGReader::LANG_FULL_MATCH,
113 'tlh-ca' => SVGReader::LANG_FULL_MATCH,
114 'tlh' => SVGReader::LANG_PREFIX_MATCH
115 ],
116 ]
117 ],
118 [
119 "$base/Soccer_ball_animated.svg",
120 [
121 'width' => 150,
122 'height' => 150,
123 'originalWidth' => '150',
124 'originalHeight' => '150',
125 'animated' => true,
126 'translations' => []
127 ],
128 ],
129 [
130 "$base/comma_separated_viewbox.svg",
131 [
132 'width' => 512,
133 'height' => 594,
134 'originalWidth' => '100%',
135 'originalHeight' => '100%',
136 'translations' => []
137 ],
138 ],
139 [
140 "$base/css-animated.svg",
141 [
142 'width' => 100,
143 'height' => 100,
144 'originalWidth' => '100',
145 'originalHeight' => '100',
146 'animated' => true,
147 'translations' => []
148 ],
149 ],
150 ];
151 }
152
153 public static function provideSvgFilesWithXMLMetadata() {
154 $base = __DIR__ . '/../../data/media';
155 $metadata = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
156 <ns4:Work xmlns:ns4="http://creativecommons.org/ns#" rdf:about="">
157 <ns5:format xmlns:ns5="http://purl.org/dc/elements/1.1/">image/svg+xml</ns5:format>
158 <ns5:type xmlns:ns5="http://purl.org/dc/elements/1.1/" rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
159 </ns4:Work>
160 </rdf:RDF>';
161 // phpcs:enable
162
163 $metadata = str_replace( "\r", '', $metadata ); // Windows compat
164 return [
165 [
166 "$base/US_states_by_total_state_tax_revenue.svg",
167 [
168 'height' => 593,
169 'metadata' => $metadata,
170 'width' => 959,
171 'originalWidth' => '958.69',
172 'originalHeight' => '592.78998',
173 'translations' => [],
174 ]
175 ],
176 ];
177 }
178
179 public static function provideSvgUnits() {
180 return [
181 [ '1', 1 ],
182 [ '1.1', 1.1 ],
183 [ '0.1', 0.1 ],
184 [ '.1', 0.1 ],
185 [ '1e2', 100 ],
186 [ '1E2', 100 ],
187 [ '+1', 1 ],
188 [ '-1', -1 ],
189 [ '-1.1', -1.1 ],
190 [ '1e+2', 100 ],
191 [ '1e-2', 0.01 ],
192 [ '10px', 10 ],
193 [ '10.2cm', 10.2 * 37.795275 ],
194 [ '10pt', 10 * 1.333333 ],
195 [ '10pc', 10 * 16 ],
196 [ '10mm', 10 * 3.7795275 ],
197 [ '10q', 10 * 0.944881 ],
198 [ '10Q', 10 * 0.944881 ],
199 [ '10cm', 10 * 37.795275 ],
200 [ '10in', 10 * 96 ],
201 [ '10em', 10 * 16 ],
202 [ '10rem', 10 * 16 ],
203 [ '10ex', 10 * 8 ],
204 [ '10ch', 10 * 8 ],
205 [ '10%', 51.2 ],
206 [ '10 px', 10 ],
207 // Invalid values
208 [ '1e1.1', 10 ],
209 [ '10bp', 10 ],
210 [ 'p10', null ],
211 ];
212 }
213 }