MediaWiki  1.29.1
PasswordResetTest.php
Go to the documentation of this file.
1 <?php
2 
4 
8 class PasswordResetTest extends PHPUnit_Framework_TestCase {
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 }
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:63
PasswordResetTest
Database.
Definition: PasswordResetTest.php:8
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
MediaWiki\Auth\AuthManager
This serves as the entry point to the authentication system.
Definition: AuthManager.php:82
PasswordResetTest\testIsAllowed
testIsAllowed( $passwordResetRoutes, $enableEmail, $allowsAuthenticationDataChange, $canEditPrivate, $canSeePassword, $userIsBlocked, $isAllowed)
provideIsAllowed
Definition: PasswordResetTest.php:12
PasswordResetTest\provideIsAllowed
provideIsAllowed()
Definition: PasswordResetTest.php:45
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1956
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
PasswordReset
Helper class for the password reset functionality shared by the web UI and the API.
Definition: PasswordReset.php:36
PasswordResetTest\testExecute_email
testExecute_email()
Definition: PasswordResetTest.php:113