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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
|
<?php
namespace MediaWiki\Tests\Maintenance;
use DeleteBatch;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use WikiPage;
/**
* @covers \DeleteBatch
* @group Database
* @author Dreamy Jazz
*/
class DeleteBatchTest extends MaintenanceBaseTestCase {
use MockAuthorityTrait;
protected function getMaintenanceClass() {
return DeleteBatch::class;
}
/**
* @param array $options
* @param string $fileContents
* @param WikiPage[] $pagesToDelete
*/
private function commonTestExecute( array $options, string $fileContents, array $pagesToDelete ) {
// Add the specified $options
foreach ( $options as $name => $value ) {
$this->maintenance->setOption( $name, $value );
}
// Create a temporary file, write $fileContents to it, and then pass the filename in argv.
$file = $this->getNewTempFile();
file_put_contents( $file, $fileContents );
$this->maintenance->setArg( 'listfile', $file );
// Call ::execute
$this->maintenance->execute();
// Verify that pages are now deleted.
foreach ( $pagesToDelete as $page ) {
$page->clear();
$this->assertFalse( $page->exists(), 'Page was not deleted' );
}
}
public function testExecute() {
$existingPages = [];
for ( $i = 0; $i < 4; $i++ ) {
$existingPages[] = $this->getExistingTestPage();
}
// Generate the file contents to pass as the 'listfile' argument and also generate the expected output regex.
$fileContents = '';
$expectedOutputRegex = '/';
foreach ( $existingPages as $page ) {
$fileContents .= $page->getTitle()->getPrefixedText() . PHP_EOL;
$expectedOutputRegex .= ".*Deleted!\n";
}
$this->expectOutputRegex( $expectedOutputRegex . '/' );
$this->commonTestExecute( [], $fileContents, $existingPages );
}
public function testExecuteForPageIds() {
$existingPages = [];
for ( $i = 0; $i < 4; $i++ ) {
$existingPages[] = $this->getExistingTestPage();
}
// Generate the file contents to pass as the 'listfile' argument and also generate the expected output regex.
$fileContents = '';
$expectedOutputRegex = '/';
foreach ( $existingPages as $page ) {
$fileContents .= $page->getId() . PHP_EOL;
$expectedOutputRegex .= ".*Deleted!\n";
}
$this->expectOutputRegex( $expectedOutputRegex . '/' );
$this->commonTestExecute( [ 'by-id' => 1 ], $fileContents, $existingPages );
}
/** @dataProvider provideExecuteForInvalidPages */
public function testExecuteForInvalidPages( $options, $fileContents, $expectedOutputRegex ) {
$this->commonTestExecute( $options, $fileContents, [] );
$this->expectOutputRegex( $expectedOutputRegex );
}
public static function provideExecuteForInvalidPages() {
return [
'Invalid page names and empty line' => [
[],
"Talk:::Test\n\n~~~~",
"/Invalid title 'Talk:::Test' on line 1\nInvalid title '~~~~' on line 3\n/"
],
'Non-existent page name' => [
[], "Non-existent-test-page-1234", "/Skipping nonexistent page 'Non-existent-test-page-1234'\n/",
],
'Invalid page IDs' => [ [ 'by-id' => 1 ], "test\n", "/Invalid page ID 'test' on line 1\n/" ],
];
}
}
|