8 * @covers WatchedItemStore
10 class WatchedItemStoreIntegrationTest
extends MediaWikiTestCase
{
12 public function setUp() {
14 self
::$users['WatchedItemStoreIntegrationTestUser']
15 = new TestUser( 'WatchedItemStoreIntegrationTestUser' );
18 private function getUser() {
19 return self
::$users['WatchedItemStoreIntegrationTestUser']->getUser();
22 public function testWatchAndUnWatchItem() {
23 $user = $this->getUser();
24 $title = Title
::newFromText( 'WatchedItemStoreIntegrationTestPage' );
25 $store = WatchedItemStore
::getDefaultInstance();
26 // Cleanup after previous tests
27 $store->removeWatch( $user, $title );
28 $initialWatchers = $store->countWatchers( $title );
29 $initialUserWatchedItems = $store->countWatchedItems( $user );
32 $store->isWatched( $user, $title ),
33 'Page should not initially be watched'
36 $store->addWatch( $user, $title );
38 $store->isWatched( $user, $title ),
39 'Page should be watched'
41 $this->assertEquals( $initialUserWatchedItems +
1, $store->countWatchedItems( $user ) );
42 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
43 $this->assertCount( $initialUserWatchedItems +
1, $watchedItemsForUser );
44 $watchedItemsForUserHasExpectedItem = false;
45 foreach ( $watchedItemsForUser as $watchedItem ) {
47 $watchedItem->getUser()->equals( $user ) &&
48 $watchedItem->getLinkTarget() == $title->getTitleValue()
50 $watchedItemsForUserHasExpectedItem = true;
54 $watchedItemsForUserHasExpectedItem,
55 'getWatchedItemsForUser should contain the page'
57 $this->assertEquals( $initialWatchers +
1, $store->countWatchers( $title ) );
60 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
63 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers +
1 ] ],
64 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers +
1 ] )
67 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
68 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers +
2 ] )
71 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
72 $store->getNotificationTimestampsBatch( $user, [ $title ] )
75 $store->removeWatch( $user, $title );
77 $store->isWatched( $user, $title ),
78 'Page should be unwatched'
80 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
81 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
82 $this->assertCount( $initialUserWatchedItems, $watchedItemsForUser );
83 $watchedItemsForUserHasExpectedItem = false;
84 foreach ( $watchedItemsForUser as $watchedItem ) {
86 $watchedItem->getUser()->equals( $user ) &&
87 $watchedItem->getLinkTarget() == $title->getTitleValue()
89 $watchedItemsForUserHasExpectedItem = true;
93 $watchedItemsForUserHasExpectedItem,
94 'getWatchedItemsForUser should not contain the page'
96 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
99 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
102 [ $title->getNamespace() => [ $title->getDBkey() => false ] ],
103 $store->getNotificationTimestampsBatch( $user, [ $title ] )
107 public function testUpdateAndResetNotificationTimestamp() {
108 $user = $this->getUser();
109 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
110 $title = Title
::newFromText( 'WatchedItemStoreIntegrationTestPage' );
111 $store = WatchedItemStore
::getDefaultInstance();
112 $store->addWatch( $user, $title );
113 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
114 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
115 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
117 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
120 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
123 [ $title->getNamespace() => [ $title->getDBkey() => '20150202010101' ] ],
124 $store->getNotificationTimestampsBatch( $user, [ $title ] )
127 $initialVisitingWatchers - 1,
128 $store->countVisitingWatchers( $title, '20150202020202' )
131 $initialVisitingWatchers - 1,
132 $store->countVisitingWatchersMultiple(
133 [ [ $title, '20150202020202' ] ]
134 )[$title->getNamespace()][$title->getDBkey()]
137 $initialUnreadNotifications +
1,
138 $store->countUnreadNotifications( $user )
142 $store->countUnreadNotifications( $user, $initialUnreadNotifications +
1 )
145 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
146 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
148 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
149 $store->getNotificationTimestampsBatch( $user, [ $title ] )
152 $initialVisitingWatchers,
153 $store->countVisitingWatchers( $title, '20150202020202' )
156 $initialVisitingWatchers,
157 $store->countVisitingWatchersMultiple(
158 [ [ $title, '20150202020202' ] ]
159 )[$title->getNamespace()][$title->getDBkey()]
162 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialVisitingWatchers ] ],
163 $store->countVisitingWatchersMultiple(
164 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers
168 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
169 $store->countVisitingWatchersMultiple(
170 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers +
1
175 public function testDuplicateAllAssociatedEntries() {
176 $user = $this->getUser();
177 $titleOld = Title
::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
178 $titleNew = Title
::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
179 $store = WatchedItemStore
::getDefaultInstance();
180 $store->addWatch( $user, $titleOld->getSubjectPage() );
181 $store->addWatch( $user, $titleOld->getTalkPage() );
182 // Cleanup after previous tests
183 $store->removeWatch( $user, $titleNew->getSubjectPage() );
184 $store->removeWatch( $user, $titleNew->getTalkPage() );
186 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
188 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
189 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
190 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
191 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );