Avoid assuming a user with ID 0 exists in ApiMainTest::testAssert
authorAaron Schulz <aschulz@wikimedia.org>
Wed, 10 May 2017 02:25:56 +0000 (19:25 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Wed, 10 May 2017 02:25:56 +0000 (19:25 -0700)
If the load() triggered by User method calls fails, then mId becomes 0
which means there is no "user" right set in getAutomaticGroups().

Bug: T75174
Change-Id: I2d719e4b96c0142e9d408aa2d4f7c5e7a767a754

tests/phpunit/includes/api/ApiMainTest.php

index 3e6ceb7..ea33a9e 100644 (file)
@@ -45,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 {
@@ -57,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'" );
                }
        }