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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
<?php
namespace MediaWiki\Tests\Maintenance;
use DeleteOldRevisions;
use WikiPage;
/**
* @covers \DeleteOldRevisions
* @group Database
* @author Dreamy Jazz
*/
class DeleteOldRevisionsTest extends MaintenanceBaseTestCase {
public function getMaintenanceClass() {
return DeleteOldRevisions::class;
}
private function expectTheExpectedOutputRegex(
bool $shouldDelete, int $specifiedPageId, int $expectedOldRevisionsCount
) {
$expectedOutputRegex = '/Delete old revisions';
// Only expect the page IDs to be outputted if we specified some.
if ( $specifiedPageId ) {
$expectedOutputRegex .= '[\s\S]*Limiting to page IDs.*' . $specifiedPageId;
} else {
$expectedOutputRegex .= '[\s\S]*(?!Limiting to page IDs)';
}
$expectedOutputRegex .= '[\s\S]*Searching for active revisions.*done[\s\S]*' .
'Searching for inactive revisions.*done[\s\S]*' .
"$expectedOldRevisionsCount old revisions found";
if ( $shouldDelete ) {
$expectedOutputRegex .= '[\s\S]*Deleting/';
} else {
$expectedOutputRegex .= '[\s\S]*(?!Deleting)/';
}
$this->expectOutputRegex( $expectedOutputRegex );
}
/** @dataProvider provideExecuteForNoPages */
public function testExecuteForNoPages( $deleteOptionProvided ) {
if ( $deleteOptionProvided ) {
$this->maintenance->setOption( 'delete', 1 );
}
$this->maintenance->execute();
$this->expectTheExpectedOutputRegex( false, 0, 0 );
}
public static function provideExecuteForNoPages() {
return [
'Delete option provided' => [ true ],
'Delete option not provided' => [ false ],
];
}
private function verifyRevisionCountForPage( WikiPage $page, int $expectedCount ) {
$this->newSelectQueryBuilder()
->select( 'COUNT(*)' )
->from( 'revision' )
->where( [ 'rev_page' => $page->getId() ] )
->assertFieldValue( $expectedCount );
}
/** @dataProvider provideExecute */
public function testExecute( $shouldDelete, $shouldSpecifyPageId, $expectedOldRevisionsCount ) {
$firstExistingTestPage = $this->getExistingTestPage();
$secondExistingTestPage = $this->getExistingTestPage();
$thirdExistingTestPage = $this->getExistingTestPage();
// Make an edit to both the first and second existing test pages, so that they have old revisions
$this->editPage( $firstExistingTestPage, 'abcdef' );
$this->editPage( $secondExistingTestPage, 'abcef' );
// Check that the revision count for each page is correct for the test.
$this->verifyRevisionCountForPage( $thirdExistingTestPage, 1 );
$this->verifyRevisionCountForPage( $secondExistingTestPage, 2 );
$this->verifyRevisionCountForPage( $firstExistingTestPage, 2 );
// Set the options for the maintenance script
if ( $shouldDelete ) {
$this->maintenance->setOption( 'delete', 1 );
}
if ( $shouldSpecifyPageId ) {
$this->maintenance->setArg( 'page_id', $firstExistingTestPage->getId() );
}
// Run the maintenance script and then verify the number of revisions for each test page is as expected
$this->maintenance->execute();
$this->verifyRevisionCountForPage( $thirdExistingTestPage, 1 );
$this->verifyRevisionCountForPage(
$secondExistingTestPage,
// Should not have been touched, unless --delete was specified and --page_id was not specified
$shouldDelete && !$shouldSpecifyPageId ? 1 : 2
);
$this->verifyRevisionCountForPage(
$firstExistingTestPage,
// Should not be touched, unless --delete is specified.
$shouldDelete ? 1 : 2
);
$this->expectTheExpectedOutputRegex(
$shouldDelete,
$shouldSpecifyPageId ? $firstExistingTestPage->getId() : 0,
$expectedOldRevisionsCount
);
}
public static function provideExecute() {
return [
'No options provided' => [ false, false, 2 ],
'Not deleting, but specified page ID' => [ false, true, 1 ],
'Deleting without specifying page IDs' => [ true, false, 2 ],
'Deleting while specifying page ID' => [ true, true, 1 ],
];
}
}
|