From 78955203c3c1f810cae04c7c92790c99c1de9e7f Mon Sep 17 00:00:00 2001 From: Brad Jorsch Date: Thu, 19 Jul 2018 09:24:48 -0400 Subject: [PATCH] ApiMain: Always create a new printer in getPrinterByName() 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 | 2 +- tests/phpunit/includes/api/ApiMainTest.php | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 610ecf5145..b398ecd04d 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -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' diff --git a/tests/phpunit/includes/api/ApiMainTest.php b/tests/phpunit/includes/api/ApiMainTest.php index f06d97efd9..b7869e689d 100644 --- a/tests/phpunit/includes/api/ApiMainTest.php +++ b/tests/phpunit/includes/api/ApiMainTest.php @@ -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'] ); + } } -- 2.20.1