MediaWiki  1.29.2
UserGroupMembershipTest.php
Go to the documentation of this file.
1 <?php
2 
10  protected $userNoGroups;
15  protected $userTester;
20  protected $expiryTime;
21 
22  protected function setUp() {
23  parent::setUp();
24 
25  $this->setMwGlobals( [
26  'wgGroupPermissions' => [
27  'unittesters' => [
28  'runtest' => true,
29  ],
30  'testwriters' => [
31  'writetest' => true,
32  ]
33  ]
34  ] );
35 
36  $this->userNoGroups = new User;
37  $this->userNoGroups->setName( 'NoGroups' );
38  $this->userNoGroups->addToDatabase();
39 
40  $this->userTester = new User;
41  $this->userTester->setName( 'Tester' );
42  $this->userTester->addToDatabase();
43  $this->userTester->addGroup( 'unittesters' );
44  $this->expiryTime = wfTimestamp( TS_MW, time() + 100500 );
45  $this->userTester->addGroup( 'testwriters', $this->expiryTime );
46  }
47 
52  public function testAddAndRemoveGroups() {
53  $user = new User;
54  $user->addToDatabase();
55 
56  // basic tests
57  $ugm = new UserGroupMembership( $user->getId(), 'unittesters' );
58  $this->assertTrue( $ugm->insert() );
59  $user->clearInstanceCache();
60  $this->assertContains( 'unittesters', $user->getGroups() );
61  $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
62  $this->assertTrue( $user->isAllowed( 'runtest' ) );
63 
64  // try updating without allowUpdate. Should fail
65  $ugm = new UserGroupMembership( $user->getId(), 'unittesters', $this->expiryTime );
66  $this->assertFalse( $ugm->insert() );
67 
68  // now try updating with allowUpdate
69  $this->assertTrue( $ugm->insert( 2 ) );
70  $user->clearInstanceCache();
71  $this->assertContains( 'unittesters', $user->getGroups() );
72  $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
73  $this->assertTrue( $user->isAllowed( 'runtest' ) );
74 
75  // try removing the group
76  $ugm->delete();
77  $user->clearInstanceCache();
78  $this->assertThat( $user->getGroups(),
79  $this->logicalNot( $this->contains( 'unittesters' ) ) );
80  $this->assertThat( $user->getGroupMemberships(),
81  $this->logicalNot( $this->arrayHasKey( 'unittesters' ) ) );
82  $this->assertFalse( $user->isAllowed( 'runtest' ) );
83 
84  // check that the user group is now in user_former_groups
85  $this->assertContains( 'unittesters', $user->getFormerGroups() );
86  }
87 
88  private function addUserTesterToExpiredGroup() {
89  // put $userTester in a group with expiry in the past
90  $ugm = new UserGroupMembership( $this->userTester->getId(), 'sysop', '20010102030405' );
91  $ugm->insert();
92  }
93 
97  public function testGetMembershipsForUser() {
99 
100  // check that the user in no groups has no group memberships
101  $ugms = UserGroupMembership::getMembershipsForUser( $this->userNoGroups->getId() );
102  $this->assertEmpty( $ugms );
103 
104  // check that the user in 2 groups has 2 group memberships
105  $testerUserId = $this->userTester->getId();
106  $ugms = UserGroupMembership::getMembershipsForUser( $testerUserId );
107  $this->assertCount( 2, $ugms );
108 
109  // check that the required group memberships are present on $userTester,
110  // with the correct user IDs and expiries
111  $expectedGroups = [ 'unittesters', 'testwriters' ];
112 
113  foreach ( $expectedGroups as $group ) {
114  $this->assertArrayHasKey( $group, $ugms );
115  $this->assertEquals( $ugms[$group]->getUserId(), $testerUserId );
116  $this->assertEquals( $ugms[$group]->getGroup(), $group );
117 
118  if ( $group === 'unittesters' ) {
119  $this->assertNull( $ugms[$group]->getExpiry() );
120  } elseif ( $group === 'testwriters' ) {
121  $this->assertEquals( $ugms[$group]->getExpiry(), $this->expiryTime );
122  }
123  }
124  }
125 
129  public function testGetMembership() {
131 
132  // groups that the user doesn't belong to shouldn't be returned
133  $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'sysop' );
134  $this->assertFalse( $ugm );
135 
136  // implicit groups shouldn't be returned
137  $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'user' );
138  $this->assertFalse( $ugm );
139 
140  // expired groups shouldn't be returned
141  $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'sysop' );
142  $this->assertFalse( $ugm );
143 
144  // groups that the user does belong to should be returned with correct properties
145  $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'unittesters' );
146  $this->assertInstanceOf( UserGroupMembership::class, $ugm );
147  $this->assertEquals( $ugm->getUserId(), $this->userTester->getId() );
148  $this->assertEquals( $ugm->getGroup(), 'unittesters' );
149  $this->assertNull( $ugm->getExpiry() );
150  }
151 }
UserGroupMembershipTest\$userNoGroups
User $userNoGroups
Belongs to no groups.
Definition: UserGroupMembershipTest.php:10
UserGroupMembership\insert
insert( $allowUpdate=false, IDatabase $dbw=null)
Insert a user right membership into the database.
Definition: UserGroupMembership.php:155
UserGroupMembershipTest\$userTester
User $userTester
Belongs to the 'unittesters' group indefinitely, and the 'testwriters' group with expiry.
Definition: UserGroupMembershipTest.php:15
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1994
UserGroupMembershipTest\setUp
setUp()
Definition: UserGroupMembershipTest.php:22
UserGroupMembershipTest\$expiryTime
string $expiryTime
The timestamp, in TS_MW format, of the expiry of $userTester's membership in the 'testwriters' group.
Definition: UserGroupMembershipTest.php:20
$user
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 account $user
Definition: hooks.txt:246
User
User
Definition: All_system_messages.txt:425
php
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
UserGroupMembership\getMembershipsForUser
static getMembershipsForUser( $userId, IDatabase $db=null)
Returns UserGroupMembership objects for all the groups a user currently belongs to.
Definition: UserGroupMembership.php:281
UserGroupMembershipTest\addUserTesterToExpiredGroup
addUserTesterToExpiredGroup()
Definition: UserGroupMembershipTest.php:88
UserGroupMembershipTest
Database.
Definition: UserGroupMembershipTest.php:6
MediaWikiTestCase\setMwGlobals
setMwGlobals( $pairs, $value=null)
Definition: MediaWikiTestCase.php:658
MediaWikiTestCase
Definition: MediaWikiTestCase.php:13
UserGroupMembershipTest\testAddAndRemoveGroups
testAddAndRemoveGroups()
UserGroupMembership::insert UserGroupMembership::delete.
Definition: UserGroupMembershipTest.php:52
UserGroupMembershipTest\testGetMembershipsForUser
testGetMembershipsForUser()
UserGroupMembership::getMembershipsForUser.
Definition: UserGroupMembershipTest.php:97
as
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
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
UserGroupMembership\getMembership
static getMembership( $userId, $group, IDatabase $db=null)
Returns a UserGroupMembership object that pertains to the given user and group, or false if the user ...
Definition: UserGroupMembership.php:312
contains
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it contains
Definition: COPYING.txt:157
UserGroupMembershipTest\testGetMembership
testGetMembership()
UserGroupMembership::getMembership.
Definition: UserGroupMembershipTest.php:129
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
UserGroupMembership
Represents a "user group membership" – a specific instance of a user belonging to a group.
Definition: UserGroupMembership.php:36