3 namespace MediaWiki\Tests\Maintenance
;
5 use MediaWiki\User\UserIdentity
;
13 class MoveBatchTest
extends MaintenanceBaseTestCase
{
15 private static UserIdentity
$testPerformer;
17 public function getMaintenanceClass() {
18 return MoveBatch
::class;
21 public function addDBDataOnce() {
22 self
::$testPerformer = $this->getTestUser()->getUserIdentity();
25 private function getFileWithContent( string $content ): string {
26 $testFilename = $this->getNewTempFile();
27 $testFile = fopen( $testFilename, 'w' );
28 fwrite( $testFile, $content );
33 /** @dataProvider provideInvalidPerformerUsernames */
34 public function testExecuteForInvalidPerformer( $performerUsername ) {
35 $this->expectCallToFatalError();
36 $this->expectOutputRegex( '/Invalid username/' );
37 $this->maintenance
->setArg( 'listfile', $this->getNewTempFile() );
38 $this->maintenance
->setOption( 'u', $performerUsername );
39 $this->maintenance
->execute();
42 public static function provideInvalidPerformerUsernames() {
44 'Username is invalid' => [ 'Template:Testing#test' ],
45 'Username does not exist' => [ 'Non-existing-test-user-1234' ],
49 /** @dataProvider provideExecuteOnFailedMove */
50 public function testExecuteOnFailedMove( $fileInputContent, $expectedOutputRegex ) {
51 $this->expectOutputRegex( $expectedOutputRegex );
52 $this->maintenance
->setArg( 'listfile', $this->getFileWithContent( $fileInputContent ) );
53 $this->maintenance
->execute();
56 public static function provideExecuteOnFailedMove() {
58 'Line is missing line break' => [ 'Template:Test#testing', '/Error on line 1, no pipe character/' ],
59 'Invalid source title' => [ ':::|Abc', '/Invalid title on line 1/' ],
60 'Invalid destination title' => [ 'Abc|:::', '/Invalid title on line 1/' ],
61 'Non-existing source title' => [ 'Abc|Def', '/Abc --> Def FAILED[\s\S]*Abc doesn\'t exist/' ],
65 /** @dataProvider provideExecuteForGoodMove */
66 public function testExecuteForGoodMove(
67 $options, $shouldCreateRedirect, $expectedReason, $expectedPerformerUsernameCallback
69 foreach ( $options as $name => $value ) {
70 if ( is_callable( $value ) ) {
73 $this->maintenance
->setOption( $name, $value );
75 // Get a source page and destination page
76 $sourcePage = $this->getExistingTestPage();
77 $sourcePageContentBeforeMove = $sourcePage->getContent()->getWikitextForTransclusion();
78 $destPage = $this->getNonexistingTestPage();
79 // Move the page using the maintenance script
80 $this->maintenance
->setArg(
82 $this->getFileWithContent( "$sourcePage|$destPage" )
84 $this->maintenance
->execute();
85 // Validate that the move occurred
88 // First check that the source page either is a redirect or is deleted, depending on the
89 // --noredirects option being provided.
90 if ( $shouldCreateRedirect ) {
91 $this->assertTrue( $sourcePage->getContent()->isRedirect() );
93 $this->assertFalse( $sourcePage->exists() );
95 // Next check that the content of the destination page is the same as the source page (as the move should not
96 // have modified the content).
97 $this->assertSame( $sourcePageContentBeforeMove, $destPage->getContent()->getWikitextForTransclusion() );
98 // Check the reason for the move is as expected
99 $this->newSelectQueryBuilder()
100 ->select( 'comment_text' )
102 ->join( 'comment', null, 'log_comment_id=comment_id' )
103 ->where( [ 'log_type' => 'move' ] )
104 ->assertFieldValue( $expectedReason );
105 // Check the performer of the move is as expected
106 $this->newSelectQueryBuilder()
107 ->select( 'actor_name' )
109 ->join( 'actor', null, 'log_actor=actor_id' )
110 ->where( [ 'log_type' => 'move' ] )
111 ->assertFieldValue( $expectedPerformerUsernameCallback() );
112 $this->expectOutputString( "$sourcePage --> $destPage\n" );
115 public static function provideExecuteForGoodMove() {
117 'No options provided' => [ [], true, '', fn () => 'Move page script' ],
118 '--noredirects set, custom reason, and custom performer' => [
119 [ 'noredirects' => 1, 'r' => 'Test', 'u' => fn () => static::$testPerformer->getName() ],
120 false, 'Test', fn () => static::$testPerformer->getName()