createMock( MessageLocalizer::class ); $ml->method( 'msg' )->willReturnCallback( function ( $k, $p ) { return $this->getMockMessage( $k, $p ); } ); return $ml; } private function getFilterUser(): FilterUser { $filterUser = $this->createMock( FilterUser::class ); $filterUser->method( 'getUser' ) ->willReturn( new UserIdentityValue( 2, 'FilterUser' ) ); return $filterUser; } public function provideExecute(): iterable { foreach ( [ true, false ] as $result ) { $resStr = wfBoolToStr( $result ); yield "IPv4, $resStr" => [ new UserIdentityValue( 0, '1.2.3.4' ), $result ]; yield "IPv6, $resStr" => [ // random IP from https://en.wikipedia.org/w/index.php?title=IPv6&oldid=989727833 new UserIdentityValue( 0, '2001:0db8:0000:0000:0000:ff00:0042:8329' ), $result ]; yield "Registered, $resStr" => [ new UserIdentityValue( 3, 'Some random user' ), $result ]; } } /** * @dataProvider provideExecute * @covers ::__construct * @covers ::execute */ public function testExecute( UserIdentity $target, bool $result ) { $expiry = '1 day'; $params = $this->provideGetMessageParameters( $target )->current()[0]; $blockUser = $this->createMock( BlockUser::class ); $blockUser->expects( $this->once() ) ->method( 'placeBlockUnsafe' ) ->willReturn( $result ? Status::newGood() : Status::newFatal( 'error' ) ); $blockUserFactory = $this->createMock( BlockUserFactory::class ); $blockUserFactory->expects( $this->once() ) ->method( 'newBlockUser' ) ->with( $target->getName(), $this->anything(), $expiry, $this->anything(), $this->anything() ) ->willReturn( $blockUser ); $block = new Block( $params, $expiry, $preventsTalkEdit = true, $blockUserFactory, $this->createMock( DatabaseBlockStore::class ), static function () { return null; }, $this->getFilterUser(), $this->getMsgLocalizer() ); $this->assertSame( $result, $block->execute() ); } /** * @covers ::getMessage * @dataProvider provideGetMessageParameters */ public function testGetMessage( Parameters $params ) { $block = new Block( $params, '0', false, $this->createMock( BlockUserFactory::class ), $this->createMock( DatabaseBlockStore::class ), static function () { return null; }, $this->createMock( FilterUser::class ), $this->getMsgLocalizer() ); $this->doTestGetMessage( $block, $params, 'abusefilter-blocked-display' ); } public function provideRevert() { yield 'no block to revert' => [ null, null, false ]; $randomUser = new UserIdentityValue( 1234, 'Some other user' ); yield 'not blocked by AF user' => [ new DatabaseBlock( [ 'by' => $randomUser ] ), null, false ]; $blockByFilter = new DatabaseBlock( [ 'by' => $this->getFilterUser()->getUser() ] ); $failBlockStore = $this->createMock( DatabaseBlockStore::class ); $failBlockStore->expects( $this->once() )->method( 'deleteBlock' )->willReturn( false ); yield 'cannot delete block' => [ $blockByFilter, $failBlockStore, false ]; $succeedBlockStore = $this->createMock( DatabaseBlockStore::class ); $succeedBlockStore->expects( $this->once() )->method( 'deleteBlock' )->willReturn( true ); yield 'succeed' => [ $blockByFilter, $succeedBlockStore, true ]; } /** * @covers ::revert * @dataProvider provideRevert */ public function testRevert( ?DatabaseBlock $block, ?DatabaseBlockStore $blockStore, bool $expected ) { $params = $this->createMock( Parameters::class ); $params->method( 'getUser' )->willReturn( new UserIdentityValue( 1, 'Foobar' ) ); $block = new Block( $params, '0', false, $this->createMock( BlockUserFactory::class ), $blockStore ?? $this->createMock( DatabaseBlockStore::class ), static function () use ( $block ) { return $block; }, $this->getFilterUser(), $this->getMsgLocalizer() ); $this->assertSame( $expected, $block->revert( [], $this->createMock( UserIdentity::class ), '' ) ); } }