MediaWiki  master
LocalIdLookupTest.php
Go to the documentation of this file.
1 <?php
2 
4 
10  private $localUsers = [];
11 
12  protected function setUp() {
13  parent::setUp();
14 
15  $this->setGroupPermissions( 'local-id-lookup-test', 'hideuser', true );
16  }
17 
18  public function addDBData() {
19  for ( $i = 1; $i <= 4; $i++ ) {
20  $this->localUsers[] = $this->getMutableTestUser()->getUser();
21  }
22 
23  $sysop = static::getTestSysop()->getUser();
24 
25  $block = new DatabaseBlock( [
26  'address' => $this->localUsers[2]->getName(),
27  'by' => $sysop->getId(),
28  'reason' => __METHOD__,
29  'expiry' => '1 day',
30  'hideName' => false,
31  ] );
32  $block->insert();
33 
34  $block = new DatabaseBlock( [
35  'address' => $this->localUsers[3]->getName(),
36  'by' => $sysop->getId(),
37  'reason' => __METHOD__,
38  'expiry' => '1 day',
39  'hideName' => true,
40  ] );
41  $block->insert();
42  }
43 
44  public function getLookupUser() {
45  return static::getTestUser( [ 'local-id-lookup-test' ] )->getUser();
46  }
47 
48  public function testLookupCentralIds() {
49  $lookup = new LocalIdLookup();
50 
51  $user1 = $this->getLookupUser();
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 = [];
60  foreach ( $this->localUsers as $localUser ) {
61  $expect[$localUser->getId()] = $localUser->getName();
62  }
63  $expect[12345] = 'X';
64  ksort( $expect );
65 
66  $expect2 = $expect;
67  $expect2[$this->localUsers[3]->getId()] = '';
68 
69  $arg = array_fill_keys( array_keys( $expect ), 'X' );
70 
71  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg ) );
72  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, CentralIdLookup::AUDIENCE_RAW ) );
73  $this->assertSame( $expect, $lookup->lookupCentralIds( $arg, $user1 ) );
74  $this->assertSame( $expect2, $lookup->lookupCentralIds( $arg, $user2 ) );
75  }
76 
77  public function testLookupUserNames() {
78  $lookup = new LocalIdLookup();
79  $user1 = $this->getLookupUser();
80  $user2 = User::newFromName( 'UTLocalIdLookup2' );
81 
82  $this->assertTrue( $user1->isAllowed( 'hideuser' ), 'sanity check' );
83  $this->assertFalse( $user2->isAllowed( 'hideuser' ), 'sanity check' );
84 
85  $this->assertSame( [], $lookup->lookupUserNames( [] ) );
86 
87  $expect = [];
88  foreach ( $this->localUsers as $localUser ) {
89  $expect[$localUser->getName()] = $localUser->getId();
90  }
91  $expect['UTDoesNotExist'] = 'X';
92  ksort( $expect );
93 
94  $expect2 = $expect;
95  $expect2[$this->localUsers[3]->getName()] = 'X';
96 
97  $arg = array_fill_keys( array_keys( $expect ), 'X' );
98 
99  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg ) );
100  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, CentralIdLookup::AUDIENCE_RAW ) );
101  $this->assertSame( $expect, $lookup->lookupUserNames( $arg, $user1 ) );
102  $this->assertSame( $expect2, $lookup->lookupUserNames( $arg, $user2 ) );
103  }
104 
105  public function testIsAttached() {
106  $lookup = new LocalIdLookup();
107  $user1 = $this->getLookupUser();
108  $user2 = User::newFromName( 'DoesNotExist' );
109 
110  $this->assertTrue( $lookup->isAttached( $user1 ) );
111  $this->assertFalse( $lookup->isAttached( $user2 ) );
112 
113  $wiki = wfWikiID();
114  $this->assertTrue( $lookup->isAttached( $user1, $wiki ) );
115  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
116 
117  $wiki = 'not-' . wfWikiID();
118  $this->assertFalse( $lookup->isAttached( $user1, $wiki ) );
119  $this->assertFalse( $lookup->isAttached( $user2, $wiki ) );
120  }
121 
128  public function testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet ) {
129  $this->setMwGlobals( [
130  'wgSharedDB' => $sharedDB ? "dummy" : null,
131  'wgSharedTables' => $sharedTable ? [ 'user' ] : [],
132  'wgLocalDatabases' => $localDBSet ? [ 'shared' ] : [],
133  ] );
134 
135  $lookup = new LocalIdLookup();
136  $this->assertSame(
137  $sharedDB && $sharedTable && $localDBSet,
138  $lookup->isAttached( $this->getLookupUser(), 'shared' )
139  );
140  }
141 
142  public static function provideIsAttachedShared() {
143  $ret = [];
144  for ( $i = 0; $i < 7; $i++ ) {
145  $ret[] = [
146  (bool)( $i & 1 ),
147  (bool)( $i & 2 ),
148  (bool)( $i & 4 ),
149  ];
150  }
151  return $ret;
152  }
153 
154 }
insert(IDatabase $dbw=null)
Insert a block into the block table.
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:1972
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static provideIsAttachedShared()
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
testIsAttachedShared( $sharedDB, $sharedTable, $localDBSet)
provideIsAttachedShared
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:767
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
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:535