MediaWiki  1.29.2
CheckBlocksSecondaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
5 use Wikimedia\TestingAccessWrapper;
6 
13  public function testConstructor() {
15  $providerPriv = TestingAccessWrapper::newFromObject( $provider );
16  $config = new \HashConfig( [
17  'BlockDisablesLogin' => false
18  ] );
19  $provider->setConfig( $config );
20  $this->assertSame( false, $providerPriv->blockDisablesLogin );
21 
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() {
35  $user = \User::newFromName( 'UTSysop' );
36 
37  $this->assertEquals(
39  $provider->beginSecondaryAccountCreation( $user, $user, [] )
40  );
41  }
42 
50 
51  $this->assertEquals( $response, $provider->getAuthenticationRequests( $action, [] ) );
52  }
53 
54  public static function provideGetAuthenticationRequests() {
55  return [
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 
89  $unblockedUser = \User::newFromName( 'UTSysop' );
90  $blockedUser = $this->getBlockedUser();
91 
93  [ 'blockDisablesLogin' => false ]
94  );
95  $this->assertEquals(
97  $provider->beginSecondaryAuthentication( $unblockedUser, [] )
98  );
99  $this->assertEquals(
101  $provider->beginSecondaryAuthentication( $blockedUser, [] )
102  );
103 
105  [ 'blockDisablesLogin' => true ]
106  );
107  $this->assertEquals(
109  $provider->beginSecondaryAuthentication( $unblockedUser, [] )
110  );
111  $ret = $provider->beginSecondaryAuthentication( $blockedUser, [] );
112  $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
113  }
114 
115  public function testTestUserForCreation() {
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(
130  $provider->testUserForCreation( $unblockedUser, AuthManager::AUTOCREATE_SOURCE_SESSION )
131  );
132  $this->assertEquals(
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  $newuser = \User::newFromName( 'RandomUser' );
167 
169  [ 'blockDisablesLogin' => true ]
170  );
171  $provider->setLogger( new \Psr\Log\NullLogger() );
172  $provider->setConfig( new \HashConfig() );
173  $provider->setManager( AuthManager::singleton() );
174 
175  $ret = $provider->beginSecondaryAuthentication( $user, [] );
176  $this->assertEquals( AuthenticationResponse::FAIL, $ret->status );
177 
178  $status = $provider->testUserForCreation( $newuser, AuthManager::AUTOCREATE_SOURCE_SESSION );
179  $this->assertInstanceOf( 'StatusValue', $status );
180  $this->assertFalse( $status->isOK() );
181  $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
182 
183  $status = $provider->testUserForCreation( $newuser, false );
184  $this->assertInstanceOf( 'StatusValue', $status );
185  $this->assertFalse( $status->isOK() );
186  $this->assertTrue( $status->hasMessage( 'cantcreateaccount-range-text' ) );
187  }
188 }
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider
Check if the user is blocked, and prevent authentication if so.
Definition: CheckBlocksSecondaryAuthenticationProvider.php:33
MediaWiki\$action
String $action
Cache what action this request is.
Definition: MediaWiki.php:47
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testGetAuthenticationRequests
testGetAuthenticationRequests( $action, $response)
provideGetAuthenticationRequests
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:48
MediaWiki\Auth\AuthManager\AUTOCREATE_SOURCE_SESSION
const AUTOCREATE_SOURCE_SESSION
Auto-creation is due to SessionManager.
Definition: AuthManager.php:113
HashConfig
A Config instance which stores all settings as a member variable.
Definition: HashConfig.php:28
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup 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
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
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 account $user
Definition: hooks.txt:246
Block\newFromTarget
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:1113
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:556
php
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
MediaWiki\Auth\AuthenticationResponse\newAbstain
static newAbstain()
Definition: AuthenticationResponse.php:170
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest
AuthManager Database MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProvider.
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:12
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testRangeBlock
testRangeBlock()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:148
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testBasics
testBasics()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:33
MediaWiki\Auth\AuthenticationResponse\FAIL
const FAIL
Indicates that the authentication failed.
Definition: AuthenticationResponse.php:42
MediaWiki\Auth\AuthManager\ACTION_CREATE
const ACTION_CREATE
Create a new user.
Definition: AuthManager.php:89
TestUser\setPasswordForUser
static setPasswordForUser(User $user, $password)
Set the password on a testing user.
Definition: TestUser.php:127
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\getBlockedUser
getBlockedUser()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:64
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:76
MediaWiki\Auth\AuthManager\ACTION_CHANGE
const ACTION_CHANGE
Change a user's credentials.
Definition: AuthManager.php:99
$ret
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:1956
MediaWiki\Auth\AuthManager\ACTION_LINK
const ACTION_LINK
Link an existing user to a third-party account.
Definition: AuthManager.php:94
$response
this hook is for auditing only $response
Definition: hooks.txt:783
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testTestUserForCreation
testTestUserForCreation()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:115
MediaWiki\Auth\AuthManager\ACTION_REMOVE
const ACTION_REMOVE
Remove a user's credentials.
Definition: AuthManager.php:101
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testConstructor
testConstructor()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:13
MediaWiki\Auth\AuthManager\singleton
static singleton()
Get the global AuthManager.
Definition: AuthManager.php:146
MediaWiki\$config
Config $config
Definition: MediaWiki.php:42
MediaWiki\Auth\AuthManager\ACTION_LOGIN
const ACTION_LOGIN
Log in with an existing (not necessarily local) user.
Definition: AuthManager.php:84
MediaWiki\Auth
Definition: AbstractAuthenticationProvider.php:22
MediaWiki\Auth\AuthenticationResponse\newPass
static newPass( $username=null)
Definition: AuthenticationResponse.php:134
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\provideGetAuthenticationRequests
static provideGetAuthenticationRequests()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:54
MediaWiki\Auth\CheckBlocksSecondaryAuthenticationProviderTest\testBeginSecondaryAuthentication
testBeginSecondaryAuthentication()
Definition: CheckBlocksSecondaryAuthenticationProviderTest.php:88