From: TK-999 Date: Wed, 15 Feb 2017 10:34:52 +0000 (+0100) Subject: In Autopromote skip edit count lookup if requirement is 0 or invalid X-Git-Tag: 1.31.0-rc.0~3194^2 X-Git-Url: http://git.cyclocoop.org/%22%20.%20generer_url_ecrire%28%22articles_versions%22%2C%22id_article=%24id_article%22%29%20.%20%22?a=commitdiff_plain;h=c182df6e38d50c449ff9c4d329088e5266ce041d;p=lhc%2Fweb%2Fwiklou.git In Autopromote skip edit count lookup if requirement is 0 or invalid Autopromote makes a DB call to fetch user edit count when checking edit count requirements. We can skip this call if requirement is set to 0 or invalid (less than 0). Bug: T157718 Change-Id: I7bcfa6e7e4991fe7b48bef84ad24621564261abc --- diff --git a/includes/Autopromote.php b/includes/Autopromote.php index 56fbb07f88..a01465e9ae 100644 --- a/includes/Autopromote.php +++ b/includes/Autopromote.php @@ -177,7 +177,13 @@ class Autopromote { } return false; case APCOND_EDITCOUNT: - return $user->getEditCount() >= $cond[1]; + $reqEditCount = $cond[1]; + + // T157718: Avoid edit count lookup if specified edit count is 0 or invalid + if ( $reqEditCount <= 0 ) { + return true; + } + return $user->getEditCount() >= $reqEditCount; case APCOND_AGE: $age = time() - wfTimestampOrNull( TS_UNIX, $user->getRegistration() ); return $age >= $cond[1]; diff --git a/tests/phpunit/includes/AutopromoteTest.php b/tests/phpunit/includes/AutopromoteTest.php new file mode 100644 index 0000000000..785aa4e3d4 --- /dev/null +++ b/tests/phpunit/includes/AutopromoteTest.php @@ -0,0 +1,53 @@ +setMwGlobals( [ + 'wgAutopromote' => [ + 'autoconfirmed' => [ APCOND_EDITCOUNT, $requirement ] + ] + ] ); + + /** @var PHPUnit_Framework_MockObject_MockObject|User $userMock */ + $userMock = $this->getMock( 'User', [ 'getEditCount' ] ); + if ( $requirement > 0 ) { + $userMock->expects( $this->once() ) + ->method( 'getEditCount' ) + ->willReturn( $editCount ); + } else { + $userMock->expects( $this->never() ) + ->method( 'getEditCount' ); + } + + $result = Autopromote::getAutopromoteGroups( $userMock ); + if ( $editCount >= $requirement ) { + $this->assertContains( + 'autoconfirmed', + $result, + 'User must be promoted if they meet edit count requirement' + ); + } else { + $this->assertNotContains( + 'autoconfirmed', + $result, + 'User must not be promoted if they fail edit count requirement' + ); + } + } + + public static function provideEditCountsAndRequirements() { + return [ + 'user with sufficient editcount' => [ 100, 10 ], + 'user with insufficient editcount' => [ 4, 10 ], + 'edit count requirement set to 0' => [ 1, 0 ], + ]; + } +}