X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2FApiErrorFormatterTest.php;h=1b7f6bff5737efa7acb083c458c806d63d07c7ae;hb=9e07891ca7cd2f83004206c637e50da2a0419a31;hp=d13b00be2e1a41cb97f0fcc61e2b0917e8885959;hpb=29735c97d36258a63c9d2de7e105ef1fc34ed71b;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/ApiErrorFormatterTest.php b/tests/phpunit/includes/api/ApiErrorFormatterTest.php index d13b00be2e..1b7f6bff57 100644 --- a/tests/phpunit/includes/api/ApiErrorFormatterTest.php +++ b/tests/phpunit/includes/api/ApiErrorFormatterTest.php @@ -5,6 +5,30 @@ */ class ApiErrorFormatterTest extends MediaWikiLangTestCase { + /** + * @covers ApiErrorFormatter + */ + public function testErrorFormatterBasics() { + $result = new ApiResult( 8388608 ); + $formatter = new ApiErrorFormatter( $result, Language::factory( 'de' ), 'wikitext', false ); + $this->assertSame( 'de', $formatter->getLanguage()->getCode() ); + + $formatter->addMessagesFromStatus( null, Status::newGood() ); + $this->assertSame( + [ ApiResult::META_TYPE => 'assoc' ], + $result->getResultData() + ); + + $this->assertSame( [], $formatter->arrayFromStatus( Status::newGood() ) ); + + $wrappedFormatter = TestingAccessWrapper::newFromObject( $formatter ); + $this->assertSame( + 'Blah "kbd" 😊', + $wrappedFormatter->stripMarkup( 'Blah kbd <X> 😊' ), + 'stripMarkup' + ); + } + /** * @covers ApiErrorFormatter * @dataProvider provideErrorFormatter @@ -22,7 +46,7 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $formatter->addWarning( 'string', 'mainpage' ); $formatter->addError( 'err', 'mainpage' ); - $this->assertSame( $expect1, $result->getResultData(), 'Simple test' ); + $this->assertEquals( $expect1, $result->getResultData(), 'Simple test' ); $result->reset(); $formatter->addWarning( 'foo', 'mainpage' ); @@ -35,6 +59,17 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $formatter->addError( 'errWithData', $msg2 ); $this->assertSame( $expect2, $result->getResultData(), 'Complex test' ); + $this->assertEquals( + $this->removeModuleTag( $expect2['warnings'][2] ), + $formatter->formatMessage( $msg1 ), + 'formatMessage test 1' + ); + $this->assertEquals( + $this->removeModuleTag( $expect2['warnings'][3] ), + $formatter->formatMessage( $msg2 ), + 'formatMessage test 2' + ); + $result->reset(); $status = Status::newGood(); $status->warning( 'mainpage' ); @@ -47,245 +82,256 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $this->assertSame( $expect3, $result->getResultData(), 'Status test' ); $this->assertSame( - $expect3['errors']['status'], + array_map( [ $this, 'removeModuleTag' ], $expect3['errors'] ), $formatter->arrayFromStatus( $status, 'error' ), 'arrayFromStatus test for error' ); $this->assertSame( - $expect3['warnings']['status'], + array_map( [ $this, 'removeModuleTag' ], $expect3['warnings'] ), $formatter->arrayFromStatus( $status, 'warning' ), 'arrayFromStatus test for warning' ); } + private function removeModuleTag( $s ) { + if ( is_array( $s ) ) { + unset( $s['module'] ); + } + return $s; + } + public static function provideErrorFormatter() { - $mainpagePlain = wfMessage( 'mainpage' )->useDatabase( false )->plain(); - $parensPlain = wfMessage( 'parentheses', 'foobar' )->useDatabase( false )->plain(); - $mainpageText = wfMessage( 'mainpage' )->inLanguage( 'de' )->text(); - $parensText = wfMessage( 'parentheses', 'foobar' )->inLanguage( 'de' )->text(); + $mainpageText = wfMessage( 'mainpage' )->inLanguage( 'de' )->useDatabase( false )->text(); + $parensText = wfMessage( 'parentheses', 'foobar' )->inLanguage( 'de' ) + ->useDatabase( false )->text(); + $mainpageHTML = wfMessage( 'mainpage' )->inLanguage( 'en' )->parse(); + $parensHTML = wfMessage( 'parentheses', 'foobar' )->inLanguage( 'en' )->parse(); $C = ApiResult::META_CONTENT; $I = ApiResult::META_INDEXED_TAG_NAME; + $overriddenData = [ 'overriddenData' => true, ApiResult::META_TYPE => 'assoc' ]; return [ - [ 'wikitext', 'de', true, + $tmp = [ 'wikitext', 'de', false, [ 'errors' => [ - 'err' => [ - [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ], - $I => 'error', - ], + [ 'code' => 'mainpage', 'text' => $mainpageText, 'module' => 'err', $C => 'text' ], + $I => 'error', ], 'warnings' => [ - 'string' => [ - [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ], - $I => 'warning', - ], + [ 'code' => 'mainpage', 'text' => $mainpageText, 'module' => 'string', $C => 'text' ], + $I => 'warning', ], ], [ 'errors' => [ - 'errWithData' => [ - [ 'code' => 'overriddenCode', 'text' => $mainpageText, - 'overriddenData' => true, $C => 'text' ], - $I => 'error', - ], + [ 'code' => 'overriddenCode', 'text' => $mainpageText, + 'data' => $overriddenData, 'module' => 'errWithData', $C => 'text' ], + $I => 'error', ], 'warnings' => [ - 'messageWithData' => [ - [ 'code' => 'overriddenCode', 'text' => $mainpageText, - 'overriddenData' => true, $C => 'text' ], - $I => 'warning', - ], - 'message' => [ - [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ], - $I => 'warning', - ], - 'foo' => [ - [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ], - [ 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ], - $I => 'warning', - ], + [ 'code' => 'mainpage', 'text' => $mainpageText, 'module' => 'foo', $C => 'text' ], + [ 'code' => 'parentheses', 'text' => $parensText, 'module' => 'foo', $C => 'text' ], + [ 'code' => 'mainpage', 'text' => $mainpageText, 'module' => 'message', $C => 'text' ], + [ 'code' => 'overriddenCode', 'text' => $mainpageText, + 'data' => $overriddenData, 'module' => 'messageWithData', $C => 'text' ], + $I => 'warning', ], ], [ 'errors' => [ - 'status' => [ - [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ], - [ 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ], - $I => 'error', - ], + [ 'code' => 'mainpage', 'text' => $mainpageText, 'module' => 'status', $C => 'text' ], + [ 'code' => 'parentheses', 'text' => $parensText, 'module' => 'status', $C => 'text' ], + $I => 'error', ], 'warnings' => [ - 'status' => [ - [ 'code' => 'mainpage', 'text' => $mainpageText, $C => 'text' ], - [ 'code' => 'parentheses', 'text' => $parensText, $C => 'text' ], - [ 'code' => 'overriddenCode', 'text' => $mainpageText, - 'overriddenData' => true, $C => 'text' ], - $I => 'warning', - ], + [ 'code' => 'mainpage', 'text' => $mainpageText, 'module' => 'status', $C => 'text' ], + [ 'code' => 'parentheses', 'text' => $parensText, 'module' => 'status', $C => 'text' ], + [ 'code' => 'overriddenCode', 'text' => $mainpageText, + 'data' => $overriddenData, 'module' => 'status', $C => 'text' ], + $I => 'warning', + ], + ], + ], + [ 'plaintext' ] + $tmp, // For these messages, plaintext and wikitext are the same + [ 'html', 'en', true, + [ + 'errors' => [ + [ 'code' => 'mainpage', 'html' => $mainpageHTML, 'module' => 'err', $C => 'html' ], + $I => 'error', + ], + 'warnings' => [ + [ 'code' => 'mainpage', 'html' => $mainpageHTML, 'module' => 'string', $C => 'html' ], + $I => 'warning', + ], + ], + [ + 'errors' => [ + [ 'code' => 'overriddenCode', 'html' => $mainpageHTML, + 'data' => $overriddenData, 'module' => 'errWithData', $C => 'html' ], + $I => 'error', + ], + 'warnings' => [ + [ 'code' => 'mainpage', 'html' => $mainpageHTML, 'module' => 'foo', $C => 'html' ], + [ 'code' => 'parentheses', 'html' => $parensHTML, 'module' => 'foo', $C => 'html' ], + [ 'code' => 'mainpage', 'html' => $mainpageHTML, 'module' => 'message', $C => 'html' ], + [ 'code' => 'overriddenCode', 'html' => $mainpageHTML, + 'data' => $overriddenData, 'module' => 'messageWithData', $C => 'html' ], + $I => 'warning', + ], + ], + [ + 'errors' => [ + [ 'code' => 'mainpage', 'html' => $mainpageHTML, 'module' => 'status', $C => 'html' ], + [ 'code' => 'parentheses', 'html' => $parensHTML, 'module' => 'status', $C => 'html' ], + $I => 'error', + ], + 'warnings' => [ + [ 'code' => 'mainpage', 'html' => $mainpageHTML, 'module' => 'status', $C => 'html' ], + [ 'code' => 'parentheses', 'html' => $parensHTML, 'module' => 'status', $C => 'html' ], + [ 'code' => 'overriddenCode', 'html' => $mainpageHTML, + 'data' => $overriddenData, 'module' => 'status', $C => 'html' ], + $I => 'warning', ], ], ], [ 'raw', 'fr', true, [ 'errors' => [ - 'err' => [ - [ - 'code' => 'mainpage', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ] - ], - $I => 'error', + [ + 'code' => 'mainpage', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'module' => 'err', ], + $I => 'error', ], 'warnings' => [ - 'string' => [ - [ - 'code' => 'mainpage', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ] - ], - $I => 'warning', + [ + 'code' => 'mainpage', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'module' => 'string', ], + $I => 'warning', ], ], [ 'errors' => [ - 'errWithData' => [ - [ - 'code' => 'overriddenCode', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ], - 'overriddenData' => true - ], - $I => 'error', + [ + 'code' => 'overriddenCode', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'data' => $overriddenData, + 'module' => 'errWithData', ], + $I => 'error', ], 'warnings' => [ - 'messageWithData' => [ - [ - 'code' => 'overriddenCode', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ], - 'overriddenData' => true - ], - $I => 'warning', + [ + 'code' => 'mainpage', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'module' => 'foo', + ], + [ + 'code' => 'parentheses', + 'key' => 'parentheses', + 'params' => [ 'foobar', $I => 'param' ], + 'module' => 'foo', ], - 'message' => [ - [ - 'code' => 'mainpage', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ] - ], - $I => 'warning', + [ + 'code' => 'mainpage', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'module' => 'message', ], - 'foo' => [ - [ - 'code' => 'mainpage', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ] - ], - [ - 'code' => 'parentheses', - 'key' => 'parentheses', - 'params' => [ 'foobar', $I => 'param' ] - ], - $I => 'warning', + [ + 'code' => 'overriddenCode', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'data' => $overriddenData, + 'module' => 'messageWithData', ], + $I => 'warning', ], ], [ 'errors' => [ - 'status' => [ - [ - 'code' => 'mainpage', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ] - ], - [ - 'code' => 'parentheses', - 'key' => 'parentheses', - 'params' => [ 'foobar', $I => 'param' ] - ], - $I => 'error', + [ + 'code' => 'mainpage', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'module' => 'status', + ], + [ + 'code' => 'parentheses', + 'key' => 'parentheses', + 'params' => [ 'foobar', $I => 'param' ], + 'module' => 'status', ], + $I => 'error', ], 'warnings' => [ - 'status' => [ - [ - 'code' => 'mainpage', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ] - ], - [ - 'code' => 'parentheses', - 'key' => 'parentheses', - 'params' => [ 'foobar', $I => 'param' ] - ], - [ - 'code' => 'overriddenCode', - 'key' => 'mainpage', - 'params' => [ $I => 'param' ], - 'overriddenData' => true - ], - $I => 'warning', + [ + 'code' => 'mainpage', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'module' => 'status', ], + [ + 'code' => 'parentheses', + 'key' => 'parentheses', + 'params' => [ 'foobar', $I => 'param' ], + 'module' => 'status', + ], + [ + 'code' => 'overriddenCode', + 'key' => 'mainpage', + 'params' => [ $I => 'param' ], + 'data' => $overriddenData, + 'module' => 'status', + ], + $I => 'warning', ], ], ], [ 'none', 'fr', true, [ 'errors' => [ - 'err' => [ - [ 'code' => 'mainpage' ], - $I => 'error', - ], + [ 'code' => 'mainpage', 'module' => 'err' ], + $I => 'error', ], 'warnings' => [ - 'string' => [ - [ 'code' => 'mainpage' ], - $I => 'warning', - ], + [ 'code' => 'mainpage', 'module' => 'string' ], + $I => 'warning', ], ], [ 'errors' => [ - 'errWithData' => [ - [ 'code' => 'overriddenCode', 'overriddenData' => true ], - $I => 'error', - ], + [ 'code' => 'overriddenCode', 'data' => $overriddenData, + 'module' => 'errWithData' ], + $I => 'error', ], 'warnings' => [ - 'messageWithData' => [ - [ 'code' => 'overriddenCode', 'overriddenData' => true ], - $I => 'warning', - ], - 'message' => [ - [ 'code' => 'mainpage' ], - $I => 'warning', - ], - 'foo' => [ - [ 'code' => 'mainpage' ], - [ 'code' => 'parentheses' ], - $I => 'warning', - ], + [ 'code' => 'mainpage', 'module' => 'foo' ], + [ 'code' => 'parentheses', 'module' => 'foo' ], + [ 'code' => 'mainpage', 'module' => 'message' ], + [ 'code' => 'overriddenCode', 'data' => $overriddenData, + 'module' => 'messageWithData' ], + $I => 'warning', ], ], [ 'errors' => [ - 'status' => [ - [ 'code' => 'mainpage' ], - [ 'code' => 'parentheses' ], - $I => 'error', - ], + [ 'code' => 'mainpage', 'module' => 'status' ], + [ 'code' => 'parentheses', 'module' => 'status' ], + $I => 'error', ], 'warnings' => [ - 'status' => [ - [ 'code' => 'mainpage' ], - [ 'code' => 'parentheses' ], - [ 'code' => 'overriddenCode', 'overriddenData' => true ], - $I => 'warning', - ], + [ 'code' => 'mainpage', 'module' => 'status' ], + [ 'code' => 'parentheses', 'module' => 'status' ], + [ 'code' => 'overriddenCode', 'data' => $overriddenData, 'module' => 'status' ], + $I => 'warning', ], ], ], @@ -302,7 +348,14 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $result = new ApiResult( 8388608 ); $formatter = new ApiErrorFormatter_BackCompat( $result ); + $this->assertSame( 'en', $formatter->getLanguage()->getCode() ); + + $this->assertSame( [], $formatter->arrayFromStatus( Status::newGood() ) ); + $formatter->addWarning( 'string', 'mainpage' ); + $formatter->addWarning( 'raw', + new RawMessage( 'Blah kbd <X> 😞' ) + ); $formatter->addError( 'err', 'mainpage' ); $this->assertSame( [ 'error' => [ @@ -310,6 +363,10 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { 'info' => $mainpagePlain, ], 'warnings' => [ + 'raw' => [ + 'warnings' => 'Blah "kbd" 😞', + ApiResult::META_CONTENT => 'warnings', + ], 'string' => [ 'warnings' => $mainpagePlain, ApiResult::META_CONTENT => 'warnings', @@ -321,12 +378,13 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $result->reset(); $formatter->addWarning( 'foo', 'mainpage' ); $formatter->addWarning( 'foo', 'mainpage' ); - $formatter->addWarning( 'foo', [ 'parentheses', 'foobar' ] ); + $formatter->addWarning( 'xxx+foo', [ 'parentheses', 'foobar' ] ); $msg1 = wfMessage( 'mainpage' ); $formatter->addWarning( 'message', $msg1 ); $msg2 = new ApiMessage( 'mainpage', 'overriddenCode', [ 'overriddenData' => true ] ); $formatter->addWarning( 'messageWithData', $msg2 ); $formatter->addError( 'errWithData', $msg2 ); + $formatter->addWarning( null, 'mainpage' ); $this->assertSame( [ 'error' => [ 'code' => 'overriddenCode', @@ -334,6 +392,10 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { 'overriddenData' => true, ], 'warnings' => [ + 'unknown' => [ + 'warnings' => $mainpagePlain, + ApiResult::META_CONTENT => 'warnings', + ], 'messageWithData' => [ 'warnings' => $mainpagePlain, ApiResult::META_CONTENT => 'warnings', @@ -350,6 +412,22 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { ApiResult::META_TYPE => 'assoc', ], $result->getResultData(), 'Complex test' ); + $this->assertSame( + [ + 'code' => 'mainpage', + 'info' => 'Main Page', + ], + $formatter->formatMessage( $msg1 ) + ); + $this->assertSame( + [ + 'code' => 'overriddenCode', + 'info' => 'Main Page', + 'overriddenData' => true, + ], + $formatter->formatMessage( $msg2 ) + ); + $result->reset(); $status = Status::newGood(); $status->warning( 'mainpage' ); @@ -377,14 +455,16 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $this->assertSame( [ [ - 'type' => 'error', 'message' => 'mainpage', - 'params' => [ $I => 'param' ] + 'params' => [ $I => 'param' ], + 'code' => 'mainpage', + 'type' => 'error', ], [ - 'type' => 'error', 'message' => 'parentheses', - 'params' => [ 'foobar', $I => 'param' ] + 'params' => [ 'foobar', $I => 'param' ], + 'code' => 'parentheses', + 'type' => 'error', ], $I => 'error', ], @@ -394,24 +474,28 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase { $this->assertSame( [ [ - 'type' => 'warning', 'message' => 'mainpage', - 'params' => [ $I => 'param' ] + 'params' => [ $I => 'param' ], + 'code' => 'mainpage', + 'type' => 'warning', ], [ - 'type' => 'warning', 'message' => 'parentheses', - 'params' => [ 'foobar', $I => 'param' ] + 'params' => [ 'foobar', $I => 'param' ], + 'code' => 'parentheses', + 'type' => 'warning', ], [ 'message' => 'mainpage', 'params' => [ $I => 'param' ], - 'type' => 'warning' + 'code' => 'mainpage', + 'type' => 'warning', ], [ 'message' => 'mainpage', 'params' => [ $I => 'param' ], - 'type' => 'warning' + 'code' => 'overriddenCode', + 'type' => 'warning', ], $I => 'warning', ],