3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
10 * @covers \DeleteBatch
14 class DeleteBatchTest
extends MaintenanceBaseTestCase
{
15 use MockAuthorityTrait
;
17 protected function getMaintenanceClass() {
18 return DeleteBatch
::class;
22 * @param array $options
23 * @param string $fileContents
24 * @param WikiPage[] $pagesToDelete
26 private function commonTestExecute( array $options, string $fileContents, array $pagesToDelete ) {
27 // Add the specified $options
28 foreach ( $options as $name => $value ) {
29 $this->maintenance
->setOption( $name, $value );
31 // Create a temporary file, write $fileContents to it, and then pass the filename in argv.
32 $file = $this->getNewTempFile();
33 file_put_contents( $file, $fileContents );
34 $this->maintenance
->setArg( 'listfile', $file );
36 $this->maintenance
->execute();
37 // Verify that pages are now deleted.
38 foreach ( $pagesToDelete as $page ) {
40 $this->assertFalse( $page->exists(), 'Page was not deleted' );
44 public function testExecute() {
46 for ( $i = 0; $i < 4; $i++
) {
47 $existingPages[] = $this->getExistingTestPage();
49 // Generate the file contents to pass as the 'listfile' argument and also generate the expected output regex.
51 $expectedOutputRegex = '/';
52 foreach ( $existingPages as $page ) {
53 $fileContents .= $page->getTitle()->getPrefixedText() . PHP_EOL
;
54 $expectedOutputRegex .= ".*Deleted!\n";
56 $this->expectOutputRegex( $expectedOutputRegex . '/' );
57 $this->commonTestExecute( [], $fileContents, $existingPages );
60 public function testExecuteForPageIds() {
62 for ( $i = 0; $i < 4; $i++
) {
63 $existingPages[] = $this->getExistingTestPage();
65 // Generate the file contents to pass as the 'listfile' argument and also generate the expected output regex.
67 $expectedOutputRegex = '/';
68 foreach ( $existingPages as $page ) {
69 $fileContents .= $page->getId() . PHP_EOL
;
70 $expectedOutputRegex .= ".*Deleted!\n";
72 $this->expectOutputRegex( $expectedOutputRegex . '/' );
73 $this->commonTestExecute( [ 'by-id' => 1 ], $fileContents, $existingPages );
76 /** @dataProvider provideExecuteForInvalidPages */
77 public function testExecuteForInvalidPages( $options, $fileContents, $expectedOutputRegex ) {
78 $this->commonTestExecute( $options, $fileContents, [] );
79 $this->expectOutputRegex( $expectedOutputRegex );
82 public static function provideExecuteForInvalidPages() {
84 'Invalid page names and empty line' => [
86 "Talk:::Test\n\n~~~~",
87 "/Invalid title 'Talk:::Test' on line 1\nInvalid title '~~~~' on line 3\n/"
89 'Non-existent page name' => [
90 [], "Non-existent-test-page-1234", "/Skipping nonexistent page 'Non-existent-test-page-1234'\n/",
92 'Invalid page IDs' => [ [ 'by-id' => 1 ], "test\n", "/Invalid page ID 'test' on line 1\n/" ],