From f0a379891028494a839a0ad26ef3288fa8e51410 Mon Sep 17 00:00:00 2001 From: Huji Lee Date: Mon, 18 Sep 2017 14:57:17 -0400 Subject: [PATCH] Specify a blocker for all UserTest blocks Also, un-register the PerofmrRetroactiveAutoblock hook from CheckUser as it will assume an IP address for the user which is not specified in UserTest. This will keep the unit tests focused on unit testing, as opposed to integration. Bug:T176103 Change-Id: I3a090f7bae5ba266c3cd4500ecf83338fb8503bb --- tests/phpunit/includes/user/UserTest.php | 39 +++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/includes/user/UserTest.php b/tests/phpunit/includes/user/UserTest.php index 52fd3208d0..2721c18e9f 100644 --- a/tests/phpunit/includes/user/UserTest.php +++ b/tests/phpunit/includes/user/UserTest.php @@ -600,7 +600,13 @@ class UserTest extends MediaWikiTestCase { 'wgSecretKey' => MWCryptRand::generateHex( 64, true ), ] ); + // Unregister the hooks for proper unit testing + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'PerformRetroactiveAutoblock' => [] + ] ); + // 1. Log in a test user, and block them. + $userBlocker = $this->getTestSysop()->getUser(); $user1tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1tmp ); @@ -610,6 +616,7 @@ class UserTest extends MediaWikiTestCase { 'expiry' => wfTimestamp( TS_MW, $expiryFiveHours ), ] ); $block->setTarget( $user1tmp ); + $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -640,7 +647,8 @@ class UserTest extends MediaWikiTestCase { $this->assertTrue( $user2->isAnon() ); $this->assertFalse( $user2->isLoggedIn() ); $this->assertTrue( $user2->isBlocked() ); - $this->assertEquals( true, $user2->getBlock()->isAutoblocking() ); // Non-strict type-check. + // Non-strict type-check. + $this->assertEquals( true, $user2->getBlock()->isAutoblocking(), 'Autoblock does not work' ); // Can't directly compare the objects becuase of member type differences. // One day this will work: $this->assertEquals( $block, $user2->getBlock() ); $this->assertEquals( $block->getId(), $user2->getBlock()->getId() ); @@ -673,12 +681,19 @@ class UserTest extends MediaWikiTestCase { 'wgSecretKey' => MWCryptRand::generateHex( 64, true ), ] ); + // Unregister the hooks for proper unit testing + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'PerformRetroactiveAutoblock' => [] + ] ); + // 1. Log in a test user, and block them. + $userBlocker = $this->getTestSysop()->getUser(); $testUser = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $testUser ); $block = new Block( [ 'enableAutoblock' => true ] ); $block->setTarget( $testUser ); + $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user = User::newFromSession( $request1 ); @@ -710,12 +725,20 @@ class UserTest extends MediaWikiTestCase { 'wgCookiePrefix' => 'wm_infinite_block', 'wgSecretKey' => MWCryptRand::generateHex( 64, true ), ] ); + + // Unregister the hooks for proper unit testing + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'PerformRetroactiveAutoblock' => [] + ] ); + // 1. Log in a test user, and block them indefinitely. + $userBlocker = $this->getTestSysop()->getUser(); $user1Tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1Tmp ); $block = new Block( [ 'enableAutoblock' => true, 'expiry' => 'infinity' ] ); $block->setTarget( $user1Tmp ); + $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -798,12 +821,19 @@ class UserTest extends MediaWikiTestCase { 'wgSecretKey' => MWCryptRand::generateHex( 64, true ), ] ); + // Unregister the hooks for proper unit testing + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'PerformRetroactiveAutoblock' => [] + ] ); + // 1. Log in a blocked test user. + $userBlocker = $this->getTestSysop()->getUser(); $user1tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1tmp ); $block = new Block( [ 'enableAutoblock' => true ] ); $block->setTarget( $user1tmp ); + $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); @@ -836,12 +866,19 @@ class UserTest extends MediaWikiTestCase { 'wgSecretKey' => null, ] ); + // Unregister the hooks for proper unit testing + $this->mergeMwGlobalArrayValue( 'wgHooks', [ + 'PerformRetroactiveAutoblock' => [] + ] ); + // 1. Log in a blocked test user. + $userBlocker = $this->getTestSysop()->getUser(); $user1tmp = $this->getTestUser()->getUser(); $request1 = new FauxRequest(); $request1->getSession()->setUser( $user1tmp ); $block = new Block( [ 'enableAutoblock' => true ] ); $block->setTarget( $user1tmp ); + $block->setBlocker( $userBlocker ); $res = $block->insert(); $this->assertTrue( (bool)$res['id'], 'Failed to insert block' ); $user1 = User::newFromSession( $request1 ); -- 2.20.1