$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(
);
}
+ /**
+ * @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
$formatter = new ApiErrorFormatter_BackCompat( $result );
$this->assertSame( 'en', $formatter->getLanguage()->getCode() );
+ $this->assertSame( 'bc', $formatter->getFormat() );
$this->assertSame( [], $formatter->arrayFromStatus( Status::newGood() ) );
* @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 );
* @param array $expect
*/
public function testGetMessageFromException_BC( $exception, $options, $expect ) {
- if ( $exception instanceof UsageException ) {
- $this->hideDeprecated( 'UsageException::getMessageArray' );
- }
-
$result = new ApiResult( 8388608 );
$formatter = new ApiErrorFormatter_BackCompat( $result );
}
public static function provideGetMessageFromException() {
- Wikimedia\suppressWarnings();
- $usageException = new UsageException(
- '<b>Something broke!</b>', 'ue-code', 0, [ 'xxx' => 'yyy', 'baz' => 23 ]
- );
- Wikimedia\restoreWarnings();
-
return [
'Normal exception' => [
new RuntimeException( '<b>Something broke!</b>' ),
[
'text' => '<b>Something broke!</b>',
'code' => 'internal_api_error_RuntimeException',
- 'data' => [],
+ 'data' => [
+ 'errorclass' => 'RuntimeException',
+ ],
]
],
'Normal exception, wrapped' => [
'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', '<b>FooBar</b>' ] ),
[],
];
}
+ /**
+ * @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 ],
+ ];
+ }
+
}