jenkins-bot has submitted this change and it was merged. Change subject: Sanitize global overrides in unit tests ......................................................................
Sanitize global overrides in unit tests Avoid reinventing the wheel by doing manual backups. Change-Id: I41c3e8160169d2f7817dec0d1b36b3db2eaa8f3c --- M client/tests/phpunit/includes/ChangeHandlerTest.php M lib/tests/phpunit/LanguageFallbackChainFactoryTest.php M repo/tests/phpunit/includes/api/BotEditTest.php M repo/tests/phpunit/includes/api/WikibaseApiTestCase.php 4 files changed, 27 insertions(+), 43 deletions(-) Approvals: Aude: Looks good to me, approved jenkins-bot: Verified diff --git a/client/tests/phpunit/includes/ChangeHandlerTest.php b/client/tests/phpunit/includes/ChangeHandlerTest.php index da753e2..249811a 100644 --- a/client/tests/phpunit/includes/ChangeHandlerTest.php +++ b/client/tests/phpunit/includes/ChangeHandlerTest.php @@ -13,7 +13,6 @@ use Wikibase\EntityDiff; use Wikibase\ItemUsageIndex; use Wikibase\Item; -use Wikibase\Property; /** * @covers Wikibase\ChangeHandler @@ -762,27 +761,26 @@ * @dataProvider provideHandleChanges */ public function testHandleChanges() { + global $handleChangeCallCount, $handleChangesCallCount; $changes = func_get_args(); - global $wgHooks; + $testHooks = array( + 'WikibaseHandleChange' => array( function( Change $change ) { + global $handleChangeCallCount; + $handleChangeCallCount++; + return true; + } ), + 'WikibaseHandleChanges' => array( function( array $changes ) { + global $handleChangesCallCount; + $handleChangesCallCount++; + return true; + } ) + ); - $wgHooksOriginal = $wgHooks; + $this->mergeMwGlobalArrayValue( 'wgHooks', $testHooks ); - global $handleChangeCallCount, $handleChangesCallCount; $handleChangeCallCount = 0; $handleChangesCallCount = 0; - - $wgHooks['WikibaseHandleChange'] = array( function( Change $change ) { - global $handleChangeCallCount; - $handleChangeCallCount++; - return true; - } ); - - $wgHooks['WikibaseHandleChanges'] = array( function( array $changes ) { - global $handleChangesCallCount; - $handleChangesCallCount++; - return true; - } ); $changeHandler = $this->getMockBuilder( 'Wikibase\ChangeHandler' ) ->disableOriginalConstructor()->setMethods( array( 'coalesceChanges', 'handleChange' ) )->getMock(); @@ -798,7 +796,8 @@ $this->assertEquals( count( $changes ), $handleChangeCallCount ); $this->assertEquals( 1, $handleChangesCallCount ); - $wgHooks = $wgHooksOriginal; + unset( $handleChangeCallCount ); + unset( $handleChangesCallCount ); } // ========================================================================================== diff --git a/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php b/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php index 53efb5e..dd70f8f 100644 --- a/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php +++ b/lib/tests/phpunit/LanguageFallbackChainFactoryTest.php @@ -35,40 +35,29 @@ } private function setupDisabledVariants( $disabledVariants ) { - global $wgDisabledVariants, $wgLangObjCacheSize; - $originalDisabledVariants = $wgDisabledVariants; - $originalLangObjCacheSize = $wgLangObjCacheSize; - if ( $disabledVariants ) { - $wgDisabledVariants = $disabledVariants; - } - $wgLangObjCacheSize = 0; - return array( $originalDisabledVariants, $originalLangObjCacheSize ); - } - - private function clearDisabledVariants( $state ) { - global $wgDisabledVariants, $wgLangObjCacheSize; - list( $wgDisabledVariants, $wgLangObjCacheSize ) = $state; + $this->setMwGlobals( array( + 'wgDisabledVariants' => $disabledVariants, + 'wgLangObjCacheSize' => 0 + ) ); } /** * @dataProvider providerNewFromLanguage */ - public function testNewFromLanguage( $lang, $mode, $expected, $disabledVariants = null ) { - $state = $this->setupDisabledVariants( $disabledVariants ); + public function testNewFromLanguage( $lang, $mode, $expected, $disabledVariants = array() ) { + $this->setupDisabledVariants( $disabledVariants ); $factory = new LanguageFallbackChainFactory(); $chain = $factory->newFromLanguage( \Language::factory( $lang ), $mode )->getFallbackChain(); - $this->clearDisabledVariants( $state ); $this->assertChainEquals( $expected, $chain ); } /** * @dataProvider providerNewFromLanguage */ - public function testNewFromLanguageCode( $lang, $mode, $expected, $disabledVariants = null ) { - $state = $this->setupDisabledVariants( $disabledVariants ); + public function testNewFromLanguageCode( $lang, $mode, $expected, $disabledVariants = array() ) { + $this->setupDisabledVariants( $disabledVariants ); $factory = new LanguageFallbackChainFactory(); $chain = $factory->newFromLanguageCode( $lang, $mode )->getFallbackChain(); - $this->clearDisabledVariants( $state ); $this->assertChainEquals( $expected, $chain ); } @@ -232,16 +221,15 @@ /** * @dataProvider providerNewFromLanguage */ - public function testNewFromUserAndLanguageCode( $lang, $mode, $expected, $disabledVariants = null ) { + public function testNewFromUserAndLanguageCode( $lang, $mode, $expected, $disabledVariants = array() ) { if ( $mode !== LanguageFallbackChainFactory::FALLBACK_ALL ) { $this->assertTrue( true ); return; } - $state = $this->setupDisabledVariants( $disabledVariants ); + $this->setupDisabledVariants( $disabledVariants ); $factory = new LanguageFallbackChainFactory(); $anon = new \User(); $chain = $factory->newFromUserAndLanguageCode( $anon, $lang )->getFallbackChain(); - $this->clearDisabledVariants( $state ); $this->assertChainEquals( $expected, $chain ); } diff --git a/repo/tests/phpunit/includes/api/BotEditTest.php b/repo/tests/phpunit/includes/api/BotEditTest.php index 38307f7..1e42756 100644 --- a/repo/tests/phpunit/includes/api/BotEditTest.php +++ b/repo/tests/phpunit/includes/api/BotEditTest.php @@ -34,7 +34,6 @@ private static $hasSetup; public function setUp() { - global $wgUser; parent::setUp(); if( !isset( self::$hasSetup ) ){ @@ -48,7 +47,6 @@ 'api_test_...@example.com', array( 'bot' ) ); - $wgUser = self::$users['wbbot']->user; $this->login( 'wbbot' ); } diff --git a/repo/tests/phpunit/includes/api/WikibaseApiTestCase.php b/repo/tests/phpunit/includes/api/WikibaseApiTestCase.php index 08822eb..4dccfa0 100644 --- a/repo/tests/phpunit/includes/api/WikibaseApiTestCase.php +++ b/repo/tests/phpunit/includes/api/WikibaseApiTestCase.php @@ -30,7 +30,6 @@ protected static $token = null; protected function setUp() { - global $wgUser; parent::setUp(); static $isSetup = false; @@ -46,7 +45,7 @@ array( 'wbeditor' ) ); - $wgUser = self::$users['wbeditor']->user; + $this->setMwGlobals( 'wgUser', self::$users['wbeditor']->user ); if ( !$isSetup ) { TestSites::insertIntoDb(); -- To view, visit https://gerrit.wikimedia.org/r/101489 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I41c3e8160169d2f7817dec0d1b36b3db2eaa8f3c Gerrit-PatchSet: 3 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Hoo man <h...@online.de> Gerrit-Reviewer: Addshore <addshorew...@gmail.com> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com> Gerrit-Reviewer: Tobias Gritschacher <tobias.gritschac...@wikimedia.de> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits