MediaWiki  1.27.2
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, $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 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static newFatal($message)
Factory function for fatal errors.
Definition: Status.php:89
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:1798
Helper class for the password reset functionality shared by the web UI and the API.
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 local account $user
Definition: hooks.txt:242
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
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2418
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
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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:1004
testIsAllowed($passwordResetRoutes, $enableEmail, $allowsAuthenticationDataChange, $canEditPrivate, $canSeePassword, $userIsBlocked, $isAllowed, $isAllowedToDisplayPassword)
provideIsAllowed
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
static newGood($value=null)
Factory function for good results.
Definition: Status.php:101