File: PurgeExpiredWatchlistItemsTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (64 lines) | stat: -rw-r--r-- 2,468 bytes parent folder | download
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
<?php

use MediaWiki\MainConfigNames;
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
use Wikimedia\Timestamp\ConvertibleTimestamp;

/**
 * @covers \PurgeExpiredWatchlistItems
 * @group Database
 * @author Dreamy Jazz
 */
class PurgeExpiredWatchlistItemsTest extends MaintenanceBaseTestCase {
	public function getMaintenanceClass() {
		return PurgeExpiredWatchlistItems::class;
	}

	public function testExecuteWhenWatchlistExpiryDisabled() {
		$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, false );
		$this->maintenance->execute();
		$this->expectOutputRegex( '/Watchlist expiry is not enabled.*/' );
	}

	public function testExecuteWhenNoItemsInWatchedItemStore() {
		$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
		$this->maintenance->execute();
		$this->expectOutputRegex( '/0 expired watchlist entries found.*/' );
	}

	/** @dataProvider provideExecuteWhenItemsExpired */
	public function testExecuteWhenItemsExpired( $batchSize, $expiredWatchlistCount, $nonExpiredWatchlistCount ) {
		$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
		$testPage = $this->getExistingTestPage();
		$watchedItemStore = $this->getServiceContainer()->getWatchedItemStore();
		// Add some items that will be expired once we run the maintenance script.
		ConvertibleTimestamp::setFakeTime( '20230405060708' );
		for ( $i = 0; $i < $expiredWatchlistCount; $i++ ) {
			$watchedItemStore->addWatch( $this->getMutableTestUser()->getUserIdentity(), $testPage, '1 month' );
		}
		// Add some items that will not be expired.
		for ( $i = 0; $i < $nonExpiredWatchlistCount; $i++ ) {
			$watchedItemStore->addWatch( $this->getMutableTestUser()->getUserIdentity(), $testPage, '1 year' );
		}
		// Set the fake time to 2 months in advance of the previous take time
		ConvertibleTimestamp::setFakeTime( '20230605060708' );
		// Run the maintenance script
		$this->maintenance->setOption( 'batch-size', $batchSize );
		$this->maintenance->execute();
		$this->expectOutputRegex(
			"/$expiredWatchlistCount expired watchlist entries found.\nAll expired entries purged.\n/"
		);
		$this->assertSame(
			$nonExpiredWatchlistCount,
			$watchedItemStore->countWatchers( $testPage ),
			'The number of watched items after the maintenance script was not as expected.'
		);
	}

	public static function provideExecuteWhenItemsExpired() {
		return [
			'All items expired' => [ 3, 7, 0 ],
			'Some items expired' => [ 2, 3, 3 ],
		];
	}
}