| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use CleanupWatchlist; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class CleanupWatchlistTest extends MaintenanceBaseTestCase { |
| 14 |
use MockAuthorityTrait; |
| 15 |
|
| 16 |
protected function getMaintenanceClass() { |
| 17 |
return CleanupWatchlist::class; |
| 18 |
} |
| 19 |
|
| 20 |
public static function provideCleanup() { |
| 21 |
return [ |
| 22 |
'Invalid title with dry run' => [ |
| 23 |
|
| 24 |
':::', |
| 25 |
|
| 26 |
null, |
| 27 |
|
| 28 |
true, |
| 29 |
|
| 30 |
4, |
| 31 |
|
| 32 |
2, |
| 33 |
], |
| 34 |
'Invalid user ID with dry run' => [ null, 0, true, 4, 2 ], |
| 35 |
'Invalid title' => [ ':::', null, false, 2, 0 ], |
| 36 |
'Invalid user ID' => [ null, 0, false, 2, 0 ], |
| 37 |
'No invalid data' => [ null, null, false, 4, 2 ], |
| 38 |
]; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
public function testCleanup( |
| 43 |
$title, $userId, $dryRun, $expectedWatchlistRowCountAfterExecution, $expectedWatchlistExpiryRowCountAfterExecution |
| 44 |
) { |
| 45 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true ); |
| 46 |
$existingTestPage = $this->getExistingTestPage(); |
| 47 |
|
| 48 |
$this->getServiceContainer()->getWatchlistManager() |
| 49 |
->addWatch( $this->getMutableTestUser()->getAuthority(), $existingTestPage ); |
| 50 |
|
| 51 |
$testUser1 = $this->getMutableTestUser()->getAuthority(); |
| 52 |
$this->getServiceContainer()->getWatchlistManager() |
| 53 |
->addWatch( $testUser1, $existingTestPage, '1 week' ); |
| 54 |
$this->getDb()->newUpdateQueryBuilder() |
| 55 |
->update( 'watchlist' ) |
| 56 |
->set( [ |
| 57 |
'wl_title' => $title ?? $existingTestPage->getDBkey(), |
| 58 |
'wl_user' => $userId ?? $testUser1->getUser()->getId() |
| 59 |
] ) |
| 60 |
->where( [ |
| 61 |
'wl_user' => $testUser1->getUser()->getId() |
| 62 |
] ) |
| 63 |
->caller( __METHOD__ ) |
| 64 |
->execute(); |
| 65 |
|
| 66 |
$this->newSelectQueryBuilder() |
| 67 |
->select( 'COUNT(*)' ) |
| 68 |
->from( 'watchlist_expiry' ) |
| 69 |
->assertFieldValue( 2 ); |
| 70 |
|
| 71 |
if ( !$dryRun ) { |
| 72 |
$this->maintenance->setOption( 'fix', 1 ); |
| 73 |
} |
| 74 |
$this->maintenance->execute(); |
| 75 |
|
| 76 |
$this->newSelectQueryBuilder() |
| 77 |
->select( 'COUNT(*)' ) |
| 78 |
->from( 'watchlist' ) |
| 79 |
->assertFieldValue( $expectedWatchlistRowCountAfterExecution ); |
| 80 |
$this->newSelectQueryBuilder() |
| 81 |
->select( 'COUNT(*)' ) |
| 82 |
->from( 'watchlist_expiry' ) |
| 83 |
->assertFieldValue( $expectedWatchlistExpiryRowCountAfterExecution ); |
| 84 |
} |
| 85 |
} |