3 use MediaWiki\Linker\LinkTarget
;
4 use MediaWiki\Revision\RevisionLookup
;
5 use MediaWiki\Revision\RevisionRecord
;
6 use MediaWiki\User\UserIdentityValue
;
7 use Wikimedia\Rdbms\LBFactory
;
8 use Wikimedia\Rdbms\LoadBalancer
;
9 use Wikimedia\TestingAccessWrapper
;
14 * @covers WatchedItemStore
16 class WatchedItemStoreUnitTest
extends MediaWikiTestCase
{
19 * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
21 private function getMockDb() {
22 return $this->createMock( IDatabase
::class );
26 * @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
28 private function getMockLoadBalancer(
30 $expectedConnectionType = null
32 $mock = $this->getMockBuilder( LoadBalancer
::class )
33 ->disableOriginalConstructor()
35 if ( $expectedConnectionType !== null ) {
36 $mock->expects( $this->any() )
37 ->method( 'getConnectionRef' )
38 ->with( $expectedConnectionType )
39 ->will( $this->returnValue( $mockDb ) );
41 $mock->expects( $this->any() )
42 ->method( 'getConnectionRef' )
43 ->will( $this->returnValue( $mockDb ) );
49 * @return PHPUnit_Framework_MockObject_MockObject|LBFactory
51 private function getMockLBFactory(
53 $expectedConnectionType = null
55 $loadBalancer = $this->getMockLoadBalancer( $mockDb, $expectedConnectionType );
56 $mock = $this->getMockBuilder( LBFactory
::class )
57 ->disableOriginalConstructor()
59 $mock->expects( $this->any() )
60 ->method( 'getMainLB' )
61 ->will( $this->returnValue( $loadBalancer ) );
66 * @return PHPUnit_Framework_MockObject_MockObject|JobQueueGroup
68 private function getMockJobQueueGroup() {
69 $mock = $this->getMockBuilder( JobQueueGroup
::class )
70 ->disableOriginalConstructor()
72 $mock->expects( $this->any() )
74 ->will( $this->returnCallback( function ( Job
$job ) {
77 $mock->expects( $this->any() )
78 ->method( 'lazyPush' )
79 ->will( $this->returnCallback( function ( Job
$job ) {
86 * @return PHPUnit_Framework_MockObject_MockObject|HashBagOStuff
88 private function getMockCache() {
89 $mock = $this->getMockBuilder( HashBagOStuff
::class )
90 ->disableOriginalConstructor()
91 ->setMethods( [ 'get', 'set', 'delete', 'makeKey' ] )
93 $mock->expects( $this->any() )
95 ->will( $this->returnCallback( function ( ...$args ) {
96 return implode( ':', $args );
102 * @return PHPUnit_Framework_MockObject_MockObject|ReadOnlyMode
104 private function getMockReadOnlyMode( $readOnly = false ) {
105 $mock = $this->getMockBuilder( ReadOnlyMode
::class )
106 ->disableOriginalConstructor()
108 $mock->expects( $this->any() )
109 ->method( 'isReadOnly' )
110 ->will( $this->returnValue( $readOnly ) );
115 * Assumes that only getSubjectPage and getTalkPage will ever be called, and everything passed
116 * to them will have namespace 0.
118 private function getMockNsInfo() : NamespaceInfo
{
119 $mock = $this->createMock( NamespaceInfo
::class );
120 $mock->method( 'getSubjectPage' )->will( $this->returnArgument( 0 ) );
121 $mock->method( 'getTalkPage' )->will( $this->returnCallback(
122 function ( $target ) {
123 return new TitleValue( 1, $target->getDbKey() );
126 $mock->expects( $this->never() )
127 ->method( $this->anythingBut( 'getSubjectPage', 'getTalkPage' ) );
132 * No methods may be called except provided callbacks, if any.
134 * @param array $callbacks Keys are method names, values are callbacks
135 * @param array $counts Keys are method names, values are expected number of times to be called
136 * (default is any number is okay)
138 private function getMockRevisionLookup(
139 array $callbacks = [], array $counts = []
141 $mock = $this->createMock( RevisionLookup
::class );
142 foreach ( $callbacks as $method => $callback ) {
143 $count = isset( $counts[$method] ) ?
$this->exactly( $counts[$method] ) : $this->any();
144 $mock->expects( $count )
146 ->will( $this->returnCallback( $callbacks[$method] ) );
148 $mock->expects( $this->never() )
149 ->method( $this->anythingBut( ...array_keys( $callbacks ) ) );
153 private function getFakeRow( array $rowValues ) {
154 $fakeRow = new stdClass();
155 foreach ( $rowValues as $valueName => $value ) {
156 $fakeRow->$valueName = $value;
162 * @param array $mocks Associative array providing mocks to use when constructing the
163 * WatchedItemStore. Anything not provided will fall back to a default. Valid keys:
172 private function newWatchedItemStore( array $mocks = [] ) : WatchedItemStore
{
173 return new WatchedItemStore(
174 $mocks['lbFactory'] ??
175 $this->getMockLBFactory( $mocks['db'] ??
$this->getMockDb() ),
176 $mocks['queueGroup'] ??
$this->getMockJobQueueGroup(),
178 $mocks['cache'] ??
$this->getMockCache(),
179 $mocks['readOnlyMode'] ??
$this->getMockReadOnlyMode(),
181 $mocks['nsInfo'] ??
$this->getMockNsInfo(),
182 $mocks['revisionLookup'] ??
$this->getMockRevisionLookup()
186 public function testClearWatchedItems() {
187 $user = new UserIdentityValue( 7, 'MockUser', 0 );
189 $mockDb = $this->getMockDb();
190 $mockDb->expects( $this->once() )
191 ->method( 'selectField' )
196 'wl_user' => $user->getId(),
198 $this->isType( 'string' )
200 ->will( $this->returnValue( 12 ) );
201 $mockDb->expects( $this->once() )
206 $this->isType( 'string' )
209 $mockCache = $this->getMockCache();
210 $mockCache->expects( $this->never() )->method( 'get' );
211 $mockCache->expects( $this->never() )->method( 'set' );
212 $mockCache->expects( $this->once() )
216 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
217 TestingAccessWrapper
::newFromObject( $store )
218 ->cacheIndex
= [ 0 => [ 'F' => [ 7 => 'RM-KEY', 9 => 'KEEP-KEY' ] ] ];
220 $this->assertTrue( $store->clearUserWatchedItems( $user ) );
223 public function testClearWatchedItems_tooManyItemsWatched() {
224 $user = new UserIdentityValue( 7, 'MockUser', 0 );
226 $mockDb = $this->getMockDb();
227 $mockDb->expects( $this->once() )
228 ->method( 'selectField' )
233 'wl_user' => $user->getId(),
235 $this->isType( 'string' )
237 ->will( $this->returnValue( 99999 ) );
239 $mockCache = $this->getMockCache();
240 $mockCache->expects( $this->never() )->method( 'get' );
241 $mockCache->expects( $this->never() )->method( 'set' );
242 $mockCache->expects( $this->never() )->method( 'delete' );
244 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
246 $this->assertFalse( $store->clearUserWatchedItems( $user ) );
249 public function testCountWatchedItems() {
250 $user = new UserIdentityValue( 1, 'MockUser', 0 );
252 $mockDb = $this->getMockDb();
253 $mockDb->expects( $this->exactly( 1 ) )
254 ->method( 'selectField' )
259 'wl_user' => $user->getId(),
261 $this->isType( 'string' )
263 ->will( $this->returnValue( '12' ) );
265 $mockCache = $this->getMockCache();
266 $mockCache->expects( $this->never() )->method( 'get' );
267 $mockCache->expects( $this->never() )->method( 'set' );
268 $mockCache->expects( $this->never() )->method( 'delete' );
270 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
272 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
275 public function testCountWatchers() {
276 $titleValue = new TitleValue( 0, 'SomeDbKey' );
278 $mockDb = $this->getMockDb();
279 $mockDb->expects( $this->exactly( 1 ) )
280 ->method( 'selectField' )
285 'wl_namespace' => $titleValue->getNamespace(),
286 'wl_title' => $titleValue->getDBkey(),
288 $this->isType( 'string' )
290 ->will( $this->returnValue( '7' ) );
292 $mockCache = $this->getMockCache();
293 $mockCache->expects( $this->never() )->method( 'get' );
294 $mockCache->expects( $this->never() )->method( 'set' );
295 $mockCache->expects( $this->never() )->method( 'delete' );
297 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
299 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
302 public function testCountWatchersMultiple() {
304 new TitleValue( 0, 'SomeDbKey' ),
305 new TitleValue( 0, 'OtherDbKey' ),
306 new TitleValue( 1, 'AnotherDbKey' ),
309 $mockDb = $this->getMockDb();
312 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
313 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
314 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
317 $mockDb->expects( $this->once() )
318 ->method( 'makeWhereFrom2d' )
320 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
321 $this->isType( 'string' ),
322 $this->isType( 'string' )
324 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
325 $mockDb->expects( $this->once() )
329 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
330 [ 'makeWhereFrom2d return value' ],
331 $this->isType( 'string' ),
333 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
337 $this->returnValue( $dbResult )
340 $mockCache = $this->getMockCache();
341 $mockCache->expects( $this->never() )->method( 'get' );
342 $mockCache->expects( $this->never() )->method( 'set' );
343 $mockCache->expects( $this->never() )->method( 'delete' );
345 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
348 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
349 1 => [ 'AnotherDbKey' => 500 ],
351 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
354 public function provideIntWithDbUnsafeVersion() {
357 [ "50; DROP TABLE watchlist;\n--" ],
362 * @dataProvider provideIntWithDbUnsafeVersion
364 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
366 new TitleValue( 0, 'SomeDbKey' ),
367 new TitleValue( 0, 'OtherDbKey' ),
368 new TitleValue( 1, 'AnotherDbKey' ),
371 $mockDb = $this->getMockDb();
374 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
375 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
376 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
379 $mockDb->expects( $this->once() )
380 ->method( 'makeWhereFrom2d' )
382 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
383 $this->isType( 'string' ),
384 $this->isType( 'string' )
386 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
387 $mockDb->expects( $this->once() )
391 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
392 [ 'makeWhereFrom2d return value' ],
393 $this->isType( 'string' ),
395 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
396 'HAVING' => 'COUNT(*) >= 50',
400 $this->returnValue( $dbResult )
403 $mockCache = $this->getMockCache();
404 $mockCache->expects( $this->never() )->method( 'get' );
405 $mockCache->expects( $this->never() )->method( 'set' );
406 $mockCache->expects( $this->never() )->method( 'delete' );
408 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
411 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
412 1 => [ 'AnotherDbKey' => 500 ],
416 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
420 public function testCountVisitingWatchers() {
421 $titleValue = new TitleValue( 0, 'SomeDbKey' );
423 $mockDb = $this->getMockDb();
424 $mockDb->expects( $this->exactly( 1 ) )
425 ->method( 'selectField' )
430 'wl_namespace' => $titleValue->getNamespace(),
431 'wl_title' => $titleValue->getDBkey(),
432 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
434 $this->isType( 'string' )
436 ->will( $this->returnValue( '7' ) );
437 $mockDb->expects( $this->exactly( 1 ) )
438 ->method( 'addQuotes' )
439 ->will( $this->returnCallback( function ( $value ) {
442 $mockDb->expects( $this->exactly( 1 ) )
443 ->method( 'timestamp' )
444 ->will( $this->returnCallback( function ( $value ) {
445 return 'TS' . $value . 'TS';
448 $mockCache = $this->getMockCache();
449 $mockCache->expects( $this->never() )->method( 'set' );
450 $mockCache->expects( $this->never() )->method( 'get' );
451 $mockCache->expects( $this->never() )->method( 'delete' );
453 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
455 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
458 public function testCountVisitingWatchersMultiple() {
459 $titleValuesWithThresholds = [
460 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
461 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
462 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
466 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
467 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
469 [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
472 $mockDb = $this->getMockDb();
473 $mockDb->expects( $this->exactly( 2 * 3 ) )
474 ->method( 'addQuotes' )
475 ->will( $this->returnCallback( function ( $value ) {
478 $mockDb->expects( $this->exactly( 3 ) )
479 ->method( 'timestamp' )
480 ->will( $this->returnCallback( function ( $value ) {
481 return 'TS' . $value . 'TS';
483 $mockDb->expects( $this->any() )
484 ->method( 'makeList' )
486 $this->isType( 'array' ),
487 $this->isType( 'int' )
489 ->will( $this->returnCallback( function ( $a, $conj ) {
490 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
491 return implode( $sqlConj, array_map( function ( $s ) {
492 return '(' . $s . ')';
496 $mockDb->expects( $this->never() )
497 ->method( 'makeWhereFrom2d' );
500 '((wl_namespace = 0) AND (' .
501 "(((wl_title = 'SomeDbKey') AND (" .
502 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
504 "(wl_title = 'OtherDbKey') AND (" .
505 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
507 ') OR ((wl_namespace = 1) AND (' .
508 "(((wl_title = 'AnotherDbKey') AND (" .
509 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
511 $mockDb->expects( $this->once() )
515 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
517 $this->isType( 'string' ),
519 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
523 $this->returnValue( $dbResult )
526 $mockCache = $this->getMockCache();
527 $mockCache->expects( $this->never() )->method( 'get' );
528 $mockCache->expects( $this->never() )->method( 'set' );
529 $mockCache->expects( $this->never() )->method( 'delete' );
531 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
534 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
535 1 => [ 'AnotherDbKey' => 500 ],
539 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
543 public function testCountVisitingWatchersMultiple_withMissingTargets() {
544 $titleValuesWithThresholds = [
545 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
546 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
547 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
548 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
549 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
553 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
554 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
556 [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
559 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '100' ]
562 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '200' ]
565 $mockDb = $this->getMockDb();
566 $mockDb->expects( $this->exactly( 2 * 3 ) )
567 ->method( 'addQuotes' )
568 ->will( $this->returnCallback( function ( $value ) {
571 $mockDb->expects( $this->exactly( 3 ) )
572 ->method( 'timestamp' )
573 ->will( $this->returnCallback( function ( $value ) {
574 return 'TS' . $value . 'TS';
576 $mockDb->expects( $this->any() )
577 ->method( 'makeList' )
579 $this->isType( 'array' ),
580 $this->isType( 'int' )
582 ->will( $this->returnCallback( function ( $a, $conj ) {
583 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
584 return implode( $sqlConj, array_map( function ( $s ) {
585 return '(' . $s . ')';
589 $mockDb->expects( $this->once() )
590 ->method( 'makeWhereFrom2d' )
592 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
593 $this->isType( 'string' ),
594 $this->isType( 'string' )
596 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
599 '((wl_namespace = 0) AND (' .
600 "(((wl_title = 'SomeDbKey') AND (" .
601 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
603 "(wl_title = 'OtherDbKey') AND (" .
604 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
606 ') OR ((wl_namespace = 1) AND (' .
607 "(((wl_title = 'AnotherDbKey') AND (" .
608 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
611 '(makeWhereFrom2d return value)';
612 $mockDb->expects( $this->once() )
616 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
618 $this->isType( 'string' ),
620 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
624 $this->returnValue( $dbResult )
627 $mockCache = $this->getMockCache();
628 $mockCache->expects( $this->never() )->method( 'get' );
629 $mockCache->expects( $this->never() )->method( 'set' );
630 $mockCache->expects( $this->never() )->method( 'delete' );
632 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
636 'SomeDbKey' => 100, 'OtherDbKey' => 300,
637 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
639 1 => [ 'AnotherDbKey' => 500 ],
643 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
648 * @dataProvider provideIntWithDbUnsafeVersion
650 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
651 $titleValuesWithThresholds = [
652 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
653 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
654 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
657 $mockDb = $this->getMockDb();
658 $mockDb->expects( $this->any() )
659 ->method( 'makeList' )
660 ->will( $this->returnValue( 'makeList return value' ) );
661 $mockDb->expects( $this->once() )
665 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
666 'makeList return value',
667 $this->isType( 'string' ),
669 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
670 'HAVING' => 'COUNT(*) >= 50',
674 $this->returnValue( [] )
677 $mockCache = $this->getMockCache();
678 $mockCache->expects( $this->never() )->method( 'get' );
679 $mockCache->expects( $this->never() )->method( 'set' );
680 $mockCache->expects( $this->never() )->method( 'delete' );
682 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
685 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
686 1 => [ 'AnotherDbKey' => 0 ],
690 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
694 public function testCountUnreadNotifications() {
695 $user = new UserIdentityValue( 1, 'MockUser', 0 );
697 $mockDb = $this->getMockDb();
698 $mockDb->expects( $this->exactly( 1 ) )
699 ->method( 'selectRowCount' )
704 "wl_notificationtimestamp IS NOT NULL",
707 $this->isType( 'string' )
709 ->will( $this->returnValue( '9' ) );
711 $mockCache = $this->getMockCache();
712 $mockCache->expects( $this->never() )->method( 'set' );
713 $mockCache->expects( $this->never() )->method( 'get' );
714 $mockCache->expects( $this->never() )->method( 'delete' );
716 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
718 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
722 * @dataProvider provideIntWithDbUnsafeVersion
724 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
725 $user = new UserIdentityValue( 1, 'MockUser', 0 );
727 $mockDb = $this->getMockDb();
728 $mockDb->expects( $this->exactly( 1 ) )
729 ->method( 'selectRowCount' )
734 "wl_notificationtimestamp IS NOT NULL",
737 $this->isType( 'string' ),
740 ->will( $this->returnValue( '50' ) );
742 $mockCache = $this->getMockCache();
743 $mockCache->expects( $this->never() )->method( 'set' );
744 $mockCache->expects( $this->never() )->method( 'get' );
745 $mockCache->expects( $this->never() )->method( 'delete' );
747 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
751 $store->countUnreadNotifications( $user, $limit )
756 * @dataProvider provideIntWithDbUnsafeVersion
758 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
759 $user = new UserIdentityValue( 1, 'MockUser', 0 );
761 $mockDb = $this->getMockDb();
762 $mockDb->expects( $this->exactly( 1 ) )
763 ->method( 'selectRowCount' )
768 "wl_notificationtimestamp IS NOT NULL",
771 $this->isType( 'string' ),
774 ->will( $this->returnValue( '9' ) );
776 $mockCache = $this->getMockCache();
777 $mockCache->expects( $this->never() )->method( 'set' );
778 $mockCache->expects( $this->never() )->method( 'get' );
779 $mockCache->expects( $this->never() )->method( 'delete' );
781 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
785 $store->countUnreadNotifications( $user, $limit )
789 public function testDuplicateEntry_nothingToDuplicate() {
790 $mockDb = $this->getMockDb();
791 $mockDb->expects( $this->once() )
797 'wl_notificationtimestamp',
801 'wl_title' => 'Old_Title',
803 'WatchedItemStore::duplicateEntry',
806 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
808 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
810 $store->duplicateEntry(
811 new TitleValue( 0, 'Old_Title' ),
812 new TitleValue( 0, 'New_Title' )
816 public function testDuplicateEntry_somethingToDuplicate() {
818 $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
819 $this->getFakeRow( [ 'wl_user' => '2', 'wl_notificationtimestamp' => null ] ),
822 $mockDb = $this->getMockDb();
823 $mockDb->expects( $this->at( 0 ) )
829 'wl_notificationtimestamp',
833 'wl_title' => 'Old_Title',
836 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
837 $mockDb->expects( $this->at( 1 ) )
838 ->method( 'replace' )
841 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
846 'wl_title' => 'New_Title',
847 'wl_notificationtimestamp' => '20151212010101',
852 'wl_title' => 'New_Title',
853 'wl_notificationtimestamp' => null,
856 $this->isType( 'string' )
859 $mockCache = $this->getMockCache();
860 $mockCache->expects( $this->never() )->method( 'get' );
861 $mockCache->expects( $this->never() )->method( 'delete' );
863 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
865 $store->duplicateEntry(
866 new TitleValue( 0, 'Old_Title' ),
867 new TitleValue( 0, 'New_Title' )
871 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
872 $mockDb = $this->getMockDb();
873 $mockDb->expects( $this->at( 0 ) )
879 'wl_notificationtimestamp',
883 'wl_title' => 'Old_Title',
886 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
887 $mockDb->expects( $this->at( 1 ) )
893 'wl_notificationtimestamp',
897 'wl_title' => 'Old_Title',
900 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
902 $mockCache = $this->getMockCache();
903 $mockCache->expects( $this->never() )->method( 'get' );
904 $mockCache->expects( $this->never() )->method( 'delete' );
906 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
908 $store->duplicateAllAssociatedEntries(
909 new TitleValue( 0, 'Old_Title' ),
910 new TitleValue( 0, 'New_Title' )
914 public function provideLinkTargetPairs() {
916 [ new TitleValue( 0, 'Old_Title' ), new TitleValue( 0, 'New_Title' ) ],
917 [ new TitleValue( 0, 'Old_Title' ), new TitleValue( 0, 'New_Title' ) ],
922 * @dataProvider provideLinkTargetPairs
924 public function testDuplicateAllAssociatedEntries_somethingToDuplicate(
925 LinkTarget
$oldTarget,
926 LinkTarget
$newTarget
929 $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
932 $mockDb = $this->getMockDb();
933 $mockDb->expects( $this->at( 0 ) )
939 'wl_notificationtimestamp',
942 'wl_namespace' => $oldTarget->getNamespace(),
943 'wl_title' => $oldTarget->getDBkey(),
946 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
947 $mockDb->expects( $this->at( 1 ) )
948 ->method( 'replace' )
951 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
955 'wl_namespace' => $newTarget->getNamespace(),
956 'wl_title' => $newTarget->getDBkey(),
957 'wl_notificationtimestamp' => '20151212010101',
960 $this->isType( 'string' )
962 $mockDb->expects( $this->at( 2 ) )
968 'wl_notificationtimestamp',
971 'wl_namespace' => $oldTarget->getNamespace() +
1,
972 'wl_title' => $oldTarget->getDBkey(),
975 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
976 $mockDb->expects( $this->at( 3 ) )
977 ->method( 'replace' )
980 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
984 'wl_namespace' => $newTarget->getNamespace() +
1,
985 'wl_title' => $newTarget->getDBkey(),
986 'wl_notificationtimestamp' => '20151212010101',
989 $this->isType( 'string' )
992 $mockCache = $this->getMockCache();
993 $mockCache->expects( $this->never() )->method( 'get' );
994 $mockCache->expects( $this->never() )->method( 'delete' );
996 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
998 $store->duplicateAllAssociatedEntries(
1004 public function testAddWatch_nonAnonymousUser() {
1005 $mockDb = $this->getMockDb();
1006 $mockDb->expects( $this->once() )
1007 ->method( 'insert' )
1013 'wl_namespace' => 0,
1014 'wl_title' => 'Some_Page',
1015 'wl_notificationtimestamp' => null,
1020 $mockCache = $this->getMockCache();
1021 $mockCache->expects( $this->once() )
1022 ->method( 'delete' )
1023 ->with( '0:Some_Page:1' );
1025 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1028 new UserIdentityValue( 1, 'MockUser', 0 ),
1029 new TitleValue( 0, 'Some_Page' )
1033 public function testAddWatch_anonymousUser() {
1034 $mockDb = $this->getMockDb();
1035 $mockDb->expects( $this->never() )
1036 ->method( 'insert' );
1038 $mockCache = $this->getMockCache();
1039 $mockCache->expects( $this->never() )
1040 ->method( 'delete' );
1042 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1045 new UserIdentityValue( 0, 'AnonUser', 0 ),
1046 new TitleValue( 0, 'Some_Page' )
1050 public function testAddWatchBatchForUser_readOnlyDBReturnsFalse() {
1051 $store = $this->newWatchedItemStore(
1052 [ 'readOnlyMode' => $this->getMockReadOnlyMode( true ) ] );
1055 $store->addWatchBatchForUser(
1056 new UserIdentityValue( 1, 'MockUser', 0 ),
1057 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1062 public function testAddWatchBatchForUser_nonAnonymousUser() {
1063 $mockDb = $this->getMockDb();
1064 $mockDb->expects( $this->once() )
1065 ->method( 'insert' )
1071 'wl_namespace' => 0,
1072 'wl_title' => 'Some_Page',
1073 'wl_notificationtimestamp' => null,
1077 'wl_namespace' => 1,
1078 'wl_title' => 'Some_Page',
1079 'wl_notificationtimestamp' => null,
1084 $mockDb->expects( $this->once() )
1085 ->method( 'affectedRows' )
1088 $mockCache = $this->getMockCache();
1089 $mockCache->expects( $this->exactly( 2 ) )
1090 ->method( 'delete' );
1091 $mockCache->expects( $this->at( 1 ) )
1092 ->method( 'delete' )
1093 ->with( '0:Some_Page:1' );
1094 $mockCache->expects( $this->at( 3 ) )
1095 ->method( 'delete' )
1096 ->with( '1:Some_Page:1' );
1098 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1100 $mockUser = new UserIdentityValue( 1, 'MockUser', 0 );
1103 $store->addWatchBatchForUser(
1105 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1110 public function testAddWatchBatchForUser_anonymousUsersAreSkipped() {
1111 $mockDb = $this->getMockDb();
1112 $mockDb->expects( $this->never() )
1113 ->method( 'insert' );
1115 $mockCache = $this->getMockCache();
1116 $mockCache->expects( $this->never() )
1117 ->method( 'delete' );
1119 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1122 $store->addWatchBatchForUser(
1123 new UserIdentityValue( 0, 'AnonUser', 0 ),
1124 [ new TitleValue( 0, 'Other_Page' ) ]
1129 public function testAddWatchBatchReturnsTrue_whenGivenEmptyList() {
1130 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1131 $mockDb = $this->getMockDb();
1132 $mockDb->expects( $this->never() )
1133 ->method( 'insert' );
1135 $mockCache = $this->getMockCache();
1136 $mockCache->expects( $this->never() )
1137 ->method( 'delete' );
1139 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1142 $store->addWatchBatchForUser( $user, [] )
1146 public function testLoadWatchedItem_existingItem() {
1147 $mockDb = $this->getMockDb();
1148 $mockDb->expects( $this->once() )
1149 ->method( 'selectRow' )
1152 'wl_notificationtimestamp',
1155 'wl_namespace' => 0,
1156 'wl_title' => 'SomeDbKey',
1159 ->will( $this->returnValue(
1160 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1163 $mockCache = $this->getMockCache();
1164 $mockCache->expects( $this->once() )
1170 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1172 $watchedItem = $store->loadWatchedItem(
1173 new UserIdentityValue( 1, 'MockUser', 0 ),
1174 new TitleValue( 0, 'SomeDbKey' )
1176 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1177 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1178 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1179 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1182 public function testLoadWatchedItem_noItem() {
1183 $mockDb = $this->getMockDb();
1184 $mockDb->expects( $this->once() )
1185 ->method( 'selectRow' )
1188 'wl_notificationtimestamp',
1191 'wl_namespace' => 0,
1192 'wl_title' => 'SomeDbKey',
1195 ->will( $this->returnValue( [] ) );
1197 $mockCache = $this->getMockCache();
1198 $mockCache->expects( $this->never() )->method( 'get' );
1199 $mockCache->expects( $this->never() )->method( 'delete' );
1201 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1204 $store->loadWatchedItem(
1205 new UserIdentityValue( 1, 'MockUser', 0 ),
1206 new TitleValue( 0, 'SomeDbKey' )
1211 public function testLoadWatchedItem_anonymousUser() {
1212 $mockDb = $this->getMockDb();
1213 $mockDb->expects( $this->never() )
1214 ->method( 'selectRow' );
1216 $mockCache = $this->getMockCache();
1217 $mockCache->expects( $this->never() )->method( 'get' );
1218 $mockCache->expects( $this->never() )->method( 'delete' );
1220 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1223 $store->loadWatchedItem(
1224 new UserIdentityValue( 0, 'AnonUser', 0 ),
1225 new TitleValue( 0, 'SomeDbKey' )
1230 public function testRemoveWatch_existingItem() {
1231 $mockDb = $this->getMockDb();
1232 $mockDb->expects( $this->once() )
1233 ->method( 'delete' )
1239 'wl_namespace' => 0,
1240 'wl_title' => [ 'SomeDbKey' ],
1247 'wl_namespace' => 1,
1248 'wl_title' => [ 'SomeDbKey' ],
1252 $mockDb->expects( $this->exactly( 1 ) )
1253 ->method( 'affectedRows' )
1256 $mockCache = $this->getMockCache();
1257 $mockCache->expects( $this->never() )->method( 'get' );
1258 $mockCache->expects( $this->once() )
1259 ->method( 'delete' )
1261 [ '0:SomeDbKey:1' ],
1265 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1268 $store->removeWatch(
1269 new UserIdentityValue( 1, 'MockUser', 0 ),
1270 new TitleValue( 0, 'SomeDbKey' )
1275 public function testRemoveWatch_noItem() {
1276 $mockDb = $this->getMockDb();
1277 $mockDb->expects( $this->once() )
1278 ->method( 'delete' )
1284 'wl_namespace' => 0,
1285 'wl_title' => [ 'SomeDbKey' ],
1292 'wl_namespace' => 1,
1293 'wl_title' => [ 'SomeDbKey' ],
1298 $mockDb->expects( $this->once() )
1299 ->method( 'affectedRows' )
1302 $mockCache = $this->getMockCache();
1303 $mockCache->expects( $this->never() )->method( 'get' );
1304 $mockCache->expects( $this->once() )
1305 ->method( 'delete' )
1307 [ '0:SomeDbKey:1' ],
1311 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1314 $store->removeWatch(
1315 new UserIdentityValue( 1, 'MockUser', 0 ),
1316 new TitleValue( 0, 'SomeDbKey' )
1321 public function testRemoveWatch_anonymousUser() {
1322 $mockDb = $this->getMockDb();
1323 $mockDb->expects( $this->never() )
1324 ->method( 'delete' );
1326 $mockCache = $this->getMockCache();
1327 $mockCache->expects( $this->never() )->method( 'get' );
1328 $mockCache->expects( $this->never() )
1329 ->method( 'delete' );
1331 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1334 $store->removeWatch(
1335 new UserIdentityValue( 0, 'AnonUser', 0 ),
1336 new TitleValue( 0, 'SomeDbKey' )
1341 public function testGetWatchedItem_existingItem() {
1342 $mockDb = $this->getMockDb();
1343 $mockDb->expects( $this->once() )
1344 ->method( 'selectRow' )
1347 'wl_notificationtimestamp',
1350 'wl_namespace' => 0,
1351 'wl_title' => 'SomeDbKey',
1354 ->will( $this->returnValue(
1355 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1358 $mockCache = $this->getMockCache();
1359 $mockCache->expects( $this->never() )->method( 'delete' );
1360 $mockCache->expects( $this->once() )
1365 ->will( $this->returnValue( null ) );
1366 $mockCache->expects( $this->once() )
1372 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1374 $watchedItem = $store->getWatchedItem(
1375 new UserIdentityValue( 1, 'MockUser', 0 ),
1376 new TitleValue( 0, 'SomeDbKey' )
1378 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1379 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1380 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1381 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1384 public function testGetWatchedItem_cachedItem() {
1385 $mockDb = $this->getMockDb();
1386 $mockDb->expects( $this->never() )
1387 ->method( 'selectRow' );
1389 $mockUser = new UserIdentityValue( 1, 'MockUser', 0 );
1390 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1391 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1393 $mockCache = $this->getMockCache();
1394 $mockCache->expects( $this->never() )->method( 'delete' );
1395 $mockCache->expects( $this->never() )->method( 'set' );
1396 $mockCache->expects( $this->once() )
1401 ->will( $this->returnValue( $cachedItem ) );
1403 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1405 $this->assertEquals(
1407 $store->getWatchedItem(
1414 public function testGetWatchedItem_noItem() {
1415 $mockDb = $this->getMockDb();
1416 $mockDb->expects( $this->once() )
1417 ->method( 'selectRow' )
1420 'wl_notificationtimestamp',
1423 'wl_namespace' => 0,
1424 'wl_title' => 'SomeDbKey',
1427 ->will( $this->returnValue( [] ) );
1429 $mockCache = $this->getMockCache();
1430 $mockCache->expects( $this->never() )->method( 'set' );
1431 $mockCache->expects( $this->never() )->method( 'delete' );
1432 $mockCache->expects( $this->once() )
1434 ->with( '0:SomeDbKey:1' )
1435 ->will( $this->returnValue( false ) );
1437 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1440 $store->getWatchedItem(
1441 new UserIdentityValue( 1, 'MockUser', 0 ),
1442 new TitleValue( 0, 'SomeDbKey' )
1447 public function testGetWatchedItem_anonymousUser() {
1448 $mockDb = $this->getMockDb();
1449 $mockDb->expects( $this->never() )
1450 ->method( 'selectRow' );
1452 $mockCache = $this->getMockCache();
1453 $mockCache->expects( $this->never() )->method( 'set' );
1454 $mockCache->expects( $this->never() )->method( 'get' );
1455 $mockCache->expects( $this->never() )->method( 'delete' );
1457 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1460 $store->getWatchedItem(
1461 new UserIdentityValue( 0, 'AnonUser', 0 ),
1462 new TitleValue( 0, 'SomeDbKey' )
1467 public function testGetWatchedItemsForUser() {
1468 $mockDb = $this->getMockDb();
1469 $mockDb->expects( $this->once() )
1470 ->method( 'select' )
1473 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1476 ->will( $this->returnValue( [
1477 $this->getFakeRow( [
1478 'wl_namespace' => 0,
1479 'wl_title' => 'Foo1',
1480 'wl_notificationtimestamp' => '20151212010101',
1482 $this->getFakeRow( [
1483 'wl_namespace' => 1,
1484 'wl_title' => 'Foo2',
1485 'wl_notificationtimestamp' => null,
1489 $mockCache = $this->getMockCache();
1490 $mockCache->expects( $this->never() )->method( 'delete' );
1491 $mockCache->expects( $this->never() )->method( 'get' );
1492 $mockCache->expects( $this->never() )->method( 'set' );
1494 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1495 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1497 $watchedItems = $store->getWatchedItemsForUser( $user );
1499 $this->assertInternalType( 'array', $watchedItems );
1500 $this->assertCount( 2, $watchedItems );
1501 foreach ( $watchedItems as $watchedItem ) {
1502 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1504 $this->assertEquals(
1505 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1508 $this->assertEquals(
1509 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1514 public function provideDbTypes() {
1516 [ false, DB_REPLICA
],
1517 [ true, DB_MASTER
],
1522 * @dataProvider provideDbTypes
1524 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1525 $mockDb = $this->getMockDb();
1526 $mockCache = $this->getMockCache();
1527 $mockLoadBalancer = $this->getMockLBFactory( $mockDb, $dbType );
1528 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1530 $mockDb->expects( $this->once() )
1531 ->method( 'select' )
1534 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1536 $this->isType( 'string' ),
1537 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1539 ->will( $this->returnValue( [] ) );
1541 $store = $this->newWatchedItemStore(
1542 [ 'lbFactory' => $mockLoadBalancer, 'cache' => $mockCache ] );
1544 $watchedItems = $store->getWatchedItemsForUser(
1546 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore
::SORT_ASC
]
1548 $this->assertEquals( [], $watchedItems );
1551 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
1552 $store = $this->newWatchedItemStore();
1554 $this->setExpectedException( InvalidArgumentException
::class );
1555 $store->getWatchedItemsForUser(
1556 new UserIdentityValue( 1, 'MockUser', 0 ),
1561 public function testIsWatchedItem_existingItem() {
1562 $mockDb = $this->getMockDb();
1563 $mockDb->expects( $this->once() )
1564 ->method( 'selectRow' )
1567 'wl_notificationtimestamp',
1570 'wl_namespace' => 0,
1571 'wl_title' => 'SomeDbKey',
1574 ->will( $this->returnValue(
1575 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1578 $mockCache = $this->getMockCache();
1579 $mockCache->expects( $this->never() )->method( 'delete' );
1580 $mockCache->expects( $this->once() )
1582 ->with( '0:SomeDbKey:1' )
1583 ->will( $this->returnValue( false ) );
1584 $mockCache->expects( $this->once() )
1590 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1594 new UserIdentityValue( 1, 'MockUser', 0 ),
1595 new TitleValue( 0, 'SomeDbKey' )
1600 public function testIsWatchedItem_noItem() {
1601 $mockDb = $this->getMockDb();
1602 $mockDb->expects( $this->once() )
1603 ->method( 'selectRow' )
1606 'wl_notificationtimestamp',
1609 'wl_namespace' => 0,
1610 'wl_title' => 'SomeDbKey',
1613 ->will( $this->returnValue( [] ) );
1615 $mockCache = $this->getMockCache();
1616 $mockCache->expects( $this->never() )->method( 'set' );
1617 $mockCache->expects( $this->never() )->method( 'delete' );
1618 $mockCache->expects( $this->once() )
1620 ->with( '0:SomeDbKey:1' )
1621 ->will( $this->returnValue( false ) );
1623 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1627 new UserIdentityValue( 1, 'MockUser', 0 ),
1628 new TitleValue( 0, 'SomeDbKey' )
1633 public function testIsWatchedItem_anonymousUser() {
1634 $mockDb = $this->getMockDb();
1635 $mockDb->expects( $this->never() )
1636 ->method( 'selectRow' );
1638 $mockCache = $this->getMockCache();
1639 $mockCache->expects( $this->never() )->method( 'set' );
1640 $mockCache->expects( $this->never() )->method( 'get' );
1641 $mockCache->expects( $this->never() )->method( 'delete' );
1643 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1647 new UserIdentityValue( 0, 'AnonUser', 0 ),
1648 new TitleValue( 0, 'SomeDbKey' )
1653 public function testGetNotificationTimestampsBatch() {
1655 new TitleValue( 0, 'SomeDbKey' ),
1656 new TitleValue( 1, 'AnotherDbKey' ),
1659 $mockDb = $this->getMockDb();
1661 $this->getFakeRow( [
1662 'wl_namespace' => '0',
1663 'wl_title' => 'SomeDbKey',
1664 'wl_notificationtimestamp' => '20151212010101',
1668 'wl_namespace' => '1',
1669 'wl_title' => 'AnotherDbKey',
1670 'wl_notificationtimestamp' => null,
1675 $mockDb->expects( $this->once() )
1676 ->method( 'makeWhereFrom2d' )
1678 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1679 $this->isType( 'string' ),
1680 $this->isType( 'string' )
1682 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1683 $mockDb->expects( $this->once() )
1684 ->method( 'select' )
1687 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1689 'makeWhereFrom2d return value',
1692 $this->isType( 'string' )
1694 ->will( $this->returnValue( $dbResult ) );
1696 $mockCache = $this->getMockCache();
1697 $mockCache->expects( $this->exactly( 2 ) )
1700 [ '0:SomeDbKey:1' ],
1701 [ '1:AnotherDbKey:1' ]
1703 ->will( $this->returnValue( null ) );
1704 $mockCache->expects( $this->never() )->method( 'set' );
1705 $mockCache->expects( $this->never() )->method( 'delete' );
1707 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1709 $this->assertEquals(
1711 0 => [ 'SomeDbKey' => '20151212010101', ],
1712 1 => [ 'AnotherDbKey' => null, ],
1714 $store->getNotificationTimestampsBatch(
1715 new UserIdentityValue( 1, 'MockUser', 0 ), $targets )
1719 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1721 new TitleValue( 0, 'OtherDbKey' ),
1724 $mockDb = $this->getMockDb();
1726 $mockDb->expects( $this->once() )
1727 ->method( 'makeWhereFrom2d' )
1729 [ [ 'OtherDbKey' => 1 ] ],
1730 $this->isType( 'string' ),
1731 $this->isType( 'string' )
1733 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1734 $mockDb->expects( $this->once() )
1735 ->method( 'select' )
1738 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1740 'makeWhereFrom2d return value',
1743 $this->isType( 'string' )
1745 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1747 $mockCache = $this->getMockCache();
1748 $mockCache->expects( $this->once() )
1750 ->with( '0:OtherDbKey:1' )
1751 ->will( $this->returnValue( null ) );
1752 $mockCache->expects( $this->never() )->method( 'set' );
1753 $mockCache->expects( $this->never() )->method( 'delete' );
1755 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1757 $this->assertEquals(
1759 0 => [ 'OtherDbKey' => false, ],
1761 $store->getNotificationTimestampsBatch(
1762 new UserIdentityValue( 1, 'MockUser', 0 ), $targets )
1766 public function testGetNotificationTimestampsBatch_cachedItem() {
1768 new TitleValue( 0, 'SomeDbKey' ),
1769 new TitleValue( 1, 'AnotherDbKey' ),
1772 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1773 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1775 $mockDb = $this->getMockDb();
1777 $mockDb->expects( $this->once() )
1778 ->method( 'makeWhereFrom2d' )
1780 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1781 $this->isType( 'string' ),
1782 $this->isType( 'string' )
1784 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1785 $mockDb->expects( $this->once() )
1786 ->method( 'select' )
1789 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1791 'makeWhereFrom2d return value',
1794 $this->isType( 'string' )
1796 ->will( $this->returnValue( [
1798 [ 'wl_namespace' => '1', 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1802 $mockCache = $this->getMockCache();
1803 $mockCache->expects( $this->at( 1 ) )
1805 ->with( '0:SomeDbKey:1' )
1806 ->will( $this->returnValue( $cachedItem ) );
1807 $mockCache->expects( $this->at( 3 ) )
1809 ->with( '1:AnotherDbKey:1' )
1810 ->will( $this->returnValue( null ) );
1811 $mockCache->expects( $this->never() )->method( 'set' );
1812 $mockCache->expects( $this->never() )->method( 'delete' );
1814 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1816 $this->assertEquals(
1818 0 => [ 'SomeDbKey' => '20151212010101', ],
1819 1 => [ 'AnotherDbKey' => null, ],
1821 $store->getNotificationTimestampsBatch( $user, $targets )
1825 public function testGetNotificationTimestampsBatch_allItemsCached() {
1827 new TitleValue( 0, 'SomeDbKey' ),
1828 new TitleValue( 1, 'AnotherDbKey' ),
1831 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1833 new WatchedItem( $user, $targets[0], '20151212010101' ),
1834 new WatchedItem( $user, $targets[1], null ),
1836 $mockDb = $this->getMockDb();
1837 $mockDb->expects( $this->never() )->method( $this->anything() );
1839 $mockCache = $this->getMockCache();
1840 $mockCache->expects( $this->at( 1 ) )
1842 ->with( '0:SomeDbKey:1' )
1843 ->will( $this->returnValue( $cachedItems[0] ) );
1844 $mockCache->expects( $this->at( 3 ) )
1846 ->with( '1:AnotherDbKey:1' )
1847 ->will( $this->returnValue( $cachedItems[1] ) );
1848 $mockCache->expects( $this->never() )->method( 'set' );
1849 $mockCache->expects( $this->never() )->method( 'delete' );
1851 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1853 $this->assertEquals(
1855 0 => [ 'SomeDbKey' => '20151212010101', ],
1856 1 => [ 'AnotherDbKey' => null, ],
1858 $store->getNotificationTimestampsBatch( $user, $targets )
1862 public function testGetNotificationTimestampsBatch_anonymousUser() {
1864 new TitleValue( 0, 'SomeDbKey' ),
1865 new TitleValue( 1, 'AnotherDbKey' ),
1868 $mockDb = $this->getMockDb();
1869 $mockDb->expects( $this->never() )->method( $this->anything() );
1871 $mockCache = $this->getMockCache();
1872 $mockCache->expects( $this->never() )->method( $this->anything() );
1874 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1876 $this->assertEquals(
1878 0 => [ 'SomeDbKey' => false, ],
1879 1 => [ 'AnotherDbKey' => false, ],
1881 $store->getNotificationTimestampsBatch(
1882 new UserIdentityValue( 0, 'AnonUser', 0 ), $targets )
1886 public function testResetNotificationTimestamp_anonymousUser() {
1887 $mockDb = $this->getMockDb();
1888 $mockDb->expects( $this->never() )
1889 ->method( 'selectRow' );
1891 $mockCache = $this->getMockCache();
1892 $mockCache->expects( $this->never() )->method( 'get' );
1893 $mockCache->expects( $this->never() )->method( 'set' );
1894 $mockCache->expects( $this->never() )->method( 'delete' );
1896 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1899 $store->resetNotificationTimestamp(
1900 new UserIdentityValue( 0, 'AnonUser', 0 ),
1901 new TitleValue( 0, 'SomeDbKey' )
1906 public function testResetNotificationTimestamp_noItem() {
1907 $mockDb = $this->getMockDb();
1908 $mockDb->expects( $this->once() )
1909 ->method( 'selectRow' )
1912 'wl_notificationtimestamp',
1915 'wl_namespace' => 0,
1916 'wl_title' => 'SomeDbKey',
1919 ->will( $this->returnValue( [] ) );
1921 $mockCache = $this->getMockCache();
1922 $mockCache->expects( $this->never() )->method( 'get' );
1923 $mockCache->expects( $this->never() )->method( 'set' );
1924 $mockCache->expects( $this->never() )->method( 'delete' );
1926 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1929 $store->resetNotificationTimestamp(
1930 new UserIdentityValue( 1, 'MockUser', 0 ),
1931 new TitleValue( 0, 'SomeDbKey' )
1936 public function testResetNotificationTimestamp_item() {
1937 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1938 $title = new TitleValue( 0, 'SomeDbKey' );
1940 $mockDb = $this->getMockDb();
1941 $mockDb->expects( $this->once() )
1942 ->method( 'selectRow' )
1945 'wl_notificationtimestamp',
1948 'wl_namespace' => 0,
1949 'wl_title' => 'SomeDbKey',
1952 ->will( $this->returnValue(
1953 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1956 $mockCache = $this->getMockCache();
1957 $mockCache->expects( $this->never() )->method( 'get' );
1958 $mockCache->expects( $this->once() )
1962 $this->isInstanceOf( WatchedItem
::class )
1964 $mockCache->expects( $this->once() )
1965 ->method( 'delete' )
1966 ->with( '0:SomeDbKey:1' );
1968 $mockQueueGroup = $this->getMockJobQueueGroup();
1969 $mockQueueGroup->expects( $this->once() )
1970 ->method( 'lazyPush' )
1971 ->willReturnCallback( function ( ActivityUpdateJob
$job ) {
1975 // We don't care if these methods actually do anything here
1976 $mockRevisionLookup = $this->getMockRevisionLookup( [
1977 'getRevisionByTitle' => function () {
1980 'getTimestampFromId' => function () {
1981 return '00000000000000';
1985 $store = $this->newWatchedItemStore( [
1987 'queueGroup' => $mockQueueGroup,
1988 'cache' => $mockCache,
1989 'revisionLookup' => $mockRevisionLookup,
1993 $store->resetNotificationTimestamp(
2000 public function testResetNotificationTimestamp_noItemForced() {
2001 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2002 $title = new TitleValue( 0, 'SomeDbKey' );
2004 $mockDb = $this->getMockDb();
2005 $mockDb->expects( $this->never() )
2006 ->method( 'selectRow' );
2008 $mockCache = $this->getMockCache();
2009 $mockCache->expects( $this->never() )->method( 'get' );
2010 $mockCache->expects( $this->never() )->method( 'set' );
2011 $mockCache->expects( $this->once() )
2012 ->method( 'delete' )
2013 ->with( '0:SomeDbKey:1' );
2015 $mockQueueGroup = $this->getMockJobQueueGroup();
2017 // We don't care if these methods actually do anything here
2018 $mockRevisionLookup = $this->getMockRevisionLookup( [
2019 'getRevisionByTitle' => function () {
2022 'getTimestampFromId' => function () {
2023 return '00000000000000';
2027 $store = $this->newWatchedItemStore( [
2029 'queueGroup' => $mockQueueGroup,
2030 'cache' => $mockCache,
2031 'revisionLookup' => $mockRevisionLookup,
2034 $mockQueueGroup->expects( $this->any() )
2035 ->method( 'lazyPush' )
2036 ->will( $this->returnCallback( function ( ActivityUpdateJob
$job ) {
2041 $store->resetNotificationTimestamp(
2049 private function verifyCallbackJob(
2050 ActivityUpdateJob
$job,
2051 LinkTarget
$expectedTitle,
2053 callable
$notificationTimestampCondition
2055 $this->assertEquals( $expectedTitle->getDBkey(), $job->getTitle()->getDBkey() );
2056 $this->assertEquals( $expectedTitle->getNamespace(), $job->getTitle()->getNamespace() );
2058 $jobParams = $job->getParams();
2059 $this->assertArrayHasKey( 'type', $jobParams );
2060 $this->assertEquals( 'updateWatchlistNotification', $jobParams['type'] );
2061 $this->assertArrayHasKey( 'userid', $jobParams );
2062 $this->assertEquals( $expectedUserId, $jobParams['userid'] );
2063 $this->assertArrayHasKey( 'notifTime', $jobParams );
2064 $this->assertTrue( $notificationTimestampCondition( $jobParams['notifTime'] ) );
2067 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
2068 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2070 $title = new TitleValue( 0, 'SomeTitle' );
2072 $mockDb = $this->getMockDb();
2073 $mockDb->expects( $this->never() )
2074 ->method( 'selectRow' );
2076 $mockCache = $this->getMockCache();
2077 $mockCache->expects( $this->never() )->method( 'get' );
2078 $mockCache->expects( $this->never() )->method( 'set' );
2079 $mockCache->expects( $this->once() )
2080 ->method( 'delete' )
2081 ->with( '0:SomeTitle:1' );
2083 $mockQueueGroup = $this->getMockJobQueueGroup();
2085 $mockRevisionRecord = $this->createMock( RevisionRecord
::class );
2086 $mockRevisionRecord->expects( $this->never() )->method( $this->anything() );
2088 $mockRevisionLookup = $this->getMockRevisionLookup( [
2089 'getTimestampFromId' => function () {
2090 return '00000000000000';
2092 'getRevisionById' => function ( $id, $flags ) use ( $oldid, $mockRevisionRecord ) {
2093 $this->assertSame( $oldid, $id );
2094 $this->assertSame( 0, $flags );
2095 return $mockRevisionRecord;
2097 'getNextRevision' =>
2098 function ( $oldRev ) use ( $mockRevisionRecord ) {
2099 $this->assertSame( $mockRevisionRecord, $oldRev );
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 ),