Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / DeleteBatchTest.php
blob66ff9274932577698a1675b06693e3a63ac74a24
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use DeleteBatch;
6 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
7 use WikiPage;
9 /**
10 * @covers \DeleteBatch
11 * @group Database
12 * @author Dreamy Jazz
14 class DeleteBatchTest extends MaintenanceBaseTestCase {
15 use MockAuthorityTrait;
17 protected function getMaintenanceClass() {
18 return DeleteBatch::class;
21 /**
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 );
35 // Call ::execute
36 $this->maintenance->execute();
37 // Verify that pages are now deleted.
38 foreach ( $pagesToDelete as $page ) {
39 $page->clear();
40 $this->assertFalse( $page->exists(), 'Page was not deleted' );
44 public function testExecute() {
45 $existingPages = [];
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.
50 $fileContents = '';
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() {
61 $existingPages = [];
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.
66 $fileContents = '';
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() {
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/"
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/" ],