Replace usages of deprecated User::isAllowed. Step 2.
[lhc/web/wiklou.git] / tests / phpunit / includes / user / PasswordResetTest.php
index e8334d6..4a7aa05 100644 (file)
@@ -1,6 +1,10 @@
 <?php
 
 use MediaWiki\Auth\AuthManager;
+use MediaWiki\Block\DatabaseBlock;
+use MediaWiki\Block\CompositeBlock;
+use MediaWiki\Block\SystemBlock;
+use MediaWiki\Permissions\PermissionManager;
 
 /**
  * @covers PasswordReset
@@ -27,16 +31,19 @@ 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 );
+
+               $passwordReset = new PasswordReset(
+                       $config,
+                       $authManager,
+                       $permissionManager
+               );
 
                $this->assertSame( $isAllowed, $passwordReset->isAllowed( $user )->isGood() );
        }
@@ -84,7 +91,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,
                        ],
@@ -93,7 +100,7 @@ class PasswordResetTest extends MediaWikiTestCase {
                                'enableEmail' => true,
                                'allowsAuthenticationDataChange' => true,
                                'canEditPrivate' => true,
-                               'block' => new Block( [] ),
+                               'block' => new DatabaseBlock( [] ),
                                'globalBlock' => null,
                                'isAllowed' => true,
                        ],
@@ -102,43 +109,68 @@ class PasswordResetTest extends MediaWikiTestCase {
                                'enableEmail' => true,
                                'allowsAuthenticationDataChange' => true,
                                'canEditPrivate' => true,
-                               'block' => new Block( [ 'systemBlock' => 'proxy' ] ),
+                               'block' => new SystemBlock(
+                                       [ 'systemBlock' => 'proxy' ]
+                               ),
                                'globalBlock' => null,
                                'isAllowed' => false,
                        ],
-                       'globally blocked with account creation disabled' => [
+                       'globally blocked with account creation not disabled' => [
                                'passwordResetRoutes' => [ 'username' => true ],
                                'enableEmail' => true,
                                'allowsAuthenticationDataChange' => true,
                                'canEditPrivate' => true,
                                'block' => null,
-                               'globalBlock' => new Block( [ 'systemBlock' => 'global-block', 'createAccount' => true ] ),
-                               'isAllowed' => false,
+                               'globalBlock' => new SystemBlock(
+                                       [ 'systemBlock' => 'global-block' ]
+                               ),
+                               'isAllowed' => true,
                        ],
-                       'globally blocked with account creation not disabled' => [
+                       'blocked via wgSoftBlockRanges' => [
                                'passwordResetRoutes' => [ 'username' => true ],
                                'enableEmail' => true,
                                'allowsAuthenticationDataChange' => true,
                                'canEditPrivate' => true,
-                               'block' => null,
-                               'globalBlock' => new Block( [ 'systemBlock' => 'global-block', 'createAccount' => false ] ),
+                               'block' => new SystemBlock(
+                                       [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ]
+                               ),
+                               'globalBlock' => null,
                                'isAllowed' => true,
                        ],
-                       'blocked via wgSoftBlockRanges' => [
+                       'blocked with an unknown system block type' => [
                                'passwordResetRoutes' => [ 'username' => true ],
                                'enableEmail' => true,
                                'allowsAuthenticationDataChange' => true,
                                'canEditPrivate' => true,
-                               'block' => new Block( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ),
+                               'block' => new SystemBlock( [ 'systemBlock' => 'unknown' ] ),
+                               '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 an unknown system block type' => [
+                       'blocked with multiple blocks, not all allowing password reset' => [
                                'passwordResetRoutes' => [ 'username' => true ],
                                'enableEmail' => true,
                                'allowsAuthenticationDataChange' => true,
                                'canEditPrivate' => true,
-                               'block' => new Block( [ 'systemBlock' => 'unknown' ] ),
+                               'block' => new CompositeBlock( [
+                                       'originalBlocks' => [
+                                               new SystemBlock( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ),
+                                               new SystemBlock( [ 'systemBlock' => 'proxy' ] ),
+                                       ]
+                               ] ),
                                'globalBlock' => null,
                                'isAllowed' => false,
                        ],
@@ -176,9 +208,16 @@ class PasswordResetTest extends MediaWikiTestCase {
                $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' );
@@ -189,7 +228,8 @@ class PasswordResetTest extends MediaWikiTestCase {
                $targetUser2->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
 
                $passwordReset = $this->getMockBuilder( PasswordReset::class )
-                       ->setMethods( [ 'getUsersByEmail' ] )->setConstructorArgs( [ $config, $authManager ] )
+                       ->setConstructorArgs( [ $config, $authManager, $permissionManager ] )
+                       ->setMethods( [ 'getUsersByEmail' ] )
                        ->getMock();
                $passwordReset->expects( $this->any() )->method( 'getUsersByEmail' )->with( 'foo@bar.baz' )
                        ->willReturn( [ $targetUser1, $targetUser2 ] );