Debian Codegraph @

tests/phpunit/unit/includes/editpage/Constraint/UserRateLimitConstraintTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
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 * Tests the UserRateLimitConstraint
30 *
31 * @author DannyS712
32 *
33 * @covers \MediaWiki\EditPage\Constraint\UserRateLimitConstraint
34 */
35 class UserRateLimitConstraintTest extends MediaWikiUnitTestCase {
36 use EditConstraintTestTrait;
37
38 /**
39 * @param bool $fail
40 *
41 * @return MockObject&RateLimiter
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 }