| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Title\TitleValue; |
| 5 |
use MediaWiki\User\UserIdentity; |
| 6 |
use MediaWiki\User\UserIdentityValue; |
| 7 |
use MediaWiki\Watchlist\ClearUserWatchlistJob; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
class ClearUserWatchlistJobTest extends MediaWikiIntegrationTestCase { |
| 19 |
private function getUser(): UserIdentity { |
| 20 |
return new UserIdentityValue( 42, 'ClearUserWatchlistJobTestUser' ); |
| 21 |
} |
| 22 |
|
| 23 |
private function getWatchedItemStore() { |
| 24 |
return $this->getServiceContainer()->getWatchedItemStore(); |
| 25 |
} |
| 26 |
|
| 27 |
public function testRun() { |
| 28 |
$user = $this->getUser(); |
| 29 |
$watchedItemStore = $this->getWatchedItemStore(); |
| 30 |
|
| 31 |
$watchedItemStore->addWatch( $user, new TitleValue( 0, 'A' ) ); |
| 32 |
$watchedItemStore->addWatch( $user, new TitleValue( 1, 'A' ) ); |
| 33 |
$watchedItemStore->addWatch( $user, new TitleValue( 0, 'B' ) ); |
| 34 |
$watchedItemStore->addWatch( $user, new TitleValue( 1, 'B' ) ); |
| 35 |
|
| 36 |
$maxId = $watchedItemStore->getMaxId(); |
| 37 |
|
| 38 |
$watchedItemStore->addWatch( $user, new TitleValue( 0, 'C' ) ); |
| 39 |
$watchedItemStore->addWatch( $user, new TitleValue( 1, 'C' ) ); |
| 40 |
|
| 41 |
$this->overrideConfigValue( MainConfigNames::UpdateRowsPerQuery, 2 ); |
| 42 |
|
| 43 |
$jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup(); |
| 44 |
$jobQueueGroup->push( |
| 45 |
new ClearUserWatchlistJob( [ |
| 46 |
'userId' => $user->getId(), 'maxWatchlistId' => $maxId, |
| 47 |
] ) |
| 48 |
); |
| 49 |
|
| 50 |
$this->assertSame( 1, $jobQueueGroup->getQueueSizes()['clearUserWatchlist'] ); |
| 51 |
$this->assertEquals( 6, $watchedItemStore->countWatchedItems( $user ) ); |
| 52 |
$this->runJobs( [ 'complete' => false ], [ 'maxJobs' => 1 ] ); |
| 53 |
$this->assertSame( 1, $jobQueueGroup->getQueueSizes()['clearUserWatchlist'] ); |
| 54 |
$this->assertEquals( 4, $watchedItemStore->countWatchedItems( $user ) ); |
| 55 |
$this->runJobs( [ 'complete' => false ], [ 'maxJobs' => 1 ] ); |
| 56 |
$this->assertSame( 1, $jobQueueGroup->getQueueSizes()['clearUserWatchlist'] ); |
| 57 |
$this->assertEquals( 2, $watchedItemStore->countWatchedItems( $user ) ); |
| 58 |
$this->runJobs( [ 'complete' => false ], [ 'maxJobs' => 1 ] ); |
| 59 |
$this->assertSame( 0, $jobQueueGroup->getQueueSizes()['clearUserWatchlist'] ); |
| 60 |
$this->assertEquals( 2, $watchedItemStore->countWatchedItems( $user ) ); |
| 61 |
|
| 62 |
$this->assertTrue( $watchedItemStore->isWatched( $user, new TitleValue( 0, 'C' ) ) ); |
| 63 |
$this->assertTrue( $watchedItemStore->isWatched( $user, new TitleValue( 1, 'C' ) ) ); |
| 64 |
} |
| 65 |
|
| 66 |
public function testRunWithWatchlistExpiry() { |
| 67 |
|
| 68 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true ); |
| 69 |
$user = $this->getUser(); |
| 70 |
$watchedItemStore = $this->getWatchedItemStore(); |
| 71 |
|
| 72 |
|
| 73 |
$watchedItemStore->addWatch( $user, new TitleValue( 0, __METHOD__ . 'no expiry' ) ); |
| 74 |
$watchedItemStore->addWatch( $user, new TitleValue( 0, __METHOD__ . 'has expiry' ), '1 week' ); |
| 75 |
|
| 76 |
|
| 77 |
$itemIds = $this->getDb()->newSelectQueryBuilder() |
| 78 |
->select( 'wl_id' ) |
| 79 |
->from( 'watchlist' ) |
| 80 |
->where( [ 'wl_user' => $user->getId() ] ) |
| 81 |
->caller( __METHOD__ )->fetchFieldValues(); |
| 82 |
|
| 83 |
|
| 84 |
$job = new ClearUserWatchlistJob( [ |
| 85 |
'userId' => $user->getId(), |
| 86 |
'maxWatchlistId' => max( $itemIds ), |
| 87 |
] ); |
| 88 |
$this->getServiceContainer()->getJobQueueGroup()->push( $job ); |
| 89 |
$this->runJobs( [ 'complete' => false ], [ 'maxJobs' => 1 ] ); |
| 90 |
|
| 91 |
|
| 92 |
$watchedCount = $this->getDb()->newSelectQueryBuilder() |
| 93 |
->select( '*' ) |
| 94 |
->from( 'watchlist_expiry' ) |
| 95 |
->where( [ 'we_item' => $itemIds ] ) |
| 96 |
->caller( __METHOD__ )->fetchRowCount(); |
| 97 |
$this->assertSame( 0, $watchedCount ); |
| 98 |
} |
| 99 |
} |