2020-03-06 06:03:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
2022-08-14 13:47:11 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-09-18 13:56:39 +00:00
|
|
|
use MediaWiki\Title\TitleValue;
|
2023-11-29 10:21:43 +00:00
|
|
|
use MediaWiki\User\Options\UserOptionsLookup;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-04-22 12:46:10 +00:00
|
|
|
|
2020-03-06 06:03:44 +00:00
|
|
|
/**
|
|
|
|
|
* @group Database
|
|
|
|
|
*
|
2024-05-17 22:11:47 +00:00
|
|
|
* @covers \MediaWiki\Watchlist\WatchedItemQueryService
|
2020-03-06 06:03:44 +00:00
|
|
|
*/
|
|
|
|
|
class WatchedItemQueryServiceIntegrationTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
2020-06-14 10:51:39 +00:00
|
|
|
protected function setUp(): void {
|
2020-03-06 06:03:44 +00:00
|
|
|
parent::setUp();
|
2020-09-10 10:58:13 +00:00
|
|
|
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, true );
|
2020-03-06 06:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetWatchedItemsForUser(): void {
|
2022-01-12 20:13:39 +00:00
|
|
|
$store = $this->getServiceContainer()->getWatchedItemStore();
|
|
|
|
|
$queryService = $this->getServiceContainer()->getWatchedItemQueryService();
|
2020-09-10 10:58:13 +00:00
|
|
|
$user = self::getTestUser()->getUser();
|
2020-03-06 06:03:44 +00:00
|
|
|
$initialCount = count( $store->getWatchedItemsForUser( $user ) );
|
|
|
|
|
|
|
|
|
|
// Add two watched items, one of which is already expired, and check that only 1 is returned.
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
new TitleValue( 0, __METHOD__ . ' no expiry 1' )
|
|
|
|
|
);
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
new TitleValue( 0, __METHOD__ . ' expired a week ago or in a week' ),
|
|
|
|
|
'1 week ago'
|
|
|
|
|
);
|
|
|
|
|
$result1 = $queryService->getWatchedItemsForUser( $user );
|
2020-09-10 10:58:13 +00:00
|
|
|
$this->assertCount( $initialCount + 1, $result1, "User ID: " . $user->getId() );
|
2020-03-06 06:03:44 +00:00
|
|
|
|
|
|
|
|
// Add another of each type of item, and make sure the new results are as expected.
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
new TitleValue( 0, __METHOD__ . ' no expiry 2' )
|
|
|
|
|
);
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
new TitleValue( 0, __METHOD__ . ' expired a week ago 2' ),
|
|
|
|
|
'1 week ago'
|
|
|
|
|
);
|
|
|
|
|
$result2 = $queryService->getWatchedItemsForUser( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 2, $result2 );
|
|
|
|
|
|
|
|
|
|
// Make one of the expired items permanent, and check again.
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
new TitleValue( 0, __METHOD__ . ' expired a week ago 2' ),
|
|
|
|
|
'infinity'
|
|
|
|
|
);
|
|
|
|
|
$result3 = $queryService->getWatchedItemsForUser( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 3, $result3 );
|
|
|
|
|
|
|
|
|
|
// Make the other expired item expire in a week's time, and make sure it appears in the list.
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
new TitleValue( 0, __METHOD__ . ' expired a week ago or in a week' ),
|
|
|
|
|
'1 week'
|
|
|
|
|
);
|
|
|
|
|
$result4 = $queryService->getWatchedItemsForUser( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 4, $result4 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetWatchedItemsForUserWithExpiriesDisabled() {
|
2022-08-14 13:47:11 +00:00
|
|
|
$this->overrideConfigValue( MainConfigNames::WatchlistExpiry, false );
|
2022-01-12 20:13:39 +00:00
|
|
|
$store = $this->getServiceContainer()->getWatchedItemStore();
|
|
|
|
|
$queryService = $this->getServiceContainer()->getWatchedItemQueryService();
|
2020-09-10 10:58:13 +00:00
|
|
|
$user = self::getTestUser()->getUser();
|
2020-03-06 06:03:44 +00:00
|
|
|
$initialCount = count( $store->getWatchedItemsForUser( $user ) );
|
|
|
|
|
$store->addWatch( $user, new TitleValue( 0, __METHOD__ ), '1 week ago' );
|
|
|
|
|
$result = $queryService->getWatchedItemsForUser( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 1, $result );
|
|
|
|
|
}
|
2020-03-03 00:29:28 +00:00
|
|
|
|
|
|
|
|
public function testGetWatchedItemsWithRecentChangeInfo_watchlistExpiry(): void {
|
2022-01-12 20:13:39 +00:00
|
|
|
$store = $this->getServiceContainer()->getWatchedItemStore();
|
|
|
|
|
$queryService = $this->getServiceContainer()->getWatchedItemQueryService();
|
2020-09-10 10:58:13 +00:00
|
|
|
$user = self::getTestUser()->getUser();
|
2020-03-03 00:29:28 +00:00
|
|
|
$options = [];
|
|
|
|
|
$startFrom = null;
|
|
|
|
|
$initialCount = count( $queryService->getWatchedItemsWithRecentChangeInfo( $user,
|
|
|
|
|
$options, $startFrom ) );
|
|
|
|
|
|
|
|
|
|
// Add two watched items, one of which is already expired, and check that only 1 is returned.
|
|
|
|
|
$userEditTarget1 = new TitleValue( 0, __METHOD__ . ' no expiry 1' );
|
2023-07-14 22:45:47 +00:00
|
|
|
$this->editPage( $userEditTarget1, 'First revision' );
|
2020-03-03 00:29:28 +00:00
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
$userEditTarget1
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$userEditTarget2 = new TitleValue( 0, __METHOD__ . ' expired a week ago or in a week' );
|
2023-07-14 22:45:47 +00:00
|
|
|
$this->editPage( $userEditTarget2, 'First revision' );
|
2020-03-03 00:29:28 +00:00
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
$userEditTarget2,
|
|
|
|
|
'1 week ago'
|
|
|
|
|
);
|
|
|
|
|
|
2020-06-27 01:13:01 +00:00
|
|
|
$result1 = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options, $startFrom );
|
2020-03-03 00:29:28 +00:00
|
|
|
$this->assertCount( $initialCount + 1, $result1 );
|
|
|
|
|
|
|
|
|
|
// Add another of each type of item, and make sure the new results are as expected.
|
|
|
|
|
$userEditTarget3 = new TitleValue( 0, __METHOD__ . ' no expiry 2' );
|
2023-07-14 22:45:47 +00:00
|
|
|
$this->editPage( $userEditTarget3, 'First revision' );
|
2020-03-03 00:29:28 +00:00
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
$userEditTarget3
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$userEditTarget4 = new TitleValue( 0, __METHOD__ . ' expired a week ago 2' );
|
2023-07-14 22:45:47 +00:00
|
|
|
$this->editPage( $userEditTarget4, 'First revision' );
|
2020-03-03 00:29:28 +00:00
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
$userEditTarget4,
|
|
|
|
|
'1 week ago'
|
|
|
|
|
);
|
|
|
|
|
$result2 = $queryService->getWatchedItemsWithRecentChangeInfo( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 2, $result2 );
|
|
|
|
|
|
|
|
|
|
// Make one of the expired items permanent, and check again.
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
$userEditTarget4,
|
|
|
|
|
'infinity'
|
|
|
|
|
);
|
|
|
|
|
$result3 = $queryService->getWatchedItemsWithRecentChangeInfo( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 3, $result3 );
|
|
|
|
|
|
|
|
|
|
// Make the other expired item expire in a week's time, and make sure it appears in the list.
|
|
|
|
|
$store->addWatch(
|
|
|
|
|
$user,
|
|
|
|
|
$userEditTarget2,
|
|
|
|
|
'1 week'
|
|
|
|
|
);
|
|
|
|
|
$result4 = $queryService->getWatchedItemsWithRecentChangeInfo( $user );
|
|
|
|
|
$this->assertCount( $initialCount + 4, $result4 );
|
|
|
|
|
}
|
2021-01-10 02:19:44 +00:00
|
|
|
|
2023-05-19 22:32:30 +00:00
|
|
|
public static function invalidWatchlistTokenProvider() {
|
2021-01-10 02:19:44 +00:00
|
|
|
return [
|
|
|
|
|
[ 'wrongToken' ],
|
|
|
|
|
[ '' ],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider invalidWatchlistTokenProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testGetWatchedItemsWithRecentChangeInfo_watchlistOwnerAndInvalidToken( $token ) {
|
|
|
|
|
// Moved from the Unit test because the ApiUsageException call creates a Message object
|
|
|
|
|
// and down the line needs MediaWikiServices
|
|
|
|
|
$user = $this->createNoOpMock(
|
|
|
|
|
User::class,
|
|
|
|
|
[ 'isRegistered', 'getId', 'useRCPatrol' ]
|
|
|
|
|
);
|
|
|
|
|
$user->method( 'isRegistered' )->willReturn( true );
|
|
|
|
|
$user->method( 'getId' )->willReturn( 1 );
|
|
|
|
|
$user->method( 'useRCPatrol' )->willReturn( true );
|
|
|
|
|
|
|
|
|
|
$otherUser = $this->createNoOpMock(
|
|
|
|
|
User::class,
|
2022-04-22 12:46:10 +00:00
|
|
|
[ 'isRegistered', 'getId', 'useRCPatrol' ]
|
2021-01-10 02:19:44 +00:00
|
|
|
);
|
|
|
|
|
$otherUser->method( 'isRegistered' )->willReturn( true );
|
|
|
|
|
$otherUser->method( 'getId' )->willReturn( 2 );
|
|
|
|
|
$otherUser->method( 'useRCPatrol' )->willReturn( true );
|
2022-04-22 12:46:10 +00:00
|
|
|
|
|
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
|
$userOptionsLookup->expects( $this->once() )
|
2021-01-10 02:19:44 +00:00
|
|
|
->method( 'getOption' )
|
2022-04-22 12:46:10 +00:00
|
|
|
->with( $otherUser, 'watchlisttoken' )
|
2021-01-10 02:19:44 +00:00
|
|
|
->willReturn( '0123456789abcdef' );
|
|
|
|
|
|
2022-04-22 12:46:10 +00:00
|
|
|
$this->setService( 'UserOptionsLookup', $userOptionsLookup );
|
|
|
|
|
|
|
|
|
|
$queryService = $this->getServiceContainer()->getWatchedItemQueryService();
|
|
|
|
|
|
2021-01-10 02:19:44 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
|
$this->expectExceptionMessage( 'Incorrect watchlist token provided' );
|
|
|
|
|
$queryService->getWatchedItemsWithRecentChangeInfo(
|
|
|
|
|
$user,
|
|
|
|
|
[ 'watchlistOwner' => $otherUser, 'watchlistOwnerToken' => $token ]
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-03-06 06:03:44 +00:00
|
|
|
}
|