2 use MediaWiki\Linker\LinkTarget
;
3 use MediaWiki\Revision\RevisionLookup
;
4 use MediaWiki\Revision\RevisionRecord
;
5 use MediaWiki\User\UserIdentityValue
;
6 use Wikimedia\Rdbms\LBFactory
;
7 use Wikimedia\Rdbms\LoadBalancer
;
8 use Wikimedia\TestingAccessWrapper
;
13 * @covers WatchedItemStore
15 class WatchedItemStoreUnitTest
extends MediaWikiTestCase
{
18 * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
20 private function getMockDb() {
21 return $this->createMock( IDatabase
::class );
25 * @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
27 private function getMockLoadBalancer(
29 $expectedConnectionType = null
31 $mock = $this->getMockBuilder( LoadBalancer
::class )
32 ->disableOriginalConstructor()
34 if ( $expectedConnectionType !== null ) {
35 $mock->expects( $this->any() )
36 ->method( 'getConnectionRef' )
37 ->with( $expectedConnectionType )
38 ->will( $this->returnValue( $mockDb ) );
40 $mock->expects( $this->any() )
41 ->method( 'getConnectionRef' )
42 ->will( $this->returnValue( $mockDb ) );
48 * @return PHPUnit_Framework_MockObject_MockObject|LBFactory
50 private function getMockLBFactory(
52 $expectedConnectionType = null
54 $loadBalancer = $this->getMockLoadBalancer( $mockDb, $expectedConnectionType );
55 $mock = $this->getMockBuilder( LBFactory
::class )
56 ->disableOriginalConstructor()
58 $mock->expects( $this->any() )
59 ->method( 'getMainLB' )
60 ->will( $this->returnValue( $loadBalancer ) );
65 * @return PHPUnit_Framework_MockObject_MockObject|JobQueueGroup
67 private function getMockJobQueueGroup() {
68 $mock = $this->getMockBuilder( JobQueueGroup
::class )
69 ->disableOriginalConstructor()
71 $mock->expects( $this->any() )
73 ->will( $this->returnCallback( function ( Job
$job ) {
76 $mock->expects( $this->any() )
77 ->method( 'lazyPush' )
78 ->will( $this->returnCallback( function ( Job
$job ) {
85 * @return PHPUnit_Framework_MockObject_MockObject|HashBagOStuff
87 private function getMockCache() {
88 $mock = $this->getMockBuilder( HashBagOStuff
::class )
89 ->disableOriginalConstructor()
90 ->setMethods( [ 'get', 'set', 'delete', 'makeKey' ] )
92 $mock->expects( $this->any() )
94 ->will( $this->returnCallback( function ( ...$args ) {
95 return implode( ':', $args );
101 * @return PHPUnit_Framework_MockObject_MockObject|ReadOnlyMode
103 private function getMockReadOnlyMode( $readOnly = false ) {
104 $mock = $this->getMockBuilder( ReadOnlyMode
::class )
105 ->disableOriginalConstructor()
107 $mock->expects( $this->any() )
108 ->method( 'isReadOnly' )
109 ->will( $this->returnValue( $readOnly ) );
114 * Assumes that only getSubjectPage and getTalkPage will ever be called, and everything passed
115 * to them will have namespace 0.
117 private function getMockNsInfo() : NamespaceInfo
{
118 $mock = $this->createMock( NamespaceInfo
::class );
119 $mock->method( 'getSubjectPage' )->will( $this->returnArgument( 0 ) );
120 $mock->method( 'getTalkPage' )->will( $this->returnCallback(
121 function ( $target ) {
122 return new TitleValue( 1, $target->getDbKey() );
125 $mock->expects( $this->never() )
126 ->method( $this->anythingBut( 'getSubjectPage', 'getTalkPage' ) );
131 * No methods may be called except provided callbacks, if any.
133 * @param array $callbacks Keys are method names, values are callbacks
134 * @param array $counts Keys are method names, values are expected number of times to be called
135 * (default is any number is okay)
137 private function getMockRevisionLookup(
138 array $callbacks = [], array $counts = []
140 $mock = $this->createMock( RevisionLookup
::class );
141 foreach ( $callbacks as $method => $callback ) {
142 $count = isset( $counts[$method] ) ?
$this->exactly( $counts[$method] ) : $this->any();
143 $mock->expects( $count )
145 ->will( $this->returnCallback( $callbacks[$method] ) );
147 $mock->expects( $this->never() )
148 ->method( $this->anythingBut( ...array_keys( $callbacks ) ) );
152 private function getFakeRow( array $rowValues ) {
153 $fakeRow = new stdClass();
154 foreach ( $rowValues as $valueName => $value ) {
155 $fakeRow->$valueName = $value;
161 * @param array $mocks Associative array providing mocks to use when constructing the
162 * WatchedItemStore. Anything not provided will fall back to a default. Valid keys:
171 private function newWatchedItemStore( array $mocks = [] ) : WatchedItemStore
{
172 return new WatchedItemStore(
173 $mocks['lbFactory'] ??
174 $this->getMockLBFactory( $mocks['db'] ??
$this->getMockDb() ),
175 $mocks['queueGroup'] ??
$this->getMockJobQueueGroup(),
177 $mocks['cache'] ??
$this->getMockCache(),
178 $mocks['readOnlyMode'] ??
$this->getMockReadOnlyMode(),
180 $mocks['nsInfo'] ??
$this->getMockNsInfo(),
181 $mocks['revisionLookup'] ??
$this->getMockRevisionLookup()
185 public function testClearWatchedItems() {
186 $user = new UserIdentityValue( 7, 'MockUser', 0 );
188 $mockDb = $this->getMockDb();
189 $mockDb->expects( $this->once() )
190 ->method( 'selectField' )
195 'wl_user' => $user->getId(),
197 $this->isType( 'string' )
199 ->will( $this->returnValue( 12 ) );
200 $mockDb->expects( $this->once() )
205 $this->isType( 'string' )
208 $mockCache = $this->getMockCache();
209 $mockCache->expects( $this->never() )->method( 'get' );
210 $mockCache->expects( $this->never() )->method( 'set' );
211 $mockCache->expects( $this->once() )
215 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
216 TestingAccessWrapper
::newFromObject( $store )
217 ->cacheIndex
= [ 0 => [ 'F' => [ 7 => 'RM-KEY', 9 => 'KEEP-KEY' ] ] ];
219 $this->assertTrue( $store->clearUserWatchedItems( $user ) );
222 public function testClearWatchedItems_tooManyItemsWatched() {
223 $user = new UserIdentityValue( 7, 'MockUser', 0 );
225 $mockDb = $this->getMockDb();
226 $mockDb->expects( $this->once() )
227 ->method( 'selectField' )
232 'wl_user' => $user->getId(),
234 $this->isType( 'string' )
236 ->will( $this->returnValue( 99999 ) );
238 $mockCache = $this->getMockCache();
239 $mockCache->expects( $this->never() )->method( 'get' );
240 $mockCache->expects( $this->never() )->method( 'set' );
241 $mockCache->expects( $this->never() )->method( 'delete' );
243 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
245 $this->assertFalse( $store->clearUserWatchedItems( $user ) );
248 public function testCountWatchedItems() {
249 $user = new UserIdentityValue( 1, 'MockUser', 0 );
251 $mockDb = $this->getMockDb();
252 $mockDb->expects( $this->exactly( 1 ) )
253 ->method( 'selectField' )
258 'wl_user' => $user->getId(),
260 $this->isType( 'string' )
262 ->will( $this->returnValue( '12' ) );
264 $mockCache = $this->getMockCache();
265 $mockCache->expects( $this->never() )->method( 'get' );
266 $mockCache->expects( $this->never() )->method( 'set' );
267 $mockCache->expects( $this->never() )->method( 'delete' );
269 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
271 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
274 public function testCountWatchers() {
275 $titleValue = new TitleValue( 0, 'SomeDbKey' );
277 $mockDb = $this->getMockDb();
278 $mockDb->expects( $this->exactly( 1 ) )
279 ->method( 'selectField' )
284 'wl_namespace' => $titleValue->getNamespace(),
285 'wl_title' => $titleValue->getDBkey(),
287 $this->isType( 'string' )
289 ->will( $this->returnValue( '7' ) );
291 $mockCache = $this->getMockCache();
292 $mockCache->expects( $this->never() )->method( 'get' );
293 $mockCache->expects( $this->never() )->method( 'set' );
294 $mockCache->expects( $this->never() )->method( 'delete' );
296 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
298 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
301 public function testCountWatchersMultiple() {
303 new TitleValue( 0, 'SomeDbKey' ),
304 new TitleValue( 0, 'OtherDbKey' ),
305 new TitleValue( 1, 'AnotherDbKey' ),
308 $mockDb = $this->getMockDb();
311 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
312 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
313 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
316 $mockDb->expects( $this->once() )
317 ->method( 'makeWhereFrom2d' )
319 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
320 $this->isType( 'string' ),
321 $this->isType( 'string' )
323 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
324 $mockDb->expects( $this->once() )
328 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
329 [ 'makeWhereFrom2d return value' ],
330 $this->isType( 'string' ),
332 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
336 $this->returnValue( $dbResult )
339 $mockCache = $this->getMockCache();
340 $mockCache->expects( $this->never() )->method( 'get' );
341 $mockCache->expects( $this->never() )->method( 'set' );
342 $mockCache->expects( $this->never() )->method( 'delete' );
344 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
347 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
348 1 => [ 'AnotherDbKey' => 500 ],
350 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
353 public function provideIntWithDbUnsafeVersion() {
356 [ "50; DROP TABLE watchlist;\n--" ],
361 * @dataProvider provideIntWithDbUnsafeVersion
363 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
365 new TitleValue( 0, 'SomeDbKey' ),
366 new TitleValue( 0, 'OtherDbKey' ),
367 new TitleValue( 1, 'AnotherDbKey' ),
370 $mockDb = $this->getMockDb();
373 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
374 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
375 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
378 $mockDb->expects( $this->once() )
379 ->method( 'makeWhereFrom2d' )
381 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
382 $this->isType( 'string' ),
383 $this->isType( 'string' )
385 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
386 $mockDb->expects( $this->once() )
390 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
391 [ 'makeWhereFrom2d return value' ],
392 $this->isType( 'string' ),
394 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
395 'HAVING' => 'COUNT(*) >= 50',
399 $this->returnValue( $dbResult )
402 $mockCache = $this->getMockCache();
403 $mockCache->expects( $this->never() )->method( 'get' );
404 $mockCache->expects( $this->never() )->method( 'set' );
405 $mockCache->expects( $this->never() )->method( 'delete' );
407 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
410 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
411 1 => [ 'AnotherDbKey' => 500 ],
415 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
419 public function testCountVisitingWatchers() {
420 $titleValue = new TitleValue( 0, 'SomeDbKey' );
422 $mockDb = $this->getMockDb();
423 $mockDb->expects( $this->exactly( 1 ) )
424 ->method( 'selectField' )
429 'wl_namespace' => $titleValue->getNamespace(),
430 'wl_title' => $titleValue->getDBkey(),
431 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
433 $this->isType( 'string' )
435 ->will( $this->returnValue( '7' ) );
436 $mockDb->expects( $this->exactly( 1 ) )
437 ->method( 'addQuotes' )
438 ->will( $this->returnCallback( function ( $value ) {
441 $mockDb->expects( $this->exactly( 1 ) )
442 ->method( 'timestamp' )
443 ->will( $this->returnCallback( function ( $value ) {
444 return 'TS' . $value . 'TS';
447 $mockCache = $this->getMockCache();
448 $mockCache->expects( $this->never() )->method( 'set' );
449 $mockCache->expects( $this->never() )->method( 'get' );
450 $mockCache->expects( $this->never() )->method( 'delete' );
452 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
454 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
457 public function testCountVisitingWatchersMultiple() {
458 $titleValuesWithThresholds = [
459 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
460 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
461 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
465 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
466 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
468 [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
471 $mockDb = $this->getMockDb();
472 $mockDb->expects( $this->exactly( 2 * 3 ) )
473 ->method( 'addQuotes' )
474 ->will( $this->returnCallback( function ( $value ) {
477 $mockDb->expects( $this->exactly( 3 ) )
478 ->method( 'timestamp' )
479 ->will( $this->returnCallback( function ( $value ) {
480 return 'TS' . $value . 'TS';
482 $mockDb->expects( $this->any() )
483 ->method( 'makeList' )
485 $this->isType( 'array' ),
486 $this->isType( 'int' )
488 ->will( $this->returnCallback( function ( $a, $conj ) {
489 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
490 return implode( $sqlConj, array_map( function ( $s ) {
491 return '(' . $s . ')';
495 $mockDb->expects( $this->never() )
496 ->method( 'makeWhereFrom2d' );
499 '((wl_namespace = 0) AND (' .
500 "(((wl_title = 'SomeDbKey') AND (" .
501 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
503 "(wl_title = 'OtherDbKey') AND (" .
504 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
506 ') OR ((wl_namespace = 1) AND (' .
507 "(((wl_title = 'AnotherDbKey') AND (" .
508 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
510 $mockDb->expects( $this->once() )
514 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
516 $this->isType( 'string' ),
518 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
522 $this->returnValue( $dbResult )
525 $mockCache = $this->getMockCache();
526 $mockCache->expects( $this->never() )->method( 'get' );
527 $mockCache->expects( $this->never() )->method( 'set' );
528 $mockCache->expects( $this->never() )->method( 'delete' );
530 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
533 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
534 1 => [ 'AnotherDbKey' => 500 ],
538 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
542 public function testCountVisitingWatchersMultiple_withMissingTargets() {
543 $titleValuesWithThresholds = [
544 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
545 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
546 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
547 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
548 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
552 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
553 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
555 [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
558 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '100' ]
561 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '200' ]
564 $mockDb = $this->getMockDb();
565 $mockDb->expects( $this->exactly( 2 * 3 ) )
566 ->method( 'addQuotes' )
567 ->will( $this->returnCallback( function ( $value ) {
570 $mockDb->expects( $this->exactly( 3 ) )
571 ->method( 'timestamp' )
572 ->will( $this->returnCallback( function ( $value ) {
573 return 'TS' . $value . 'TS';
575 $mockDb->expects( $this->any() )
576 ->method( 'makeList' )
578 $this->isType( 'array' ),
579 $this->isType( 'int' )
581 ->will( $this->returnCallback( function ( $a, $conj ) {
582 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
583 return implode( $sqlConj, array_map( function ( $s ) {
584 return '(' . $s . ')';
588 $mockDb->expects( $this->once() )
589 ->method( 'makeWhereFrom2d' )
591 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
592 $this->isType( 'string' ),
593 $this->isType( 'string' )
595 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
598 '((wl_namespace = 0) AND (' .
599 "(((wl_title = 'SomeDbKey') AND (" .
600 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
602 "(wl_title = 'OtherDbKey') AND (" .
603 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
605 ') OR ((wl_namespace = 1) AND (' .
606 "(((wl_title = 'AnotherDbKey') AND (" .
607 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
610 '(makeWhereFrom2d return value)';
611 $mockDb->expects( $this->once() )
615 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
617 $this->isType( 'string' ),
619 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
623 $this->returnValue( $dbResult )
626 $mockCache = $this->getMockCache();
627 $mockCache->expects( $this->never() )->method( 'get' );
628 $mockCache->expects( $this->never() )->method( 'set' );
629 $mockCache->expects( $this->never() )->method( 'delete' );
631 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
635 'SomeDbKey' => 100, 'OtherDbKey' => 300,
636 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
638 1 => [ 'AnotherDbKey' => 500 ],
642 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
647 * @dataProvider provideIntWithDbUnsafeVersion
649 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
650 $titleValuesWithThresholds = [
651 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
652 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
653 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
656 $mockDb = $this->getMockDb();
657 $mockDb->expects( $this->any() )
658 ->method( 'makeList' )
659 ->will( $this->returnValue( 'makeList return value' ) );
660 $mockDb->expects( $this->once() )
664 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
665 'makeList return value',
666 $this->isType( 'string' ),
668 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
669 'HAVING' => 'COUNT(*) >= 50',
673 $this->returnValue( [] )
676 $mockCache = $this->getMockCache();
677 $mockCache->expects( $this->never() )->method( 'get' );
678 $mockCache->expects( $this->never() )->method( 'set' );
679 $mockCache->expects( $this->never() )->method( 'delete' );
681 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
684 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
685 1 => [ 'AnotherDbKey' => 0 ],
689 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
693 public function testCountUnreadNotifications() {
694 $user = new UserIdentityValue( 1, 'MockUser', 0 );
696 $mockDb = $this->getMockDb();
697 $mockDb->expects( $this->exactly( 1 ) )
698 ->method( 'selectRowCount' )
703 "wl_notificationtimestamp IS NOT NULL",
706 $this->isType( 'string' )
708 ->will( $this->returnValue( '9' ) );
710 $mockCache = $this->getMockCache();
711 $mockCache->expects( $this->never() )->method( 'set' );
712 $mockCache->expects( $this->never() )->method( 'get' );
713 $mockCache->expects( $this->never() )->method( 'delete' );
715 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
717 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
721 * @dataProvider provideIntWithDbUnsafeVersion
723 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
724 $user = new UserIdentityValue( 1, 'MockUser', 0 );
726 $mockDb = $this->getMockDb();
727 $mockDb->expects( $this->exactly( 1 ) )
728 ->method( 'selectRowCount' )
733 "wl_notificationtimestamp IS NOT NULL",
736 $this->isType( 'string' ),
739 ->will( $this->returnValue( '50' ) );
741 $mockCache = $this->getMockCache();
742 $mockCache->expects( $this->never() )->method( 'set' );
743 $mockCache->expects( $this->never() )->method( 'get' );
744 $mockCache->expects( $this->never() )->method( 'delete' );
746 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
750 $store->countUnreadNotifications( $user, $limit )
755 * @dataProvider provideIntWithDbUnsafeVersion
757 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
758 $user = new UserIdentityValue( 1, 'MockUser', 0 );
760 $mockDb = $this->getMockDb();
761 $mockDb->expects( $this->exactly( 1 ) )
762 ->method( 'selectRowCount' )
767 "wl_notificationtimestamp IS NOT NULL",
770 $this->isType( 'string' ),
773 ->will( $this->returnValue( '9' ) );
775 $mockCache = $this->getMockCache();
776 $mockCache->expects( $this->never() )->method( 'set' );
777 $mockCache->expects( $this->never() )->method( 'get' );
778 $mockCache->expects( $this->never() )->method( 'delete' );
780 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
784 $store->countUnreadNotifications( $user, $limit )
788 public function testDuplicateEntry_nothingToDuplicate() {
789 $mockDb = $this->getMockDb();
790 $mockDb->expects( $this->once() )
796 'wl_notificationtimestamp',
800 'wl_title' => 'Old_Title',
802 'WatchedItemStore::duplicateEntry',
805 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
807 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
809 $store->duplicateEntry(
810 new TitleValue( 0, 'Old_Title' ),
811 new TitleValue( 0, 'New_Title' )
815 public function testDuplicateEntry_somethingToDuplicate() {
817 $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
818 $this->getFakeRow( [ 'wl_user' => '2', 'wl_notificationtimestamp' => null ] ),
821 $mockDb = $this->getMockDb();
822 $mockDb->expects( $this->at( 0 ) )
828 'wl_notificationtimestamp',
832 'wl_title' => 'Old_Title',
835 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
836 $mockDb->expects( $this->at( 1 ) )
837 ->method( 'replace' )
840 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
845 'wl_title' => 'New_Title',
846 'wl_notificationtimestamp' => '20151212010101',
851 'wl_title' => 'New_Title',
852 'wl_notificationtimestamp' => null,
855 $this->isType( 'string' )
858 $mockCache = $this->getMockCache();
859 $mockCache->expects( $this->never() )->method( 'get' );
860 $mockCache->expects( $this->never() )->method( 'delete' );
862 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
864 $store->duplicateEntry(
865 new TitleValue( 0, 'Old_Title' ),
866 new TitleValue( 0, 'New_Title' )
870 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
871 $mockDb = $this->getMockDb();
872 $mockDb->expects( $this->at( 0 ) )
878 'wl_notificationtimestamp',
882 'wl_title' => 'Old_Title',
885 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
886 $mockDb->expects( $this->at( 1 ) )
892 'wl_notificationtimestamp',
896 'wl_title' => 'Old_Title',
899 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
901 $mockCache = $this->getMockCache();
902 $mockCache->expects( $this->never() )->method( 'get' );
903 $mockCache->expects( $this->never() )->method( 'delete' );
905 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
907 $store->duplicateAllAssociatedEntries(
908 new TitleValue( 0, 'Old_Title' ),
909 new TitleValue( 0, 'New_Title' )
913 public function provideLinkTargetPairs() {
915 [ new TitleValue( 0, 'Old_Title' ), new TitleValue( 0, 'New_Title' ) ],
916 [ new TitleValue( 0, 'Old_Title' ), new TitleValue( 0, 'New_Title' ) ],
921 * @dataProvider provideLinkTargetPairs
923 public function testDuplicateAllAssociatedEntries_somethingToDuplicate(
924 LinkTarget
$oldTarget,
925 LinkTarget
$newTarget
928 $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
931 $mockDb = $this->getMockDb();
932 $mockDb->expects( $this->at( 0 ) )
938 'wl_notificationtimestamp',
941 'wl_namespace' => $oldTarget->getNamespace(),
942 'wl_title' => $oldTarget->getDBkey(),
945 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
946 $mockDb->expects( $this->at( 1 ) )
947 ->method( 'replace' )
950 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
954 'wl_namespace' => $newTarget->getNamespace(),
955 'wl_title' => $newTarget->getDBkey(),
956 'wl_notificationtimestamp' => '20151212010101',
959 $this->isType( 'string' )
961 $mockDb->expects( $this->at( 2 ) )
967 'wl_notificationtimestamp',
970 'wl_namespace' => $oldTarget->getNamespace() +
1,
971 'wl_title' => $oldTarget->getDBkey(),
974 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
975 $mockDb->expects( $this->at( 3 ) )
976 ->method( 'replace' )
979 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
983 'wl_namespace' => $newTarget->getNamespace() +
1,
984 'wl_title' => $newTarget->getDBkey(),
985 'wl_notificationtimestamp' => '20151212010101',
988 $this->isType( 'string' )
991 $mockCache = $this->getMockCache();
992 $mockCache->expects( $this->never() )->method( 'get' );
993 $mockCache->expects( $this->never() )->method( 'delete' );
995 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
997 $store->duplicateAllAssociatedEntries(
1003 public function testAddWatch_nonAnonymousUser() {
1004 $mockDb = $this->getMockDb();
1005 $mockDb->expects( $this->once() )
1006 ->method( 'insert' )
1012 'wl_namespace' => 0,
1013 'wl_title' => 'Some_Page',
1014 'wl_notificationtimestamp' => null,
1019 $mockCache = $this->getMockCache();
1020 $mockCache->expects( $this->once() )
1021 ->method( 'delete' )
1022 ->with( '0:Some_Page:1' );
1024 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1027 new UserIdentityValue( 1, 'MockUser', 0 ),
1028 new TitleValue( 0, 'Some_Page' )
1032 public function testAddWatch_anonymousUser() {
1033 $mockDb = $this->getMockDb();
1034 $mockDb->expects( $this->never() )
1035 ->method( 'insert' );
1037 $mockCache = $this->getMockCache();
1038 $mockCache->expects( $this->never() )
1039 ->method( 'delete' );
1041 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1044 new UserIdentityValue( 0, 'AnonUser', 0 ),
1045 new TitleValue( 0, 'Some_Page' )
1049 public function testAddWatchBatchForUser_readOnlyDBReturnsFalse() {
1050 $store = $this->newWatchedItemStore(
1051 [ 'readOnlyMode' => $this->getMockReadOnlyMode( true ) ] );
1054 $store->addWatchBatchForUser(
1055 new UserIdentityValue( 1, 'MockUser', 0 ),
1056 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1061 public function testAddWatchBatchForUser_nonAnonymousUser() {
1062 $mockDb = $this->getMockDb();
1063 $mockDb->expects( $this->once() )
1064 ->method( 'insert' )
1070 'wl_namespace' => 0,
1071 'wl_title' => 'Some_Page',
1072 'wl_notificationtimestamp' => null,
1076 'wl_namespace' => 1,
1077 'wl_title' => 'Some_Page',
1078 'wl_notificationtimestamp' => null,
1083 $mockDb->expects( $this->once() )
1084 ->method( 'affectedRows' )
1087 $mockCache = $this->getMockCache();
1088 $mockCache->expects( $this->exactly( 2 ) )
1089 ->method( 'delete' );
1090 $mockCache->expects( $this->at( 1 ) )
1091 ->method( 'delete' )
1092 ->with( '0:Some_Page:1' );
1093 $mockCache->expects( $this->at( 3 ) )
1094 ->method( 'delete' )
1095 ->with( '1:Some_Page:1' );
1097 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1099 $mockUser = new UserIdentityValue( 1, 'MockUser', 0 );
1102 $store->addWatchBatchForUser(
1104 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1109 public function testAddWatchBatchForUser_anonymousUsersAreSkipped() {
1110 $mockDb = $this->getMockDb();
1111 $mockDb->expects( $this->never() )
1112 ->method( 'insert' );
1114 $mockCache = $this->getMockCache();
1115 $mockCache->expects( $this->never() )
1116 ->method( 'delete' );
1118 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1121 $store->addWatchBatchForUser(
1122 new UserIdentityValue( 0, 'AnonUser', 0 ),
1123 [ new TitleValue( 0, 'Other_Page' ) ]
1128 public function testAddWatchBatchReturnsTrue_whenGivenEmptyList() {
1129 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1130 $mockDb = $this->getMockDb();
1131 $mockDb->expects( $this->never() )
1132 ->method( 'insert' );
1134 $mockCache = $this->getMockCache();
1135 $mockCache->expects( $this->never() )
1136 ->method( 'delete' );
1138 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1141 $store->addWatchBatchForUser( $user, [] )
1145 public function testLoadWatchedItem_existingItem() {
1146 $mockDb = $this->getMockDb();
1147 $mockDb->expects( $this->once() )
1148 ->method( 'selectRow' )
1151 'wl_notificationtimestamp',
1154 'wl_namespace' => 0,
1155 'wl_title' => 'SomeDbKey',
1158 ->will( $this->returnValue(
1159 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1162 $mockCache = $this->getMockCache();
1163 $mockCache->expects( $this->once() )
1169 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1171 $watchedItem = $store->loadWatchedItem(
1172 new UserIdentityValue( 1, 'MockUser', 0 ),
1173 new TitleValue( 0, 'SomeDbKey' )
1175 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1176 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1177 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1178 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1181 public function testLoadWatchedItem_noItem() {
1182 $mockDb = $this->getMockDb();
1183 $mockDb->expects( $this->once() )
1184 ->method( 'selectRow' )
1187 'wl_notificationtimestamp',
1190 'wl_namespace' => 0,
1191 'wl_title' => 'SomeDbKey',
1194 ->will( $this->returnValue( [] ) );
1196 $mockCache = $this->getMockCache();
1197 $mockCache->expects( $this->never() )->method( 'get' );
1198 $mockCache->expects( $this->never() )->method( 'delete' );
1200 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1203 $store->loadWatchedItem(
1204 new UserIdentityValue( 1, 'MockUser', 0 ),
1205 new TitleValue( 0, 'SomeDbKey' )
1210 public function testLoadWatchedItem_anonymousUser() {
1211 $mockDb = $this->getMockDb();
1212 $mockDb->expects( $this->never() )
1213 ->method( 'selectRow' );
1215 $mockCache = $this->getMockCache();
1216 $mockCache->expects( $this->never() )->method( 'get' );
1217 $mockCache->expects( $this->never() )->method( 'delete' );
1219 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1222 $store->loadWatchedItem(
1223 new UserIdentityValue( 0, 'AnonUser', 0 ),
1224 new TitleValue( 0, 'SomeDbKey' )
1229 public function testRemoveWatch_existingItem() {
1230 $mockDb = $this->getMockDb();
1231 $mockDb->expects( $this->once() )
1232 ->method( 'delete' )
1238 'wl_namespace' => 0,
1239 'wl_title' => [ 'SomeDbKey' ],
1246 'wl_namespace' => 1,
1247 'wl_title' => [ 'SomeDbKey' ],
1251 $mockDb->expects( $this->exactly( 1 ) )
1252 ->method( 'affectedRows' )
1255 $mockCache = $this->getMockCache();
1256 $mockCache->expects( $this->never() )->method( 'get' );
1257 $mockCache->expects( $this->once() )
1258 ->method( 'delete' )
1260 [ '0:SomeDbKey:1' ],
1264 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1267 $store->removeWatch(
1268 new UserIdentityValue( 1, 'MockUser', 0 ),
1269 new TitleValue( 0, 'SomeDbKey' )
1274 public function testRemoveWatch_noItem() {
1275 $mockDb = $this->getMockDb();
1276 $mockDb->expects( $this->once() )
1277 ->method( 'delete' )
1283 'wl_namespace' => 0,
1284 'wl_title' => [ 'SomeDbKey' ],
1291 'wl_namespace' => 1,
1292 'wl_title' => [ 'SomeDbKey' ],
1297 $mockDb->expects( $this->once() )
1298 ->method( 'affectedRows' )
1301 $mockCache = $this->getMockCache();
1302 $mockCache->expects( $this->never() )->method( 'get' );
1303 $mockCache->expects( $this->once() )
1304 ->method( 'delete' )
1306 [ '0:SomeDbKey:1' ],
1310 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1313 $store->removeWatch(
1314 new UserIdentityValue( 1, 'MockUser', 0 ),
1315 new TitleValue( 0, 'SomeDbKey' )
1320 public function testRemoveWatch_anonymousUser() {
1321 $mockDb = $this->getMockDb();
1322 $mockDb->expects( $this->never() )
1323 ->method( 'delete' );
1325 $mockCache = $this->getMockCache();
1326 $mockCache->expects( $this->never() )->method( 'get' );
1327 $mockCache->expects( $this->never() )
1328 ->method( 'delete' );
1330 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1333 $store->removeWatch(
1334 new UserIdentityValue( 0, 'AnonUser', 0 ),
1335 new TitleValue( 0, 'SomeDbKey' )
1340 public function testGetWatchedItem_existingItem() {
1341 $mockDb = $this->getMockDb();
1342 $mockDb->expects( $this->once() )
1343 ->method( 'selectRow' )
1346 'wl_notificationtimestamp',
1349 'wl_namespace' => 0,
1350 'wl_title' => 'SomeDbKey',
1353 ->will( $this->returnValue(
1354 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1357 $mockCache = $this->getMockCache();
1358 $mockCache->expects( $this->never() )->method( 'delete' );
1359 $mockCache->expects( $this->once() )
1364 ->will( $this->returnValue( null ) );
1365 $mockCache->expects( $this->once() )
1371 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1373 $watchedItem = $store->getWatchedItem(
1374 new UserIdentityValue( 1, 'MockUser', 0 ),
1375 new TitleValue( 0, 'SomeDbKey' )
1377 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1378 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1379 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1380 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1383 public function testGetWatchedItem_cachedItem() {
1384 $mockDb = $this->getMockDb();
1385 $mockDb->expects( $this->never() )
1386 ->method( 'selectRow' );
1388 $mockUser = new UserIdentityValue( 1, 'MockUser', 0 );
1389 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1390 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1392 $mockCache = $this->getMockCache();
1393 $mockCache->expects( $this->never() )->method( 'delete' );
1394 $mockCache->expects( $this->never() )->method( 'set' );
1395 $mockCache->expects( $this->once() )
1400 ->will( $this->returnValue( $cachedItem ) );
1402 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1404 $this->assertEquals(
1406 $store->getWatchedItem(
1413 public function testGetWatchedItem_noItem() {
1414 $mockDb = $this->getMockDb();
1415 $mockDb->expects( $this->once() )
1416 ->method( 'selectRow' )
1419 'wl_notificationtimestamp',
1422 'wl_namespace' => 0,
1423 'wl_title' => 'SomeDbKey',
1426 ->will( $this->returnValue( [] ) );
1428 $mockCache = $this->getMockCache();
1429 $mockCache->expects( $this->never() )->method( 'set' );
1430 $mockCache->expects( $this->never() )->method( 'delete' );
1431 $mockCache->expects( $this->once() )
1433 ->with( '0:SomeDbKey:1' )
1434 ->will( $this->returnValue( false ) );
1436 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1439 $store->getWatchedItem(
1440 new UserIdentityValue( 1, 'MockUser', 0 ),
1441 new TitleValue( 0, 'SomeDbKey' )
1446 public function testGetWatchedItem_anonymousUser() {
1447 $mockDb = $this->getMockDb();
1448 $mockDb->expects( $this->never() )
1449 ->method( 'selectRow' );
1451 $mockCache = $this->getMockCache();
1452 $mockCache->expects( $this->never() )->method( 'set' );
1453 $mockCache->expects( $this->never() )->method( 'get' );
1454 $mockCache->expects( $this->never() )->method( 'delete' );
1456 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1459 $store->getWatchedItem(
1460 new UserIdentityValue( 0, 'AnonUser', 0 ),
1461 new TitleValue( 0, 'SomeDbKey' )
1466 public function testGetWatchedItemsForUser() {
1467 $mockDb = $this->getMockDb();
1468 $mockDb->expects( $this->once() )
1469 ->method( 'select' )
1472 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1475 ->will( $this->returnValue( [
1476 $this->getFakeRow( [
1477 'wl_namespace' => 0,
1478 'wl_title' => 'Foo1',
1479 'wl_notificationtimestamp' => '20151212010101',
1481 $this->getFakeRow( [
1482 'wl_namespace' => 1,
1483 'wl_title' => 'Foo2',
1484 'wl_notificationtimestamp' => null,
1488 $mockCache = $this->getMockCache();
1489 $mockCache->expects( $this->never() )->method( 'delete' );
1490 $mockCache->expects( $this->never() )->method( 'get' );
1491 $mockCache->expects( $this->never() )->method( 'set' );
1493 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1494 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1496 $watchedItems = $store->getWatchedItemsForUser( $user );
1498 $this->assertInternalType( 'array', $watchedItems );
1499 $this->assertCount( 2, $watchedItems );
1500 foreach ( $watchedItems as $watchedItem ) {
1501 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1503 $this->assertEquals(
1504 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1507 $this->assertEquals(
1508 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1513 public function provideDbTypes() {
1515 [ false, DB_REPLICA
],
1516 [ true, DB_MASTER
],
1521 * @dataProvider provideDbTypes
1523 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1524 $mockDb = $this->getMockDb();
1525 $mockCache = $this->getMockCache();
1526 $mockLoadBalancer = $this->getMockLBFactory( $mockDb, $dbType );
1527 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1529 $mockDb->expects( $this->once() )
1530 ->method( 'select' )
1533 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1535 $this->isType( 'string' ),
1536 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1538 ->will( $this->returnValue( [] ) );
1540 $store = $this->newWatchedItemStore(
1541 [ 'lbFactory' => $mockLoadBalancer, 'cache' => $mockCache ] );
1543 $watchedItems = $store->getWatchedItemsForUser(
1545 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore
::SORT_ASC
]
1547 $this->assertEquals( [], $watchedItems );
1550 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
1551 $store = $this->newWatchedItemStore();
1553 $this->setExpectedException( InvalidArgumentException
::class );
1554 $store->getWatchedItemsForUser(
1555 new UserIdentityValue( 1, 'MockUser', 0 ),
1560 public function testIsWatchedItem_existingItem() {
1561 $mockDb = $this->getMockDb();
1562 $mockDb->expects( $this->once() )
1563 ->method( 'selectRow' )
1566 'wl_notificationtimestamp',
1569 'wl_namespace' => 0,
1570 'wl_title' => 'SomeDbKey',
1573 ->will( $this->returnValue(
1574 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1577 $mockCache = $this->getMockCache();
1578 $mockCache->expects( $this->never() )->method( 'delete' );
1579 $mockCache->expects( $this->once() )
1581 ->with( '0:SomeDbKey:1' )
1582 ->will( $this->returnValue( false ) );
1583 $mockCache->expects( $this->once() )
1589 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1593 new UserIdentityValue( 1, 'MockUser', 0 ),
1594 new TitleValue( 0, 'SomeDbKey' )
1599 public function testIsWatchedItem_noItem() {
1600 $mockDb = $this->getMockDb();
1601 $mockDb->expects( $this->once() )
1602 ->method( 'selectRow' )
1605 'wl_notificationtimestamp',
1608 'wl_namespace' => 0,
1609 'wl_title' => 'SomeDbKey',
1612 ->will( $this->returnValue( [] ) );
1614 $mockCache = $this->getMockCache();
1615 $mockCache->expects( $this->never() )->method( 'set' );
1616 $mockCache->expects( $this->never() )->method( 'delete' );
1617 $mockCache->expects( $this->once() )
1619 ->with( '0:SomeDbKey:1' )
1620 ->will( $this->returnValue( false ) );
1622 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1626 new UserIdentityValue( 1, 'MockUser', 0 ),
1627 new TitleValue( 0, 'SomeDbKey' )
1632 public function testIsWatchedItem_anonymousUser() {
1633 $mockDb = $this->getMockDb();
1634 $mockDb->expects( $this->never() )
1635 ->method( 'selectRow' );
1637 $mockCache = $this->getMockCache();
1638 $mockCache->expects( $this->never() )->method( 'set' );
1639 $mockCache->expects( $this->never() )->method( 'get' );
1640 $mockCache->expects( $this->never() )->method( 'delete' );
1642 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1646 new UserIdentityValue( 0, 'AnonUser', 0 ),
1647 new TitleValue( 0, 'SomeDbKey' )
1652 public function testGetNotificationTimestampsBatch() {
1654 new TitleValue( 0, 'SomeDbKey' ),
1655 new TitleValue( 1, 'AnotherDbKey' ),
1658 $mockDb = $this->getMockDb();
1660 $this->getFakeRow( [
1661 'wl_namespace' => '0',
1662 'wl_title' => 'SomeDbKey',
1663 'wl_notificationtimestamp' => '20151212010101',
1667 'wl_namespace' => '1',
1668 'wl_title' => 'AnotherDbKey',
1669 'wl_notificationtimestamp' => null,
1674 $mockDb->expects( $this->once() )
1675 ->method( 'makeWhereFrom2d' )
1677 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1678 $this->isType( 'string' ),
1679 $this->isType( 'string' )
1681 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1682 $mockDb->expects( $this->once() )
1683 ->method( 'select' )
1686 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1688 'makeWhereFrom2d return value',
1691 $this->isType( 'string' )
1693 ->will( $this->returnValue( $dbResult ) );
1695 $mockCache = $this->getMockCache();
1696 $mockCache->expects( $this->exactly( 2 ) )
1699 [ '0:SomeDbKey:1' ],
1700 [ '1:AnotherDbKey:1' ]
1702 ->will( $this->returnValue( null ) );
1703 $mockCache->expects( $this->never() )->method( 'set' );
1704 $mockCache->expects( $this->never() )->method( 'delete' );
1706 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1708 $this->assertEquals(
1710 0 => [ 'SomeDbKey' => '20151212010101', ],
1711 1 => [ 'AnotherDbKey' => null, ],
1713 $store->getNotificationTimestampsBatch(
1714 new UserIdentityValue( 1, 'MockUser', 0 ), $targets )
1718 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1720 new TitleValue( 0, 'OtherDbKey' ),
1723 $mockDb = $this->getMockDb();
1725 $mockDb->expects( $this->once() )
1726 ->method( 'makeWhereFrom2d' )
1728 [ [ 'OtherDbKey' => 1 ] ],
1729 $this->isType( 'string' ),
1730 $this->isType( 'string' )
1732 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1733 $mockDb->expects( $this->once() )
1734 ->method( 'select' )
1737 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1739 'makeWhereFrom2d return value',
1742 $this->isType( 'string' )
1744 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1746 $mockCache = $this->getMockCache();
1747 $mockCache->expects( $this->once() )
1749 ->with( '0:OtherDbKey:1' )
1750 ->will( $this->returnValue( null ) );
1751 $mockCache->expects( $this->never() )->method( 'set' );
1752 $mockCache->expects( $this->never() )->method( 'delete' );
1754 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1756 $this->assertEquals(
1758 0 => [ 'OtherDbKey' => false, ],
1760 $store->getNotificationTimestampsBatch(
1761 new UserIdentityValue( 1, 'MockUser', 0 ), $targets )
1765 public function testGetNotificationTimestampsBatch_cachedItem() {
1767 new TitleValue( 0, 'SomeDbKey' ),
1768 new TitleValue( 1, 'AnotherDbKey' ),
1771 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1772 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1774 $mockDb = $this->getMockDb();
1776 $mockDb->expects( $this->once() )
1777 ->method( 'makeWhereFrom2d' )
1779 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1780 $this->isType( 'string' ),
1781 $this->isType( 'string' )
1783 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1784 $mockDb->expects( $this->once() )
1785 ->method( 'select' )
1788 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1790 'makeWhereFrom2d return value',
1793 $this->isType( 'string' )
1795 ->will( $this->returnValue( [
1797 [ 'wl_namespace' => '1', 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1801 $mockCache = $this->getMockCache();
1802 $mockCache->expects( $this->at( 1 ) )
1804 ->with( '0:SomeDbKey:1' )
1805 ->will( $this->returnValue( $cachedItem ) );
1806 $mockCache->expects( $this->at( 3 ) )
1808 ->with( '1:AnotherDbKey:1' )
1809 ->will( $this->returnValue( null ) );
1810 $mockCache->expects( $this->never() )->method( 'set' );
1811 $mockCache->expects( $this->never() )->method( 'delete' );
1813 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1815 $this->assertEquals(
1817 0 => [ 'SomeDbKey' => '20151212010101', ],
1818 1 => [ 'AnotherDbKey' => null, ],
1820 $store->getNotificationTimestampsBatch( $user, $targets )
1824 public function testGetNotificationTimestampsBatch_allItemsCached() {
1826 new TitleValue( 0, 'SomeDbKey' ),
1827 new TitleValue( 1, 'AnotherDbKey' ),
1830 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1832 new WatchedItem( $user, $targets[0], '20151212010101' ),
1833 new WatchedItem( $user, $targets[1], null ),
1835 $mockDb = $this->getMockDb();
1836 $mockDb->expects( $this->never() )->method( $this->anything() );
1838 $mockCache = $this->getMockCache();
1839 $mockCache->expects( $this->at( 1 ) )
1841 ->with( '0:SomeDbKey:1' )
1842 ->will( $this->returnValue( $cachedItems[0] ) );
1843 $mockCache->expects( $this->at( 3 ) )
1845 ->with( '1:AnotherDbKey:1' )
1846 ->will( $this->returnValue( $cachedItems[1] ) );
1847 $mockCache->expects( $this->never() )->method( 'set' );
1848 $mockCache->expects( $this->never() )->method( 'delete' );
1850 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1852 $this->assertEquals(
1854 0 => [ 'SomeDbKey' => '20151212010101', ],
1855 1 => [ 'AnotherDbKey' => null, ],
1857 $store->getNotificationTimestampsBatch( $user, $targets )
1861 public function testGetNotificationTimestampsBatch_anonymousUser() {
1863 new TitleValue( 0, 'SomeDbKey' ),
1864 new TitleValue( 1, 'AnotherDbKey' ),
1867 $mockDb = $this->getMockDb();
1868 $mockDb->expects( $this->never() )->method( $this->anything() );
1870 $mockCache = $this->getMockCache();
1871 $mockCache->expects( $this->never() )->method( $this->anything() );
1873 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1875 $this->assertEquals(
1877 0 => [ 'SomeDbKey' => false, ],
1878 1 => [ 'AnotherDbKey' => false, ],
1880 $store->getNotificationTimestampsBatch(
1881 new UserIdentityValue( 0, 'AnonUser', 0 ), $targets )
1885 public function testResetNotificationTimestamp_anonymousUser() {
1886 $mockDb = $this->getMockDb();
1887 $mockDb->expects( $this->never() )
1888 ->method( 'selectRow' );
1890 $mockCache = $this->getMockCache();
1891 $mockCache->expects( $this->never() )->method( 'get' );
1892 $mockCache->expects( $this->never() )->method( 'set' );
1893 $mockCache->expects( $this->never() )->method( 'delete' );
1895 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1898 $store->resetNotificationTimestamp(
1899 new UserIdentityValue( 0, 'AnonUser', 0 ),
1900 new TitleValue( 0, 'SomeDbKey' )
1905 public function testResetNotificationTimestamp_noItem() {
1906 $mockDb = $this->getMockDb();
1907 $mockDb->expects( $this->once() )
1908 ->method( 'selectRow' )
1911 'wl_notificationtimestamp',
1914 'wl_namespace' => 0,
1915 'wl_title' => 'SomeDbKey',
1918 ->will( $this->returnValue( [] ) );
1920 $mockCache = $this->getMockCache();
1921 $mockCache->expects( $this->never() )->method( 'get' );
1922 $mockCache->expects( $this->never() )->method( 'set' );
1923 $mockCache->expects( $this->never() )->method( 'delete' );
1925 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1928 $store->resetNotificationTimestamp(
1929 new UserIdentityValue( 1, 'MockUser', 0 ),
1930 new TitleValue( 0, 'SomeDbKey' )
1935 public function testResetNotificationTimestamp_item() {
1936 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1937 $title = new TitleValue( 0, 'SomeDbKey' );
1939 $mockDb = $this->getMockDb();
1940 $mockDb->expects( $this->once() )
1941 ->method( 'selectRow' )
1944 'wl_notificationtimestamp',
1947 'wl_namespace' => 0,
1948 'wl_title' => 'SomeDbKey',
1951 ->will( $this->returnValue(
1952 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1955 $mockCache = $this->getMockCache();
1956 $mockCache->expects( $this->never() )->method( 'get' );
1957 $mockCache->expects( $this->once() )
1961 $this->isInstanceOf( WatchedItem
::class )
1963 $mockCache->expects( $this->once() )
1964 ->method( 'delete' )
1965 ->with( '0:SomeDbKey:1' );
1967 $mockQueueGroup = $this->getMockJobQueueGroup();
1968 $mockQueueGroup->expects( $this->once() )
1969 ->method( 'lazyPush' )
1970 ->willReturnCallback( function ( ActivityUpdateJob
$job ) {
1974 // We don't care if these methods actually do anything here
1975 $mockRevisionLookup = $this->getMockRevisionLookup( [
1976 'getRevisionByTitle' => function () {
1979 'getTimestampFromId' => function () {
1980 return '00000000000000';
1984 $store = $this->newWatchedItemStore( [
1986 'queueGroup' => $mockQueueGroup,
1987 'cache' => $mockCache,
1988 'revisionLookup' => $mockRevisionLookup,
1992 $store->resetNotificationTimestamp(
1999 public function testResetNotificationTimestamp_noItemForced() {
2000 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2001 $title = new TitleValue( 0, 'SomeDbKey' );
2003 $mockDb = $this->getMockDb();
2004 $mockDb->expects( $this->never() )
2005 ->method( 'selectRow' );
2007 $mockCache = $this->getMockCache();
2008 $mockCache->expects( $this->never() )->method( 'get' );
2009 $mockCache->expects( $this->never() )->method( 'set' );
2010 $mockCache->expects( $this->once() )
2011 ->method( 'delete' )
2012 ->with( '0:SomeDbKey:1' );
2014 $mockQueueGroup = $this->getMockJobQueueGroup();
2016 // We don't care if these methods actually do anything here
2017 $mockRevisionLookup = $this->getMockRevisionLookup( [
2018 'getRevisionByTitle' => function () {
2021 'getTimestampFromId' => function () {
2022 return '00000000000000';
2026 $store = $this->newWatchedItemStore( [
2028 'queueGroup' => $mockQueueGroup,
2029 'cache' => $mockCache,
2030 'revisionLookup' => $mockRevisionLookup,
2033 $mockQueueGroup->expects( $this->any() )
2034 ->method( 'lazyPush' )
2035 ->will( $this->returnCallback( function ( ActivityUpdateJob
$job ) {
2040 $store->resetNotificationTimestamp(
2048 private function verifyCallbackJob(
2049 ActivityUpdateJob
$job,
2050 LinkTarget
$expectedTitle,
2052 callable
$notificationTimestampCondition
2054 $this->assertEquals( $expectedTitle->getDBkey(), $job->getTitle()->getDBkey() );
2055 $this->assertEquals( $expectedTitle->getNamespace(), $job->getTitle()->getNamespace() );
2057 $jobParams = $job->getParams();
2058 $this->assertArrayHasKey( 'type', $jobParams );
2059 $this->assertEquals( 'updateWatchlistNotification', $jobParams['type'] );
2060 $this->assertArrayHasKey( 'userid', $jobParams );
2061 $this->assertEquals( $expectedUserId, $jobParams['userid'] );
2062 $this->assertArrayHasKey( 'notifTime', $jobParams );
2063 $this->assertTrue( $notificationTimestampCondition( $jobParams['notifTime'] ) );
2066 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
2067 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2069 $title = new TitleValue( 0, 'SomeTitle' );
2071 $mockDb = $this->getMockDb();
2072 $mockDb->expects( $this->never() )
2073 ->method( 'selectRow' );
2075 $mockCache = $this->getMockCache();
2076 $mockCache->expects( $this->never() )->method( 'get' );
2077 $mockCache->expects( $this->never() )->method( 'set' );
2078 $mockCache->expects( $this->once() )
2079 ->method( 'delete' )
2080 ->with( '0:SomeTitle:1' );
2082 $mockQueueGroup = $this->getMockJobQueueGroup();
2084 $mockRevisionRecord = $this->createMock( RevisionRecord
::class );
2085 $mockRevisionRecord->expects( $this->never() )->method( $this->anything() );
2087 $mockRevisionLookup = $this->getMockRevisionLookup( [
2088 'getTimestampFromId' => function () {
2089 return '00000000000000';
2091 'getRevisionById' => function ( $id, $flags ) use ( $oldid, $mockRevisionRecord ) {
2092 $this->assertSame( $oldid, $id );
2093 $this->assertSame( 0, $flags );
2094 return $mockRevisionRecord;
2096 'getNextRevision' =>
2097 function ( $oldRev, $titleArg ) use ( $mockRevisionRecord, $title ) {
2098 $this->assertSame( $mockRevisionRecord, $oldRev );
2099 $this->assertSame( $title, $titleArg );
2103 'getNextRevision' => 1,
2106 $store = $this->newWatchedItemStore( [
2108 'queueGroup' => $mockQueueGroup,
2109 'cache' => $mockCache,
2110 'revisionLookup' => $mockRevisionLookup,
2113 $mockQueueGroup->expects( $this->any() )
2114 ->method( 'lazyPush' )
2115 ->will( $this->returnCallback(
2116 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2117 $this->verifyCallbackJob(
2121 function ( $time ) {
2122 return $time === null;
2129 $store->resetNotificationTimestamp(
2138 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced() {
2139 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2141 $title = new TitleValue( 0, 'SomeDbKey' );
2143 $mockRevision = $this->createMock( RevisionRecord
::class );
2144 $mockRevision->expects( $this->never() )->method( $this->anything() );
2146 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2147 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2149 $mockDb = $this->getMockDb();
2150 $mockDb->expects( $this->once() )
2151 ->method( 'selectRow' )
2154 'wl_notificationtimestamp',
2157 'wl_namespace' => 0,
2158 'wl_title' => 'SomeDbKey',
2161 ->will( $this->returnValue(
2162 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2165 $mockCache = $this->getMockCache();
2166 $mockCache->expects( $this->never() )->method( 'get' );
2167 $mockCache->expects( $this->once() )
2169 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2170 $mockCache->expects( $this->once() )
2171 ->method( 'delete' )
2172 ->with( '0:SomeDbKey:1' );
2174 $mockQueueGroup = $this->getMockJobQueueGroup();
2176 $mockRevisionLookup = $this->getMockRevisionLookup(
2178 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2179 $this->assertSame( $oldid, $oldidParam );
2181 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2182 $this->assertSame( $oldid, $id );
2183 return $mockRevision;
2185 'getNextRevision' =>
2186 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2187 $this->assertSame( $mockRevision, $rev );
2188 return $mockNextRevision;
2192 'getTimestampFromId' => 2,
2193 'getRevisionById' => 1,
2194 'getNextRevision' => 1,
2197 $store = $this->newWatchedItemStore( [
2199 'queueGroup' => $mockQueueGroup,
2200 'cache' => $mockCache,
2201 'revisionLookup' => $mockRevisionLookup,
2204 $mockQueueGroup->expects( $this->any() )
2205 ->method( 'lazyPush' )
2206 ->will( $this->returnCallback(
2207 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2208 $this->verifyCallbackJob(
2212 function ( $time ) {
2213 return $time !== null && $time > '20151212010101';
2220 $store->resetNotificationTimestamp(
2229 public function testResetNotificationTimestamp_notWatchedPageForced() {
2230 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2232 $title = new TitleValue( 0, 'SomeDbKey' );
2234 $mockDb = $this->getMockDb();
2235 $mockDb->expects( $this->once() )
2236 ->method( 'selectRow' )
2239 'wl_notificationtimestamp',
2242 'wl_namespace' => 0,
2243 'wl_title' => 'SomeDbKey',
2246 ->will( $this->returnValue( false ) );
2248 $mockCache = $this->getMockCache();
2249 $mockCache->expects( $this->never() )->method( 'get' );
2250 $mockCache->expects( $this->never() )->method( 'set' );
2251 $mockCache->expects( $this->once() )
2252 ->method( 'delete' )
2253 ->with( '0:SomeDbKey:1' );
2255 $mockQueueGroup = $this->getMockJobQueueGroup();
2257 $mockRevision = $this->createMock( RevisionRecord
::class );
2258 $mockRevision->expects( $this->never() )->method( $this->anything() );
2260 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2261 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2263 $mockRevisionLookup = $this->getMockRevisionLookup(
2265 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2266 $this->assertSame( $oldid, $oldidParam );
2268 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2269 $this->assertSame( $oldid, $id );
2270 return $mockRevision;
2272 'getNextRevision' =>
2273 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2274 $this->assertSame( $mockRevision, $rev );
2275 return $mockNextRevision;
2279 'getTimestampFromId' => 1,
2280 'getRevisionById' => 1,
2281 'getNextRevision' => 1,
2285 $store = $this->newWatchedItemStore( [
2287 'queueGroup' => $mockQueueGroup,
2288 'cache' => $mockCache,
2289 'revisionLookup' => $mockRevisionLookup,
2292 $mockQueueGroup->expects( $this->any() )
2293 ->method( 'lazyPush' )
2294 ->will( $this->returnCallback(
2295 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2296 $this->verifyCallbackJob(
2300 function ( $time ) {
2301 return $time === null;
2308 $store->resetNotificationTimestamp(
2317 public function testResetNotificationTimestamp_futureNotificationTimestampForced() {
2318 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2320 $title = new TitleValue( 0, 'SomeDbKey' );
2322 $mockDb = $this->getMockDb();
2323 $mockDb->expects( $this->once() )
2324 ->method( 'selectRow' )
2327 'wl_notificationtimestamp',
2330 'wl_namespace' => 0,
2331 'wl_title' => 'SomeDbKey',
2334 ->will( $this->returnValue(
2335 $this->getFakeRow( [ 'wl_notificationtimestamp' => '30151212010101' ] )
2338 $mockCache = $this->getMockCache();
2339 $mockCache->expects( $this->never() )->method( 'get' );
2340 $mockCache->expects( $this->once() )
2342 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2343 $mockCache->expects( $this->once() )
2344 ->method( 'delete' )
2345 ->with( '0:SomeDbKey:1' );
2347 $mockQueueGroup = $this->getMockJobQueueGroup();
2349 $mockRevision = $this->createMock( RevisionRecord
::class );
2350 $mockRevision->expects( $this->never() )->method( $this->anything() );
2352 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2353 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2355 $mockRevisionLookup = $this->getMockRevisionLookup(
2357 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2358 $this->assertEquals( $oldid, $oldidParam );
2360 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2361 $this->assertSame( $oldid, $id );
2362 return $mockRevision;
2364 'getNextRevision' =>
2365 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2366 $this->assertSame( $mockRevision, $rev );
2367 return $mockNextRevision;
2371 'getTimestampFromId' => 2,
2372 'getRevisionById' => 1,
2373 'getNextRevision' => 1,
2377 $store = $this->newWatchedItemStore( [
2379 'queueGroup' => $mockQueueGroup,
2380 'cache' => $mockCache,
2381 'revisionLookup' => $mockRevisionLookup,
2384 $mockQueueGroup->expects( $this->any() )
2385 ->method( 'lazyPush' )
2386 ->will( $this->returnCallback(
2387 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2388 $this->verifyCallbackJob(
2392 function ( $time ) {
2393 return $time === '30151212010101';
2400 $store->resetNotificationTimestamp(
2409 public function testResetNotificationTimestamp_futureNotificationTimestampNotForced() {
2410 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2412 $title = new TitleValue( 0, 'SomeDbKey' );
2414 $mockDb = $this->getMockDb();
2415 $mockDb->expects( $this->once() )
2416 ->method( 'selectRow' )
2419 'wl_notificationtimestamp',
2422 'wl_namespace' => 0,
2423 'wl_title' => 'SomeDbKey',
2426 ->will( $this->returnValue(
2427 $this->getFakeRow( [ 'wl_notificationtimestamp' => '30151212010101' ] )
2430 $mockCache = $this->getMockCache();
2431 $mockCache->expects( $this->never() )->method( 'get' );
2432 $mockCache->expects( $this->once() )
2434 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2435 $mockCache->expects( $this->once() )
2436 ->method( 'delete' )
2437 ->with( '0:SomeDbKey:1' );
2439 $mockQueueGroup = $this->getMockJobQueueGroup();
2441 $mockRevision = $this->createMock( RevisionRecord
::class );
2442 $mockRevision->expects( $this->never() )->method( $this->anything() );
2444 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2445 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2447 $mockRevisionLookup = $this->getMockRevisionLookup(
2449 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2450 $this->assertEquals( $oldid, $oldidParam );
2452 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2453 $this->assertSame( $oldid, $id );
2454 return $mockRevision;
2456 'getNextRevision' =>
2457 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2458 $this->assertSame( $mockRevision, $rev );
2459 return $mockNextRevision;
2463 'getTimestampFromId' => 2,
2464 'getRevisionById' => 1,
2465 'getNextRevision' => 1,
2468 $store = $this->newWatchedItemStore( [
2470 'queueGroup' => $mockQueueGroup,
2471 'cache' => $mockCache,
2472 'revisionLookup' => $mockRevisionLookup,
2475 $mockQueueGroup->expects( $this->any() )
2476 ->method( 'lazyPush' )
2477 ->will( $this->returnCallback(
2478 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2479 $this->verifyCallbackJob(
2483 function ( $time ) {
2484 return $time === false;
2491 $store->resetNotificationTimestamp(
2500 public function testSetNotificationTimestampsForUser_anonUser() {
2501 $store = $this->newWatchedItemStore();
2502 $this->assertFalse( $store->setNotificationTimestampsForUser(
2503 new UserIdentityValue( 0, 'AnonUser', 0 ), '' ) );
2506 public function testSetNotificationTimestampsForUser_allRows() {
2507 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2508 $timestamp = '20100101010101';
2510 $store = $this->newWatchedItemStore();
2512 // Note: This does not actually assert the job is correct
2513 $callableCallCounter = 0;
2514 $mockCallback = function ( $callable ) use ( &$callableCallCounter ) {
2515 $callableCallCounter++
;
2516 $this->assertInternalType( 'callable', $callable );
2518 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
2521 $store->setNotificationTimestampsForUser( $user, $timestamp )
2523 $this->assertEquals( 1, $callableCallCounter );
2526 public function testSetNotificationTimestampsForUser_nullTimestamp() {
2527 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2530 $store = $this->newWatchedItemStore();
2532 // Note: This does not actually assert the job is correct
2533 $callableCallCounter = 0;
2534 $mockCallback = function ( $callable ) use ( &$callableCallCounter ) {
2535 $callableCallCounter++
;
2536 $this->assertInternalType( 'callable', $callable );
2538 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
2541 $store->setNotificationTimestampsForUser( $user, $timestamp )
2545 public function testSetNotificationTimestampsForUser_specificTargets() {
2546 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2547 $timestamp = '20100101010101';
2548 $targets = [ new TitleValue( 0, 'Foo' ), new TitleValue( 0, 'Bar' ) ];
2550 $mockDb = $this->getMockDb();
2551 $mockDb->expects( $this->once() )
2552 ->method( 'update' )
2555 [ 'wl_notificationtimestamp' => 'TS' . $timestamp . 'TS' ],
2556 [ 'wl_user' => 1, 'wl_namespace' => 0, 'wl_title' => [ 'Foo', 'Bar' ] ]
2558 ->will( $this->returnValue( true ) );
2559 $mockDb->expects( $this->exactly( 1 ) )
2560 ->method( 'timestamp' )
2561 ->will( $this->returnCallback( function ( $value ) {
2562 return 'TS' . $value . 'TS';
2564 $mockDb->expects( $this->once() )
2565 ->method( 'affectedRows' )
2566 ->will( $this->returnValue( 2 ) );
2568 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
2571 $store->setNotificationTimestampsForUser( $user, $timestamp, $targets )
2575 public function testUpdateNotificationTimestamp_watchersExist() {
2576 $mockDb = $this->getMockDb();
2577 $mockDb->expects( $this->once() )
2578 ->method( 'selectFieldValues' )
2584 'wl_namespace' => 0,
2585 'wl_title' => 'SomeDbKey',
2586 'wl_notificationtimestamp IS NULL'
2589 ->will( $this->returnValue( [ '2', '3' ] ) );
2590 $mockDb->expects( $this->once() )
2591 ->method( 'update' )
2594 [ 'wl_notificationtimestamp' => null ],
2596 'wl_user' => [ 2, 3 ],
2597 'wl_namespace' => 0,
2598 'wl_title' => 'SomeDbKey',
2602 $mockCache = $this->getMockCache();
2603 $mockCache->expects( $this->never() )->method( 'set' );
2604 $mockCache->expects( $this->never() )->method( 'get' );
2605 $mockCache->expects( $this->never() )->method( 'delete' );
2607 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2609 $this->assertEquals(
2611 $store->updateNotificationTimestamp(
2612 new UserIdentityValue( 1, 'MockUser', 0 ),
2613 new TitleValue( 0, 'SomeDbKey' ),
2619 public function testUpdateNotificationTimestamp_noWatchers() {
2620 $mockDb = $this->getMockDb();
2621 $mockDb->expects( $this->once() )
2622 ->method( 'selectFieldValues' )
2628 'wl_namespace' => 0,
2629 'wl_title' => 'SomeDbKey',
2630 'wl_notificationtimestamp IS NULL'
2634 $this->returnValue( [] )
2636 $mockDb->expects( $this->never() )
2637 ->method( 'update' );
2639 $mockCache = $this->getMockCache();
2640 $mockCache->expects( $this->never() )->method( 'set' );
2641 $mockCache->expects( $this->never() )->method( 'get' );
2642 $mockCache->expects( $this->never() )->method( 'delete' );
2644 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2646 $watchers = $store->updateNotificationTimestamp(
2647 new UserIdentityValue( 1, 'MockUser', 0 ),
2648 new TitleValue( 0, 'SomeDbKey' ),
2651 $this->assertInternalType( 'array', $watchers );
2652 $this->assertEmpty( $watchers );
2655 public function testUpdateNotificationTimestamp_clearsCachedItems() {
2656 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2657 $titleValue = new TitleValue( 0, 'SomeDbKey' );
2659 $mockDb = $this->getMockDb();
2660 $mockDb->expects( $this->once() )
2661 ->method( 'selectRow' )
2662 ->will( $this->returnValue(
2663 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2665 $mockDb->expects( $this->once() )
2666 ->method( 'selectFieldValues' )
2668 $this->returnValue( [ '2', '3' ] )
2670 $mockDb->expects( $this->once() )
2671 ->method( 'update' );
2673 $mockCache = $this->getMockCache();
2674 $mockCache->expects( $this->once() )
2676 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2677 $mockCache->expects( $this->once() )
2679 ->with( '0:SomeDbKey:1' );
2680 $mockCache->expects( $this->once() )
2681 ->method( 'delete' )
2682 ->with( '0:SomeDbKey:1' );
2684 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2686 // This will add the item to the cache
2687 $store->getWatchedItem( $user, $titleValue );
2689 $store->updateNotificationTimestamp(
2690 new UserIdentityValue( 1, 'MockUser', 0 ),