Merge "Consolidate mobile and desktop designs for login form"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / format / ApiFormatXmlTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @covers ApiFormatXml
7 */
8 class ApiFormatXmlTest extends ApiFormatTestBase {
9
10 protected $printerName = 'xml';
11
12 protected function setUp() {
13 parent::setUp();
14 $page = WikiPage::factory( Title::newFromText( 'MediaWiki:ApiFormatXmlTest.xsl' ) );
15 $page->doEditContent( new WikitextContent(
16 '<?xml version="1.0"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" />'
17 ), 'Summary' );
18 $page = WikiPage::factory( Title::newFromText( 'MediaWiki:ApiFormatXmlTest' ) );
19 $page->doEditContent( new WikitextContent( 'Bogus' ), 'Summary' );
20 $page = WikiPage::factory( Title::newFromText( 'ApiFormatXmlTest' ) );
21 $page->doEditContent( new WikitextContent( 'Bogus' ), 'Summary' );
22 }
23
24 public static function provideGeneralEncoding() {
25 $tests = array(
26 // Basic types
27 array( array( null ), '<?xml version="1.0"?><api><x /></api>' ),
28 array( array( true, 'a' => true ), '<?xml version="1.0"?><api a=""><x>1</x></api>' ),
29 array( array( false, 'a' => false ), '<?xml version="1.0"?><api><x></x></api>' ),
30 array( array( 42, 'a' => 42 ), '<?xml version="1.0"?><api a="42"><x>42</x></api>' ),
31 array( array( 42.5, 'a' => 42.5 ), '<?xml version="1.0"?><api a="42.5"><x>42.5</x></api>' ),
32 array( array( 1e42, 'a' => 1e42 ), '<?xml version="1.0"?><api a="1.0E+42"><x>1.0E+42</x></api>' ),
33 array( array( 'foo', 'a' => 'foo' ), '<?xml version="1.0"?><api a="foo"><x>foo</x></api>' ),
34 array( array( 'fóo', 'a' => 'fóo' ), '<?xml version="1.0"?><api a="fóo"><x>fóo</x></api>' ),
35
36 // Arrays and objects
37 array( array( array() ), '<?xml version="1.0"?><api><x /></api>' ),
38 array( array( array( 'x' => 1 ) ), '<?xml version="1.0"?><api><x x="1" /></api>' ),
39 array( array( array( 2 => 1, '_element' => 'x' ) ), '<?xml version="1.0"?><api><x><x>1</x></x></api>' ),
40
41 // Content
42 array( array( '*' => 'foo' ), '<?xml version="1.0"?><api xml:space="preserve">foo</api>' ),
43
44 // Subelements
45 array( array( 'a' => 1, 's' => 1, '_subelements' => array( 's' ) ),
46 '<?xml version="1.0"?><api a="1"><s xml:space="preserve">1</s></api>' ),
47
48 // includenamespace param
49 array( array( 'x' => 'foo' ), '<?xml version="1.0"?><api x="foo" xmlns="http://www.mediawiki.org/xml/api/" />',
50 array( 'includexmlnamespace' => 1 ) ),
51
52 // xslt param
53 array( array(), '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Invalid or non-existent stylesheet specified</xml></warnings></api>',
54 array( 'xslt' => 'DoesNotExist' ) ),
55 array( array(), '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Stylesheet should be in the MediaWiki namespace.</xml></warnings></api>',
56 array( 'xslt' => 'ApiFormatXmlTest' ) ),
57 array( array(), '<?xml version="1.0"?><api><warnings><xml xml:space="preserve">Stylesheet should have .xsl extension.</xml></warnings></api>',
58 array( 'xslt' => 'MediaWiki:ApiFormatXmlTest' ) ),
59 array( array(),
60 '<?xml version="1.0"?><?xml-stylesheet href="' .
61 htmlspecialchars( Title::newFromText( 'MediaWiki:ApiFormatXmlTest.xsl' )->getLocalURL( 'action=raw' ) ) .
62 '" type="text/xsl" ?><api />',
63 array( 'xslt' => 'MediaWiki:ApiFormatXmlTest.xsl' ) ),
64 );
65
66 // Add in the needed "_element" for all indexed arrays
67 $ret = array();
68 foreach ( $tests as $v ) {
69 $v[0] += array( '_element' => 'x' );
70 $ret[] = $v;
71 }
72 return $ret;
73 }
74
75 /**
76 * @dataProvider provideXmlFail
77 */
78 public function testXmlFail( array $data, $expect, array $params = array() ) {
79 try {
80 echo $this->encodeData( $params, $data ) . "\n";
81 $this->fail( "Expected exception not thrown" );
82 } catch ( MWException $ex ) {
83 $this->assertSame( $expect, $ex->getMessage(), 'Expected exception' );
84 }
85 }
86
87 public static function provideXmlFail() {
88 return array(
89 // Array without _element
90 array( array( 1 ), 'Internal error in ApiFormatXml::recXmlPrint: (api, ...) has integer keys without _element value. Use ApiResult::setIndexedTagName().' ),
91 // Content and subelement
92 array( array( 1, 's' => array(), '*' => 2, '_element' => 'x' ), 'Internal error in ApiFormatXml::recXmlPrint: (api, ...) has content and subelements' ),
93 array( array( 1, 's' => 1, '*' => 2, '_element' => 'x', '_subelements' => array( 's' ) ), 'Internal error in ApiFormatXml::recXmlPrint: (api, ...) has content and subelements' ),
94 // These should fail but don't because of a long-standing bug (see T57371#639713)
95 //array( array( 1, '*' => 2, '_element' => 'x' ), 'Internal error in ApiFormatXml::recXmlPrint: (api, ...) has content and subelements' ),
96 //array( array( 's' => array(), '*' => 2 ), 'Internal error in ApiFormatXml::recXmlPrint: (api, ...) has content and subelements' ),
97 //array( array( 's' => 1, '*' => 2, '_subelements' => array( 's' ) ), 'Internal error in ApiFormatXml::recXmlPrint: (api, ...) has content and subelements' ),
98 );
99 }
100
101 }