| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use MediaWiki\User\UserIdentity; |
| 6 |
use MoveBatch; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class MoveBatchTest extends MaintenanceBaseTestCase { |
| 14 |
|
| 15 |
private static UserIdentity $testPerformer; |
| 16 |
|
| 17 |
public function getMaintenanceClass() { |
| 18 |
return MoveBatch::class; |
| 19 |
} |
| 20 |
|
| 21 |
public function addDBDataOnce() { |
| 22 |
self::$testPerformer = $this->getTestUser()->getUserIdentity(); |
| 23 |
} |
| 24 |
|
| 25 |
private function getFileWithContent( string $content ): string { |
| 26 |
$testFilename = $this->getNewTempFile(); |
| 27 |
$testFile = fopen( $testFilename, 'w' ); |
| 28 |
fwrite( $testFile, $content ); |
| 29 |
fclose( $testFile ); |
| 30 |
return $testFilename; |
| 31 |
} |
| 32 |
|
| 33 |
|
| 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(); |
| 40 |
} |
| 41 |
|
| 42 |
public static function provideInvalidPerformerUsernames() { |
| 43 |
return [ |
| 44 |
'Username is invalid' => [ 'Template:Testing#test' ], |
| 45 |
'Username does not exist' => [ 'Non-existing-test-user-1234' ], |
| 46 |
]; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
public function testExecuteOnFailedMove( $fileInputContent, $expectedOutputRegex ) { |
| 51 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 52 |
$this->maintenance->setArg( 'listfile', $this->getFileWithContent( $fileInputContent ) ); |
| 53 |
$this->maintenance->execute(); |
| 54 |
} |
| 55 |
|
| 56 |
public static function provideExecuteOnFailedMove() { |
| 57 |
return [ |
| 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/' ], |
| 62 |
]; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public function testExecuteForGoodMove( |
| 67 |
$options, $shouldCreateRedirect, $expectedReason, $expectedPerformerUsernameCallback |
| 68 |
) { |
| 69 |
foreach ( $options as $name => $value ) { |
| 70 |
if ( is_callable( $value ) ) { |
| 71 |
$value = $value(); |
| 72 |
} |
| 73 |
$this->maintenance->setOption( $name, $value ); |
| 74 |
} |
| 75 |
|
| 76 |
$sourcePage = $this->getExistingTestPage(); |
| 77 |
$sourcePageContentBeforeMove = $sourcePage->getContent()->getWikitextForTransclusion(); |
| 78 |
$destPage = $this->getNonexistingTestPage(); |
| 79 |
|
| 80 |
$this->maintenance->setArg( |
| 81 |
'listfile', |
| 82 |
$this->getFileWithContent( "$sourcePage|$destPage" ) |
| 83 |
); |
| 84 |
$this->maintenance->execute(); |
| 85 |
|
| 86 |
$sourcePage->clear(); |
| 87 |
$destPage->clear(); |
| 88 |
|
| 89 |
|
| 90 |
if ( $shouldCreateRedirect ) { |
| 91 |
$this->assertTrue( $sourcePage->getContent()->isRedirect() ); |
| 92 |
} else { |
| 93 |
$this->assertFalse( $sourcePage->exists() ); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
$this->assertSame( $sourcePageContentBeforeMove, $destPage->getContent()->getWikitextForTransclusion() ); |
| 98 |
|
| 99 |
$this->newSelectQueryBuilder() |
| 100 |
->select( 'comment_text' ) |
| 101 |
->from( 'logging' ) |
| 102 |
->join( 'comment', null, 'log_comment_id=comment_id' ) |
| 103 |
->where( [ 'log_type' => 'move' ] ) |
| 104 |
->assertFieldValue( $expectedReason ); |
| 105 |
|
| 106 |
$this->newSelectQueryBuilder() |
| 107 |
->select( 'actor_name' ) |
| 108 |
->from( 'logging' ) |
| 109 |
->join( 'actor', null, 'log_actor=actor_id' ) |
| 110 |
->where( [ 'log_type' => 'move' ] ) |
| 111 |
->assertFieldValue( $expectedPerformerUsernameCallback() ); |
| 112 |
$this->expectOutputString( "$sourcePage --> $destPage\n" ); |
| 113 |
} |
| 114 |
|
| 115 |
public static function provideExecuteForGoodMove() { |
| 116 |
return [ |
| 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() |
| 121 |
], |
| 122 |
]; |
| 123 |
} |
| 124 |
} |