MediaWiki REL1_32
UserGroupMembershipTest.php
Go to the documentation of this file.
1<?php
2
7
8 protected $tablesUsed = [ 'user', 'user_groups' ];
9
13 protected $userNoGroups;
18 protected $userTester;
23 protected $expiryTime;
24
25 protected function setUp() {
26 parent::setUp();
27
28 $this->setMwGlobals( [
29 'wgGroupPermissions' => [
30 'unittesters' => [
31 'runtest' => true,
32 ],
33 'testwriters' => [
34 'writetest' => true,
35 ]
36 ]
37 ] );
38
39 $this->userNoGroups = new User;
40 $this->userNoGroups->setName( 'NoGroups' );
41 $this->userNoGroups->addToDatabase();
42
43 $this->userTester = new User;
44 $this->userTester->setName( 'Tester' );
45 $this->userTester->addToDatabase();
46 $this->userTester->addGroup( 'unittesters' );
47 $this->expiryTime = wfTimestamp( TS_MW, time() + 100500 );
48 $this->userTester->addGroup( 'testwriters', $this->expiryTime );
49 }
50
55 public function testAddAndRemoveGroups() {
56 $user = $this->getMutableTestUser()->getUser();
57
58 // basic tests
59 $ugm = new UserGroupMembership( $user->getId(), 'unittesters' );
60 $this->assertTrue( $ugm->insert() );
61 $user->clearInstanceCache();
62 $this->assertContains( 'unittesters', $user->getGroups() );
63 $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
64 $this->assertTrue( $user->isAllowed( 'runtest' ) );
65
66 // try updating without allowUpdate. Should fail
67 $ugm = new UserGroupMembership( $user->getId(), 'unittesters', $this->expiryTime );
68 $this->assertFalse( $ugm->insert() );
69
70 // now try updating with allowUpdate
71 $this->assertTrue( $ugm->insert( 2 ) );
72 $user->clearInstanceCache();
73 $this->assertContains( 'unittesters', $user->getGroups() );
74 $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
75 $this->assertTrue( $user->isAllowed( 'runtest' ) );
76
77 // try removing the group
78 $ugm->delete();
79 $user->clearInstanceCache();
80 $this->assertThat( $user->getGroups(),
81 $this->logicalNot( $this->contains( 'unittesters' ) ) );
82 $this->assertThat( $user->getGroupMemberships(),
83 $this->logicalNot( $this->arrayHasKey( 'unittesters' ) ) );
84 $this->assertFalse( $user->isAllowed( 'runtest' ) );
85
86 // check that the user group is now in user_former_groups
87 $this->assertContains( 'unittesters', $user->getFormerGroups() );
88 }
89
90 private function addUserTesterToExpiredGroup() {
91 // put $userTester in a group with expiry in the past
92 $ugm = new UserGroupMembership( $this->userTester->getId(), 'sysop', '20010102030405' );
93 $ugm->insert();
94 }
95
99 public function testGetMembershipsForUser() {
101
102 // check that the user in no groups has no group memberships
103 $ugms = UserGroupMembership::getMembershipsForUser( $this->userNoGroups->getId() );
104 $this->assertEmpty( $ugms );
105
106 // check that the user in 2 groups has 2 group memberships
107 $testerUserId = $this->userTester->getId();
108 $ugms = UserGroupMembership::getMembershipsForUser( $testerUserId );
109 $this->assertCount( 2, $ugms );
110
111 // check that the required group memberships are present on $userTester,
112 // with the correct user IDs and expiries
113 $expectedGroups = [ 'unittesters', 'testwriters' ];
114
115 foreach ( $expectedGroups as $group ) {
116 $this->assertArrayHasKey( $group, $ugms );
117 $this->assertEquals( $ugms[$group]->getUserId(), $testerUserId );
118 $this->assertEquals( $ugms[$group]->getGroup(), $group );
119
120 if ( $group === 'unittesters' ) {
121 $this->assertNull( $ugms[$group]->getExpiry() );
122 } elseif ( $group === 'testwriters' ) {
123 $this->assertEquals( $ugms[$group]->getExpiry(), $this->expiryTime );
124 }
125 }
126 }
127
131 public function testGetMembership() {
133
134 // groups that the user doesn't belong to shouldn't be returned
135 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'sysop' );
136 $this->assertFalse( $ugm );
137
138 // implicit groups shouldn't be returned
139 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'user' );
140 $this->assertFalse( $ugm );
141
142 // expired groups shouldn't be returned
143 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'sysop' );
144 $this->assertFalse( $ugm );
145
146 // groups that the user does belong to should be returned with correct properties
147 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'unittesters' );
148 $this->assertInstanceOf( UserGroupMembership::class, $ugm );
149 $this->assertEquals( $ugm->getUserId(), $this->userTester->getId() );
150 $this->assertEquals( $ugm->getGroup(), 'unittesters' );
151 $this->assertNull( $ugm->getExpiry() );
152 }
153}
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.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:47
setName( $str)
Set the user name.
Definition User.php:2489