Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / media / SVGMetadataExtractorTest.php
1 <?php
2
3 /**
4 * @group Media
5 * @covers SVGMetadataExtractor
6 */
7 class SVGMetadataExtractorTest 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 function assertMetadata( $infile, $expected ) {
36 try {
37 $data = SVGMetadataExtractor::getMetadata( $infile );
38 $this->assertEquals( $expected, $data, 'SVG metadata extraction test' );
39 } catch ( MWException $e ) {
40 if ( $expected === false ) {
41 $this->assertTrue( true, 'SVG metadata extracted test (expected failure)' );
42 } else {
43 throw $e;
44 }
45 }
46 }
47
48 public static function provideSvgFiles() {
49 $base = __DIR__ . '/../../data/media';
50
51 return [
52 [
53 "$base/Wikimedia-logo.svg",
54 [
55 'width' => 1024,
56 'height' => 1024,
57 'originalWidth' => '1024',
58 'originalHeight' => '1024',
59 'translations' => [],
60 ]
61 ],
62 [
63 "$base/QA_icon.svg",
64 [
65 'width' => 60,
66 'height' => 60,
67 'originalWidth' => '60',
68 'originalHeight' => '60',
69 'translations' => [],
70 ]
71 ],
72 [
73 "$base/Gtk-media-play-ltr.svg",
74 [
75 'width' => 60,
76 'height' => 60,
77 'originalWidth' => '60.0000000',
78 'originalHeight' => '60.0000000',
79 'translations' => [],
80 ]
81 ],
82 [
83 "$base/Toll_Texas_1.svg",
84 // This file triggered T33719, needs entity expansion in the xmlns checks
85 [
86 'width' => 385,
87 'height' => 385,
88 'originalWidth' => '385',
89 'originalHeight' => '385.0004883',
90 'translations' => [],
91 ]
92 ],
93 [
94 "$base/Tux.svg",
95 [
96 'width' => 512,
97 'height' => 594,
98 'originalWidth' => '100%',
99 'originalHeight' => '100%',
100 'title' => 'Tux',
101 'translations' => [],
102 'description' => 'For more information see: http://commons.wikimedia.org/wiki/Image:Tux.svg',
103 ]
104 ],
105 [
106 "$base/Speech_bubbles.svg",
107 [
108 'width' => 627,
109 'height' => 461,
110 'originalWidth' => '17.7cm',
111 'originalHeight' => '13cm',
112 'translations' => [
113 'de' => SVGReader::LANG_FULL_MATCH,
114 'fr' => SVGReader::LANG_FULL_MATCH,
115 'nl' => SVGReader::LANG_FULL_MATCH,
116 'tlh-ca' => SVGReader::LANG_FULL_MATCH,
117 'tlh' => SVGReader::LANG_PREFIX_MATCH
118 ],
119 ]
120 ],
121 [
122 "$base/Soccer_ball_animated.svg",
123 [
124 'width' => 150,
125 'height' => 150,
126 'originalWidth' => '150',
127 'originalHeight' => '150',
128 'animated' => true,
129 'translations' => []
130 ],
131 ],
132 [
133 "$base/comma_separated_viewbox.svg",
134 [
135 'width' => 512,
136 'height' => 594,
137 'originalWidth' => '100%',
138 'originalHeight' => '100%',
139 'translations' => []
140 ],
141 ],
142 ];
143 }
144
145 public static function provideSvgFilesWithXMLMetadata() {
146 $base = __DIR__ . '/../../data/media';
147 // phpcs:disable Generic.Files.LineLength
148 $metadata = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
149 <ns4:Work xmlns:ns4="http://creativecommons.org/ns#" rdf:about="">
150 <ns5:format xmlns:ns5="http://purl.org/dc/elements/1.1/">image/svg+xml</ns5:format>
151 <ns5:type xmlns:ns5="http://purl.org/dc/elements/1.1/" rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
152 </ns4:Work>
153 </rdf:RDF>';
154 // phpcs:enable
155
156 $metadata = str_replace( "\r", '', $metadata ); // Windows compat
157 return [
158 [
159 "$base/US_states_by_total_state_tax_revenue.svg",
160 [
161 'height' => 593,
162 'metadata' => $metadata,
163 'width' => 959,
164 'originalWidth' => '958.69',
165 'originalHeight' => '592.78998',
166 'translations' => [],
167 ]
168 ],
169 ];
170 }
171
172 public static function provideSvgUnits() {
173 return [
174 [ '1' , 1 ],
175 [ '1.1' , 1.1 ],
176 [ '0.1' , 0.1 ],
177 [ '.1' , 0.1 ],
178 [ '1e2' , 100 ],
179 [ '1E2' , 100 ],
180 [ '+1' , 1 ],
181 [ '-1' , -1 ],
182 [ '-1.1' , -1.1 ],
183 [ '1e+2' , 100 ],
184 [ '1e-2' , 0.01 ],
185 [ '10px' , 10 ],
186 [ '10pt' , 10 * 1.25 ],
187 [ '10pc' , 10 * 15 ],
188 [ '10mm' , 10 * 3.543307 ],
189 [ '10cm' , 10 * 35.43307 ],
190 [ '10in' , 10 * 90 ],
191 [ '10em' , 10 * 16 ],
192 [ '10ex' , 10 * 12 ],
193 [ '10%' , 51.2 ],
194 [ '10 px' , 10 ],
195 // Invalid values
196 [ '1e1.1', 10 ],
197 [ '10bp', 10 ],
198 [ 'p10', null ],
199 ];
200 }
201 }