Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / tests / phpunit / includes / user / PasswordResetTest.php
blob7ff882a5add5b26a5460ff80cb5ca1ffba33226e
1 <?php
3 use MediaWiki\Auth\AuthManager;
5 /**
6 * @group Database
7 */
8 class PasswordResetTest extends PHPUnit_Framework_TestCase {
9 /**
10 * @dataProvider provideIsAllowed
12 public function testIsAllowed( $passwordResetRoutes, $enableEmail,
13 $allowsAuthenticationDataChange, $canEditPrivate, $canSeePassword,
14 $userIsBlocked, $isAllowed
15 ) {
16 $config = new HashConfig( [
17 'PasswordResetRoutes' => $passwordResetRoutes,
18 'EnableEmail' => $enableEmail,
19 ] );
21 $authManager = $this->getMockBuilder( AuthManager::class )->disableOriginalConstructor()
22 ->getMock();
23 $authManager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
24 ->willReturn( $allowsAuthenticationDataChange ? Status::newGood() : Status::newFatal( 'foo' ) );
26 $user = $this->getMock( User::class );
27 $user->expects( $this->any() )->method( 'getName' )->willReturn( 'Foo' );
28 $user->expects( $this->any() )->method( 'isBlocked' )->willReturn( $userIsBlocked );
29 $user->expects( $this->any() )->method( 'isAllowed' )
30 ->will( $this->returnCallback( function ( $perm ) use ( $canEditPrivate, $canSeePassword ) {
31 if ( $perm === 'editmyprivateinfo' ) {
32 return $canEditPrivate;
33 } elseif ( $perm === 'passwordreset' ) {
34 return $canSeePassword;
35 } else {
36 $this->fail( 'Unexpected permission check' );
38 } ) );
40 $passwordReset = new PasswordReset( $config, $authManager );
42 $this->assertSame( $isAllowed, $passwordReset->isAllowed( $user )->isGood() );
45 public function provideIsAllowed() {
46 return [
48 'passwordResetRoutes' => [],
49 'enableEmail' => true,
50 'allowsAuthenticationDataChange' => true,
51 'canEditPrivate' => true,
52 'canSeePassword' => true,
53 'userIsBlocked' => false,
54 'isAllowed' => false,
57 'passwordResetRoutes' => [ 'username' => true ],
58 'enableEmail' => false,
59 'allowsAuthenticationDataChange' => true,
60 'canEditPrivate' => true,
61 'canSeePassword' => true,
62 'userIsBlocked' => false,
63 'isAllowed' => false,
66 'passwordResetRoutes' => [ 'username' => true ],
67 'enableEmail' => true,
68 'allowsAuthenticationDataChange' => false,
69 'canEditPrivate' => true,
70 'canSeePassword' => true,
71 'userIsBlocked' => false,
72 'isAllowed' => false,
75 'passwordResetRoutes' => [ 'username' => true ],
76 'enableEmail' => true,
77 'allowsAuthenticationDataChange' => true,
78 'canEditPrivate' => false,
79 'canSeePassword' => true,
80 'userIsBlocked' => false,
81 'isAllowed' => false,
84 'passwordResetRoutes' => [ 'username' => true ],
85 'enableEmail' => true,
86 'allowsAuthenticationDataChange' => true,
87 'canEditPrivate' => true,
88 'canSeePassword' => true,
89 'userIsBlocked' => true,
90 'isAllowed' => false,
93 'passwordResetRoutes' => [ 'username' => true ],
94 'enableEmail' => true,
95 'allowsAuthenticationDataChange' => true,
96 'canEditPrivate' => true,
97 'canSeePassword' => false,
98 'userIsBlocked' => false,
99 'isAllowed' => true,
102 'passwordResetRoutes' => [ 'username' => true ],
103 'enableEmail' => true,
104 'allowsAuthenticationDataChange' => true,
105 'canEditPrivate' => true,
106 'canSeePassword' => true,
107 'userIsBlocked' => false,
108 'isAllowed' => true,
113 public function testExecute_email() {
114 $config = new HashConfig( [
115 'PasswordResetRoutes' => [ 'username' => true, 'email' => true ],
116 'EnableEmail' => true,
117 ] );
119 $authManager = $this->getMockBuilder( AuthManager::class )->disableOriginalConstructor()
120 ->getMock();
121 $authManager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
122 ->willReturn( Status::newGood() );
123 $authManager->expects( $this->exactly( 2 ) )->method( 'changeAuthenticationData' );
125 $request = new FauxRequest();
126 $request->setIP( '1.2.3.4' );
127 $performingUser = $this->getMock( User::class );
128 $performingUser->expects( $this->any() )->method( 'getRequest' )->willReturn( $request );
129 $performingUser->expects( $this->any() )->method( 'isAllowed' )->willReturn( true );
131 $targetUser1 = $this->getMock( User::class );
132 $targetUser2 = $this->getMock( User::class );
133 $targetUser1->expects( $this->any() )->method( 'getName' )->willReturn( 'User1' );
134 $targetUser2->expects( $this->any() )->method( 'getName' )->willReturn( 'User2' );
135 $targetUser1->expects( $this->any() )->method( 'getId' )->willReturn( 1 );
136 $targetUser2->expects( $this->any() )->method( 'getId' )->willReturn( 2 );
137 $targetUser1->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
138 $targetUser2->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
140 $passwordReset = $this->getMockBuilder( PasswordReset::class )
141 ->setMethods( [ 'getUsersByEmail' ] )->setConstructorArgs( [ $config, $authManager ] )
142 ->getMock();
143 $passwordReset->expects( $this->any() )->method( 'getUsersByEmail' )->with( 'foo@bar.baz' )
144 ->willReturn( [ $targetUser1, $targetUser2 ] );
146 $status = $passwordReset->isAllowed( $performingUser );
147 $this->assertTrue( $status->isGood() );
149 $status = $passwordReset->execute( $performingUser, null, 'foo@bar.baz' );
150 $this->assertTrue( $status->isGood() );