X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2FApiErrorFormatterTest.php;h=2eec176bfc9b91934a06054212cf1ad7047959ce;hb=7ecbff2360a07755f771c648ed1e27ee3dca8ad5;hp=d47481cbe4f006981fc99bbb1e9cd5c765aee349;hpb=237d3271fd313ebe09858a5c442a91216a7b61cf;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/ApiErrorFormatterTest.php b/tests/phpunit/includes/api/ApiErrorFormatterTest.php index d47481cbe4..2eec176bfc 100644 --- a/tests/phpunit/includes/api/ApiErrorFormatterTest.php +++ b/tests/phpunit/includes/api/ApiErrorFormatterTest.php @@ -14,6 +14,7 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $result = new ApiResult( 8388608 ); $formatter = new ApiErrorFormatter( $result, Language::factory( 'de' ), 'wikitext', false ); $this->assertSame( 'de', $formatter->getLanguage()->getCode() ); + $this->assertSame( 'wikitext', $formatter->getFormat() ); $formatter->addMessagesFromStatus( null, Status::newGood() ); $this->assertSame( @@ -31,6 +32,25 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { ); } + /** + * @covers ApiErrorFormatter + * @covers ApiErrorFormatter_BackCompat + */ + public function testNewWithFormat() { + $result = new ApiResult( 8388608 ); + $formatter = new ApiErrorFormatter( $result, Language::factory( 'de' ), 'wikitext', false ); + $formatter2 = $formatter->newWithFormat( 'html' ); + + $this->assertSame( $formatter->getLanguage(), $formatter2->getLanguage() ); + $this->assertSame( 'html', $formatter2->getFormat() ); + + $formatter3 = new ApiErrorFormatter_BackCompat( $result ); + $formatter4 = $formatter3->newWithFormat( 'html' ); + $this->assertNotInstanceOf( ApiErrorFormatter_BackCompat::class, $formatter4 ); + $this->assertSame( $formatter3->getLanguage(), $formatter4->getLanguage() ); + $this->assertSame( 'html', $formatter4->getFormat() ); + } + /** * @covers ApiErrorFormatter * @dataProvider provideErrorFormatter @@ -351,6 +371,7 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $formatter = new ApiErrorFormatter_BackCompat( $result ); $this->assertSame( 'en', $formatter->getLanguage()->getCode() ); + $this->assertSame( 'bc', $formatter->getFormat() ); $this->assertSame( [], $formatter->arrayFromStatus( Status::newGood() ) ); @@ -526,10 +547,6 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { * @param array $expect */ public function testGetMessageFromException( $exception, $options, $expect ) { - if ( $exception instanceof UsageException ) { - $this->hideDeprecated( 'UsageException::getMessageArray' ); - } - $result = new ApiResult( 8388608 ); $formatter = new ApiErrorFormatter( $result, Language::factory( 'en' ), 'html', false ); @@ -575,12 +592,6 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { } public static function provideGetMessageFromException() { - MediaWiki\suppressWarnings(); - $usageException = new UsageException( - 'Something broke!', 'ue-code', 0, [ 'xxx' => 'yyy', 'baz' => 23 ] - ); - MediaWiki\restoreWarnings(); - return [ 'Normal exception' => [ new RuntimeException( 'Something broke!' ), @@ -588,7 +599,9 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { [ 'text' => '<b>Something broke!</b>', 'code' => 'internal_api_error_RuntimeException', - 'data' => [], + 'data' => [ + 'errorclass' => 'RuntimeException', + ], ] ], 'Normal exception, wrapped' => [ @@ -600,24 +613,6 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { 'data' => [ 'foo' => 'bar', 'baz' => 42 ], ] ], - 'UsageException' => [ - $usageException, - [], - [ - 'text' => '<b>Something broke!</b>', - 'code' => 'ue-code', - 'data' => [ 'xxx' => 'yyy', 'baz' => 23 ], - ] - ], - 'UsageException, wrapped' => [ - $usageException, - [ 'wrap' => 'parentheses', 'code' => 'some-code', 'data' => [ 'foo' => 'bar', 'baz' => 42 ] ], - [ - 'text' => '(<b>Something broke!</b>)', - 'code' => 'some-code', - 'data' => [ 'xxx' => 'yyy', 'baz' => 42, 'foo' => 'bar' ], - ] - ], 'LocalizedException' => [ new LocalizedException( [ 'returnto', 'FooBar' ] ), [], @@ -639,4 +634,62 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { ]; } + /** + * @covers ApiErrorFormatter::addMessagesFromStatus + * @covers ApiErrorFormatter::addWarningOrError + * @covers ApiErrorFormatter::formatMessageInternal + */ + public function testAddMessagesFromStatus_filter() { + $result = new ApiResult( 8388608 ); + $formatter = new ApiErrorFormatter( $result, Language::factory( 'qqx' ), 'plaintext', false ); + + $status = Status::newGood(); + $status->warning( 'mainpage' ); + $status->warning( 'parentheses', 'foobar' ); + $status->warning( wfMessage( 'mainpage' ) ); + $status->error( 'mainpage' ); + $status->error( 'parentheses', 'foobaz' ); + $formatter->addMessagesFromStatus( 'status', $status, [ 'warning', 'error' ], [ 'mainpage' ] ); + $this->assertSame( [ + 'errors' => [ + [ + 'code' => 'parentheses', + 'text' => '(parentheses: foobaz)', + 'module' => 'status', + ApiResult::META_CONTENT => 'text', + ], + ApiResult::META_INDEXED_TAG_NAME => 'error', + ], + 'warnings' => [ + [ + 'code' => 'parentheses', + 'text' => '(parentheses: foobar)', + 'module' => 'status', + ApiResult::META_CONTENT => 'text', + ], + ApiResult::META_INDEXED_TAG_NAME => 'warning', + ], + ApiResult::META_TYPE => 'assoc', + ], $result->getResultData() ); + } + + /** + * @dataProvider provideIsValidApiCode + * @covers ApiErrorFormatter::isValidApiCode + * @param string $code + * @param bool $expect + */ + public function testIsValidApiCode( $code, $expect ) { + $this->assertSame( $expect, ApiErrorFormatter::isValidApiCode( $code ) ); + } + + public static function provideIsValidApiCode() { + return [ + [ 'foo-bar_Baz123', true ], + [ 'foo bar', false ], + [ 'foo\\bar', false ], + [ 'internal_api_error_foo\\bar baz', true ], + ]; + } + }