Debian Codegraph @

tests/phpunit/unit/includes/watchlist/WatchedItemStoreUnitTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Cache\GenderCache;
4 use MediaWiki\Cache\LinkBatchFactory;
5 use MediaWiki\Cache\LinkCache;
6 use MediaWiki\Config\ServiceOptions;
7 use MediaWiki\Deferred\DeferredUpdates;
8 use MediaWiki\Language\Language;
9 use MediaWiki\Linker\LinksMigration;
10 use MediaWiki\Linker\LinkTarget;
11 use MediaWiki\Logger\LoggerFactory;
12 use MediaWiki\MainConfigNames;
13 use MediaWiki\Page\PageIdentity;
14 use MediaWiki\Page\PageIdentityValue;
15 use MediaWiki\Revision\RevisionLookup;
16 use MediaWiki\Revision\RevisionRecord;
17 use MediaWiki\Tests\Unit\DummyServicesTrait;
18 use MediaWiki\Tests\Unit\Libs\Rdbms\AddQuoterMock;
19 use MediaWiki\Title\TitleFormatter;
20 use MediaWiki\Title\TitleValue;
21 use MediaWiki\User\UserIdentityValue;
22 use MediaWiki\Watchlist\ActivityUpdateJob;
23 use MediaWiki\Watchlist\WatchedItem;
24 use MediaWiki\Watchlist\WatchedItemStore;
25 use PHPUnit\Framework\MockObject\MockObject;
26 use Wikimedia\ObjectCache\HashBagOStuff;
27 use Wikimedia\Rdbms\DeleteQueryBuilder;
28 use Wikimedia\Rdbms\Expression;
29 use Wikimedia\Rdbms\FakeResultWrapper;
30 use Wikimedia\Rdbms\IDatabase;
31 use Wikimedia\Rdbms\IExpression;
32 use Wikimedia\Rdbms\InsertQueryBuilder;
33 use Wikimedia\Rdbms\LBFactory;
34 use Wikimedia\Rdbms\ReplaceQueryBuilder;
35 use Wikimedia\Rdbms\SelectQueryBuilder;
36 use Wikimedia\Rdbms\UpdateQueryBuilder;
37 use Wikimedia\Stats\StatsFactory;
38 use Wikimedia\TestingAccessWrapper;
39
40 /**
41 * @author Addshore
42 * @author DannyS712
43 *
44 * @covers \MediaWiki\Watchlist\WatchedItemStore
45 */
46 class WatchedItemStoreUnitTest extends MediaWikiUnitTestCase {
47 use DummyServicesTrait;
48 use MockTitleTrait;
49
50 /** @var StatsFactory */
51 private $statsFactory;
52
53 protected function setUp(): void {
54 parent::setUp();
55 $this->statsFactory = StatsFactory::newNull();
56 }
57
58 /**
59 * @return MockObject&IDatabase
60 */
61 private function getMockDb() {
62 $mock = $this->createMock( IDatabase::class );
63 $mock->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $mock ) );
64 $mock->method( 'newUpdateQueryBuilder' )->willReturnCallback( static fn () => new UpdateQueryBuilder( $mock ) );
65 $mock->method( 'newDeleteQueryBuilder' )->willReturnCallback( static fn () => new DeleteQueryBuilder( $mock ) );
66 $mock->method( 'newReplaceQueryBuilder' )->willReturnCallback( static fn () => new ReplaceQueryBuilder( $mock ) );
67 $mock->method( 'newInsertQueryBuilder' )->willReturnCallback( static fn () => new InsertQueryBuilder( $mock ) );
68 $mock->method( 'expr' )->willReturnCallback( static function ( $field, $op, $value ) {
69 return new Expression( $field, $op, $value );
70 } );
71 return $mock;
72 }
73
74 private function expandExpr( array $conds ): array {
75 foreach ( $conds as &$cond ) {
76 if ( $cond instanceof IExpression ) {
77 $cond = $cond->toSql( new AddQuoterMock() );
78 }
79 }
80 return $conds;
81 }
82
83 /**
84 * @param IDatabase $mockDb
85 * @param string|null $expectedConnectionType
86 * @return MockObject&LBFactory
87 */
88 private function getMockLBFactory(
89 IDatabase $mockDb,
90 $expectedConnectionType = null
91 ) {
92 $mock = $this->createMock( LBFactory::class );
93 $mock->method( 'getLocalDomainID' )
94 ->willReturn( 'phpunitdb' );
95 $mock->method( 'getPrimaryDatabase' )
96 ->willReturn( $mockDb );
97 $mock->method( 'getReplicaDatabase' )
98 ->willReturn( $mockDb );
99 $mock->method( 'getLBsForOwner' )
100 ->willReturn( [] );
101 return $mock;
102 }
103
104 /**
105 * The job queue is used in three different places - two "push" calls, and a
106 * "lazyPush" call - we don't test any of the "push" calls, so the callback
107 * can just run the job, but we do test the "lazyPush" call, and so the test
108 * that is using this may want to do something other than just run the job, since
109 * for ActivityUpdateJob instances this results in using global functions, which we
110 * cannot do in this unit test
111 *
112 * @param bool $mockLazyPush whether to add mock behavior for "lazyPush"
113 * @return MockObject|JobQueueGroup
114 */
115 private function getMockJobQueueGroup( $mockLazyPush = true ) {
116 $mock = $this->createMock( JobQueueGroup::class );
117 $mock->method( 'push' )
118 ->willReturnCallback( static function ( Job $job ) {
119 $job->run();
120 } );
121 if ( $mockLazyPush ) {
122 $mock->method( 'lazyPush' )
123 ->willReturnCallback( static function ( Job $job ) {
124 $job->run();
125 } );
126 }
127 return $mock;
128 }
129
130 /**
131 * @return MockObject|HashBagOStuff
132 */
133 private function getMockCache() {
134 $mock = $this->getMockBuilder( HashBagOStuff::class )
135 ->disableOriginalConstructor()
136 ->onlyMethods( [ 'get', 'set', 'delete', 'makeKey' ] )
137 ->getMock();
138 $mock->method( 'makeKey' )
139 ->willReturnCallback( static function ( ...$args ) {
140 return implode( ':', $args );
141 } );
142 return $mock;
143 }
144
145 /**
146 * No methods may be called except provided callbacks, if any.
147 *
148 * @param array $callbacks Keys are method names, values are callbacks
149 * @param array $counts Keys are method names, values are expected number of times to be called
150 * (default is any number is okay)
151 * @return MockObject|RevisionLookup
152 */
153 private function getMockRevisionLookup(
154 array $callbacks = [],
155 array $counts = []
156 ): RevisionLookup {
157 $mock = $this->createNoOpMock( RevisionLookup::class, array_keys( $callbacks ) );
158 foreach ( $callbacks as $method => $callback ) {
159 $count = isset( $counts[$method] ) ? $this->exactly( $counts[$method] ) : $this->any();
160 $mock->expects( $count )
161 ->method( $method )
162 ->willReturnCallback( $callbacks[$method] );
163 }
164 return $mock;
165 }
166
167 /**
168 * @param IDatabase $mockDb
169 * @return LinkBatchFactory
170 */
171 private function getMockLinkBatchFactory( IDatabase $mockDb ) {
172 return new LinkBatchFactory(
173 $this->createMock( LinkCache::class ),
174 $this->createMock( TitleFormatter::class ),
175 $this->createMock( Language::class ),
176 $this->createMock( GenderCache::class ),
177 $this->getMockLBFactory( $mockDb ),
178 $this->createMock( LinksMigration::class ),
179 LoggerFactory::getInstance( 'LinkBatch' )
180 );
181 }
182
183 /**
184 * @param array $mocks Associative array providing mocks to use when constructing the
185 * WatchedItemStore. Anything not provided will fall back to a default. Valid keys:
186 * * lbFactory
187 * * db
188 * * queueGroup
189 * * cache
190 * * readOnlyMode
191 * * nsInfo
192 * * revisionLookup
193 * * expiryEnabled
194 * * maxExpiryDuration
195 * * watchlistPurgeRate
196 * @return WatchedItemStore
197 */
198 private function newWatchedItemStore( array $mocks = [] ): WatchedItemStore {
199 $options = new ServiceOptions( WatchedItemStore::CONSTRUCTOR_OPTIONS, [
200 MainConfigNames::UpdateRowsPerQuery => 1000,
201 MainConfigNames::WatchlistExpiry => $mocks['expiryEnabled'] ?? true,
202 MainConfigNames::WatchlistExpiryMaxDuration => $mocks['maxExpiryDuration'] ?? null,
203 MainConfigNames::WatchlistPurgeRate => $mocks['watchlistPurgeRate'] ?? 0.1,
204 ] );
205
206 $db = $mocks['db'] ?? $this->getMockDb();
207
208 // If we don't use a manual mock for something specific, get a full
209 // NamespaceInfo service from DummyServicesTrait::getDummyNamespaceInfo
210 $nsInfo = $mocks['nsInfo'] ?? $this->getDummyNamespaceInfo();
211
212 return new WatchedItemStore(
213 $options,
214 $mocks['lbFactory'] ??
215 $this->getMockLBFactory( $db ),
216 $mocks['queueGroup'] ?? $this->getMockJobQueueGroup(),
217 $mocks['stash'] ?? new HashBagOStuff(),
218 $mocks['cache'] ?? $this->getMockCache(),
219 $mocks['readOnlyMode'] ?? $this->getDummyReadOnlyMode( false ),
220 $nsInfo,
221 $mocks['revisionLookup'] ?? $this->getMockRevisionLookup(),
222 $this->getMockLinkBatchFactory( $db ),
223 $this->statsFactory
224 );
225 }
226
227 public function testClearWatchedItems() {
228 $user = new UserIdentityValue( 7, 'MockUser' );
229
230 $mockDb = $this->getMockDb();
231 $mockDb->expects( $this->once() )
232 ->method( 'selectField' )
233 ->with(
234 [ 'watchlist' ],
235 'COUNT(*)',
236 [
237 'wl_user' => $user->getId(),
238 ],
239 $this->isType( 'string' )
240 )
241 ->willReturn( 12 );
242 $mockDb->expects( $this->once() )
243 ->method( 'delete' )
244 ->with(
245 'watchlist',
246 [ 'wl_user' => 7 ],
247 $this->isType( 'string' )
248 );
249
250 $mockCache = $this->getMockCache();
251 $mockCache->expects( $this->never() )->method( 'get' );
252 $mockCache->expects( $this->never() )->method( 'set' );
253 $mockCache->expects( $this->once() )
254 ->method( 'delete' )
255 ->with( 'RM-KEY' );
256
257 $store = $this->newWatchedItemStore( [
258 'db' => $mockDb,
259 'cache' => $mockCache,
260 'expiryEnabled' => false,
261 ] );
262 TestingAccessWrapper::newFromObject( $store )
263 ->cacheIndex = [ 0 => [ 'F' => [ 7 => 'RM-KEY', 9 => 'KEEP-KEY' ] ] ];
264
265 $this->assertTrue( $store->clearUserWatchedItems( $user ) );
266 }
267
268 public function testClearWatchedItems_watchlistExpiry() {
269 $user = new UserIdentityValue( 7, 'MockUser' );
270
271 $mockDb = $this->getMockDb();
272 // Select watchlist IDs.
273 $mockDb->expects( $this->once() )
274 ->method( 'selectFieldValues' )
275 ->willReturn( [ 1, 2 ] );
276
277 $deleteArgs = [
278 [
279 'watchlist',
280 [ 'wl_id' => [ 1, 2 ] ]
281 ],
282 [
283 'watchlist_expiry',
284 [ 'we_item' => [ 1, 2 ] ]
285 ]
286 ];
287 $mockDb->expects( $this->exactly( 2 ) )
288 ->method( 'delete' )
289 ->willReturnCallback( function ( $table, $conds ) use ( &$deleteArgs ): void {
290 [ $nextTable, $nextConds ] = array_shift( $deleteArgs );
291 $this->assertSame( $nextTable, $table );
292 $this->assertSame( $nextConds, $conds );
293 } );
294 $mockDb->method( 'timestamp' )
295 ->willReturnArgument( 0 );
296
297 $mockCache = $this->getMockCache();
298 $mockCache->expects( $this->never() )->method( 'get' );
299 $mockCache->expects( $this->never() )->method( 'set' );
300 $mockCache->expects( $this->once() )
301 ->method( 'delete' )
302 ->with( 'RM-KEY' );
303
304 $store = $this->newWatchedItemStore( [
305 'db' => $mockDb,
306 'cache' => $mockCache,
307 'expiryEnabled' => true,
308 ] );
309 TestingAccessWrapper::newFromObject( $store )
310 ->cacheIndex = [ 0 => [ 'F' => [ 7 => 'RM-KEY', 9 => 'KEEP-KEY' ] ] ];
311
312 $this->assertTrue( $store->clearUserWatchedItems( $user ) );
313 }
314
315 public function testClearWatchedItems_tooManyItemsWatched() {
316 $user = new UserIdentityValue( 7, 'MockUser' );
317
318 $mockDb = $this->getMockDb();
319 $mockDb->expects( $this->once() )
320 ->method( 'selectField' )
321 ->with(
322 [ 'watchlist' ],
323 'COUNT(*)',
324 [
325 'wl_user' => $user->getId(),
326 ],
327 $this->isType( 'string' )
328 )
329 ->willReturn( 99999 );
330
331 $mockCache = $this->getMockCache();
332 $mockCache->expects( $this->never() )->method( 'get' );
333 $mockCache->expects( $this->never() )->method( 'set' );
334 $mockCache->expects( $this->never() )->method( 'delete' );
335
336 $store = $this->newWatchedItemStore( [
337 'db' => $mockDb,
338 'cache' => $mockCache,
339 'expiryEnabled' => false,
340 ] );
341
342 $this->assertFalse( $store->clearUserWatchedItems( $user ) );
343 }
344
345 public function testCountWatchedItems() {
346 $user = new UserIdentityValue( 1, 'MockUser' );
347
348 $mockDb = $this->getMockDb();
349 $mockDb->expects( $this->once() )
350 ->method( 'timestamp' )
351 ->willReturn( '20200101000000' );
352 $selectArgs = [
353 [
354 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
355 'COUNT(*)',
356 [
357 'wl_user' => $user->getId(),
358 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
359 ],
360 '12',
361 ],
362 ];
363 $mockDb->expects( $this->once() )
364 ->method( 'selectField' )
365 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
366 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
367 $this->assertSame( $nextTable, $table );
368 $this->assertSame( $nextVars, $vars );
369 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
370 return $returnValue;
371 } );
372
373 $mockCache = $this->getMockCache();
374 $mockCache->expects( $this->never() )->method( 'get' );
375 $mockCache->expects( $this->never() )->method( 'set' );
376 $mockCache->expects( $this->never() )->method( 'delete' );
377
378 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
379
380 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
381 }
382
383 public function provideTestPageFactory() {
384 yield [ static function ( $pageId, $namespace, $dbKey ) {
385 return new TitleValue( $namespace, $dbKey );
386 } ];
387 yield [ static function ( $pageId, $namespace, $dbKey ) {
388 return new PageIdentityValue( $pageId, $namespace, $dbKey, PageIdentityValue::LOCAL );
389 } ];
390 yield [ function ( $pageId, $namespace, $dbKey ) {
391 return $this->makeMockTitle( $dbKey, [
392 'id' => $pageId,
393 'namespace' => $namespace
394 ] );
395 } ];
396 }
397
398 /**
399 * @dataProvider provideTestPageFactory
400 */
401 public function testCountWatchers( $testPageFactory ) {
402 $titleValue = $testPageFactory( 100, 0, 'SomeDbKey' );
403
404 $mockDb = $this->getMockDb();
405 $mockDb->expects( $this->once() )
406 ->method( 'timestamp' )
407 ->willReturn( '20200101000000' );
408 $selectArgs = [
409 [
410 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
411 'COUNT(*)',
412 [
413 'wl_namespace' => $titleValue->getNamespace(),
414 'wl_title' => $titleValue->getDBkey(),
415 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
416 ],
417 '7',
418 ],
419 ];
420 $mockDb->expects( $this->once() )
421 ->method( 'selectField' )
422 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
423 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
424 $this->assertSame( $nextTable, $table );
425 $this->assertSame( $nextVars, $vars );
426 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
427 return $returnValue;
428 } );
429
430 $mockCache = $this->getMockCache();
431 $mockCache->expects( $this->never() )->method( 'get' );
432 $mockCache->expects( $this->never() )->method( 'set' );
433 $mockCache->expects( $this->never() )->method( 'delete' );
434
435 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
436
437 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
438 }
439
440 /**
441 * @dataProvider provideTestPageFactory
442 */
443 public function testCountWatchersMultiple( $testPageFactory ) {
444 $titleValues = [
445 $testPageFactory( 100, 0, 'SomeDbKey' ),
446 $testPageFactory( 101, 0, 'OtherDbKey' ),
447 $testPageFactory( 102, 1, 'AnotherDbKey' ),
448 ];
449
450 $mockDb = $this->getMockDb();
451
452 $dbResult = [
453 (object)[ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ],
454 (object)[ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ],
455 (object)[ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ],
456 ];
457 $mockDb->expects( $this->once() )
458 ->method( 'makeWhereFrom2d' )
459 ->with(
460 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
461 $this->isType( 'string' ),
462 $this->isType( 'string' )
463 )
464 ->willReturn( 'makeWhereFrom2d return value' );
465
466 $mockDb->expects( $this->once() )
467 ->method( 'timestamp' )
468 ->willReturn( '20200101000000' );
469
470 $selectArgs = [
471 [
472 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
473 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
474 [
475 'makeWhereFrom2d return value',
476 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
477 ],
478 [
479 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
480 ],
481 new FakeResultWrapper( $dbResult ),
482 ],
483 ];
484 $mockDb->expects( $this->once() )
485 ->method( 'select' )
486 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
487 [ $nextTable, $nextVars, $nextConds, $nextOptions, $returnValue ] = array_shift( $selectArgs );
488 $this->assertSame( $nextTable, $table );
489 $this->assertSame( $nextVars, $vars );
490 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
491 $this->assertSame( $nextOptions, $options );
492 return $returnValue;
493 } );
494
495 $mockCache = $this->getMockCache();
496 $mockCache->expects( $this->never() )->method( 'get' );
497 $mockCache->expects( $this->never() )->method( 'set' );
498 $mockCache->expects( $this->never() )->method( 'delete' );
499
500 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
501
502 $expected = [
503 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
504 1 => [ 'AnotherDbKey' => 500 ],
505 ];
506 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
507 }
508
509 public static function provideIntWithDbUnsafeVersion() {
510 return [
511 [ 50 ],
512 [ "50; DROP TABLE watchlist;\n--" ],
513 ];
514 }
515
516 public function provideTestPageFactoryAndIntWithDbUnsafeVersion() {
517 foreach ( $this->provideIntWithDbUnsafeVersion() as $dbint ) {
518 foreach ( $this->provideTestPageFactory() as $testPageFactory ) {
519 yield [ $dbint[0], $testPageFactory[0] ];
520 }
521 }
522 }
523
524 /**
525 * @dataProvider provideTestPageFactoryAndIntWithDbUnsafeVersion
526 */
527 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers, $testPageFactory ) {
528 $titleValues = [
529 $testPageFactory( 100, 0, 'SomeDbKey' ),
530 $testPageFactory( 101, 0, 'OtherDbKey' ),
531 $testPageFactory( 102, 1, 'AnotherDbKey' ),
532 ];
533
534 $mockDb = $this->getMockDb();
535
536 $dbResult = [
537 (object)[ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ],
538 (object)[ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ],
539 (object)[ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ],
540 ];
541
542 $mockDb->expects( $this->once() )
543 ->method( 'makeWhereFrom2d' )
544 ->with(
545 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
546 $this->isType( 'string' ),
547 $this->isType( 'string' )
548 )
549 ->willReturn( 'makeWhereFrom2d return value' );
550
551 $mockDb->expects( $this->once() )
552 ->method( 'timestamp' )
553 ->willReturn( '20200101000000' );
554
555 $selectArgs = [
556 [
557 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
558 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
559 [
560 'makeWhereFrom2d return value',
561 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
562 ],
563 [
564 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
565 'HAVING' => [ 'COUNT(*) >= 50' ],
566 ],
567 new FakeResultWrapper( $dbResult ),
568 ],
569 ];
570 $mockDb->expects( $this->once() )
571 ->method( 'select' )
572 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
573 [ $nextTable, $nextVars, $nextConds, $nextOptions, $returnValue ] = array_shift( $selectArgs );
574 $this->assertSame( $nextTable, $table );
575 $this->assertSame( $nextVars, $vars );
576 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
577 $this->assertSame( $nextOptions, $options );
578 return $returnValue;
579 } );
580
581 $mockCache = $this->getMockCache();
582 $mockCache->expects( $this->never() )->method( 'get' );
583 $mockCache->expects( $this->never() )->method( 'set' );
584 $mockCache->expects( $this->never() )->method( 'delete' );
585
586 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
587
588 $expected = [
589 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
590 1 => [ 'AnotherDbKey' => 500 ],
591 ];
592 $this->assertEquals(
593 $expected,
594 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
595 );
596 }
597
598 /**
599 * @dataProvider provideTestPageFactory
600 */
601 public function testCountVisitingWatchers( $testPageFactory ) {
602 $titleValue = $testPageFactory( 100, 0, 'SomeDbKey' );
603
604 $mockDb = $this->getMockDb();
605
606 $selectArgs = [
607 [
608 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
609 'COUNT(*)',
610 [
611 'wl_namespace' => $titleValue->getNamespace(),
612 'wl_title' => $titleValue->getDBkey(),
613 '(wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL)',
614 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
615 ],
616 '7',
617 ],
618 ];
619 $mockDb->expects( $this->once() )
620 ->method( 'selectField' )
621 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
622 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
623 $this->assertSame( $nextTable, $table );
624 $this->assertSame( $nextVars, $vars );
625 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
626 return $returnValue;
627 } );
628
629 $mockDb->expects( $this->exactly( 2 ) )
630 ->method( 'timestamp' )
631 ->willReturnCallback( static function ( $value ) {
632 if ( $value === 0 ) {
633 return '20200101000000';
634 }
635 return 'TS' . $value . 'TS';
636 } );
637
638 $mockCache = $this->getMockCache();
639 $mockCache->expects( $this->never() )->method( 'set' );
640 $mockCache->expects( $this->never() )->method( 'get' );
641 $mockCache->expects( $this->never() )->method( 'delete' );
642
643 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
644
645 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
646 }
647
648 /**
649 * @dataProvider provideTestPageFactory
650 */
651 public function testCountVisitingWatchersMultiple( $testPageFactory ) {
652 $titleValuesWithThresholds = [
653 [ $testPageFactory( 100, 0, 'SomeDbKey' ), '111' ],
654 [ $testPageFactory( 101, 0, 'OtherDbKey' ), '111' ],
655 [ $testPageFactory( 102, 1, 'AnotherDbKey' ), '123' ],
656 ];
657
658 $dbResult = [
659 (object)[ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ],
660 (object)[ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ],
661 (object)[ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ],
662 ];
663 $mockDb = $this->getMockDb();
664 $mockDb->expects( $this->exactly( 4 ) )
665 ->method( 'timestamp' )
666 ->willReturnCallback( static function ( $value ) {
667 if ( $value === 0 ) {
668 return '20200101000000';
669 }
670 return 'TS' . $value . 'TS';
671 } );
672
673 $mockDb->method( 'makeList' )
674 ->with(
675 $this->isType( 'array' ),
676 $this->isType( 'int' )
677 )
678 ->willReturnCallback( static function ( $a, $conj ) {
679 $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
680 return implode( $sqlConj, array_map( static function ( $s ) {
681 if ( $s instanceof IExpression ) {
682 return $s->toSql( new AddQuoterMock() );
683 }
684 return '(' . $s . ')';
685 }, $a
686 ) );
687 } );
688 $mockDb->expects( $this->never() )
689 ->method( 'makeWhereFrom2d' );
690
691 $expectedCond =
692 '((wl_namespace = 0) AND (' .
693 "((wl_title = 'SomeDbKey' AND (" .
694 "wl_notificationtimestamp >= 'TS111TS' OR wl_notificationtimestamp IS NULL" .
695 ')) OR (' .
696 "wl_title = 'OtherDbKey' AND (" .
697 "wl_notificationtimestamp >= 'TS111TS' OR wl_notificationtimestamp IS NULL" .
698 '))))' .
699 ') OR ((wl_namespace = 1) AND (' .
700 "((wl_title = 'AnotherDbKey' AND (" .
701 "wl_notificationtimestamp >= 'TS123TS' OR wl_notificationtimestamp IS NULL" .
702 ')))))';
703 $selectArgs = [
704 [
705 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
706 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
707 [
708 $expectedCond,
709 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
710 ],
711 [
712 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
713 ],
714 new FakeResultWrapper( $dbResult ),
715 ],
716 ];
717 $mockDb->expects( $this->once() )
718 ->method( 'select' )
719 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
720 [ $nextTable, $nextVars, $nextConds, $nextOptions, $returnValue ] = array_shift( $selectArgs );
721 $this->assertSame( $nextTable, $table );
722 $this->assertSame( $nextVars, $vars );
723 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
724 $this->assertSame( $nextOptions, $options );
725 return $returnValue;
726 } );
727
728 $mockCache = $this->getMockCache();
729 $mockCache->expects( $this->never() )->method( 'get' );
730 $mockCache->expects( $this->never() )->method( 'set' );
731 $mockCache->expects( $this->never() )->method( 'delete' );
732
733 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
734
735 $expected = [
736 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
737 1 => [ 'AnotherDbKey' => 500 ],
738 ];
739 $this->assertEquals(
740 $expected,
741 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
742 );
743 }
744
745 /**
746 * @dataProvider provideTestPageFactory
747 */
748 public function testCountVisitingWatchersMultiple_withMissingTargets( $testPageFactory ) {
749 $titleValuesWithThresholds = [
750 [ $testPageFactory( 100, 0, 'SomeDbKey' ), '111' ],
751 [ $testPageFactory( 101, 0, 'OtherDbKey' ), '111' ],
752 [ $testPageFactory( 102, 1, 'AnotherDbKey' ), '123' ],
753 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
754 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
755 ];
756
757 $dbResult = [
758 (object)[ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ],
759 (object)[ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ],
760 (object)[ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ],
761 (object)[ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '100' ],
762 (object)[ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '200' ],
763 ];
764 $mockDb = $this->getMockDb();
765 $mockDb->expects( $this->exactly( 3 ) )
766 ->method( 'timestamp' )
767 ->willReturnCallback( static function ( $value ) {
768 return 'TS' . $value . 'TS';
769 } );
770 $mockDb->method( 'makeList' )
771 ->with(
772 $this->isType( 'array' ),
773 $this->isType( 'int' )
774 )
775 ->willReturnCallback( static function ( $a, $conj ) {
776 $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
777 return implode( $sqlConj, array_map( static function ( $s ) {
778 if ( $s instanceof IExpression ) {
779 return $s->toSql( new AddQuoterMock() );
780 }
781 return '(' . $s . ')';
782 }, $a
783 ) );
784 } );
785 $mockDb->expects( $this->once() )
786 ->method( 'makeWhereFrom2d' )
787 ->with(
788 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
789 $this->isType( 'string' ),
790 $this->isType( 'string' )
791 )
792 ->willReturn( 'makeWhereFrom2d return value' );
793
794 $expectedCond =
795 '((wl_namespace = 0) AND (' .
796 "((wl_title = 'SomeDbKey' AND (" .
797 "wl_notificationtimestamp >= 'TS111TS' OR wl_notificationtimestamp IS NULL" .
798 ')) OR (' .
799 "wl_title = 'OtherDbKey' AND (" .
800 "wl_notificationtimestamp >= 'TS111TS' OR wl_notificationtimestamp IS NULL" .
801 '))))' .
802 ') OR ((wl_namespace = 1) AND (' .
803 "((wl_title = 'AnotherDbKey' AND (" .
804 "wl_notificationtimestamp >= 'TS123TS' OR wl_notificationtimestamp IS NULL" .
805 '))))' .
806 ') OR ' .
807 '(makeWhereFrom2d return value)';
808 $mockDb->expects( $this->once() )
809 ->method( 'select' )
810 ->with(
811 [ 'watchlist' ],
812 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
813 [ $expectedCond ],
814 $this->isType( 'string' ),
815 [
816 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
817 ]
818 )
819 ->willReturn( new FakeResultWrapper( $dbResult ) );
820
821 $mockCache = $this->getMockCache();
822 $mockCache->expects( $this->never() )->method( 'get' );
823 $mockCache->expects( $this->never() )->method( 'set' );
824 $mockCache->expects( $this->never() )->method( 'delete' );
825
826 $store = $this->newWatchedItemStore( [
827 'db' => $mockDb,
828 'cache' => $mockCache,
829 'expiryEnabled' => false
830 ] );
831
832 $expected = [
833 0 => [
834 'SomeDbKey' => 100, 'OtherDbKey' => 300,
835 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
836 ],
837 1 => [ 'AnotherDbKey' => 500 ],
838 ];
839 $this->assertEquals(
840 $expected,
841 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
842 );
843 }
844
845 /**
846 * @dataProvider provideTestPageFactoryAndIntWithDbUnsafeVersion
847 */
848 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers, $testPageFactory ) {
849 $titleValuesWithThresholds = [
850 [ $testPageFactory( 100, 0, 'SomeDbKey' ), '111' ],
851 [ $testPageFactory( 101, 0, 'OtherDbKey' ), '111' ],
852 [ $testPageFactory( 102, 1, 'AnotherDbKey' ), '123' ],
853 ];
854
855 $mockDb = $this->getMockDb();
856 $mockDb->method( 'makeList' )
857 ->willReturn( 'makeList return value' );
858 $mockDb->method( 'timestamp' )
859 ->willReturnArgument( 0 );
860 $mockDb->expects( $this->once() )
861 ->method( 'select' )
862 ->with(
863 [ 'watchlist' ],
864 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
865 [ 'makeList return value' ],
866 $this->isType( 'string' ),
867 [
868 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
869 'HAVING' => [ 'COUNT(*) >= 50' ],
870 ]
871 )
872 ->willReturn( new FakeResultWrapper( [] ) );
873
874 $mockCache = $this->getMockCache();
875 $mockCache->expects( $this->never() )->method( 'get' );
876 $mockCache->expects( $this->never() )->method( 'set' );
877 $mockCache->expects( $this->never() )->method( 'delete' );
878
879 $store = $this->newWatchedItemStore( [
880 'db' => $mockDb,
881 'cache' => $mockCache,
882 'expiryEnabled' => false,
883 ] );
884
885 $expected = [
886 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
887 1 => [ 'AnotherDbKey' => 0 ],
888 ];
889 $this->assertEquals(
890 $expected,
891 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
892 );
893 }
894
895 public function testCountUnreadNotifications() {
896 $user = new UserIdentityValue( 1, 'MockUser' );
897
898 $mockDb = $this->getMockDb();
899 $mockDb->expects( $this->once() )
900 ->method( 'selectRowCount' )
901 ->with(
902 [ 'watchlist' ],
903 '1',
904 [
905 "wl_notificationtimestamp IS NOT NULL",
906 'wl_user' => 1,
907 ],
908 $this->isType( 'string' )
909 )
910 ->willReturn( 9 );
911
912 $mockCache = $this->getMockCache();
913 $mockCache->expects( $this->never() )->method( 'set' );
914 $mockCache->expects( $this->never() )->method( 'get' );
915 $mockCache->expects( $this->never() )->method( 'delete' );
916
917 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
918
919 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
920 }
921
922 /**
923 * @dataProvider provideIntWithDbUnsafeVersion
924 */
925 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
926 $user = new UserIdentityValue( 1, 'MockUser' );
927
928 $mockDb = $this->getMockDb();
929 $mockDb->expects( $this->once() )
930 ->method( 'selectRowCount' )
931 ->with(
932 [ 'watchlist' ],
933 '1',
934 [
935 "wl_notificationtimestamp IS NOT NULL",
936 'wl_user' => 1,
937 ],
938 $this->isType( 'string' ),
939 [ 'LIMIT' => 50 ]
940 )
941 ->willReturn( 50 );
942
943 $mockCache = $this->getMockCache();
944 $mockCache->expects( $this->never() )->method( 'set' );
945 $mockCache->expects( $this->never() )->method( 'get' );
946 $mockCache->expects( $this->never() )->method( 'delete' );
947
948 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
949
950 $this->assertSame(
951 true,
952 $store->countUnreadNotifications( $user, $limit )
953 );
954 }
955
956 /**
957 * @dataProvider provideIntWithDbUnsafeVersion
958 */
959 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
960 $user = new UserIdentityValue( 1, 'MockUser' );
961
962 $mockDb = $this->getMockDb();
963 $mockDb->expects( $this->once() )
964 ->method( 'selectRowCount' )
965 ->with(
966 [ 'watchlist' ],
967 '1',
968 [
969 "wl_notificationtimestamp IS NOT NULL",
970 'wl_user' => 1,
971 ],
972 $this->isType( 'string' ),
973 [ 'LIMIT' => 50 ]
974 )
975 ->willReturn( 9 );
976
977 $mockCache = $this->getMockCache();
978 $mockCache->expects( $this->never() )->method( 'set' );
979 $mockCache->expects( $this->never() )->method( 'get' );
980 $mockCache->expects( $this->never() )->method( 'delete' );
981
982 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
983
984 $this->assertEquals(
985 9,
986 $store->countUnreadNotifications( $user, $limit )
987 );
988 }
989
990 /**
991 * @dataProvider provideTestPageFactory
992 */
993 public function testDuplicateEntry_nothingToDuplicate( $testPageFactory ) {
994 $mockDb = $this->getMockDb();
995 $mockDb->expects( $this->once() )
996 ->method( 'select' )
997 ->with(
998 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
999 [ 'wl_user', 'wl_notificationtimestamp', 'we_expiry' ],
1000 [
1001 'wl_namespace' => 0,
1002 'wl_title' => 'Old_Title',
1003 ],
1004 'MediaWiki\Watchlist\WatchedItemStore::fetchWatchedItemsForPage',
1005 [ 'FOR UPDATE' ],
1006 [ 'watchlist_expiry' => [ 'LEFT JOIN', [ 'wl_id = we_item' ] ] ]
1007 )
1008 ->willReturn( new FakeResultWrapper( [] ) );
1009
1010 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
1011
1012 $store->duplicateEntry(
1013 $testPageFactory( 100, 0, 'Old_Title' ),
1014 $testPageFactory( 101, 0, 'New_Title' )
1015 );
1016 }
1017
1018 /**
1019 * @dataProvider provideTestPageFactory
1020 */
1021 public function testDuplicateEntry_somethingToDuplicate( $testPageFactory ) {
1022 $fakeRows = [
1023 (object)[
1024 'wl_user' => '1',
1025 'wl_notificationtimestamp' => '20151212010101',
1026 ],
1027 (object)[
1028 'wl_user' => '2',
1029 'wl_notificationtimestamp' => null,
1030 ],
1031 ];
1032
1033 $mockDb = $this->getMockDb();
1034 $mockDb->expects( $this->once() )
1035 ->method( 'select' )
1036 ->with(
1037 [ 'watchlist' ],
1038 [ 'wl_user', 'wl_notificationtimestamp' ],
1039 [
1040 'wl_namespace' => 0,
1041 'wl_title' => 'Old_Title',
1042 ]
1043 )
1044 ->willReturn(
1045 new FakeResultWrapper( $fakeRows ),
1046 );
1047
1048 $mockCache = $this->getMockCache();
1049 $mockCache->expects( $this->never() )->method( 'get' );
1050 $mockCache->expects( $this->never() )->method( 'delete' );
1051
1052 $store = $this->newWatchedItemStore( [
1053 'db' => $mockDb,
1054 'cache' => $mockCache,
1055 'expiryEnabled' => false,
1056 ] );
1057
1058 $store->duplicateEntry(
1059 $testPageFactory( 100, 0, 'Old_Title' ),
1060 $testPageFactory( 101, 0, 'New_Title' )
1061 );
1062 }
1063
1064 /**
1065 * @dataProvider provideTestPageFactory
1066 */
1067 public function testDuplicateAllAssociatedEntries_nothingToDuplicate( $testPageFactory ) {
1068 $selectArgs = [
1069 [
1070 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1071 [ 'wl_user', 'wl_notificationtimestamp', 'we_expiry' ],
1072 [
1073 'wl_namespace' => 0,
1074 'wl_title' => 'Old_Title',
1075 ]
1076 ],
1077 [
1078 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1079 [ 'wl_user', 'wl_notificationtimestamp', 'we_expiry' ],
1080 [
1081 'wl_namespace' => 1,
1082 'wl_title' => 'Old_Title',
1083 ]
1084 ]
1085 ];
1086 $mockDb = $this->getMockDb();
1087 $mockDb->expects( $this->exactly( 2 ) )
1088 ->method( 'select' )
1089 ->willReturnCallback( function ( $table, $vars, $conds ) use ( &$selectArgs ) {
1090 [ $nextTable, $nextVars, $nextConds ] = array_shift( $selectArgs );
1091 $this->assertSame( $nextTable, $table );
1092 $this->assertSame( $nextVars, $vars );
1093 $this->assertSame( $nextConds, $conds );
1094 return new FakeResultWrapper( [] );
1095 } );
1096
1097 $mockCache = $this->getMockCache();
1098 $mockCache->expects( $this->never() )->method( 'get' );
1099 $mockCache->expects( $this->never() )->method( 'delete' );
1100
1101 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1102
1103 $store->duplicateAllAssociatedEntries(
1104 $testPageFactory( 100, 0, 'Old_Title' ),
1105 $testPageFactory( 101, 0, 'New_Title' )
1106 );
1107 }
1108
1109 public function provideLinkTargetPairs() {
1110 foreach ( $this->provideTestPageFactory() as $testPageFactoryArray ) {
1111 $testPageFactory = $testPageFactoryArray[0];
1112 yield [ $testPageFactory( 100, 0, 'Old_Title' ), $testPageFactory( 101, 0, 'New_Title' ) ];
1113 }
1114 }
1115
1116 /**
1117 * @param LinkTarget|PageIdentity $oldTarget
1118 * @param LinkTarget|PageIdentity $newTarget
1119 * @dataProvider provideLinkTargetPairs
1120 */
1121 public function testDuplicateAllAssociatedEntries_somethingToDuplicate(
1122 $oldTarget,
1123 $newTarget
1124 ) {
1125 $fakeRows = [
1126 (object)[
1127 'wl_user' => '1',
1128 'wl_notificationtimestamp' => '20151212010101',
1129 'we_expiry' => null,
1130 ],
1131 ];
1132
1133 $selectArgs = [
1134 [
1135 [ 'watchlist' ],
1136 [ 'wl_user', 'wl_notificationtimestamp' ],
1137 [
1138 'wl_namespace' => $oldTarget->getNamespace(),
1139 'wl_title' => $oldTarget->getDBkey(),
1140 ]
1141 ],
1142 [
1143 [ 'watchlist' ],
1144 [ 'wl_user', 'wl_notificationtimestamp' ],
1145 [
1146 'wl_namespace' => $oldTarget->getNamespace() + 1,
1147 'wl_title' => $oldTarget->getDBkey(),
1148 ]
1149 ]
1150 ];
1151 $mockDb = $this->getMockDb();
1152 $mockDb->expects( $this->exactly( 2 ) )
1153 ->method( 'select' )
1154 ->willReturnCallback( function ( $table, $vars, $conds ) use ( &$selectArgs, $fakeRows ) {
1155 [ $nextTable, $nextVars, $nextConds ] = array_shift( $selectArgs );
1156 $this->assertSame( $nextTable, $table );
1157 $this->assertSame( $nextVars, $vars );
1158 $this->assertSame( $nextConds, $conds );
1159 return new FakeResultWrapper( $fakeRows );
1160 } );
1161 $replaceArgs = [
1162 [
1163 'watchlist',
1164 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1165 [
1166 [
1167 'wl_user' => '1',
1168 'wl_namespace' => $newTarget->getNamespace(),
1169 'wl_title' => $newTarget->getDBkey(),
1170 'wl_notificationtimestamp' => '20151212010101',
1171 ],
1172 ],
1173 ],
1174 [
1175 'watchlist',
1176 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
1177 [
1178 [
1179 'wl_user' => '1',
1180 'wl_namespace' => $newTarget->getNamespace() + 1,
1181 'wl_title' => $newTarget->getDBkey(),
1182 'wl_notificationtimestamp' => '20151212010101',
1183 ],
1184 ],
1185 ],
1186 ];
1187 $mockDb->expects( $this->exactly( 2 ) )
1188 ->method( 'replace' )
1189 ->willReturnCallback( function ( $table, $uniqueKeys, $rows ) use ( &$replaceArgs ): void {
1190 [ $nextTable, $nextUniqueKeys, $nextRows ] = array_shift( $replaceArgs );
1191 $this->assertSame( $nextTable, $table );
1192 $this->assertSame( $nextUniqueKeys, $uniqueKeys );
1193 $this->assertSame( $nextRows, $rows );
1194 } );
1195 $mockDb
1196 ->method( 'newSelectQueryBuilder' )
1197 ->willReturn( new SelectQueryBuilder( $mockDb ) );
1198
1199 $mockCache = $this->getMockCache();
1200 $mockCache->expects( $this->never() )->method( 'get' );
1201 $mockCache->expects( $this->never() )->method( 'delete' );
1202
1203 $store = $this->newWatchedItemStore( [
1204 'db' => $mockDb,
1205 'cache' => $mockCache,
1206 'expiryEnabled' => false,
1207 ] );
1208
1209 $store->duplicateAllAssociatedEntries(
1210 $oldTarget,
1211 $newTarget
1212 );
1213 }
1214
1215 /**
1216 * @dataProvider provideTestPageFactory
1217 */
1218 public function testAddWatch_nonAnonymousUser( $testPageFactory ) {
1219 $mockDb = $this->getMockDb();
1220 $mockDb->expects( $this->once() )
1221 ->method( 'insert' )
1222 ->with(
1223 'watchlist',
1224 [
1225 [
1226 'wl_user' => 1,
1227 'wl_namespace' => 0,
1228 'wl_title' => 'Some_Page',
1229 'wl_notificationtimestamp' => null,
1230 ]
1231 ]
1232 );
1233
1234 $mockCache = $this->getMockCache();
1235 $mockCache->expects( $this->once() )
1236 ->method( 'delete' )
1237 ->with( '0:Some_Page:1' );
1238 $mockDb->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
1239 $mockDb->method( 'timestamp' )
1240 ->willReturnArgument( 0 );
1241
1242 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1243
1244 $store->addWatch(
1245 new UserIdentityValue( 1, 'MockUser' ),
1246 $testPageFactory( 100, 0, 'Some_Page' )
1247 );
1248
1249 $this->assertSame(
1250 1,
1251 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1252 );
1253 $this->assertSame(
1254 0,
1255 $this->statsFactory->getCounter( 'WatchedItemStore_cache_total' )->getSampleCount()
1256 );
1257 }
1258
1259 /**
1260 * @dataProvider provideTestPageFactory
1261 */
1262 public function testAddWatch_anonymousUser( $testPageFactory ) {
1263 $mockDb = $this->getMockDb();
1264 $mockDb->expects( $this->never() )
1265 ->method( 'insert' );
1266
1267 $mockCache = $this->getMockCache();
1268 $mockCache->expects( $this->never() )
1269 ->method( 'delete' );
1270
1271 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1272
1273 $store->addWatch(
1274 new UserIdentityValue( 0, 'AnonUser' ),
1275 $testPageFactory( 100, 0, 'Some_Page' )
1276 );
1277 $this->assertSame(
1278 0,
1279 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1280 );
1281 $this->assertSame(
1282 0,
1283 $this->statsFactory->getCounter( 'WatchedItemStore_cache_total' )->getSampleCount()
1284 );
1285 }
1286
1287 /**
1288 * @dataProvider provideTestPageFactory
1289 */
1290 public function testAddWatchBatchForUser_readOnlyDBReturnsFalse( $testPageFactory ) {
1291 $store = $this->newWatchedItemStore(
1292 [ 'readOnlyMode' => $this->getDummyReadOnlyMode( true ) ]
1293 );
1294
1295 $this->assertFalse(
1296 $store->addWatchBatchForUser(
1297 new UserIdentityValue( 1, 'MockUser' ),
1298 [ $testPageFactory( 100, 0, 'Some_Page' ), $testPageFactory( 101, 1, 'Some_Page' ) ]
1299 )
1300 );
1301 }
1302
1303 /**
1304 * @dataProvider provideTestPageFactory
1305 */
1306 public function testAddWatchBatchForUser_nonAnonymousUser( $testPageFactory ) {
1307 $mockDb = $this->getMockDb();
1308 $mockDb->expects( $this->once() )
1309 ->method( 'insert' )
1310 ->with(
1311 'watchlist',
1312 [
1313 [
1314 'wl_user' => 1,
1315 'wl_namespace' => 0,
1316 'wl_title' => 'Some_Page',
1317 'wl_notificationtimestamp' => null,
1318 ],
1319 [
1320 'wl_user' => 1,
1321 'wl_namespace' => 1,
1322 'wl_title' => 'Some_Page',
1323 'wl_notificationtimestamp' => null,
1324 ]
1325 ]
1326 );
1327
1328 $mockDb->expects( $this->once() )
1329 ->method( 'affectedRows' )
1330 ->willReturn( 2 );
1331
1332 $cacheKeys = [ '0:Some_Page:1', '1:Some_Page:1' ];
1333 $mockCache = $this->getMockCache();
1334 $mockCache->expects( $this->exactly( 2 ) )
1335 ->method( 'delete' )
1336 ->with( $this->callback( static function ( $key ) use ( &$cacheKeys ) {
1337 $nextKey = array_shift( $cacheKeys );
1338 return $nextKey === $key;
1339 } ) );
1340
1341 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1342
1343 $mockUser = new UserIdentityValue( 1, 'MockUser' );
1344
1345 $this->assertTrue(
1346 $store->addWatchBatchForUser(
1347 $mockUser,
1348 [ $testPageFactory( 100, 0, 'Some_Page' ), $testPageFactory( 101, 1, 'Some_Page' ) ]
1349 )
1350 );
1351 $this->assertSame(
1352 2,
1353 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1354 );
1355 }
1356
1357 /**
1358 * @dataProvider provideTestPageFactory
1359 */
1360 public function testAddWatchBatchForUser_anonymousUsersAreSkipped( $testPageFactory ) {
1361 $mockDb = $this->getMockDb();
1362 $mockDb->expects( $this->never() )
1363 ->method( 'insert' );
1364
1365 $mockCache = $this->getMockCache();
1366 $mockCache->expects( $this->never() )
1367 ->method( 'delete' );
1368
1369 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1370
1371 $this->assertFalse(
1372 $store->addWatchBatchForUser(
1373 new UserIdentityValue( 0, 'AnonUser' ),
1374 [ $testPageFactory( 100, 0, 'Other_Page' ) ]
1375 )
1376 );
1377 $this->assertSame(
1378 0,
1379 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1380 );
1381 }
1382
1383 public function testAddWatchBatchReturnsTrue_whenGivenEmptyList() {
1384 $user = new UserIdentityValue( 1, 'MockUser' );
1385 $mockDb = $this->getMockDb();
1386 $mockDb->expects( $this->never() )
1387 ->method( 'insert' );
1388
1389 $mockCache = $this->getMockCache();
1390 $mockCache->expects( $this->never() )
1391 ->method( 'delete' );
1392
1393 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1394
1395 $this->assertTrue(
1396 $store->addWatchBatchForUser( $user, [] )
1397 );
1398 $this->assertSame(
1399 0,
1400 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1401 );
1402 }
1403
1404 /**
1405 * @dataProvider provideTestPageFactory
1406 */
1407 public function testLoadWatchedItem_existingItem( $testPageFactory ) {
1408 $mockDb = $this->getMockDb();
1409 $mockDb->expects( $this->once() )
1410 ->method( 'timestamp' )
1411 ->willReturn( '20200101000000' );
1412 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
1413 $mockDb->expects( $this->exactly( 2 ) )
1414 ->method( 'makeList' )
1415 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
1416 $selectArgs = [
1417 [
1418 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1419 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
1420 [
1421 'wl_user' => 1,
1422 $makeListSql,
1423 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
1424 ],
1425 new FakeResultWrapper( [
1426 (object)[
1427 'wl_namespace' => 0,
1428 'wl_title' => 'SomeDbKey',
1429 'wl_notificationtimestamp' => '20151212010101',
1430 'we_expiry' => '20300101000000'
1431 ]
1432 ] ),
1433 ],
1434 ];
1435 $mockDb->expects( $this->once() )
1436 ->method( 'select' )
1437 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
1438 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
1439 $this->assertSame( $nextTable, $table );
1440 $this->assertSame( $nextVars, $vars );
1441 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
1442 return $returnValue;
1443 } );
1444
1445 $mockCache = $this->getMockCache();
1446 $mockCache->expects( $this->once() )
1447 ->method( 'set' )
1448 ->with(
1449 '0:SomeDbKey:1'
1450 );
1451
1452 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1453
1454 $watchedItem = $store->loadWatchedItem(
1455 new UserIdentityValue( 1, 'MockUser' ),
1456 $testPageFactory( 100, 0, 'SomeDbKey' )
1457 );
1458 $this->assertInstanceOf( WatchedItem::class, $watchedItem );
1459 $this->assertSame( 1, $watchedItem->getUserIdentity()->getId() );
1460 $this->assertEquals( 'SomeDbKey', $watchedItem->getTarget()->getDBkey() );
1461 $this->assertSame( '20300101000000', $watchedItem->getExpiry() );
1462 $this->assertSame( 0, $watchedItem->getTarget()->getNamespace() );
1463
1464 $this->assertSame(
1465 1,
1466 $this->statsFactory->getCounter( 'WatchedItemStore_cache_total' )->getSampleCount()
1467 );
1468 }
1469
1470 /**
1471 * @dataProvider provideTestPageFactory
1472 */
1473 public function testLoadWatchedItem_noItem( $testPageFactory ) {
1474 $mockDb = $this->getMockDb();
1475 $mockDb->expects( $this->once() )
1476 ->method( 'timestamp' )
1477 ->willReturn( '20200101000000' );
1478 $mockDb->expects( $this->once() )
1479 ->method( 'select' )
1480 ->willReturn( new FakeResultWrapper( [] ) );
1481
1482 $mockCache = $this->getMockCache();
1483 $mockCache->expects( $this->never() )->method( 'get' );
1484 $mockCache->expects( $this->never() )->method( 'delete' );
1485
1486 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1487
1488 $this->assertFalse(
1489 $store->loadWatchedItem(
1490 new UserIdentityValue( 1, 'MockUser' ),
1491 $testPageFactory( 100, 0, 'SomeDbKey' )
1492 )
1493 );
1494 $this->assertSame(
1495 0,
1496 $this->statsFactory->getCounter( 'WatchedItemStore_cache_total' )->getSampleCount()
1497 );
1498 }
1499
1500 /**
1501 * @dataProvider provideTestPageFactory
1502 */
1503 public function testLoadWatchedItem_anonymousUser( $testPageFactory ) {
1504 $mockDb = $this->getMockDb();
1505 $mockDb->expects( $this->never() )
1506 ->method( 'select' );
1507
1508 $mockCache = $this->getMockCache();
1509 $mockCache->expects( $this->never() )->method( 'get' );
1510 $mockCache->expects( $this->never() )->method( 'delete' );
1511
1512 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1513
1514 $this->assertFalse(
1515 $store->loadWatchedItem(
1516 new UserIdentityValue( 0, 'AnonUser' ),
1517 $testPageFactory( 100, 0, 'SomeDbKey' )
1518 )
1519 );
1520 $this->assertSame(
1521 0,
1522 $this->statsFactory->getCounter( 'WatchedItemStore_cache_total' )->getSampleCount()
1523 );
1524 }
1525
1526 /**
1527 * @dataProvider provideTestPageFactory
1528 */
1529 public function testRemoveWatch_existingItem( $testPageFactory ) {
1530 $mockDb = $this->getMockDb();
1531 $mockDb->expects( $this->once() )
1532 ->method( 'selectFieldValues' )
1533 ->willReturn( [ 1, 2 ] );
1534 $deleteArgs = [
1535 [
1536 'watchlist',
1537 [ 'wl_id' => [ 1, 2 ] ]
1538 ],
1539 [
1540 'watchlist_expiry',
1541 [ 'we_item' => [ 1, 2 ] ]
1542 ]
1543 ];
1544 $mockDb->expects( $this->exactly( 2 ) )
1545 ->method( 'delete' )
1546 ->willReturnCallback( function ( $table, $conds ) use ( &$deleteArgs ): void {
1547 [ $nextTable, $nextConds ] = array_shift( $deleteArgs );
1548 $this->assertSame( $nextTable, $table );
1549 $this->assertSame( $nextConds, $conds );
1550 } );
1551 $mockDb->expects( $this->exactly( 2 ) )
1552 ->method( 'affectedRows' )
1553 ->willReturn( 2 );
1554
1555 $mockCache = $this->getMockCache();
1556 $mockCache->expects( $this->never() )->method( 'get' );
1557 $mockCache->expects( $this->once() )
1558 ->method( 'delete' )
1559 ->with( '0:SomeDbKey:1' );
1560
1561 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1562
1563 $this->assertTrue(
1564 $store->removeWatch(
1565 new UserIdentityValue( 1, 'MockUser' ),
1566 $testPageFactory( 100, 0, 'SomeDbKey' )
1567 )
1568 );
1569 $this->assertSame(
1570 1,
1571 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1572 );
1573 }
1574
1575 /**
1576 * @dataProvider provideTestPageFactory
1577 */
1578 public function testRemoveWatch_noItem( $testPageFactory ) {
1579 $mockDb = $this->getMockDb();
1580 $mockDb->expects( $this->once() )
1581 ->method( 'selectFieldValues' )
1582 ->willReturn( [] );
1583 $mockDb->expects( $this->never() )
1584 ->method( 'delete' );
1585 $mockDb->expects( $this->never() )
1586 ->method( 'affectedRows' );
1587
1588 $mockCache = $this->getMockCache();
1589 $mockCache->expects( $this->never() )->method( 'get' );
1590 $mockCache->expects( $this->once() )
1591 ->method( 'delete' )
1592 ->with( '0:SomeDbKey:1' );
1593
1594 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1595
1596 $this->assertFalse(
1597 $store->removeWatch(
1598 new UserIdentityValue( 1, 'MockUser' ),
1599 $testPageFactory( 100, 0, 'SomeDbKey' )
1600 )
1601 );
1602 $this->assertSame(
1603 1,
1604 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1605 );
1606 }
1607
1608 /**
1609 * @dataProvider provideTestPageFactory
1610 */
1611 public function testRemoveWatch_anonymousUser( $testPageFactory ) {
1612 $mockDb = $this->getMockDb();
1613 $mockDb->expects( $this->never() )
1614 ->method( 'delete' );
1615
1616 $mockCache = $this->getMockCache();
1617 $mockCache->expects( $this->never() )->method( 'get' );
1618 $mockCache->expects( $this->never() )
1619 ->method( 'delete' );
1620
1621 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1622
1623 $this->assertFalse(
1624 $store->removeWatch(
1625 new UserIdentityValue( 0, 'AnonUser' ),
1626 $testPageFactory( 100, 0, 'SomeDbKey' )
1627 )
1628 );
1629 $this->assertSame(
1630 0,
1631 $this->statsFactory->getCounter( 'WatchedItemStore_uncache_total' )->getSampleCount()
1632 );
1633 }
1634
1635 /**
1636 * @dataProvider provideTestPageFactory
1637 */
1638 public function testGetWatchedItem_existingItem( $testPageFactory ) {
1639 $mockDb = $this->getMockDb();
1640 $mockDb->expects( $this->once() )
1641 ->method( 'timestamp' )
1642 ->willReturn( '20200101000000' );
1643 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
1644 $mockDb->expects( $this->exactly( 2 ) )
1645 ->method( 'makeList' )
1646 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
1647 $selectArgs = [
1648 [
1649 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1650 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
1651 [
1652 'wl_user' => 1,
1653 $makeListSql,
1654 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
1655 ],
1656 new FakeResultWrapper( [
1657 (object)[
1658 'wl_namespace' => 0,
1659 'wl_title' => 'SomeDbKey',
1660 'wl_notificationtimestamp' => '20151212010101',
1661 'we_expiry' => '20300101000000'
1662 ]
1663 ] ),
1664 ],
1665 ];
1666 $mockDb->expects( $this->once() )
1667 ->method( 'select' )
1668 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
1669 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
1670 $this->assertSame( $nextTable, $table );
1671 $this->assertSame( $nextVars, $vars );
1672 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
1673 return $returnValue;
1674 } );
1675
1676 $mockCache = $this->getMockCache();
1677 $mockCache->expects( $this->never() )->method( 'delete' );
1678 $mockCache->expects( $this->once() )
1679 ->method( 'get' )
1680 ->with(
1681 '0:SomeDbKey:1'
1682 )
1683 ->willReturn( null );
1684 $mockCache->expects( $this->once() )
1685 ->method( 'set' )
1686 ->with(
1687 '0:SomeDbKey:1'
1688 );
1689
1690 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1691
1692 $watchedItem = $store->getWatchedItem(
1693 new UserIdentityValue( 1, 'MockUser' ),
1694 $testPageFactory( 100, 0, 'SomeDbKey' )
1695 );
1696 $this->assertInstanceOf( WatchedItem::class, $watchedItem );
1697 $this->assertSame( 1, $watchedItem->getUserIdentity()->getId() );
1698 $this->assertEquals( 'SomeDbKey', $watchedItem->getTarget()->getDBkey() );
1699 $this->assertSame( '20300101000000', $watchedItem->getExpiry() );
1700 $this->assertSame( 0, $watchedItem->getTarget()->getNamespace() );
1701 }
1702
1703 /**
1704 * @dataProvider provideTestPageFactory
1705 */
1706 public function testGetWatchedItem_cachedItem( $testPageFactory ) {
1707 $mockDb = $this->getMockDb();
1708 $mockDb->expects( $this->never() )
1709 ->method( 'selectRow' );
1710
1711 $mockUser = new UserIdentityValue( 1, 'MockUser' );
1712 $linkTarget = $testPageFactory( 100, 0, 'SomeDbKey' );
1713 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1714
1715 $mockCache = $this->getMockCache();
1716 $mockCache->expects( $this->never() )->method( 'delete' );
1717 $mockCache->expects( $this->never() )->method( 'set' );
1718 $mockCache->expects( $this->once() )
1719 ->method( 'get' )
1720 ->with(
1721 '0:SomeDbKey:1'
1722 )
1723 ->willReturn( $cachedItem );
1724
1725 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1726
1727 $this->assertEquals(
1728 $cachedItem,
1729 $store->getWatchedItem(
1730 $mockUser,
1731 $linkTarget
1732 )
1733 );
1734 $this->assertSame(
1735 1,
1736 $this->statsFactory->getCounter( 'WatchedItemStore_getWatchedItem_accesses_total' )
1737 ->getSampleCount()
1738 );
1739 }
1740
1741 /**
1742 * @dataProvider provideTestPageFactory
1743 */
1744 public function testGetWatchedItem_noItem( $testPageFactory ) {
1745 $mockDb = $this->getMockDb();
1746 $mockDb->expects( $this->once() )
1747 ->method( 'timestamp' )
1748 ->willReturn( '20200101000000' );
1749 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
1750 $mockDb->expects( $this->exactly( 2 ) )
1751 ->method( 'makeList' )
1752 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
1753 $selectArgs = [
1754 [
1755 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1756 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
1757 [
1758 'wl_user' => 1,
1759 $makeListSql,
1760 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
1761 ],
1762 new FakeResultWrapper( [] ),
1763 ],
1764 ];
1765 $mockDb->expects( $this->once() )
1766 ->method( 'select' )
1767 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
1768 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
1769 $this->assertSame( $nextTable, $table );
1770 $this->assertSame( $nextVars, $vars );
1771 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
1772 return $returnValue;
1773 } );
1774
1775 $mockCache = $this->getMockCache();
1776 $mockCache->expects( $this->never() )->method( 'set' );
1777 $mockCache->expects( $this->never() )->method( 'delete' );
1778 $mockCache->expects( $this->once() )
1779 ->method( 'get' )
1780 ->with( '0:SomeDbKey:1' )
1781 ->willReturn( false );
1782
1783 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1784
1785 $this->assertFalse(
1786 $store->getWatchedItem(
1787 new UserIdentityValue( 1, 'MockUser' ),
1788 $testPageFactory( 100, 0, 'SomeDbKey' )
1789 )
1790 );
1791
1792 $this->assertSame(
1793 1,
1794 $this->statsFactory->getCounter( 'WatchedItemStore_getWatchedItem_accesses_total' )
1795 ->getSampleCount()
1796 );
1797 }
1798
1799 /**
1800 * @dataProvider provideTestPageFactory
1801 */
1802 public function testGetWatchedItem_anonymousUser( $testPageFactory ) {
1803 $mockDb = $this->getMockDb();
1804 $mockDb->expects( $this->never() )
1805 ->method( 'selectRow' );
1806
1807 $mockCache = $this->getMockCache();
1808 $mockCache->expects( $this->never() )->method( 'set' );
1809 $mockCache->expects( $this->never() )->method( 'get' );
1810 $mockCache->expects( $this->never() )->method( 'delete' );
1811
1812 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1813
1814 $this->assertFalse(
1815 $store->getWatchedItem(
1816 new UserIdentityValue( 0, 'AnonUser' ),
1817 $testPageFactory( 100, 0, 'SomeDbKey' )
1818 )
1819 );
1820 $this->assertSame(
1821 0,
1822 $this->statsFactory->getCounter( 'WatchedItemStore_getWatchedItem_accesses_total' )
1823 ->getSampleCount()
1824 );
1825 }
1826
1827 public function testGetWatchedItemsForUser() {
1828 $mockDb = $this->getMockDb();
1829 $mockDb->expects( $this->once() )
1830 ->method( 'timestamp' )
1831 ->willReturn( '20200101000000' );
1832 $selectArgs = [
1833 [
1834 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1835 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
1836 [ 'wl_user' => 1, '(we_expiry IS NULL OR we_expiry > \'20200101000000\')' ],
1837 new FakeResultWrapper( [
1838 (object)[
1839 'wl_namespace' => 0,
1840 'wl_title' => 'Foo1',
1841 'wl_notificationtimestamp' => '20151212010101',
1842 'we_expiry' => '20300101000000'
1843 ],
1844 (object)[
1845 'wl_namespace' => 1,
1846 'wl_title' => 'Foo2',
1847 'wl_notificationtimestamp' => null,
1848 ],
1849 ] ),
1850 ],
1851 ];
1852 $mockDb->expects( $this->once() )
1853 ->method( 'select' )
1854 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
1855 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
1856 $this->assertSame( $nextTable, $table );
1857 $this->assertSame( $nextVars, $vars );
1858 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
1859 return $returnValue;
1860 } );
1861
1862 $mockCache = $this->getMockCache();
1863 $mockCache->expects( $this->never() )->method( 'delete' );
1864 $mockCache->expects( $this->never() )->method( 'get' );
1865 $mockCache->expects( $this->never() )->method( 'set' );
1866
1867 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1868 $user = new UserIdentityValue( 1, 'MockUser' );
1869
1870 $watchedItems = $store->getWatchedItemsForUser( $user );
1871
1872 $this->assertIsArray( $watchedItems );
1873 $this->assertCount( 2, $watchedItems );
1874 foreach ( $watchedItems as $watchedItem ) {
1875 $this->assertInstanceOf( WatchedItem::class, $watchedItem );
1876 }
1877 $this->assertEquals(
1878 new WatchedItem(
1879 $user,
1880 new TitleValue( 0, 'Foo1' ),
1881 '20151212010101',
1882 '20300101000000'
1883 ),
1884 $watchedItems[0]
1885 );
1886 $this->assertEquals(
1887 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1888 $watchedItems[1]
1889 );
1890 }
1891
1892 public static function provideDbTypes() {
1893 return [
1894 [ false, DB_REPLICA ],
1895 [ true, DB_PRIMARY ],
1896 ];
1897 }
1898
1899 /**
1900 * @dataProvider provideDbTypes
1901 */
1902 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1903 $mockDb = $this->getMockDb();
1904 $mockCache = $this->getMockCache();
1905 $mockLoadBalancer = $this->getMockLBFactory( $mockDb, $dbType );
1906 $user = new UserIdentityValue( 1, 'MockUser' );
1907
1908 $mockDb->expects( $this->once() )
1909 ->method( 'timestamp' )
1910 ->willReturn( '20200101000000' );
1911 $selectArgs = [
1912 [
1913 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1914 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
1915 [ 'wl_user' => 1, '(we_expiry IS NULL OR we_expiry > \'20200101000000\')' ],
1916 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ],
1917 new FakeResultWrapper( [] ),
1918 ],
1919 ];
1920 $mockDb->expects( $this->once() )
1921 ->method( 'select' )
1922 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
1923 [ $nextTable, $nextVars, $nextConds, $nextOptions, $returnValue ] = array_shift( $selectArgs );
1924 $this->assertSame( $nextTable, $table );
1925 $this->assertSame( $nextVars, $vars );
1926 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
1927 $this->assertSame( $nextOptions, $options );
1928 return $returnValue;
1929 } );
1930
1931 $store = $this->newWatchedItemStore(
1932 [ 'lbFactory' => $mockLoadBalancer, 'cache' => $mockCache ] );
1933
1934 $watchedItems = $store->getWatchedItemsForUser(
1935 $user,
1936 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore::SORT_ASC ]
1937 );
1938 $this->assertEquals( [], $watchedItems );
1939 }
1940
1941 public function testGetWatchedItemsForUser_sortByExpiry() {
1942 $mockDb = $this->getMockDb();
1943 $mockDb->expects( $this->once() )
1944 ->method( 'timestamp' )
1945 ->willReturn( '20200101000000' );
1946 $selectArgs = [
1947 [
1948 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
1949 [
1950 'wl_namespace',
1951 'wl_title',
1952 'wl_notificationtimestamp',
1953 'wl_has_expiry' => null,
1954 'we_expiry',
1955 ],
1956 [ 'wl_user' => 1, '(we_expiry IS NULL OR we_expiry > \'20200101000000\')' ],
1957 new FakeResultWrapper( [
1958 (object)[
1959 'wl_namespace' => 0,
1960 'wl_title' => 'Foo1',
1961 'wl_notificationtimestamp' => '20151212010101',
1962 'we_expiry' => '20300101000000'
1963 ],
1964 (object)[
1965 'wl_namespace' => 0,
1966 'wl_title' => 'Foo2',
1967 'wl_notificationtimestamp' => '20151212010101',
1968 'we_expiry' => '20300701000000'
1969 ],
1970 (object)[
1971 'wl_namespace' => 1,
1972 'wl_title' => 'Foo3',
1973 'wl_notificationtimestamp' => null,
1974 ],
1975 ] ),
1976 ],
1977 ];
1978 $mockDb->expects( $this->once() )
1979 ->method( 'select' )
1980 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
1981 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
1982 $this->assertSame( $nextTable, $table );
1983 $this->assertSame( $nextVars, $vars );
1984 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
1985 return $returnValue;
1986 } );
1987
1988 $mockCache = $this->getMockCache();
1989 $mockCache->expects( $this->never() )->method( 'delete' );
1990 $mockCache->expects( $this->never() )->method( 'get' );
1991 $mockCache->expects( $this->never() )->method( 'set' );
1992
1993 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1994 $user = new UserIdentityValue( 1, 'MockUser' );
1995
1996 $watchedItems = $store->getWatchedItemsForUser(
1997 $user,
1998 [ 'sortByExpiry' => true, 'sort' => WatchedItemStore::SORT_ASC ]
1999 );
2000
2001 $this->assertIsArray( $watchedItems );
2002 $this->assertCount( 3, $watchedItems );
2003 foreach ( $watchedItems as $watchedItem ) {
2004 $this->assertInstanceOf( WatchedItem::class, $watchedItem );
2005 }
2006 $this->assertEquals(
2007 new WatchedItem(
2008 $user,
2009 new TitleValue( 0, 'Foo1' ),
2010 '20151212010101',
2011 '20300101000000'
2012 ),
2013 $watchedItems[0]
2014 );
2015 $this->assertEquals(
2016 new WatchedItem( $user, new TitleValue( 1, 'Foo3' ), null ),
2017 $watchedItems[2]
2018 );
2019 }
2020
2021 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
2022 $store = $this->newWatchedItemStore();
2023
2024 $this->expectException( InvalidArgumentException::class );
2025 $store->getWatchedItemsForUser(
2026 new UserIdentityValue( 1, 'MockUser' ),
2027 [ 'sort' => 'foo' ]
2028 );
2029 }
2030
2031 /**
2032 * @dataProvider provideTestPageFactory
2033 */
2034 public function testIsWatchedItem_existingItem( $testPageFactory ) {
2035 $mockDb = $this->getMockDb();
2036 $mockDb->expects( $this->once() )
2037 ->method( 'timestamp' )
2038 ->willReturn( '20200101000000' );
2039 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2040 $mockDb->expects( $this->exactly( 2 ) )
2041 ->method( 'makeList' )
2042 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2043 $selectArgs = [
2044 [
2045 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2046 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2047 [
2048 'wl_user' => 1,
2049 $makeListSql,
2050 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2051 ],
2052 new FakeResultWrapper( [
2053 (object)[
2054 'wl_namespace' => 0,
2055 'wl_title' => 'SomeDbKey',
2056 'wl_notificationtimestamp' => '20151212010101',
2057 ]
2058 ] ),
2059 ],
2060 ];
2061 $mockDb->expects( $this->once() )
2062 ->method( 'select' )
2063 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2064 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2065 $this->assertSame( $nextTable, $table );
2066 $this->assertSame( $nextVars, $vars );
2067 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2068 return $returnValue;
2069 } );
2070
2071 $mockCache = $this->getMockCache();
2072 $mockCache->expects( $this->never() )->method( 'delete' );
2073 $mockCache->expects( $this->once() )
2074 ->method( 'get' )
2075 ->with( '0:SomeDbKey:1' )
2076 ->willReturn( false );
2077 $mockCache->expects( $this->once() )
2078 ->method( 'set' )
2079 ->with(
2080 '0:SomeDbKey:1'
2081 );
2082
2083 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2084
2085 $this->assertTrue(
2086 $store->isWatched(
2087 new UserIdentityValue( 1, 'MockUser' ),
2088 $testPageFactory( 100, 0, 'SomeDbKey' )
2089 )
2090 );
2091 }
2092
2093 /**
2094 * @dataProvider provideTestPageFactory
2095 */
2096 public function testIsWatchedItem_noItem( $testPageFactory ) {
2097 $mockDb = $this->getMockDb();
2098 $mockDb->expects( $this->once() )
2099 ->method( 'timestamp' )
2100 ->willReturn( '20200101000000' );
2101 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2102 $mockDb->expects( $this->exactly( 2 ) )
2103 ->method( 'makeList' )
2104 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2105 $selectArgs = [
2106 [
2107 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2108 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2109 [
2110 'wl_user' => 1,
2111 $makeListSql,
2112 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2113 ],
2114 new FakeResultWrapper( [] ),
2115 ],
2116 ];
2117 $mockDb->expects( $this->once() )
2118 ->method( 'select' )
2119 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2120 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2121 $this->assertSame( $nextTable, $table );
2122 $this->assertSame( $nextVars, $vars );
2123 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2124 return $returnValue;
2125 } );
2126
2127 $mockCache = $this->getMockCache();
2128 $mockCache->expects( $this->never() )->method( 'set' );
2129 $mockCache->expects( $this->never() )->method( 'delete' );
2130 $mockCache->expects( $this->once() )
2131 ->method( 'get' )
2132 ->with( '0:SomeDbKey:1' )
2133 ->willReturn( false );
2134
2135 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2136
2137 $this->assertFalse(
2138 $store->isWatched(
2139 new UserIdentityValue( 1, 'MockUser' ),
2140 $testPageFactory( 100, 0, 'SomeDbKey' )
2141 )
2142 );
2143 }
2144
2145 /**
2146 * @dataProvider provideTestPageFactory
2147 */
2148 public function testIsWatchedItem_anonymousUser( $testPageFactory ) {
2149 $mockDb = $this->getMockDb();
2150 $mockDb->expects( $this->never() )
2151 ->method( 'selectRow' );
2152
2153 $mockCache = $this->getMockCache();
2154 $mockCache->expects( $this->never() )->method( 'set' );
2155 $mockCache->expects( $this->never() )->method( 'get' );
2156 $mockCache->expects( $this->never() )->method( 'delete' );
2157
2158 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2159
2160 $this->assertFalse(
2161 $store->isWatched(
2162 new UserIdentityValue( 0, 'AnonUser' ),
2163 $testPageFactory( 100, 0, 'SomeDbKey' )
2164 )
2165 );
2166 }
2167
2168 /**
2169 * @dataProvider provideTestPageFactory
2170 */
2171 public function testGetNotificationTimestampsBatch( $testPageFactory ) {
2172 $targets = [
2173 $testPageFactory( 100, 0, 'SomeDbKey' ),
2174 $testPageFactory( 101, 1, 'AnotherDbKey' ),
2175 ];
2176
2177 $mockDb = $this->getMockDb();
2178 $dbResult = [
2179 (object)[
2180 'wl_namespace' => '0',
2181 'wl_title' => 'SomeDbKey',
2182 'wl_notificationtimestamp' => '20151212010101',
2183 ],
2184 (object)[
2185 'wl_namespace' => '1',
2186 'wl_title' => 'AnotherDbKey',
2187 'wl_notificationtimestamp' => null,
2188 ],
2189 ];
2190
2191 $mockDb->expects( $this->once() )
2192 ->method( 'makeWhereFrom2d' )
2193 ->with(
2194 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
2195 $this->isType( 'string' ),
2196 $this->isType( 'string' )
2197 )
2198 ->willReturn( 'makeWhereFrom2d return value' );
2199 $mockDb->expects( $this->once() )
2200 ->method( 'select' )
2201 ->with(
2202 [ 'watchlist' ],
2203 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
2204 [
2205 'makeWhereFrom2d return value',
2206 'wl_user' => 1
2207 ],
2208 $this->isType( 'string' )
2209 )
2210 ->willReturn( new FakeResultWrapper( $dbResult ) );
2211
2212 $cacheKeys = [ '0:SomeDbKey:1', '1:AnotherDbKey:1' ];
2213 $mockCache = $this->getMockCache();
2214 $mockCache->expects( $this->exactly( 2 ) )
2215 ->method( 'get' )
2216 ->willReturnCallback( function ( $key ) use ( &$cacheKeys ) {
2217 $nextKey = array_shift( $cacheKeys );
2218 $this->assertSame( $nextKey, $key );
2219 return null;
2220 } );
2221 $mockCache->expects( $this->never() )->method( 'set' );
2222 $mockCache->expects( $this->never() )->method( 'delete' );
2223
2224 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2225
2226 $this->assertEquals(
2227 [
2228 0 => [ 'SomeDbKey' => '20151212010101', ],
2229 1 => [ 'AnotherDbKey' => null, ],
2230 ],
2231 $store->getNotificationTimestampsBatch(
2232 new UserIdentityValue( 1, 'MockUser' ), $targets )
2233 );
2234 }
2235
2236 /**
2237 * @dataProvider provideTestPageFactory
2238 */
2239 public function testGetNotificationTimestampsBatch_notWatchedTarget( $testPageFactory ) {
2240 $targets = [
2241 $testPageFactory( 100, 0, 'OtherDbKey' ),
2242 ];
2243
2244 $mockDb = $this->getMockDb();
2245
2246 $mockDb->expects( $this->once() )
2247 ->method( 'makeWhereFrom2d' )
2248 ->with(
2249 [ [ 'OtherDbKey' => 1 ] ],
2250 $this->isType( 'string' ),
2251 $this->isType( 'string' )
2252 )
2253 ->willReturn( 'makeWhereFrom2d return value' );
2254 $mockDb->expects( $this->once() )
2255 ->method( 'select' )
2256 ->with(
2257 [ 'watchlist' ],
2258 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
2259 [
2260 'makeWhereFrom2d return value',
2261 'wl_user' => 1
2262 ],
2263 $this->isType( 'string' )
2264 )
2265 ->willReturn( new FakeResultWrapper( [] ) );
2266
2267 $mockCache = $this->getMockCache();
2268 $mockCache->expects( $this->once() )
2269 ->method( 'get' )
2270 ->with( '0:OtherDbKey:1' )
2271 ->willReturn( null );
2272 $mockCache->expects( $this->never() )->method( 'set' );
2273 $mockCache->expects( $this->never() )->method( 'delete' );
2274
2275 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2276
2277 $this->assertEquals(
2278 [
2279 0 => [ 'OtherDbKey' => false, ],
2280 ],
2281 $store->getNotificationTimestampsBatch(
2282 new UserIdentityValue( 1, 'MockUser' ), $targets )
2283 );
2284 }
2285
2286 /**
2287 * @dataProvider provideTestPageFactory
2288 */
2289 public function testGetNotificationTimestampsBatch_cachedItem( $testPageFactory ) {
2290 $targets = [
2291 $testPageFactory( 100, 0, 'SomeDbKey' ),
2292 $testPageFactory( 101, 1, 'AnotherDbKey' ),
2293 ];
2294
2295 $user = new UserIdentityValue( 1, 'MockUser' );
2296 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
2297
2298 $mockDb = $this->getMockDb();
2299
2300 $mockDb->expects( $this->once() )
2301 ->method( 'makeWhereFrom2d' )
2302 ->with(
2303 [ 1 => [ 'AnotherDbKey' => 1 ] ],
2304 $this->isType( 'string' ),
2305 $this->isType( 'string' )
2306 )
2307 ->willReturn( 'makeWhereFrom2d return value' );
2308 $mockDb->expects( $this->once() )
2309 ->method( 'select' )
2310 ->with(
2311 [ 'watchlist' ],
2312 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
2313 [
2314 'makeWhereFrom2d return value',
2315 'wl_user' => 1
2316 ],
2317 $this->isType( 'string' )
2318 )
2319 ->willReturn( new FakeResultWrapper( [
2320 (object)[ 'wl_namespace' => '1', 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
2321 ] ) );
2322
2323 $cacheKeys = [
2324 [ '0:SomeDbKey:1', $cachedItem ],
2325 [ '1:AnotherDbKey:1', null ]
2326 ];
2327 $mockCache = $this->getMockCache();
2328 $mockCache->expects( $this->exactly( 2 ) )
2329 ->method( 'get' )
2330 ->willReturnCallback( function ( $key ) use ( &$cacheKeys ) {
2331 [ $nextKey, $returnValue ] = array_shift( $cacheKeys );
2332 $this->assertSame( $nextKey, $key );
2333 return $returnValue;
2334 } );
2335 $mockCache->expects( $this->never() )->method( 'set' );
2336 $mockCache->expects( $this->never() )->method( 'delete' );
2337
2338 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2339
2340 $this->assertEquals(
2341 [
2342 0 => [ 'SomeDbKey' => '20151212010101', ],
2343 1 => [ 'AnotherDbKey' => null, ],
2344 ],
2345 $store->getNotificationTimestampsBatch( $user, $targets )
2346 );
2347 }
2348
2349 /**
2350 * @dataProvider provideTestPageFactory
2351 */
2352 public function testGetNotificationTimestampsBatch_allItemsCached( $testPageFactory ) {
2353 $targets = [
2354 $testPageFactory( 100, 0, 'SomeDbKey' ),
2355 $testPageFactory( 101, 1, 'AnotherDbKey' ),
2356 ];
2357
2358 $user = new UserIdentityValue( 1, 'MockUser' );
2359 $cachedItems = [
2360 new WatchedItem( $user, $targets[0], '20151212010101' ),
2361 new WatchedItem( $user, $targets[1], null ),
2362 ];
2363 $mockDb = $this->createNoOpMock( IDatabase::class );
2364
2365 $cacheKeys = [
2366 [ '0:SomeDbKey:1', $cachedItems[0] ],
2367 [ '1:AnotherDbKey:1', $cachedItems[1] ],
2368 ];
2369 $mockCache = $this->getMockCache();
2370 $mockCache->expects( $this->exactly( 2 ) )
2371 ->method( 'get' )
2372 ->willReturnCallback( function ( $key ) use ( &$cacheKeys ) {
2373 [ $nextKey, $returnValue ] = array_shift( $cacheKeys );
2374 $this->assertSame( $nextKey, $key );
2375 return $returnValue;
2376 } );
2377 $mockCache->expects( $this->never() )->method( 'set' );
2378 $mockCache->expects( $this->never() )->method( 'delete' );
2379
2380 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2381
2382 $this->assertEquals(
2383 [
2384 0 => [ 'SomeDbKey' => '20151212010101', ],
2385 1 => [ 'AnotherDbKey' => null, ],
2386 ],
2387 $store->getNotificationTimestampsBatch( $user, $targets )
2388 );
2389 }
2390
2391 /**
2392 * @dataProvider provideTestPageFactory
2393 */
2394 public function testGetNotificationTimestampsBatch_anonymousUser( $testPageFactory ) {
2395 $targets = [
2396 $testPageFactory( 100, 0, 'SomeDbKey' ),
2397 $testPageFactory( 101, 1, 'AnotherDbKey' ),
2398 ];
2399
2400 $mockDb = $this->createNoOpMock( IDatabase::class );
2401
2402 $mockCache = $this->createNoOpMock( HashBagOStuff::class );
2403
2404 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2405
2406 $this->assertEquals(
2407 [
2408 0 => [ 'SomeDbKey' => false, ],
2409 1 => [ 'AnotherDbKey' => false, ],
2410 ],
2411 $store->getNotificationTimestampsBatch(
2412 new UserIdentityValue( 0, 'AnonUser' ), $targets )
2413 );
2414 }
2415
2416 /**
2417 * @dataProvider provideTestPageFactory
2418 */
2419 public function testResetNotificationTimestamp_anonymousUser( $testPageFactory ) {
2420 $mockDb = $this->getMockDb();
2421 $mockDb->expects( $this->never() )
2422 ->method( 'selectRow' );
2423
2424 $mockCache = $this->getMockCache();
2425 $mockCache->expects( $this->never() )->method( 'get' );
2426 $mockCache->expects( $this->never() )->method( 'set' );
2427 $mockCache->expects( $this->never() )->method( 'delete' );
2428
2429 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2430
2431 $this->assertFalse(
2432 $store->resetNotificationTimestamp(
2433 new UserIdentityValue( 0, 'AnonUser' ),
2434 $testPageFactory( 100, 0, 'SomeDbKey' )
2435 )
2436 );
2437 }
2438
2439 /**
2440 * @dataProvider provideTestPageFactory
2441 */
2442 public function testResetNotificationTimestamp_noItem( $testPageFactory ) {
2443 $mockDb = $this->getMockDb();
2444 $mockDb->expects( $this->once() )
2445 ->method( 'timestamp' )
2446 ->willReturn( '20200101000000' );
2447 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2448 $mockDb->expects( $this->exactly( 2 ) )
2449 ->method( 'makeList' )
2450 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2451 $selectArgs = [
2452 [
2453 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2454 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2455 [
2456 'wl_user' => 1,
2457 $makeListSql,
2458 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2459 ],
2460 new FakeResultWrapper( [] ),
2461 ],
2462 ];
2463 $mockDb->expects( $this->once() )
2464 ->method( 'select' )
2465 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2466 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2467 $this->assertSame( $nextTable, $table );
2468 $this->assertSame( $nextVars, $vars );
2469 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2470 return $returnValue;
2471 } );
2472
2473 $mockCache = $this->getMockCache();
2474 $mockCache->expects( $this->once() )->method( 'get' );
2475 $mockCache->expects( $this->never() )->method( 'set' );
2476 $mockCache->expects( $this->never() )->method( 'delete' );
2477
2478 $user = new UserIdentityValue( 1, 'MockUser' );
2479
2480 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
2481
2482 $store = $this->newWatchedItemStore( [
2483 'db' => $mockDb,
2484 'cache' => $mockCache,
2485 ] );
2486
2487 $this->assertFalse(
2488 $store->resetNotificationTimestamp(
2489 $user,
2490 $title
2491 )
2492 );
2493 }
2494
2495 /**
2496 * @dataProvider provideTestPageFactory
2497 */
2498 public function testResetNotificationTimestamp_item( $testPageFactory ) {
2499 $user = new UserIdentityValue( 1, 'MockUser' );
2500 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
2501
2502 $mockDb = $this->getMockDb();
2503 $mockDb->expects( $this->once() )
2504 ->method( 'timestamp' )
2505 ->willReturn( '20200101000000' );
2506 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2507 $mockDb->expects( $this->exactly( 2 ) )
2508 ->method( 'makeList' )
2509 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2510 $selectArgs = [
2511 [
2512 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2513 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2514 [
2515 'wl_user' => 1,
2516 $makeListSql,
2517 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2518 ],
2519 new FakeResultWrapper( [
2520 (object)[
2521 'wl_namespace' => 0,
2522 'wl_title' => 'SomeDbKey',
2523 'wl_notificationtimestamp' => '20151212010101',
2524 ]
2525 ] ),
2526 ],
2527 ];
2528 $mockDb->expects( $this->once() )
2529 ->method( 'select' )
2530 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2531 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2532 $this->assertSame( $nextTable, $table );
2533 $this->assertSame( $nextVars, $vars );
2534 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2535 return $returnValue;
2536 } );
2537
2538 $mockCache = $this->getMockCache();
2539 $mockCache->expects( $this->once() )->method( 'get' );
2540 $mockCache->expects( $this->once() )
2541 ->method( 'set' )
2542 ->with(
2543 '0:SomeDbKey:1',
2544 $this->isInstanceOf( WatchedItem::class )
2545 );
2546 $mockCache->expects( $this->once() )
2547 ->method( 'delete' )
2548 ->with( '0:SomeDbKey:1' );
2549
2550 $mockQueueGroup = $this->getMockJobQueueGroup( false );
2551 $mockQueueGroup->expects( $this->once() )
2552 ->method( 'lazyPush' )
2553 ->willReturnCallback( static function ( ActivityUpdateJob $job ) {
2554 // don't run
2555 } );
2556
2557 // We don't care if these methods actually do anything here
2558 $mockRevisionLookup = $this->getMockRevisionLookup( [
2559 'getRevisionByTitle' => static function () {
2560 return null;
2561 },
2562 'getTimestampFromId' => static function () {
2563 return '00000000000000';
2564 },
2565 ] );
2566
2567 $store = $this->newWatchedItemStore( [
2568 'db' => $mockDb,
2569 'queueGroup' => $mockQueueGroup,
2570 'cache' => $mockCache,
2571 'revisionLookup' => $mockRevisionLookup,
2572 ] );
2573
2574 $this->assertTrue(
2575 $store->resetNotificationTimestamp(
2576 $user,
2577 $title
2578 )
2579 );
2580 }
2581
2582 /**
2583 * @dataProvider provideTestPageFactory
2584 */
2585 public function testResetNotificationTimestamp_noItemForced( $testPageFactory ) {
2586 $user = new UserIdentityValue( 1, 'MockUser' );
2587 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
2588
2589 $mockDb = $this->getMockDb();
2590 $mockDb->expects( $this->never() )
2591 ->method( 'selectRow' );
2592
2593 $mockCache = $this->getMockCache();
2594 $mockCache->expects( $this->never() )->method( 'get' );
2595 $mockCache->expects( $this->never() )->method( 'set' );
2596 $mockCache->expects( $this->once() )
2597 ->method( 'delete' )
2598 ->with( '0:SomeDbKey:1' );
2599
2600 $mockQueueGroup = $this->getMockJobQueueGroup( false );
2601
2602 // We don't care if these methods actually do anything here
2603 $mockRevisionLookup = $this->getMockRevisionLookup( [
2604 'getRevisionByTitle' => static function () {
2605 return null;
2606 },
2607 'getTimestampFromId' => static function () {
2608 return '00000000000000';
2609 },
2610 ] );
2611
2612 $store = $this->newWatchedItemStore( [
2613 'db' => $mockDb,
2614 'queueGroup' => $mockQueueGroup,
2615 'cache' => $mockCache,
2616 'revisionLookup' => $mockRevisionLookup,
2617 ] );
2618
2619 $mockQueueGroup->method( 'lazyPush' )
2620 ->willReturnCallback( static function ( ActivityUpdateJob $job ) {
2621 // don't run
2622 } );
2623
2624 $this->assertTrue(
2625 $store->resetNotificationTimestamp(
2626 $user,
2627 $title,
2628 'force'
2629 )
2630 );
2631 }
2632
2633 /**
2634 * @param ActivityUpdateJob $job
2635 * @param LinkTarget|PageIdentity $expectedTitle
2636 * @param string $expectedUserId
2637 * @param callable $notificationTimestampCondition
2638 */
2639 private function verifyCallbackJob(
2640 ActivityUpdateJob $job,
2641 $expectedTitle,
2642 $expectedUserId,
2643 callable $notificationTimestampCondition
2644 ) {
2645 $this->assertEquals( $expectedTitle->getDBkey(), $job->getTitle()->getDBkey() );
2646 $this->assertEquals( $expectedTitle->getNamespace(), $job->getTitle()->getNamespace() );
2647
2648 $jobParams = $job->getParams();
2649 $this->assertArrayHasKey( 'type', $jobParams );
2650 $this->assertEquals( 'updateWatchlistNotification', $jobParams['type'] );
2651 $this->assertArrayHasKey( 'userid', $jobParams );
2652 $this->assertEquals( $expectedUserId, $jobParams['userid'] );
2653 $this->assertArrayHasKey( 'notifTime', $jobParams );
2654 $this->assertTrue( $notificationTimestampCondition( $jobParams['notifTime'] ) );
2655 }
2656
2657 /**
2658 * @dataProvider provideTestPageFactory
2659 */
2660 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced( $testPageFactory ) {
2661 $user = new UserIdentityValue( 1, 'MockUser' );
2662 $oldid = 22;
2663 $title = $testPageFactory( 100, 0, 'SomeTitle' );
2664
2665 $mockDb = $this->getMockDb();
2666 $mockDb->expects( $this->never() )
2667 ->method( 'selectRow' );
2668
2669 $mockCache = $this->getMockCache();
2670 $mockCache->expects( $this->never() )->method( 'get' );
2671 $mockCache->expects( $this->never() )->method( 'set' );
2672 $mockCache->expects( $this->once() )
2673 ->method( 'delete' )
2674 ->with( '0:SomeTitle:1' );
2675
2676 $mockQueueGroup = $this->getMockJobQueueGroup( false );
2677
2678 $mockRevisionRecord = $this->createNoOpMock( RevisionRecord::class );
2679
2680 $mockRevisionLookup = $this->getMockRevisionLookup( [
2681 'getTimestampFromId' => static function () {
2682 return '00000000000000';
2683 },
2684 'getRevisionById' => function ( $id, $flags ) use ( $oldid, $mockRevisionRecord ) {
2685 $this->assertSame( $oldid, $id );
2686 $this->assertSame( 0, $flags );
2687 return $mockRevisionRecord;
2688 },
2689 'getNextRevision' =>
2690 function ( $oldRev ) use ( $mockRevisionRecord ) {
2691 $this->assertSame( $mockRevisionRecord, $oldRev );
2692 return false;
2693 },
2694 ], [
2695 'getNextRevision' => 1,
2696 ] );
2697
2698 $store = $this->newWatchedItemStore( [
2699 'db' => $mockDb,
2700 'queueGroup' => $mockQueueGroup,
2701 'cache' => $mockCache,
2702 'revisionLookup' => $mockRevisionLookup,
2703 ] );
2704
2705 $mockQueueGroup->method( 'lazyPush' )
2706 ->willReturnCallback(
2707 function ( ActivityUpdateJob $job ) use ( $title, $user ) {
2708 $this->verifyCallbackJob(
2709 $job,
2710 $title,
2711 $user->getId(),
2712 static function ( $time ) {
2713 return $time === null;
2714 }
2715 );
2716 }
2717 );
2718
2719 $this->assertTrue(
2720 $store->resetNotificationTimestamp(
2721 $user,
2722 $title,
2723 'force',
2724 $oldid
2725 )
2726 );
2727 }
2728
2729 /**
2730 * @dataProvider provideTestPageFactory
2731 */
2732 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced( $testPageFactory ) {
2733 $user = new UserIdentityValue( 1, 'MockUser' );
2734 $oldid = 22;
2735 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
2736
2737 $mockRevision = $this->createNoOpMock( RevisionRecord::class );
2738 $mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
2739
2740 $mockDb = $this->getMockDb();
2741 $mockDb->expects( $this->once() )
2742 ->method( 'timestamp' )
2743 ->willReturn( '20200101000000' );
2744 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2745 $mockDb->expects( $this->exactly( 2 ) )
2746 ->method( 'makeList' )
2747 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2748 $selectArgs = [
2749 [
2750 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2751 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2752 [
2753 'wl_user' => 1,
2754 $makeListSql,
2755 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2756 ],
2757 new FakeResultWrapper( [
2758 (object)[
2759 'wl_namespace' => 0,
2760 'wl_title' => 'SomeDbKey',
2761 'wl_notificationtimestamp' => '20151212010101',
2762 ]
2763 ] ),
2764 ],
2765 ];
2766 $mockDb->expects( $this->once() )
2767 ->method( 'select' )
2768 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2769 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2770 $this->assertSame( $nextTable, $table );
2771 $this->assertSame( $nextVars, $vars );
2772 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2773 return $returnValue;
2774 } );
2775
2776 $mockCache = $this->getMockCache();
2777 $mockCache->expects( $this->never() )->method( 'get' );
2778 $mockCache->expects( $this->once() )
2779 ->method( 'set' )
2780 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2781 $mockCache->expects( $this->once() )
2782 ->method( 'delete' )
2783 ->with( '0:SomeDbKey:1' );
2784
2785 $mockQueueGroup = $this->getMockJobQueueGroup( false );
2786
2787 $mockRevisionLookup = $this->getMockRevisionLookup(
2788 [
2789 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2790 $this->assertSame( $oldid, $oldidParam );
2791 },
2792 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2793 $this->assertSame( $oldid, $id );
2794 return $mockRevision;
2795 },
2796 'getNextRevision' =>
2797 function ( RevisionRecord $rev ) use ( $mockRevision, $mockNextRevision ) {
2798 $this->assertSame( $mockRevision, $rev );
2799 return $mockNextRevision;
2800 },
2801 ],
2802 [
2803 'getTimestampFromId' => 2,
2804 'getRevisionById' => 1,
2805 'getNextRevision' => 1,
2806 ]
2807 );
2808
2809 $store = $this->newWatchedItemStore( [
2810 'db' => $mockDb,
2811 'queueGroup' => $mockQueueGroup,
2812 'cache' => $mockCache,
2813 'revisionLookup' => $mockRevisionLookup,
2814 ] );
2815
2816 $mockQueueGroup->method( 'lazyPush' )
2817 ->willReturnCallback(
2818 function ( ActivityUpdateJob $job ) use ( $title, $user ) {
2819 $this->verifyCallbackJob(
2820 $job,
2821 $title,
2822 $user->getId(),
2823 static function ( $time ) {
2824 return $time !== null && $time > '20151212010101';
2825 }
2826 );
2827 }
2828 );
2829
2830 $this->assertTrue(
2831 $store->resetNotificationTimestamp(
2832 $user,
2833 $title,
2834 'force',
2835 $oldid
2836 )
2837 );
2838 }
2839
2840 /**
2841 * @dataProvider provideTestPageFactory
2842 */
2843 public function testResetNotificationTimestamp_notWatchedPageForced( $testPageFactory ) {
2844 $user = new UserIdentityValue( 1, 'MockUser' );
2845 $oldid = 22;
2846 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
2847
2848 $mockDb = $this->getMockDb();
2849 $mockDb->expects( $this->once() )
2850 ->method( 'timestamp' )
2851 ->willReturn( '20200101000000' );
2852 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2853 $mockDb->expects( $this->exactly( 2 ) )
2854 ->method( 'makeList' )
2855 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2856 $selectArgs = [
2857 [
2858 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2859 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2860 [
2861 'wl_user' => 1,
2862 $makeListSql,
2863 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2864 ],
2865 new FakeResultWrapper( [] ),
2866 ],
2867 ];
2868 $mockDb->expects( $this->once() )
2869 ->method( 'select' )
2870 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2871 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2872 $this->assertSame( $nextTable, $table );
2873 $this->assertSame( $nextVars, $vars );
2874 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2875 return $returnValue;
2876 } );
2877
2878 $mockCache = $this->getMockCache();
2879 $mockCache->expects( $this->never() )->method( 'get' );
2880 $mockCache->expects( $this->never() )->method( 'set' );
2881 $mockCache->expects( $this->once() )
2882 ->method( 'delete' )
2883 ->with( '0:SomeDbKey:1' );
2884
2885 $mockQueueGroup = $this->getMockJobQueueGroup( false );
2886
2887 $mockRevision = $this->createNoOpMock( RevisionRecord::class );
2888 $mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
2889
2890 $mockRevisionLookup = $this->getMockRevisionLookup(
2891 [
2892 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2893 $this->assertSame( $oldid, $oldidParam );
2894 },
2895 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2896 $this->assertSame( $oldid, $id );
2897 return $mockRevision;
2898 },
2899 'getNextRevision' =>
2900 function ( RevisionRecord $rev ) use ( $mockRevision, $mockNextRevision ) {
2901 $this->assertSame( $mockRevision, $rev );
2902 return $mockNextRevision;
2903 },
2904 ],
2905 [
2906 'getTimestampFromId' => 1,
2907 'getRevisionById' => 1,
2908 'getNextRevision' => 1,
2909 ]
2910 );
2911
2912 $store = $this->newWatchedItemStore( [
2913 'db' => $mockDb,
2914 'queueGroup' => $mockQueueGroup,
2915 'cache' => $mockCache,
2916 'revisionLookup' => $mockRevisionLookup,
2917 ] );
2918
2919 $mockQueueGroup->method( 'lazyPush' )
2920 ->willReturnCallback(
2921 function ( ActivityUpdateJob $job ) use ( $title, $user ) {
2922 $this->verifyCallbackJob(
2923 $job,
2924 $title,
2925 $user->getId(),
2926 static function ( $time ) {
2927 return $time === null;
2928 }
2929 );
2930 }
2931 );
2932
2933 $this->assertTrue(
2934 $store->resetNotificationTimestamp(
2935 $user,
2936 $title,
2937 'force',
2938 $oldid
2939 )
2940 );
2941 }
2942
2943 /**
2944 * @dataProvider provideTestPageFactory
2945 */
2946 public function testResetNotificationTimestamp_futureNotificationTimestampForced( $testPageFactory ) {
2947 $user = new UserIdentityValue( 1, 'MockUser' );
2948 $oldid = 22;
2949 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
2950
2951 $mockDb = $this->getMockDb();
2952 $mockDb->expects( $this->once() )
2953 ->method( 'timestamp' )
2954 ->willReturn( '20200101000000' );
2955 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
2956 $mockDb->expects( $this->exactly( 2 ) )
2957 ->method( 'makeList' )
2958 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
2959 $selectArgs = [
2960 [
2961 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
2962 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
2963 [
2964 'wl_user' => 1,
2965 $makeListSql,
2966 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')'
2967 ],
2968 new FakeResultWrapper( [
2969 (object)[
2970 'wl_namespace' => 0,
2971 'wl_title' => 'SomeDbKey',
2972 'wl_notificationtimestamp' => '30151212010101',
2973 ]
2974 ] ),
2975 ],
2976 ];
2977 $mockDb->expects( $this->once() )
2978 ->method( 'select' )
2979 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
2980 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
2981 $this->assertSame( $nextTable, $table );
2982 $this->assertSame( $nextVars, $vars );
2983 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
2984 return $returnValue;
2985 } );
2986
2987 $mockCache = $this->getMockCache();
2988 $mockCache->expects( $this->never() )->method( 'get' );
2989 $mockCache->expects( $this->once() )
2990 ->method( 'set' )
2991 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2992 $mockCache->expects( $this->once() )
2993 ->method( 'delete' )
2994 ->with( '0:SomeDbKey:1' );
2995
2996 $mockQueueGroup = $this->getMockJobQueueGroup( false );
2997
2998 $mockRevision = $this->createNoOpMock( RevisionRecord::class );
2999 $mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
3000
3001 $mockRevisionLookup = $this->getMockRevisionLookup(
3002 [
3003 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
3004 $this->assertEquals( $oldid, $oldidParam );
3005 },
3006 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
3007 $this->assertSame( $oldid, $id );
3008 return $mockRevision;
3009 },
3010 'getNextRevision' =>
3011 function ( RevisionRecord $rev ) use ( $mockRevision, $mockNextRevision ) {
3012 $this->assertSame( $mockRevision, $rev );
3013 return $mockNextRevision;
3014 },
3015 ],
3016 [
3017 'getTimestampFromId' => 2,
3018 'getRevisionById' => 1,
3019 'getNextRevision' => 1,
3020 ]
3021 );
3022
3023 $store = $this->newWatchedItemStore( [
3024 'db' => $mockDb,
3025 'queueGroup' => $mockQueueGroup,
3026 'cache' => $mockCache,
3027 'revisionLookup' => $mockRevisionLookup,
3028 ] );
3029
3030 $mockQueueGroup->method( 'lazyPush' )
3031 ->willReturnCallback(
3032 function ( ActivityUpdateJob $job ) use ( $title, $user ) {
3033 $this->verifyCallbackJob(
3034 $job,
3035 $title,
3036 $user->getId(),
3037 static function ( $time ) {
3038 return $time === '30151212010101';
3039 }
3040 );
3041 }
3042 );
3043
3044 $this->assertTrue(
3045 $store->resetNotificationTimestamp(
3046 $user,
3047 $title,
3048 'force',
3049 $oldid
3050 )
3051 );
3052 }
3053
3054 /**
3055 * @dataProvider provideTestPageFactory
3056 */
3057 public function testResetNotificationTimestamp_futureNotificationTimestampNotForced( $testPageFactory ) {
3058 $user = new UserIdentityValue( 1, 'MockUser' );
3059 $oldid = 22;
3060 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
3061
3062 $mockDb = $this->getMockDb();
3063 $mockDb->expects( $this->once() )
3064 ->method( 'timestamp' )
3065 ->willReturn( '20200101000000' );
3066 $makeListSql = "wl_namespace = 0 AND wl_title = 'SomeDbKey'";
3067 $mockDb->expects( $this->exactly( 2 ) )
3068 ->method( 'makeList' )
3069 ->willReturnOnConsecutiveCalls( $makeListSql, $makeListSql );
3070 $selectArgs = [
3071 [
3072 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
3073 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp', 'we_expiry' ],
3074 [
3075 'wl_user' => 1,
3076 $makeListSql,
3077 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')',
3078 ],
3079 new FakeResultWrapper( [
3080 (object)[
3081 'wl_namespace' => 0,
3082 'wl_title' => 'SomeDbKey',
3083 'wl_notificationtimestamp' => '30151212010101',
3084 ]
3085 ] ),
3086 ],
3087 ];
3088 $mockDb->expects( $this->once() )
3089 ->method( 'select' )
3090 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
3091 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
3092 $this->assertSame( $nextTable, $table );
3093 $this->assertSame( $nextVars, $vars );
3094 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
3095 return $returnValue;
3096 } );
3097
3098 $mockCache = $this->getMockCache();
3099 $mockCache->expects( $this->once() )->method( 'get' );
3100 $mockCache->expects( $this->once() )
3101 ->method( 'set' )
3102 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
3103 $mockCache->expects( $this->once() )
3104 ->method( 'delete' )
3105 ->with( '0:SomeDbKey:1' );
3106
3107 $mockQueueGroup = $this->getMockJobQueueGroup( false );
3108
3109 $mockRevision = $this->createNoOpMock( RevisionRecord::class );
3110 $mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
3111
3112 $mockRevisionLookup = $this->getMockRevisionLookup(
3113 [
3114 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
3115 $this->assertEquals( $oldid, $oldidParam );
3116 },
3117 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
3118 $this->assertSame( $oldid, $id );
3119 return $mockRevision;
3120 },
3121 'getNextRevision' =>
3122 function ( RevisionRecord $rev ) use ( $mockRevision, $mockNextRevision ) {
3123 $this->assertSame( $mockRevision, $rev );
3124 return $mockNextRevision;
3125 },
3126 ],
3127 [
3128 'getTimestampFromId' => 2,
3129 'getRevisionById' => 1,
3130 'getNextRevision' => 1,
3131 ]
3132 );
3133
3134 $store = $this->newWatchedItemStore( [
3135 'db' => $mockDb,
3136 'queueGroup' => $mockQueueGroup,
3137 'cache' => $mockCache,
3138 'revisionLookup' => $mockRevisionLookup,
3139 ] );
3140
3141 $mockQueueGroup->method( 'lazyPush' )
3142 ->willReturnCallback(
3143 function ( ActivityUpdateJob $job ) use ( $title, $user ) {
3144 $this->verifyCallbackJob(
3145 $job,
3146 $title,
3147 $user->getId(),
3148 static function ( $time ) {
3149 return $time === false;
3150 }
3151 );
3152 }
3153 );
3154
3155 $this->assertTrue(
3156 $store->resetNotificationTimestamp(
3157 $user,
3158 $title,
3159 '',
3160 $oldid
3161 )
3162 );
3163 }
3164
3165 public function testSetNotificationTimestampsForUser_anonUser() {
3166 $store = $this->newWatchedItemStore();
3167 $this->assertFalse( $store->setNotificationTimestampsForUser(
3168 new UserIdentityValue( 0, 'AnonUser' ), '' ) );
3169 }
3170
3171 public function testSetNotificationTimestampsForUser_allRows() {
3172 $user = new UserIdentityValue( 1, 'MockUser' );
3173 $timestamp = '20100101010101';
3174
3175 $store = $this->newWatchedItemStore();
3176
3177 // Note: This does not actually assert the job is correct
3178 $callableCallCounter = 0;
3179 $mockCallback = function ( $callable ) use ( &$callableCallCounter ) {
3180 $callableCallCounter++;
3181 $this->assertIsCallable( $callable );
3182 };
3183 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
3184
3185 $this->assertTrue(
3186 $store->setNotificationTimestampsForUser( $user, $timestamp )
3187 );
3188 $this->assertSame( 1, $callableCallCounter );
3189 }
3190
3191 public function testSetNotificationTimestampsForUser_nullTimestamp() {
3192 $user = new UserIdentityValue( 1, 'MockUser' );
3193 $timestamp = null;
3194
3195 $store = $this->newWatchedItemStore();
3196
3197 // Note: This does not actually assert the job is correct
3198 $callableCallCounter = 0;
3199 $mockCallback = function ( $callable ) use ( &$callableCallCounter ) {
3200 $callableCallCounter++;
3201 $this->assertIsCallable( $callable );
3202 };
3203 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
3204
3205 $this->assertTrue(
3206 $store->setNotificationTimestampsForUser( $user, $timestamp )
3207 );
3208 }
3209
3210 /**
3211 * @dataProvider provideTestPageFactory
3212 */
3213 public function testSetNotificationTimestampsForUser_specificTargets( $testPageFactory ) {
3214 $user = new UserIdentityValue( 1, 'MockUser' );
3215 $timestamp = '20100101010101';
3216 $targets = [ $testPageFactory( 100, 0, 'Foo' ), $testPageFactory( 101, 0, 'Bar' ) ];
3217
3218 $mockDb = $this->getMockDb();
3219 $mockDb->expects( $this->once() )
3220 ->method( 'selectFieldValues' )
3221 ->with(
3222 [ 'watchlist' ],
3223 'wl_id',
3224 [ 'wl_user' => 1, 'wl_namespace' => 0, 'wl_title' => [ 'Foo', 'Bar' ] ]
3225 )
3226 ->willReturn( [ '2', '3' ] );
3227 $mockDb->expects( $this->once() )
3228 ->method( 'update' )
3229 ->with(
3230 'watchlist',
3231 [ 'wl_notificationtimestamp' => 'TS' . $timestamp . 'TS' ],
3232 [ 'wl_id' => [ 2, 3 ] ]
3233 )
3234 ->willReturn( true );
3235 $mockDb->expects( $this->once() )
3236 ->method( 'timestamp' )
3237 ->willReturnCallback( static function ( $value ) {
3238 return 'TS' . $value . 'TS';
3239 } );
3240 $mockDb->expects( $this->once() )
3241 ->method( 'affectedRows' )
3242 ->willReturn( 2 );
3243
3244 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
3245
3246 $this->assertTrue(
3247 $store->setNotificationTimestampsForUser( $user, $timestamp, $targets )
3248 );
3249 }
3250
3251 /**
3252 * @dataProvider provideTestPageFactory
3253 */
3254 public function testUpdateNotificationTimestamp_watchersExist( $testPageFactory ) {
3255 $mockDb = $this->getMockDb();
3256 $mockDb->method( 'timestamp' )
3257 ->willReturn( '20200101000000' );
3258 $selectArgs = [
3259 [
3260 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
3261 [ 'wl_id', 'wl_user' ],
3262 [
3263 'wl_user != 1',
3264 'wl_namespace' => 0,
3265 'wl_title' => 'SomeDbKey',
3266 'wl_notificationtimestamp' => null,
3267 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')',
3268 ],
3269 new FakeResultWrapper( [
3270 (object)[
3271 'wl_id' => '4',
3272 'wl_user' => '2',
3273 ],
3274 (object)[
3275 'wl_id' => '5',
3276 'wl_user' => '3',
3277 ],
3278 ] ),
3279 ],
3280 ];
3281 $mockDb->expects( $this->once() )
3282 ->method( 'select' )
3283 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
3284 [ $nextTable, $nextVars, $nextConds, $returnValue ] = array_shift( $selectArgs );
3285 $this->assertSame( $nextTable, $table );
3286 $this->assertSame( $nextVars, $vars );
3287 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
3288 return $returnValue;
3289 } );
3290 $mockDb->expects( $this->once() )
3291 ->method( 'update' )
3292 ->with(
3293 'watchlist',
3294 [ 'wl_notificationtimestamp' => '20200101000000' ],
3295 [
3296 'wl_id' => [ 4, 5 ],
3297 ]
3298 );
3299
3300 $mockCache = $this->getMockCache();
3301 $mockCache->expects( $this->never() )->method( 'set' );
3302 $mockCache->expects( $this->never() )->method( 'get' );
3303 $mockCache->expects( $this->never() )->method( 'delete' );
3304
3305 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
3306
3307 // updateNotificationTimestamp calls DeferredUpdates::addCallableUpdate
3308 // in normal operation, but we want to test that update actually running, so
3309 // override it
3310 $mockCallback = function ( $callable, $stage, $dbw ) use ( $mockDb ) {
3311 $this->assertIsCallable( $callable );
3312 $this->assertSame( DeferredUpdates::POSTSEND, $stage );
3313 $this->assertSame( $mockDb, $dbw );
3314 ( $callable )();
3315 };
3316 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
3317
3318 $this->assertEquals(
3319 [ 2, 3 ],
3320 $store->updateNotificationTimestamp(
3321 new UserIdentityValue( 1, 'MockUser' ),
3322 $testPageFactory( 100, 0, 'SomeDbKey' ),
3323 '20151212010101'
3324 )
3325 );
3326 }
3327
3328 /**
3329 * @dataProvider provideTestPageFactory
3330 */
3331 public function testUpdateNotificationTimestamp_noWatchers( $testPageFactory ) {
3332 $mockDb = $this->getMockDb();
3333 $mockDb->expects( $this->once() )
3334 ->method( 'timestamp' )
3335 ->willReturn( '20200101000000' );
3336 $selectArgs = [
3337 [
3338 [ 'watchlist', 'watchlist_expiry' => 'watchlist_expiry' ],
3339 [ 'wl_id', 'wl_user' ],
3340 [
3341 'wl_user != 1',
3342 'wl_namespace' => 0,
3343 'wl_title' => 'SomeDbKey',
3344 'wl_notificationtimestamp' => null,
3345 '(we_expiry IS NULL OR we_expiry > \'20200101000000\')',
3346 ],
3347 [],
3348 [ 'watchlist_expiry' => [ 'LEFT JOIN', 'wl_id = we_item' ] ],
3349 new FakeResultWrapper( [] ),
3350 ],
3351 ];
3352 $mockDb->expects( $this->once() )
3353 ->method( 'select' )
3354 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectArgs ) {
3355 [ $nextTable, $nextVars, $nextConds, $nextOptions, $nextJoinConds, $returnValue ] = array_shift( $selectArgs );
3356 $this->assertSame( $nextTable, $table );
3357 $this->assertSame( $nextVars, $vars );
3358 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
3359 $this->assertSame( $nextOptions, $options );
3360 $this->assertSame( $nextJoinConds, $join_conds );
3361 return $returnValue;
3362 } );
3363 $mockDb->expects( $this->never() )
3364 ->method( 'update' );
3365
3366 $mockCache = $this->getMockCache();
3367 $mockCache->expects( $this->never() )->method( 'set' );
3368 $mockCache->expects( $this->never() )->method( 'get' );
3369 $mockCache->expects( $this->never() )->method( 'delete' );
3370
3371 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
3372
3373 $watchers = $store->updateNotificationTimestamp(
3374 new UserIdentityValue( 1, 'MockUser' ),
3375 $testPageFactory( 100, 0, 'SomeDbKey' ),
3376 '20151212010101'
3377 );
3378 $this->assertSame( [], $watchers );
3379 }
3380
3381 /**
3382 * @dataProvider provideTestPageFactory
3383 */
3384 public function testUpdateNotificationTimestamp_clearsCachedItems( $testPageFactory ) {
3385 $user = new UserIdentityValue( 1, 'MockUser' );
3386 $titleValue = $testPageFactory( 100, 0, 'SomeDbKey' );
3387
3388 $mockDb = $this->getMockDb();
3389 $mockDb->method( 'timestamp' )
3390 ->willReturnArgument( 0 );
3391 $mockDb->expects( $this->exactly( 2 ) )
3392 ->method( 'select' )
3393 ->willReturnOnConsecutiveCalls( new FakeResultWrapper( [
3394 (object)[
3395 'wl_namespace' => 0,
3396 'wl_title' => 'SomeDbKey',
3397 'wl_notificationtimestamp' => '20151212010101'
3398 ]
3399 ] ),
3400 new FakeResultWrapper( [
3401 (object)[
3402 'wl_id' => '4',
3403 'wl_user' => '2',
3404 ],
3405 (object)[
3406 'wl_id' => '5',
3407 'wl_user' => '3',
3408 ],
3409 ] ) );
3410 $mockDb->expects( $this->once() )
3411 ->method( 'update' );
3412
3413 $mockCache = $this->getMockCache();
3414 $mockCache->expects( $this->once() )
3415 ->method( 'set' )
3416 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
3417 $mockCache->expects( $this->once() )
3418 ->method( 'get' )
3419 ->with( '0:SomeDbKey:1' );
3420 $mockCache->expects( $this->once() )
3421 ->method( 'delete' )
3422 ->with( '0:SomeDbKey:1' );
3423
3424 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
3425
3426 // This will add the item to the cache
3427 $store->getWatchedItem( $user, $titleValue );
3428
3429 // updateNotificationTimestamp calls DeferredUpdates::addCallableUpdate
3430 // in normal operation, but we want to test that update actually running, so
3431 // override it
3432 $mockCallback = function ( $callable, $stage, $dbw ) use ( $mockDb ) {
3433 $this->assertIsCallable( $callable );
3434 $this->assertSame( DeferredUpdates::POSTSEND, $stage );
3435 $this->assertSame( $mockDb, $dbw );
3436 ( $callable )();
3437 };
3438 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
3439
3440 $store->updateNotificationTimestamp(
3441 new UserIdentityValue( 1, 'MockUser' ),
3442 $titleValue,
3443 '20151212010101'
3444 );
3445 }
3446
3447 public function testRemoveExpired() {
3448 $mockDb = $this->getMockDb();
3449
3450 $mockDb->expects( $this->once() )
3451 ->method( 'timestamp' )
3452 ->willReturn( '20200101000000' );
3453
3454 $selectFieldValuesArgs = [
3455 // Select expired items.
3456 [
3457 [ 'watchlist_expiry' ],
3458 'we_item',
3459 [ 'we_expiry <= \'20200101000000\'' ],
3460 [ 'LIMIT' => 2 ],
3461 [],
3462 [ 1, 2 ],
3463 ],
3464 // Select orphaned items.
3465 [
3466 [ 'watchlist_expiry', 'watchlist' => 'watchlist' ],
3467 'we_item',
3468 [ 'wl_id' => null, 'we_expiry' => null ],
3469 [],
3470 [ 'watchlist' => [ 'LEFT JOIN', 'wl_id = we_item' ] ],
3471 [ 3 ]
3472 ]
3473 ];
3474 // Select watchlist IDs.
3475 $mockDb->expects( $this->exactly( 2 ) )
3476 ->method( 'selectFieldValues' )
3477 ->willReturnCallback( function ( $table, $vars, $conds, $fname, $options, $join_conds ) use ( &$selectFieldValuesArgs ) {
3478 [ $nextTable, $nextVars, $nextConds, $nextOptions, $nextJoinConds, $returnValue ] = array_shift( $selectFieldValuesArgs );
3479 $this->assertSame( $nextTable, $table );
3480 $this->assertSame( $nextVars, $vars );
3481 $this->assertSame( $nextConds, $this->expandExpr( $conds ) );
3482 $this->assertSame( $nextOptions, $options );
3483 $this->assertSame( $nextJoinConds, $join_conds );
3484 return $returnValue;
3485 } );
3486
3487 // Return whatever is passed to makeList, to be tested below.
3488 $mockDb->expects( $this->once() )
3489 ->method( 'makeList' )
3490 ->willReturnArgument( 0 );
3491
3492 // Delete from watchlist and watchlist_expiry.
3493 $deleteArgs = [
3494 // Delete expired items from watchlist
3495 [
3496 'watchlist',
3497 [ 'wl_id' => [ 1, 2 ] ],
3498 ],
3499 // Delete expired items from watchlist_expiry
3500 [
3501 'watchlist_expiry',
3502 [ 'we_item' => [ 1, 2 ] ],
3503 ],
3504 // Delete orphaned items
3505 [
3506 'watchlist_expiry',
3507 [ 'we_item' => [ 3 ] ],
3508 ]
3509 ];
3510 $mockDb->expects( $this->exactly( 3 ) )
3511 ->method( 'delete' )
3512 ->willReturnCallback( function ( $table, $conds ) use ( &$deleteArgs ): void {
3513 [ $nextTable, $nextConds ] = array_shift( $deleteArgs );
3514 $this->assertSame( $nextTable, $table );
3515 $this->assertSame( $nextConds, $conds );
3516 } );
3517
3518 $mockCache = $this->getMockCache();
3519 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
3520 $store->removeExpired( 2, true );
3521 }
3522
3523 public static function provideGetLatestNotificationTimestamp() {
3524 $emptyMap = new MapCacheLRU( 300 );
3525 $oldMap = new MapCacheLRU( 300 );
3526 $oldMap->set( '0:Title', '20090101000000' );
3527 $newMap = new MapCacheLRU( 300 );
3528 $newMap->set( '0:Title', '20110101000000' );
3529 $wrongKeyMap = new MapCacheLRU( 300 );
3530 $wrongKeyMap->set( '0:Wrong', '20110101000000' );
3531 // Arrays are used for stash values after T282105. We test forwards and
3532 // backwards compatibility. The MapCacheLRU cases can be removed after
3533 // deployment of T282105 has finished.
3534 return [
3535 'empty cache' => [
3536 null,
3537 true
3538 ],
3539 'empty MapCacheLRU' => [
3540 $emptyMap,
3541 true
3542 ],
3543 'empty array' => [
3544 $emptyMap->toArray(),
3545 true
3546 ],
3547 'old MapCacheLRU' => [
3548 $oldMap,
3549 true,
3550 ],
3551 'old array' => [
3552 $oldMap->toArray(),
3553 true
3554 ],
3555 'new MapCacheLRU' => [
3556 $newMap,
3557 false
3558 ],
3559 'new array' => [
3560 $newMap->toArray(),
3561 false
3562 ],
3563 'wrong key MapCacheLRU' => [
3564 $wrongKeyMap,
3565 true
3566 ],
3567 'wrong key array' => [
3568 $wrongKeyMap->toArray(),
3569 true
3570 ],
3571 ];
3572 }
3573
3574 /** @dataProvider provideGetLatestNotificationTimestamp */
3575 public function testGetLatestNotificationTimestamp( $cacheValue, $expectNonNull ) {
3576 $user = new UserIdentityValue( 1, 'User' );
3577 $title = new TitleValue( 0, 'Title' );
3578 $stash = new HashBagOStuff;
3579 $stash->set(
3580 $stash->makeGlobalKey(
3581 'watchlist-recent-updates',
3582 'phpunitdb',
3583 $user->getId()
3584 ),
3585 $cacheValue
3586 );
3587 $store = $this->newWatchedItemStore( [ 'stash' => $stash ] );
3588 $result = $store->getLatestNotificationTimestamp(
3589 '20100101000000', $user, $title );
3590 $this->assertSame( $expectNonNull, $result !== null );
3591 }
3592
3593 /**
3594 * @dataProvider provideTestPageFactory
3595 */
3596 public function testResetNotificationTimestamp_stashItemTypeCheck( $testPageFactory ) {
3597 $user = new UserIdentityValue( 1, 'MockUser' );
3598 $oldid = 22;
3599 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
3600 $stash = new HashBagOStuff;
3601 $mockCache = $this->getMockCache();
3602 $mockRevision = $this->createNoOpMock( RevisionRecord::class );
3603 $mockNextRevision = $this->createNoOpMock( RevisionRecord::class );
3604 $mockRevisionLookup = $this->getMockRevisionLookup(
3605 [
3606 'getTimestampFromId' => static function () {
3607 return '00000000000000';
3608 },
3609 'getRevisionByTitle' => static function () {
3610 return null;
3611 },
3612 'getRevisionById' => static function ( $id ) use ( $mockRevision ) {
3613 return $mockRevision;
3614 },
3615 'getNextRevision' => static function ( RevisionRecord $rev ) use ( $mockNextRevision ) {
3616 return $mockNextRevision;
3617 },
3618 ]
3619 );
3620 $mockDb = $this->getMockDb();
3621 $mockDb->method( 'timestamp' )
3622 ->willReturnArgument( 0 );
3623 $mockDb->method( 'select' )->willReturn( new FakeResultWrapper( [] ) );
3624 $store = $this->newWatchedItemStore( [
3625 'db' => $mockDb,
3626 'cache' => $mockCache,
3627 'revisionLookup' => $mockRevisionLookup,
3628 'stash' => $stash,
3629 'queueGroup' => $this->getMockJobQueueGroup( false ),
3630 ] );
3631 $store->resetNotificationTimestamp( $user,
3632 $title,
3633 'force',
3634 $oldid );
3635 $this->assertIsArray( $stash->get( 'global:watchlist-recent-updates:phpunitdb:1' ) );
3636 }
3637 }