| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 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; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class RenameUsersMatchingPatternTest extends MaintenanceBaseTestCase { |
| 19 |
|
| 20 |
protected function setUp(): void { |
| 21 |
parent::setUp(); |
| 22 |
|
| 23 |
|
| 24 |
$this->overrideConfigValue( MainConfigNames::CentralIdLookupProvider, 'local' ); |
| 25 |
} |
| 26 |
|
| 27 |
public function getMaintenanceClass() { |
| 28 |
return RenameUsersMatchingPattern::class; |
| 29 |
} |
| 30 |
|
| 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(); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
public function testExecuteForFatalError( $options, $expectedOutputRegex ) { |
| 43 |
foreach ( $options as $name => $value ) { |
| 44 |
$this->maintenance->setOption( $name, $value ); |
| 45 |
} |
| 46 |
$this->expectCallToFatalError(); |
| 47 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 48 |
$this->maintenance->execute(); |
| 49 |
} |
| 50 |
|
| 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/', |
| 56 |
] |
| 57 |
]; |
| 58 |
} |
| 59 |
|
| 60 |
public function testExecuteForInvalidNewName() { |
| 61 |
|
| 62 |
$testUser = $this->getMutableTestUser( [], 'Abc' )->getUser(); |
| 63 |
$testUser->addToDatabase(); |
| 64 |
|
| 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\)/" |
| 70 |
); |
| 71 |
$this->maintenance->execute(); |
| 72 |
} |
| 73 |
|
| 74 |
public function testExecuteNewUsernameAlreadyInUse() { |
| 75 |
|
| 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 |
|
| 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\)/" |
| 87 |
); |
| 88 |
$this->maintenance->execute(); |
| 89 |
} |
| 90 |
|
| 91 |
public function testExecuteWithoutPageMoves() { |
| 92 |
|
| 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(); |
| 100 |
} |
| 101 |
|
| 102 |
$testUserToNotBeRenamed = $this->getMutableTestUser( [], 'Def' )->getUser(); |
| 103 |
$testUserToNotBeRenamed->addToDatabase(); |
| 104 |
$userIdsUsedForTest[] = $testUserToNotBeRenamed->getId(); |
| 105 |
|
| 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 |
|
| 111 |
|
| 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; |
| 119 |
} |
| 120 |
$expectedOutputRegex .= 'Renamed 3 user\(s\)/'; |
| 121 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 122 |
|
| 123 |
$this->newSelectQueryBuilder() |
| 124 |
->select( 'user_name' ) |
| 125 |
->from( 'user' ) |
| 126 |
->where( [ 'user_id' => $userIdsUsedForTest ] ) |
| 127 |
->assertFieldValues( array_merge( $expectedUsernamesAfterCall, [ $testUserToNotBeRenamed->getName() ] ) ); |
| 128 |
} |
| 129 |
|
| 130 |
public function testExecuteWithPageMoves() { |
| 131 |
|
| 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 |
|
| 140 |
$this->maintenance->setOption( 'from', 'Abc$1' ); |
| 141 |
$this->maintenance->setOption( 'to', 'Xyz$1' ); |
| 142 |
$this->maintenance->execute(); |
| 143 |
|
| 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\)/' |
| 149 |
); |
| 150 |
|
| 151 |
$this->newSelectQueryBuilder() |
| 152 |
->select( 'user_name' ) |
| 153 |
->from( 'user' ) |
| 154 |
->where( [ 'user_id' => $testUserIdentity->getId() ] ) |
| 155 |
->assertFieldValue( $expectedUsernameAfterCall ); |
| 156 |
|
| 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 ), |
| 161 |
]; |
| 162 |
foreach ( $expectedPageContent as $expectedContent => $title ) { |
| 163 |
$userPageContent = $this->getServiceContainer()->getRevisionLookup() |
| 164 |
->getRevisionByTitle( $title ) |
| 165 |
->getContent( SlotRecord::MAIN )->getWikitextForTransclusion(); |
| 166 |
$this->assertSame( $expectedContent, $userPageContent ); |
| 167 |
} |
| 168 |
} |
| 169 |
} |