X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fuser%2FPasswordResetTest.php;h=b5677fa99fe44da49084f266c372f49a8adb5989;hp=ca57c10b6f5ae9e2b2555adabbfe0d0a2bd5cad4;hb=f6633d9f46951e57e5c7060953194aeeb7522024;hpb=447574ceb746c2f4026a8bf77632bdc4604314bb diff --git a/tests/phpunit/includes/user/PasswordResetTest.php b/tests/phpunit/includes/user/PasswordResetTest.php index ca57c10b6f..b5677fa99f 100644 --- a/tests/phpunit/includes/user/PasswordResetTest.php +++ b/tests/phpunit/includes/user/PasswordResetTest.php @@ -1,23 +1,35 @@ $enableEmail, + 'PasswordResetRoutes' => $passwordResetRoutes, + ] ); + + return new ServiceOptions( PasswordReset::$constructorOptions, $hash ); + } + /** * @dataProvider provideIsAllowed */ public function testIsAllowed( $passwordResetRoutes, $enableEmail, $allowsAuthenticationDataChange, $canEditPrivate, $block, $globalBlock, $isAllowed ) { - $config = new HashConfig( [ - 'PasswordResetRoutes' => $passwordResetRoutes, - 'EnableEmail' => $enableEmail, - ] ); + $config = $this->makeConfig( $enableEmail, $passwordResetRoutes ); $authManager = $this->getMockBuilder( AuthManager::class )->disableOriginalConstructor() ->getMock(); @@ -28,16 +40,23 @@ class PasswordResetTest extends MediaWikiTestCase { $user->expects( $this->any() )->method( 'getName' )->willReturn( 'Foo' ); $user->expects( $this->any() )->method( 'getBlock' )->willReturn( $block ); $user->expects( $this->any() )->method( 'getGlobalBlock' )->willReturn( $globalBlock ); - $user->expects( $this->any() )->method( 'isAllowed' ) - ->will( $this->returnCallback( function ( $perm ) use ( $canEditPrivate ) { - if ( $perm === 'editmyprivateinfo' ) { - return $canEditPrivate; - } else { - $this->fail( 'Unexpected permission check' ); - } - } ) ); - $passwordReset = new PasswordReset( $config, $authManager ); + $permissionManager = $this->getMockBuilder( PermissionManager::class ) + ->disableOriginalConstructor() + ->getMock(); + $permissionManager->method( 'userHasRight' ) + ->with( $user, 'editmyprivateinfo' ) + ->willReturn( $canEditPrivate ); + + $loadBalancer = $this->getMockBuilder( ILoadBalancer::class )->getMock(); + + $passwordReset = new PasswordReset( + $config, + $authManager, + $permissionManager, + $loadBalancer, + new NullLogger() + ); $this->assertSame( $isAllowed, $passwordReset->isAllowed( $user )->isGood() ); } @@ -85,7 +104,7 @@ class PasswordResetTest extends MediaWikiTestCase { 'enableEmail' => true, 'allowsAuthenticationDataChange' => true, 'canEditPrivate' => true, - 'block' => new Block( [ 'createAccount' => true ] ), + 'block' => new DatabaseBlock( [ 'createAccount' => true ] ), 'globalBlock' => null, 'isAllowed' => false, ], @@ -94,7 +113,7 @@ class PasswordResetTest extends MediaWikiTestCase { 'enableEmail' => true, 'allowsAuthenticationDataChange' => true, 'canEditPrivate' => true, - 'block' => new Block( [] ), + 'block' => new DatabaseBlock( [] ), 'globalBlock' => null, 'isAllowed' => true, ], @@ -140,6 +159,34 @@ class PasswordResetTest extends MediaWikiTestCase { 'globalBlock' => null, 'isAllowed' => false, ], + 'blocked with multiple blocks, all allowing password reset' => [ + 'passwordResetRoutes' => [ 'username' => true ], + 'enableEmail' => true, + 'allowsAuthenticationDataChange' => true, + 'canEditPrivate' => true, + 'block' => new CompositeBlock( [ + 'originalBlocks' => [ + new SystemBlock( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ), + new Block( [] ), + ] + ] ), + 'globalBlock' => null, + 'isAllowed' => true, + ], + 'blocked with multiple blocks, not all allowing password reset' => [ + 'passwordResetRoutes' => [ 'username' => true ], + 'enableEmail' => true, + 'allowsAuthenticationDataChange' => true, + 'canEditPrivate' => true, + 'block' => new CompositeBlock( [ + 'originalBlocks' => [ + new SystemBlock( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ), + new SystemBlock( [ 'systemBlock' => 'proxy' ] ), + ] + ] ), + 'globalBlock' => null, + 'isAllowed' => false, + ], 'all OK' => [ 'passwordResetRoutes' => [ 'username' => true ], 'enableEmail' => true, @@ -153,10 +200,7 @@ class PasswordResetTest extends MediaWikiTestCase { } public function testExecute_email() { - $config = new HashConfig( [ - 'PasswordResetRoutes' => [ 'username' => true, 'email' => true ], - 'EnableEmail' => true, - ] ); + $config = $this->makeConfig( true, [ 'username' => true, 'email' => true ] ); // Unregister the hooks for proper unit testing $this->mergeMwGlobalArrayValue( 'wgHooks', [ @@ -170,13 +214,28 @@ class PasswordResetTest extends MediaWikiTestCase { ->willReturn( Status::newGood() ); $authManager->expects( $this->exactly( 2 ) )->method( 'changeAuthenticationData' ); + $permissionManager = $this->getMockBuilder( PermissionManager::class ) + ->disableOriginalConstructor() + ->getMock(); + $permissionManager->method( 'userHasRight' )->willReturn( true ); + + $loadBalancer = $this->getMockBuilder( ILoadBalancer::class ) + ->getMock(); + $request = new FauxRequest(); $request->setIP( '1.2.3.4' ); $performingUser = $this->getMockBuilder( User::class )->getMock(); $performingUser->expects( $this->any() )->method( 'getRequest' )->willReturn( $request ); - $performingUser->expects( $this->any() )->method( 'isAllowed' )->willReturn( true ); $performingUser->expects( $this->any() )->method( 'getName' )->willReturn( 'Performer' ); + $permissionManager = $this->getMockBuilder( PermissionManager::class ) + ->disableOriginalConstructor() + ->getMock(); + $permissionManager->expects( $this->once() ) + ->method( 'userHasRight' ) + ->with( $performingUser, 'editmyprivateinfo' ) + ->willReturn( true ); + $targetUser1 = $this->getMockBuilder( User::class )->getMock(); $targetUser2 = $this->getMockBuilder( User::class )->getMock(); $targetUser1->expects( $this->any() )->method( 'getName' )->willReturn( 'User1' ); @@ -187,7 +246,14 @@ class PasswordResetTest extends MediaWikiTestCase { $targetUser2->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' ); $passwordReset = $this->getMockBuilder( PasswordReset::class ) - ->setMethods( [ 'getUsersByEmail' ] )->setConstructorArgs( [ $config, $authManager ] ) + ->setMethods( [ 'getUsersByEmail' ] ) + ->setConstructorArgs( [ + $config, + $authManager, + $permissionManager, + $loadBalancer, + new NullLogger() + ] ) ->getMock(); $passwordReset->expects( $this->any() )->method( 'getUsersByEmail' )->with( 'foo@bar.baz' ) ->willReturn( [ $targetUser1, $targetUser2 ] );