Replace usages of deprecated User::isAllowed. Step 2.
[lhc/web/wiklou.git] / tests / phpunit / includes / user / UserGroupMembershipTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @group Database
7 */
8 class UserGroupMembershipTest extends MediaWikiTestCase {
9
10 protected $tablesUsed = [ 'user', 'user_groups' ];
11
12 /**
13 * @var User Belongs to no groups
14 */
15 protected $userNoGroups;
16 /**
17 * @var User Belongs to the 'unittesters' group indefinitely, and the
18 * 'testwriters' group with expiry
19 */
20 protected $userTester;
21 /**
22 * @var string The timestamp, in TS_MW format, of the expiry of $userTester's
23 * membership in the 'testwriters' group
24 */
25 protected $expiryTime;
26
27 protected function setUp() {
28 parent::setUp();
29
30 $this->setMwGlobals( [
31 'wgGroupPermissions' => [
32 'unittesters' => [
33 'runtest' => true,
34 ],
35 'testwriters' => [
36 'writetest' => true,
37 ]
38 ]
39 ] );
40
41 $this->userNoGroups = new User;
42 $this->userNoGroups->setName( 'NoGroups' );
43 $this->userNoGroups->addToDatabase();
44
45 $this->userTester = new User;
46 $this->userTester->setName( 'Tester' );
47 $this->userTester->addToDatabase();
48 $this->userTester->addGroup( 'unittesters' );
49 $this->expiryTime = wfTimestamp( TS_MW, time() + 100500 );
50 $this->userTester->addGroup( 'testwriters', $this->expiryTime );
51 }
52
53 /**
54 * @covers UserGroupMembership::insert
55 * @covers UserGroupMembership::delete
56 */
57 public function testAddAndRemoveGroups() {
58 $user = $this->getMutableTestUser()->getUser();
59
60 // basic tests
61 $ugm = new UserGroupMembership( $user->getId(), 'unittesters' );
62 $this->assertTrue( $ugm->insert() );
63 $user->clearInstanceCache();
64 $this->assertContains( 'unittesters', $user->getGroups() );
65 $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
66 $this->assertTrue( MediaWikiServices::getInstance()
67 ->getPermissionManager()
68 ->userHasRight( $user, 'runtest' ) );
69
70 // try updating without allowUpdate. Should fail
71 $ugm = new UserGroupMembership( $user->getId(), 'unittesters', $this->expiryTime );
72 $this->assertFalse( $ugm->insert() );
73
74 // now try updating with allowUpdate
75 $this->assertTrue( $ugm->insert( 2 ) );
76 $user->clearInstanceCache();
77 $this->assertContains( 'unittesters', $user->getGroups() );
78 $this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
79 $this->assertTrue( MediaWikiServices::getInstance()
80 ->getPermissionManager()
81 ->userHasRight( $user, 'runtest' ) );
82
83 // try removing the group
84 $ugm->delete();
85 $user->clearInstanceCache();
86 $this->assertThat( $user->getGroups(),
87 $this->logicalNot( $this->contains( 'unittesters' ) ) );
88 $this->assertThat( $user->getGroupMemberships(),
89 $this->logicalNot( $this->arrayHasKey( 'unittesters' ) ) );
90 $this->assertFalse( MediaWikiServices::getInstance()
91 ->getPermissionManager()
92 ->userHasRight( $user, 'runtest' ) );
93
94 // check that the user group is now in user_former_groups
95 $this->assertContains( 'unittesters', $user->getFormerGroups() );
96 }
97
98 private function addUserTesterToExpiredGroup() {
99 // put $userTester in a group with expiry in the past
100 $ugm = new UserGroupMembership( $this->userTester->getId(), 'sysop', '20010102030405' );
101 $ugm->insert();
102 }
103
104 /**
105 * @covers UserGroupMembership::getMembershipsForUser
106 */
107 public function testGetMembershipsForUser() {
108 $this->addUserTesterToExpiredGroup();
109
110 // check that the user in no groups has no group memberships
111 $ugms = UserGroupMembership::getMembershipsForUser( $this->userNoGroups->getId() );
112 $this->assertEmpty( $ugms );
113
114 // check that the user in 2 groups has 2 group memberships
115 $testerUserId = $this->userTester->getId();
116 $ugms = UserGroupMembership::getMembershipsForUser( $testerUserId );
117 $this->assertCount( 2, $ugms );
118
119 // check that the required group memberships are present on $userTester,
120 // with the correct user IDs and expiries
121 $expectedGroups = [ 'unittesters', 'testwriters' ];
122
123 foreach ( $expectedGroups as $group ) {
124 $this->assertArrayHasKey( $group, $ugms );
125 $this->assertEquals( $ugms[$group]->getUserId(), $testerUserId );
126 $this->assertEquals( $ugms[$group]->getGroup(), $group );
127
128 if ( $group === 'unittesters' ) {
129 $this->assertNull( $ugms[$group]->getExpiry() );
130 } elseif ( $group === 'testwriters' ) {
131 $this->assertEquals( $ugms[$group]->getExpiry(), $this->expiryTime );
132 }
133 }
134 }
135
136 /**
137 * @covers UserGroupMembership::getMembership
138 */
139 public function testGetMembership() {
140 $this->addUserTesterToExpiredGroup();
141
142 // groups that the user doesn't belong to shouldn't be returned
143 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'sysop' );
144 $this->assertFalse( $ugm );
145
146 // implicit groups shouldn't be returned
147 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'user' );
148 $this->assertFalse( $ugm );
149
150 // expired groups shouldn't be returned
151 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'sysop' );
152 $this->assertFalse( $ugm );
153
154 // groups that the user does belong to should be returned with correct properties
155 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'unittesters' );
156 $this->assertInstanceOf( UserGroupMembership::class, $ugm );
157 $this->assertEquals( $ugm->getUserId(), $this->userTester->getId() );
158 $this->assertEquals( $ugm->getGroup(), 'unittesters' );
159 $this->assertNull( $ugm->getExpiry() );
160 }
161 }