Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / CleanupWatchlistTest.php
blob075f233584fe2d16d755a6603e4eef36f5738e6a
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use CleanupWatchlist;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
9 /**
10 * @covers \CleanupWatchlist
11 * @group Database
13 class CleanupWatchlistTest extends MaintenanceBaseTestCase {
14 use MockAuthorityTrait;
16 protected function getMaintenanceClass() {
17 return CleanupWatchlist::class;
20 public static function provideCleanup() {
21 return [
22 'Invalid title with dry run' => [
23 // The value of wl_title for the second watch. null if this should remain valid
24 ':::',
25 // The value of wl_user for the second watch. null if this should remain valid
26 null,
27 // Whether this is a dry run (i.e. no rows should actually be deleted
28 true,
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
44 ) {
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' )
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 // 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
71 if ( !$dryRun ) {
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(*)' )
78 ->from( 'watchlist' )
79 ->assertFieldValue( $expectedWatchlistRowCountAfterExecution );
80 $this->newSelectQueryBuilder()
81 ->select( 'COUNT(*)' )
82 ->from( 'watchlist_expiry' )
83 ->assertFieldValue( $expectedWatchlistExpiryRowCountAfterExecution );