3 namespace MediaWiki\Tests\Maintenance
;
5 use MediaWiki\User\UserIdentity
;
9 * @covers \MigrateUserGroup
13 class MigrateUserGroupTest
extends MaintenanceBaseTestCase
{
15 protected function getMaintenanceClass() {
16 return MigrateUserGroup
::class;
20 * Creates several testing users, and adds them to the specified $group.
22 * @param string $group The group the users should be in
23 * @param int $numberOfUsers The number of users to create
24 * @return UserIdentity[] The users that were created
26 private function createTestUsersWithGroup( string $group, int $numberOfUsers ): array {
27 $userGroupManager = $this->getServiceContainer()->getUserGroupManager();
29 for ( $i = 0; $i < $numberOfUsers; $i++
) {
30 $user = $this->getMutableTestUser()->getUserIdentity();
31 $userGroupManager->addUserToGroup( $user, $group );
32 $returnArray[] = $user;
37 public function testExecute() {
38 // Test moving 3 users with the "bot" group to the "sysop" group
39 $testUsers = $this->createTestUsersWithGroup( 'bot', 3 );
40 $this->maintenance
->setArg( 'oldgroup', 'bot' );
41 $this->maintenance
->setArg( 'newgroup', 'sysop' );
42 $this->maintenance
->execute();
43 // Verify that the move correctly occurred.
44 foreach ( $testUsers as $testUser ) {
45 $testUserGroups = $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $testUser );
46 $this->assertContains( 'sysop', $testUserGroups );
47 $this->assertNotContains( 'bot', $testUserGroups );
49 $this->expectOutputRegex( "/Done! 3 users in group 'bot' are now in 'sysop' instead.\n/" );
52 public function testExecuteWithOneUserInNewGroup() {
53 // Test moving 5 users with the "bot" group to the "sysop" group, adding one already into the "sysop" group
54 // to test that functionality.
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 // Verify that the move correctly occurred.
62 foreach ( $testUsers as $testUser ) {
63 $testUserGroups = $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $testUser );
64 $this->assertContains( 'sysop', $testUserGroups );
65 $this->assertNotContains( 'bot', $testUserGroups );
67 $this->expectOutputRegex( "/Done! 5 users in group 'bot' are now in 'sysop' instead.\n/" );
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();