Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / EmptyUserGroupTest.php
blobfcbf93c3a1c3a83b91fbcf13f0357d6534d98844
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use EmptyUserGroup;
6 use MediaWiki\User\UserIdentity;
8 /**
9 * @covers \EmptyUserGroup
10 * @group Database
11 * @author Dreamy Jazz
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"
28 /**
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();
37 $returnArray = [];
38 for ( $i = 0; $i < $numberOfUsers; $i++ ) {
39 $user = $this->getMutableTestUser()->getUserIdentity();
40 $userGroupManager->addUserToGroup( $user, $group );
41 $returnArray[] = $user;
43 return $returnArray;
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(
62 'sysop',
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(
69 'bot',
70 $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $user )