diff --git a/includes/jobqueue/jobs/ClearUserWatchlistJob.php b/includes/jobqueue/jobs/ClearUserWatchlistJob.php index e3736052816..0cee8e17038 100644 --- a/includes/jobqueue/jobs/ClearUserWatchlistJob.php +++ b/includes/jobqueue/jobs/ClearUserWatchlistJob.php @@ -84,6 +84,9 @@ class ClearUserWatchlistJob extends Job implements GenericParameterJob { } $dbw->delete( 'watchlist', [ 'wl_id' => $watchlistIds ], __METHOD__ ); + if ( MediaWikiServices::getInstance()->getMainConfig()->get( 'WatchlistExpiry' ) ) { + $dbw->delete( 'watchlist_expiry', [ 'we_item' => $watchlistIds ], __METHOD__ ); + } // Commit changes and remove lock before inserting next job. $lbf = MediaWikiServices::getInstance()->getDBLoadBalancerFactory(); diff --git a/tests/phpunit/includes/jobqueue/jobs/ClearUserWatchlistJobTest.php b/tests/phpunit/includes/jobqueue/jobs/ClearUserWatchlistJobTest.php index 5fd6ea8f860..470f942aedd 100644 --- a/tests/phpunit/includes/jobqueue/jobs/ClearUserWatchlistJobTest.php +++ b/tests/phpunit/includes/jobqueue/jobs/ClearUserWatchlistJobTest.php @@ -73,4 +73,39 @@ class ClearUserWatchlistJobTest extends MediaWikiTestCase { $this->assertTrue( $watchedItemStore->isWatched( $user, new TitleValue( 1, 'C' ) ) ); } + public function testRunWithWatchlistExpiry() { + // Set up. + $this->setMwGlobals( 'wgWatchlistExpiry', true ); + $user = $this->getUser(); + $watchedItemStore = $this->getWatchedItemStore(); + + // Add two watched items, one with an expiry. + $watchedItemStore->addWatch( $user, new TitleValue( 0, __METHOD__ . 'no expiry' ) ); + $watchedItemStore->addWatch( $user, new TitleValue( 0, __METHOD__ . 'has expiry' ), '1 week' ); + + // Get the IDs of these items. + $itemIds = $this->db->selectFieldValues( + [ 'watchlist' ], + 'wl_id', + [ 'wl_user' => $user->getId() ], + __METHOD__ + ); + + // Clear the watchlist by running the job. + $job = new ClearUserWatchlistJob( [ + 'userId' => $user->getId(), + 'maxWatchlistId' => max( $itemIds ), + ] ); + JobQueueGroup::singleton()->push( $job ); + $this->runJobs( 1 ); + + // Confirm that there are now no expiry records. + $watchedCount = $this->db->selectRowCount( + 'watchlist_expiry', + '*', + [ 'we_item' => $itemIds ], + __METHOD__ + ); + $this->assertSame( 0, $watchedCount ); + } }