MediaWiki REL1_28
CheckBlocksSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Auth;
4
11 public function testConstructor() {
13 $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
14 $config = new \HashConfig( [
15 'BlockDisablesLogin' => false
16 ] );
17 $provider->setConfig( $config );
18 $this->assertSame( false, $providerPriv->blockDisablesLogin );
19
21 [ 'blockDisablesLogin' => true ]
22 );
23 $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
24 $config = new \HashConfig( [
25 'BlockDisablesLogin' => false
26 ] );
27 $provider->setConfig( $config );
28 $this->assertSame( true, $providerPriv->blockDisablesLogin );
29 }
30
31 public function testBasics() {
33 $user = \User::newFromName( 'UTSysop' );
34
35 $this->assertEquals(
37 $provider->beginSecondaryAccountCreation( $user, $user, [] )
38 );
39 }
40
46 public function testGetAuthenticationRequests( $action, $response ) {
48
49 $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
50 }
51
52 public static function provideGetAuthenticationRequests() {
53 return [
59 ];
60 }
61
62 private function getBlockedUser() {
63 $user = \User::newFromName( 'UTBlockee' );
64 if ( $user->getID() == 0 ) {
65 $user->addToDatabase();
66 \TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
67 $user->saveSettings();
68 }
69 $oldBlock = \Block::newFromTarget( 'UTBlockee' );
70 if ( $oldBlock ) {
71 // An old block will prevent our new one from saving.
72 $oldBlock->delete();
73 }
74 $blockOptions = [
75 'address' => 'UTBlockee',
76 'user' => $user->getID(),
77 'reason' => __METHOD__,
78 'expiry' => time() + 100500,
79 'createAccount' => true,
80 ];
81 $block = new \Block( $blockOptions );
82 $block->insert();
83 return $user;
84 }
85
87 $unblockedUser = \User::newFromName( 'UTSysop' );
88 $blockedUser = $this->getBlockedUser();
89
91 [ 'blockDisablesLogin' => false ]
92 );
93 $this->assertEquals(
95 $provider->beginSecondaryAuthentication( $unblockedUser, [] )
96 );
97 $this->assertEquals(
99 $provider->beginSecondaryAuthentication( $blockedUser, [] )
100 );
101
103 [ 'blockDisablesLogin' => true ]
104 );
105 $this->assertEquals(
107 $provider->beginSecondaryAuthentication( $unblockedUser, [] )
108 );
109 $ret = $provider->beginSecondaryAuthentication( $blockedUser, [] );
110 $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
111 }
112
113 public function testTestUserForCreation() {
115 [ 'blockDisablesLogin' => false ]
116 );
117 $provider->setLogger( new \Psr\Log\NullLogger() );
118 $provider->setConfig( new \HashConfig() );
119 $provider->setManager( AuthManager::singleton() );
120
121 $unblockedUser = \User::newFromName( 'UTSysop' );
122 $blockedUser = $this->getBlockedUser();
123
124 $user = \User::newFromName( 'RandomUser' );
125
126 $this->assertEquals(
127 \StatusValue::newGood(),
128 $provider->testUserForCreation( $unblockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION )
129 );
130 $this->assertEquals(
131 \StatusValue::newGood(),
132 $provider->testUserForCreation( $unblockedUser, false )
133 );
134
135 $status = $provider->testUserForCreation( $blockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION );
136 $this->assertInstanceOf( 'StatusValue', $status );
137 $this->assertFalse( $status->isOK() );
138 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
139
140 $status = $provider->testUserForCreation( $blockedUser, false );
141 $this->assertInstanceOf( 'StatusValue', $status );
142 $this->assertFalse( $status->isOK() );
143 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
144 }
145
146 public function testRangeBlock() {
147 $blockOptions = [
148 'address' => '127.0.0.0/24',
149 'reason' => __METHOD__,
150 'expiry' => time() + 100500,
151 'createAccount' => true,
152 ];
153 $block = new \Block( $blockOptions );
154 $block->insert();
155 $scopeVariable = new \Wikimedia\ScopedCallback( [ $block, 'delete' ] );
156
157 $user = \User::newFromName( 'UTNormalUser' );
158 if ( $user->getID() == 0 ) {
159 $user->addToDatabase();
160 \TestUser::setPasswordForUser( $user, 'UTNormalUserPassword' );
161 $user->saveSettings();
162 }
163 $this->setMwGlobals( [ 'wgUser' => $user ] );
164 $newuser = \User::newFromName( 'RandomUser' );
165
167 [ 'blockDisablesLogin' => true ]
168 );
169 $provider->setLogger( new \Psr\Log\NullLogger() );
170 $provider->setConfig( new \HashConfig() );
171 $provider->setManager( AuthManager::singleton() );
172
173 $ret = $provider->beginSecondaryAuthentication( $user, [] );
174 $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
175
176 $status = $provider->testUserForCreation( $newuser, AuthManager::AUTOCREATE_SOURCE_SESSION );
177 $this->assertInstanceOf( 'StatusValue', $status );
178 $this->assertFalse( $status->isOK() );
179 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
180
181 $status = $provider->testUserForCreation( $newuser, false );
182 $this->assertInstanceOf( 'StatusValue', $status );
183 $this->assertFalse( $status->isOK() );
184 $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
185 }
186}
A Config instance which stores all settings as a member variable.
setMwGlobals( $pairs, $value=null)
static singleton()
Get the global AuthManager.
const ACTION_CHANGE
Change a user's credentials.
const ACTION_REMOVE
Remove a user's credentials.
const ACTION_LINK
Link an existing user to a third-party account.
const AUTOCREATE_SOURCE_SESSION
Auto-creation is due to SessionManager.
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
const ACTION_CREATE
Create a new user.
const FAIL
Indicates that the authentication failed.
AuthManager Database MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider.
Check if the user is blocked, and prevent authentication if so.
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:1049
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:249
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 & $ret
Definition hooks.txt:1949
this hook is for auditing only $response
Definition hooks.txt:805
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:37