diff options
Diffstat (limited to 'tests/phpunit')
13 files changed, 594 insertions, 48 deletions
diff --git a/tests/phpunit/includes/block/DatabaseBlockTest.php b/tests/phpunit/includes/block/DatabaseBlockTest.php index bec14eada0f8..7f9f842aea94 100644 --- a/tests/phpunit/includes/block/DatabaseBlockTest.php +++ b/tests/phpunit/includes/block/DatabaseBlockTest.php @@ -1,10 +1,10 @@ <?php use MediaWiki\Block\BlockRestrictionStore; -use MediaWiki\Block\BlockUtils; use MediaWiki\Block\DatabaseBlock; use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\Block\Restriction\PageRestriction; +use MediaWiki\Block\UserBlockTarget; use MediaWiki\DAO\WikiAwareEntity; use MediaWiki\MainConfigNames; use MediaWiki\Tests\User\TempUser\TempUserTestTrait; @@ -122,14 +122,9 @@ class DatabaseBlockTest extends MediaWikiLangTestCase { ->willReturn( $lbMock ); $this->setService( 'DBLoadBalancerFactory', $lbFactoryMock ); - $target = UserIdentityValue::newExternal( 'm', 'UserOnForeignWiki', 'm' ); - - $blockUtilsMock = $this->createMock( BlockUtils::class ); - $blockUtilsMock - ->method( 'parseBlockTarget' ) - ->with( $target ) - ->willReturn( [ $target, DatabaseBlock::TYPE_USER ] ); - $this->setService( 'BlockUtils', $blockUtilsMock ); + $target = new UserBlockTarget( + UserIdentityValue::newExternal( 'm', 'UserOnForeignWiki', 'm' ) + ); $blocker = UserIdentityValue::newExternal( 'm', 'MetaWikiUser', 'm' ); @@ -141,7 +136,7 @@ class DatabaseBlockTest extends MediaWikiLangTestCase { $this->setService( 'UserNameUtils', $userNameUtilsMock ); $blockOptions = [ - 'address' => $target, + 'target' => $target, 'wiki' => 'm', 'reason' => 'testing crosswiki blocking', 'timestamp' => wfTimestampNow(), diff --git a/tests/phpunit/includes/specials/SpecialBlockTest.php b/tests/phpunit/includes/specials/SpecialBlockTest.php index 6dd6490a8804..52ea424f8b88 100644 --- a/tests/phpunit/includes/specials/SpecialBlockTest.php +++ b/tests/phpunit/includes/specials/SpecialBlockTest.php @@ -35,7 +35,7 @@ class SpecialBlockTest extends SpecialPageTestBase { protected function newSpecialPage() { $services = $this->getServiceContainer(); return new SpecialBlock( - $services->getBlockUtils(), + $services->getBlockTargetFactory(), $services->getBlockPermissionCheckerFactory(), $services->getBlockUserFactory(), $this->blockStore, @@ -166,7 +166,7 @@ class SpecialBlockTest extends SpecialPageTestBase { $page = $this->newSpecialPage(); $wrappedPage = TestingAccessWrapper::newFromObject( $page ); - $wrappedPage->target = $block->getTargetUserIdentity(); + $wrappedPage->target = $block->getTarget(); $fields = $wrappedPage->getFormFields(); $this->assertSame( $block->getTargetName(), $fields['Target']['default'] ); @@ -214,7 +214,7 @@ class SpecialBlockTest extends SpecialPageTestBase { $page = $this->newSpecialPage(); $wrappedPage = TestingAccessWrapper::newFromObject( $page ); - $wrappedPage->target = $block->getTargetUserIdentity(); + $wrappedPage->target = $block->getTarget(); $fields = $wrappedPage->getFormFields(); $titles = [ @@ -881,18 +881,18 @@ class SpecialBlockTest extends SpecialPageTestBase { } /** - * @dataProvider provideGetTargetAndType - * @covers ::getTargetAndTypeInternal + * @dataProvider provideGetTargetInternal + * @covers ::getTargetInternal */ - public function testGetTargetAndType( $par, $requestData, $expectedTarget ) { + public function testGetTargetInternal( $par, $requestData, $expectedTarget ) { $request = new FauxRequest( $requestData ); /** @var SpecialBlock $page */ $page = TestingAccessWrapper::newFromObject( $this->newSpecialPage() ); - [ $target, $type ] = $page->getTargetAndTypeInternal( $par, $request ); - $this->assertSame( $expectedTarget, $target ); + $target = $page->getTargetInternal( $par, $request ); + $this->assertSame( $expectedTarget, $target ? (string)$target : $target ); } - public static function provideGetTargetAndType() { + public static function provideGetTargetInternal() { $invalidTarget = ''; return [ 'Choose \'wpTarget\' parameter first' => [ diff --git a/tests/phpunit/includes/specials/SpecialUnblockTest.php b/tests/phpunit/includes/specials/SpecialUnblockTest.php index aa8e4f98b9d8..a577be9408be 100644 --- a/tests/phpunit/includes/specials/SpecialUnblockTest.php +++ b/tests/phpunit/includes/specials/SpecialUnblockTest.php @@ -20,7 +20,7 @@ class SpecialUnblockTest extends SpecialPageTestBase { $services = $this->getServiceContainer(); return new SpecialUnblock( $services->getUnblockUserFactory(), - $services->getBlockUtils(), + $services->getBlockTargetFactory(), $services->getDatabaseBlockStore(), $services->getUserNameUtils(), $services->getUserNamePrefixSearch(), diff --git a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php index da66e6039fdb..fa949a23b4ea 100644 --- a/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php +++ b/tests/phpunit/includes/specials/pagers/BlockListPagerTest.php @@ -2,7 +2,7 @@ use MediaWiki\Block\BlockActionInfo; use MediaWiki\Block\BlockRestrictionStore; -use MediaWiki\Block\BlockUtils; +use MediaWiki\Block\BlockTargetFactory; use MediaWiki\Block\DatabaseBlock; use MediaWiki\Block\HideUserUtils; use MediaWiki\Block\Restriction\NamespaceRestriction; @@ -34,8 +34,8 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { /** @var BlockRestrictionStore */ private $blockRestrictionStore; - /** @var BlockUtils */ - private $blockUtils; + /** @var BlockTargetFactory */ + private $blockTargetFactory; /** @var HideUserUtils */ private $hideUserUtils; @@ -64,7 +64,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { $services = $this->getServiceContainer(); $this->blockActionInfo = $services->getBlockActionInfo(); $this->blockRestrictionStore = $services->getBlockRestrictionStore(); - $this->blockUtils = $services->getBlockUtils(); + $this->blockTargetFactory = $services->getBlockTargetFactory(); $this->hideUserUtils = $services->getHideUserUtils(); $this->commentStore = $services->getCommentStore(); $this->linkBatchFactory = $services->getLinkBatchFactory(); @@ -79,7 +79,7 @@ class BlockListPagerTest extends MediaWikiIntegrationTestCase { RequestContext::getMain(), $this->blockActionInfo, $this->blockRestrictionStore, - $this->blockUtils, + $this->blockTargetFactory, $this->hideUserUtils, $this->commentStore, $this->linkBatchFactory, diff --git a/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php b/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php new file mode 100644 index 000000000000..6eded3e76258 --- /dev/null +++ b/tests/phpunit/integration/includes/block/BlockTargetFactoryTest.php @@ -0,0 +1,216 @@ +<?php + +use MediaWiki\Block\AnonIpBlockTarget; +use MediaWiki\Block\AutoBlockTarget; +use MediaWiki\Block\BlockTargetFactory; +use MediaWiki\Block\RangeBlockTarget; +use MediaWiki\Block\UserBlockTarget; +use MediaWiki\Config\ServiceOptions; +use MediaWiki\MainConfigNames; +use MediaWiki\User\UserIdentity; +use MediaWiki\User\UserIdentityLookup; +use MediaWiki\User\UserIdentityValue; +use MediaWiki\User\UserSelectQueryBuilder; +use Wikimedia\Rdbms\IDBAccessObject; + +/** + * @covers \MediaWiki\Block\BlockTargetFactory + */ +class BlockTargetFactoryTest extends \MediaWikiIntegrationTestCase { + private function getBlockTargetFactory( $wikiId = false ) { + return new BlockTargetFactory( + new ServiceOptions( + BlockTargetFactory::CONSTRUCTOR_OPTIONS, + [ + MainConfigNames::BlockCIDRLimit => [ + 'IPv4' => 16, + 'IPv6' => 19, + ], + ] + ), + $this->getMockUserIdentityLookup(), + $this->getServiceContainer()->getUserNameUtils(), + $wikiId + ); + } + + private function getMockUserIdentityLookup() { + return new class implements UserIdentityLookup { + public function getUserIdentityByName( + string $name, int $queryFlags = IDBAccessObject::READ_NORMAL + ): ?UserIdentity { + if ( $name === 'Exists' ) { + return new UserIdentityValue( 1, $name ); + } else { + return null; + } + } + + public function getUserIdentityByUserId( + int $userId, int $queryFlags = IDBAccessObject::READ_NORMAL + ): ?UserIdentity { + if ( $userId === 1 ) { + return new UserIdentityValue( 1, 'Exists' ); + } else { + return null; + } + } + + public function newSelectQueryBuilder( $dbOrQueryFlags = IDBAccessObject::READ_NORMAL + ): UserSelectQueryBuilder { + throw new RuntimeException( 'unimplemented' ); + } + }; + } + + public function testGetWikiId() { + $this->assertSame( 'enwiki', + $this->getBlockTargetFactory( 'enwiki' )->getWikiId() ); + } + + public static function provideNewFromString() { + return [ + [ '1.2.3.4', AnonIpBlockTarget::class ], + [ '::1', AnonIpBlockTarget::class, '0:0:0:0:0:0:0:1' ], + [ ' 1.2.3.4', AnonIpBlockTarget::class, '1.2.3.4' ], + [ '1.2.3.0/24', RangeBlockTarget::class ], + [ '::1/64', RangeBlockTarget::class, '0:0:0:0:0:0:0:0/64' ], + [ 'Exists', UserBlockTarget::class ], + [ 'Nonexistent', UserBlockTarget::class ], + [ '#1234', AutoBlockTarget::class ], + [ '__', null ], + [ '', null ], + [ null, null ] + ]; + } + + /** + * @dataProvider provideNewFromString + * @param string $input + * @param string $class + * @param string|null $serialized + */ + public function testNewFromString( $input, $class, $serialized = null ) { + $target = $this->getBlockTargetFactory()->newFromString( $input ); + if ( $class === null ) { + $this->assertNull( $target ); + return; + } + $this->assertInstanceOf( $class, $target ); + // Confirm round trip + $this->assertSame( $serialized ?? $input, $target->toString() ); + } + + /** + * @covers \MediaWiki\Block\UserBlockTarget::validateForCreation + */ + public function testNoSuchUser() { + $target = $this->getBlockTargetFactory()->newFromString( 'Nonexistent' ); + $status = $target->validateForCreation(); + $this->assertStatusError( 'nosuchusershort', $status ); + $this->assertSame( '<text>Nonexistent</text>', + $status->getMessages()[0]->getParams()[0]->dump() ); + } + + public static function provideNewFromUser() { + return [ + [ 1, 'Alice', UserBlockTarget::class ], + [ 5, 'Exists', UserBlockTarget::class ], + [ 0, 'Nonexistent', UserBlockTarget::class ], + [ 0, '127.0.0.1', AnonIpBlockTarget::class ], + [ 0, '1.2.3.0/24', RangeBlockTarget::class ], + ]; + } + + /** + * @dataProvider provideNewFromUser + * @param int $id + * @param string $name + * @param string $class + */ + public function testNewFromUser( $id, $name, $class ) { + $user = new UserIdentityValue( $id, $name ); + $target = $this->getBlockTargetFactory()->newFromUser( $user ); + $this->assertInstanceOf( $class, $target ); + $this->assertSame( $name, $target->toString() ); + } + + public static function provideNewFromRow() { + $default = [ + 'bt_auto' => '0', + 'bt_address' => '127.0.0.1', + 'bt_user' => null, + 'bt_user_text' => null, + 'bl_id' => 5, + ]; + return [ + 'IP block' => [ + $default, + false, + AnonIpBlockTarget::class, + '127.0.0.1', + ], + 'Autoblock exposed' => [ + [ 'bt_auto' => '1' ] + $default, + false, + AnonIpBlockTarget::class, + '127.0.0.1', + ], + 'Autoblock redacted' => [ + [ 'bt_auto' => '1' ] + $default, + true, + AutoBlockTarget::class, + '#5', + ], + 'Range block' => [ + [ 'bt_address' => '127.0.0.0/24' ] + $default, + false, + RangeBlockTarget::class, + '127.0.0.0/24', + ], + 'IPv6 range block' => [ + [ 'bt_address' => '2001:0:0:0:0:0:0:0/19' ] + $default, + false, + RangeBlockTarget::class, + '2001:0:0:0:0:0:0:0/19', + ], + 'User block' => [ + [ 'bt_user' => 2, 'bt_user_text' => 'Bob' ] + $default, + false, + UserBlockTarget::class, + 'Bob' + ], + 'Error case 1' => [ + [ 'bt_address' => null ] + $default, + false, + null, + '' + ], + 'Error case 2' => [ + [ 'bt_address' => 'Some invalid string' ] + $default, + false, + null, + '' + ], + ]; + } + + /** + * @dataProvider provideNewFromRow + * @param array $data + * @param bool $redact + * @param string|null $class + * @param string $serialized + */ + public function testNewFromRow( $data, $redact, $class, $serialized ) { + $factory = $this->getBlockTargetFactory(); + $target = $redact ? $factory->newFromRowRedacted( (object)$data ) + : $factory->newFromRowRaw( (object)$data ); + if ( $class === null ) { + $this->assertNull( $target ); + return; + } + $this->assertInstanceOf( $class, $target ); + $this->assertSame( $serialized, $target->toString() ); + } +} diff --git a/tests/phpunit/integration/includes/block/DatabaseBlockStoreTest.php b/tests/phpunit/integration/includes/block/DatabaseBlockStoreTest.php index 93fb77007b9f..c2e51ff01ea2 100644 --- a/tests/phpunit/integration/includes/block/DatabaseBlockStoreTest.php +++ b/tests/phpunit/integration/includes/block/DatabaseBlockStoreTest.php @@ -75,7 +75,7 @@ class DatabaseBlockStoreTest extends MediaWikiIntegrationTestCase { 'readOnlyMode' => $readOnlyMode, 'userFactory' => $services->getUserFactory(), 'tempUserConfig' => $services->getTempUserConfig(), - 'blockUtils' => $services->getBlockUtils(), + 'blockTargetFactory' => $services->getBlockTargetFactory(), 'autoblockExemptionList' => $services->getAutoblockExemptionList(), ]; $constructorArgs = array_merge( $defaultConstructorArgs, $overrideConstructorArgs ); diff --git a/tests/phpunit/unit/includes/block/AnonIpBlockTargetTest.php b/tests/phpunit/unit/includes/block/AnonIpBlockTargetTest.php new file mode 100644 index 000000000000..030314e4f12d --- /dev/null +++ b/tests/phpunit/unit/includes/block/AnonIpBlockTargetTest.php @@ -0,0 +1,76 @@ +<?php + +use MediaWiki\Block\AnonIpBlockTarget; +use MediaWiki\Block\Block; +use MediaWiki\User\UserIdentity; + +/** + * @covers \MediaWiki\Block\AnonIpBlockTarget + */ +class AnonIpBlockTargetTest extends MediaWikiUnitTestCase { + public function testToString() { + $target = new AnonIpBlockTarget( '1.2.3.4', false ); + $this->assertSame( '1.2.3.4', $target->toString() ); + } + + public function testGetType() { + $target = new AnonIpBlockTarget( '1.2.3.4', false ); + $this->assertSame( Block::TYPE_IP, $target->getType() ); + } + + public function testGetSpecificity() { + $target = new AnonIpBlockTarget( '1.2.3.4', false ); + $this->assertSame( 2, $target->getSpecificity() ); + } + + public function testGetLogPage() { + $target = new AnonIpBlockTarget( '1.2.3.4', false ); + $logPage = $target->getLogPage(); + $this->assertSame( NS_USER, $logPage->getNamespace() ); + $this->assertSame( '1.2.3.4', $logPage->getDBkey() ); + } + + public function testGetUserPage() { + $target = new AnonIpBlockTarget( '1.2.3.4', false ); + $page = $target->getUserPage(); + $this->assertSame( NS_USER, $page->getNamespace() ); + $this->assertSame( '1.2.3.4', $page->getDBkey() ); + } + + public function testGetUserIdentity() { + $target = new AnonIpBlockTarget( '1.2.3.4', 'enwiki' ); + $user = $target->getUserIdentity(); + $this->assertSame( 'enwiki', $user->getWikiId() ); + $this->assertSame( '1.2.3.4', $user->getName() ); + $this->assertSame( 0, $user->getId( 'enwiki' ) ); + } + + public function testValidateForCreation() { + $target = new AnonIpBlockTarget( '1.2.3.4', 'enwiki' ); + $this->assertStatusGood( $target->validateForCreation() ); + } + + public function testToHex() { + $target = new AnonIpBlockTarget( '1.2.3.4', 'enwiki' ); + $this->assertSame( '01020304', $target->toHex() ); + } + + public function testToHexRange() { + $target = new AnonIpBlockTarget( '1.2.3.4', 'enwiki' ); + $this->assertSame( [ '01020304', '01020304' ], $target->toHexRange() ); + } + + public function testGetLegacyTuple() { + $target = new AnonIpBlockTarget( '1.2.3.4', 'enwiki' ); + [ $resTarget, $resType ] = $target->getLegacyTuple(); + $this->assertSame( Block::TYPE_IP, $resType ); + $this->assertInstanceOf( UserIdentity::class, $resTarget ); + $this->assertSame( 0, $resTarget->getId( 'enwiki' ) ); + $this->assertSame( '1.2.3.4', $resTarget->getName() ); + } + + public function testGetWikiId() { + $target = new AnonIpBlockTarget( '1.2.3.4', 'enwiki' ); + $this->assertSame( 'enwiki', $target->getWikiId() ); + } +} diff --git a/tests/phpunit/unit/includes/block/AutoBlockTargetTest.php b/tests/phpunit/unit/includes/block/AutoBlockTargetTest.php new file mode 100644 index 000000000000..9d5bcfa44185 --- /dev/null +++ b/tests/phpunit/unit/includes/block/AutoBlockTargetTest.php @@ -0,0 +1,53 @@ +<?php + +use MediaWiki\Block\AutoBlockTarget; +use MediaWiki\Block\Block; + +/** + * @covers \MediaWiki\Block\AutoBlockTarget + */ +class AutoBlockTargetTest extends MediaWikiUnitTestCase { + public function testToString() { + $target = new AutoBlockTarget( 1234, false ); + $this->assertSame( '#1234', $target->toString() ); + } + + public function testGetType() { + $target = new AutoBlockTarget( 1234, false ); + $this->assertSame( Block::TYPE_AUTO, $target->getType() ); + } + + public function testGetLogPage() { + $target = new AutoBlockTarget( 1234, false ); + $page = $target->getLogPage(); + $this->assertSame( NS_USER, $page->getNamespace() ); + $this->assertSame( '#1234', $page->getDBkey() ); + } + + public function testGetSpecificity() { + $target = new AutoBlockTarget( 1234, false ); + $this->assertSame( 2, $target->getSpecificity() ); + } + + public function testValidateForCreation() { + $target = new AutoBlockTarget( 1234, false ); + $this->assertStatusNotOK( $target->validateForCreation() ); + } + + public function testGetId() { + $target = new AutoBlockTarget( 1234, false ); + $this->assertSame( 1234, $target->getId() ); + } + + public function testGetLegacyTuple() { + $target = new AutoBlockTarget( 1234, false ); + [ $id, $type ] = $target->getLegacyTuple(); + $this->assertSame( '1234', $id ); + $this->assertSame( Block::TYPE_AUTO, $type ); + } + + public function testGetWikiId() { + $target = new AutoBlockTarget( 1234, 'enwiki' ); + $this->assertSame( 'enwiki', $target->getWikiId() ); + } +} diff --git a/tests/phpunit/unit/includes/block/BlockPermissionCheckerTest.php b/tests/phpunit/unit/includes/block/BlockPermissionCheckerTest.php index 12b891e7c7db..fd0a417285b1 100644 --- a/tests/phpunit/unit/includes/block/BlockPermissionCheckerTest.php +++ b/tests/phpunit/unit/includes/block/BlockPermissionCheckerTest.php @@ -1,8 +1,9 @@ <?php use MediaWiki\Block\BlockPermissionChecker; -use MediaWiki\Block\BlockUtils; +use MediaWiki\Block\BlockTargetFactory; use MediaWiki\Block\DatabaseBlock; +use MediaWiki\Block\UserBlockTarget; use MediaWiki\Config\ServiceOptions; use MediaWiki\MainConfigNames; use MediaWiki\Permissions\Authority; @@ -33,15 +34,18 @@ class BlockPermissionCheckerTest extends MediaWikiUnitTestCase { [ MainConfigNames::EnableUserEmail => $enableUserEmail ] ); - $blockUtils = $this->createNoOpMock( BlockUtils::class, [ 'parseBlockTarget' ] ); - $blockUtils->expects( $this->any() ) - ->method( 'parseBlockTarget' ) + $blockTargetFactory = $this->createNoOpMock( + BlockTargetFactory::class, + [ 'newFromLegacyUnion' ] + ); + $blockTargetFactory->expects( $this->any() ) + ->method( 'newFromLegacyUnion' ) ->willReturnCallback( static function ( $target ) { - return [ $target, 0 ]; + return new UserBlockTarget( $target ); } ); return new BlockPermissionChecker( $options, - $blockUtils, + $blockTargetFactory, $performer ); } diff --git a/tests/phpunit/unit/includes/block/BlockUtilsTest.php b/tests/phpunit/unit/includes/block/BlockUtilsTest.php index 32f897d11221..33cbd04a85e1 100644 --- a/tests/phpunit/unit/includes/block/BlockUtilsTest.php +++ b/tests/phpunit/unit/includes/block/BlockUtilsTest.php @@ -1,6 +1,7 @@ <?php use MediaWiki\Block\AbstractBlock; +use MediaWiki\Block\BlockTargetFactory; use MediaWiki\Block\BlockUtils; use MediaWiki\Config\ServiceOptions; use MediaWiki\MainConfigNames; @@ -35,14 +36,16 @@ class BlockUtilsTest extends MediaWikiUnitTestCase { ] ]; $serviceOptions = new ServiceOptions( - BlockUtils::CONSTRUCTOR_OPTIONS, + BlockTargetFactory::CONSTRUCTOR_OPTIONS, $options + $baseOptions ); return TestingAccessWrapper::newFromObject( new BlockUtils( - $serviceOptions, - $userIdentityLookup ?? $this->createMock( UserIdentityLookup::class ), - $this->getDummyUserNameUtils() + new BlockTargetFactory( + $serviceOptions, + $userIdentityLookup ?? $this->createMock( UserIdentityLookup::class ), + $this->getDummyUserNameUtils() + ) ) ); } @@ -57,10 +60,15 @@ class BlockUtilsTest extends MediaWikiUnitTestCase { // - 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 ) - ); + [ $resTarget, $resType ] = $this->getUtils()->parseBlockTarget( $userIdentity ); + $this->assertSame( $type, $resType ); + if ( $type === AbstractBlock::TYPE_IP ) { + // With the migration to BlockTargetFactory, preservation of the + // UserIdentity object for an IP is no longer guaranteed. + $this->assertTrue( $userIdentity->equals( $resTarget ) ); + } else { + $this->assertSame( $userIdentity, $resTarget ); + } } public static function provideTestParseBlockTargetUserIdentity() { diff --git a/tests/phpunit/unit/includes/block/DatabaseBlockStoreFactoryTest.php b/tests/phpunit/unit/includes/block/DatabaseBlockStoreFactoryTest.php index 62e8454cb4b1..009ef148536e 100644 --- a/tests/phpunit/unit/includes/block/DatabaseBlockStoreFactoryTest.php +++ b/tests/phpunit/unit/includes/block/DatabaseBlockStoreFactoryTest.php @@ -5,8 +5,8 @@ namespace MediaWiki\Tests\Block; use MediaWiki\Block\AutoblockExemptionList; use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Block\BlockRestrictionStoreFactory; -use MediaWiki\Block\BlockUtils; -use MediaWiki\Block\BlockUtilsFactory; +use MediaWiki\Block\BlockTargetFactory; +use MediaWiki\Block\CrossWikiBlockTargetFactory; use MediaWiki\Block\DatabaseBlockStore; use MediaWiki\Block\DatabaseBlockStoreFactory; use MediaWiki\CommentStore\CommentStore; @@ -43,12 +43,12 @@ class DatabaseBlockStoreFactoryTest extends MediaWikiUnitTestCase { ->with( $domain ) ->willReturn( $blockRestrictionStore ); - $blockUtils = $this->createMock( BlockUtils::class ); - $blockUtilsFactory = $this->createMock( BlockUtilsFactory::class ); - $blockUtilsFactory - ->method( 'getBlockUtils' ) + $blockTargetFactory = $this->createNoOpMock( BlockTargetFactory::class ); + $crossWikiBlockTargetFactory = $this->createMock( CrossWikiBlockTargetFactory::class ); + $crossWikiBlockTargetFactory + ->method( 'getFactory' ) ->with( $domain ) - ->willReturn( $blockUtils ); + ->willReturn( $blockTargetFactory ); $factory = new DatabaseBlockStoreFactory( new ServiceOptions( @@ -66,7 +66,7 @@ class DatabaseBlockStoreFactoryTest extends MediaWikiUnitTestCase { $this->createMock( ReadOnlyMode::class ), $this->createMock( UserFactory::class ), $this->createMock( TempUserConfig::class ), - $blockUtilsFactory, + $crossWikiBlockTargetFactory, $this->createMock( AutoblockExemptionList::class ) ); diff --git a/tests/phpunit/unit/includes/block/RangeBlockTargetTest.php b/tests/phpunit/unit/includes/block/RangeBlockTargetTest.php new file mode 100644 index 000000000000..34dd8be09ae1 --- /dev/null +++ b/tests/phpunit/unit/includes/block/RangeBlockTargetTest.php @@ -0,0 +1,127 @@ +<?php + +use MediaWiki\Block\Block; +use MediaWiki\Block\RangeBlockTarget; + +/** + * @covers \MediaWiki\Block\RangeBlockTarget + */ +class RangeBlockTargetTest extends MediaWikiUnitTestCase { + private const DEFAULT_LIMITS = [ 'IPv4' => 16, 'IPv6' => 19 ]; + + private function getTarget() { + return new RangeBlockTarget( + '1.2.3.0/24', + self::DEFAULT_LIMITS, + 'enwiki' + ); + } + + public function testToString() { + $this->assertSame( '1.2.3.0/24', $this->getTarget()->toString() ); + } + + public function testGetType() { + $this->assertSame( Block::TYPE_RANGE, $this->getTarget()->getType() ); + } + + public function testGetLogPage() { + $page = $this->getTarget()->getLogPage(); + $this->assertSame( NS_USER, $page->getNamespace() ); + $this->assertSame( '1.2.3.0/24', $page->getDBkey() ); + $this->assertSame( 'enwiki', $page->getWikiId() ); + } + + public static function provideGetSpecificity() { + return [ + [ '1.2.3.4/32', 2 ], + [ '1.2.3.0/24', 2.25 ], + [ '0.0.0.0/0', 3 ], + [ '::1/64', 2.5 ], + [ '::1/32', 2.75 ], + ]; + } + + /** + * @dataProvider provideGetSpecificity + */ + public function testGetSpecificity( $range, $expected ) { + $target = new RangeBlockTarget( $range, self::DEFAULT_LIMITS, false ); + $this->assertSame( $expected, $target->getSpecificity() ); + } + + public static function provideValidateForCreation() { + return [ + [ '1.2.3.4/16', false ], + [ '1.2.3.4/15', 'ip_range_toolarge' ], + [ '1.2.3.4/65', 'ip_range_invalid' ], + [ '::1/19', false ], + [ '::1/18', 'ip_range_toolarge' ], + [ '::1/130', 'ip_range_invalid' ], + ]; + } + + /** + * @dataProvider provideValidateForCreation + * @param string $range + * @param string|false $error + */ + public function testValidateForCreation( $range, $error ) { + $target = new RangeBlockTarget( $range, self::DEFAULT_LIMITS, false ); + $status = $target->validateForCreation(); + if ( $error === false ) { + $this->assertStatusGood( $status ); + } else { + $this->assertStatusError( $error, $status ); + } + } + + public static function provideValidateForCreationDisabled() { + return [ + [ '1.2.3.0/24' ], + [ '::1/24' ], + ]; + } + + /** + * @dataProvider provideValidateForCreationDisabled + * @param string $range + */ + public function testValidateForCreationDisabled( $range ) { + $target = new RangeBlockTarget( + $range, [ + 'IPv4' => 32, + 'IPv6' => 128 + ], + false + ); + $status = $target->validateForCreation(); + $this->assertStatusError( 'range_block_disabled', $status ); + } + + public function testToHexRange() { + $this->assertSame( + [ '01020300', '010203FF' ], + $this->getTarget()->toHexRange() + ); + } + + public function testGetHexRangeStart() { + $this->assertSame( '01020300', $this->getTarget()->getHexRangeStart() ); + } + + public function testGetHexRangeEnd() { + $this->assertSame( '010203FF', $this->getTarget()->getHexRangeEnd() ); + } + + public function testGetLegacyTuple() { + $this->assertSame( + [ '1.2.3.0/24', Block::TYPE_RANGE ], + $this->getTarget()->getLegacyTuple() + ); + } + + public function testGetWikiId() { + $this->assertSame( 'enwiki', $this->getTarget()->getWikiId() ); + } +} diff --git a/tests/phpunit/unit/includes/block/UserBlockTargetTest.php b/tests/phpunit/unit/includes/block/UserBlockTargetTest.php new file mode 100644 index 000000000000..3a3b23742023 --- /dev/null +++ b/tests/phpunit/unit/includes/block/UserBlockTargetTest.php @@ -0,0 +1,67 @@ +<?php + +use MediaWiki\Block\Block; +use MediaWiki\Block\UserBlockTarget; +use MediaWiki\User\UserIdentityValue; + +/** + * @covers \MediaWiki\Block\UserBlockTarget + */ +class UserBlockTargetTest extends MediaWikiUnitTestCase { + private function getTarget() { + return new UserBlockTarget( + new UserIdentityValue( 5, 'Alice', 'enwiki' ) + ); + } + + public function testToString() { + $this->assertSame( 'Alice', $this->getTarget()->toString() ); + } + + public function testGetType() { + $this->assertSame( Block::TYPE_USER, $this->getTarget()->getType() ); + } + + public function testGetSpecificity() { + $this->assertSame( 1, $this->getTarget()->getSpecificity() ); + } + + public function testGetLogPage() { + $page = $this->getTarget()->getLogPage(); + $this->assertSame( NS_USER, $page->getNamespace() ); + $this->assertSame( 'Alice', $page->getDBkey() ); + $this->assertSame( 'enwiki', $page->getWikiId() ); + } + + public function testGetUserPage() { + $page = $this->getTarget()->getUserPage(); + $this->assertSame( NS_USER, $page->getNamespace() ); + $this->assertSame( 'Alice', $page->getDBkey() ); + $this->assertSame( 'enwiki', $page->getWikiId() ); + } + + public function testGetUserIdentity() { + $user = $this->getTarget()->getUserIdentity(); + $this->assertSame( 'Alice', $user->getName() ); + $this->assertSame( 5, $user->getId( 'enwiki' ) ); + $this->assertSame( 'enwiki', $user->getWikiId() ); + } + + public function testValidateForCreation() { + $this->assertStatusGood( $this->getTarget()->validateForCreation() ); + // The failure case is covered by BlockTargetFactoryTest since + // wfEscapeWikiText() needs config + } + + public function testGetLegacyTuple() { + $user = new UserIdentityValue( 5, 'Alice', 'enwiki' ); + $target = new UserBlockTarget( $user ); + [ $resTarget, $resType ] = $target->getLegacyTuple(); + $this->assertSame( $user, $resTarget ); + $this->assertSame( Block::TYPE_USER, $resType ); + } + + public function testGetWikiId() { + $this->assertSame( 'enwiki', $this->getTarget()->getWikiId() ); + } +} |