aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes
diff options
context:
space:
mode:
authordaniel <dkinzler@wikimedia.org>2023-10-27 19:34:38 +0200
committerdaniel <dkinzler@wikimedia.org>2024-01-02 20:14:12 +0100
commitafbad85185329982499d79b45a6e27002eccc50e (patch)
treebe1ae5911444f2b7fd5b64d06e6324f921808437 /tests/phpunit/unit/includes
parent58ca755ce5b7c4faab1386cfe008bba1c63a7b92 (diff)
downloadmediawikicore-afbad85185329982499d79b45a6e27002eccc50e.tar.gz
mediawikicore-afbad85185329982499d79b45a6e27002eccc50e.zip
EditPage: replace usage of User::pingLimiter
Using RateLimiter::limit instead. Change-Id: Ia27cba5023994bfdc61f6d27702eeb98502d6dd4
Diffstat (limited to 'tests/phpunit/unit/includes')
-rw-r--r--tests/phpunit/unit/includes/editpage/Constraint/EditConstraintFactoryTest.php4
-rw-r--r--tests/phpunit/unit/includes/editpage/Constraint/UserRateLimitConstraintTest.php74
2 files changed, 30 insertions, 48 deletions
diff --git a/tests/phpunit/unit/includes/editpage/Constraint/EditConstraintFactoryTest.php b/tests/phpunit/unit/includes/editpage/Constraint/EditConstraintFactoryTest.php
index 6696da59a7f4..a9be685c080b 100644
--- a/tests/phpunit/unit/includes/editpage/Constraint/EditConstraintFactoryTest.php
+++ b/tests/phpunit/unit/includes/editpage/Constraint/EditConstraintFactoryTest.php
@@ -31,6 +31,7 @@ use MediaWiki\HookContainer\HookContainer;
use MediaWiki\Logger\Spi;
use MediaWiki\MainConfigNames;
use MediaWiki\Permissions\PermissionManager;
+use MediaWiki\Permissions\RateLimiter;
use MediaWiki\Title\Title;
use MediaWiki\User\User;
use Psr\Log\NullLogger;
@@ -60,7 +61,8 @@ class EditConstraintFactoryTest extends MediaWikiUnitTestCase {
$this->createMock( PermissionManager::class ),
$this->createMock( HookContainer::class ),
$this->createMock( ReadOnlyMode::class ),
- $this->createMock( SpamChecker::class )
+ $this->createMock( SpamChecker::class ),
+ $this->createMock( RateLimiter::class ),
);
$user = $this->createMock( User::class );
diff --git a/tests/phpunit/unit/includes/editpage/Constraint/UserRateLimitConstraintTest.php b/tests/phpunit/unit/includes/editpage/Constraint/UserRateLimitConstraintTest.php
index bd87b4f065bc..c562bb877a3d 100644
--- a/tests/phpunit/unit/includes/editpage/Constraint/UserRateLimitConstraintTest.php
+++ b/tests/phpunit/unit/includes/editpage/Constraint/UserRateLimitConstraintTest.php
@@ -20,8 +20,10 @@
use MediaWiki\EditPage\Constraint\IEditConstraint;
use MediaWiki\EditPage\Constraint\UserRateLimitConstraint;
-use MediaWiki\Title\Title;
-use MediaWiki\User\User;
+use MediaWiki\Permissions\RateLimiter;
+use MediaWiki\Permissions\RateLimitSubject;
+use MediaWiki\User\UserIdentityValue;
+use PHPUnit\Framework\MockObject\MockObject;
/**
* Tests the UserRateLimitConstraint
@@ -33,61 +35,39 @@ use MediaWiki\User\User;
class UserRateLimitConstraintTest extends MediaWikiUnitTestCase {
use EditConstraintTestTrait;
- public function testPass() {
- // Cannot assert that 0 parameters are passed, since PHP fills in the default
- // values before PHPUnit checks; first call uses both defaults, third call
- // uses the default of 1 for the second parameter
- $user = $this->getMockBuilder( User::class )
- ->onlyMethods( [ 'pingLimiter' ] )
- ->getMock();
- $user->expects( $this->exactly( 3 ) )
- ->method( 'pingLimiter' )
+ /**
+ * @param bool $fail
+ *
+ * @return MockObject&RateLimiter
+ */
+ private function getRateLimiter( $fail ) {
+ $mock = $this->createNoOpMock( RateLimiter::class, [ 'limit' ] );
+ $mock->expects( $this->exactly( 3 ) )
+ ->method( 'limit' )
->withConsecutive(
- [ 'edit', 1 ],
- [ 'linkpurge', 0 ],
- [ 'editcontentmodel', 1 ]
+ [ $this->anything(), 'edit', 1 ],
+ [ $this->anything(), 'linkpurge', 0 ],
+ [ $this->anything(), 'editcontentmodel', 1 ]
)
- ->willReturnOnConsecutiveCalls(
- false,
- false,
- false
- );
+ ->willReturnOnConsecutiveCalls( false, false, $fail );
+ return $mock;
+ }
+
+ public function testPass() {
+ $limiter = $this->getRateLimiter( false );
- $title = $this->createMock( Title::class );
- $title->expects( $this->once() )
- ->method( 'getContentModel' )
- ->willReturn( 'OldContentModel' );
+ $subject = new RateLimitSubject( new UserIdentityValue( 1, 'test' ), null, [] );
- $constraint = new UserRateLimitConstraint( $user, $title, 'NewContentModel' );
+ $constraint = new UserRateLimitConstraint( $limiter, $subject, 'OldContentModel', 'NewContentModel' );
$this->assertConstraintPassed( $constraint );
}
public function testFailure() {
- // Cannot assert that 0 parameters are passed, since PHP fills in the default
- // values before PHPUnit checks; first call uses both defaults, third call
- // uses the default of 1 for the second parameter
- $user = $this->getMockBuilder( User::class )
- ->onlyMethods( [ 'pingLimiter' ] )
- ->getMock();
- $user->expects( $this->exactly( 3 ) )
- ->method( 'pingLimiter' )
- ->withConsecutive(
- [ 'edit', 1 ],
- [ 'linkpurge', 0 ],
- [ 'editcontentmodel', 1 ]
- )
- ->willReturnOnConsecutiveCalls(
- false,
- false,
- true // Only die on the last check
- );
+ $limiter = $this->getRateLimiter( true );
- $title = $this->createMock( Title::class );
- $title->expects( $this->once() )
- ->method( 'getContentModel' )
- ->willReturn( 'OldContentModel' );
+ $subject = new RateLimitSubject( new UserIdentityValue( 1, 'test' ), null, [] );
- $constraint = new UserRateLimitConstraint( $user, $title, 'NewContentModel' );
+ $constraint = new UserRateLimitConstraint( $limiter, $subject, 'OldContentModel', 'NewContentModel' );
$this->assertConstraintFailed( $constraint, IEditConstraint::AS_RATE_LIMITED );
}