]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - tests/phpunit/includes/auth/CheckBlocksSecondaryAuthenticationProviderTest.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / tests / phpunit / includes / auth / CheckBlocksSecondaryAuthenticationProviderTest.php
1 <?php
2
3 namespace MediaWiki\Auth;
4
5 use Wikimedia\TestingAccessWrapper;
6
7 /**
8  * @group AuthManager
9  * @group Database
10  * @covers MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider
11  */
12 class CheckBlocksSecondaryAuthenticationProviderTest extends \MediaWikiTestCase {
13         public function testConstructor() {
14                 $provider = new CheckBlocksSecondaryAuthenticationProvider();
15                 $providerPriv = TestingAccessWrapper::newFromObject( $provider );
16                 $config = new \HashConfig( [
17                         'BlockDisablesLogin' => false
18                 ] );
19                 $provider->setConfig( $config );
20                 $this->assertSame( false, $providerPriv->blockDisablesLogin );
21
22                 $provider = new CheckBlocksSecondaryAuthenticationProvider(
23                         [ 'blockDisablesLogin' => true ]
24                 );
25                 $providerPriv = TestingAccessWrapper::newFromObject( $provider );
26                 $config = new \HashConfig( [
27                         'BlockDisablesLogin' => false
28                 ] );
29                 $provider->setConfig( $config );
30                 $this->assertSame( true, $providerPriv->blockDisablesLogin );
31         }
32
33         public function testBasics() {
34                 $provider = new CheckBlocksSecondaryAuthenticationProvider();
35                 $user = \User::newFromName( 'UTSysop' );
36
37                 $this->assertEquals(
38                         AuthenticationResponse::newAbstain(),
39                         $provider->beginSecondaryAccountCreation( $user, $user, [] )
40                 );
41         }
42
43         /**
44          * @dataProvider provideGetAuthenticationRequests
45          * @param string $action
46          * @param array $response
47          */
48         public function testGetAuthenticationRequests( $action, $response ) {
49                 $provider = new CheckBlocksSecondaryAuthenticationProvider();
50
51                 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
52         }
53
54         public static function provideGetAuthenticationRequests() {
55                 return [
56                         [ AuthManager::ACTION_LOGIN, [] ],
57                         [ AuthManager::ACTION_CREATE, [] ],
58                         [ AuthManager::ACTION_LINK, [] ],
59                         [ AuthManager::ACTION_CHANGE, [] ],
60                         [ AuthManager::ACTION_REMOVE, [] ],
61                 ];
62         }
63
64         private function getBlockedUser() {
65                 $user = \User::newFromName( 'UTBlockee' );
66                 if ( $user->getID() == 0 ) {
67                         $user->addToDatabase();
68                         \TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
69                         $user->saveSettings();
70                 }
71                 $oldBlock = \Block::newFromTarget( 'UTBlockee' );
72                 if ( $oldBlock ) {
73                         // An old block will prevent our new one from saving.
74                         $oldBlock->delete();
75                 }
76                 $blockOptions = [
77                         'address' => 'UTBlockee',
78                         'user' => $user->getID(),
79                         'reason' => __METHOD__,
80                         'expiry' => time() + 100500,
81                         'createAccount' => true,
82                 ];
83                 $block = new \Block( $blockOptions );
84                 $block->insert();
85                 return $user;
86         }
87
88         public function testBeginSecondaryAuthentication() {
89                 $unblockedUser = \User::newFromName( 'UTSysop' );
90                 $blockedUser = $this->getBlockedUser();
91
92                 $provider = new CheckBlocksSecondaryAuthenticationProvider(
93                         [ 'blockDisablesLogin' => false ]
94                 );
95                 $this->assertEquals(
96                         AuthenticationResponse::newAbstain(),
97                         $provider->beginSecondaryAuthentication( $unblockedUser, [] )
98                 );
99                 $this->assertEquals(
100                         AuthenticationResponse::newAbstain(),
101                         $provider->beginSecondaryAuthentication( $blockedUser, [] )
102                 );
103
104                 $provider = new CheckBlocksSecondaryAuthenticationProvider(
105                         [ 'blockDisablesLogin' => true ]
106                 );
107                 $this->assertEquals(
108                         AuthenticationResponse::newPass(),
109                         $provider->beginSecondaryAuthentication( $unblockedUser, [] )
110                 );
111                 $ret = $provider->beginSecondaryAuthentication( $blockedUser, [] );
112                 $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
113         }
114
115         public function testTestUserForCreation() {
116                 $provider = new CheckBlocksSecondaryAuthenticationProvider(
117                         [ 'blockDisablesLogin' => false ]
118                 );
119                 $provider->setLogger( new \Psr\Log\NullLogger() );
120                 $provider->setConfig( new \HashConfig() );
121                 $provider->setManager( AuthManager::singleton() );
122
123                 $unblockedUser = \User::newFromName( 'UTSysop' );
124                 $blockedUser = $this->getBlockedUser();
125
126                 $user = \User::newFromName( 'RandomUser' );
127
128                 $this->assertEquals(
129                         \StatusValue::newGood(),
130                         $provider->testUserForCreation( $unblockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION )
131                 );
132                 $this->assertEquals(
133                         \StatusValue::newGood(),
134                         $provider->testUserForCreation( $unblockedUser, false )
135                 );
136
137                 $status = $provider->testUserForCreation( $blockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION );
138                 $this->assertInstanceOf( 'StatusValue', $status );
139                 $this->assertFalse( $status->isOK() );
140                 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
141
142                 $status = $provider->testUserForCreation( $blockedUser, false );
143                 $this->assertInstanceOf( 'StatusValue', $status );
144                 $this->assertFalse( $status->isOK() );
145                 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
146         }
147
148         public function testRangeBlock() {
149                 $blockOptions = [
150                         'address' => '127.0.0.0/24',
151                         'reason' => __METHOD__,
152                         'expiry' => time() + 100500,
153                         'createAccount' => true,
154                 ];
155                 $block = new \Block( $blockOptions );
156                 $block->insert();
157                 $scopeVariable = new \Wikimedia\ScopedCallback( [ $block, 'delete' ] );
158
159                 $user = \User::newFromName( 'UTNormalUser' );
160                 if ( $user->getID() == 0 ) {
161                         $user->addToDatabase();
162                         \TestUser::setPasswordForUser( $user, 'UTNormalUserPassword' );
163                         $user->saveSettings();
164                 }
165                 $this->setMwGlobals( [ 'wgUser' => $user ] );
166                 \RequestContext::getMain()->setUser( $user );
167                 $newuser = \User::newFromName( 'RandomUser' );
168
169                 $provider = new CheckBlocksSecondaryAuthenticationProvider(
170                         [ 'blockDisablesLogin' => true ]
171                 );
172                 $provider->setLogger( new \Psr\Log\NullLogger() );
173                 $provider->setConfig( new \HashConfig() );
174                 $provider->setManager( AuthManager::singleton() );
175
176                 $ret = $provider->beginSecondaryAuthentication( $user, [] );
177                 $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
178
179                 $status = $provider->testUserForCreation( $newuser, AuthManager::AUTOCREATE_SOURCE_SESSION );
180                 $this->assertInstanceOf( 'StatusValue', $status );
181                 $this->assertFalse( $status->isOK() );
182                 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
183
184                 $status = $provider->testUserForCreation( $newuser, false );
185                 $this->assertInstanceOf( 'StatusValue', $status );
186                 $this->assertFalse( $status->isOK() );
187                 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
188         }
189 }