Debian Codegraph @

tests/phpunit/unit/includes/Permissions/RateLimitSubjectTest.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 * @since 1.42
20 */
21
22 namespace MediaWiki\Tests\Unit\Permissions;
23
24 use MediaWiki\Permissions\RateLimitSubject;
25 use MediaWiki\User\UserIdentityValue;
26 use MediaWikiUnitTestCase;
27
28 /**
29 * @group Permissions
30 * @covers \MediaWiki\Permissions\RateLimitSubject
31 */
32 class RateLimitSubjectTest extends MediaWikiUnitTestCase {
33
34 /**
35 * Provide data for testConstructorAndGetters
36 */
37 public static function provideIp(): array {
38 return [
39 'null' => [ null, true ],
40 'empty' => [ '', true ],
41 'ipv4' => [ '10.0.0.1', true ],
42 'ipv6' => [ '2001:db8:1::', true ],
43 ];
44 }
45
46 /**
47 * Test that constructing a RateLimitSubject with different IPs and then fetching properties through getters
48 * returns the values passed to the constructor.
49 * @dataProvider provideIp
50 */
51 public function testConstructorAndGetters( $ip, $expected ) {
52 $actor = new UserIdentityValue( 12, 'Test' );
53 $flags = [ 'foo' => true, 'bar' => false ];
54
55 $subject = new RateLimitSubject( $actor, $ip, $flags );
56 $this->assertInstanceOf( RateLimitSubject::class, $subject );
57
58 $this->assertSame( $actor, $subject->getUser(), 'The getUser method did not return the expected actor.' );
59 $this->assertSame( $expected ? $ip : null, $subject->getIp(),
60 'The getIp method did not return the expected IP.' );
61 $this->assertTrue( $subject->is( 'foo' ), 'The is method did not return true for flag "foo".' );
62 $this->assertFalse( $subject->is( 'bar' ), 'The is method did not return false for flag "bar".' );
63 $this->assertFalse( $subject->is( 'baz' ),
64 'The is method returned true for an undefined flag "baz", expected false.' );
65 }
66
67 }