MediaWiki  1.30.0
AbstractPrimaryAuthenticationProviderTest.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Auth;
4 
11  $user = \User::newFromName( 'UTSysop' );
12 
13  $provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class );
14 
15  try {
16  $provider->continuePrimaryAuthentication( [] );
17  $this->fail( 'Expected exception not thrown' );
18  } catch ( \BadMethodCallException $ex ) {
19  }
20 
21  try {
22  $provider->continuePrimaryAccountCreation( $user, $user, [] );
23  $this->fail( 'Expected exception not thrown' );
24  } catch ( \BadMethodCallException $ex ) {
25  }
26 
27  $req = $this->getMockForAbstractClass( AuthenticationRequest::class );
28 
29  $this->assertTrue( $provider->providerAllowsPropertyChange( 'foo' ) );
30  $this->assertEquals(
32  $provider->testForAccountCreation( $user, $user, [] )
33  );
34  $this->assertEquals(
36  $provider->testUserForCreation( $user, AuthManager::AUTOCREATE_SOURCE_SESSION )
37  );
38  $this->assertEquals(
40  $provider->testUserForCreation( $user, false )
41  );
42 
43  $this->assertNull(
44  $provider->finishAccountCreation( $user, $user, AuthenticationResponse::newPass() )
45  );
46  $provider->autoCreatedAccount( $user, AuthManager::AUTOCREATE_SOURCE_SESSION );
47 
49  $provider->postAuthentication( $user, $res );
50  $provider->postAccountCreation( $user, $user, $res );
51  $provider->postAccountLink( $user, $res );
52 
53  $provider->expects( $this->once() )
54  ->method( 'testUserExists' )
55  ->with( $this->equalTo( 'foo' ) )
56  ->will( $this->returnValue( true ) );
57  $this->assertTrue( $provider->testUserCanAuthenticate( 'foo' ) );
58  }
59 
60  public function testProviderRevokeAccessForUser() {
61  $reqs = [];
62  for ( $i = 0; $i < 3; $i++ ) {
63  $reqs[$i] = $this->createMock( AuthenticationRequest::class );
64  $reqs[$i]->done = false;
65  }
66 
67  $provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class );
68  $provider->expects( $this->once() )->method( 'getAuthenticationRequests' )
69  ->with(
70  $this->identicalTo( AuthManager::ACTION_REMOVE ),
71  $this->identicalTo( [ 'username' => 'UTSysop' ] )
72  )
73  ->will( $this->returnValue( $reqs ) );
74  $provider->expects( $this->exactly( 3 ) )->method( 'providerChangeAuthenticationData' )
75  ->will( $this->returnCallback( function ( $req ) {
76  $this->assertSame( 'UTSysop', $req->username );
77  $this->assertFalse( $req->done );
78  $req->done = true;
79  } ) );
80 
81  $provider->providerRevokeAccessForUser( 'UTSysop' );
82 
83  foreach ( $reqs as $i => $req ) {
84  $this->assertTrue( $req->done, "#$i" );
85  }
86  }
87 
93  public function testPrimaryAccountLink( $type, $msg ) {
94  $provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class );
95  $provider->expects( $this->any() )->method( 'accountCreationType' )
96  ->will( $this->returnValue( $type ) );
97 
99  $msg1 = "{$class}::beginPrimaryAccountLink $msg";
100  $msg2 = "{$class}::continuePrimaryAccountLink is not implemented.";
101 
102  $user = \User::newFromName( 'Whatever' );
103 
104  try {
105  $provider->beginPrimaryAccountLink( $user, [] );
106  $this->fail( 'Expected exception not thrown' );
107  } catch ( \BadMethodCallException $ex ) {
108  $this->assertSame( $msg1, $ex->getMessage() );
109  }
110  try {
111  $provider->continuePrimaryAccountLink( $user, [] );
112  $this->fail( 'Expected exception not thrown' );
113  } catch ( \BadMethodCallException $ex ) {
114  $this->assertSame( $msg2, $ex->getMessage() );
115  }
116  }
117 
118  public static function providePrimaryAccountLink() {
119  return [
120  [
122  'should not be called on a non-link provider.',
123  ],
124  [
126  'should not be called on a non-link provider.',
127  ],
128  [
130  'is not implemented.',
131  ],
132  ];
133  }
134 
138  public function testProviderNormalizeUsername( $name, $expect ) {
139  // fake interwiki map for the 'Interwiki prefix' testcase
140  $this->mergeMwGlobalArrayValue( 'wgHooks', [
141  'InterwikiLoadPrefix' => [
142  function ( $prefix, &$iwdata ) {
143  if ( $prefix === 'interwiki' ) {
144  $iwdata = [
145  'iw_url' => 'http://example.com/',
146  'iw_local' => 0,
147  'iw_trans' => 0,
148  ];
149  return false;
150  }
151  },
152  ],
153  ] );
154 
155  $provider = $this->getMockForAbstractClass( AbstractPrimaryAuthenticationProvider::class );
156  $this->assertSame( $expect, $provider->providerNormalizeUsername( $name ) );
157  }
158 
159  public static function provideProviderNormalizeUsername() {
160  return [
161  'Leading space' => [ ' Leading space', 'Leading space' ],
162  'Trailing space ' => [ 'Trailing space ', 'Trailing space' ],
163  'Namespace prefix' => [ 'Talk:Username', null ],
164  'Interwiki prefix' => [ 'interwiki:Username', null ],
165  'With hash' => [ 'name with # hash', null ],
166  'Multi spaces' => [ 'Multi spaces', 'Multi spaces' ],
167  'Lowercase' => [ 'lowercase', 'Lowercase' ],
168  'Invalid character' => [ 'in[]valid', null ],
169  'With slash' => [ 'with / slash', null ],
170  'Underscores' => [ '___under__scores___', 'Under scores' ],
171  ];
172  }
173 
174 }
MediaWiki\Auth\AbstractPrimaryAuthenticationProviderTest\providePrimaryAccountLink
static providePrimaryAccountLink()
Definition: AbstractPrimaryAuthenticationProviderTest.php:118
$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:244
MediaWiki\Auth\PrimaryAuthenticationProvider\TYPE_CREATE
const TYPE_CREATE
Provider can create accounts.
Definition: PrimaryAuthenticationProvider.php:77
MediaWiki\Auth\PrimaryAuthenticationProvider\TYPE_NONE
const TYPE_NONE
Provider cannot create or link to accounts.
Definition: PrimaryAuthenticationProvider.php:81
MediaWiki\Auth\AbstractPrimaryAuthenticationProviderTest\testPrimaryAccountLink
testPrimaryAccountLink( $type, $msg)
providePrimaryAccountLink
Definition: AbstractPrimaryAuthenticationProviderTest.php:93
MediaWikiTestCase\mergeMwGlobalArrayValue
mergeMwGlobalArrayValue( $name, $values)
Merges the given values into a MW global array variable.
Definition: MediaWikiTestCase.php:807
MediaWiki\Auth\AuthManager\AUTOCREATE_SOURCE_SESSION
const AUTOCREATE_SOURCE_SESSION
Auto-creation is due to SessionManager.
Definition: AuthManager.php:113
$req
this hook is for auditing only $req
Definition: hooks.txt:988
MediaWiki\Auth\PrimaryAuthenticationProvider\TYPE_LINK
const TYPE_LINK
Provider can link to existing accounts elsewhere.
Definition: PrimaryAuthenticationProvider.php:79
MediaWiki\Auth\AbstractPrimaryAuthenticationProviderTest\testAbstractPrimaryAuthenticationProvider
testAbstractPrimaryAuthenticationProvider()
Definition: AbstractPrimaryAuthenticationProviderTest.php:10
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:550
$res
$res
Definition: database.txt:21
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
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\AbstractPrimaryAuthenticationProviderTest
AuthManager MediaWiki\Auth\AbstractPrimaryAuthenticationProvider.
Definition: AbstractPrimaryAuthenticationProviderTest.php:9
MediaWikiTestCase
Definition: MediaWikiTestCase.php:15
MediaWiki\Auth\AbstractPrimaryAuthenticationProviderTest\provideProviderNormalizeUsername
static provideProviderNormalizeUsername()
Definition: AbstractPrimaryAuthenticationProviderTest.php:159
any
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition: COPYING.txt:326
StatusValue\newGood
static newGood( $value=null)
Factory function for good results.
Definition: StatusValue.php:81
MediaWiki\Auth\AuthManager\ACTION_REMOVE
const ACTION_REMOVE
Remove a user's credentials.
Definition: AuthManager.php:101
MediaWiki\Auth\AbstractPrimaryAuthenticationProviderTest\testProviderNormalizeUsername
testProviderNormalizeUsername( $name, $expect)
provideProviderNormalizeUsername
Definition: AbstractPrimaryAuthenticationProviderTest.php:138
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
MediaWiki\Auth
Definition: AbstractAuthenticationProvider.php:22
MediaWiki\Auth\AuthenticationResponse\newPass
static newPass( $username=null)
Definition: AuthenticationResponse.php:134
MediaWiki\Auth\AbstractPrimaryAuthenticationProviderTest\testProviderRevokeAccessForUser
testProviderRevokeAccessForUser()
Definition: AbstractPrimaryAuthenticationProviderTest.php:60
$type
$type
Definition: testCompression.php:48