Fix uncaught ApiFormatXml exception with api debuginfo
authoraude <aude.wiki@gmail.com>
Sat, 28 Jun 2014 18:35:52 +0000 (20:35 +0200)
committeraude <aude.wiki@gmail.com>
Sat, 28 Jun 2014 18:35:52 +0000 (20:35 +0200)
Also added tests to cover this.

Bug: 67246
Change-Id: Ifb2e392d3277a4702832727f70c77b170d4b2bf5

includes/debug/Debug.php
tests/phpunit/includes/debug/MWDebugTest.php

index 2117010..0cea658 100644 (file)
@@ -513,6 +513,7 @@ class MWDebug {
                $result->setIndexedTagName( $debugInfo['debugLog'], 'msg' );
                $result->setIndexedTagName( $debugInfo['queries'], 'query' );
                $result->setIndexedTagName( $debugInfo['includes'], 'queries' );
+               $result->setIndexedTagName( $debugInfo['profile'], 'function' );
                $result->addValue( null, 'debuginfo', $debugInfo );
        }
 
index 91399be..e642177 100644 (file)
@@ -80,4 +80,62 @@ class MWDebugTest extends MediaWikiTestCase {
                        "Only one deprecated warning per function should be kept"
                );
        }
+
+       /**
+        * @covers MWDebug::appendDebugInfoToApiResult
+        */
+       public function testAppendDebugInfoToApiResultXmlFormat() {
+               $request = $this->newApiRequest(
+                       array( 'action' => 'help', 'format' => 'xml' ),
+                       '/api.php?action=help&format=xml'
+               );
+
+               $context = new RequestContext();
+               $context->setRequest( $request );
+
+               $apiMain = new ApiMain( $context );
+
+               $result = new ApiResult( $apiMain );
+               $result->setRawMode( true );
+
+               MWDebug::appendDebugInfoToApiResult( $context, $result );
+
+               $this->assertInstanceOf( 'ApiResult', $result );
+               $data = $result->getData();
+
+               $expectedKeys = array( 'mwVersion', 'phpVersion', 'gitRevision', 'gitBranch',
+                       'gitViewUrl', 'time', 'log', 'debugLog', 'queries', 'request', 'memory',
+                       'memoryPeak', 'includes', 'profile', '_element' );
+
+               foreach( $expectedKeys as $expectedKey ) {
+                       $this->assertArrayHasKey( $expectedKey, $data['debuginfo'], "debuginfo has $expectedKey" );
+               }
+
+               $xml = ApiFormatXml::recXmlPrint( 'help', $data );
+
+               // exception not thrown
+               $this->assertInternalType( 'string', $xml );
+       }
+
+       /**
+        * @param string[] $params
+        * @param string $requestUrl
+        *
+        * @return FauxRequest
+        */
+       private function newApiRequest( array $params, $requestUrl ) {
+               $request = $this->getMockBuilder( 'FauxRequest' )
+                       ->setMethods( array( 'getRequestURL' ) )
+                       ->setConstructorArgs( array(
+                               $params
+                       ) )
+                       ->getMock();
+
+               $request->expects( $this->any() )
+                       ->method( 'getRequestURL' )
+                       ->will( $this->returnValue( $requestUrl ) );
+
+               return $request;
+       }
+
 }