MediaWiki  1.28.0
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  $this->localUsers[] = $this->getMutableTestUser()->getUser();
22  }
23 
24  $sysop = static::getTestSysop()->getUser();
25 
26  $block = new Block( [
27  'address' => $this->localUsers[2]->getName(),
28  'by' => $sysop->getId(),
29  'reason' => __METHOD__,
30  'expiry' => '1 day',
31  'hideName' => false,
32  ] );
33  $block->insert();
34 
35  $block = new Block( [
36  'address' => $this->localUsers[3]->getName(),
37  'by' => $sysop->getId(),
38  'reason' => __METHOD__,
39  'expiry' => '1 day',
40  'hideName' => true,
41  ] );
42  $block->insert();
43  }
44 
45  public function getLookupUser() {
46  return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
47  }
48 
49  public function testLookupCentralIds() {
50  $lookup = new LocalIdLookup();
51 
52  $user1 = $this->getLookupUser();
53  $user2 = User::newFromName( 'UTLocalIdLookup2' );
54 
55  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
56  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
57 
58  $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
59 
60  $expect = [];
61  foreach ( $this->localUsers as $localUser ) {
62  $expect[$localUser->getId()] = $localUser->getName();
63  }
64  $expect[12345] = 'X';
65  ksort( $expect );
66 
67  $expect2 = $expect;
68  $expect2[$this->localUsers[3]->getId()] = '';
69 
70  $arg = array_fill_keys( array_keys( $expect ), 'X' );
71 
72  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
73  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) );
74  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
75  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
76  }
77 
78  public function testLookupUserNames() {
79  $lookup = new LocalIdLookup();
80  $user1 = $this->getLookupUser();
81  $user2 = User::newFromName( 'UTLocalIdLookup2' );
82 
83  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
84  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
85 
86  $this->assertSame( [], $lookup->lookupUserNames( [] ) );
87 
88  $expect = [];
89  foreach ( $this->localUsers as $localUser ) {
90  $expect[$localUser->getName()] = $localUser->getId();
91  }
92  $expect['UTDoesNotExist'] = 'X';
93  ksort( $expect );
94 
95  $expect2 = $expect;
96  $expect2[$this->localUsers[3]->getName()] = 'X';
97 
98  $arg = array_fill_keys( array_keys( $expect ), 'X' );
99 
100  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
101  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) );
102  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
103  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
104  }
105 
106  public function testIsAttached() {
107  $lookup = new LocalIdLookup();
108  $user1 = $this->getLookupUser();
109  $user2 = User::newFromName( 'DoesNotExist' );
110 
111  $this->assertTrue( $lookup->isAttached( $user1 ) );
112  $this->assertFalse( $lookup->isAttached( $user2 ) );
113 
114  $wiki = wfWikiID();
115  $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
116  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
117 
118  $wiki = 'not-' . wfWikiID();
119  $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
120  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
121  }
122 
129  public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
130  global $wgDBName;
131  $this->setMwGlobals( [
132  'wgSharedDB' => $sharedDB ? $wgDBName : null,
133  'wgSharedTables' => $sharedTable ? [ 'user' ] : [],
134  'wgLocalDatabases' => $localDBSet ? [ 'shared' ] : [],
135  ] );
136 
137  $lookup = new LocalIdLookup();
138  $this->assertSame(
139  $sharedDB && $sharedTable && $localDBSet,
140  $lookup->isAttached( $this->getLookupUser(), 'shared' )
141  );
142  }
143 
144  public static function provideIsAttachedShared() {
145  $ret = [];
146  for ( $i = 0; $i < 7; $i++ ) {
147  $ret[] = [
148  (bool)( $i & 1 ),
149  (bool)( $i & 2 ),
150  (bool)( $i & 4 ),
151  ];
152  }
153  return $ret;
154  }
155 
156 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
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:1936
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
insert($dbw=null)
Insert a block into the block table.
Definition: Block.php:462
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
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
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 getMutableTestUser($groups=[])
Convenience method for getting a mutable test user.
LocalIdLookup Database.
A CentralIdLookup provider that just uses local IDs.
setMwGlobals($pairs, $value=null)
Definition: Block.php:25
stashMwGlobals($globalKeys)
Stashes the global, will be restored in tearDown()