MediaWiki  master
LocalIdLookupTest.php
Go to the documentation of this file.
1 <?php
2 
8  private $localUsers = [];
9 
10  protected function setUp() {
11  parent::setUp();
12 
13  $this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
14  }
15 
16  public function addDBData() {
17  for ( $i = 1; $i <= 4; $i++ ) {
18  $this->localUsers[] = $this->getMutableTestUser()->getUser();
19  }
20 
21  $sysop = static::getTestSysop()->getUser();
22 
23  $block = new Block( [
24  'address' => $this->localUsers[2]->getName(),
25  'by' => $sysop->getId(),
26  'reason' => __METHOD__,
27  'expiry' => '1 day',
28  'hideName' => false,
29  ] );
30  $block->insert();
31 
32  $block = new Block( [
33  'address' => $this->localUsers[3]->getName(),
34  'by' => $sysop->getId(),
35  'reason' => __METHOD__,
36  'expiry' => '1 day',
37  'hideName' => true,
38  ] );
39  $block->insert();
40  }
41 
42  public function getLookupUser() {
43  return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
44  }
45 
46  public function testLookupCentralIds() {
47  $lookup = new LocalIdLookup();
48 
49  $user1 = $this->getLookupUser();
50  $user2 = User::newFromName( 'UTLocalIdLookup2' );
51 
52  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
53  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
54 
55  $this->assertSame( [], $lookup->lookupCentralIds( [] ) );
56 
57  $expect = [];
58  foreach ( $this->localUsers as $localUser ) {
59  $expect[$localUser->getId()] = $localUser->getName();
60  }
61  $expect[12345] = 'X';
62  ksort( $expect );
63 
64  $expect2 = $expect;
65  $expect2[$this->localUsers[3]->getId()] = '';
66 
67  $arg = array_fill_keys( array_keys( $expect ), 'X' );
68 
69  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
70  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) );
71  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
72  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
73  }
74 
75  public function testLookupUserNames() {
76  $lookup = new LocalIdLookup();
77  $user1 = $this->getLookupUser();
78  $user2 = User::newFromName( 'UTLocalIdLookup2' );
79 
80  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
81  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
82 
83  $this->assertSame( [], $lookup->lookupUserNames( [] ) );
84 
85  $expect = [];
86  foreach ( $this->localUsers as $localUser ) {
87  $expect[$localUser->getName()] = $localUser->getId();
88  }
89  $expect['UTDoesNotExist'] = 'X';
90  ksort( $expect );
91 
92  $expect2 = $expect;
93  $expect2[$this->localUsers[3]->getName()] = 'X';
94 
95  $arg = array_fill_keys( array_keys( $expect ), 'X' );
96 
97  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
98  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) );
99  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
100  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
101  }
102 
103  public function testIsAttached() {
104  $lookup = new LocalIdLookup();
105  $user1 = $this->getLookupUser();
106  $user2 = User::newFromName( 'DoesNotExist' );
107 
108  $this->assertTrue( $lookup->isAttached( $user1 ) );
109  $this->assertFalse( $lookup->isAttached( $user2 ) );
110 
111  $wiki = wfWikiID();
112  $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
113  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
114 
115  $wiki = 'not-' . wfWikiID();
116  $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
117  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
118  }
119 
126  public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
127  $this->setMwGlobals( [
128  'wgSharedDB' => $sharedDB ? "dummy" : null,
129  'wgSharedTables' => $sharedTable ? [ 'user' ] : [],
130  'wgLocalDatabases' => $localDBSet ? [ 'shared' ] : [],
131  ] );
132 
133  $lookup = new LocalIdLookup();
134  $this->assertSame(
135  $sharedDB && $sharedTable && $localDBSet,
136  $lookup->isAttached( $this->getLookupUser(), 'shared' )
137  );
138  }
139 
140  public static function provideIsAttachedShared() {
141  $ret = [];
142  for ( $i = 0; $i < 7; $i++ ) {
143  $ret[] = [
144  (bool)( $i & 1 ),
145  (bool)( $i & 2 ),
146  (bool)( $i & 4 ),
147  ];
148  }
149  return $ret;
150  }
151 
152 }
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:1982
static provideIsAttachedShared()
insert( $dbw=null)
Insert a block into the block table.
Definition: Block.php:543
testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet)
provideIsAttachedShared
setGroupPermissions( $newPerms, $newKey=null, $newValue=null)
Alters $wgGroupPermissions for the duration of the test.
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown...
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
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
LocalIdLookup Database.
A CentralIdLookup provider that just uses local IDs.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:587
Definition: Block.php:31