MediaWiki  master
CheckBlocksSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
11  protected function setUp() {
13 
14  parent::setUp();
15  if ( $wgDisableAuthManager ) {
16  $this->markTestSkipped( '$wgDisableAuthManager is set' );
17  }
18  }
19 
20  public function testConstructor() {
22  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
23  $config = new \HashConfig( [
24  'BlockDisablesLogin' => false
25  ] );
26  $provider->setConfig( $config );
27  $this->assertSame( false, $providerPriv->blockDisablesLogin );
28 
30  [ 'blockDisablesLogin' => true ]
31  );
32  $providerPriv = \TestingAccessWrapper::newFromObject( $provider );
33  $config = new \HashConfig( [
34  'BlockDisablesLogin' => false
35  ] );
36  $provider->setConfig( $config );
37  $this->assertSame( true, $providerPriv->blockDisablesLogin );
38  }
39 
40  public function testBasics() {
42  $user = \User::newFromName( 'UTSysop' );
43 
44  $this->assertEquals(
46  $provider->beginSecondaryAccountCreation( $user, $user, [] )
47  );
48  }
49 
57 
58  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
59  }
60 
61  public static function provideGetAuthenticationRequests() {
62  return [
68  ];
69  }
70 
71  private function getBlockedUser() {
72  $user = \User::newFromName( 'UTBlockee' );
73  if ( $user->getID() == 0 ) {
74  $user->addToDatabase();
75  \TestUser::setPasswordForUser( $user, 'UTBlockeePassword' );
76  $user->saveSettings();
77  }
78  $oldBlock = \Block::newFromTarget( 'UTBlockee' );
79  if ( $oldBlock ) {
80  // An old block will prevent our new one from saving.
81  $oldBlock->delete();
82  }
83  $blockOptions = [
84  'address' => 'UTBlockee',
85  'user' => $user->getID(),
86  'reason' => __METHOD__,
87  'expiry' => time() + 100500,
88  'createAccount' => true,
89  ];
90  $block = new \Block( $blockOptions );
91  $block->insert();
92  return $user;
93  }
94 
96  $unblockedUser = \User::newFromName( 'UTSysop' );
97  $blockedUser = $this->getBlockedUser();
98 
100  [ 'blockDisablesLogin' => false ]
101  );
102  $this->assertEquals(
104  $provider->beginSecondaryAuthentication( $unblockedUser, [] )
105  );
106  $this->assertEquals(
108  $provider->beginSecondaryAuthentication( $blockedUser, [] )
109  );
110 
112  [ 'blockDisablesLogin' => true ]
113  );
114  $this->assertEquals(
116  $provider->beginSecondaryAuthentication( $unblockedUser, [] )
117  );
118  $ret = $provider->beginSecondaryAuthentication( $blockedUser, [] );
119  $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
120  }
121 
122  public function testTestUserForCreation() {
124  [ 'blockDisablesLogin' => false ]
125  );
126  $provider->setLogger( new \Psr\Log\NullLogger() );
127  $provider->setConfig( new \HashConfig() );
128  $provider->setManager( AuthManager::singleton() );
129 
130  $unblockedUser = \User::newFromName( 'UTSysop' );
131  $blockedUser = $this->getBlockedUser();
132 
133  $user = \User::newFromName( 'RandomUser' );
134 
135  $this->assertEquals(
137  $provider->testUserForCreation( $unblockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION )
138  );
139  $this->assertEquals(
141  $provider->testUserForCreation( $unblockedUser, false )
142  );
143 
144  $status = $provider->testUserForCreation( $blockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION );
145  $this->assertInstanceOf( 'StatusValue', $status );
146  $this->assertFalse( $status->isOK() );
147  $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
148 
149  $status = $provider->testUserForCreation( $blockedUser, false );
150  $this->assertInstanceOf( 'StatusValue', $status );
151  $this->assertFalse( $status->isOK() );
152  $this->assertTrue( $status->hasMessage( 'cantcreateaccount-text' ) );
153  }
154 
155  public function testRangeBlock() {
156  $blockOptions = [
157  'address' => '127.0.0.0/24',
158  'reason' => __METHOD__,
159  'expiry' => time() + 100500,
160  'createAccount' => true,
161  ];
162  $block = new \Block( $blockOptions );
163  $block->insert();
164  $scopeVariable = new \ScopedCallback( [ $block, 'delete' ] );
165 
166  $user = \User::newFromName( 'UTNormalUser' );
167  if ( $user->getID() == 0 ) {
168  $user->addToDatabase();
169  \TestUser::setPasswordForUser( $user, 'UTNormalUserPassword' );
170  $user->saveSettings();
171  }
172  $this->setMwGlobals( [ 'wgUser' => $user ] );
173  $newuser = \User::newFromName( 'RandomUser' );
174 
176  [ 'blockDisablesLogin' => true ]
177  );
178  $provider->setLogger( new \Psr\Log\NullLogger() );
179  $provider->setConfig( new \HashConfig() );
180  $provider->setManager( AuthManager::singleton() );
181 
182  $ret = $provider->beginSecondaryAuthentication( $user, [] );
183  $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
184 
185  $status = $provider->testUserForCreation( $newuser, AuthManager::AUTOCREATE_SOURCE_SESSION );
186  $this->assertInstanceOf( 'StatusValue', $status );
187  $this->assertFalse( $status->isOK() );
188  $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
189 
190  $status = $provider->testUserForCreation( $newuser, false );
191  $this->assertInstanceOf( 'StatusValue', $status );
192  $this->assertFalse( $status->isOK() );
193  $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
194  }
195 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:522
Config $config
Definition: MediaWiki.php:37
Check if the user is blocked, and prevent authentication if so.
this hook is for auditing only $response
Definition: hooks.txt:776
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static setPasswordForUser(User $user, $password)
Set the password on a testing user.
Definition: TestUser.php:127
const FAIL
Indicates that the authentication failed.
static newFromTarget($specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target's type, get an existing Block object if possible.
Definition: Block.php:1057
static singleton()
Get the global AuthManager.
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:60
const AUTOCREATE_SOURCE_SESSION
Auto-creation is due to SessionManager.
Definition: AuthManager.php:74
static newGood($value=null)
Factory function for good results.
Definition: StatusValue.php:76
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:1816
const ACTION_LINK
Link an existing user to a third-party account.
Definition: AuthManager.php:55
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
String $action
Cache what action this request is.
Definition: MediaWiki.php:42
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
const ACTION_REMOVE
Remove a user's credentials.
Definition: AuthManager.php:62
$wgDisableAuthManager
Disable AuthManager.
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:1020
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:50
static newFromObject($object)
Return the same object, without access restrictions.
AuthManager Database MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider.
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:45
setMwGlobals($pairs, $value=null)
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28