MediaWiki  1.27.2
LocalIdLookupTest.php
Go to the documentation of this file.
1 <?php
2 
8  private $localUsers = [];
9 
10  protected function setUp() {
11  global $wgGroupPermissions;
12 
13  parent::setUp();
14 
15  $this->stashMwGlobals( [ 'wgGroupPermissions' ] );
16  $wgGroupPermissions['local-id-lookup-test']['hideuser'] = true;
17  }
18 
19  public function addDBData() {
20  for ( $i = 1; $i <= 4; $i++ ) {
21  $user = User::newFromName( "UTLocalIdLookup$i" );
22  if ( $user->getId() == 0 ) {
23  $user->addToDatabase();
24  }
25  $this->localUsers["UTLocalIdLookup$i"] = $user->getId();
26  }
27 
28  User::newFromName( 'UTLocalIdLookup1' )->addGroup( 'local-id-lookup-test' );
29 
30  $block = new Block( [
31  'address' => 'UTLocalIdLookup3',
32  'by' => User::idFromName( 'UTSysop' ),
33  'reason' => __METHOD__,
34  'expiry' => '1 day',
35  'hideName' => false,
36  ] );
37  $block->insert();
38 
39  $block = new Block( [
40  'address' => 'UTLocalIdLookup4',
41  'by' => User::idFromName( 'UTSysop' ),
42  'reason' => __METHOD__,
43  'expiry' => '1 day',
44  'hideName' => true,
45  ] );
46  $block->insert();
47  }
48 
49  public function testLookupCentralIds() {
50  $lookup = new LocalIdLookup();
51  $user1 = User::newFromName( 'UTLocalIdLookup1' );
52  $user2 = User::newFromName( 'UTLocalIdLookup2' );
53 
54  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
55  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
56 
57  $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
58 
59  $expect = array_flip( $this->localUsers );
60  $expect[123] = 'X';
61  ksort( $expect );
62 
63  $expect2 = $expect;
64  $expect2[$this->localUsers['UTLocalIdLookup4']] = '';
65 
66  $arg = array_fill_keys( array_keys( $expect ), 'X' );
67 
68  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
69  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) );
70  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
71  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
72  }
73 
74  public function testLookupUserNames() {
75  $lookup = new LocalIdLookup();
76  $user1 = User::newFromName( 'UTLocalIdLookup1' );
77  $user2 = User::newFromName( 'UTLocalIdLookup2' );
78 
79  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
80  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
81 
82  $this->assertSame( [], $lookup->lookupUserNames( [] ) );
83 
84  $expect = $this->localUsers;
85  $expect['UTDoesNotExist'] = 'X';
86  ksort( $expect );
87 
88  $expect2 = $expect;
89  $expect2['UTLocalIdLookup4'] = 'X';
90 
91  $arg = array_fill_keys( array_keys( $expect ), 'X' );
92 
93  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
94  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) );
95  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
96  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
97  }
98 
99  public function testIsAttached() {
100  $lookup = new LocalIdLookup();
101  $user1 = User::newFromName( 'UTLocalIdLookup1' );
102  $user2 = User::newFromName( 'DoesNotExist' );
103 
104  $this->assertTrue( $lookup->isAttached( $user1 ) );
105  $this->assertFalse( $lookup->isAttached( $user2 ) );
106 
107  $wiki = wfWikiID();
108  $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
109  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
110 
111  $wiki = 'not-' . wfWikiID();
112  $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
113  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
114  }
115 
122  public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
123  global $wgDBName;
124  $this->setMwGlobals( [
125  'wgSharedDB' => $sharedDB ? $wgDBName : null,
126  'wgSharedTables' => $sharedTable ? [ 'user' ] : [],
127  'wgLocalDatabases' => $localDBSet ? [ 'shared' ] : [],
128  ] );
129 
130  $lookup = new LocalIdLookup();
131  $this->assertSame(
132  $sharedDB && $sharedTable && $localDBSet,
133  $lookup->isAttached( User::newFromName( 'UTLocalIdLookup1' ), 'shared' )
134  );
135  }
136 
137  public static function provideIsAttachedShared() {
138  $ret = [];
139  for ( $i = 0; $i < 7; $i++ ) {
140  $ret[] = [
141  (bool)( $i & 1 ),
142  (bool)( $i & 2 ),
143  (bool)( $i & 4 ),
144  ];
145  }
146  return $ret;
147  }
148 
149 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:568
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:1798
static provideIsAttachedShared()
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
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
testIsAttachedShared($sharedDB, $sharedTable, $localDBSet)
provideIsAttachedShared
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 idFromName($name, $flags=self::READ_NORMAL)
Get database id given a user name.
Definition: User.php:770
LocalIdLookup Database.
A CentralIdLookup provider that just uses local IDs.
setMwGlobals($pairs, $value=null)
Definition: Block.php:22
stashMwGlobals($globalKeys)
Stashes the global, will be restored in tearDown()