| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\EditPage\Constraint\IEditConstraint; |
| 22 |
use MediaWiki\EditPage\Constraint\UserRateLimitConstraint; |
| 23 |
use MediaWiki\Permissions\RateLimiter; |
| 24 |
use MediaWiki\Permissions\RateLimitSubject; |
| 25 |
use MediaWiki\User\UserIdentityValue; |
| 26 |
use PHPUnit\Framework\MockObject\MockObject; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class UserRateLimitConstraintTest extends MediaWikiUnitTestCase { |
| 36 |
use EditConstraintTestTrait; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private function getRateLimiter( $fail ) { |
| 44 |
$mock = $this->createNoOpMock( RateLimiter::class, [ 'limit' ] ); |
| 45 |
$expectedArgs = [ |
| 46 |
[ 'edit', 1, false ], |
| 47 |
[ 'linkpurge', 0, false ], |
| 48 |
[ 'editcontentmodel', 1, $fail ] |
| 49 |
]; |
| 50 |
$mock->expects( $this->exactly( 3 ) ) |
| 51 |
->method( 'limit' ) |
| 52 |
->willReturnCallback( function ( $_, $action, $incrBy ) use ( &$expectedArgs ) { |
| 53 |
$curExpectedArgs = array_shift( $expectedArgs ); |
| 54 |
$this->assertSame( $curExpectedArgs[0], $action ); |
| 55 |
$this->assertSame( $curExpectedArgs[1], $incrBy ); |
| 56 |
return $curExpectedArgs[2]; |
| 57 |
} ); |
| 58 |
return $mock; |
| 59 |
} |
| 60 |
|
| 61 |
public function testPass() { |
| 62 |
$limiter = $this->getRateLimiter( false ); |
| 63 |
|
| 64 |
$subject = new RateLimitSubject( new UserIdentityValue( 1, 'test' ), null, [] ); |
| 65 |
|
| 66 |
$constraint = new UserRateLimitConstraint( $limiter, $subject, 'OldContentModel', 'NewContentModel' ); |
| 67 |
$this->assertConstraintPassed( $constraint ); |
| 68 |
} |
| 69 |
|
| 70 |
public function testFailure() { |
| 71 |
$limiter = $this->getRateLimiter( true ); |
| 72 |
|
| 73 |
$subject = new RateLimitSubject( new UserIdentityValue( 1, 'test' ), null, [] ); |
| 74 |
|
| 75 |
$constraint = new UserRateLimitConstraint( $limiter, $subject, 'OldContentModel', 'NewContentModel' ); |
| 76 |
$this->assertConstraintFailed( $constraint, IEditConstraint::AS_RATE_LIMITED ); |
| 77 |
} |
| 78 |
|
| 79 |
} |