3 namespace MediaWiki\Auth
;
5 use MediaWiki\Block\DatabaseBlock
;
6 use Wikimedia\TestingAccessWrapper
;
11 * @covers \MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider
13 class CheckBlocksSecondaryAuthenticationProviderTest
extends \MediaWikiTestCase
{
14 public function testConstructor() {
15 $provider = new CheckBlocksSecondaryAuthenticationProvider();
16 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
17 $config = new \
HashConfig( [
18 'BlockDisablesLogin' => false
20 $provider->setConfig( $config );
21 $this->assertSame( false, $providerPriv->blockDisablesLogin
);
23 $provider = new CheckBlocksSecondaryAuthenticationProvider(
24 [ 'blockDisablesLogin' => true ]
26 $providerPriv = TestingAccessWrapper
::newFromObject( $provider );
27 $config = new \
HashConfig( [
28 'BlockDisablesLogin' => false
30 $provider->setConfig( $config );
31 $this->assertSame( true, $providerPriv->blockDisablesLogin
);
34 public function testBasics() {
35 $provider = new CheckBlocksSecondaryAuthenticationProvider();
36 $user = \User
::newFromName( 'UTSysop' );
39 AuthenticationResponse
::newAbstain(),
40 $provider->beginSecondaryAccountCreation( $user, $user, [] )
45 * @dataProvider provideGetAuthenticationRequests
46 * @param string $action
47 * @param array $response
49 public function testGetAuthenticationRequests( $action, $response ) {
50 $provider = new CheckBlocksSecondaryAuthenticationProvider();
52 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
55 public static function provideGetAuthenticationRequests() {
57 [ AuthManager
::ACTION_LOGIN
, [] ],
58 [ AuthManager
::ACTION_CREATE
, [] ],
59 [ AuthManager
::ACTION_LINK
, [] ],
60 [ AuthManager
::ACTION_CHANGE
, [] ],
61 [ AuthManager
::ACTION_REMOVE
, [] ],
65 private function getBlockedUser() {
66 $user = \User
::newFromName( 'UTBlockee' );
67 if ( $user->getID() == 0 ) {
68 $user->addToDatabase();
69 \TestUser
::setPasswordForUser( $user, 'UTBlockeePassword' );
70 $user->saveSettings();
72 $oldBlock = DatabaseBlock
::newFromTarget( 'UTBlockee' );
74 // An old block will prevent our new one from saving.
78 'address' => 'UTBlockee',
79 'user' => $user->getID(),
80 'by' => $this->getTestSysop()->getUser()->getId(),
81 'reason' => __METHOD__
,
82 'expiry' => time() +
100500,
83 'createAccount' => true,
85 $block = new DatabaseBlock( $blockOptions );
90 public function testBeginSecondaryAuthentication() {
91 $unblockedUser = \User
::newFromName( 'UTSysop' );
92 $blockedUser = $this->getBlockedUser();
94 $provider = new CheckBlocksSecondaryAuthenticationProvider(
95 [ 'blockDisablesLogin' => false ]
98 AuthenticationResponse
::newAbstain(),
99 $provider->beginSecondaryAuthentication( $unblockedUser, [] )
102 AuthenticationResponse
::newAbstain(),
103 $provider->beginSecondaryAuthentication( $blockedUser, [] )
106 $provider = new CheckBlocksSecondaryAuthenticationProvider(
107 [ 'blockDisablesLogin' => true ]
110 AuthenticationResponse
::newPass(),
111 $provider->beginSecondaryAuthentication( $unblockedUser, [] )
113 $ret = $provider->beginSecondaryAuthentication( $blockedUser, [] );
114 $this->assertEquals( AuthenticationResponse
::FAIL
, $ret->status
);
117 public function testTestUserForCreation() {
118 $provider = new CheckBlocksSecondaryAuthenticationProvider(
119 [ 'blockDisablesLogin' => false ]
121 $provider->setLogger( new \Psr\Log\
NullLogger() );
122 $provider->setConfig( new \
HashConfig() );
123 $provider->setManager( AuthManager
::singleton() );
125 $unblockedUser = \User
::newFromName( 'UTSysop' );
126 $blockedUser = $this->getBlockedUser();
128 $user = \User
::newFromName( 'RandomUser' );
131 \StatusValue
::newGood(),
132 $provider->testUserForCreation( $unblockedUser, AuthManager
::AUTOCREATE_SOURCE_SESSION
)
135 \StatusValue
::newGood(),
136 $provider->testUserForCreation( $unblockedUser, false )
139 $status = $provider->testUserForCreation( $blockedUser, AuthManager
::AUTOCREATE_SOURCE_SESSION
);
140 $this->assertInstanceOf( \StatusValue
::class, $status );
141 $this->assertFalse( $status->isOK() );
142 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
144 $status = $provider->testUserForCreation( $blockedUser, false );
145 $this->assertInstanceOf( \StatusValue
::class, $status );
146 $this->assertFalse( $status->isOK() );
147 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
150 public function testRangeBlock() {
152 'address' => '127.0.0.0/24',
153 'reason' => __METHOD__
,
154 'by' => $this->getTestSysop()->getUser()->getId(),
155 'expiry' => time() +
100500,
156 'createAccount' => true,
158 $block = new DatabaseBlock( $blockOptions );
160 $scopeVariable = new \Wikimedia\
ScopedCallback( [ $block, 'delete' ] );
162 $user = \User
::newFromName( 'UTNormalUser' );
163 if ( $user->getID() == 0 ) {
164 $user->addToDatabase();
165 \TestUser
::setPasswordForUser( $user, 'UTNormalUserPassword' );
166 $user->saveSettings();
168 $this->setMwGlobals( [ 'wgUser' => $user ] );
169 \RequestContext
::getMain()->setUser( $user );
170 $newuser = \User
::newFromName( 'RandomUser' );
172 $provider = new CheckBlocksSecondaryAuthenticationProvider(
173 [ 'blockDisablesLogin' => true ]
175 $provider->setLogger( new \Psr\Log\
NullLogger() );
176 $provider->setConfig( new \
HashConfig() );
177 $provider->setManager( AuthManager
::singleton() );
179 $ret = $provider->beginSecondaryAuthentication( $user, [] );
180 $this->assertEquals( AuthenticationResponse
::FAIL
, $ret->status
);
182 $status = $provider->testUserForCreation( $newuser, AuthManager
::AUTOCREATE_SOURCE_SESSION
);
183 $this->assertInstanceOf( \StatusValue
::class, $status );
184 $this->assertFalse( $status->isOK() );
185 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
187 $status = $provider->testUserForCreation( $newuser, false );
188 $this->assertInstanceOf( \StatusValue
::class, $status );
189 $this->assertFalse( $status->isOK() );
190 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );