Merge "mediawiki.jqueryMsg: Allow logging errors through mw.track"
[lhc/web/wiklou.git] / tests / phpunit / includes / user / PasswordResetTest.php
1 <?php
2
3 use MediaWiki\Auth\AuthManager;
4
5 /**
6 * @group Database
7 */
8 class PasswordResetTest extends PHPUnit_Framework_TestCase {
9 /**
10 * @dataProvider provideIsAllowed
11 */
12 public function testIsAllowed( $passwordResetRoutes, $enableEmail,
13 $allowsAuthenticationDataChange, $canEditPrivate, $canSeePassword,
14 $userIsBlocked, $isAllowed, $isAllowedToDisplayPassword
15 ) {
16 $config = new HashConfig( [
17 'PasswordResetRoutes' => $passwordResetRoutes,
18 'EnableEmail' => $enableEmail,
19 ] );
20
21 $authManager = $this->getMockBuilder( AuthManager::class )->disableOriginalConstructor()
22 ->getMock();
23 $authManager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
24 ->willReturn( $allowsAuthenticationDataChange ? Status::newGood() : Status::newFatal( 'foo' ) );
25
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' );
37 }
38 } ) );
39
40 $passwordReset = new PasswordReset( $config, $authManager );
41
42 $this->assertSame( $isAllowed, $passwordReset->isAllowed( $user )->isGood() );
43 $this->assertSame( $isAllowedToDisplayPassword,
44 $passwordReset->isAllowed( $user, true )->isGood() );
45 }
46
47 public function provideIsAllowed() {
48 return [
49 [
50 'passwordResetRoutes' => [],
51 'enableEmail' => true,
52 'allowsAuthenticationDataChange' => true,
53 'canEditPrivate' => true,
54 'canSeePassword' => true,
55 'userIsBlocked' => false,
56 'isAllowed' => false,
57 'isAllowedToDisplayPassword' => false,
58 ],
59 [
60 'passwordResetRoutes' => [ 'username' => true ],
61 'enableEmail' => false,
62 'allowsAuthenticationDataChange' => true,
63 'canEditPrivate' => true,
64 'canSeePassword' => true,
65 'userIsBlocked' => false,
66 'isAllowed' => false,
67 'isAllowedToDisplayPassword' => false,
68 ],
69 [
70 'passwordResetRoutes' => [ 'username' => true ],
71 'enableEmail' => true,
72 'allowsAuthenticationDataChange' => false,
73 'canEditPrivate' => true,
74 'canSeePassword' => true,
75 'userIsBlocked' => false,
76 'isAllowed' => false,
77 'isAllowedToDisplayPassword' => false,
78 ],
79 [
80 'passwordResetRoutes' => [ 'username' => true ],
81 'enableEmail' => true,
82 'allowsAuthenticationDataChange' => true,
83 'canEditPrivate' => false,
84 'canSeePassword' => true,
85 'userIsBlocked' => false,
86 'isAllowed' => false,
87 'isAllowedToDisplayPassword' => false,
88 ],
89 [
90 'passwordResetRoutes' => [ 'username' => true ],
91 'enableEmail' => true,
92 'allowsAuthenticationDataChange' => true,
93 'canEditPrivate' => true,
94 'canSeePassword' => true,
95 'userIsBlocked' => true,
96 'isAllowed' => false,
97 'isAllowedToDisplayPassword' => false,
98 ],
99 [
100 'passwordResetRoutes' => [ 'username' => true ],
101 'enableEmail' => true,
102 'allowsAuthenticationDataChange' => true,
103 'canEditPrivate' => true,
104 'canSeePassword' => false,
105 'userIsBlocked' => false,
106 'isAllowed' => true,
107 'isAllowedToDisplayPassword' => false,
108 ],
109 [
110 'passwordResetRoutes' => [ 'username' => true ],
111 'enableEmail' => true,
112 'allowsAuthenticationDataChange' => true,
113 'canEditPrivate' => true,
114 'canSeePassword' => true,
115 'userIsBlocked' => false,
116 'isAllowed' => true,
117 'isAllowedToDisplayPassword' => true,
118 ],
119 ];
120 }
121
122 public function testExecute_email() {
123 $config = new HashConfig( [
124 'PasswordResetRoutes' => [ 'username' => true, 'email' => true ],
125 'EnableEmail' => true,
126 ] );
127
128 $authManager = $this->getMockBuilder( AuthManager::class )->disableOriginalConstructor()
129 ->getMock();
130 $authManager->expects( $this->any() )->method( 'allowsAuthenticationDataChange' )
131 ->willReturn( Status::newGood() );
132 $authManager->expects( $this->exactly( 2 ) )->method( 'changeAuthenticationData' );
133
134 $request = new FauxRequest();
135 $request->setIP( '1.2.3.4' );
136 $performingUser = $this->getMock( User::class );
137 $performingUser->expects( $this->any() )->method( 'getRequest' )->willReturn( $request );
138 $performingUser->expects( $this->any() )->method( 'isAllowed' )->willReturn( true );
139
140 $targetUser1 = $this->getMock( User::class );
141 $targetUser2 = $this->getMock( User::class );
142 $targetUser1->expects( $this->any() )->method( 'getName' )->willReturn( 'User1' );
143 $targetUser2->expects( $this->any() )->method( 'getName' )->willReturn( 'User2' );
144 $targetUser1->expects( $this->any() )->method( 'getId' )->willReturn( 1 );
145 $targetUser2->expects( $this->any() )->method( 'getId' )->willReturn( 2 );
146 $targetUser1->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
147 $targetUser2->expects( $this->any() )->method( 'getEmail' )->willReturn( 'foo@bar.baz' );
148
149 $passwordReset = $this->getMockBuilder( PasswordReset::class )
150 ->setMethods( [ 'getUsersByEmail' ] )->setConstructorArgs( [ $config, $authManager ] )
151 ->getMock();
152 $passwordReset->expects( $this->any() )->method( 'getUsersByEmail' )->with( 'foo@bar.baz' )
153 ->willReturn( [ $targetUser1, $targetUser2 ] );
154
155 $status = $passwordReset->isAllowed( $performingUser );
156 $this->assertTrue( $status->isGood() );
157
158 $status = $passwordReset->execute( $performingUser, null, 'foo@bar.baz' );
159 $this->assertTrue( $status->isGood() );
160 }
161 }