| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use DeleteOldRevisions; |
| 6 |
use WikiPage; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class DeleteOldRevisionsTest extends MaintenanceBaseTestCase { |
| 14 |
public function getMaintenanceClass() { |
| 15 |
return DeleteOldRevisions::class; |
| 16 |
} |
| 17 |
|
| 18 |
private function expectTheExpectedOutputRegex( |
| 19 |
bool $shouldDelete, int $specifiedPageId, int $expectedOldRevisionsCount |
| 20 |
) { |
| 21 |
$expectedOutputRegex = '/Delete old revisions'; |
| 22 |
|
| 23 |
if ( $specifiedPageId ) { |
| 24 |
$expectedOutputRegex .= '[\s\S]*Limiting to page IDs.*' . $specifiedPageId; |
| 25 |
} else { |
| 26 |
$expectedOutputRegex .= '[\s\S]*(?!Limiting to page IDs)'; |
| 27 |
} |
| 28 |
$expectedOutputRegex .= '[\s\S]*Searching for active revisions.*done[\s\S]*' . |
| 29 |
'Searching for inactive revisions.*done[\s\S]*' . |
| 30 |
"$expectedOldRevisionsCount old revisions found"; |
| 31 |
if ( $shouldDelete ) { |
| 32 |
$expectedOutputRegex .= '[\s\S]*Deleting/'; |
| 33 |
} else { |
| 34 |
$expectedOutputRegex .= '[\s\S]*(?!Deleting)/'; |
| 35 |
} |
| 36 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
public function testExecuteForNoPages( $deleteOptionProvided ) { |
| 41 |
if ( $deleteOptionProvided ) { |
| 42 |
$this->maintenance->setOption( 'delete', 1 ); |
| 43 |
} |
| 44 |
$this->maintenance->execute(); |
| 45 |
$this->expectTheExpectedOutputRegex( false, 0, 0 ); |
| 46 |
} |
| 47 |
|
| 48 |
public static function provideExecuteForNoPages() { |
| 49 |
return [ |
| 50 |
'Delete option provided' => [ true ], |
| 51 |
'Delete option not provided' => [ false ], |
| 52 |
]; |
| 53 |
} |
| 54 |
|
| 55 |
private function verifyRevisionCountForPage( WikiPage $page, int $expectedCount ) { |
| 56 |
$this->newSelectQueryBuilder() |
| 57 |
->select( 'COUNT(*)' ) |
| 58 |
->from( 'revision' ) |
| 59 |
->where( [ 'rev_page' => $page->getId() ] ) |
| 60 |
->assertFieldValue( $expectedCount ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
public function testExecute( $shouldDelete, $shouldSpecifyPageId, $expectedOldRevisionsCount ) { |
| 65 |
$firstExistingTestPage = $this->getExistingTestPage(); |
| 66 |
$secondExistingTestPage = $this->getExistingTestPage(); |
| 67 |
$thirdExistingTestPage = $this->getExistingTestPage(); |
| 68 |
|
| 69 |
$this->editPage( $firstExistingTestPage, 'abcdef' ); |
| 70 |
$this->editPage( $secondExistingTestPage, 'abcef' ); |
| 71 |
|
| 72 |
$this->verifyRevisionCountForPage( $thirdExistingTestPage, 1 ); |
| 73 |
$this->verifyRevisionCountForPage( $secondExistingTestPage, 2 ); |
| 74 |
$this->verifyRevisionCountForPage( $firstExistingTestPage, 2 ); |
| 75 |
|
| 76 |
if ( $shouldDelete ) { |
| 77 |
$this->maintenance->setOption( 'delete', 1 ); |
| 78 |
} |
| 79 |
if ( $shouldSpecifyPageId ) { |
| 80 |
$this->maintenance->setArg( 'page_id', $firstExistingTestPage->getId() ); |
| 81 |
} |
| 82 |
|
| 83 |
$this->maintenance->execute(); |
| 84 |
$this->verifyRevisionCountForPage( $thirdExistingTestPage, 1 ); |
| 85 |
$this->verifyRevisionCountForPage( |
| 86 |
$secondExistingTestPage, |
| 87 |
|
| 88 |
$shouldDelete && !$shouldSpecifyPageId ? 1 : 2 |
| 89 |
); |
| 90 |
$this->verifyRevisionCountForPage( |
| 91 |
$firstExistingTestPage, |
| 92 |
|
| 93 |
$shouldDelete ? 1 : 2 |
| 94 |
); |
| 95 |
$this->expectTheExpectedOutputRegex( |
| 96 |
$shouldDelete, |
| 97 |
$shouldSpecifyPageId ? $firstExistingTestPage->getId() : 0, |
| 98 |
$expectedOldRevisionsCount |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
public static function provideExecute() { |
| 103 |
return [ |
| 104 |
'No options provided' => [ false, false, 2 ], |
| 105 |
'Not deleting, but specified page ID' => [ false, true, 1 ], |
| 106 |
'Deleting without specifying page IDs' => [ true, false, 2 ], |
| 107 |
'Deleting while specifying page ID' => [ true, true, 1 ], |
| 108 |
]; |
| 109 |
} |
| 110 |
} |