Debian Codegraph @

tests/phpunit/unit/includes/block/BlockUtilsTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Block\AbstractBlock;
4 use MediaWiki\Block\BlockUtils;
5 use MediaWiki\Config\ServiceOptions;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Tests\Unit\DummyServicesTrait;
8 use MediaWiki\User\UserIdentity;
9 use MediaWiki\User\UserIdentityLookup;
10 use MediaWiki\User\UserIdentityValue;
11 use Wikimedia\TestingAccessWrapper;
12
13 /**
14 * @covers \MediaWiki\Block\BlockUtils
15 * @group Blocking
16 * @author DannyS712
17 */
18 class BlockUtilsTest extends MediaWikiUnitTestCase {
19 use DummyServicesTrait;
20
21 /**
22 * @param array $options
23 * @param UserIdentityLookup|null $userIdentityLookup
24 *
25 * @return BlockUtils
26 */
27 private function getUtils(
28 array $options = [],
29 ?UserIdentityLookup $userIdentityLookup = null
30 ) {
31 $baseOptions = [
32 MainConfigNames::BlockCIDRLimit => [
33 'IPv4' => 16,
34 'IPv6' => 19
35 ]
36 ];
37 $serviceOptions = new ServiceOptions(
38 BlockUtils::CONSTRUCTOR_OPTIONS,
39 $options + $baseOptions
40 );
41
42 return TestingAccessWrapper::newFromObject( new BlockUtils(
43 $serviceOptions,
44 $userIdentityLookup ?? $this->createMock( UserIdentityLookup::class ),
45 $this->getDummyUserNameUtils()
46 ) );
47 }
48
49 /**
50 * @dataProvider provideTestParseBlockTargetUserIdentity
51 * @param string $name
52 * @param int $type either AbstractBlock::TYPE_IP or TYPE_USER
53 */
54 public function testParseBlockTargetUserIdentity( string $name, int $type ) {
55 // Code path: providing a UserIdentity
56 // - target name is a valid IP, TYPE_IP
57 // - target name is not a valid IP, TYPE_USER
58 $userIdentity = new UserIdentityValue( $type === AbstractBlock::TYPE_IP ? 0 : 1, $name );
59
60 $this->assertSame(
61 [ $userIdentity, $type ],
62 $this->getUtils()->parseBlockTarget( $userIdentity )
63 );
64 }
65
66 public static function provideTestParseBlockTargetUserIdentity() {
67 return [
68 'Valid IP #1' => [ '1.2.3.4', AbstractBlock::TYPE_IP ],
69 'Invalid IP #1' => [ 'DannyS712', AbstractBlock::TYPE_USER ],
70 ];
71 }
72
73 public function testParseBlockTargetNull() {
74 // Code path: providing null
75 $blockUtils = $this->getUtils( [], null );
76 $this->assertSame(
77 [ null, null ],
78 $blockUtils->parseBlockTarget( null )
79 );
80 }
81
82 public function testParseBlockTargetString() {
83 // Code path: providing a string
84 // - valid IP string
85 $ip = '1.2.3.4';
86 $userIdentity = UserIdentityValue::newAnonymous( $ip );
87
88 $blockUtils = $this->getUtils();
89 [ $target, $type ] = $blockUtils->parseBlockTarget( $ip );
90 $this->assertTrue( $userIdentity->equals( $target ) );
91 $this->assertSame( AbstractBlock::TYPE_IP, $type );
92
93 // - valid IP range
94 $ipRange = '127.111.113.151/24';
95 $sanitizedRange = '127.111.113.0/24';
96 $this->assertSame(
97 [ $sanitizedRange, AbstractBlock::TYPE_RANGE ],
98 $blockUtils->parseBlockTarget( $ipRange )
99 );
100 }
101
102 /**
103 * @dataProvider provideTestParseBlockTargetNonIpString
104 * @param string $inputTarget
105 * @param ?UserIdentity $userIdentityLookupResult
106 * @param UserIdentity|string|null $outputTarget
107 * @param ?int $targetType
108 */
109 public function testParseBlockTargetNonIpString(
110 string $inputTarget,
111 ?UserIdentity $userIdentityLookupResult,
112 $outputTarget,
113 ?int $targetType
114 ) {
115 // - not an IP string, UserFactory::newFromName returns a User
116 // - not an IP string, UserFactory::newFromName returns null,
117 // string begins with # and is an autoblock reference
118 // - not an IP string, UserFactory::newFromName returns null,
119 // string does not begin with #
120 // Also include the case for subpage handling
121 $userIdentityLookup = $this->createMock( UserIdentityLookup::class );
122 $userIdentityLookup
123 ->method( 'getUserIdentityByName' )
124 ->with( $inputTarget )
125 ->willReturn( $userIdentityLookupResult );
126 $blockUtils = $this->getUtils(
127 [],
128 $userIdentityLookup
129 );
130 $this->assertSame(
131 [ $outputTarget, $targetType ],
132 $blockUtils->parseBlockTarget( $inputTarget )
133 );
134 }
135
136 public function provideTestParseBlockTargetNonIpString() {
137 $userIdentity = $this->createMock( UserIdentity::class );
138 yield 'Name returns a valid user' => [
139 'DannyS712',
140 $userIdentity,
141 $userIdentity,
142 AbstractBlock::TYPE_USER
143 ];
144
145 yield 'Auto block id' => [
146 '#123',
147 null,
148 '123',
149 AbstractBlock::TYPE_AUTO
150 ];
151
152 yield 'Invalid user name, with subpage' => [
153 'SomeInvalid#UserName/WithASubpage',
154 null,
155 null,
156 null
157 ];
158 }
159
160 }