diff options
author | Tim Starling <tstarling@wikimedia.org> | 2025-02-07 09:42:31 +1100 |
---|---|---|
committer | Tim Starling <tstarling@wikimedia.org> | 2025-03-04 09:12:16 +1100 |
commit | c2f4d23ffe704bcfb9bab7b379cfdaa38232c653 (patch) | |
tree | f4b1c22288dc107a0b3331a4f0d1fdc16fe12a9c /tests/phpunit/includes/api | |
parent | f141346a30b421370b5d7ff0202e41b59c5eadbe (diff) | |
download | mediawikicore-c2f4d23ffe704bcfb9bab7b379cfdaa38232c653.tar.gz mediawikicore-c2f4d23ffe704bcfb9bab7b379cfdaa38232c653.zip |
block: DatabaseBlock constructor caller migration
There's about 100 callers of the DatabaseBlock constructor in core
tests, most of them passing an address parameter which needs access to
the global service container to parse.
Many are passing the constructed object straight to
DatabaseBlockStore::insertBlock(). So add insertWithParams() for their
convenience, which has some handy shortcut parameters, has service
access, and throws on failure. The calling code tends to be shorter
than before.
For unit tests trying to construct DatabaseBlock objects without a
service container, direct construction of BlockTarget subclasses is
warranted. Add a default to the $wikiId parameters for their
convenience.
MockBlockManager had its own 'target' parameter, mixed in with block
options, carrying its own special idea of a target, which conflicted
with DatabaseBlock's new 'target' parameter. Harmonise the parameters
and fix the callers.
Bug: T382106
Bug: T385966
Change-Id: I78b45a6003b62962211379c36da5587081f90f00
Diffstat (limited to 'tests/phpunit/includes/api')
-rw-r--r-- | tests/phpunit/includes/api/ApiBlockTest.php | 16 | ||||
-rw-r--r-- | tests/phpunit/includes/api/ApiEditPageTest.php | 4 | ||||
-rw-r--r-- | tests/phpunit/includes/api/ApiMoveTest.php | 4 | ||||
-rw-r--r-- | tests/phpunit/includes/api/ApiRevisionDeleteTest.php | 20 | ||||
-rw-r--r-- | tests/phpunit/includes/api/ApiUnblockTest.php | 21 | ||||
-rw-r--r-- | tests/phpunit/includes/api/ApiUserrightsTest.php | 11 | ||||
-rw-r--r-- | tests/phpunit/includes/api/query/ApiQueryBlocksTest.php | 40 | ||||
-rw-r--r-- | tests/phpunit/includes/api/query/ApiQueryInfoTest.php | 35 |
8 files changed, 63 insertions, 88 deletions
diff --git a/tests/phpunit/includes/api/ApiBlockTest.php b/tests/phpunit/includes/api/ApiBlockTest.php index a953c67a478c..7f15985a9c6f 100644 --- a/tests/phpunit/includes/api/ApiBlockTest.php +++ b/tests/phpunit/includes/api/ApiBlockTest.php @@ -99,14 +99,14 @@ class ApiBlockTest extends ApiTestCase { $this->expectApiErrorCode( 'ipbblocked' ); $blocked = $this->getMutableTestUser( [ 'sysop' ] )->getUser(); - $block = new DatabaseBlock( [ - 'address' => $blocked->getName(), - 'by' => $this->getTestSysop()->getUser(), - 'reason' => 'Capriciousness', - 'timestamp' => '19370101000000', - 'expiry' => 'infinity', - ] ); - $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); + $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'address' => $blocked->getName(), + 'by' => $this->getTestSysop()->getUser(), + 'reason' => 'Capriciousness', + 'timestamp' => '19370101000000', + 'expiry' => 'infinity', + ] ); $this->doBlock( [], $blocked ); } diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index 2cdbd8f18068..c13eebecc13d 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -3,7 +3,6 @@ namespace MediaWiki\Tests\Api; use MediaWiki\Api\ApiUsageException; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\CommentStore\CommentStoreComment; use MediaWiki\Content\JavaScriptContent; use MediaWiki\Content\WikitextContent; @@ -1619,7 +1618,7 @@ class ApiEditPageTest extends ApiTestCase { $this->assertNull( $blockStore->newFromTarget( '127.0.0.1' ) ); $user = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ + $blockStore->insertBlockWithParams( [ 'address' => $user->getName(), 'by' => $user, 'reason' => 'Capriciousness', @@ -1627,7 +1626,6 @@ class ApiEditPageTest extends ApiTestCase { 'expiry' => 'infinity', 'enableAutoblock' => true, ] ); - $blockStore->insertBlock( $block ); try { $this->doApiRequestWithToken( [ diff --git a/tests/phpunit/includes/api/ApiMoveTest.php b/tests/phpunit/includes/api/ApiMoveTest.php index eb156aa963ae..4f2f90ab14e8 100644 --- a/tests/phpunit/includes/api/ApiMoveTest.php +++ b/tests/phpunit/includes/api/ApiMoveTest.php @@ -3,7 +3,6 @@ namespace MediaWiki\Tests\Api; use MediaWiki\Api\ApiUsageException; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\MainConfigNames; use MediaWiki\Revision\SlotRecord; use MediaWiki\Title\Title; @@ -200,7 +199,7 @@ class ApiMoveTest extends ApiTestCase { $this->assertNull( $blockStore->newFromTarget( '127.0.0.1' ) ); $user = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ + $blockStore->insertBlockWithParams( [ 'address' => $user->getName(), 'by' => $user, 'reason' => 'Capriciousness', @@ -208,7 +207,6 @@ class ApiMoveTest extends ApiTestCase { 'expiry' => 'infinity', 'enableAutoblock' => true, ] ); - $blockStore->insertBlock( $block ); $title = Title::makeTitle( NS_MAIN, 'TestMoveWhileBlocked' ); $title2 = Title::makeTitle( NS_MAIN, 'TestMoveWhileBlocked 2' ); diff --git a/tests/phpunit/includes/api/ApiRevisionDeleteTest.php b/tests/phpunit/includes/api/ApiRevisionDeleteTest.php index b248e09054da..ddade084a6d1 100644 --- a/tests/phpunit/includes/api/ApiRevisionDeleteTest.php +++ b/tests/phpunit/includes/api/ApiRevisionDeleteTest.php @@ -2,7 +2,6 @@ namespace MediaWiki\Tests\Api; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\Block\Restriction\PageRestriction; use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\SlotRecord; @@ -130,17 +129,16 @@ class ApiRevisionDeleteTest extends ApiTestCase { $this->expectApiErrorCode( 'blocked' ); $performer = $this->mockAnonAuthorityWithPermissions( [ 'deleterevision' ] ); - $block = new DatabaseBlock( [ - 'address' => $performer->getUser(), - 'by' => static::getTestSysop()->getUser(), - 'sitewide' => false, - ] ); - $title = Title::makeTitle( NS_HELP, 'ApiRevDel_test' ); - $block->setRestrictions( [ - new PageRestriction( 0, $title->getArticleID() ) - ] ); - $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); + $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'address' => $performer->getUser(), + 'by' => static::getTestSysop()->getUser(), + 'sitewide' => false, + 'restrictions' => [ + new PageRestriction( 0, $title->getArticleID() ) + ] + ] ); $revid = array_shift( $this->revs ); diff --git a/tests/phpunit/includes/api/ApiUnblockTest.php b/tests/phpunit/includes/api/ApiUnblockTest.php index 7647ba413149..e831c1387c4b 100644 --- a/tests/phpunit/includes/api/ApiUnblockTest.php +++ b/tests/phpunit/includes/api/ApiUnblockTest.php @@ -3,7 +3,6 @@ namespace MediaWiki\Tests\Api; use MediaWiki\Api\ApiUsageException; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\Block\DatabaseBlockStore; use MediaWiki\MainConfigNames; use MediaWiki\Title\Title; @@ -40,15 +39,11 @@ class ApiUnblockTest extends ApiTestCase { private function insertBlock( $options = [] ) { $options = array_merge( [ - 'address' => $this->blockee->getName(), + 'targetUser' => $this->blockee, 'by' => $this->blocker, ], $options ); - $block = new DatabaseBlock( $options ); - $result = $this->blockStore->insertBlock( $block, null ); - - $this->assertNotFalse( $result, 'Could not insert block' ); - return $result; + return $this->blockStore->insertBlockWithParams( $options ); } private function getBlocksFromParams( array $params ): array { @@ -120,23 +115,21 @@ class ApiUnblockTest extends ApiTestCase { } public function testUnblockSelfWhenBlocked() { - $result = $this->insertBlock( [ + $this->insertBlock( [ 'address' => $this->blocker->getName(), 'by' => $this->getTestUser( 'sysop' )->getUser(), ] ); - $this->assertNotFalse( $result, 'Could not insert block' ); $this->doUnblock( [ 'user' => $this->blocker->getName() ] ); } public function testUnblockSelfByIdWhenBlocked() { - $result = $this->insertBlock( [ + $block = $this->insertBlock( [ 'address' => $this->blocker->getName(), 'by' => $this->getTestUser( 'sysop' )->getUser(), ] ); - $this->assertNotFalse( $result, 'Could not insert block' ); - $this->doUnblock( [ 'id' => $result['id'] ] ); + $this->doUnblock( [ 'id' => $block->getId() ] ); } public function testUnblockWithTagNewBackend() { @@ -183,8 +176,8 @@ class ApiUnblockTest extends ApiTestCase { } public function testUnblockByBlockId() { - $result = $this->insertBlock(); - $this->doUnblock( [ 'id' => $result['id'] ] ); + $block = $this->insertBlock(); + $this->doUnblock( [ 'id' => $block->getId() ] ); } public function testWatched() { diff --git a/tests/phpunit/includes/api/ApiUserrightsTest.php b/tests/phpunit/includes/api/ApiUserrightsTest.php index 5350ff15a262..3906248f7128 100644 --- a/tests/phpunit/includes/api/ApiUserrightsTest.php +++ b/tests/phpunit/includes/api/ApiUserrightsTest.php @@ -2,7 +2,6 @@ namespace MediaWiki\Tests\Api; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\MainConfigNames; use MediaWiki\MainConfigSchema; use MediaWiki\Title\Title; @@ -148,9 +147,8 @@ class ApiUserrightsTest extends ApiTestCase { public function testBlockedWithUserrights() { $user = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ 'address' => $user, 'by' => $user, ] ); - $blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); - $blockStore->insertBlock( $block ); + $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ 'targetUser' => $user, 'by' => $user ] ); $this->doSuccessfulRightsChange(); } @@ -160,9 +158,8 @@ class ApiUserrightsTest extends ApiTestCase { $this->setPermissions( true, true ); - $block = new DatabaseBlock( [ 'address' => $user, 'by' => $user ] ); - $blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); - $blockStore->insertBlock( $block ); + $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ 'targetUser' => $user, 'by' => $user ] ); $this->doFailedRightsChange( 'blocked' ); } diff --git a/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php b/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php index 347f76e3df92..c7c8ba6906b4 100644 --- a/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryBlocksTest.php @@ -3,7 +3,6 @@ namespace MediaWiki\Tests\Api\Query; use MediaWiki\Block\BlockActionInfo; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\Block\Restriction\ActionRestriction; use MediaWiki\Block\Restriction\NamespaceRestriction; use MediaWiki\Block\Restriction\PageRestriction; @@ -31,13 +30,12 @@ class ApiQueryBlocksTest extends ApiTestCase { $badActor = $this->getTestUser()->getUser(); $sysop = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ - 'address' => $badActor, - 'by' => $sysop, - 'expiry' => 'infinity', - ] ); - - $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); + $block = $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'targetUser' => $badActor, + 'by' => $sysop, + 'expiry' => 'infinity', + ] ); [ $data ] = $this->doApiRequest( [ 'action' => 'query', @@ -58,12 +56,11 @@ class ApiQueryBlocksTest extends ApiTestCase { $badActor = $this->getTestUser()->getUser(); $sysop = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ - 'address' => $badActor, - 'by' => $sysop, - ] ); - - $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); + $block = $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'targetUser' => $badActor, + 'by' => $sysop, + ] ); [ $data ] = $this->doApiRequest( [ 'action' => 'query', @@ -86,14 +83,13 @@ class ApiQueryBlocksTest extends ApiTestCase { $badActor = $this->getTestUser()->getUser(); $sysop = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ - 'address' => $badActor, - 'by' => $sysop, - 'expiry' => 'infinity', - 'sitewide' => 0, - ] ); - - $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); + $block = $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'targetUser' => $badActor, + 'by' => $sysop, + 'expiry' => 'infinity', + 'sitewide' => 0, + ] ); $subset = [ 'id' => $block->getId(), diff --git a/tests/phpunit/includes/api/query/ApiQueryInfoTest.php b/tests/phpunit/includes/api/query/ApiQueryInfoTest.php index 8b5d2fc3a622..fac1bf1e2fdd 100644 --- a/tests/phpunit/includes/api/query/ApiQueryInfoTest.php +++ b/tests/phpunit/includes/api/query/ApiQueryInfoTest.php @@ -2,7 +2,6 @@ namespace MediaWiki\Tests\Api\Query; -use MediaWiki\Block\DatabaseBlock; use MediaWiki\Context\RequestContext; use MediaWiki\MainConfigNames; use MediaWiki\Tests\Api\ApiTestCase; @@ -217,16 +216,14 @@ class ApiQueryInfoTest extends ApiTestCase { $badActor = $this->getTestUser()->getUser(); $sysop = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ - 'address' => $badActor, - 'by' => $sysop, - 'expiry' => 'infinity', - 'sitewide' => 1, - 'enableAutoblock' => true, - ] ); - - $blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); - $blockStore->insertBlock( $block ); + $block = $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'targetUser' => $badActor, + 'by' => $sysop, + 'expiry' => 'infinity', + 'sitewide' => 1, + 'enableAutoblock' => true, + ] ); $page = $this->getExistingTestPage( 'Pluto' ); $title = $page->getTitle(); @@ -265,15 +262,13 @@ class ApiQueryInfoTest extends ApiTestCase { $sysop = $this->getTestSysop()->getUser(); - $block = new DatabaseBlock( [ - 'address' => $blockIp, - 'by' => $sysop, - 'expiry' => 'infinity', - 'sitewide' => 1, - ] ); - - $blockStore = $this->getServiceContainer()->getDatabaseBlockStore(); - $blockStore->insertBlock( $block ); + $block = $this->getServiceContainer()->getDatabaseBlockStore() + ->insertBlockWithParams( [ + 'address' => $blockIp, + 'by' => $sysop, + 'expiry' => 'infinity', + 'sitewide' => 1, + ] ); $page = $this->getExistingTestPage( 'Pluto' ); $title = $page->getTitle(); |