summaryrefslogtreecommitdiff
path: root/www/wiki/tests/phpunit/includes/user/UserGroupMembershipTest.php
blob: 4862747b4f6281ef65454bf9c34c0397d0492209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

/**
 * @group Database
 */
class UserGroupMembershipTest extends MediaWikiTestCase {

	protected $tablesUsed = [ 'user', 'user_groups' ];

	/**
	 * @var User Belongs to no groups
	 */
	protected $userNoGroups;
	/**
	 * @var User Belongs to the 'unittesters' group indefinitely, and the
	 * 'testwriters' group with expiry
	 */
	protected $userTester;
	/**
	 * @var string The timestamp, in TS_MW format, of the expiry of $userTester's
	 * membership in the 'testwriters' group
	 */
	protected $expiryTime;

	protected function setUp() {
		parent::setUp();

		$this->setMwGlobals( [
			'wgGroupPermissions' => [
				'unittesters' => [
					'runtest' => true,
				],
				'testwriters' => [
					'writetest' => true,
				]
			]
		] );

		$this->userNoGroups = new User;
		$this->userNoGroups->setName( 'NoGroups' );
		$this->userNoGroups->addToDatabase();

		$this->userTester = new User;
		$this->userTester->setName( 'Tester' );
		$this->userTester->addToDatabase();
		$this->userTester->addGroup( 'unittesters' );
		$this->expiryTime = wfTimestamp( TS_MW, time() + 100500 );
		$this->userTester->addGroup( 'testwriters', $this->expiryTime );
	}

	/**
	 * @covers UserGroupMembership::insert
	 * @covers UserGroupMembership::delete
	 */
	public function testAddAndRemoveGroups() {
		$user = $this->getMutableTestUser()->getUser();

		// basic tests
		$ugm = new UserGroupMembership( $user->getId(), 'unittesters' );
		$this->assertTrue( $ugm->insert() );
		$user->clearInstanceCache();
		$this->assertContains( 'unittesters', $user->getGroups() );
		$this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
		$this->assertTrue( $user->isAllowed( 'runtest' ) );

		// try updating without allowUpdate. Should fail
		$ugm = new UserGroupMembership( $user->getId(), 'unittesters', $this->expiryTime );
		$this->assertFalse( $ugm->insert() );

		// now try updating with allowUpdate
		$this->assertTrue( $ugm->insert( 2 ) );
		$user->clearInstanceCache();
		$this->assertContains( 'unittesters', $user->getGroups() );
		$this->assertArrayHasKey( 'unittesters', $user->getGroupMemberships() );
		$this->assertTrue( $user->isAllowed( 'runtest' ) );

		// try removing the group
		$ugm->delete();
		$user->clearInstanceCache();
		$this->assertThat( $user->getGroups(),
			$this->logicalNot( $this->contains( 'unittesters' ) ) );
		$this->assertThat( $user->getGroupMemberships(),
			$this->logicalNot( $this->arrayHasKey( 'unittesters' ) ) );
		$this->assertFalse( $user->isAllowed( 'runtest' ) );

		// check that the user group is now in user_former_groups
		$this->assertContains( 'unittesters', $user->getFormerGroups() );
	}

	private function addUserTesterToExpiredGroup() {
		// put $userTester in a group with expiry in the past
		$ugm = new UserGroupMembership( $this->userTester->getId(), 'sysop', '20010102030405' );
		$ugm->insert();
	}

	/**
	 * @covers UserGroupMembership::getMembershipsForUser
	 */
	public function testGetMembershipsForUser() {
		$this->addUserTesterToExpiredGroup();

		// check that the user in no groups has no group memberships
		$ugms = UserGroupMembership::getMembershipsForUser( $this->userNoGroups->getId() );
		$this->assertEmpty( $ugms );

		// check that the user in 2 groups has 2 group memberships
		$testerUserId = $this->userTester->getId();
		$ugms = UserGroupMembership::getMembershipsForUser( $testerUserId );
		$this->assertCount( 2, $ugms );

		// check that the required group memberships are present on $userTester,
		// with the correct user IDs and expiries
		$expectedGroups = [ 'unittesters', 'testwriters' ];

		foreach ( $expectedGroups as $group ) {
			$this->assertArrayHasKey( $group, $ugms );
			$this->assertEquals( $ugms[$group]->getUserId(), $testerUserId );
			$this->assertEquals( $ugms[$group]->getGroup(), $group );

			if ( $group === 'unittesters' ) {
				$this->assertNull( $ugms[$group]->getExpiry() );
			} elseif ( $group === 'testwriters' ) {
				$this->assertEquals( $ugms[$group]->getExpiry(), $this->expiryTime );
			}
		}
	}

	/**
	 * @covers UserGroupMembership::getMembership
	 */
	public function testGetMembership() {
		$this->addUserTesterToExpiredGroup();

		// groups that the user doesn't belong to shouldn't be returned
		$ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'sysop' );
		$this->assertFalse( $ugm );

		// implicit groups shouldn't be returned
		$ugm = UserGroupMembership::getMembership( $this->userNoGroups->getId(), 'user' );
		$this->assertFalse( $ugm );

		// expired groups shouldn't be returned
		$ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'sysop' );
		$this->assertFalse( $ugm );

		// groups that the user does belong to should be returned with correct properties
		$ugm = UserGroupMembership::getMembership( $this->userTester->getId(), 'unittesters' );
		$this->assertInstanceOf( UserGroupMembership::class, $ugm );
		$this->assertEquals( $ugm->getUserId(), $this->userTester->getId() );
		$this->assertEquals( $ugm->getGroup(), 'unittesters' );
		$this->assertNull( $ugm->getExpiry() );
	}
}