3 use MediaWiki\Auth\AuthManager
;
8 class PasswordResetTest
extends PHPUnit_Framework_TestCase
{
10 * @dataProvider provideIsAllowed
12 public function testIsAllowed( $passwordResetRoutes, $enableEmail,
13 $allowsAuthenticationDataChange, $canEditPrivate, $block, $globalBlock, $isAllowed
15 $config = new HashConfig( [
16 'PasswordResetRoutes' => $passwordResetRoutes,
17 'EnableEmail' => $enableEmail,
20 $authManager = $this->getMockBuilder( AuthManager
::class )->disableOriginalConstructor()
22 $authManager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
23 ->willReturn( $allowsAuthenticationDataChange ? Status
::newGood() : Status
::newFatal( 'foo' ) );
25 $user = $this->getMockBuilder( User
::class )->getMock();
26 $user->expects( $this->any() )->method( 'getName' )->willReturn( 'Foo' );
27 $user->expects( $this->any() )->method( 'getBlock' )->willReturn( $block );
28 $user->expects( $this->any() )->method( 'getGlobalBlock' )->willReturn( $globalBlock );
29 $user->expects( $this->any() )->method( 'isAllowed' )
30 ->will( $this->returnCallback( function ( $perm ) use ( $canEditPrivate ) {
31 if ( $perm === 'editmyprivateinfo' ) {
32 return $canEditPrivate;
34 $this->fail( 'Unexpected permission check' );
38 $passwordReset = new PasswordReset( $config, $authManager );
40 $this->assertSame( $isAllowed, $passwordReset->isAllowed( $user )->isGood() );
43 public function provideIsAllowed() {
46 'passwordResetRoutes' => [],
47 'enableEmail' => true,
48 'allowsAuthenticationDataChange' => true,
49 'canEditPrivate' => true,
51 'globalBlock' => null,
55 'passwordResetRoutes' => [ 'username' => true ],
56 'enableEmail' => false,
57 'allowsAuthenticationDataChange' => true,
58 'canEditPrivate' => true,
60 'globalBlock' => null,
63 'auth data change disabled' => [
64 'passwordResetRoutes' => [ 'username' => true ],
65 'enableEmail' => true,
66 'allowsAuthenticationDataChange' => false,
67 'canEditPrivate' => true,
69 'globalBlock' => null,
72 'cannot edit private data' => [
73 'passwordResetRoutes' => [ 'username' => true ],
74 'enableEmail' => true,
75 'allowsAuthenticationDataChange' => true,
76 'canEditPrivate' => false,
78 'globalBlock' => null,
81 'blocked with account creation disabled' => [
82 'passwordResetRoutes' => [ 'username' => true ],
83 'enableEmail' => true,
84 'allowsAuthenticationDataChange' => true,
85 'canEditPrivate' => true,
86 'block' => new Block( [ 'createAccount' => true ] ),
87 'globalBlock' => null,
90 'blocked w/o account creation disabled' => [
91 'passwordResetRoutes' => [ 'username' => true ],
92 'enableEmail' => true,
93 'allowsAuthenticationDataChange' => true,
94 'canEditPrivate' => true,
95 'block' => new Block( [] ),
96 'globalBlock' => null,
99 'using blocked proxy' => [
100 'passwordResetRoutes' => [ 'username' => true ],
101 'enableEmail' => true,
102 'allowsAuthenticationDataChange' => true,
103 'canEditPrivate' => true,
104 'block' => new Block( [ 'systemBlock' => 'proxy' ] ),
105 'globalBlock' => null,
106 'isAllowed' => false,
108 'globally blocked with account creation disabled' => [
109 'passwordResetRoutes' => [ 'username' => true ],
110 'enableEmail' => true,
111 'allowsAuthenticationDataChange' => true,
112 'canEditPrivate' => true,
114 'globalBlock' => new Block( [ 'systemBlock' => 'global-block', 'createAccount' => true ] ),
115 'isAllowed' => false,
117 'globally blocked with account creation not disabled' => [
118 'passwordResetRoutes' => [ 'username' => true ],
119 'enableEmail' => true,
120 'allowsAuthenticationDataChange' => true,
121 'canEditPrivate' => true,
123 'globalBlock' => new Block( [ 'systemBlock' => 'global-block', 'createAccount' => false ] ),
126 'blocked via wgSoftBlockRanges' => [
127 'passwordResetRoutes' => [ 'username' => true ],
128 'enableEmail' => true,
129 'allowsAuthenticationDataChange' => true,
130 'canEditPrivate' => true,
131 'block' => new Block( [ 'systemBlock' => 'wgSoftBlockRanges', 'anonOnly' => true ] ),
132 'globalBlock' => null,
136 'passwordResetRoutes' => [ 'username' => true ],
137 'enableEmail' => true,
138 'allowsAuthenticationDataChange' => true,
139 'canEditPrivate' => true,
141 'globalBlock' => null,
147 public function testExecute_email() {
148 $config = new HashConfig( [
149 'PasswordResetRoutes' => [ 'username' => true, 'email' => true ],
150 'EnableEmail' => true,
153 $authManager = $this->getMockBuilder( AuthManager
::class )->disableOriginalConstructor()
155 $authManager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
156 ->willReturn( Status
::newGood() );
157 $authManager->expects( $this->exactly( 2 ) )->method( 'changeAuthenticationData' );
159 $request = new FauxRequest();
160 $request->setIP( '1.2.3.4' );
161 $performingUser = $this->getMockBuilder( User
::class )->getMock();
162 $performingUser->expects( $this->any() )->method( 'getRequest' )->willReturn( $request );
163 $performingUser->expects( $this->any() )->method( 'isAllowed' )->willReturn( true );
165 $targetUser1 = $this->getMockBuilder( User
::class )->getMock();
166 $targetUser2 = $this->getMockBuilder( User
::class )->getMock();
167 $targetUser1->expects( $this->any() )->method( 'getName' )->willReturn( 'User1' );
168 $targetUser2->expects( $this->any() )->method( 'getName' )->willReturn( 'User2' );
169 $targetUser1->expects( $this->any() )->method( 'getId' )->willReturn( 1 );
170 $targetUser2->expects( $this->any() )->method( 'getId' )->willReturn( 2 );
171 $targetUser1->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
172 $targetUser2->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
174 $passwordReset = $this->getMockBuilder( PasswordReset
::class )
175 ->setMethods( [ 'getUsersByEmail' ] )->setConstructorArgs( [ $config, $authManager ] )
177 $passwordReset->expects( $this->any() )->method( 'getUsersByEmail' )->with( 'foo@bar.baz' )
178 ->willReturn( [ $targetUser1, $targetUser2 ] );
180 $status = $passwordReset->isAllowed( $performingUser );
181 $this->assertTrue( $status->isGood() );
183 $status = $passwordReset->execute( $performingUser, null, 'foo@bar.baz' );
184 $this->assertTrue( $status->isGood() );