3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\User\UserIdentity
;
9 * @covers \EmptyUserGroup
13 class EmptyUserGroupTest
extends MaintenanceBaseTestCase
{
15 protected function getMaintenanceClass() {
16 return EmptyUserGroup
::class;
19 public function testExecuteForEmptyGroup() {
20 $this->maintenance
->setArg( 0, 'sysop' );
21 $this->maintenance
->execute();
22 $this->expectOutputString(
23 "Removing users from sysop...\n" .
24 " ...nothing to do, group was empty.\n"
29 * Creates several testing users, and adds them to the specified $group.
31 * @param string $group The group the users should be in
32 * @param int $numberOfUsers The number of users to create
33 * @return UserIdentity[] The users that were created
35 private function createTestUsersWithGroup( string $group, int $numberOfUsers ): array {
36 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
38 for ( $i = 0; $i < $numberOfUsers; $i++
) {
39 $user = $this->getMutableTestUser()->getUserIdentity();
40 $userGroupManager->addUserToGroup( $user, $group );
41 $returnArray[] = $user;
46 public function testExecuteForGroupWithUsers() {
47 // Create 5 test users with the 'sysop' group
48 $sysopUsers = $this->createTestUsersWithGroup( 'sysop', 5 );
49 // Create a test user with the 'bot' group to verify that it will not loose it's group
50 $botUsers = $this->createTestUsersWithGroup( 'bot', 1 );
51 // Run the maintenance script to empty the sysop group, with a batch size of 2.
52 $this->maintenance
->setArg( 0, 'sysop' );
53 $this->maintenance
->setOption( 'batch-size', 2 );
54 $this->maintenance
->execute();
55 $this->expectOutputString(
56 "Removing users from sysop...\n" .
57 " ...done! Removed 5 users in total.\n"
59 // Verify all users in the 'sysop' group actually had their group removed.
60 foreach ( $sysopUsers as $user ) {
61 $this->assertNotContains(
63 $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user )
66 // Verify all users in the 'bot' group still have their group
67 foreach ( $botUsers as $user ) {
68 $this->assertContains(
70 $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user )