File: MigrateUserGroupTest.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 (77 lines) | stat: -rw-r--r-- 2,873 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
75
76
77
<?php

namespace MediaWiki\Tests\Maintenance;

use MediaWiki\User\UserIdentity;
use MigrateUserGroup;

/**
 * @covers \MigrateUserGroup
 * @group Database
 * @author Dreamy Jazz
 */
class MigrateUserGroupTest extends MaintenanceBaseTestCase {

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

	/**
	 * 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 testExecute() {
		// Test moving 3 users with the "bot" group to the "sysop" group
		$testUsers = $this->createTestUsersWithGroup( 'bot', 3 );
		$this->maintenance->setArg( 'oldgroup', 'bot' );
		$this->maintenance->setArg( 'newgroup', 'sysop' );
		$this->maintenance->execute();
		// Verify that the move correctly occurred.
		foreach ( $testUsers as $testUser ) {
			$testUserGroups = $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $testUser );
			$this->assertContains( 'sysop', $testUserGroups );
			$this->assertNotContains( 'bot', $testUserGroups );
		}
		$this->expectOutputRegex( "/Done! 3 users in group 'bot' are now in 'sysop' instead.\n/" );
	}

	public function testExecuteWithOneUserInNewGroup() {
		// Test moving 5 users with the "bot" group to the "sysop" group, adding one already into the "sysop" group
		// to test that functionality.
		$testUsers = $this->createTestUsersWithGroup( 'bot', 5 );
		$this->getServiceContainer()->getUserGroupManager()
			->addUserToGroup( $testUsers[1], 'sysop' );
		$this->maintenance->setArg( 'oldgroup', 'bot' );
		$this->maintenance->setArg( 'newgroup', 'sysop' );
		$this->maintenance->execute();
		// Verify that the move correctly occurred.
		foreach ( $testUsers as $testUser ) {
			$testUserGroups = $this->getServiceContainer()->getUserGroupManager()->getUserGroups( $testUser );
			$this->assertContains( 'sysop', $testUserGroups );
			$this->assertNotContains( 'bot', $testUserGroups );
		}
		$this->expectOutputRegex( "/Done! 5 users in group 'bot' are now in 'sysop' instead.\n/" );
	}

	public function testExecuteForNoUsersInOldGroup() {
		$this->maintenance->setArg( 'oldgroup', 'bot' );
		$this->maintenance->setArg( 'newgroup', 'sysop' );
		$this->expectOutputRegex( "/no users in the 'bot' group/" );
		$this->expectCallToFatalError();
		$this->maintenance->execute();
	}
}