3 use MediaWiki\MediaWikiServices
;
10 * @covers WatchedItemStore
12 class WatchedItemStoreIntegrationTest
extends MediaWikiTestCase
{
14 public function setUp() {
16 self
::$users['WatchedItemStoreIntegrationTestUser']
17 = new TestUser( 'WatchedItemStoreIntegrationTestUser' );
20 private function getUser() {
21 return self
::$users['WatchedItemStoreIntegrationTestUser']->getUser();
24 public function testWatchAndUnWatchItem() {
25 $user = $this->getUser();
26 $title = Title
::newFromText( 'WatchedItemStoreIntegrationTestPage' );
27 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
28 // Cleanup after previous tests
29 $store->removeWatch( $user, $title );
30 $initialWatchers = $store->countWatchers( $title );
31 $initialUserWatchedItems = $store->countWatchedItems( $user );
34 $store->isWatched( $user, $title ),
35 'Page should not initially be watched'
38 $store->addWatch( $user, $title );
40 $store->isWatched( $user, $title ),
41 'Page should be watched'
43 $this->assertEquals( $initialUserWatchedItems +
1, $store->countWatchedItems( $user ) );
44 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
45 $this->assertCount( $initialUserWatchedItems +
1, $watchedItemsForUser );
46 $watchedItemsForUserHasExpectedItem = false;
47 foreach ( $watchedItemsForUser as $watchedItem ) {
49 $watchedItem->getUser()->equals( $user ) &&
50 $watchedItem->getLinkTarget() == $title->getTitleValue()
52 $watchedItemsForUserHasExpectedItem = true;
56 $watchedItemsForUserHasExpectedItem,
57 'getWatchedItemsForUser should contain the page'
59 $this->assertEquals( $initialWatchers +
1, $store->countWatchers( $title ) );
62 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
65 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers +
1 ] ],
66 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers +
1 ] )
69 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
70 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers +
2 ] )
73 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
74 $store->getNotificationTimestampsBatch( $user, [ $title ] )
77 $store->removeWatch( $user, $title );
79 $store->isWatched( $user, $title ),
80 'Page should be unwatched'
82 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
83 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
84 $this->assertCount( $initialUserWatchedItems, $watchedItemsForUser );
85 $watchedItemsForUserHasExpectedItem = false;
86 foreach ( $watchedItemsForUser as $watchedItem ) {
88 $watchedItem->getUser()->equals( $user ) &&
89 $watchedItem->getLinkTarget() == $title->getTitleValue()
91 $watchedItemsForUserHasExpectedItem = true;
95 $watchedItemsForUserHasExpectedItem,
96 'getWatchedItemsForUser should not contain the page'
98 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
101 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
104 [ $title->getNamespace() => [ $title->getDBkey() => false ] ],
105 $store->getNotificationTimestampsBatch( $user, [ $title ] )
109 public function testWatchBatchAndClearItems() {
110 $user = $this->getUser();
111 $title1 = Title
::newFromText( 'WatchedItemStoreIntegrationTestPage1' );
112 $title2 = Title
::newFromText( 'WatchedItemStoreIntegrationTestPage2' );
113 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
115 $store->addWatchBatchForUser( $user, [ $title1, $title2 ] );
117 $this->assertTrue( $store->isWatched( $user, $title1 ) );
118 $this->assertTrue( $store->isWatched( $user, $title2 ) );
120 $store->clearUserWatchedItems( $user );
122 $this->assertFalse( $store->isWatched( $user, $title1 ) );
123 $this->assertFalse( $store->isWatched( $user, $title2 ) );
126 public function testUpdateResetAndSetNotificationTimestamp() {
127 $user = $this->getUser();
128 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
129 $title = Title
::newFromText( 'WatchedItemStoreIntegrationTestPage' );
130 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
131 $store->addWatch( $user, $title );
132 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
133 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
134 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
136 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
139 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
142 [ $title->getNamespace() => [ $title->getDBkey() => '20150202010101' ] ],
143 $store->getNotificationTimestampsBatch( $user, [ $title ] )
146 $initialVisitingWatchers - 1,
147 $store->countVisitingWatchers( $title, '20150202020202' )
150 $initialVisitingWatchers - 1,
151 $store->countVisitingWatchersMultiple(
152 [ [ $title, '20150202020202' ] ]
153 )[$title->getNamespace()][$title->getDBkey()]
156 $initialUnreadNotifications +
1,
157 $store->countUnreadNotifications( $user )
161 $store->countUnreadNotifications( $user, $initialUnreadNotifications +
1 )
164 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
165 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
167 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
168 $store->getNotificationTimestampsBatch( $user, [ $title ] )
171 $initialVisitingWatchers,
172 $store->countVisitingWatchers( $title, '20150202020202' )
175 $initialVisitingWatchers,
176 $store->countVisitingWatchersMultiple(
177 [ [ $title, '20150202020202' ] ]
178 )[$title->getNamespace()][$title->getDBkey()]
181 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialVisitingWatchers ] ],
182 $store->countVisitingWatchersMultiple(
183 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers
187 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
188 $store->countVisitingWatchersMultiple(
189 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers +
1
193 // setNotificationTimestampsForUser specifying a title
195 $store->setNotificationTimestampsForUser( $user, '20200202020202', [ $title ] )
199 $store->getWatchedItem( $user, $title )->getNotificationTimestamp()
202 // setNotificationTimestampsForUser not specifying a title
204 $store->setNotificationTimestampsForUser( $user, '20210202020202' )
208 $store->getWatchedItem( $user, $title )->getNotificationTimestamp()
212 public function testDuplicateAllAssociatedEntries() {
213 $user = $this->getUser();
214 $titleOld = Title
::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
215 $titleNew = Title
::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
216 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
217 $store->addWatch( $user, $titleOld->getSubjectPage() );
218 $store->addWatch( $user, $titleOld->getTalkPage() );
219 // Cleanup after previous tests
220 $store->removeWatch( $user, $titleNew->getSubjectPage() );
221 $store->removeWatch( $user, $titleNew->getTalkPage() );
223 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
225 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
226 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
227 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
228 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );