SVGMetadataExtractorTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @group Media
  4. * @covers SVGMetadataExtractor
  5. */
  6. class SVGMetadataExtractorTest extends MediaWikiTestCase {
  7. /**
  8. * @dataProvider provideSvgFiles
  9. */
  10. public function testGetMetadata( $infile, $expected ) {
  11. $this->assertMetadata( $infile, $expected );
  12. }
  13. /**
  14. * @dataProvider provideSvgFilesWithXMLMetadata
  15. */
  16. public function testGetXMLMetadata( $infile, $expected ) {
  17. $r = new XMLReader();
  18. if ( !method_exists( $r, 'readInnerXML' ) ) {
  19. $this->markTestSkipped( 'XMLReader::readInnerXML() does not exist (libxml >2.6.20 needed).' );
  20. return;
  21. }
  22. $this->assertMetadata( $infile, $expected );
  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. function assertMetadata( $infile, $expected ) {
  35. try {
  36. $data = SVGMetadataExtractor::getMetadata( $infile );
  37. $this->assertEquals( $expected, $data, 'SVG metadata extraction test' );
  38. } catch ( MWException $e ) {
  39. if ( $expected === false ) {
  40. $this->assertTrue( true, 'SVG metadata extracted test (expected failure)' );
  41. } else {
  42. throw $e;
  43. }
  44. }
  45. }
  46. public static function provideSvgFiles() {
  47. $base = __DIR__ . '/../../data/media';
  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' => 627,
  106. 'height' => 461,
  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. }
  141. public static function provideSvgFilesWithXMLMetadata() {
  142. $base = __DIR__ . '/../../data/media';
  143. // phpcs:disable Generic.Files.LineLength
  144. $metadata = '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  145. <ns4:Work xmlns:ns4="http://creativecommons.org/ns#" rdf:about="">
  146. <ns5:format xmlns:ns5="http://purl.org/dc/elements/1.1/">image/svg+xml</ns5:format>
  147. <ns5:type xmlns:ns5="http://purl.org/dc/elements/1.1/" rdf:resource="http://purl.org/dc/dcmitype/StillImage"/>
  148. </ns4:Work>
  149. </rdf:RDF>';
  150. // phpcs:enable
  151. $metadata = str_replace( "\r", '', $metadata ); // Windows compat
  152. return [
  153. [
  154. "$base/US_states_by_total_state_tax_revenue.svg",
  155. [
  156. 'height' => 593,
  157. 'metadata' => $metadata,
  158. 'width' => 959,
  159. 'originalWidth' => '958.69',
  160. 'originalHeight' => '592.78998',
  161. 'translations' => [],
  162. ]
  163. ],
  164. ];
  165. }
  166. public static function provideSvgUnits() {
  167. return [
  168. [ '1' , 1 ],
  169. [ '1.1' , 1.1 ],
  170. [ '0.1' , 0.1 ],
  171. [ '.1' , 0.1 ],
  172. [ '1e2' , 100 ],
  173. [ '1E2' , 100 ],
  174. [ '+1' , 1 ],
  175. [ '-1' , -1 ],
  176. [ '-1.1' , -1.1 ],
  177. [ '1e+2' , 100 ],
  178. [ '1e-2' , 0.01 ],
  179. [ '10px' , 10 ],
  180. [ '10pt' , 10 * 1.25 ],
  181. [ '10pc' , 10 * 15 ],
  182. [ '10mm' , 10 * 3.543307 ],
  183. [ '10cm' , 10 * 35.43307 ],
  184. [ '10in' , 10 * 90 ],
  185. [ '10em' , 10 * 16 ],
  186. [ '10ex' , 10 * 12 ],
  187. [ '10%' , 51.2 ],
  188. [ '10 px' , 10 ],
  189. // Invalid values
  190. [ '1e1.1', 10 ],
  191. [ '10bp', 10 ],
  192. [ 'p10', null ],
  193. ];
  194. }
  195. }