Merge "phpunit: Avoid use of deprecated getMock for PHPUnit 5 compat"
[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
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->getMockBuilder( User::class )->getMock();
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 }
44
45 public function provideIsAllowed() {
46 return [
47 [
48 'passwordResetRoutes' => [],
49 'enableEmail' => true,
50 'allowsAuthenticationDataChange' => true,
51 'canEditPrivate' => true,
52 'canSeePassword' => true,
53 'userIsBlocked' => false,
54 'isAllowed' => false,
55 ],
56 [
57 'passwordResetRoutes' => [ 'username' => true ],
58 'enableEmail' => false,
59 'allowsAuthenticationDataChange' => true,
60 'canEditPrivate' => true,
61 'canSeePassword' => true,
62 'userIsBlocked' => false,
63 'isAllowed' => false,
64 ],
65 [
66 'passwordResetRoutes' => [ 'username' => true ],
67 'enableEmail' => true,
68 'allowsAuthenticationDataChange' => false,
69 'canEditPrivate' => true,
70 'canSeePassword' => true,
71 'userIsBlocked' => false,
72 'isAllowed' => false,
73 ],
74 [
75 'passwordResetRoutes' => [ 'username' => true ],
76 'enableEmail' => true,
77 'allowsAuthenticationDataChange' => true,
78 'canEditPrivate' => false,
79 'canSeePassword' => true,
80 'userIsBlocked' => false,
81 'isAllowed' => false,
82 ],
83 [
84 'passwordResetRoutes' => [ 'username' => true ],
85 'enableEmail' => true,
86 'allowsAuthenticationDataChange' => true,
87 'canEditPrivate' => true,
88 'canSeePassword' => true,
89 'userIsBlocked' => true,
90 'isAllowed' => false,
91 ],
92 [
93 'passwordResetRoutes' => [ 'username' => true ],
94 'enableEmail' => true,
95 'allowsAuthenticationDataChange' => true,
96 'canEditPrivate' => true,
97 'canSeePassword' => false,
98 'userIsBlocked' => false,
99 'isAllowed' => true,
100 ],
101 [
102 'passwordResetRoutes' => [ 'username' => true ],
103 'enableEmail' => true,
104 'allowsAuthenticationDataChange' => true,
105 'canEditPrivate' => true,
106 'canSeePassword' => true,
107 'userIsBlocked' => false,
108 'isAllowed' => true,
109 ],
110 ];
111 }
112
113 public function testExecute_email() {
114 $config = new HashConfig( [
115 'PasswordResetRoutes' => [ 'username' => true, 'email' => true ],
116 'EnableEmail' => true,
117 ] );
118
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' );
124
125 $request = new FauxRequest();
126 $request->setIP( '1.2.3.4' );
127 $performingUser = $this->getMockBuilder( User::class )->getMock();
128 $performingUser->expects( $this->any() )->method( 'getRequest' )->willReturn( $request );
129 $performingUser->expects( $this->any() )->method( 'isAllowed' )->willReturn( true );
130
131 $targetUser1 = $this->getMockBuilder( User::class )->getMock();
132 $targetUser2 = $this->getMockBuilder( User::class )->getMock();
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' );
139
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 ] );
145
146 $status = $passwordReset->isAllowed( $performingUser );
147 $this->assertTrue( $status->isGood() );
148
149 $status = $passwordReset->execute( $performingUser, null, 'foo@bar.baz' );
150 $this->assertTrue( $status->isGood() );
151 }
152 }