ApiMain: Always create a new printer in getPrinterByName()
authorBrad Jorsch <bjorsch@wikimedia.org>
Thu, 19 Jul 2018 13:24:48 +0000 (09:24 -0400)
committerBrad Jorsch <bjorsch@wikimedia.org>
Thu, 19 Jul 2018 14:45:28 +0000 (10:45 -0400)
ApiMain already caches the printer in ->mPrinter, so if
getPrinterByName() is being called more than once that's because we
really want a new printer instance, without any cached errors or other
behavior that results from reusing the same instance.

Bug: T199949
Change-Id: I779cbbaa8aab9b049a8eed732416edd828121ec4

includes/api/ApiMain.php
tests/phpunit/includes/api/ApiMainTest.php

index 610ecf5..b398ecd 100644 (file)
@@ -486,7 +486,7 @@ class ApiMain extends ApiBase {
         * @return ApiFormatBase
         */
        public function createPrinterByName( $format ) {
-               $printer = $this->mModuleMgr->getModule( $format, 'format' );
+               $printer = $this->mModuleMgr->getModule( $format, 'format', /* $ignoreCache */ true );
                if ( $printer === null ) {
                        $this->dieWithError(
                                [ 'apierror-unknownformat', wfEscapeWikiText( $format ) ], 'unknown_format'
index f06d97e..b7869e6 100644 (file)
@@ -1098,4 +1098,21 @@ class ApiMainTest extends ApiTestCase {
                        ],
                ];
        }
+
+       public function testPrinterParameterValidationError() {
+               $api = $this->getNonInternalApiMain( [
+                       'action' => 'query', 'meta' => 'siteinfo', 'format' => 'json', 'formatversion' => 'bogus',
+               ] );
+
+               ob_start();
+               $api->execute();
+               $txt = ob_get_clean();
+
+               // Test that the actual output is valid JSON, not just the format of the ApiResult.
+               $data = FormatJson::decode( $txt, true );
+               $this->assertInternalType( 'array', $data );
+               $this->assertArrayHasKey( 'error', $data );
+               $this->assertArrayHasKey( 'code', $data['error'] );
+               $this->assertSame( 'unknown_formatversion', $data['error']['code'] );
+       }
 }