Merge "Don't generate RC entries for filterable log types"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiMainTest.php
index 584c60c..20d758e 100644 (file)
@@ -434,6 +434,35 @@ class ApiMainTest extends ApiTestCase {
                }
        }
 
+       /**
+        * Test that 'assert' is processed before module errors
+        */
+       public function testAssertBeforeModule() {
+               // Sanity check that the query without assert throws too-many-titles
+               try {
+                       $this->doApiRequest( [
+                               'action' => 'query',
+                               'titles' => implode( '|', range( 1, ApiBase::LIMIT_SML1 + 1 ) ),
+                       ], null, null, new User );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( ApiUsageException $e ) {
+                       $this->assertTrue( self::apiExceptionHasCode( $e, 'too-many-titles' ), 'sanity check' );
+               }
+
+               // Now test that the assert happens first
+               try {
+                       $this->doApiRequest( [
+                               'action' => 'query',
+                               'titles' => implode( '|', range( 1, ApiBase::LIMIT_SML1 + 1 ) ),
+                               'assert' => 'user',
+                       ], null, null, new User );
+                       $this->fail( 'Expected exception not thrown' );
+               } catch ( ApiUsageException $e ) {
+                       $this->assertTrue( self::apiExceptionHasCode( $e, 'assertuserfailed' ),
+                               "Error '{$e->getMessage()}' matched expected 'assertuserfailed'" );
+               }
+       }
+
        /**
         * Test if all classes in the main module manager exists
         */
@@ -938,8 +967,7 @@ class ApiMainTest extends ApiTestCase {
                $context->setLanguage( 'en' );
                $context->setConfig( new MultiConfig( [
                        new HashConfig( [
-                               'ShowHostnames' => true, 'ShowSQLErrors' => false,
-                               'ShowExceptionDetails' => true, 'ShowDBErrorBacktrace' => true,
+                               'ShowHostnames' => true, 'ShowExceptionDetails' => true,
                        ] ),
                        $context->getConfig()
                ] ) );
@@ -1023,7 +1051,8 @@ class ApiMainTest extends ApiTestCase {
                                                [ 'code' => 'existing-error', 'text' => 'existing error', 'module' => 'main' ],
                                                [
                                                        'code' => 'internal_api_error_DBQueryError',
-                                                       'text' => "[$reqId] Database query error.",
+                                                       'text' => "[$reqId] Exception caught: A database query error has occurred. " .
+                                                               "This may indicate a bug in the software.",
                                                ]
                                        ],
                                        'trace' => $dbtrace,
@@ -1069,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'] );
+       }
 }