Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / DeleteOldRevisionsTest.php
blob83d2d8e18aa31f3588eaeb2bcdcc2f759732d884
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use DeleteOldRevisions;
6 use WikiPage;
8 /**
9 * @covers \DeleteOldRevisions
10 * @group Database
11 * @author Dreamy Jazz
13 class DeleteOldRevisionsTest extends MaintenanceBaseTestCase {
14 public function getMaintenanceClass() {
15 return DeleteOldRevisions::class;
18 private function expectTheExpectedOutputRegex(
19 bool $shouldDelete, int $specifiedPageId, int $expectedOldRevisionsCount
20 ) {
21 $expectedOutputRegex = '/Delete old revisions';
22 // Only expect the page IDs to be outputted if we specified some.
23 if ( $specifiedPageId ) {
24 $expectedOutputRegex .= '[\s\S]*Limiting to page IDs.*' . $specifiedPageId;
25 } else {
26 $expectedOutputRegex .= '[\s\S]*(?!Limiting to page IDs)';
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)/';
36 $this->expectOutputRegex( $expectedOutputRegex );
39 /** @dataProvider provideExecuteForNoPages */
40 public function testExecuteForNoPages( $deleteOptionProvided ) {
41 if ( $deleteOptionProvided ) {
42 $this->maintenance->setOption( 'delete', 1 );
44 $this->maintenance->execute();
45 $this->expectTheExpectedOutputRegex( false, 0, 0 );
48 public static function provideExecuteForNoPages() {
49 return [
50 'Delete option provided' => [ true ],
51 'Delete option not provided' => [ false ],
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 );
63 /** @dataProvider provideExecute */
64 public function testExecute( $shouldDelete, $shouldSpecifyPageId, $expectedOldRevisionsCount ) {
65 $firstExistingTestPage = $this->getExistingTestPage();
66 $secondExistingTestPage = $this->getExistingTestPage();
67 $thirdExistingTestPage = $this->getExistingTestPage();
68 // Make an edit to both the first and second existing test pages, so that they have old revisions
69 $this->editPage( $firstExistingTestPage, 'abcdef' );
70 $this->editPage( $secondExistingTestPage, 'abcef' );
71 // Check that the revision count for each page is correct for the test.
72 $this->verifyRevisionCountForPage( $thirdExistingTestPage, 1 );
73 $this->verifyRevisionCountForPage( $secondExistingTestPage, 2 );
74 $this->verifyRevisionCountForPage( $firstExistingTestPage, 2 );
75 // Set the options for the maintenance script
76 if ( $shouldDelete ) {
77 $this->maintenance->setOption( 'delete', 1 );
79 if ( $shouldSpecifyPageId ) {
80 $this->maintenance->setArg( 'page_id', $firstExistingTestPage->getId() );
82 // Run the maintenance script and then verify the number of revisions for each test page is as expected
83 $this->maintenance->execute();
84 $this->verifyRevisionCountForPage( $thirdExistingTestPage, 1 );
85 $this->verifyRevisionCountForPage(
86 $secondExistingTestPage,
87 // Should not have been touched, unless --delete was specified and --page_id was not specified
88 $shouldDelete && !$shouldSpecifyPageId ? 1 : 2
90 $this->verifyRevisionCountForPage(
91 $firstExistingTestPage,
92 // Should not be touched, unless --delete is specified.
93 $shouldDelete ? 1 : 2
95 $this->expectTheExpectedOutputRegex(
96 $shouldDelete,
97 $shouldSpecifyPageId ? $firstExistingTestPage->getId() : 0,
98 $expectedOldRevisionsCount
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 ],