Merge "Update docs/hooks.txt for ShowSearchHitTitle"
[mediawiki.git] / tests / phpunit / includes / WatchedItemStoreIntegrationTest.php
blob61b62aa66bb0df516947986fcb12b5adfd2a2bb6
1 <?php
3 use MediaWiki\MediaWikiServices;
5 /**
6 * @author Addshore
8 * @group Database
10 * @covers WatchedItemStore
12 class WatchedItemStoreIntegrationTest extends MediaWikiTestCase {
14 public function setUp() {
15 parent::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 );
33 $this->assertFalse(
34 $store->isWatched( $user, $title ),
35 'Page should not initially be watched'
38 $store->addWatch( $user, $title );
39 $this->assertTrue(
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 ) {
48 if (
49 $watchedItem->getUser()->equals( $user ) &&
50 $watchedItem->getLinkTarget() == $title->getTitleValue()
51 ) {
52 $watchedItemsForUserHasExpectedItem = true;
55 $this->assertTrue(
56 $watchedItemsForUserHasExpectedItem,
57 'getWatchedItemsForUser should contain the page'
59 $this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
60 $this->assertEquals(
61 $initialWatchers + 1,
62 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
64 $this->assertEquals(
65 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers + 1 ] ],
66 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 1 ] )
68 $this->assertEquals(
69 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
70 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 2 ] )
72 $this->assertEquals(
73 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
74 $store->getNotificationTimestampsBatch( $user, [ $title ] )
77 $store->removeWatch( $user, $title );
78 $this->assertFalse(
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 ) {
87 if (
88 $watchedItem->getUser()->equals( $user ) &&
89 $watchedItem->getLinkTarget() == $title->getTitleValue()
90 ) {
91 $watchedItemsForUserHasExpectedItem = true;
94 $this->assertFalse(
95 $watchedItemsForUserHasExpectedItem,
96 'getWatchedItemsForUser should not contain the page'
98 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
99 $this->assertEquals(
100 $initialWatchers,
101 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
103 $this->assertEquals(
104 [ $title->getNamespace() => [ $title->getDBkey() => false ] ],
105 $store->getNotificationTimestampsBatch( $user, [ $title ] )
109 public function testUpdateResetAndSetNotificationTimestamp() {
110 $user = $this->getUser();
111 $otherUser = ( new TestUser( 'WatchedItemStoreIntegrationTestUser_otherUser' ) )->getUser();
112 $title = Title::newFromText( 'WatchedItemStoreIntegrationTestPage' );
113 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
114 $store->addWatch( $user, $title );
115 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
116 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
117 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
119 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
120 $this->assertEquals(
121 '20150202010101',
122 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
124 $this->assertEquals(
125 [ $title->getNamespace() => [ $title->getDBkey() => '20150202010101' ] ],
126 $store->getNotificationTimestampsBatch( $user, [ $title ] )
128 $this->assertEquals(
129 $initialVisitingWatchers - 1,
130 $store->countVisitingWatchers( $title, '20150202020202' )
132 $this->assertEquals(
133 $initialVisitingWatchers - 1,
134 $store->countVisitingWatchersMultiple(
135 [ [ $title, '20150202020202' ] ]
136 )[$title->getNamespace()][$title->getDBkey()]
138 $this->assertEquals(
139 $initialUnreadNotifications + 1,
140 $store->countUnreadNotifications( $user )
142 $this->assertSame(
143 true,
144 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
147 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
148 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
149 $this->assertEquals(
150 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
151 $store->getNotificationTimestampsBatch( $user, [ $title ] )
153 $this->assertEquals(
154 $initialVisitingWatchers,
155 $store->countVisitingWatchers( $title, '20150202020202' )
157 $this->assertEquals(
158 $initialVisitingWatchers,
159 $store->countVisitingWatchersMultiple(
160 [ [ $title, '20150202020202' ] ]
161 )[$title->getNamespace()][$title->getDBkey()]
163 $this->assertEquals(
164 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialVisitingWatchers ] ],
165 $store->countVisitingWatchersMultiple(
166 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers
169 $this->assertEquals(
170 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
171 $store->countVisitingWatchersMultiple(
172 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers + 1
176 // setNotificationTimestampsForUser specifying a title
177 $this->assertTrue(
178 $store->setNotificationTimestampsForUser( $user, '20200202020202', [ $title ] )
180 $this->assertEquals(
181 '20200202020202',
182 $store->getWatchedItem( $user, $title )->getNotificationTimestamp()
185 // setNotificationTimestampsForUser not specifying a title
186 $this->assertTrue(
187 $store->setNotificationTimestampsForUser( $user, '20210202020202' )
189 $this->assertEquals(
190 '20210202020202',
191 $store->getWatchedItem( $user, $title )->getNotificationTimestamp()
195 public function testDuplicateAllAssociatedEntries() {
196 $user = $this->getUser();
197 $titleOld = Title::newFromText( 'WatchedItemStoreIntegrationTestPageOld' );
198 $titleNew = Title::newFromText( 'WatchedItemStoreIntegrationTestPageNew' );
199 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
200 $store->addWatch( $user, $titleOld->getSubjectPage() );
201 $store->addWatch( $user, $titleOld->getTalkPage() );
202 // Cleanup after previous tests
203 $store->removeWatch( $user, $titleNew->getSubjectPage() );
204 $store->removeWatch( $user, $titleNew->getTalkPage() );
206 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
208 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
209 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
210 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
211 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );