| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Api\ApiUsageException; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use MediaWiki\Title\TitleValue; |
| 6 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 7 |
use MediaWiki\User\User; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class WatchedItemQueryServiceIntegrationTest extends MediaWikiIntegrationTestCase { |
| 15 |
|
| 16 |
protected function setUp(): void { |
| 17 |
parent::setUp(); |
| 18 |
|
| 19 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true ); |
| 20 |
} |
| 21 |
|
| 22 |
public function testGetWatchedItemsForUser(): void { |
| 23 |
$store = $this->getServiceContainer()->getWatchedItemStore(); |
| 24 |
$queryService = $this->getServiceContainer()->getWatchedItemQueryService(); |
| 25 |
$user = self::getTestUser()->getUser(); |
| 26 |
$initialCount = count( $store->getWatchedItemsForUser( $user ) ); |
| 27 |
|
| 28 |
|
| 29 |
$store->addWatch( |
| 30 |
$user, |
| 31 |
new TitleValue( 0, __METHOD__ . ' no expiry 1' ) |
| 32 |
); |
| 33 |
$store->addWatch( |
| 34 |
$user, |
| 35 |
new TitleValue( 0, __METHOD__ . ' expired a week ago or in a week' ), |
| 36 |
'1 week ago' |
| 37 |
); |
| 38 |
$result1 = $queryService->getWatchedItemsForUser( $user ); |
| 39 |
$this->assertCount( $initialCount + 1, $result1, "User ID: " . $user->getId() ); |
| 40 |
|
| 41 |
|
| 42 |
$store->addWatch( |
| 43 |
$user, |
| 44 |
new TitleValue( 0, __METHOD__ . ' no expiry 2' ) |
| 45 |
); |
| 46 |
$store->addWatch( |
| 47 |
$user, |
| 48 |
new TitleValue( 0, __METHOD__ . ' expired a week ago 2' ), |
| 49 |
'1 week ago' |
| 50 |
); |
| 51 |
$result2 = $queryService->getWatchedItemsForUser( $user ); |
| 52 |
$this->assertCount( $initialCount + 2, $result2 ); |
| 53 |
|
| 54 |
|
| 55 |
$store->addWatch( |
| 56 |
$user, |
| 57 |
new TitleValue( 0, __METHOD__ . ' expired a week ago 2' ), |
| 58 |
'infinity' |
| 59 |
); |
| 60 |
$result3 = $queryService->getWatchedItemsForUser( $user ); |
| 61 |
$this->assertCount( $initialCount + 3, $result3 ); |
| 62 |
|
| 63 |
|
| 64 |
$store->addWatch( |
| 65 |
$user, |
| 66 |
new TitleValue( 0, __METHOD__ . ' expired a week ago or in a week' ), |
| 67 |
'1 week' |
| 68 |
); |
| 69 |
$result4 = $queryService->getWatchedItemsForUser( $user ); |
| 70 |
$this->assertCount( $initialCount + 4, $result4 ); |
| 71 |
} |
| 72 |
|
| 73 |
public function testGetWatchedItemsForUserWithExpiriesDisabled() { |
| 74 |
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, false ); |
| 75 |
$store = $this->getServiceContainer()->getWatchedItemStore(); |
| 76 |
$queryService = $this->getServiceContainer()->getWatchedItemQueryService(); |
| 77 |
$user = self::getTestUser()->getUser(); |
| 78 |
$initialCount = count( $store->getWatchedItemsForUser( $user ) ); |
| 79 |
$store->addWatch( $user, new TitleValue( 0, __METHOD__ ), '1 week ago' ); |
| 80 |
$result = $queryService->getWatchedItemsForUser( $user ); |
| 81 |
$this->assertCount( $initialCount + 1, $result ); |
| 82 |
} |
| 83 |
|
| 84 |
public function testGetWatchedItemsWithRecentChangeInfo_watchlistExpiry(): void { |
| 85 |
$store = $this->getServiceContainer()->getWatchedItemStore(); |
| 86 |
$queryService = $this->getServiceContainer()->getWatchedItemQueryService(); |
| 87 |
$user = self::getTestUser()->getUser(); |
| 88 |
$options = []; |
| 89 |
$startFrom = null; |
| 90 |
$initialCount = count( $queryService->getWatchedItemsWithRecentChangeInfo( $user, |
| 91 |
$options, $startFrom ) ); |
| 92 |
|
| 93 |
|
| 94 |
$userEditTarget1 = new TitleValue( 0, __METHOD__ . ' no expiry 1' ); |
| 95 |
$this->editPage( $userEditTarget1, 'First revision' ); |
| 96 |
$store->addWatch( |
| 97 |
$user, |
| 98 |
$userEditTarget1 |
| 99 |
); |
| 100 |
|
| 101 |
$userEditTarget2 = new TitleValue( 0, __METHOD__ . ' expired a week ago or in a week' ); |
| 102 |
$this->editPage( $userEditTarget2, 'First revision' ); |
| 103 |
$store->addWatch( |
| 104 |
$user, |
| 105 |
$userEditTarget2, |
| 106 |
'1 week ago' |
| 107 |
); |
| 108 |
|
| 109 |
$result1 = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options, $startFrom ); |
| 110 |
$this->assertCount( $initialCount + 1, $result1 ); |
| 111 |
|
| 112 |
|
| 113 |
$userEditTarget3 = new TitleValue( 0, __METHOD__ . ' no expiry 2' ); |
| 114 |
$this->editPage( $userEditTarget3, 'First revision' ); |
| 115 |
$store->addWatch( |
| 116 |
$user, |
| 117 |
$userEditTarget3 |
| 118 |
); |
| 119 |
|
| 120 |
$userEditTarget4 = new TitleValue( 0, __METHOD__ . ' expired a week ago 2' ); |
| 121 |
$this->editPage( $userEditTarget4, 'First revision' ); |
| 122 |
$store->addWatch( |
| 123 |
$user, |
| 124 |
$userEditTarget4, |
| 125 |
'1 week ago' |
| 126 |
); |
| 127 |
$result2 = $queryService->getWatchedItemsWithRecentChangeInfo( $user ); |
| 128 |
$this->assertCount( $initialCount + 2, $result2 ); |
| 129 |
|
| 130 |
|
| 131 |
$store->addWatch( |
| 132 |
$user, |
| 133 |
$userEditTarget4, |
| 134 |
'infinity' |
| 135 |
); |
| 136 |
$result3 = $queryService->getWatchedItemsWithRecentChangeInfo( $user ); |
| 137 |
$this->assertCount( $initialCount + 3, $result3 ); |
| 138 |
|
| 139 |
|
| 140 |
$store->addWatch( |
| 141 |
$user, |
| 142 |
$userEditTarget2, |
| 143 |
'1 week' |
| 144 |
); |
| 145 |
$result4 = $queryService->getWatchedItemsWithRecentChangeInfo( $user ); |
| 146 |
$this->assertCount( $initialCount + 4, $result4 ); |
| 147 |
} |
| 148 |
|
| 149 |
public static function invalidWatchlistTokenProvider() { |
| 150 |
return [ |
| 151 |
[ 'wrongToken' ], |
| 152 |
[ '' ], |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public function testGetWatchedItemsWithRecentChangeInfo_watchlistOwnerAndInvalidToken( $token ) { |
| 160 |
|
| 161 |
|
| 162 |
$user = $this->createNoOpMock( |
| 163 |
User::class, |
| 164 |
[ 'isRegistered', 'getId', 'useRCPatrol' ] |
| 165 |
); |
| 166 |
$user->method( 'isRegistered' )->willReturn( true ); |
| 167 |
$user->method( 'getId' )->willReturn( 1 ); |
| 168 |
$user->method( 'useRCPatrol' )->willReturn( true ); |
| 169 |
|
| 170 |
$otherUser = $this->createNoOpMock( |
| 171 |
User::class, |
| 172 |
[ 'isRegistered', 'getId', 'useRCPatrol' ] |
| 173 |
); |
| 174 |
$otherUser->method( 'isRegistered' )->willReturn( true ); |
| 175 |
$otherUser->method( 'getId' )->willReturn( 2 ); |
| 176 |
$otherUser->method( 'useRCPatrol' )->willReturn( true ); |
| 177 |
|
| 178 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 179 |
$userOptionsLookup->expects( $this->once() ) |
| 180 |
->method( 'getOption' ) |
| 181 |
->with( $otherUser, 'watchlisttoken' ) |
| 182 |
->willReturn( '0123456789abcdef' ); |
| 183 |
|
| 184 |
$this->setService( 'UserOptionsLookup', $userOptionsLookup ); |
| 185 |
|
| 186 |
$queryService = $this->getServiceContainer()->getWatchedItemQueryService(); |
| 187 |
|
| 188 |
$this->expectException( ApiUsageException::class ); |
| 189 |
$this->expectExceptionMessage( 'Incorrect watchlist token provided' ); |
| 190 |
$queryService->getWatchedItemsWithRecentChangeInfo( |
| 191 |
$user, |
| 192 |
[ 'watchlistOwner' => $otherUser, 'watchlistOwnerToken' => $token ] |
| 193 |
); |
| 194 |
} |
| 195 |
} |