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