Merge "Fix and make some types in PHPDoc and JSDoc tags more specific"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiMainTest.php
index c9a3428..ad334e9 100644 (file)
@@ -1,7 +1,10 @@
 <?php
 
+use Wikimedia\TestingAccessWrapper;
+
 /**
  * @group API
+ * @group Database
  * @group medium
  *
  * @covers ApiMain
@@ -42,9 +45,11 @@ class ApiMainTest extends ApiTestCase {
         * @param string|bool $error False if no error expected
         */
        public function testAssert( $registered, $rights, $assert, $error ) {
-               $user = new User();
                if ( $registered ) {
-                       $user->setId( 1 );
+                       $user = $this->getMutableTestUser()->getUser();
+                       $user->load(); // load before setting mRights
+               } else {
+                       $user = new User();
                }
                $user->mRights = $rights;
                try {
@@ -54,7 +59,8 @@ class ApiMainTest extends ApiTestCase {
                        ], null, null, $user );
                        $this->assertFalse( $error ); // That no error was expected
                } catch ( ApiUsageException $e ) {
-                       $this->assertTrue( self::apiExceptionHasCode( $e, $error ) );
+                       $this->assertTrue( self::apiExceptionHasCode( $e, $error ),
+                               "Error '{$e->getMessage()}' matched expected '$error'" );
                }
        }
 
@@ -85,20 +91,15 @@ class ApiMainTest extends ApiTestCase {
         * Test if all classes in the main module manager exists
         */
        public function testClassNamesInModuleManager() {
-               global $wgAutoloadLocalClasses, $wgAutoloadClasses;
-
-               // wgAutoloadLocalClasses has precedence, just like in includes/AutoLoader.php
-               $classes = $wgAutoloadLocalClasses + $wgAutoloadClasses;
-
                $api = new ApiMain(
                        new FauxRequest( [ 'action' => 'query', 'meta' => 'siteinfo' ] )
                );
                $modules = $api->getModuleManager()->getNamesWithClasses();
+
                foreach ( $modules as $name => $class ) {
-                       $this->assertArrayHasKey(
-                               $class,
-                               $classes,
-                               'Class ' . $class . ' for api module ' . $name . ' not in autoloader (with exact case)'
+                       $this->assertTrue(
+                               class_exists( $class ),
+                               'Class ' . $class . ' for api module ' . $name . ' does not exist (with exact case)'
                        );
                }
        }
@@ -335,8 +336,6 @@ class ApiMainTest extends ApiTestCase {
        }
 
        public static function provideApiErrorFormatterCreation() {
-               global $wgContLang;
-
                return [
                        'Default (BC)' => [ [], [
                                'uselang' => 'ru',
@@ -386,9 +385,9 @@ class ApiMainTest extends ApiTestCase {
                        'uselang=content' => [
                                [ 'uselang' => 'content', 'errorformat' => 'plaintext' ],
                                [
-                                       'uselang' => $wgContLang->getCode(),
+                                       'uselang' => 'en',
                                        'class' => ApiErrorFormatter::class,
-                                       'lang' => $wgContLang->getCode(),
+                                       'lang' => 'en',
                                        'format' => 'plaintext',
                                        'usedb' => false,
                                ]
@@ -398,7 +397,7 @@ class ApiMainTest extends ApiTestCase {
                                [
                                        'uselang' => 'ru',
                                        'class' => ApiErrorFormatter::class,
-                                       'lang' => $wgContLang->getCode(),
+                                       'lang' => 'en',
                                        'format' => 'plaintext',
                                        'usedb' => false,
                                ]
@@ -456,7 +455,10 @@ class ApiMainTest extends ApiTestCase {
                $context->setRequest( new FauxRequest( [ 'errorformat' => 'plaintext' ] ) );
                $context->setLanguage( 'en' );
                $context->setConfig( new MultiConfig( [
-                       new HashConfig( [ 'ShowHostnames' => true, 'ShowSQLErrors' => false ] ),
+                       new HashConfig( [
+                               'ShowHostnames' => true, 'ShowSQLErrors' => false,
+                               'ShowExceptionDetails' => true, 'ShowDBErrorBacktrace' => true,
+                       ] ),
                        $context->getConfig()
                ] ) );
 
@@ -475,7 +477,7 @@ class ApiMainTest extends ApiTestCase {
                );
        }
 
-       // Not static so $this->getMock() can be used
+       // Not static so $this can be used
        public function provideExceptionErrors() {
                $reqId = WebRequest::getRequestId();
                $doclink = wfExpandUrl( wfScript( 'api' ) );
@@ -488,7 +490,9 @@ class ApiMainTest extends ApiTestCase {
                        MWExceptionHandler::getRedactedTraceAsString( $ex )
                )->inLanguage( 'en' )->useDatabase( false )->text();
 
-               $dbex = new DBQueryError( $this->getMock( 'IDatabase' ), 'error', 1234, 'SELECT 1', __METHOD__ );
+               $dbex = new DBQueryError(
+                       $this->createMock( 'IDatabase' ),
+                       'error', 1234, 'SELECT 1', __METHOD__ );
                $dbtrace = wfMessage( 'api-exception-trace',
                        get_class( $dbex ),
                        $dbex->getFile(),
@@ -496,6 +500,10 @@ class ApiMainTest extends ApiTestCase {
                        MWExceptionHandler::getRedactedTraceAsString( $dbex )
                )->inLanguage( 'en' )->useDatabase( false )->text();
 
+               MediaWiki\suppressWarnings();
+               $usageEx = new UsageException( 'Usage exception!', 'ue', 0, [ 'foo' => 'bar' ] );
+               MediaWiki\restoreWarnings();
+
                $apiEx1 = new ApiUsageException( null,
                        StatusValue::newFatal( new ApiRawMessage( 'An error', 'sv-error1' ) ) );
                TestingAccessWrapper::newFromObject( $apiEx1 )->modulePath = 'foo+bar';
@@ -541,7 +549,7 @@ class ApiMainTest extends ApiTestCase {
                                ]
                        ],
                        [
-                               new UsageException( 'Usage exception!', 'ue', 0, [ 'foo' => 'bar' ] ),
+                               $usageEx,
                                [ 'existing-error', 'ue' ],
                                [
                                        'warnings' => [
@@ -551,7 +559,9 @@ class ApiMainTest extends ApiTestCase {
                                                [ 'code' => 'existing-error', 'text' => 'existing error', 'module' => 'main' ],
                                                [ 'code' => 'ue', 'text' => "Usage exception!", 'data' => [ 'foo' => 'bar' ] ]
                                        ],
-                                       'docref' => "See $doclink for API usage.",
+                                       'docref' => "See $doclink for API usage. Subscribe to the mediawiki-api-announce mailing " .
+                                               "list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; " .
+                                               "for notice of API deprecations and breaking changes.",
                                        'servedby' => wfHostname(),
                                ]
                        ],
@@ -569,7 +579,9 @@ class ApiMainTest extends ApiTestCase {
                                                [ 'code' => 'sv-error1', 'text' => 'An error', 'module' => 'foo+bar' ],
                                                [ 'code' => 'sv-error2', 'text' => 'Another error', 'module' => 'foo+bar' ],
                                        ],
-                                       'docref' => "See $doclink for API usage.",
+                                       'docref' => "See $doclink for API usage. Subscribe to the mediawiki-api-announce mailing " .
+                                               "list at &lt;https://lists.wikimedia.org/mailman/listinfo/mediawiki-api-announce&gt; " .
+                                               "for notice of API deprecations and breaking changes.",
                                        'servedby' => wfHostname(),
                                ]
                        ],