File: EmptyUserGroupTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (74 lines) | stat: -rw-r--r-- 2,338 bytes parent folder | download
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
<?php

namespace MediaWiki\Tests\Maintenance;

use EmptyUserGroup;
use MediaWiki\User\UserIdentity;

/**
 * @covers \EmptyUserGroup
 * @group Database
 * @author Dreamy Jazz
 */
class EmptyUserGroupTest extends MaintenanceBaseTestCase {

	protected function getMaintenanceClass() {
		return EmptyUserGroup::class;
	}

	public function testExecuteForEmptyGroup() {
		$this->maintenance->setArg( 0, 'sysop' );
		$this->maintenance->execute();
		$this->expectOutputString(
			"Removing users from sysop...\n" .
			"  ...nothing to do, group was empty.\n"
		);
	}

	/**
	 * Creates several testing users, and adds them to the specified $group.
	 *
	 * @param string $group The group the users should be in
	 * @param int $numberOfUsers The number of users to create
	 * @return UserIdentity[] The users that were created
	 */
	private function createTestUsersWithGroup( string $group, int $numberOfUsers ): array {
		$userGroupManager = $this->getServiceContainer()->getUserGroupManager();
		$returnArray = [];
		for ( $i = 0; $i < $numberOfUsers; $i++ ) {
			$user = $this->getMutableTestUser()->getUserIdentity();
			$userGroupManager->addUserToGroup( $user, $group );
			$returnArray[] = $user;
		}
		return $returnArray;
	}

	public function testExecuteForGroupWithUsers() {
		// Create 5 test users with the 'sysop' group
		$sysopUsers = $this->createTestUsersWithGroup( 'sysop', 5 );
		// Create a test user with the 'bot' group to verify that it will not loose it's group
		$botUsers = $this->createTestUsersWithGroup( 'bot', 1 );
		// Run the maintenance script to empty the sysop group, with a batch size of 2.
		$this->maintenance->setArg( 0, 'sysop' );
		$this->maintenance->setOption( 'batch-size', 2 );
		$this->maintenance->execute();
		$this->expectOutputString(
			"Removing users from sysop...\n" .
			"  ...done! Removed 5 users in total.\n"
		);
		// Verify all users in the 'sysop' group actually had their group removed.
		foreach ( $sysopUsers as $user ) {
			$this->assertNotContains(
				'sysop',
				$this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user )
			);
		}
		// Verify all users in the 'bot' group still have their group
		foreach ( $botUsers as $user ) {
			$this->assertContains(
				'bot',
				$this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user )
			);
		}
	}
}