| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase; |
| 5 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class PurgeExpiredWatchlistItemsTest extends MaintenanceBaseTestCase { |
| 13 |
public function getMaintenanceClass() { |
| 14 |
return PurgeExpiredWatchlistItems::class; |
| 15 |
} |
| 16 |
|
| 17 |
public function testExecuteWhenWatchlistExpiryDisabled() { |
| 18 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, false ); |
| 19 |
$this->maintenance->execute(); |
| 20 |
$this->expectOutputRegex( '/Watchlist expiry is not enabled.*/' ); |
| 21 |
} |
| 22 |
|
| 23 |
public function testExecuteWhenNoItemsInWatchedItemStore() { |
| 24 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true ); |
| 25 |
$this->maintenance->execute(); |
| 26 |
$this->expectOutputRegex( '/0 expired watchlist entries found.*/' ); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
public function testExecuteWhenItemsExpired( $batchSize, $expiredWatchlistCount, $nonExpiredWatchlistCount ) { |
| 31 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true ); |
| 32 |
$testPage = $this->getExistingTestPage(); |
| 33 |
$watchedItemStore = $this->getServiceContainer()->getWatchedItemStore(); |
| 34 |
|
| 35 |
ConvertibleTimestamp::setFakeTime( '20230405060708' ); |
| 36 |
for ( $i = 0; $i < $expiredWatchlistCount; $i++ ) { |
| 37 |
$watchedItemStore->addWatch( $this->getMutableTestUser()->getUserIdentity(), $testPage, '1 month' ); |
| 38 |
} |
| 39 |
|
| 40 |
for ( $i = 0; $i < $nonExpiredWatchlistCount; $i++ ) { |
| 41 |
$watchedItemStore->addWatch( $this->getMutableTestUser()->getUserIdentity(), $testPage, '1 year' ); |
| 42 |
} |
| 43 |
|
| 44 |
ConvertibleTimestamp::setFakeTime( '20230605060708' ); |
| 45 |
|
| 46 |
$this->maintenance->setOption( 'batch-size', $batchSize ); |
| 47 |
$this->maintenance->execute(); |
| 48 |
$this->expectOutputRegex( |
| 49 |
"/$expiredWatchlistCount expired watchlist entries found.\nAll expired entries purged.\n/" |
| 50 |
); |
| 51 |
$this->assertSame( |
| 52 |
$nonExpiredWatchlistCount, |
| 53 |
$watchedItemStore->countWatchers( $testPage ), |
| 54 |
'The number of watched items after the maintenance script was not as expected.' |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
public static function provideExecuteWhenItemsExpired() { |
| 59 |
return [ |
| 60 |
'All items expired' => [ 3, 7, 0 ], |
| 61 |
'Some items expired' => [ 2, 3, 3 ], |
| 62 |
]; |
| 63 |
} |
| 64 |
} |