From de67ee197220132cb124809e214b088a99d974a5 Mon Sep 17 00:00:00 2001 From: Dayllan Maza Date: Thu, 11 Apr 2019 15:54:10 -0400 Subject: [PATCH] Rename BlockRestriction -> BlockRestrictionStore and wire it up as a service BlockRestriction was initially created as a static class and there is no reason why this shouldn't be available in the service container. Also renaming as BlockRestrictionStore to keep up with the new emerging naming patterns. Bug: T219684 Change-Id: If0b954f286d4759de2e3e41a0eb788e74bd72996 --- includes/Block.php | 42 ++++-- includes/MediaWikiServices.php | 9 ++ includes/ServiceWiring.php | 7 + includes/api/ApiQueryBlocks.php | 5 +- ...triction.php => BlockRestrictionStore.php} | 81 +++++++----- includes/specials/SpecialBlock.php | 5 +- includes/specials/pagers/BlockListPager.php | 4 +- tests/phpunit/includes/BlockTest.php | 17 ++- ...Test.php => BlockRestrictionStoreTest.php} | 123 ++++++++++-------- .../includes/specials/SpecialBlockTest.php | 22 +++- 10 files changed, 197 insertions(+), 118 deletions(-) rename includes/block/{BlockRestriction.php => BlockRestrictionStore.php} (80%) rename tests/phpunit/includes/block/{BlockRestrictionTest.php => BlockRestrictionStoreTest.php} (77%) diff --git a/includes/Block.php b/includes/Block.php index c6b948239c..37b9ce521e 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -22,7 +22,7 @@ use Wikimedia\Rdbms\Database; use Wikimedia\Rdbms\IDatabase; -use MediaWiki\Block\BlockRestriction; +use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Block\Restriction\Restriction; use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\Block\Restriction\PageRestriction; @@ -305,7 +305,9 @@ class Block { && $this->isSitewide() == $block->isSitewide() // Block::getRestrictions() may perform a database query, so keep it at // the end. - && BlockRestriction::equals( $this->getRestrictions(), $block->getRestrictions() ) + && $this->getBlockRestrictionStore()->equals( + $this->getRestrictions(), $block->getRestrictions() + ) ); } @@ -523,10 +525,10 @@ class Block { $dbw = wfGetDB( DB_MASTER ); - BlockRestriction::deleteByParentBlockId( $this->getId() ); + $this->getBlockRestrictionStore()->deleteByParentBlockId( $this->getId() ); $dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ ); - BlockRestriction::deleteByBlockId( $this->getId() ); + $this->getBlockRestrictionStore()->deleteByBlockId( $this->getId() ); $dbw->delete( 'ipblocks', [ 'ipb_id' => $this->getId() ], __METHOD__ ); return $dbw->affectedRows() > 0; @@ -565,7 +567,7 @@ class Block { if ( $affected ) { $this->setId( $dbw->insertId() ); if ( $this->restrictions ) { - BlockRestriction::insert( $this->restrictions ); + $this->getBlockRestrictionStore()->insert( $this->restrictions ); } } @@ -585,12 +587,12 @@ class Block { ); if ( $ids ) { $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], __METHOD__ ); - BlockRestriction::deleteByBlockId( $ids ); + $this->getBlockRestrictionStore()->deleteByBlockId( $ids ); $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] ); $affected = $dbw->affectedRows(); $this->setId( $dbw->insertId() ); if ( $this->restrictions ) { - BlockRestriction::insert( $this->restrictions ); + $this->getBlockRestrictionStore()->insert( $this->restrictions ); } } } @@ -634,9 +636,9 @@ class Block { if ( $this->restrictions !== null ) { // An empty array should remove all of the restrictions. if ( empty( $this->restrictions ) ) { - $success = BlockRestriction::deleteByBlockId( $this->getId() ); + $success = $this->getBlockRestrictionStore()->deleteByBlockId( $this->getId() ); } else { - $success = BlockRestriction::update( $this->restrictions ); + $success = $this->getBlockRestrictionStore()->update( $this->restrictions ); } // Update the result. The first false is the result, otherwise, true. $result = $result && $success; @@ -653,11 +655,11 @@ class Block { // Only update the restrictions if they have been modified. if ( $this->restrictions !== null ) { - BlockRestriction::updateByParentBlockId( $this->getId(), $this->restrictions ); + $this->getBlockRestrictionStore()->updateByParentBlockId( $this->getId(), $this->restrictions ); } } else { // autoblock no longer required, delete corresponding autoblock(s) - BlockRestriction::deleteByParentBlockId( $this->getId() ); + $this->getBlockRestrictionStore()->deleteByParentBlockId( $this->getId() ); $dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], @@ -1069,7 +1071,9 @@ class Block { $this->mId = (int)$blockId; if ( is_array( $this->restrictions ) ) { - $this->restrictions = BlockRestriction::setBlockId( $blockId, $this->restrictions ); + $this->restrictions = $this->getBlockRestrictionStore()->setBlockId( + $blockId, $this->restrictions + ); } return $this; @@ -1367,7 +1371,9 @@ class Block { $fname ); if ( $ids ) { - BlockRestriction::deleteByBlockId( $ids ); + $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore(); + $blockRestrictionStore->deleteByBlockId( $ids ); + $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], $fname ); } } @@ -1941,7 +1947,7 @@ class Block { if ( !$this->mId ) { return []; } - $this->restrictions = BlockRestriction::loadByBlockId( $this->mId ); + $this->restrictions = $this->getBlockRestrictionStore()->loadByBlockId( $this->mId ); } return $this->restrictions; @@ -2163,4 +2169,12 @@ class Block { } } + /** + * Get a BlockRestrictionStore instance + * + * @return BlockRestrictionStore + */ + private function getBlockRestrictionStore() : BlockRestrictionStore { + return MediaWikiServices::getInstance()->getBlockRestrictionStore(); + } } diff --git a/includes/MediaWikiServices.php b/includes/MediaWikiServices.php index 8c60dc7e52..7a5f0269c8 100644 --- a/includes/MediaWikiServices.php +++ b/includes/MediaWikiServices.php @@ -13,6 +13,7 @@ use GlobalVarConfig; use Hooks; use IBufferingStatsdDataFactory; use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; +use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Http\HttpRequestFactory; use MediaWiki\Permissions\PermissionManager; use MediaWiki\Preferences\PreferencesFactory; @@ -435,6 +436,14 @@ class MediaWikiServices extends ServiceContainer { return $this->getService( 'BlobStoreFactory' ); } + /** + * @since 1.33 + * @return BlockRestrictionStore + */ + public function getBlockRestrictionStore() : BlockRestrictionStore { + return $this->getService( 'BlockRestrictionStore' ); + } + /** * Returns the Config object containing the bootstrap configuration. * Bootstrap configuration would typically include database credentials diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php index a82feaa22f..b11e129c6a 100644 --- a/includes/ServiceWiring.php +++ b/includes/ServiceWiring.php @@ -39,6 +39,7 @@ use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface; use MediaWiki\Auth\AuthManager; +use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Config\ConfigRepository; use MediaWiki\Interwiki\ClassicInterwikiLookup; use MediaWiki\Interwiki\InterwikiLookup; @@ -83,6 +84,12 @@ return [ ); }, + 'BlockRestrictionStore' => function ( MediaWikiServices $services ) : BlockRestrictionStore { + return new BlockRestrictionStore( + $services->getDBLoadBalancer() + ); + }, + 'CommentStore' => function ( MediaWikiServices $services ) : CommentStore { return new CommentStore( $services->getContentLanguage(), diff --git a/includes/api/ApiQueryBlocks.php b/includes/api/ApiQueryBlocks.php index 8aff2aa6b4..5615f46213 100644 --- a/includes/api/ApiQueryBlocks.php +++ b/includes/api/ApiQueryBlocks.php @@ -21,7 +21,7 @@ */ use Wikimedia\Rdbms\IResultWrapper; -use MediaWiki\Block\BlockRestriction; +use MediaWiki\MediaWikiServices; /** * Query module to enumerate all user blocks @@ -292,7 +292,8 @@ class ApiQueryBlocks extends ApiQueryBase { } } - $restrictions = BlockRestriction::loadByBlockId( $partialIds ); + $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore(); + $restrictions = $blockRestrictionStore->loadByBlockId( $partialIds ); $data = []; $keys = [ diff --git a/includes/block/BlockRestriction.php b/includes/block/BlockRestrictionStore.php similarity index 80% rename from includes/block/BlockRestriction.php rename to includes/block/BlockRestrictionStore.php index 2e8752e450..d242c87e2d 100644 --- a/includes/block/BlockRestriction.php +++ b/includes/block/BlockRestrictionStore.php @@ -28,17 +28,30 @@ use MediaWiki\Block\Restriction\Restriction; use MWException; use Wikimedia\Rdbms\IResultWrapper; use Wikimedia\Rdbms\IDatabase; +use Wikimedia\Rdbms\ILoadBalancer; -class BlockRestriction { +class BlockRestrictionStore { /** * Map of all of the restriction types. */ - private static $types = [ + private $types = [ PageRestriction::TYPE_ID => PageRestriction::class, NamespaceRestriction::TYPE_ID => NamespaceRestriction::class, ]; + /** + * @var ILoadBalancer + */ + private $loadBalancer; + + /* + * @param LoadBalancer $loadBalancer load balancer for acquiring database connections + */ + public function __construct( ILoadBalancer $loadBalancer ) { + $this->loadBalancer = $loadBalancer; + } + /** * Retrieves the restrictions from the database by block id. * @@ -47,12 +60,12 @@ class BlockRestriction { * @param IDatabase|null $db * @return Restriction[] */ - public static function loadByBlockId( $blockId, IDatabase $db = null ) { + public function loadByBlockId( $blockId, IDatabase $db = null ) { if ( $blockId === null || $blockId === [] ) { return []; } - $db = $db ?: wfGetDB( DB_REPLICA ); + $db = $db ?: $this->loadBalancer->getConnection( DB_REPLICA ); $result = $db->select( [ 'ipblocks_restrictions', 'page' ], @@ -63,7 +76,7 @@ class BlockRestriction { [ 'page' => [ 'LEFT JOIN', [ 'ir_type' => PageRestriction::TYPE_ID, 'ir_value=page_id' ] ] ] ); - return self::resultToRestrictions( $result ); + return $this->resultToRestrictions( $result ); } /** @@ -73,7 +86,7 @@ class BlockRestriction { * @param Restriction[] $restrictions * @return bool */ - public static function insert( array $restrictions ) { + public function insert( array $restrictions ) { if ( !$restrictions ) { return false; } @@ -90,7 +103,7 @@ class BlockRestriction { return false; } - $dbw = wfGetDB( DB_MASTER ); + $dbw = $this->loadBalancer->getConnection( DB_MASTER ); $dbw->insert( 'ipblocks_restrictions', @@ -110,13 +123,13 @@ class BlockRestriction { * @param Restriction[] $restrictions * @return bool */ - public static function update( array $restrictions ) { - $dbw = wfGetDB( DB_MASTER ); + public function update( array $restrictions ) { + $dbw = $this->loadBalancer->getConnection( DB_MASTER ); $dbw->startAtomic( __METHOD__ ); // Organize the restrictions by blockid. - $restrictionList = self::restrictionsByBlockId( $restrictions ); + $restrictionList = $this->restrictionsByBlockId( $restrictions ); // Load the existing restrictions and organize by block id. Any block ids // that were passed into this function will be used to load all of the @@ -133,8 +146,8 @@ class BlockRestriction { [ 'FOR UPDATE' ] ); - $existingList = self::restrictionsByBlockId( - self::resultToRestrictions( $result ) + $existingList = $this->restrictionsByBlockId( + $this->resultToRestrictions( $result ) ); } @@ -142,12 +155,12 @@ class BlockRestriction { // Perform the actions on a per block-id basis. foreach ( $restrictionList as $blockId => $blockRestrictions ) { // Insert all of the restrictions first, ignoring ones that already exist. - $success = self::insert( $blockRestrictions ); + $success = $this->insert( $blockRestrictions ); // Update the result. The first false is the result, otherwise, true. $result = $success && $result; - $restrictionsToRemove = self::restrictionsToRemove( + $restrictionsToRemove = $this->restrictionsToRemove( $existingList[$blockId] ?? [], $restrictions ); @@ -156,7 +169,7 @@ class BlockRestriction { continue; } - $success = self::delete( $restrictionsToRemove ); + $success = $this->delete( $restrictionsToRemove ); // Update the result. The first false is the result, otherwise, true. $result = $success && $result; @@ -175,15 +188,15 @@ class BlockRestriction { * @param Restriction[] $restrictions * @return bool */ - public static function updateByParentBlockId( $parentBlockId, array $restrictions ) { + public function updateByParentBlockId( $parentBlockId, array $restrictions ) { // If removing all of the restrictions, then just delete them all. if ( empty( $restrictions ) ) { - return self::deleteByParentBlockId( $parentBlockId ); + return $this->deleteByParentBlockId( $parentBlockId ); } $parentBlockId = (int)$parentBlockId; - $db = wfGetDB( DB_MASTER ); + $db = $this->loadBalancer->getConnection( DB_MASTER ); $db->startAtomic( __METHOD__ ); @@ -197,7 +210,7 @@ class BlockRestriction { $result = true; foreach ( $blockIds as $id ) { - $success = self::update( self::setBlockId( $id, $restrictions ) ); + $success = $this->update( $this->setBlockId( $id, $restrictions ) ); // Update the result. The first false is the result, otherwise, true. $result = $success && $result; } @@ -215,8 +228,8 @@ class BlockRestriction { * @throws MWException * @return bool */ - public static function delete( array $restrictions ) { - $dbw = wfGetDB( DB_MASTER ); + public function delete( array $restrictions ) { + $dbw = $this->loadBalancer->getConnection( DB_MASTER ); $result = true; foreach ( $restrictions as $restriction ) { if ( !$restriction instanceof Restriction ) { @@ -245,8 +258,8 @@ class BlockRestriction { * @throws MWException * @return bool */ - public static function deleteByBlockId( $blockId ) { - $dbw = wfGetDB( DB_MASTER ); + public function deleteByBlockId( $blockId ) { + $dbw = $this->loadBalancer->getConnection( DB_MASTER ); return $dbw->delete( 'ipblocks_restrictions', [ 'ir_ipb_id' => $blockId ], @@ -262,8 +275,8 @@ class BlockRestriction { * @throws MWException * @return bool */ - public static function deleteByParentBlockId( $parentBlockId ) { - $dbw = wfGetDB( DB_MASTER ); + public function deleteByParentBlockId( $parentBlockId ) { + $dbw = $this->loadBalancer->getConnection( DB_MASTER ); return $dbw->deleteJoin( 'ipblocks_restrictions', 'ipblocks', @@ -284,7 +297,7 @@ class BlockRestriction { * @param Restriction[] $b * @return bool */ - public static function equals( array $a, array $b ) { + public function equals( array $a, array $b ) { $filter = function ( $restriction ) { return $restriction instanceof Restriction; }; @@ -329,7 +342,7 @@ class BlockRestriction { * @param Restriction[] $restrictions * @return Restriction[] */ - public static function setBlockId( $blockId, array $restrictions ) { + public function setBlockId( $blockId, array $restrictions ) { $blockRestrictions = []; foreach ( $restrictions as $restriction ) { @@ -356,7 +369,7 @@ class BlockRestriction { * @param Restriction[] $new * @return array */ - private static function restrictionsToRemove( array $existing, array $new ) { + private function restrictionsToRemove( array $existing, array $new ) { return array_filter( $existing, function ( $e ) use ( $new ) { foreach ( $new as $restriction ) { if ( !$restriction instanceof Restriction ) { @@ -379,7 +392,7 @@ class BlockRestriction { * @param Restriction[] $restrictions * @return array */ - private static function restrictionsByBlockId( array $restrictions ) { + private function restrictionsByBlockId( array $restrictions ) { $blockRestrictions = []; foreach ( $restrictions as $restriction ) { @@ -404,10 +417,10 @@ class BlockRestriction { * @param IResultWrapper $result * @return Restriction[] */ - private static function resultToRestrictions( IResultWrapper $result ) { + private function resultToRestrictions( IResultWrapper $result ) { $restrictions = []; foreach ( $result as $row ) { - $restriction = self::rowToRestriction( $row ); + $restriction = $this->rowToRestriction( $row ); if ( !$restriction ) { continue; @@ -425,9 +438,9 @@ class BlockRestriction { * @param \stdClass $row * @return Restriction|null */ - private static function rowToRestriction( \stdClass $row ) { - if ( array_key_exists( (int)$row->ir_type, self::$types ) ) { - $class = self::$types[ (int)$row->ir_type ]; + private function rowToRestriction( \stdClass $row ) { + if ( array_key_exists( (int)$row->ir_type, $this->types ) ) { + $class = $this->types[ (int)$row->ir_type ]; return call_user_func( [ $class, 'newFromRow' ], $row ); } diff --git a/includes/specials/SpecialBlock.php b/includes/specials/SpecialBlock.php index 155d6a48f6..0ff517efc4 100644 --- a/includes/specials/SpecialBlock.php +++ b/includes/specials/SpecialBlock.php @@ -21,9 +21,9 @@ * @ingroup SpecialPage */ -use MediaWiki\Block\BlockRestriction; use MediaWiki\Block\Restriction\PageRestriction; use MediaWiki\Block\Restriction\NamespaceRestriction; +use MediaWiki\MediaWikiServices; /** * A special page that allows users with 'block' right to block users from @@ -950,8 +950,9 @@ class SpecialBlock extends FormSpecialPage { $currentBlock->isSitewide( $block->isSitewide() ); // Set the block id of the restrictions. + $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore(); $currentBlock->setRestrictions( - BlockRestriction::setBlockId( $currentBlock->getId(), $restrictions ) + $blockRestrictionStore->setBlockId( $currentBlock->getId(), $restrictions ) ); } diff --git a/includes/specials/pagers/BlockListPager.php b/includes/specials/pagers/BlockListPager.php index 71cf787504..d09b3451b9 100644 --- a/includes/specials/pagers/BlockListPager.php +++ b/includes/specials/pagers/BlockListPager.php @@ -22,7 +22,6 @@ /** * @ingroup Pager */ -use MediaWiki\Block\BlockRestriction; use MediaWiki\Block\Restriction\Restriction; use MediaWiki\Block\Restriction\PageRestriction; use MediaWiki\Block\Restriction\NamespaceRestriction; @@ -419,7 +418,8 @@ class BlockListPager extends TablePager { if ( $partialBlocks ) { // Mutations to the $row object are not persisted. The restrictions will // need be stored in a separate store. - $this->restrictions = BlockRestriction::loadByBlockId( $partialBlocks ); + $blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore(); + $this->restrictions = $blockRestrictionStore->loadByBlockId( $partialBlocks ); } $lb->execute(); diff --git a/tests/phpunit/includes/BlockTest.php b/tests/phpunit/includes/BlockTest.php index 7874688e7b..df3de4a336 100644 --- a/tests/phpunit/includes/BlockTest.php +++ b/tests/phpunit/includes/BlockTest.php @@ -1,8 +1,9 @@ getId(), $pageFoo->getId() ); $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_USER ); - BlockRestriction::insert( [ $pageRestriction, $namespaceRestriction ] ); + $this->getBlockRestrictionStore()->insert( [ $pageRestriction, $namespaceRestriction ] ); $this->assertTrue( $block->appliesToTitle( $pageFoo->getTitle() ) ); $this->assertFalse( $block->appliesToTitle( $pageBar->getTitle() ) ); @@ -673,7 +674,7 @@ class BlockTest extends MediaWikiLangTestCase { $block->getId(), $title->getArticleID() ); - BlockRestriction::insert( [ $pageRestriction ] ); + $this->getBlockRestrictionStore()->insert( [ $pageRestriction ] ); $this->assertTrue( $block->appliesToPage( $title->getArticleID() ) ); @@ -699,7 +700,7 @@ class BlockTest extends MediaWikiLangTestCase { $block->insert(); $namespaceRestriction = new NamespaceRestriction( $block->getId(), NS_MAIN ); - BlockRestriction::insert( [ $namespaceRestriction ] ); + $this->getBlockRestrictionStore()->insert( [ $namespaceRestriction ] ); $this->assertTrue( $block->appliesToNamespace( NS_MAIN ) ); $this->assertFalse( $block->appliesToNamespace( NS_USER ) ); @@ -718,4 +719,12 @@ class BlockTest extends MediaWikiLangTestCase { $this->assertFalse( $block->appliesToRight( 'purge' ) ); } + /** + * Get an instance of BlockRestrictionStore + * + * @return BlockRestrictionStore + */ + protected function getBlockRestrictionStore() : BlockRestrictionStore { + return MediaWikiServices::getInstance()->getBlockRestrictionStore(); + } } diff --git a/tests/phpunit/includes/block/BlockRestrictionTest.php b/tests/phpunit/includes/block/BlockRestrictionStoreTest.php similarity index 77% rename from tests/phpunit/includes/block/BlockRestrictionTest.php rename to tests/phpunit/includes/block/BlockRestrictionStoreTest.php index 5bbd3d023d..4eef457339 100644 --- a/tests/phpunit/includes/block/BlockRestrictionTest.php +++ b/tests/phpunit/includes/block/BlockRestrictionStoreTest.php @@ -2,17 +2,27 @@ namespace MediaWiki\Tests\Block; -use MediaWiki\Block\BlockRestriction; +use MediaWiki\Block\BlockRestrictionStore; use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\Block\Restriction\PageRestriction; use MediaWiki\Block\Restriction\Restriction; +use MediaWiki\MediaWikiServices; /** * @group Database * @group Blocking - * @coversDefaultClass \MediaWiki\Block\BlockRestriction + * @coversDefaultClass \MediaWiki\Block\BlockRestrictionStore */ -class BlockRestrictionTest extends \MediaWikiLangTestCase { +class BlockRestrictionStoreTest extends \MediaWikiLangTestCase { + + /** @var BlockRestrictionStore */ + protected $blockRestrictionStore; + + public function setUp() { + parent::setUp(); + + $this->blockRestrictionStore = MediaWikiServices::getInstance()->getBlockRestrictionStore(); + } public function tearDown() { parent::tearDown(); @@ -33,13 +43,13 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $pageFoo = $this->getExistingTestPage( 'Foo' ); $pageBar = $this->getExistingTestPage( 'Bar' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $pageFoo->getId() ), new PageRestriction( $block->getId(), $pageBar->getId() ), new NamespaceRestriction( $block->getId(), NS_USER ), ] ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 3, $restrictions ); } @@ -51,7 +61,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { */ public function testWithNoRestrictions() { $block = $this->insertBlock(); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertEmpty( $restrictions ); } @@ -61,8 +71,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { * @covers ::rowToRestriction */ public function testWithEmptyParam() { - $restrictions = BlockRestriction::loadByBlockId( [] ); - + $restrictions = $this->blockRestrictionStore->loadByBlockId( [] ); $this->assertEmpty( $restrictions ); } @@ -85,7 +94,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $this->insertRestriction( $block->getId(), 9, $pageBar->getId() ); $this->insertRestriction( $block->getId(), 10, NS_FILE ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 2, $restrictions ); } @@ -100,11 +109,11 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $page = $this->getExistingTestPage( $title ); // Test Page Restrictions. - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); list( $pageRestriction ) = $restrictions; $this->assertInstanceOf( PageRestriction::class, $pageRestriction ); @@ -122,11 +131,11 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { public function testMappingNamespaceRestrictionObject() { $block = $this->insertBlock(); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new NamespaceRestriction( $block->getId(), NS_USER ), ] ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); list( $namespaceRestriction ) = $restrictions; $this->assertInstanceOf( NamespaceRestriction::class, $namespaceRestriction ); @@ -151,17 +160,17 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { new NamespaceRestriction( $block->getId(), NS_USER ) ]; - $result = BlockRestriction::insert( $restrictions ); + $result = $this->blockRestrictionStore->insert( $restrictions ); $this->assertTrue( $result ); $restrictions = [ new \stdClass(), ]; - $result = BlockRestriction::insert( $restrictions ); + $result = $this->blockRestrictionStore->insert( $restrictions ); $this->assertFalse( $result ); - $result = BlockRestriction::insert( [] ); + $result = $this->blockRestrictionStore->insert( [] ); $this->assertFalse( $result ); } @@ -190,10 +199,10 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $invalid, ]; - $result = BlockRestriction::insert( $restrictions ); + $result = $this->blockRestrictionStore->insert( $restrictions ); $this->assertTrue( $result ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 3, $restrictions ); } @@ -206,11 +215,11 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $block = $this->insertBlock(); $pageFoo = $this->getExistingTestPage( 'Foo' ); $pageBar = $this->getExistingTestPage( 'Bar' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $pageFoo->getId() ), ] ); - BlockRestriction::update( [ + $this->blockRestrictionStore->update( [ new \stdClass(), new PageRestriction( $block->getId(), $pageBar->getId() ), new NamespaceRestriction( $block->getId(), NS_USER ), @@ -238,7 +247,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::update( [ + $this->blockRestrictionStore->update( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); @@ -263,7 +272,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { public function testUpdateNoRestrictions() { $block = $this->insertBlock(); - BlockRestriction::update( [] ); + $this->blockRestrictionStore->update( [] ); $db = wfGetDb( DB_REPLICA ); $result = $db->select( @@ -283,11 +292,11 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { public function testUpdateSame() { $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); - BlockRestriction::update( [ + $this->blockRestrictionStore->update( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); @@ -312,33 +321,33 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $block = $this->insertBlock(); $pageFoo = $this->getExistingTestPage( 'Foo' ); $pageBar = $this->getExistingTestPage( 'Bar' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $pageFoo->getId() ), ] ); $autoblockId = $block->doAutoblock( '127.0.0.1' ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() ); // Ensure that the restrictions on the autoblock are the same as the block. - $restrictions = BlockRestriction::loadByBlockId( $autoblockId ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId ); $this->assertCount( 1, $restrictions ); $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() ); // Update the restrictions on the autoblock (but leave the block unchanged) - BlockRestriction::updateByParentBlockId( $block->getId(), [ + $this->blockRestrictionStore->updateByParentBlockId( $block->getId(), [ new PageRestriction( $block->getId(), $pageBar->getId() ), ] ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); $this->assertEquals( $pageFoo->getId(), $restrictions[0]->getValue() ); // Ensure that the restrictions on the autoblock have been updated. - $restrictions = BlockRestriction::loadByBlockId( $autoblockId ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId ); $this->assertCount( 1, $restrictions ); $this->assertEquals( $pageBar->getId(), $restrictions[0]->getValue() ); } @@ -350,28 +359,28 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { // Create a block and an autoblock (a child block) $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); $autoblockId = $block->doAutoblock( '127.0.0.1' ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); // Ensure that the restrictions on the autoblock have not changed. - $restrictions = BlockRestriction::loadByBlockId( $autoblockId ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId ); $this->assertCount( 1, $restrictions ); // Remove the restrictions on the autoblock (but leave the block unchanged) - BlockRestriction::updateByParentBlockId( $block->getId(), [] ); + $this->blockRestrictionStore->updateByParentBlockId( $block->getId(), [] ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); // Ensure that the restrictions on the autoblock have been updated. - $restrictions = BlockRestriction::loadByBlockId( $autoblockId ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId ); $this->assertCount( 0, $restrictions ); } @@ -382,19 +391,19 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { // Create a block with no autoblock. $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); // Update the restrictions on any autoblocks (there are none). - BlockRestriction::updateByParentBlockId( $block->getId(), $restrictions ); + $this->blockRestrictionStore->updateByParentBlockId( $block->getId(), $restrictions ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); } @@ -404,17 +413,19 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { public function testDelete() { $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); - $result = BlockRestriction::delete( array_merge( $restrictions, [ new \stdClass() ] ) ); + $result = $this->blockRestrictionStore->delete( + array_merge( $restrictions, [ new \stdClass() ] ) + ); $this->assertTrue( $result ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 0, $restrictions ); } @@ -424,17 +435,17 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { public function testDeleteByBlockId() { $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); - $result = BlockRestriction::deleteByBlockId( $block->getId() ); + $result = $this->blockRestrictionStore->deleteByBlockId( $block->getId() ); $this->assertNotFalse( $result ); - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 0, $restrictions ); } @@ -445,30 +456,30 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { // Create a block with no autoblock. $block = $this->insertBlock(); $page = $this->getExistingTestPage( 'Foo' ); - BlockRestriction::insert( [ + $this->blockRestrictionStore->insert( [ new PageRestriction( $block->getId(), $page->getId() ), ] ); $autoblockId = $block->doAutoblock( '127.0.0.1' ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); // Ensure that the restrictions on the autoblock are the same as the block. - $restrictions = BlockRestriction::loadByBlockId( $autoblockId ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId ); $this->assertCount( 1, $restrictions ); // Remove all of the restrictions on the autoblock (but leave the block unchanged). - $result = BlockRestriction::deleteByParentBlockId( $block->getId() ); + $result = $this->blockRestrictionStore->deleteByParentBlockId( $block->getId() ); // NOTE: commented out until https://gerrit.wikimedia.org/r/c/mediawiki/core/+/469324 is merged //$this->assertTrue( $result ); // Ensure that the restrictions on the block have not changed. - $restrictions = BlockRestriction::loadByBlockId( $block->getId() ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $block->getId() ); $this->assertCount( 1, $restrictions ); // Ensure that the restrictions on the autoblock have been removed. - $restrictions = BlockRestriction::loadByBlockId( $autoblockId ); + $restrictions = $this->blockRestrictionStore->loadByBlockId( $autoblockId ); $this->assertCount( 0, $restrictions ); } @@ -481,7 +492,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { * @param bool $expected */ public function testEquals( array $a, array $b, $expected ) { - $this->assertSame( $expected, BlockRestriction::equals( $a, $b ) ); + $this->assertSame( $expected, $this->blockRestrictionStore->equals( $a, $b ) ); } public function equalsDataProvider() { @@ -561,7 +572,7 @@ class BlockRestrictionTest extends \MediaWikiLangTestCase { $this->assertSame( 1, $restrictions[2]->getBlockId() ); $this->assertSame( 1, $restrictions[3]->getBlockId() ); - $result = BlockRestriction::setBlockId( 2, $restrictions ); + $result = $this->blockRestrictionStore->setBlockId( 2, $restrictions ); foreach ( $result as $restriction ) { $this->assertSame( 2, $restriction->getBlockId() ); diff --git a/tests/phpunit/includes/specials/SpecialBlockTest.php b/tests/phpunit/includes/specials/SpecialBlockTest.php index 182ca0d043..c1f2e42064 100644 --- a/tests/phpunit/includes/specials/SpecialBlockTest.php +++ b/tests/phpunit/includes/specials/SpecialBlockTest.php @@ -1,9 +1,10 @@ assertSame( $reason, $block->getReason() ); $this->assertSame( $expiry, $block->getExpiry() ); $this->assertCount( 2, $block->getRestrictions() ); - $this->assertTrue( BlockRestriction::equals( $block->getRestrictions(), [ + $this->assertTrue( $this->getBlockRestrictionStore()->equals( $block->getRestrictions(), [ new PageRestriction( $block->getId(), $pageMars->getId() ), new PageRestriction( $block->getId(), $pageSaturn->getId() ), ] ) ); @@ -335,7 +336,7 @@ class SpecialBlockTest extends SpecialPageTestBase { $this->assertSame( $expiry, $block->getExpiry() ); $this->assertFalse( $block->isSitewide() ); $this->assertCount( 2, $block->getRestrictions() ); - $this->assertTrue( BlockRestriction::equals( $block->getRestrictions(), [ + $this->assertTrue( $this->getBlockRestrictionStore()->equals( $block->getRestrictions(), [ new PageRestriction( $block->getId(), $pageMars->getId() ), new PageRestriction( $block->getId(), $pageSaturn->getId() ), ] ) ); @@ -351,7 +352,7 @@ class SpecialBlockTest extends SpecialPageTestBase { $this->assertSame( $expiry, $block->getExpiry() ); $this->assertFalse( $block->isSitewide() ); $this->assertCount( 1, $block->getRestrictions() ); - $this->assertTrue( BlockRestriction::equals( $block->getRestrictions(), [ + $this->assertTrue( $this->getBlockRestrictionStore()->equals( $block->getRestrictions(), [ new PageRestriction( $block->getId(), $pageMars->getId() ), ] ) ); @@ -471,4 +472,17 @@ class SpecialBlockTest extends SpecialPageTestBase { $this->db->delete( 'ipblocks', '*', __METHOD__ ); $this->db->delete( 'ipblocks_restrictions', '*', __METHOD__ ); } + + /** + * Get a BlockRestrictionStore instance + * + * @return BlockRestrictionStore + */ + private function getBlockRestrictionStore() : BlockRestrictionStore { + $loadBalancer = $this->getMockBuilder( LoadBalancer::class ) + ->disableOriginalConstructor() + ->getMock(); + + return new BlockRestrictionStore( $loadBalancer ); + } } -- 2.20.1