Merge "Simplify checking for widgets on special block page"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiErrorFormatterTest.php
index 144586e..2eec176 100644 (file)
@@ -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() ) );
 
@@ -578,7 +599,9 @@ class ApiErrorFormatterTest extends MediaWikiLangTestCase {
                                [
                                        'text' => '<b>Something broke!</b>',
                                        'code' => 'internal_api_error_RuntimeException',
-                                       'data' => [],
+                                       'data' => [
+                                               'errorclass' => 'RuntimeException',
+                                       ],
                                ]
                        ],
                        'Normal exception, wrapped' => [
@@ -611,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 ],
+               ];
+       }
+
 }