Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / RenameUsersMatchingPatternTest.php
blob424543dedd03d256b31444354de5ccd6ae26ccd3
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use MediaWiki\MainConfigNames;
6 use MediaWiki\Revision\SlotRecord;
7 use MediaWiki\Title\Title;
8 use MediaWiki\User\CentralId\CentralIdLookup;
9 use MediaWiki\User\CentralId\CentralIdLookupFactory;
10 use RenameUsersMatchingPattern;
11 use TestUser;
13 /**
14 * @covers \RenameUsersMatchingPattern
15 * @group Database
16 * @author Dreamy Jazz
18 class RenameUsersMatchingPatternTest extends MaintenanceBaseTestCase {
20 protected function setUp(): void {
21 parent::setUp();
22 // The tests only work if the local central ID provider is used. As such, force it to be the local provider in
23 // case we are running this test while an extension is loaded that provides another provider.
24 $this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' );
27 public function getMaintenanceClass() {
28 return RenameUsersMatchingPattern::class;
31 public function testExecuteWhenNonLocalCentralIdAvailible() {
32 $mockCentralIdLookupFactory = $this->createMock( CentralIdLookupFactory::class );
33 $mockCentralIdLookupFactory->method( 'getNonLocalLookup' )
34 ->willReturn( $this->createMock( CentralIdLookup::class ) );
35 $this->setService( 'CentralIdLookupFactory', $mockCentralIdLookupFactory );
36 $this->expectCallToFatalError();
37 $this->expectOutputRegex( '/This script cannot be run when CentralAuth is enabled/' );
38 $this->maintenance->execute();
41 /** @dataProvider provideExecuteForFatalError */
42 public function testExecuteForFatalError( $options, $expectedOutputRegex ) {
43 foreach ( $options as $name => $value ) {
44 $this->maintenance->setOption( $name, $value );
46 $this->expectCallToFatalError();
47 $this->expectOutputRegex( $expectedOutputRegex );
48 $this->maintenance->execute();
51 public static function provideExecuteForFatalError() {
52 return [
53 'Invalid performer username' => [
54 [ 'performer' => 'Template:Testing#test', 'from' => '*$1', 'to' => '~$1' ],
55 '/Unable to get performer account/',
60 public function testExecuteForInvalidNewName() {
61 // Get a test user and add it to the database (so that we can query the user table).
62 $testUser = $this->getMutableTestUser( [], 'Abc' )->getUser();
63 $testUser->addToDatabase();
64 // Run the maintenance script to attempt to rename to an invalid username, and expect that this fails.
65 $this->maintenance->setOption( 'from', 'Abc$1' );
66 $this->maintenance->setOption( 'to', 'Template:Test#testing_$1' );
67 $this->expectOutputRegex(
68 '/Cannot rename "' . preg_quote( $testUser, '/' ) . '" because.*not a valid title[\s\S]*' .
69 "Renamed 0 user\(s\)/"
71 $this->maintenance->execute();
74 public function testExecuteNewUsernameAlreadyInUse() {
75 // Get two users, with a different prefix but the same after the prefix.
76 $testUser = new TestUser( "Abc MediaWikiIntegrationTestCase-already-in-use-username-test" );
77 $testUser->getUser()->addToDatabase();
78 $conflictingUser = new TestUser( "Def MediaWikiIntegrationTestCase-already-in-use-username-test" );
79 $conflictingUser->getUser()->addToDatabase();
80 // Run the maintenance script to attempt to rename a user to use a name already in use, and expect this fails.
81 $this->maintenance->setOption( 'from', 'Abc$1' );
82 $this->maintenance->setOption( 'to', 'Def$1' );
83 $this->expectOutputRegex(
84 '/Cannot rename "' . preg_quote( $testUser->getUser(), '/' ) . '" because "' .
85 preg_quote( $conflictingUser->getUser(), '/' ) . '" already exists[\s\S]*' .
86 "Renamed 0 user\(s\)/"
88 $this->maintenance->execute();
91 public function testExecuteWithoutPageMoves() {
92 // Get some testing users which have a common prefix
93 $usersToBeRenamed = [];
94 $userIdsUsedForTest = [];
95 for ( $i = 0; $i < 3; $i++ ) {
96 $testUser = $this->getMutableTestUser( [], 'Abc' );
97 $testUser->getUser()->addToDatabase();
98 $usersToBeRenamed[] = $testUser->getUserIdentity();
99 $userIdsUsedForTest[] = $testUser->getUserIdentity()->getId();
101 // Get a user which does not use the common prefix, and therefore should not be renamed
102 $testUserToNotBeRenamed = $this->getMutableTestUser( [], 'Def' )->getUser();
103 $testUserToNotBeRenamed->addToDatabase();
104 $userIdsUsedForTest[] = $testUserToNotBeRenamed->getId();
105 // Run the maintenance script
106 $this->maintenance->setOption( 'from', 'Abc$1' );
107 $this->maintenance->setOption( 'to', 'Def$1' );
108 $this->maintenance->setOption( 'skip-page-moves', true );
109 $this->maintenance->execute();
110 // Check that the output of the script is as expected, and generate a list of expected new usernames to check
111 // against the DB.
112 $expectedUsernamesAfterCall = [];
113 $expectedOutputRegex = '/';
114 foreach ( $usersToBeRenamed as $userIdentity ) {
115 $expectedUsernameAfterCall = ucfirst( 'Def' . substr( $userIdentity->getName(), strlen( 'Abc' ) ) );
116 $expectedOutputRegex .= preg_quote( $userIdentity->getName(), '/' ) .
117 ' was successfully renamed to ' . preg_quote( $expectedUsernameAfterCall, '/' ) . '[\s\S]*';
118 $expectedUsernamesAfterCall[] = $expectedUsernameAfterCall;
120 $expectedOutputRegex .= 'Renamed 3 user\(s\)/';
121 $this->expectOutputRegex( $expectedOutputRegex );
122 // Check that the renames actually occurred
123 $this->newSelectQueryBuilder()
124 ->select( 'user_name' )
125 ->from( 'user' )
126 ->where( [ 'user_id' => $userIdsUsedForTest ] )
127 ->assertFieldValues( array_merge( $expectedUsernamesAfterCall, [ $testUserToNotBeRenamed->getName() ] ) );
130 public function testExecuteWithPageMoves() {
131 // Get one testing user, and create a user talk and user page for that user
132 $testUser = $this->getMutableTestUser( [], 'Abc' );
133 $testUser->getUser()->addToDatabase();
134 $this->editPage( $testUser->getUser()->getUserPage(), 'user testing1234' );
135 $userTalkPageBeforeRename = $testUser->getUser()->getUserPage()->getTalkPageIfDefined();
136 $this->editPage( $userTalkPageBeforeRename, 'usertalk testing1234' );
137 $this->editPage( Title::newFromText( $userTalkPageBeforeRename->getPrefixedText() . '/test' ), 'usertalk subpage' );
138 $testUserIdentity = $testUser->getUserIdentity();
139 // Run the maintenance script
140 $this->maintenance->setOption( 'from', 'Abc$1' );
141 $this->maintenance->setOption( 'to', 'Xyz$1' );
142 $this->maintenance->execute();
143 // Check that the output of the script is as expected
144 $expectedUsernameAfterCall = ucfirst( 'Xyz' . substr( $testUserIdentity->getName(), strlen( 'Abc' ) ) );
145 $this->expectOutputRegex(
146 '/' . preg_quote( $testUserIdentity->getName(), '/' ) .
147 ' was successfully renamed to ' . preg_quote( $expectedUsernameAfterCall, '/' ) . '[\s\S]*'
148 . 'Renamed 1 user\(s\)/'
150 // Check that the rename actually occurred
151 $this->newSelectQueryBuilder()
152 ->select( 'user_name' )
153 ->from( 'user' )
154 ->where( [ 'user_id' => $testUserIdentity->getId() ] )
155 ->assertFieldValue( $expectedUsernameAfterCall );
156 // Check that the user page, user talk page, and user talk subpage were actually moved.
157 $expectedPageContent = [
158 'user testing1234' => Title::newFromText( $expectedUsernameAfterCall, NS_USER ),
159 'usertalk testing1234' => Title::newFromText( $expectedUsernameAfterCall, NS_USER_TALK ),
160 'usertalk subpage' => Title::newFromText( "$expectedUsernameAfterCall/test", NS_USER_TALK ),
162 foreach ( $expectedPageContent as $expectedContent => $title ) {
163 $userPageContent = $this->getServiceContainer()->getRevisionLookup()
164 ->getRevisionByTitle( $title )
165 ->getContent( SlotRecord::MAIN )->getWikitextForTransclusion();
166 $this->assertSame( $expectedContent, $userPageContent );