| 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 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class BlockUtilsTest extends MediaWikiUnitTestCase { |
| 19 |
use DummyServicesTrait; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function testParseBlockTargetUserIdentity( string $name, int $type ) { |
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 75 |
$blockUtils = $this->getUtils( [], null ); |
| 76 |
$this->assertSame( |
| 77 |
[ null, null ], |
| 78 |
$blockUtils->parseBlockTarget( null ) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
public function testParseBlockTargetString() { |
| 83 |
|
| 84 |
|
| 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 |
|
| 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 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function testParseBlockTargetNonIpString( |
| 110 |
string $inputTarget, |
| 111 |
?UserIdentity $userIdentityLookupResult, |
| 112 |
$outputTarget, |
| 113 |
?int $targetType |
| 114 |
) { |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
} |