MediaWiki  1.33.0
CentralIdLookupTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
10 
11  public function testFactory() {
12  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
13 
14  $this->setMwGlobals( [
15  'wgCentralIdLookupProviders' => [
16  'local' => [ 'class' => LocalIdLookup::class ],
17  'local2' => [ 'class' => LocalIdLookup::class ],
18  'mock' => [ 'factory' => function () use ( $mock ) {
19  return $mock;
20  } ],
21  'bad' => [ 'class' => stdClass::class ],
22  ],
23  'wgCentralIdLookupProvider' => 'mock',
24  ] );
25 
26  $this->assertSame( $mock, CentralIdLookup::factory() );
27  $this->assertSame( $mock, CentralIdLookup::factory( 'mock' ) );
28  $this->assertSame( 'mock', $mock->getProviderId() );
29 
30  $local = CentralIdLookup::factory( 'local' );
31  $this->assertNotSame( $mock, $local );
32  $this->assertInstanceOf( LocalIdLookup::class, $local );
33  $this->assertSame( $local, CentralIdLookup::factory( 'local' ) );
34  $this->assertSame( 'local', $local->getProviderId() );
35 
36  $local2 = CentralIdLookup::factory( 'local2' );
37  $this->assertNotSame( $local, $local2 );
38  $this->assertInstanceOf( LocalIdLookup::class, $local2 );
39  $this->assertSame( 'local2', $local2->getProviderId() );
40 
41  $this->assertNull( CentralIdLookup::factory( 'unconfigured' ) );
42  $this->assertNull( CentralIdLookup::factory( 'bad' ) );
43  }
44 
45  public function testCheckAudience() {
46  $mock = TestingAccessWrapper::newFromObject(
47  $this->getMockForAbstractClass( CentralIdLookup::class )
48  );
49 
50  $user = static::getTestSysop()->getUser();
51  $this->assertSame( $user, $mock->checkAudience( $user ) );
52 
53  $user = $mock->checkAudience( CentralIdLookup::AUDIENCE_PUBLIC );
54  $this->assertInstanceOf( User::class, $user );
55  $this->assertSame( 0, $user->getId() );
56 
57  $this->assertNull( $mock->checkAudience( CentralIdLookup::AUDIENCE_RAW ) );
58 
59  try {
60  $mock->checkAudience( 100 );
61  $this->fail( 'Expected exception not thrown' );
62  } catch ( InvalidArgumentException $ex ) {
63  $this->assertSame( 'Invalid audience', $ex->getMessage() );
64  }
65  }
66 
67  public function testNameFromCentralId() {
68  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
69  $mock->expects( $this->once() )->method( 'lookupCentralIds' )
70  ->with(
71  $this->equalTo( [ 15 => null ] ),
72  $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
73  $this->equalTo( CentralIdLookup::READ_LATEST )
74  )
75  ->will( $this->returnValue( [ 15 => 'FooBar' ] ) );
76 
77  $this->assertSame(
78  'FooBar',
79  $mock->nameFromCentralId( 15, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
80  );
81  }
82 
88  public function testLocalUserFromCentralId( $name, $succeeds ) {
89  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
90  $mock->expects( $this->any() )->method( 'isAttached' )
91  ->will( $this->returnValue( true ) );
92  $mock->expects( $this->once() )->method( 'lookupCentralIds' )
93  ->with(
94  $this->equalTo( [ 42 => null ] ),
95  $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
96  $this->equalTo( CentralIdLookup::READ_LATEST )
97  )
98  ->will( $this->returnValue( [ 42 => $name ] ) );
99 
100  $user = $mock->localUserFromCentralId(
101  42, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
102  );
103  if ( $succeeds ) {
104  $this->assertInstanceOf( User::class, $user );
105  $this->assertSame( $name, $user->getName() );
106  } else {
107  $this->assertNull( $user );
108  }
109 
110  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
111  $mock->expects( $this->any() )->method( 'isAttached' )
112  ->will( $this->returnValue( false ) );
113  $mock->expects( $this->once() )->method( 'lookupCentralIds' )
114  ->with(
115  $this->equalTo( [ 42 => null ] ),
116  $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
117  $this->equalTo( CentralIdLookup::READ_LATEST )
118  )
119  ->will( $this->returnValue( [ 42 => $name ] ) );
120  $this->assertNull(
121  $mock->localUserFromCentralId( 42, CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
122  );
123  }
124 
125  public static function provideLocalUserFromCentralId() {
126  return [
127  [ 'UTSysop', true ],
128  [ 'UTDoesNotExist', false ],
129  [ null, false ],
130  [ '', false ],
131  [ '<X>', false ],
132  ];
133  }
134 
135  public function testCentralIdFromName() {
136  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
137  $mock->expects( $this->once() )->method( 'lookupUserNames' )
138  ->with(
139  $this->equalTo( [ 'FooBar' => 0 ] ),
140  $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
141  $this->equalTo( CentralIdLookup::READ_LATEST )
142  )
143  ->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
144 
145  $this->assertSame(
146  23,
147  $mock->centralIdFromName( 'FooBar', CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST )
148  );
149  }
150 
151  public function testCentralIdFromLocalUser() {
152  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
153  $mock->expects( $this->any() )->method( 'isAttached' )
154  ->will( $this->returnValue( true ) );
155  $mock->expects( $this->once() )->method( 'lookupUserNames' )
156  ->with(
157  $this->equalTo( [ 'FooBar' => 0 ] ),
158  $this->equalTo( CentralIdLookup::AUDIENCE_RAW ),
159  $this->equalTo( CentralIdLookup::READ_LATEST )
160  )
161  ->will( $this->returnValue( [ 'FooBar' => 23 ] ) );
162 
163  $this->assertSame(
164  23,
165  $mock->centralIdFromLocalUser(
166  User::newFromName( 'FooBar' ), CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
167  )
168  );
169 
170  $mock = $this->getMockForAbstractClass( CentralIdLookup::class );
171  $mock->expects( $this->any() )->method( 'isAttached' )
172  ->will( $this->returnValue( false ) );
173  $mock->expects( $this->never() )->method( 'lookupUserNames' );
174 
175  $this->assertSame(
176  0,
177  $mock->centralIdFromLocalUser(
178  User::newFromName( 'FooBar' ), CentralIdLookup::AUDIENCE_RAW, CentralIdLookup::READ_LATEST
179  )
180  );
181  }
182 
183 }
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
CentralIdLookupTest\testNameFromCentralId
testNameFromCentralId()
Definition: CentralIdLookupTest.php:67
CentralIdLookupTest\testFactory
testFactory()
Definition: CentralIdLookupTest.php:11
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
CentralIdLookupTest\testCentralIdFromLocalUser
testCentralIdFromLocalUser()
Definition: CentralIdLookupTest.php:151
CentralIdLookupTest\testLocalUserFromCentralId
testLocalUserFromCentralId( $name, $succeeds)
provideLocalUserFromCentralId
Definition: CentralIdLookupTest.php:88
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
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
Definition: MediaWikiTestCase.php:709
MediaWikiTestCase
Definition: MediaWikiTestCase.php:17
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
CentralIdLookupTest\testCentralIdFromName
testCentralIdFromName()
Definition: CentralIdLookupTest.php:135
CentralIdLookupTest\testCheckAudience
testCheckAudience()
Definition: CentralIdLookupTest.php:45
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:271
CentralIdLookup\AUDIENCE_PUBLIC
const AUDIENCE_PUBLIC
Definition: CentralIdLookup.php:32
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
CentralIdLookupTest
CentralIdLookup Database.
Definition: CentralIdLookupTest.php:9
true
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 just before the function returns a value If you return true
Definition: hooks.txt:1985
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
CentralIdLookup\AUDIENCE_RAW
const AUDIENCE_RAW
Definition: CentralIdLookup.php:33
CentralIdLookup\factory
static factory( $providerId=null)
Fetch a CentralIdLookup.
Definition: CentralIdLookup.php:46
CentralIdLookupTest\provideLocalUserFromCentralId
static provideLocalUserFromCentralId()
Definition: CentralIdLookupTest.php:125