X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;ds=sidebyside;f=tests%2Fphpunit%2Fincludes%2Fblock%2FBlockManagerTest.php;h=fe3bb88725c49cdbe32f6c2704d43df402798bb8;hb=c05b8b7c473a7dd9c832f91366c45cb8a35c2df2;hp=39a5534027560e00fa612b06d70675cf08500d57;hpb=e89e4a66ad8ed0d006de3ed363ed5b2f18cf9bdc;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/block/BlockManagerTest.php b/tests/phpunit/includes/block/BlockManagerTest.php index 39a5534027..fe3bb88725 100644 --- a/tests/phpunit/includes/block/BlockManagerTest.php +++ b/tests/phpunit/includes/block/BlockManagerTest.php @@ -2,6 +2,9 @@ use MediaWiki\Block\BlockManager; use MediaWiki\Block\DatabaseBlock; +use MediaWiki\Block\SystemBlock; +use MediaWiki\Config\ServiceOptions; +use MediaWiki\MediaWikiServices; /** * @group Blocking @@ -35,17 +38,29 @@ class BlockManagerTest extends MediaWikiTestCase { } private function getBlockManager( $overrideConfig ) { - $blockManagerConfig = array_merge( $this->blockManagerConfig, $overrideConfig ); return new BlockManager( - $this->user, - $this->user->getRequest(), - ...array_values( $blockManagerConfig ) + ...$this->getBlockManagerConstructorArgs( $overrideConfig ) ); } + private function getBlockManagerConstructorArgs( $overrideConfig ) { + $blockManagerConfig = array_merge( $this->blockManagerConfig, $overrideConfig ); + $this->setMwGlobals( $blockManagerConfig ); + $this->overrideMwServices(); + return [ + new ServiceOptions( + BlockManager::$constructorOptions, + MediaWikiServices::getInstance()->getMainConfig() + ), + $this->user, + $this->user->getRequest() + ]; + } + /** * @dataProvider provideGetBlockFromCookieValue * @covers ::getBlockFromCookieValue + * @covers ::shouldApplyCookieBlock */ public function testGetBlockFromCookieValue( $options, $expected ) { $blockManager = $this->getBlockManager( [ @@ -177,18 +192,14 @@ class BlockManagerTest extends MediaWikiTestCase { * @covers ::inDnsBlacklist */ public function testIsDnsBlacklisted( $options, $expected ) { - $blockManagerConfig = array_merge( $this->blockManagerConfig, [ + $blockManagerConfig = [ 'wgEnableDnsBlacklist' => true, 'wgDnsBlacklistUrls' => $options['blacklist'], 'wgProxyWhitelist' => $options['whitelist'], - ] ); + ]; $blockManager = $this->getMockBuilder( BlockManager::class ) - ->setConstructorArgs( - array_merge( [ - $this->user, - $this->user->getRequest(), - ], $blockManagerConfig ) ) + ->setConstructorArgs( $this->getBlockManagerConstructorArgs( $blockManagerConfig ) ) ->setMethods( [ 'checkHost' ] ) ->getMock(); @@ -288,4 +299,96 @@ class BlockManagerTest extends MediaWikiTestCase { ], ]; } + + /** + * @covers ::getUniqueBlocks + */ + public function testGetUniqueBlocks() { + $blockId = 100; + + $class = new ReflectionClass( BlockManager::class ); + $method = $class->getMethod( 'getUniqueBlocks' ); + $method->setAccessible( true ); + + $blockManager = $this->getBlockManager( [] ); + + $block = $this->getMockBuilder( DatabaseBlock::class ) + ->setMethods( [ 'getId' ] ) + ->getMock(); + $block->expects( $this->any() ) + ->method( 'getId' ) + ->willReturn( $blockId ); + + $autoblock = $this->getMockBuilder( DatabaseBlock::class ) + ->setMethods( [ 'getParentBlockId', 'getType' ] ) + ->getMock(); + $autoblock->expects( $this->any() ) + ->method( 'getParentBlockId' ) + ->willReturn( $blockId ); + $autoblock->expects( $this->any() ) + ->method( 'getType' ) + ->willReturn( DatabaseBlock::TYPE_AUTO ); + + $blocks = [ $block, $block, $autoblock, new SystemBlock() ]; + + $this->assertSame( 2, count( $method->invoke( $blockManager, $blocks ) ) ); + } + + /** + * @covers ::trackBlockWithCookie + * @dataProvider provideTrackBlockWithCookie + * @param bool $expectCookieSet + * @param bool $hasCookie + * @param bool $isBlocked + */ + public function testTrackBlockWithCookie( $expectCookieSet, $hasCookie, $isBlocked ) { + $blockID = 123; + $this->setMwGlobals( 'wgCookiePrefix', '' ); + + $request = new FauxRequest(); + if ( $hasCookie ) { + $request->setCookie( 'BlockID', 'the value does not matter' ); + } + + if ( $isBlocked ) { + $block = $this->getMockBuilder( DatabaseBlock::class ) + ->setMethods( [ 'getType', 'getId' ] ) + ->getMock(); + $block->method( 'getType' ) + ->willReturn( DatabaseBlock::TYPE_IP ); + $block->method( 'getId' ) + ->willReturn( $blockID ); + } else { + $block = null; + } + + $user = $this->getMockBuilder( User::class ) + ->setMethods( [ 'getBlock', 'getRequest' ] ) + ->getMock(); + $user->method( 'getBlock' ) + ->willReturn( $block ); + $user->method( 'getRequest' ) + ->willReturn( $request ); + /** @var User $user */ + + // Although the block cookie is set via DeferredUpdates, in command line mode updates are + // processed immediately + $blockManager = $this->getBlockManager( [] ); + $blockManager->trackBlockWithCookie( $user ); + + /** @var FauxResponse $response */ + $response = $request->response(); + $this->assertCount( $expectCookieSet ? 1 : 0, $response->getCookies() ); + $this->assertEquals( $expectCookieSet ? $blockID : null, $response->getCookie( 'BlockID' ) ); + } + + public function provideTrackBlockWithCookie() { + return [ + // $expectCookieSet, $hasCookie, $isBlocked + [ false, false, false ], + [ false, true, false ], + [ true, false, true ], + [ false, true, true ], + ]; + } }