| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use MediaWiki\User\UserIdentity; |
| 6 |
use MigrateUserGroup; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class MigrateUserGroupTest extends MaintenanceBaseTestCase { |
| 14 |
|
| 15 |
protected function getMaintenanceClass() { |
| 16 |
return MigrateUserGroup::class; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private function createTestUsersWithGroup( string $group, int $numberOfUsers ): array { |
| 27 |
$userGroupManager = $this->getServiceContainer()->getUserGroupManager(); |
| 28 |
$returnArray = []; |
| 29 |
for ( $i = 0; $i < $numberOfUsers; $i++ ) { |
| 30 |
$user = $this->getMutableTestUser()->getUserIdentity(); |
| 31 |
$userGroupManager->addUserToGroup( $user, $group ); |
| 32 |
$returnArray[] = $user; |
| 33 |
} |
| 34 |
return $returnArray; |
| 35 |
} |
| 36 |
|
| 37 |
public function testExecute() { |
| 38 |
|
| 39 |
$testUsers = $this->createTestUsersWithGroup( 'bot', 3 ); |
| 40 |
$this->maintenance->setArg( 'oldgroup', 'bot' ); |
| 41 |
$this->maintenance->setArg( 'newgroup', 'sysop' ); |
| 42 |
$this->maintenance->execute(); |
| 43 |
|
| 44 |
foreach ( $testUsers as $testUser ) { |
| 45 |
$testUserGroups = $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $testUser ); |
| 46 |
$this->assertContains( 'sysop', $testUserGroups ); |
| 47 |
$this->assertNotContains( 'bot', $testUserGroups ); |
| 48 |
} |
| 49 |
$this->expectOutputRegex( "/Done! 3 users in group 'bot' are now in 'sysop' instead.\n/" ); |
| 50 |
} |
| 51 |
|
| 52 |
public function testExecuteWithOneUserInNewGroup() { |
| 53 |
|
| 54 |
|
| 55 |
$testUsers = $this->createTestUsersWithGroup( 'bot', 5 ); |
| 56 |
$this->getServiceContainer()->getUserGroupManager() |
| 57 |
->addUserToGroup( $testUsers[1], 'sysop' ); |
| 58 |
$this->maintenance->setArg( 'oldgroup', 'bot' ); |
| 59 |
$this->maintenance->setArg( 'newgroup', 'sysop' ); |
| 60 |
$this->maintenance->execute(); |
| 61 |
|
| 62 |
foreach ( $testUsers as $testUser ) { |
| 63 |
$testUserGroups = $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $testUser ); |
| 64 |
$this->assertContains( 'sysop', $testUserGroups ); |
| 65 |
$this->assertNotContains( 'bot', $testUserGroups ); |
| 66 |
} |
| 67 |
$this->expectOutputRegex( "/Done! 5 users in group 'bot' are now in 'sysop' instead.\n/" ); |
| 68 |
} |
| 69 |
|
| 70 |
public function testExecuteForNoUsersInOldGroup() { |
| 71 |
$this->maintenance->setArg( 'oldgroup', 'bot' ); |
| 72 |
$this->maintenance->setArg( 'newgroup', 'sysop' ); |
| 73 |
$this->expectOutputRegex( "/no users in the 'bot' group/" ); |
| 74 |
$this->expectCallToFatalError(); |
| 75 |
$this->maintenance->execute(); |
| 76 |
} |
| 77 |
} |