MediaWiki REL1_30
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 = $this->getMutableTestUser()->getUser();
54
55 // basic tests
56 $ugm = new UserGroupMembership( $user->getId(), 'unittesters' );
57 $this->assertTrue( $ugm->insert() );
58 $user->clearInstanceCache();
59 $this->assertContains( 'unittesters', $user->getGroups() );
60 $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
61 $this->assertTrue( $user->isAllowed( 'runtest' ) );
62
63 // try updating without allowUpdate. Should fail
64 $ugm = new UserGroupMembership( $user->getId(), 'unittesters', $this->expiryTime );
65 $this->assertFalse( $ugm->insert() );
66
67 // now try updating with allowUpdate
68 $this->assertTrue( $ugm->insert( 2 ) );
69 $user->clearInstanceCache();
70 $this->assertContains( 'unittesters', $user->getGroups() );
71 $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
72 $this->assertTrue( $user->isAllowed( 'runtest' ) );
73
74 // try removing the group
75 $ugm->delete();
76 $user->clearInstanceCache();
77 $this->assertThat( $user->getGroups(),
78 $this->logicalNot( $this->contains( 'unittesters' ) ) );
79 $this->assertThat( $user->getGroupMemberships(),
80 $this->logicalNot( $this->arrayHasKey( 'unittesters' ) ) );
81 $this->assertFalse( $user->isAllowed( 'runtest' ) );
82
83 // check that the user group is now in user_former_groups
84 $this->assertContains( 'unittesters', $user->getFormerGroups() );
85 }
86
87 private function addUserTesterToExpiredGroup() {
88 // put $userTester in a group with expiry in the past
89 $ugm = new UserGroupMembership( $this->userTester->getId(), 'sysop', '20010102030405' );
90 $ugm->insert();
91 }
92
96 public function testGetMembershipsForUser() {
98
99 // check that the user in no groups has no group memberships
100 $ugms = UserGroupMembership::getMembershipsForUser( $this->userNoGroups->getId() );
101 $this->assertEmpty( $ugms );
102
103 // check that the user in 2 groups has 2 group memberships
104 $testerUserId = $this->userTester->getId();
105 $ugms = UserGroupMembership::getMembershipsForUser( $testerUserId );
106 $this->assertCount( 2, $ugms );
107
108 // check that the required group memberships are present on $userTester,
109 // with the correct user IDs and expiries
110 $expectedGroups = [ 'unittesters', 'testwriters' ];
111
112 foreach ( $expectedGroups as $group ) {
113 $this->assertArrayHasKey( $group, $ugms );
114 $this->assertEquals( $ugms[$group]->getUserId(), $testerUserId );
115 $this->assertEquals( $ugms[$group]->getGroup(), $group );
116
117 if ( $group === 'unittesters' ) {
118 $this->assertNull( $ugms[$group]->getExpiry() );
119 } elseif ( $group === 'testwriters' ) {
120 $this->assertEquals( $ugms[$group]->getExpiry(), $this->expiryTime );
121 }
122 }
123 }
124
128 public function testGetMembership() {
130
131 // groups that the user doesn't belong to shouldn't be returned
132 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'sysop' );
133 $this->assertFalse( $ugm );
134
135 // implicit groups shouldn't be returned
136 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'user' );
137 $this->assertFalse( $ugm );
138
139 // expired groups shouldn't be returned
140 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'sysop' );
141 $this->assertFalse( $ugm );
142
143 // groups that the user does belong to should be returned with correct properties
144 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'unittesters' );
145 $this->assertInstanceOf( UserGroupMembership::class, $ugm );
146 $this->assertEquals( $ugm->getUserId(), $this->userTester->getId() );
147 $this->assertEquals( $ugm->getGroup(), 'unittesters' );
148 $this->assertNull( $ugm->getExpiry() );
149 }
150}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static getMutableTestUser( $groups=[])
Convenience method for getting a mutable test user.
setMwGlobals( $pairs, $value=null)
Sets a global, maintaining a stashed version of the previous global to be restored in tearDown.
testAddAndRemoveGroups()
UserGroupMembership::insert UserGroupMembership::delete.
string $expiryTime
The timestamp, in TS_MW format, of the expiry of $userTester's membership in the 'testwriters' group.
testGetMembership()
UserGroupMembership::getMembership.
User $userTester
Belongs to the 'unittesters' group indefinitely, and the 'testwriters' group with expiry.
testGetMembershipsForUser()
UserGroupMembership::getMembershipsForUser.
User $userNoGroups
Belongs to no groups.
Represents a "user group membership" – a specific instance of a user belonging to a group.
static getMembershipsForUser( $userId, IDatabase $db=null)
Returns UserGroupMembership objects for all the groups a user currently belongs to.
static getMembership( $userId, $group, IDatabase $db=null)
Returns a UserGroupMembership object that pertains to the given user and group, or false if the user ...
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
setName( $str)
Set the user name.
Definition User.php:2276