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