Merge "Moved textbox1 building into TextConflictHelper on edit conflicts"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 4 Jan 2018 21:56:45 +0000 (21:56 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 4 Jan 2018 21:56:45 +0000 (21:56 +0000)
RELEASE-NOTES-1.31
autoload.php
includes/HtmlFormatter.php [deleted file]
includes/MediaWiki.php
includes/parser/Parser.php
resources/src/mediawiki.rcfilters/mw.rcfilters.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.ChangesLimitAndDateButtonWidget.js
resources/src/mediawiki.rcfilters/ui/mw.rcfilters.ui.FilterWrapperWidget.js
tests/phpunit/includes/api/format/ApiFormatBaseTest.php [new file with mode: 0644]
tests/phpunit/includes/api/format/ApiFormatRawTest.php [new file with mode: 0644]
tests/phpunit/includes/api/format/ApiFormatTestBase.php

index 4eb4c01..d18c5cf 100644 (file)
@@ -161,6 +161,8 @@ changes to languages because of Phabricator reports.
 * The $statementsOnOwnLine parameter of JavaScriptMinifier::minify was removed.
   The corresponding configuration variable ($wgResourceLoaderMinifierStatementsOnOwnLine)
   has been deprecated since 1.27 and was removed as well.
+* The HtmlFormatter class was removed (deprecated in 1.27). The namespaced
+  HtmlFormatter\HtmlFormatter class should be used instead.
 
 == Compatibility ==
 MediaWiki 1.31 requires PHP 5.5.9 or later. There is experimental support for
index af0b200..351136d 100644 (file)
@@ -606,7 +606,6 @@ $wgAutoloadLocalClasses = [
        'Hooks' => __DIR__ . '/includes/Hooks.php',
        'Html' => __DIR__ . '/includes/Html.php',
        'HtmlArmor' => __DIR__ . '/includes/libs/HtmlArmor.php',
-       'HtmlFormatter' => __DIR__ . '/includes/HtmlFormatter.php',
        'Http' => __DIR__ . '/includes/http/Http.php',
        'HttpError' => __DIR__ . '/includes/exception/HttpError.php',
        'HttpStatus' => __DIR__ . '/includes/libs/HttpStatus.php',
diff --git a/includes/HtmlFormatter.php b/includes/HtmlFormatter.php
deleted file mode 100644 (file)
index 9bae8b5..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php
-/**
- * Stub for extensions that haven't switched to Composer-based version of this class
- * @todo: remove in 1.28
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- * http://www.gnu.org/copyleft/gpl.html
- *
- * @file
- * @deprecated since 1.27, use HtmlFormatter\HtmlFormatter
- */
-class HtmlFormatter extends HtmlFormatter\HtmlFormatter {
-}
index beb9de5..a217cd1 100644 (file)
@@ -727,10 +727,12 @@ class MediaWiki {
                if ( function_exists( 'register_postsend_function' ) ) {
                        // https://github.com/facebook/hhvm/issues/1230
                        register_postsend_function( $callback );
+                       /** @noinspection PhpUnusedLocalVariableInspection */
                        $blocksHttpClient = false;
                } else {
                        if ( function_exists( 'fastcgi_finish_request' ) ) {
                                fastcgi_finish_request();
+                               /** @noinspection PhpUnusedLocalVariableInspection */
                                $blocksHttpClient = false;
                        } else {
                                // Either all DB and deferred updates should happen or none.
index b871735..f41ee01 100644 (file)
@@ -5787,9 +5787,9 @@ class Parser {
                global $wgFragmentMode;
                if ( isset( $wgFragmentMode[1] ) && $wgFragmentMode[1] === 'legacy' ) {
                        // ForAttribute() and ForLink() are the same for legacy encoding
-                       $id = Sanitizer::escapeIdForAttribute( $text, Sanitizer::ID_FALLBACK );
+                       $id = Sanitizer::escapeIdForAttribute( $sectionName, Sanitizer::ID_FALLBACK );
                } else {
-                       $id = Sanitizer::escapeIdForLink( $text );
+                       $id = Sanitizer::escapeIdForLink( $sectionName );
                }
 
                return "#$id";
index 466b7c0..c62d6f2 100644 (file)
@@ -44,9 +44,6 @@
 
                                return result;
                        }
-               },
-               featureFlags: {
-                       liveUpdate: mw.config.get( 'StructuredChangeFiltersLiveUpdatePollingRate' )
                }
        };
 }( mediaWiki ) );
index 6be6968..c10011c 100644 (file)
@@ -32,7 +32,7 @@
                } );
 
                this.$element
-                       .addClass( 'mw-rcfilters-ui-changesLimitButtonWidget' );
+                       .addClass( 'mw-rcfilters-ui-changesLimitAndDateButtonWidget' );
        };
 
        /* Initialization */
index 1cd7bef..dba24fc 100644 (file)
@@ -43,7 +43,7 @@
                        this.changesListModel
                );
 
-               this.numChangesWidget = new mw.rcfilters.ui.ChangesLimitAndDateButtonWidget(
+               this.numChangesAndDateWidget = new mw.rcfilters.ui.ChangesLimitAndDateButtonWidget(
                        this.controller,
                        this.model,
                        {
                        classes: [ 'mw-rcfilters-ui-filterWrapperWidget-showNewChanges' ]
                } );
 
+               // Events
+               this.filterTagWidget.menu.connect( this, { toggle: [ 'emit', 'menuToggle' ] } );
+               this.changesListModel.connect( this, { newChangesExist: 'onNewChangesExist' } );
+               this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
+               this.showNewChangesLink.toggle( false );
+
                // Initialize
                this.$top = $( '<div>' )
                        .addClass( 'mw-rcfilters-ui-filterWrapperWidget-top' );
                        .addClass( 'mw-rcfilters-ui-filterWrapperWidget-bottom' )
                        .append(
                                this.showNewChangesLink.$element,
-                               this.numChangesWidget.$element
+                               this.numChangesAndDateWidget.$element
                        );
 
-               if ( mw.rcfilters.featureFlags.liveUpdate ) {
+               if ( mw.config.get( 'StructuredChangeFiltersLiveUpdatePollingRate' ) ) {
                        $bottom.prepend( this.liveUpdateButton.$element );
                }
 
-               // Events
-               this.filterTagWidget.menu.connect( this, { toggle: [ 'emit', 'menuToggle' ] } );
-               this.changesListModel.connect( this, { newChangesExist: 'onNewChangesExist' } );
-               this.showNewChangesLink.connect( this, { click: 'onShowNewChangesClick' } );
-               this.showNewChangesLink.toggle( false );
-
                this.$element
                        .addClass( 'mw-rcfilters-ui-filterWrapperWidget' )
                        .append(
diff --git a/tests/phpunit/includes/api/format/ApiFormatBaseTest.php b/tests/phpunit/includes/api/format/ApiFormatBaseTest.php
new file mode 100644 (file)
index 0000000..d6a1390
--- /dev/null
@@ -0,0 +1,345 @@
+<?php
+
+use Wikimedia\TestingAccessWrapper;
+
+/**
+ * @group API
+ * @covers ApiFormatBase
+ */
+class ApiFormatBaseTest extends ApiFormatTestBase {
+
+       protected $printerName = 'mockbase';
+
+       public function getMockFormatter( ApiMain $main = null, $format, $methods = [] ) {
+               if ( $main === null ) {
+                       $context = new RequestContext;
+                       $context->setRequest( new FauxRequest( [], true ) );
+                       $main = new ApiMain( $context );
+               }
+
+               $mock = $this->getMockBuilder( ApiFormatBase::class )
+                       ->setConstructorArgs( [ $main, $format ] )
+                       ->setMethods( array_unique( array_merge( $methods, [ 'getMimeType', 'execute' ] ) ) )
+                       ->getMock();
+               if ( !in_array( 'getMimeType', $methods, true ) ) {
+                       $mock->method( 'getMimeType' )->willReturn( 'text/x-mock' );
+               }
+               return $mock;
+       }
+
+       protected function encodeData( array $params, array $data, $options = [] ) {
+               $options += [
+                       'name' => 'mock',
+                       'class' => ApiFormatBase::class,
+                       'factory' => function ( ApiMain $main, $format ) use ( $options ) {
+                               $mock = $this->getMockFormatter( $main, $format );
+                               $mock->expects( $this->once() )->method( 'execute' )
+                                       ->willReturnCallback( function () use ( $mock ) {
+                                               $mock->printText( "Format {$mock->getFormat()}: " );
+                                               $mock->printText( "<b>ok</b>" );
+                                       } );
+
+                               if ( isset( $options['status'] ) ) {
+                                       $mock->setHttpStatus( $options['status'] );
+                               }
+
+                               return $mock;
+                       },
+                       'returnPrinter' => true,
+               ];
+
+               $this->setMwGlobals( [
+                       'wgApiFrameOptions' => 'DENY',
+               ] );
+
+               $ret = parent::encodeData( $params, $data, $options );
+               $printer = TestingAccessWrapper::newFromObject( $ret['printer'] );
+               $text = $ret['text'];
+
+               if ( $options['name'] !== 'mockfm' ) {
+                       $ct = 'text/x-mock';
+                       $file = 'api-result.mock';
+                       $status = isset( $options['status'] ) ? $options['status'] : null;
+               } elseif ( isset( $params['wrappedhtml'] ) ) {
+                       $ct = 'text/mediawiki-api-prettyprint-wrapped';
+                       $file = 'api-result-wrapped.json';
+                       $status = null;
+
+                       // Replace varying field
+                       $text = preg_replace( '/"time":\d+/', '"time":1234', $text );
+               } else {
+                       $ct = 'text/html';
+                       $file = 'api-result.html';
+                       $status = null;
+
+                       // Strip OutputPage-generated HTML
+                       if ( preg_match( '!<pre class="api-pretty-content">.*</pre>!s', $text, $m ) ) {
+                               $text = $m[0];
+                       }
+               }
+
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertSame( "$ct; charset=utf-8", strtolower( $response->getHeader( 'Content-Type' ) ) );
+               $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertSame( $file, $printer->getFilename() );
+               $this->assertSame( "inline; filename=\"$file\"", $response->getHeader( 'Content-Disposition' ) );
+               $this->assertSame( $status, $response->getStatusCode() );
+
+               return $text;
+       }
+
+       public static function provideGeneralEncoding() {
+               return [
+                       'normal' => [
+                               [],
+                               "Format MOCK: <b>ok</b>",
+                               [],
+                               [ 'name' => 'mock' ]
+                       ],
+                       'normal ignores wrappedhtml' => [
+                               [],
+                               "Format MOCK: <b>ok</b>",
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mock' ]
+                       ],
+                       'HTML format' => [
+                               [],
+                               '<pre class="api-pretty-content">Format MOCK: &lt;b>ok&lt;/b></pre>',
+                               [],
+                               [ 'name' => 'mockfm' ]
+                       ],
+                       'wrapped HTML format' => [
+                               [],
+                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                               '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mockfm' ]
+                       ],
+                       'normal, with set status' => [
+                               [],
+                               "Format MOCK: <b>ok</b>",
+                               [],
+                               [ 'name' => 'mock', 'status' => 400 ]
+                       ],
+                       'HTML format, with set status' => [
+                               [],
+                               '<pre class="api-pretty-content">Format MOCK: &lt;b>ok&lt;/b></pre>',
+                               [],
+                               [ 'name' => 'mockfm', 'status' => 400 ]
+                       ],
+                       'wrapped HTML format, with set status' => [
+                               [],
+                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                               '{"status":400,"statustext":"Bad Request","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":null,"time":1234}',
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mockfm', 'status' => 400 ]
+                       ],
+                       'wrapped HTML format, cross-domain-policy' => [
+                               [ 'continue' => '< CrOsS-DoMaIn-PoLiCy >' ],
+                               // phpcs:ignore Generic.Files.LineLength.TooLong
+                               '{"status":200,"statustext":"OK","html":"<pre class=\"api-pretty-content\">Format MOCK: &lt;b>ok&lt;/b></pre>","modules":["mediawiki.apipretty"],"continue":"\u003C CrOsS-DoMaIn-PoLiCy \u003E","time":1234}',
+                               [ 'wrappedhtml' => 1 ],
+                               [ 'name' => 'mockfm' ]
+                       ],
+               ];
+       }
+
+       public function testBasics() {
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $this->assertTrue( $printer->canPrintErrors() );
+               $this->assertSame(
+                       'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Data_formats',
+                       $printer->getHelpUrls()
+               );
+       }
+
+       public function testDisable() {
+               $this->setMwGlobals( [
+                       'wgApiFrameOptions' => 'DENY',
+               ] );
+
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
+                       $printer->printText( 'Foo' );
+               } );
+               $this->assertFalse( $printer->isDisabled() );
+               $printer->disable();
+               $this->assertTrue( $printer->isDisabled() );
+
+               $printer->setHttpStatus( 400 );
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $this->assertSame( '', ob_get_clean() );
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertNull( $response->getHeader( 'Content-Type' ) );
+               $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
+               $this->assertNull( $response->getStatusCode() );
+       }
+
+       public function testNullMimeType() {
+               $this->setMwGlobals( [
+                       'wgApiFrameOptions' => 'DENY',
+               ] );
+
+               $printer = $this->getMockFormatter( null, 'mock', [ 'getMimeType' ] );
+               $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
+                       $printer->printText( 'Foo' );
+               } );
+               $printer->method( 'getMimeType' )->willReturn( null );
+               $this->assertNull( $printer->getMimeType(), 'sanity check' );
+
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $this->assertSame( 'Foo', ob_get_clean() );
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertNull( $response->getHeader( 'Content-Type' ) );
+               $this->assertNull( $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertNull( $response->getHeader( 'Content-Disposition' ) );
+
+               $printer = $this->getMockFormatter( null, 'mockfm', [ 'getMimeType' ] );
+               $printer->method( 'execute' )->willReturnCallback( function () use ( $printer ) {
+                       $printer->printText( 'Foo' );
+               } );
+               $printer->method( 'getMimeType' )->willReturn( null );
+               $this->assertNull( $printer->getMimeType(), 'sanity check' );
+               $this->assertTrue( $printer->getIsHtml(), 'sanity check' );
+
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $this->assertSame( 'Foo', ob_get_clean() );
+               $response = $printer->getMain()->getRequest()->response();
+               $this->assertSame(
+                       'text/html; charset=utf-8', strtolower( $response->getHeader( 'Content-Type' ) )
+               );
+               $this->assertSame( 'DENY', $response->getHeader( 'X-Frame-Options' ) );
+               $this->assertSame(
+                       'inline; filename="api-result.html"', $response->getHeader( 'Content-Disposition' )
+               );
+       }
+
+       public function testApiFrameOptions() {
+               $this->setMwGlobals( [ 'wgApiFrameOptions' => 'DENY' ] );
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->initPrinter();
+               $this->assertSame(
+                       'DENY',
+                       $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
+               );
+
+               $this->setMwGlobals( [ 'wgApiFrameOptions' => 'SAMEORIGIN' ] );
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->initPrinter();
+               $this->assertSame(
+                       'SAMEORIGIN',
+                       $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
+               );
+
+               $this->setMwGlobals( [ 'wgApiFrameOptions' => false ] );
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $printer->initPrinter();
+               $this->assertNull(
+                       $printer->getMain()->getRequest()->response()->getHeader( 'X-Frame-Options' )
+               );
+       }
+
+       public function testForceDefaultParams() {
+               $context = new RequestContext;
+               $context->setRequest( new FauxRequest( [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ], true ) );
+               $main = new ApiMain( $context );
+               $allowedParams = [
+                       'foo' => [],
+                       'bar' => [ ApiBase::PARAM_DFLT => 'bar?' ],
+                       'baz' => 'baz!',
+               ];
+
+               $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
+               $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
+               $this->assertEquals(
+                       [ 'foo' => '1', 'bar' => '2', 'baz' => '3' ],
+                       $printer->extractRequestParams(),
+                       'sanity check'
+               );
+
+               $printer = $this->getMockFormatter( $main, 'mock', [ 'getAllowedParams' ] );
+               $printer->method( 'getAllowedParams' )->willReturn( $allowedParams );
+               $printer->forceDefaultParams();
+               $this->assertEquals(
+                       [ 'foo' => null, 'bar' => 'bar?', 'baz' => 'baz!' ],
+                       $printer->extractRequestParams()
+               );
+       }
+
+       public function testGetAllowedParams() {
+               $printer = $this->getMockFormatter( null, 'mock' );
+               $this->assertSame( [], $printer->getAllowedParams() );
+
+               $printer = $this->getMockFormatter( null, 'mockfm' );
+               $this->assertSame( [
+                       'wrappedhtml' => [
+                               ApiBase::PARAM_DFLT => false,
+                               ApiBase::PARAM_HELP_MSG => 'apihelp-format-param-wrappedhtml',
+                       ]
+               ], $printer->getAllowedParams() );
+       }
+
+       public function testGetExamplesMessages() {
+               $printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mock' ) );
+               $this->assertSame( [
+                       'action=query&meta=siteinfo&siprop=namespaces&format=mock'
+                               => [ 'apihelp-format-example-generic', 'MOCK' ]
+               ], $printer->getExamplesMessages() );
+
+               $printer = TestingAccessWrapper::newFromObject( $this->getMockFormatter( null, 'mockfm' ) );
+               $this->assertSame( [
+                       'action=query&meta=siteinfo&siprop=namespaces&format=mockfm'
+                               => [ 'apihelp-format-example-generic', 'MOCK' ]
+               ], $printer->getExamplesMessages() );
+       }
+
+       /**
+        * @dataProvider provideHtmlHeader
+        */
+       public function testHtmlHeader( $post, $registerNonHtml, $expect ) {
+               $context = new RequestContext;
+               $request = new FauxRequest( [ 'a' => 1, 'b' => 2 ], $post );
+               $request->setRequestURL( 'http://example.org/wx/api.php' );
+               $context->setRequest( $request );
+               $context->setLanguage( 'qqx' );
+               $main = new ApiMain( $context );
+               $printer = $this->getMockFormatter( $main, 'mockfm' );
+               $mm = $printer->getMain()->getModuleManager();
+               $mm->addModule( 'mockfm', 'format', ApiFormatBase::class, function () {
+                       return $mock;
+               } );
+               if ( $registerNonHtml ) {
+                       $mm->addModule( 'mock', 'format', ApiFormatBase::class, function () {
+                               return $mock;
+                       } );
+               }
+
+               $printer->initPrinter();
+               $printer->execute();
+               ob_start();
+               $printer->closePrinter();
+               $text = ob_get_clean();
+               $this->assertContains( $expect, $text );
+       }
+
+       public static function provideHtmlHeader() {
+               return [
+                       [ false, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
+                       [ true, false, '(api-format-prettyprint-header-only-html: MOCK)' ],
+                       // phpcs:ignore Generic.Files.LineLength.TooLong
+                       [ false, true, '(api-format-prettyprint-header-hyperlinked: MOCK, mock, <a rel="nofollow" class="external free" href="http://example.org/wx/api.php?a=1&amp;b=2&amp;format=mock">http://example.org/wx/api.php?a=1&amp;b=2&amp;format=mock</a>)' ],
+                       [ true, true, '(api-format-prettyprint-header: MOCK, mock)' ],
+               ];
+       }
+
+}
diff --git a/tests/phpunit/includes/api/format/ApiFormatRawTest.php b/tests/phpunit/includes/api/format/ApiFormatRawTest.php
new file mode 100644 (file)
index 0000000..0d3e63f
--- /dev/null
@@ -0,0 +1,120 @@
+<?php
+
+/**
+ * @group API
+ * @covers ApiFormatRaw
+ */
+class ApiFormatRawTest extends ApiFormatTestBase {
+
+       protected $printerName = 'raw';
+
+       /**
+        * Test basic encoding and missing mime and text exceptions
+        * @return array datasets
+        */
+       public static function provideGeneralEncoding() {
+               $options = [
+                       'class' => 'ApiFormatRaw',
+                       'factory' => function ( ApiMain $main ) {
+                               return new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
+                       }
+               ];
+
+               return [
+                       [
+                               [ 'mime' => 'text/plain', 'text' => 'foo' ],
+                               'foo',
+                               [],
+                               $options
+                       ],
+                       [
+                               [ 'mime' => 'text/plain', 'text' => 'fóo' ],
+                               'fóo',
+                               [],
+                               $options
+                       ],
+                       [
+                               [ 'text' => 'some text' ],
+                               new MWException( 'No MIME type set for raw formatter' ),
+                               [],
+                               $options
+                       ],
+                       [
+                               [ 'mime' => 'text/plain' ],
+                               new MWException( 'No text given for raw formatter' ),
+                               [],
+                               $options
+                       ],
+                       'test error fallback' => [
+                               [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
+                               '{"mime":"text/plain","text":"some text","error":"some error"}',
+                               [],
+                               $options
+                       ]
+               ];
+       }
+
+       /**
+        * Test specifying filename
+        */
+       public function testFilename() {
+               $printer = new ApiFormatRaw( new ApiMain );
+               $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
+               $this->assertSame( 'whatever.raw', $printer->getFilename() );
+       }
+
+       /**
+        * Test specifying filename with error fallback printer
+        */
+       public function testErrorFallbackFilename() {
+               $apiMain = new ApiMain;
+               $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
+               $printer->getResult()->addValue( null, 'error', 'some error' );
+               $printer->getResult()->addValue( null, 'filename', 'whatever.raw' );
+               $this->assertSame( 'api-result.json', $printer->getFilename() );
+       }
+
+       /**
+        * Test specifying mime
+        */
+       public function testMime() {
+               $printer = new ApiFormatRaw( new ApiMain );
+               $printer->getResult()->addValue( null, 'mime', 'text/plain' );
+               $this->assertSame( 'text/plain', $printer->getMimeType() );
+       }
+
+       /**
+        * Test specifying mime with error fallback printer
+        */
+       public function testErrorFallbackMime() {
+               $apiMain = new ApiMain;
+               $printer = new ApiFormatRaw( $apiMain, new ApiFormatJson( $apiMain, 'json' ) );
+               $printer->getResult()->addValue( null, 'error', 'some error' );
+               $printer->getResult()->addValue( null, 'mime', 'text/plain' );
+               $this->assertSame( 'application/json', $printer->getMimeType() );
+       }
+
+       /**
+        * Check that setting failWithHTTPError to true will result in 400 response status code
+        */
+       public function testFailWithHTTPError() {
+               $apiMain = null;
+
+               $this->testGeneralEncoding(
+                       [ 'mime' => 'text/plain', 'text' => 'some text', 'error' => 'some error' ],
+                       '{"mime":"text/plain","text":"some text","error":"some error"}',
+                       [],
+                       [
+                               'class' => 'ApiFormatRaw',
+                               'factory' => function ( ApiMain $main ) use ( &$apiMain ) {
+                                       $apiMain = $main;
+                                       $printer = new ApiFormatRaw( $main, new ApiFormatJson( $main, 'json' ) );
+                                       $printer->setFailWithHTTPError( true );
+                                       return $printer;
+                               }
+                       ]
+               );
+               $this->assertEquals( 400, $apiMain->getRequest()->response()->getStatusCode() );
+       }
+
+}
index fb086e9..4169dab 100644 (file)
@@ -11,26 +11,40 @@ abstract class ApiFormatTestBase extends MediaWikiTestCase {
        /**
         * Return general data to be encoded for testing
         * @return array See self::testGeneralEncoding
-        * @throws Exception
+        * @throws BadMethodCallException
         */
        public static function provideGeneralEncoding() {
-               throw new Exception( 'Subclass must implement ' . __METHOD__ );
+               throw new BadMethodCallException( static::class . ' must implement ' . __METHOD__ );
        }
 
        /**
         * Get the formatter output for the given input data
         * @param array $params Query parameters
         * @param array $data Data to encode
-        * @param string $class Printer class to use instead of the normal one
-        * @return string
+        * @param array $options Options. If passed a string, the string is treated
+        *  as the 'class' option.
+        *  - name: Format name, rather than $this->printerName
+        *  - class: If set, register 'name' with this class (and 'factory', if that's set)
+        *  - factory: Used with 'class' to register at runtime
+        *  - returnPrinter: Return the printer object
+        * @param callable|null $factory Factory to use instead of the normal one
+        * @return string|array The string if $options['returnPrinter'] isn't set, or an array if it is:
+        *  - text: Output text string
+        *  - printer: ApiFormatBase
         * @throws Exception
         */
-       protected function encodeData( array $params, array $data, $class = null ) {
+       protected function encodeData( array $params, array $data, $options = [] ) {
+               if ( is_string( $options ) ) {
+                       $options = [ 'class' => $options ];
+               }
+               $printerName = isset( $options['name'] ) ? $options['name'] : $this->printerName;
+
                $context = new RequestContext;
                $context->setRequest( new FauxRequest( $params, true ) );
                $main = new ApiMain( $context );
-               if ( $class !== null ) {
-                       $main->getModuleManager()->addModule( $this->printerName, 'format', $class );
+               if ( isset( $options['class'] ) ) {
+                       $factory = isset( $options['factory'] ) ? $options['factory'] : null;
+                       $main->getModuleManager()->addModule( $printerName, 'format', $options['class'], $factory );
                }
                $result = $main->getResult();
                $result->addArrayType( null, 'default' );
@@ -38,27 +52,42 @@ abstract class ApiFormatTestBase extends MediaWikiTestCase {
                        $result->addValue( null, $k, $v );
                }
 
-               $printer = $main->createPrinterByName( $this->printerName );
+               $ret = [];
+               $printer = $main->createPrinterByName( $printerName );
                $printer->initPrinter();
                $printer->execute();
                ob_start();
                try {
                        $printer->closePrinter();
-                       return ob_get_clean();
+                       $ret['text'] = ob_get_clean();
                } catch ( Exception $ex ) {
                        ob_end_clean();
                        throw $ex;
                }
+
+               if ( !empty( $options['returnPrinter'] ) ) {
+                       $ret['printer'] = $printer;
+               }
+
+               return count( $ret ) === 1 ? $ret['text'] : $ret;
        }
 
        /**
         * @dataProvider provideGeneralEncoding
+        * @param array $data Data to be encoded
+        * @param string|Exception $expect String to expect, or exception expected to be thrown
+        * @param array $params Query parameters to set in the FauxRequest
+        * @param array $options Options to pass to self::encodeData()
         */
-       public function testGeneralEncoding( array $data, $expect, array $params = [] ) {
-               if ( isset( $params['SKIP'] ) ) {
-                       $this->markTestSkipped( $expect );
+       public function testGeneralEncoding(
+               array $data, $expect, array $params = [], array $options = []
+       ) {
+               if ( $expect instanceof Exception ) {
+                       $this->setExpectedException( get_class( $expect ), $expect->getMessage() );
+                       $this->encodeData( $params, $data, $options ); // Should throw
+               } else {
+                       $this->assertSame( $expect, $this->encodeData( $params, $data, $options ) );
                }
-               $this->assertSame( $expect, $this->encodeData( $params, $data ) );
        }
 
 }