ApiFormatXmlTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @group API
  4. * @group Database
  5. * @covers ApiFormatXml
  6. */
  7. class ApiFormatXmlTest extends ApiFormatTestBase {
  8. protected $printerName = 'xml';
  9. public static function setUpBeforeClass() {
  10. parent::setUpBeforeClass();
  11. $page = WikiPage::factory( Title::newFromText( 'MediaWiki:ApiFormatXmlTest.xsl' ) );
  12. // phpcs:disable Generic.Files.LineLength
  13. $page->doEditContent( new WikitextContent(
  14. '<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />'
  15. ), 'Summary' );
  16. // phpcs:enable
  17. $page = WikiPage::factory( Title::newFromText( 'MediaWiki:ApiFormatXmlTest' ) );
  18. $page->doEditContent( new WikitextContent( 'Bogus' ), 'Summary' );
  19. $page = WikiPage::factory( Title::newFromText( 'ApiFormatXmlTest' ) );
  20. $page->doEditContent( new WikitextContent( 'Bogus' ), 'Summary' );
  21. }
  22. public static function provideGeneralEncoding() {
  23. // phpcs:disable Generic.Files.LineLength
  24. return [
  25. // Basic types
  26. [ [ null, 'a' => null ], '<?xml version="1.0"?><api><_v _idx="0" /></api>' ],
  27. [ [ true, 'a' => true ], '<?xml version="1.0"?><api a=""><_v _idx="0">true</_v></api>' ],
  28. [ [ false, 'a' => false ], '<?xml version="1.0"?><api><_v _idx="0">false</_v></api>' ],
  29. [ [ true, 'a' => true, ApiResult::META_BC_BOOLS => [ 0, 'a' ] ],
  30. '<?xml version="1.0"?><api a=""><_v _idx="0">1</_v></api>' ],
  31. [ [ false, 'a' => false, ApiResult::META_BC_BOOLS => [ 0, 'a' ] ],
  32. '<?xml version="1.0"?><api><_v _idx="0"></_v></api>' ],
  33. [ [ 42, 'a' => 42 ], '<?xml version="1.0"?><api a="42"><_v _idx="0">42</_v></api>' ],
  34. [ [ 42.5, 'a' => 42.5 ], '<?xml version="1.0"?><api a="42.5"><_v _idx="0">42.5</_v></api>' ],
  35. [ [ 1e42, 'a' => 1e42 ], '<?xml version="1.0"?><api a="1.0E+42"><_v _idx="0">1.0E+42</_v></api>' ],
  36. [ [ 'foo', 'a' => 'foo' ], '<?xml version="1.0"?><api a="foo"><_v _idx="0">foo</_v></api>' ],
  37. [ [ 'fóo', 'a' => 'fóo' ], '<?xml version="1.0"?><api a="fóo"><_v _idx="0">fóo</_v></api>' ],
  38. // Arrays and objects
  39. [ [ [] ], '<?xml version="1.0"?><api><_v /></api>' ],
  40. [ [ [ 'x' => 1 ] ], '<?xml version="1.0"?><api><_v x="1" /></api>' ],
  41. [ [ [ 2 => 1 ] ], '<?xml version="1.0"?><api><_v><_v _idx="2">1</_v></_v></api>' ],
  42. [ [ (object)[] ], '<?xml version="1.0"?><api><_v /></api>' ],
  43. [ [ [ 1, ApiResult::META_TYPE => 'assoc' ] ], '<?xml version="1.0"?><api><_v><_v _idx="0">1</_v></_v></api>' ],
  44. [ [ [ 'x' => 1, ApiResult::META_TYPE => 'array' ] ], '<?xml version="1.0"?><api><_v><_v>1</_v></_v></api>' ],
  45. [ [ [ 'x' => 1, 'y' => [ 'z' => 1 ], ApiResult::META_TYPE => 'kvp' ] ],
  46. '<?xml version="1.0"?><api><_v><_v _name="x" xml:space="preserve">1</_v><_v _name="y"><z xml:space="preserve">1</z></_v></_v></api>' ],
  47. [ [ [ 'x' => 1, ApiResult::META_TYPE => 'kvp', ApiResult::META_INDEXED_TAG_NAME => 'i', ApiResult::META_KVP_KEY_NAME => 'key' ] ],
  48. '<?xml version="1.0"?><api><_v><i key="x" xml:space="preserve">1</i></_v></api>' ],
  49. [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCkvp', ApiResult::META_KVP_KEY_NAME => 'key' ] ],
  50. '<?xml version="1.0"?><api><_v><_v key="x" xml:space="preserve">1</_v></_v></api>' ],
  51. [ [ [ 'x' => 1, ApiResult::META_TYPE => 'BCarray' ] ], '<?xml version="1.0"?><api><_v x="1" /></api>' ],
  52. [ [ [ 'a', 'b', ApiResult::META_TYPE => 'BCassoc' ] ], '<?xml version="1.0"?><api><_v><_v _idx="0">a</_v><_v _idx="1">b</_v></_v></api>' ],
  53. // Content
  54. [ [ 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
  55. '<?xml version="1.0"?><api xml:space="preserve">foo</api>' ],
  56. // Specified element name
  57. [ [ 'foo', 'bar', ApiResult::META_INDEXED_TAG_NAME => 'itn' ],
  58. '<?xml version="1.0"?><api><itn>foo</itn><itn>bar</itn></api>' ],
  59. // Subelements
  60. [ [ 'a' => 1, 's' => 1, '_subelements' => [ 's' ] ],
  61. '<?xml version="1.0"?><api a="1"><s xml:space="preserve">1</s></api>' ],
  62. // Content and subelement
  63. [ [ 'a' => 1, 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
  64. '<?xml version="1.0"?><api a="1" xml:space="preserve">foo</api>' ],
  65. [ [ 's' => [], 'content' => 'foo', ApiResult::META_CONTENT => 'content' ],
  66. '<?xml version="1.0"?><api><s /><content xml:space="preserve">foo</content></api>' ],
  67. [
  68. [
  69. 's' => 1,
  70. 'content' => 'foo',
  71. ApiResult::META_CONTENT => 'content',
  72. ApiResult::META_SUBELEMENTS => [ 's' ]
  73. ],
  74. '<?xml version="1.0"?><api><s xml:space="preserve">1</s><content xml:space="preserve">foo</content></api>'
  75. ],
  76. // BC Subelements
  77. [ [ 'foo' => 'foo', ApiResult::META_BC_SUBELEMENTS => [ 'foo' ] ],
  78. '<?xml version="1.0"?><api><foo xml:space="preserve">foo</foo></api>' ],
  79. // Name mangling
  80. [ [ 'foo.bar' => 1 ], '<?xml version="1.0"?><api foo.bar="1" />' ],
  81. [ [ '' => 1 ], '<?xml version="1.0"?><api _="1" />' ],
  82. [ [ 'foo bar' => 1 ], '<?xml version="1.0"?><api _foo.20.bar="1" />' ],
  83. [ [ 'foo:bar' => 1 ], '<?xml version="1.0"?><api _foo.3A.bar="1" />' ],
  84. [ [ 'foo%.bar' => 1 ], '<?xml version="1.0"?><api _foo.25..2E.bar="1" />' ],
  85. [ [ '4foo' => 1, 'foo4' => 1 ], '<?xml version="1.0"?><api _4foo="1" foo4="1" />' ],
  86. [ [ "foo\xe3\x80\x80bar" => 1 ], '<?xml version="1.0"?><api _foo.3000.bar="1" />' ],
  87. [ [ 'foo:bar' => 1, ApiResult::META_PRESERVE_KEYS => [ 'foo:bar' ] ],
  88. '<?xml version="1.0"?><api foo:bar="1" />' ],
  89. [ [ 'a', 'b', ApiResult::META_INDEXED_TAG_NAME => 'foo bar' ],
  90. '<?xml version="1.0"?><api><_foo.20.bar>a</_foo.20.bar><_foo.20.bar>b</_foo.20.bar></api>' ],
  91. // includenamespace param
  92. [ [ 'x' => 'foo' ], '<?xml version="1.0"?><api x="foo" xmlns="http://www.mediawiki.org/xml/api/" />',
  93. [ 'includexmlnamespace' => 1 ] ],
  94. // xslt param
  95. [ [], '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Invalid or non-existent stylesheet specified.</xml></warnings></api>',
  96. [ 'xslt' => 'DoesNotExist' ] ],
  97. [ [], '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Stylesheet should be in the MediaWiki namespace.</xml></warnings></api>',
  98. [ 'xslt' => 'ApiFormatXmlTest' ] ],
  99. [ [], '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Stylesheet should have ".xsl" extension.</xml></warnings></api>',
  100. [ 'xslt' => 'MediaWiki:ApiFormatXmlTest' ] ],
  101. [ [],
  102. '<?xml version="1.0"?><?xml-stylesheet href="' .
  103. htmlspecialchars( Title::newFromText( 'MediaWiki:ApiFormatXmlTest.xsl' )->getLocalURL( 'action=raw' ) ) .
  104. '" type="text/xsl" ?><api />',
  105. [ 'xslt' => 'MediaWiki:ApiFormatXmlTest.xsl' ] ],
  106. ];
  107. // phpcs:enable
  108. }
  109. }