| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use DeleteBatch; |
| 6 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 7 |
use WikiPage; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class DeleteBatchTest extends MaintenanceBaseTestCase { |
| 15 |
use MockAuthorityTrait; |
| 16 |
|
| 17 |
protected function getMaintenanceClass() { |
| 18 |
return DeleteBatch::class; |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private function commonTestExecute( array $options, string $fileContents, array $pagesToDelete ) { |
| 27 |
|
| 28 |
foreach ( $options as $name => $value ) { |
| 29 |
$this->maintenance->setOption( $name, $value ); |
| 30 |
} |
| 31 |
|
| 32 |
$file = $this->getNewTempFile(); |
| 33 |
file_put_contents( $file, $fileContents ); |
| 34 |
$this->maintenance->setArg( 'listfile', $file ); |
| 35 |
|
| 36 |
$this->maintenance->execute(); |
| 37 |
|
| 38 |
foreach ( $pagesToDelete as $page ) { |
| 39 |
$page->clear(); |
| 40 |
$this->assertFalse( $page->exists(), 'Page was not deleted' ); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
public function testExecute() { |
| 45 |
$existingPages = []; |
| 46 |
for ( $i = 0; $i < 4; $i++ ) { |
| 47 |
$existingPages[] = $this->getExistingTestPage(); |
| 48 |
} |
| 49 |
|
| 50 |
$fileContents = ''; |
| 51 |
$expectedOutputRegex = '/'; |
| 52 |
foreach ( $existingPages as $page ) { |
| 53 |
$fileContents .= $page->getTitle()->getPrefixedText() . PHP_EOL; |
| 54 |
$expectedOutputRegex .= ".*Deleted!\n"; |
| 55 |
} |
| 56 |
$this->expectOutputRegex( $expectedOutputRegex . '/' ); |
| 57 |
$this->commonTestExecute( [], $fileContents, $existingPages ); |
| 58 |
} |
| 59 |
|
| 60 |
public function testExecuteForPageIds() { |
| 61 |
$existingPages = []; |
| 62 |
for ( $i = 0; $i < 4; $i++ ) { |
| 63 |
$existingPages[] = $this->getExistingTestPage(); |
| 64 |
} |
| 65 |
|
| 66 |
$fileContents = ''; |
| 67 |
$expectedOutputRegex = '/'; |
| 68 |
foreach ( $existingPages as $page ) { |
| 69 |
$fileContents .= $page->getId() . PHP_EOL; |
| 70 |
$expectedOutputRegex .= ".*Deleted!\n"; |
| 71 |
} |
| 72 |
$this->expectOutputRegex( $expectedOutputRegex . '/' ); |
| 73 |
$this->commonTestExecute( [ 'by-id' => 1 ], $fileContents, $existingPages ); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
public function testExecuteForInvalidPages( $options, $fileContents, $expectedOutputRegex ) { |
| 78 |
$this->commonTestExecute( $options, $fileContents, [] ); |
| 79 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 80 |
} |
| 81 |
|
| 82 |
public static function provideExecuteForInvalidPages() { |
| 83 |
return [ |
| 84 |
'Invalid page names and empty line' => [ |
| 85 |
[], |
| 86 |
"Talk:::Test\n\n~~~~", |
| 87 |
"/Invalid title 'Talk:::Test' on line 1\nInvalid title '~~~~' on line 3\n/" |
| 88 |
], |
| 89 |
'Non-existent page name' => [ |
| 90 |
[], "Non-existent-test-page-1234", "/Skipping nonexistent page 'Non-existent-test-page-1234'\n/", |
| 91 |
], |
| 92 |
'Invalid page IDs' => [ [ 'by-id' => 1 ], "test\n", "/Invalid page ID 'test' on line 1\n/" ], |
| 93 |
]; |
| 94 |
} |
| 95 |
} |