3 namespace MediaWiki\Tests\Api\Format
;
5 use MediaWiki\Api\ApiResult
;
6 use MediaWiki\Title\Title
;
11 * @covers \MediaWiki\Api\ApiFormatXml
13 class ApiFormatXmlTest
extends ApiFormatTestBase
{
16 protected $printerName = 'xml';
18 protected function setUp(): void
{
20 $performer = self
::getTestSysop()->getAuthority();
22 Title
::makeTitle( NS_MEDIAWIKI
, 'ApiFormatXmlTest.xsl' ),
23 '<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />',
29 Title
::makeTitle( NS_MEDIAWIKI
, 'ApiFormatXmlTest' ),
36 Title
::makeTitle( NS_MAIN
, 'ApiFormatXmlTest' ),
44 public static function provideGeneralEncoding() {
47 [ [ null, 'a' => null ], '<?xml version="1.0"?><api><_v _idx="0" /></api>' ],
48 [ [ true, 'a' => true ], '<?xml version="1.0"?><api a=""><_v _idx="0">true</_v></api>' ],
49 [ [ false, 'a' => false ], '<?xml version="1.0"?><api><_v _idx="0">false</_v></api>' ],
50 [ [ true, 'a' => true, ApiResult
::META_BC_BOOLS
=> [ 0, 'a' ] ],
51 '<?xml version="1.0"?><api a=""><_v _idx="0">1</_v></api>' ],
52 [ [ false, 'a' => false, ApiResult
::META_BC_BOOLS
=> [ 0, 'a' ] ],
53 '<?xml version="1.0"?><api><_v _idx="0"></_v></api>' ],
54 [ [ 42, 'a' => 42 ], '<?xml version="1.0"?><api a="42"><_v _idx="0">42</_v></api>' ],
55 [ [ 42.5, 'a' => 42.5 ], '<?xml version="1.0"?><api a="42.5"><_v _idx="0">42.5</_v></api>' ],
56 [ [ 1e42
, 'a' => 1e42
], '<?xml version="1.0"?><api a="1.0E+42"><_v _idx="0">1.0E+42</_v></api>' ],
57 [ [ 'foo', 'a' => 'foo' ], '<?xml version="1.0"?><api a="foo"><_v _idx="0">foo</_v></api>' ],
58 [ [ 'fóo', 'a' => 'fóo' ], '<?xml version="1.0"?><api a="fóo"><_v _idx="0">fóo</_v></api>' ],
61 [ [ [] ], '<?xml version="1.0"?><api><_v /></api>' ],
62 [ [ [ 'x' => 1 ] ], '<?xml version="1.0"?><api><_v x="1" /></api>' ],
63 [ [ [ 2 => 1 ] ], '<?xml version="1.0"?><api><_v><_v _idx="2">1</_v></_v></api>' ],
64 [ [ (object)[] ], '<?xml version="1.0"?><api><_v /></api>' ],
65 [ [ [ 1, ApiResult
::META_TYPE
=> 'assoc' ] ], '<?xml version="1.0"?><api><_v><_v _idx="0">1</_v></_v></api>' ],
66 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'array' ] ], '<?xml version="1.0"?><api><_v><_v>1</_v></_v></api>' ],
67 [ [ [ 'x' => 1, 'y' => [ 'z' => 1 ], ApiResult
::META_TYPE
=> 'kvp' ] ],
68 '<?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>' ],
69 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'kvp', ApiResult
::META_INDEXED_TAG_NAME
=> 'i', ApiResult
::META_KVP_KEY_NAME
=> 'key' ] ],
70 '<?xml version="1.0"?><api><_v><i key="x" xml:space="preserve">1</i></_v></api>' ],
71 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'BCkvp', ApiResult
::META_KVP_KEY_NAME
=> 'key' ] ],
72 '<?xml version="1.0"?><api><_v><_v key="x" xml:space="preserve">1</_v></_v></api>' ],
73 [ [ [ 'x' => 1, ApiResult
::META_TYPE
=> 'BCarray' ] ], '<?xml version="1.0"?><api><_v x="1" /></api>' ],
74 [ [ [ 'a', 'b', ApiResult
::META_TYPE
=> 'BCassoc' ] ], '<?xml version="1.0"?><api><_v><_v _idx="0">a</_v><_v _idx="1">b</_v></_v></api>' ],
77 [ [ 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ],
78 '<?xml version="1.0"?><api xml:space="preserve">foo</api>' ],
80 // Specified element name
81 [ [ 'foo', 'bar', ApiResult
::META_INDEXED_TAG_NAME
=> 'itn' ],
82 '<?xml version="1.0"?><api><itn>foo</itn><itn>bar</itn></api>' ],
85 [ [ 'a' => 1, 's' => 1, '_subelements' => [ 's' ] ],
86 '<?xml version="1.0"?><api a="1"><s xml:space="preserve">1</s></api>' ],
88 // Content and subelement
89 [ [ 'a' => 1, 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ],
90 '<?xml version="1.0"?><api a="1" xml:space="preserve">foo</api>' ],
91 [ [ 's' => [], 'content' => 'foo', ApiResult
::META_CONTENT
=> 'content' ],
92 '<?xml version="1.0"?><api><s /><content xml:space="preserve">foo</content></api>' ],
97 ApiResult
::META_CONTENT
=> 'content',
98 ApiResult
::META_SUBELEMENTS
=> [ 's' ]
100 '<?xml version="1.0"?><api><s xml:space="preserve">1</s><content xml:space="preserve">foo</content></api>'
104 [ [ 'foo' => 'foo', ApiResult
::META_BC_SUBELEMENTS
=> [ 'foo' ] ],
105 '<?xml version="1.0"?><api><foo xml:space="preserve">foo</foo></api>' ],
108 [ [ 'foo.bar' => 1 ], '<?xml version="1.0"?><api foo.bar="1" />' ],
109 [ [ '' => 1 ], '<?xml version="1.0"?><api _="1" />' ],
110 [ [ 'foo bar' => 1 ], '<?xml version="1.0"?><api _foo.20.bar="1" />' ],
111 [ [ 'foo:bar' => 1 ], '<?xml version="1.0"?><api _foo.3A.bar="1" />' ],
112 [ [ 'foo%.bar' => 1 ], '<?xml version="1.0"?><api _foo.25..2E.bar="1" />' ],
113 [ [ '4foo' => 1, 'foo4' => 1 ], '<?xml version="1.0"?><api _4foo="1" foo4="1" />' ],
114 [ [ "foo\xe3\x80\x80bar" => 1 ], '<?xml version="1.0"?><api _foo.3000.bar="1" />' ],
115 [ [ 'foo:bar' => 1, ApiResult
::META_PRESERVE_KEYS
=> [ 'foo:bar' ] ],
116 '<?xml version="1.0"?><api foo:bar="1" />' ],
117 [ [ 'a', 'b', ApiResult
::META_INDEXED_TAG_NAME
=> 'foo bar' ],
118 '<?xml version="1.0"?><api><_foo.20.bar>a</_foo.20.bar><_foo.20.bar>b</_foo.20.bar></api>' ],
120 // includenamespace param
121 [ [ 'x' => 'foo' ], '<?xml version="1.0"?><api x="foo" xmlns="http://www.mediawiki.org/xml/api/" />',
122 [ 'includexmlnamespace' => 1 ] ],
125 [ [], '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Invalid or non-existent stylesheet specified.</xml></warnings></api>',
126 [ 'xslt' => 'DoesNotExist' ] ],
127 [ [], '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Stylesheet should be in the MediaWiki namespace.</xml></warnings></api>',
128 [ 'xslt' => 'ApiFormatXmlTest' ] ],
129 [ [], '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Stylesheet should have ".xsl" extension.</xml></warnings></api>',
130 [ 'xslt' => 'MediaWiki:ApiFormatXmlTest' ] ],
132 '<?xml version="1.0"?><?xml-stylesheet href="' .
133 htmlspecialchars( Title
::makeTitle( NS_MEDIAWIKI
, 'ApiFormatXmlTest.xsl' )->getLocalURL( 'action=raw' ) ) .
134 '" type="text/xsl" ?><api />',
135 [ 'xslt' => 'MediaWiki:ApiFormatXmlTest.xsl' ] ],