3 namespace MediaWiki\Tests\Maintenance
;
6 use MediaWiki\MainConfigNames
;
7 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
10 * @covers \CleanupWatchlist
13 class CleanupWatchlistTest
extends MaintenanceBaseTestCase
{
14 use MockAuthorityTrait
;
16 protected function getMaintenanceClass() {
17 return CleanupWatchlist
::class;
20 public static function provideCleanup() {
22 'Invalid title with dry run' => [
23 // The value of wl_title for the second watch. null if this should remain valid
25 // The value of wl_user for the second watch. null if this should remain valid
27 // Whether this is a dry run (i.e. no rows should actually be deleted
29 // The expected row count in the watchlist table after the script has run
31 // The expected row count in the watchlist_expiry table after the script has run
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 ],
41 /** @dataProvider provideCleanup */
42 public function testCleanup(
43 $title, $userId, $dryRun, $expectedWatchlistRowCountAfterExecution, $expectedWatchlistExpiryRowCountAfterExecution
45 $this->overrideConfigValue( MainConfigNames
::WatchlistExpiry
, true );
46 $existingTestPage = $this->getExistingTestPage();
47 // Add a test watchlist entry which will not be broken
48 $this->getServiceContainer()->getWatchlistManager()
49 ->addWatch( $this->getMutableTestUser()->getAuthority(), $existingTestPage );
50 // Add another watchlist entry and then break it.
51 $testUser1 = $this->getMutableTestUser()->getAuthority();
52 $this->getServiceContainer()->getWatchlistManager()
53 ->addWatch( $testUser1, $existingTestPage, '1 week' );
54 $this->getDb()->newUpdateQueryBuilder()
55 ->update( 'watchlist' )
57 'wl_title' => $title ??
$existingTestPage->getDBkey(),
58 'wl_user' => $userId ??
$testUser1->getUser()->getId()
61 'wl_user' => $testUser1->getUser()->getId()
63 ->caller( __METHOD__
)
65 // Check that watchlist_expiry is correctly set up for the test.
66 $this->newSelectQueryBuilder()
67 ->select( 'COUNT(*)' )
68 ->from( 'watchlist_expiry' )
69 ->assertFieldValue( 2 );
70 // Run the maintenance script and check that the table has been fixed if this is not a dry run
72 $this->maintenance
->setOption( 'fix', 1 );
74 $this->maintenance
->execute();
75 // Check that the DB is as expected after running the script.
76 $this->newSelectQueryBuilder()
77 ->select( 'COUNT(*)' )
79 ->assertFieldValue( $expectedWatchlistRowCountAfterExecution );
80 $this->newSelectQueryBuilder()
81 ->select( 'COUNT(*)' )
82 ->from( 'watchlist_expiry' )
83 ->assertFieldValue( $expectedWatchlistExpiryRowCountAfterExecution );