MediaWiki REL1_30
CentralIdLookupTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
10
11 public function testFactory() {
12 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
13
14 $this->setMwGlobals( [
15 'wgCentralIdLookupProviders' => [
16 'local' => [ 'class' => 'LocalIdLookup' ],
17 'local2' => [ 'class' => 'LocalIdLookup' ],
18 'mock' => [ 'factory' => function () use ( $mock ) {
19 return $mock;
20 } ],
21 'bad' => [ 'class' => 'stdClass' ],
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', $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', $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' )
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', $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' );
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' );
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', $user );
105 $this->assertSame( $name, $user->getName() );
106 } else {
107 $this->assertNull( $user );
108 }
109
110 $mock = $this->getMockForAbstractClass( 'CentralIdLookup' );
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' );
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' );
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' );
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}
they could even be mouse clicks or menu items whatever suits your program You should also get your if any
Definition COPYING.txt:326
CentralIdLookup Database.
testLocalUserFromCentralId( $name, $succeeds)
provideLocalUserFromCentralId
static factory( $providerId=null)
Fetch a CentralIdLookup.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
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:1976
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187