Debian Codegraph @

tests/phpunit/includes/watchlist/WatchedItemStoreIntegrationTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\MainConfigNames;
4 use MediaWiki\Title\Title;
5 use MediaWiki\User\UserIdentity;
6 use MediaWiki\User\UserIdentityValue;
7 use Wikimedia\TestingAccessWrapper;
8 use Wikimedia\Timestamp\ConvertibleTimestamp;
9
10 /**
11 * @author Addshore
12 *
13 * @group Database
14 *
15 * @covers \MediaWiki\Watchlist\WatchedItemStore
16 */
17 class WatchedItemStoreIntegrationTest extends MediaWikiIntegrationTestCase {
18
19 protected function setUp(): void {
20 parent::setUp();
21 $this->overrideConfigValues( [
22 MainConfigNames::WatchlistExpiry => true,
23 MainConfigNames::WatchlistExpiryMaxDuration => '6 months',
24 ] );
25 }
26
27 private function getUser(): UserIdentity {
28 return new UserIdentityValue( 42, 'WatchedItemStoreIntegrationTestUser' );
29 }
30
31 public function testWatchAndUnWatchItem() {
32 $user = $this->getUser();
33 $title = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage' );
34 $store = $this->getServiceContainer()->getWatchedItemStore();
35 // Cleanup after previous tests
36 $store->removeWatch( $user, $title );
37 $initialWatchers = $store->countWatchers( $title );
38 $initialUserWatchedItems = $store->countWatchedItems( $user );
39
40 $this->assertFalse(
41 $store->isWatched( $user, $title ),
42 'Page should not initially be watched'
43 );
44 $this->assertFalse( $store->isTempWatched( $user, $title ) );
45
46 $store->addWatch( $user, $title );
47 $this->assertTrue(
48 $store->isWatched( $user, $title ),
49 'Page should be watched'
50 );
51 $this->assertFalse(
52 $store->isTempWatched( $user, $title ),
53 'Page should not be temporarily watched'
54 );
55 $this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
56 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
57 $this->assertCount( $initialUserWatchedItems + 1, $watchedItemsForUser );
58 $watchedItemsForUserHasExpectedItem = false;
59 foreach ( $watchedItemsForUser as $watchedItem ) {
60 if (
61 $watchedItem->getUserIdentity()->equals( $user ) &&
62 $watchedItem->getTarget() == $title->getTitleValue()
63 ) {
64 $watchedItemsForUserHasExpectedItem = true;
65 }
66 }
67 $this->assertTrue(
68 $watchedItemsForUserHasExpectedItem,
69 'getWatchedItemsForUser should contain the page'
70 );
71 $this->assertEquals( $initialWatchers + 1, $store->countWatchers( $title ) );
72 $this->assertEquals(
73 $initialWatchers + 1,
74 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
75 );
76 $this->assertEquals(
77 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialWatchers + 1 ] ],
78 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 1 ] )
79 );
80 $this->assertEquals(
81 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
82 $store->countWatchersMultiple( [ $title ], [ 'minimumWatchers' => $initialWatchers + 2 ] )
83 );
84 $this->assertEquals(
85 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
86 $store->getNotificationTimestampsBatch( $user, [ $title ] )
87 );
88
89 $store->removeWatch( $user, $title );
90 $this->assertFalse(
91 $store->isWatched( $user, $title ),
92 'Page should be unwatched'
93 );
94 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
95 $watchedItemsForUser = $store->getWatchedItemsForUser( $user );
96 $this->assertCount( $initialUserWatchedItems, $watchedItemsForUser );
97 $watchedItemsForUserHasExpectedItem = false;
98 foreach ( $watchedItemsForUser as $watchedItem ) {
99 if (
100 $watchedItem->getUserIdentity()->equals( $user ) &&
101 $watchedItem->getTarget() == $title->getTitleValue()
102 ) {
103 $watchedItemsForUserHasExpectedItem = true;
104 }
105 }
106 $this->assertFalse(
107 $watchedItemsForUserHasExpectedItem,
108 'getWatchedItemsForUser should not contain the page'
109 );
110 $this->assertEquals( $initialWatchers, $store->countWatchers( $title ) );
111 $this->assertEquals(
112 $initialWatchers,
113 $store->countWatchersMultiple( [ $title ] )[$title->getNamespace()][$title->getDBkey()]
114 );
115 $this->assertEquals(
116 [ $title->getNamespace() => [ $title->getDBkey() => false ] ],
117 $store->getNotificationTimestampsBatch( $user, [ $title ] )
118 );
119 }
120
121 public function testWatchAndUnWatchItemWithExpiry(): void {
122 $user = $this->getUser();
123 $title = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage' );
124 $store = $this->getServiceContainer()->getWatchedItemStore();
125 $initialUserWatchedItems = $store->countWatchedItems( $user );
126
127 // Watch for a duration greater than the max ($wgWatchlistExpiryMaxDuration),
128 // which should get changed to the max.
129 $expiry = wfTimestamp( TS_MW, strtotime( '10 years' ) );
130 $store->addWatch( $user, $title, $expiry );
131 $this->assertLessThanOrEqual(
132 wfTimestamp( TS_MW, strtotime( '6 months' ) ),
133 $store->loadWatchedItem( $user, $title )->getExpiry()
134 );
135
136 // Valid expiry that's less than the max.
137 $expiry = wfTimestamp( TS_MW, strtotime( '1 week' ) );
138
139 $store->addWatch( $user, $title, $expiry );
140 $this->assertSame(
141 $expiry,
142 $store->loadWatchedItem( $user, $title )->getExpiry()
143 );
144 $this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
145 $this->assertTrue( $store->isTempWatched( $user, $title ) );
146
147 // Invalid expiry, nothing should change.
148 $exceptionThrown = false;
149 try {
150 $store->addWatch( $user, $title, 'invalid expiry' );
151 } catch ( InvalidArgumentException $exception ) {
152 $exceptionThrown = true;
153 // Asserting watchedItem getExpiry stays unchanged
154 $this->assertSame(
155 $expiry,
156 $store->loadWatchedItem( $user, $title )->getExpiry()
157 );
158 $this->assertSame(
159 $initialUserWatchedItems + 1,
160 $store->countWatchedItems( $user )
161 );
162 }
163 $this->assertTrue( $exceptionThrown );
164
165 // Changed to infinity, so expiry row should be removed.
166 $store->addWatch( $user, $title, 'infinity' );
167 $this->assertNull(
168 $store->loadWatchedItem( $user, $title )->getExpiry()
169 );
170 $this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
171 $this->assertFalse( $store->isTempWatched( $user, $title ) );
172
173 // Updating to a valid expiry.
174 $store->addWatch( $user, $title, '1 month' );
175 $this->assertLessThanOrEqual(
176 strtotime( '1 month' ),
177 wfTimestamp(
178 TS_UNIX,
179 $store->loadWatchedItem( $user, $title )->getExpiry()
180 )
181 );
182 $this->assertEquals( $initialUserWatchedItems + 1, $store->countWatchedItems( $user ) );
183
184 // Expiry in the past, should not be considered watched.
185 $store->addWatch( $user, $title, '20090101000000' );
186 $this->assertEquals( $initialUserWatchedItems, $store->countWatchedItems( $user ) );
187
188 // Test isWatch(), which would normally pull from the cache. In this case
189 // the cache should bust and return false since the item has expired.
190 $this->assertFalse( $store->isWatched( $user, $title ) );
191 $this->assertFalse( $store->isTempWatched( $user, $title ) );
192 }
193
194 public function testWatchAndUnwatchMultipleWithExpiry(): void {
195 $user = $this->getUser();
196 $title1 = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage1' );
197 $title2 = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage1' );
198 $store = $this->getServiceContainer()->getWatchedItemStore();
199
200 // Use a relative timestamp in the near future to ensure we don't exceed the max.
201 // See testWatchAndUnWatchItemWithExpiry() for tests regarding the max duration.
202 $timestamp = wfTimestamp( TS_MW, strtotime( '1 week' ) );
203 $store->addWatchBatchForUser( $user, [ $title1, $title2 ], $timestamp );
204
205 $this->assertSame(
206 $timestamp,
207 $store->loadWatchedItem( $user, $title1 )->getExpiry()
208 );
209 $this->assertSame(
210 $timestamp,
211 $store->loadWatchedItem( $user, $title2 )->getExpiry()
212 );
213
214 // Clear expiries.
215 $store->addWatchBatchForUser( $user, [ $title1, $title2 ], 'infinity' );
216
217 $this->assertNull(
218 $store->loadWatchedItem( $user, $title1 )->getExpiry()
219 );
220 $this->assertNull(
221 $store->loadWatchedItem( $user, $title2 )->getExpiry()
222 );
223 }
224
225 public function testWatchBatchAndClearItems() {
226 $user = $this->getUser();
227 $title1 = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage1' );
228 $title2 = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage2' );
229 $store = $this->getServiceContainer()->getWatchedItemStore();
230
231 $store->addWatchBatchForUser( $user, [ $title1, $title2 ] );
232
233 $this->assertTrue( $store->isWatched( $user, $title1 ) );
234 $this->assertTrue( $store->isWatched( $user, $title2 ) );
235
236 $store->clearUserWatchedItems( $user );
237
238 $this->assertFalse( $store->isWatched( $user, $title1 ) );
239 $this->assertFalse( $store->isWatched( $user, $title2 ) );
240 }
241
242 public function testUpdateResetAndSetNotificationTimestamp() {
243 $user = $this->getUser();
244 $otherUser = new UserIdentityValue(
245 $user->getId() + 1,
246 $user->getName() . '_other'
247 );
248 $title = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPage' );
249 $store = $this->getServiceContainer()->getWatchedItemStore();
250 $store->addWatch( $user, $title );
251 $this->assertNull( $store->loadWatchedItem( $user, $title )->getNotificationTimestamp() );
252 $initialVisitingWatchers = $store->countVisitingWatchers( $title, '20150202020202' );
253 $initialUnreadNotifications = $store->countUnreadNotifications( $user );
254
255 $store->updateNotificationTimestamp( $otherUser, $title, '20150202010101' );
256 $this->assertSame(
257 '20150202010101',
258 $store->loadWatchedItem( $user, $title )->getNotificationTimestamp()
259 );
260 $this->assertEquals(
261 [ $title->getNamespace() => [ $title->getDBkey() => '20150202010101' ] ],
262 $store->getNotificationTimestampsBatch( $user, [ $title ] )
263 );
264 $this->assertEquals(
265 $initialVisitingWatchers - 1,
266 $store->countVisitingWatchers( $title, '20150202020202' )
267 );
268 $this->assertEquals(
269 $initialVisitingWatchers - 1,
270 $store->countVisitingWatchersMultiple(
271 [ [ $title, '20150202020202' ] ]
272 )[$title->getNamespace()][$title->getDBkey()]
273 );
274 $this->assertEquals(
275 $initialUnreadNotifications + 1,
276 $store->countUnreadNotifications( $user )
277 );
278 $this->assertSame(
279 true,
280 $store->countUnreadNotifications( $user, $initialUnreadNotifications + 1 )
281 );
282
283 $this->assertTrue( $store->resetNotificationTimestamp( $user, $title ) );
284 $this->assertNull( $store->getWatchedItem( $user, $title )->getNotificationTimestamp() );
285 $this->assertEquals(
286 [ $title->getNamespace() => [ $title->getDBkey() => null ] ],
287 $store->getNotificationTimestampsBatch( $user, [ $title ] )
288 );
289
290 // Run the job queue
291 $this->runJobs();
292
293 $this->assertEquals(
294 $initialVisitingWatchers,
295 $store->countVisitingWatchers( $title, '20150202020202' )
296 );
297 $this->assertEquals(
298 $initialVisitingWatchers,
299 $store->countVisitingWatchersMultiple(
300 [ [ $title, '20150202020202' ] ]
301 )[$title->getNamespace()][$title->getDBkey()]
302 );
303 $this->assertEquals(
304 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => $initialVisitingWatchers ] ],
305 $store->countVisitingWatchersMultiple(
306 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers
307 )
308 );
309 $this->assertEquals(
310 [ 0 => [ 'WatchedItemStoreIntegrationTestPage' => 0 ] ],
311 $store->countVisitingWatchersMultiple(
312 [ [ $title, '20150202020202' ] ], $initialVisitingWatchers + 1
313 )
314 );
315
316 // setNotificationTimestampsForUser specifying a title
317 $this->assertTrue(
318 $store->setNotificationTimestampsForUser( $user, '20100202020202', [ $title ] )
319 );
320 $this->assertSame(
321 '20100202020202',
322 $store->getWatchedItem( $user, $title )->getNotificationTimestamp()
323 );
324
325 // setNotificationTimestampsForUser not specifying a title
326 // This will try to use a DeferredUpdate; disable that
327 $mockCallback = static function ( $callback ) {
328 $callback();
329 };
330 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
331 $this->assertTrue(
332 $store->setNotificationTimestampsForUser( $user, '20110202020202' )
333 );
334 // Because the operation above is normally deferred, it doesn't clear the cache
335 // Clear the cache manually
336 $wrappedStore = TestingAccessWrapper::newFromObject( $store );
337 $wrappedStore->uncacheUser( $user );
338 $this->assertSame(
339 '20110202020202',
340 $store->getWatchedItem( $user, $title )->getNotificationTimestamp()
341 );
342 }
343
344 public function testDuplicateAllAssociatedEntries() {
345 // Fake current time to be 2020-05-27T00:00:00Z
346 ConvertibleTimestamp::setFakeTime( '20200527000000' );
347
348 $user = $this->getUser();
349 $titleOld = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPageOld' );
350 $titleNew = Title::makeTitle( NS_MAIN, 'WatchedItemStoreIntegrationTestPageNew' );
351 $store = $this->getServiceContainer()->getWatchedItemStore();
352 $store->addWatch( $user, $titleOld->getSubjectPage(), '99990123000000' );
353 $store->addWatch( $user, $titleOld->getTalkPage(), '99990123000000' );
354
355 // Fetch stored expiry (may have changed due to wgWatchlistExpiryMaxDuration).
356 // Note we use loadWatchedItem() instead of getWatchedItem() to bypass the process cache.
357 $expectedExpiry = $store->loadWatchedItem( $user, $titleOld )->getExpiry();
358
359 // Watch the new title with a different expiry, so that we can confirm
360 // it gets replaced with the old title's expiry.
361 $store->addWatch( $user, $titleNew->getSubjectPage(), '1 day' );
362 $store->addWatch( $user, $titleNew->getTalkPage(), '1 day' );
363
364 // Use the sysop test user as well on the old title, so we can test that
365 // each user's respective expiry is correctly copied.
366 $user2 = $this->getTestSysop()->getUser();
367 $store->addWatch( $user2, $titleOld->getSubjectPage(), '1 week' );
368 $store->addWatch( $user2, $titleOld->getTalkPage(), '1 week' );
369 $expectedExpiry2 = $store->loadWatchedItem( $user2, $titleOld )->getExpiry();
370
371 // Duplicate associated entries. This will try to use a DeferredUpdate; disable that.
372 $mockCallback = static function ( $callback ) {
373 $callback();
374 };
375 $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
376 $store->duplicateAllAssociatedEntries( $titleOld, $titleNew );
377
378 $this->assertTrue( $store->isWatched( $user, $titleOld->getSubjectPage() ) );
379 $this->assertTrue( $store->isWatched( $user, $titleOld->getTalkPage() ) );
380 $this->assertTrue( $store->isWatched( $user, $titleNew->getSubjectPage() ) );
381 $this->assertTrue( $store->isWatched( $user, $titleNew->getTalkPage() ) );
382
383 $oldExpiry = $store->loadWatchedItem( $user, $titleOld )->getExpiry();
384 $newExpiry = $store->loadWatchedItem( $user, $titleNew )->getExpiry();
385 $this->assertSame( $expectedExpiry, $oldExpiry );
386 $this->assertSame( $expectedExpiry, $newExpiry );
387
388 // Same for $user2 and $expectedExpiry2
389 $oldExpiry = $store->loadWatchedItem( $user2, $titleOld )->getExpiry();
390 $newExpiry = $store->loadWatchedItem( $user2, $titleNew )->getExpiry();
391 $this->assertSame( $expectedExpiry2, $oldExpiry );
392 $this->assertSame( $expectedExpiry2, $newExpiry );
393 }
394
395 public function testRemoveExpired() {
396 $store = $this->getServiceContainer()->getWatchedItemStore();
397
398 // Clear out any expired rows, to start from a known point.
399 $store->removeExpired( 10 );
400 $this->assertSame( 0, $store->countExpired() );
401
402 // Add three pages, two of which have already expired.
403 $user = $this->getUser();
404 $store->addWatch( $user, Title::makeTitle( NS_MAIN, 'P1' ), '2020-01-25' );
405 $store->addWatch( $user, Title::makeTitle( NS_MAIN, 'P2' ), '20200101000000' );
406 $store->addWatch( $user, Title::makeTitle( NS_MAIN, 'P3' ), '1 month' );
407
408 // Test that they can be counted and removed correctly.
409 $this->assertSame( 2, $store->countExpired() );
410 $store->removeExpired( 1 );
411 $this->assertSame( 1, $store->countExpired() );
412 }
413
414 public function testRemoveOrphanedExpired() {
415 $store = $this->getServiceContainer()->getWatchedItemStore();
416 // Clear out any expired rows, to start from a known point.
417 $store->removeExpired( 10 );
418
419 // Manually insert some orphaned non-expired rows.
420 $orphanRows = [
421 [ 'we_item' => '100000', 'we_expiry' => $this->getDb()->timestamp( '30300101000000' ) ],
422 [ 'we_item' => '100001', 'we_expiry' => $this->getDb()->timestamp( '30300101000000' ) ],
423 ];
424 $this->getDb()->newInsertQueryBuilder()
425 ->insertInto( 'watchlist_expiry' )
426 ->rows( $orphanRows )
427 ->caller( __METHOD__ )
428 ->execute();
429 $initialRowCount = $this->getDb()->newSelectQueryBuilder()
430 ->select( '*' )
431 ->from( 'watchlist_expiry' )
432 ->caller( __METHOD__ )->fetchRowCount();
433
434 // Make sure the orphans aren't removed if it's not requested.
435 $store->removeExpired( 10, false );
436 $this->assertSame(
437 $initialRowCount,
438 $this->getDb()->newSelectQueryBuilder()
439 ->select( '*' )
440 ->from( 'watchlist_expiry' )
441 ->caller( __METHOD__ )->fetchRowCount()
442 );
443
444 // Make sure they are removed when requested.
445 $store->removeExpired( 10, true );
446 $this->assertSame(
447 $initialRowCount - 2,
448 $this->getDb()->newSelectQueryBuilder()
449 ->select( '*' )
450 ->from( 'watchlist_expiry' )
451 ->caller( __METHOD__ )->fetchRowCount()
452 );
453 }
454 }