Move ResultWrapper subclasses to Rdbms
[mediawiki.git] / tests / phpunit / includes / user / UserGroupMembershipTest.php
bloba297f2970c21aeee2bb3ef58335a062ac77cfd0a
1 <?php
3 /**
4 * @group Database
5 */
6 class UserGroupMembershipTest extends MediaWikiTestCase {
7 /**
8 * @var User Belongs to no groups
9 */
10 protected $userNoGroups;
11 /**
12 * @var User Belongs to the 'unittesters' group indefinitely, and the
13 * 'testwriters' group with expiry
15 protected $userTester;
16 /**
17 * @var string The timestamp, in TS_MW format, of the expiry of $userTester's
18 * membership in the 'testwriters' group
20 protected $expiryTime;
22 protected function setUp() {
23 parent::setUp();
25 $this->setMwGlobals( [
26 'wgGroupPermissions' => [
27 'unittesters' => [
28 'runtest' => true,
30 'testwriters' => [
31 'writetest' => true,
34 ] );
36 $this->userNoGroups = new User;
37 $this->userNoGroups->setName( 'NoGroups' );
38 $this->userNoGroups->addToDatabase();
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 );
48 /**
49 * @covers UserGroupMembership::insert
50 * @covers UserGroupMembership::delete
52 public function testAddAndRemoveGroups() {
53 $user = new User;
54 $user->addToDatabase();
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' ) );
64 // try updating without allowUpdate. Should fail
65 $ugm = new UserGroupMembership( $user->getId(), 'unittesters', $this->expiryTime );
66 $this->assertFalse( $ugm->insert() );
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' ) );
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' ) );
84 // check that the user group is now in user_former_groups
85 $this->assertContains( 'unittesters', $user->getFormerGroups() );
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();
94 /**
95 * @covers UserGroupMembership::getMembershipsForUser
97 public function testGetMembershipsForUser() {
98 $this->addUserTesterToExpiredGroup();
100 // check that the user in no groups has no group memberships
101 $ugms = UserGroupMembership::getMembershipsForUser( $this->userNoGroups->getId() );
102 $this->assertEmpty( $ugms );
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 );
109 // check that the required group memberships are present on $userTester,
110 // with the correct user IDs and expiries
111 $expectedGroups = [ 'unittesters', 'testwriters' ];
113 foreach ( $expectedGroups as $group ) {
114 $this->assertArrayHasKey( $group, $ugms );
115 $this->assertEquals( $ugms[$group]->getUserId(), $testerUserId );
116 $this->assertEquals( $ugms[$group]->getGroup(), $group );
118 if ( $group === 'unittesters' ) {
119 $this->assertNull( $ugms[$group]->getExpiry() );
120 } elseif ( $group === 'testwriters' ) {
121 $this->assertEquals( $ugms[$group]->getExpiry(), $this->expiryTime );
127 * @covers UserGroupMembership::getMembership
129 public function testGetMembership() {
130 $this->addUserTesterToExpiredGroup();
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 );
136 // implicit groups shouldn't be returned
137 $ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'user' );
138 $this->assertFalse( $ugm );
140 // expired groups shouldn't be returned
141 $ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'sysop' );
142 $this->assertFalse( $ugm );
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() );