Turn PasswordReset into a service
[lhc/web/wiklou.git] / tests / phpunit / includes / user / PasswordResetTest.php
index b0c0fec..b5677fa 100644 (file)
@@ -4,22 +4,32 @@ use MediaWiki\Auth\AuthManager;
 use MediaWiki\Block\DatabaseBlock;
 use MediaWiki\Block\CompositeBlock;
 use MediaWiki\Block\SystemBlock;
+use MediaWiki\Config\ServiceOptions;
+use MediaWiki\Permissions\PermissionManager;
+use Psr\Log\NullLogger;
+use Wikimedia\Rdbms\ILoadBalancer;
 
 /**
  * @covers PasswordReset
  * @group Database
  */
 class PasswordResetTest extends MediaWikiTestCase {
+       private function makeConfig( $enableEmail, array $passwordResetRoutes = [] ) {
+               $hash = new HashConfig( [
+                       'EnableEmail' => $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();
@@ -30,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() );
        }
@@ -183,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', [
@@ -200,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' );
@@ -217,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 ] );