From 9f973228d5d31cb88156e2958193cdd9a3338a34 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 9 May 2019 12:13:30 +0200 Subject: [PATCH] Restore ApiQueryUserInfo::getBlockInfo() as a stub. Fixes unintended breaking change made by I84ed21641c44b2f65ebe. ApiQueryUserInfo::getBlockInfo() is restoed as a hard deprecated stub. This renames the method in the new ApiBlockInfoTrait to getBlockDetails. Depends-On: I9f40666a31bd4af50762c197c2ce5bf089a5e68c Change-Id: If47a93878f87d69800e5f305404c22528dac5e94 --- includes/api/ApiBase.php | 10 ++-- includes/api/ApiBlock.php | 2 +- includes/api/ApiBlockInfoTrait.php | 2 +- includes/api/ApiQueryUserInfo.php | 22 +++++++- includes/api/ApiUnblock.php | 2 +- tests/phpunit/includes/api/ApiBaseTest.php | 4 +- .../includes/api/ApiBlockInfoTraitTest.php | 8 +-- .../includes/api/ApiQueryUserInfoTest.php | 51 +++++++++++++++++++ 8 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 tests/phpunit/includes/api/ApiQueryUserInfoTest.php diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index bc62906ab9..b76a4eaa45 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -1816,7 +1816,7 @@ abstract class ApiBase extends ContextSource { if ( is_string( $error[0] ) && isset( self::$blockMsgMap[$error[0]] ) && $user->getBlock() ) { list( $msg, $code ) = self::$blockMsgMap[$error[0]]; $status->fatal( ApiMessage::create( $msg, $code, - [ 'blockinfo' => $this->getBlockInfo( $user->getBlock() ) ] + [ 'blockinfo' => $this->getBlockDetails( $user->getBlock() ) ] ) ); } else { $status->fatal( ...$error ); @@ -1839,7 +1839,7 @@ abstract class ApiBase extends ContextSource { foreach ( self::$blockMsgMap as $msg => list( $apiMsg, $code ) ) { if ( $status->hasMessage( $msg ) && $user->getBlock() ) { $status->replaceMessage( $msg, ApiMessage::create( $apiMsg, $code, - [ 'blockinfo' => $this->getBlockInfo( $user->getBlock() ) ] + [ 'blockinfo' => $this->getBlockDetails( $user->getBlock() ) ] ) ); } } @@ -2038,19 +2038,19 @@ abstract class ApiBase extends ContextSource { $this->dieWithError( 'apierror-autoblocked', 'autoblocked', - [ 'blockinfo' => $this->getBlockInfo( $block ) ] + [ 'blockinfo' => $this->getBlockDetails( $block ) ] ); } elseif ( !$block->isSitewide() ) { $this->dieWithError( 'apierror-blocked-partial', 'blocked', - [ 'blockinfo' => $this->getBlockInfo( $block ) ] + [ 'blockinfo' => $this->getBlockDetails( $block ) ] ); } else { $this->dieWithError( 'apierror-blocked', 'blocked', - [ 'blockinfo' => $this->getBlockInfo( $block ) ] + [ 'blockinfo' => $this->getBlockDetails( $block ) ] ); } } diff --git a/includes/api/ApiBlock.php b/includes/api/ApiBlock.php index 336943d054..f7c2bcef01 100644 --- a/includes/api/ApiBlock.php +++ b/includes/api/ApiBlock.php @@ -52,7 +52,7 @@ class ApiBlock extends ApiBase { $this->dieWithError( $status, null, - [ 'blockinfo' => $this->getBlockInfo( $block ) ] + [ 'blockinfo' => $this->getBlockDetails( $block ) ] ); } } diff --git a/includes/api/ApiBlockInfoTrait.php b/includes/api/ApiBlockInfoTrait.php index 51da835d6f..d16cd21e30 100644 --- a/includes/api/ApiBlockInfoTrait.php +++ b/includes/api/ApiBlockInfoTrait.php @@ -38,7 +38,7 @@ trait ApiBlockInfoTrait { * - blockexpiry - expiry time of the block * - systemblocktype - system block type, if any */ - private function getBlockInfo( AbstractBlock $block ) { + private function getBlockDetails( AbstractBlock $block ) { $vals = []; $vals['blockid'] = $block->getId(); $vals['blockedby'] = $block->getByName(); diff --git a/includes/api/ApiQueryUserInfo.php b/includes/api/ApiQueryUserInfo.php index c495c6db13..d73fbc4d32 100644 --- a/includes/api/ApiQueryUserInfo.php +++ b/includes/api/ApiQueryUserInfo.php @@ -52,6 +52,26 @@ class ApiQueryUserInfo extends ApiQueryBase { $result->addValue( 'query', $this->getModuleName(), $r ); } + /** + * Get basic info about a given block + * + * @deprecated since 1.34 Use ApiBlockInfoTrait::getBlockDetails() instead. + * @param Block $block + * @return array See ApiBlockInfoTrait::getBlockDetails + */ + public static function getBlockInfo( Block $block ) { + wfDeprecated( __METHOD__, '1.34' ); + + // Hack to access a private method from a trait: + $dummy = new class { + use ApiBlockInfoTrait { + getBlockDetails as public; + } + }; + + return $dummy->getBlockDetails( $block ); + } + /** * Get central user info * @param Config $config @@ -104,7 +124,7 @@ class ApiQueryUserInfo extends ApiQueryBase { if ( isset( $this->prop['blockinfo'] ) ) { $block = $user->getBlock(); if ( $block ) { - $vals = array_merge( $vals, $this->getBlockInfo( $block ) ); + $vals = array_merge( $vals, $this->getBlockDetails( $block ) ); } } diff --git a/includes/api/ApiUnblock.php b/includes/api/ApiUnblock.php index f038b9683b..1c72b67280 100644 --- a/includes/api/ApiUnblock.php +++ b/includes/api/ApiUnblock.php @@ -50,7 +50,7 @@ class ApiUnblock extends ApiBase { $this->dieWithError( $status, null, - [ 'blockinfo' => $this->getBlockInfo( $block ) ] + [ 'blockinfo' => $this->getBlockDetails( $block ) ] ); } } diff --git a/tests/phpunit/includes/api/ApiBaseTest.php b/tests/phpunit/includes/api/ApiBaseTest.php index 4e19822d94..64b2cd651c 100644 --- a/tests/phpunit/includes/api/ApiBaseTest.php +++ b/tests/phpunit/includes/api/ApiBaseTest.php @@ -1336,7 +1336,7 @@ class ApiBaseTest extends ApiTestCase { $userInfoTrait = TestingAccessWrapper::newFromObject( $this->getMockForTrait( ApiBlockInfoTrait::class ) ); - $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockInfo( $block ) ]; + $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockDetails( $block ) ]; $expect = Status::newGood(); $expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) ); @@ -1394,7 +1394,7 @@ class ApiBaseTest extends ApiTestCase { $userInfoTrait = TestingAccessWrapper::newFromObject( $this->getObjectForTrait( ApiBlockInfoTrait::class ) ); - $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockInfo( $block ) ]; + $blockinfo = [ 'blockinfo' => $userInfoTrait->getBlockDetails( $block ) ]; $expect = Status::newGood(); $expect->fatal( ApiMessage::create( 'apierror-blocked', 'blocked', $blockinfo ) ); diff --git a/tests/phpunit/includes/api/ApiBlockInfoTraitTest.php b/tests/phpunit/includes/api/ApiBlockInfoTraitTest.php index 932495a6c2..a6e2d0de0b 100644 --- a/tests/phpunit/includes/api/ApiBlockInfoTraitTest.php +++ b/tests/phpunit/includes/api/ApiBlockInfoTraitTest.php @@ -8,11 +8,11 @@ use MediaWiki\Block\SystemBlock; */ class ApiBlockInfoTraitTest extends MediaWikiTestCase { /** - * @dataProvider provideGetBlockInfo + * @dataProvider provideGetBlockDetails */ - public function testGetBlockInfo( $block, $expectedInfo ) { + public function testGetBlockDetails( $block, $expectedInfo ) { $mock = $this->getMockForTrait( ApiBlockInfoTrait::class ); - $info = TestingAccessWrapper::newFromObject( $mock )->getBlockInfo( $block ); + $info = TestingAccessWrapper::newFromObject( $mock )->getBlockDetails( $block ); $subset = array_merge( [ 'blockid' => null, 'blockedby' => '', @@ -23,7 +23,7 @@ class ApiBlockInfoTraitTest extends MediaWikiTestCase { $this->assertArraySubset( $subset, $info ); } - public static function provideGetBlockInfo() { + public static function provideGetBlockDetails() { return [ 'Sitewide block' => [ new Block(), diff --git a/tests/phpunit/includes/api/ApiQueryUserInfoTest.php b/tests/phpunit/includes/api/ApiQueryUserInfoTest.php new file mode 100644 index 0000000000..703c1508d9 --- /dev/null +++ b/tests/phpunit/includes/api/ApiQueryUserInfoTest.php @@ -0,0 +1,51 @@ +hideDeprecated( 'ApiQueryUserInfo::getBlockInfo' ); + + $apiQueryUserInfo = new ApiQueryUserInfo( + new ApiQuery( new ApiMain( $this->apiContext ), 'userinfo' ), + 'userinfo' + ); + + $block = new Block(); + $info = $apiQueryUserInfo->getBlockInfo( $block ); + $subset = [ + 'blockid' => null, + 'blockedby' => '', + 'blockedbyid' => 0, + 'blockreason' => '', + 'blockexpiry' => 'infinite', + 'blockpartial' => false, + ]; + $this->assertArraySubset( $subset, $info ); + } + + public function testGetBlockInfoPartial() { + $this->hideDeprecated( 'ApiQueryUserInfo::getBlockInfo' ); + + $apiQueryUserInfo = new ApiQueryUserInfo( + new ApiQuery( new ApiMain( $this->apiContext ), 'userinfo' ), + 'userinfo' + ); + + $block = new Block( [ + 'sitewide' => false, + ] ); + $info = $apiQueryUserInfo->getBlockInfo( $block ); + $subset = [ + 'blockid' => null, + 'blockedby' => '', + 'blockedbyid' => 0, + 'blockreason' => '', + 'blockexpiry' => 'infinite', + 'blockpartial' => true, + ]; + $this->assertArraySubset( $subset, $info ); + } +} -- 2.20.1