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 if ( defined( 'HHVM_VERSION' ) ) {
90 $this->markTestSkipped( 'HHVM Reflection buggy' );
93 $mock = $this->getMockBuilder( HashBagOStuff
::class )
94 ->disableOriginalConstructor()
95 ->setMethods( [ 'get', 'set', 'delete', 'makeKey' ] )
97 $mock->expects( $this->any() )
99 ->will( $this->returnCallback( function ( ...$args ) {
100 return implode( ':', $args );
106 * @return PHPUnit_Framework_MockObject_MockObject|ReadOnlyMode
108 private function getMockReadOnlyMode( $readOnly = false ) {
109 $mock = $this->getMockBuilder( ReadOnlyMode
::class )
110 ->disableOriginalConstructor()
112 $mock->expects( $this->any() )
113 ->method( 'isReadOnly' )
114 ->will( $this->returnValue( $readOnly ) );
119 * Assumes that only getSubjectPage and getTalkPage will ever be called, and everything passed
120 * to them will have namespace 0.
122 private function getMockNsInfo() : NamespaceInfo
{
123 $mock = $this->createMock( NamespaceInfo
::class );
124 $mock->method( 'getSubjectPage' )->will( $this->returnArgument( 0 ) );
125 $mock->method( 'getTalkPage' )->will( $this->returnCallback(
126 function ( $target ) {
127 return new TitleValue( 1, $target->getDbKey() );
130 $mock->expects( $this->never() )
131 ->method( $this->anythingBut( 'getSubjectPage', 'getTalkPage' ) );
136 * No methods may be called except provided callbacks, if any.
138 * @param array $callbacks Keys are method names, values are callbacks
139 * @param array $counts Keys are method names, values are expected number of times to be called
140 * (default is any number is okay)
142 private function getMockRevisionLookup(
143 array $callbacks = [], array $counts = []
145 $mock = $this->createMock( RevisionLookup
::class );
146 foreach ( $callbacks as $method => $callback ) {
147 $count = isset( $counts[$method] ) ?
$this->exactly( $counts[$method] ) : $this->any();
148 $mock->expects( $count )
150 ->will( $this->returnCallback( $callbacks[$method] ) );
152 $mock->expects( $this->never() )
153 ->method( $this->anythingBut( ...array_keys( $callbacks ) ) );
157 private function getFakeRow( array $rowValues ) {
158 $fakeRow = new stdClass();
159 foreach ( $rowValues as $valueName => $value ) {
160 $fakeRow->$valueName = $value;
166 * @param array $mocks Associative array providing mocks to use when constructing the
167 * WatchedItemStore. Anything not provided will fall back to a default. Valid keys:
176 private function newWatchedItemStore( array $mocks = [] ) : WatchedItemStore
{
177 return new WatchedItemStore(
178 $mocks['lbFactory'] ??
179 $this->getMockLBFactory( $mocks['db'] ??
$this->getMockDb() ),
180 $mocks['queueGroup'] ??
$this->getMockJobQueueGroup(),
182 $mocks['cache'] ??
$this->getMockCache(),
183 $mocks['readOnlyMode'] ??
$this->getMockReadOnlyMode(),
185 $mocks['nsInfo'] ??
$this->getMockNsInfo(),
186 $mocks['revisionLookup'] ??
$this->getMockRevisionLookup()
190 public function testClearWatchedItems() {
191 $user = new UserIdentityValue( 7, 'MockUser', 0 );
193 $mockDb = $this->getMockDb();
194 $mockDb->expects( $this->once() )
195 ->method( 'selectField' )
200 'wl_user' => $user->getId(),
202 $this->isType( 'string' )
204 ->will( $this->returnValue( 12 ) );
205 $mockDb->expects( $this->once() )
210 $this->isType( 'string' )
213 $mockCache = $this->getMockCache();
214 $mockCache->expects( $this->never() )->method( 'get' );
215 $mockCache->expects( $this->never() )->method( 'set' );
216 $mockCache->expects( $this->once() )
220 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
221 TestingAccessWrapper
::newFromObject( $store )
222 ->cacheIndex
= [ 0 => [ 'F' => [ 7 => 'RM-KEY', 9 => 'KEEP-KEY' ] ] ];
224 $this->assertTrue( $store->clearUserWatchedItems( $user ) );
227 public function testClearWatchedItems_tooManyItemsWatched() {
228 $user = new UserIdentityValue( 7, 'MockUser', 0 );
230 $mockDb = $this->getMockDb();
231 $mockDb->expects( $this->once() )
232 ->method( 'selectField' )
237 'wl_user' => $user->getId(),
239 $this->isType( 'string' )
241 ->will( $this->returnValue( 99999 ) );
243 $mockCache = $this->getMockCache();
244 $mockCache->expects( $this->never() )->method( 'get' );
245 $mockCache->expects( $this->never() )->method( 'set' );
246 $mockCache->expects( $this->never() )->method( 'delete' );
248 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
250 $this->assertFalse( $store->clearUserWatchedItems( $user ) );
253 public function testCountWatchedItems() {
254 $user = new UserIdentityValue( 1, 'MockUser', 0 );
256 $mockDb = $this->getMockDb();
257 $mockDb->expects( $this->exactly( 1 ) )
258 ->method( 'selectField' )
263 'wl_user' => $user->getId(),
265 $this->isType( 'string' )
267 ->will( $this->returnValue( '12' ) );
269 $mockCache = $this->getMockCache();
270 $mockCache->expects( $this->never() )->method( 'get' );
271 $mockCache->expects( $this->never() )->method( 'set' );
272 $mockCache->expects( $this->never() )->method( 'delete' );
274 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
276 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
279 public function testCountWatchers() {
280 $titleValue = new TitleValue( 0, 'SomeDbKey' );
282 $mockDb = $this->getMockDb();
283 $mockDb->expects( $this->exactly( 1 ) )
284 ->method( 'selectField' )
289 'wl_namespace' => $titleValue->getNamespace(),
290 'wl_title' => $titleValue->getDBkey(),
292 $this->isType( 'string' )
294 ->will( $this->returnValue( '7' ) );
296 $mockCache = $this->getMockCache();
297 $mockCache->expects( $this->never() )->method( 'get' );
298 $mockCache->expects( $this->never() )->method( 'set' );
299 $mockCache->expects( $this->never() )->method( 'delete' );
301 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
303 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
306 public function testCountWatchersMultiple() {
308 new TitleValue( 0, 'SomeDbKey' ),
309 new TitleValue( 0, 'OtherDbKey' ),
310 new TitleValue( 1, 'AnotherDbKey' ),
313 $mockDb = $this->getMockDb();
316 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
317 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
318 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
321 $mockDb->expects( $this->once() )
322 ->method( 'makeWhereFrom2d' )
324 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
325 $this->isType( 'string' ),
326 $this->isType( 'string' )
328 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
329 $mockDb->expects( $this->once() )
333 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
334 [ 'makeWhereFrom2d return value' ],
335 $this->isType( 'string' ),
337 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
341 $this->returnValue( $dbResult )
344 $mockCache = $this->getMockCache();
345 $mockCache->expects( $this->never() )->method( 'get' );
346 $mockCache->expects( $this->never() )->method( 'set' );
347 $mockCache->expects( $this->never() )->method( 'delete' );
349 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
352 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
353 1 => [ 'AnotherDbKey' => 500 ],
355 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
358 public function provideIntWithDbUnsafeVersion() {
361 [ "50; DROP TABLE watchlist;\n--" ],
366 * @dataProvider provideIntWithDbUnsafeVersion
368 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
370 new TitleValue( 0, 'SomeDbKey' ),
371 new TitleValue( 0, 'OtherDbKey' ),
372 new TitleValue( 1, 'AnotherDbKey' ),
375 $mockDb = $this->getMockDb();
378 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
379 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
380 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
383 $mockDb->expects( $this->once() )
384 ->method( 'makeWhereFrom2d' )
386 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
387 $this->isType( 'string' ),
388 $this->isType( 'string' )
390 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
391 $mockDb->expects( $this->once() )
395 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
396 [ 'makeWhereFrom2d return value' ],
397 $this->isType( 'string' ),
399 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
400 'HAVING' => 'COUNT(*) >= 50',
404 $this->returnValue( $dbResult )
407 $mockCache = $this->getMockCache();
408 $mockCache->expects( $this->never() )->method( 'get' );
409 $mockCache->expects( $this->never() )->method( 'set' );
410 $mockCache->expects( $this->never() )->method( 'delete' );
412 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
415 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
416 1 => [ 'AnotherDbKey' => 500 ],
420 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
424 public function testCountVisitingWatchers() {
425 $titleValue = new TitleValue( 0, 'SomeDbKey' );
427 $mockDb = $this->getMockDb();
428 $mockDb->expects( $this->exactly( 1 ) )
429 ->method( 'selectField' )
434 'wl_namespace' => $titleValue->getNamespace(),
435 'wl_title' => $titleValue->getDBkey(),
436 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
438 $this->isType( 'string' )
440 ->will( $this->returnValue( '7' ) );
441 $mockDb->expects( $this->exactly( 1 ) )
442 ->method( 'addQuotes' )
443 ->will( $this->returnCallback( function ( $value ) {
446 $mockDb->expects( $this->exactly( 1 ) )
447 ->method( 'timestamp' )
448 ->will( $this->returnCallback( function ( $value ) {
449 return 'TS' . $value . 'TS';
452 $mockCache = $this->getMockCache();
453 $mockCache->expects( $this->never() )->method( 'set' );
454 $mockCache->expects( $this->never() )->method( 'get' );
455 $mockCache->expects( $this->never() )->method( 'delete' );
457 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
459 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
462 public function testCountVisitingWatchersMultiple() {
463 $titleValuesWithThresholds = [
464 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
465 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
466 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
470 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
471 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
473 [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
476 $mockDb = $this->getMockDb();
477 $mockDb->expects( $this->exactly( 2 * 3 ) )
478 ->method( 'addQuotes' )
479 ->will( $this->returnCallback( function ( $value ) {
482 $mockDb->expects( $this->exactly( 3 ) )
483 ->method( 'timestamp' )
484 ->will( $this->returnCallback( function ( $value ) {
485 return 'TS' . $value . 'TS';
487 $mockDb->expects( $this->any() )
488 ->method( 'makeList' )
490 $this->isType( 'array' ),
491 $this->isType( 'int' )
493 ->will( $this->returnCallback( function ( $a, $conj ) {
494 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
495 return implode( $sqlConj, array_map( function ( $s ) {
496 return '(' . $s . ')';
500 $mockDb->expects( $this->never() )
501 ->method( 'makeWhereFrom2d' );
504 '((wl_namespace = 0) AND (' .
505 "(((wl_title = 'SomeDbKey') AND (" .
506 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
508 "(wl_title = 'OtherDbKey') AND (" .
509 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
511 ') OR ((wl_namespace = 1) AND (' .
512 "(((wl_title = 'AnotherDbKey') AND (" .
513 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
515 $mockDb->expects( $this->once() )
519 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
521 $this->isType( 'string' ),
523 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
527 $this->returnValue( $dbResult )
530 $mockCache = $this->getMockCache();
531 $mockCache->expects( $this->never() )->method( 'get' );
532 $mockCache->expects( $this->never() )->method( 'set' );
533 $mockCache->expects( $this->never() )->method( 'delete' );
535 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
538 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
539 1 => [ 'AnotherDbKey' => 500 ],
543 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
547 public function testCountVisitingWatchersMultiple_withMissingTargets() {
548 $titleValuesWithThresholds = [
549 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
550 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
551 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
552 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
553 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
557 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
558 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
560 [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
563 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '100' ]
566 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '200' ]
569 $mockDb = $this->getMockDb();
570 $mockDb->expects( $this->exactly( 2 * 3 ) )
571 ->method( 'addQuotes' )
572 ->will( $this->returnCallback( function ( $value ) {
575 $mockDb->expects( $this->exactly( 3 ) )
576 ->method( 'timestamp' )
577 ->will( $this->returnCallback( function ( $value ) {
578 return 'TS' . $value . 'TS';
580 $mockDb->expects( $this->any() )
581 ->method( 'makeList' )
583 $this->isType( 'array' ),
584 $this->isType( 'int' )
586 ->will( $this->returnCallback( function ( $a, $conj ) {
587 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
588 return implode( $sqlConj, array_map( function ( $s ) {
589 return '(' . $s . ')';
593 $mockDb->expects( $this->once() )
594 ->method( 'makeWhereFrom2d' )
596 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
597 $this->isType( 'string' ),
598 $this->isType( 'string' )
600 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
603 '((wl_namespace = 0) AND (' .
604 "(((wl_title = 'SomeDbKey') AND (" .
605 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
607 "(wl_title = 'OtherDbKey') AND (" .
608 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
610 ') OR ((wl_namespace = 1) AND (' .
611 "(((wl_title = 'AnotherDbKey') AND (" .
612 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
615 '(makeWhereFrom2d return value)';
616 $mockDb->expects( $this->once() )
620 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
622 $this->isType( 'string' ),
624 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
628 $this->returnValue( $dbResult )
631 $mockCache = $this->getMockCache();
632 $mockCache->expects( $this->never() )->method( 'get' );
633 $mockCache->expects( $this->never() )->method( 'set' );
634 $mockCache->expects( $this->never() )->method( 'delete' );
636 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
640 'SomeDbKey' => 100, 'OtherDbKey' => 300,
641 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
643 1 => [ 'AnotherDbKey' => 500 ],
647 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
652 * @dataProvider provideIntWithDbUnsafeVersion
654 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
655 $titleValuesWithThresholds = [
656 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
657 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
658 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
661 $mockDb = $this->getMockDb();
662 $mockDb->expects( $this->any() )
663 ->method( 'makeList' )
664 ->will( $this->returnValue( 'makeList return value' ) );
665 $mockDb->expects( $this->once() )
669 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
670 'makeList return value',
671 $this->isType( 'string' ),
673 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
674 'HAVING' => 'COUNT(*) >= 50',
678 $this->returnValue( [] )
681 $mockCache = $this->getMockCache();
682 $mockCache->expects( $this->never() )->method( 'get' );
683 $mockCache->expects( $this->never() )->method( 'set' );
684 $mockCache->expects( $this->never() )->method( 'delete' );
686 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
689 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
690 1 => [ 'AnotherDbKey' => 0 ],
694 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
698 public function testCountUnreadNotifications() {
699 $user = new UserIdentityValue( 1, 'MockUser', 0 );
701 $mockDb = $this->getMockDb();
702 $mockDb->expects( $this->exactly( 1 ) )
703 ->method( 'selectRowCount' )
708 "wl_notificationtimestamp IS NOT NULL",
711 $this->isType( 'string' )
713 ->will( $this->returnValue( '9' ) );
715 $mockCache = $this->getMockCache();
716 $mockCache->expects( $this->never() )->method( 'set' );
717 $mockCache->expects( $this->never() )->method( 'get' );
718 $mockCache->expects( $this->never() )->method( 'delete' );
720 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
722 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
726 * @dataProvider provideIntWithDbUnsafeVersion
728 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
729 $user = new UserIdentityValue( 1, 'MockUser', 0 );
731 $mockDb = $this->getMockDb();
732 $mockDb->expects( $this->exactly( 1 ) )
733 ->method( 'selectRowCount' )
738 "wl_notificationtimestamp IS NOT NULL",
741 $this->isType( 'string' ),
744 ->will( $this->returnValue( '50' ) );
746 $mockCache = $this->getMockCache();
747 $mockCache->expects( $this->never() )->method( 'set' );
748 $mockCache->expects( $this->never() )->method( 'get' );
749 $mockCache->expects( $this->never() )->method( 'delete' );
751 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
755 $store->countUnreadNotifications( $user, $limit )
760 * @dataProvider provideIntWithDbUnsafeVersion
762 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
763 $user = new UserIdentityValue( 1, 'MockUser', 0 );
765 $mockDb = $this->getMockDb();
766 $mockDb->expects( $this->exactly( 1 ) )
767 ->method( 'selectRowCount' )
772 "wl_notificationtimestamp IS NOT NULL",
775 $this->isType( 'string' ),
778 ->will( $this->returnValue( '9' ) );
780 $mockCache = $this->getMockCache();
781 $mockCache->expects( $this->never() )->method( 'set' );
782 $mockCache->expects( $this->never() )->method( 'get' );
783 $mockCache->expects( $this->never() )->method( 'delete' );
785 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
789 $store->countUnreadNotifications( $user, $limit )
793 public function testDuplicateEntry_nothingToDuplicate() {
794 $mockDb = $this->getMockDb();
795 $mockDb->expects( $this->once() )
801 'wl_notificationtimestamp',
805 'wl_title' => 'Old_Title',
807 'WatchedItemStore::duplicateEntry',
810 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
812 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
814 $store->duplicateEntry(
815 new TitleValue( 0, 'Old_Title' ),
816 new TitleValue( 0, 'New_Title' )
820 public function testDuplicateEntry_somethingToDuplicate() {
822 $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
823 $this->getFakeRow( [ 'wl_user' => '2', 'wl_notificationtimestamp' => null ] ),
826 $mockDb = $this->getMockDb();
827 $mockDb->expects( $this->at( 0 ) )
833 'wl_notificationtimestamp',
837 'wl_title' => 'Old_Title',
840 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
841 $mockDb->expects( $this->at( 1 ) )
842 ->method( 'replace' )
845 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
850 'wl_title' => 'New_Title',
851 'wl_notificationtimestamp' => '20151212010101',
856 'wl_title' => 'New_Title',
857 'wl_notificationtimestamp' => null,
860 $this->isType( 'string' )
863 $mockCache = $this->getMockCache();
864 $mockCache->expects( $this->never() )->method( 'get' );
865 $mockCache->expects( $this->never() )->method( 'delete' );
867 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
869 $store->duplicateEntry(
870 new TitleValue( 0, 'Old_Title' ),
871 new TitleValue( 0, 'New_Title' )
875 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
876 $mockDb = $this->getMockDb();
877 $mockDb->expects( $this->at( 0 ) )
883 'wl_notificationtimestamp',
887 'wl_title' => 'Old_Title',
890 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
891 $mockDb->expects( $this->at( 1 ) )
897 'wl_notificationtimestamp',
901 'wl_title' => 'Old_Title',
904 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
906 $mockCache = $this->getMockCache();
907 $mockCache->expects( $this->never() )->method( 'get' );
908 $mockCache->expects( $this->never() )->method( 'delete' );
910 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
912 $store->duplicateAllAssociatedEntries(
913 new TitleValue( 0, 'Old_Title' ),
914 new TitleValue( 0, 'New_Title' )
918 public function provideLinkTargetPairs() {
920 [ new TitleValue( 0, 'Old_Title' ), new TitleValue( 0, 'New_Title' ) ],
921 [ new TitleValue( 0, 'Old_Title' ), new TitleValue( 0, 'New_Title' ) ],
926 * @dataProvider provideLinkTargetPairs
928 public function testDuplicateAllAssociatedEntries_somethingToDuplicate(
929 LinkTarget
$oldTarget,
930 LinkTarget
$newTarget
933 $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
936 $mockDb = $this->getMockDb();
937 $mockDb->expects( $this->at( 0 ) )
943 'wl_notificationtimestamp',
946 'wl_namespace' => $oldTarget->getNamespace(),
947 'wl_title' => $oldTarget->getDBkey(),
950 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
951 $mockDb->expects( $this->at( 1 ) )
952 ->method( 'replace' )
955 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
959 'wl_namespace' => $newTarget->getNamespace(),
960 'wl_title' => $newTarget->getDBkey(),
961 'wl_notificationtimestamp' => '20151212010101',
964 $this->isType( 'string' )
966 $mockDb->expects( $this->at( 2 ) )
972 'wl_notificationtimestamp',
975 'wl_namespace' => $oldTarget->getNamespace() +
1,
976 'wl_title' => $oldTarget->getDBkey(),
979 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
980 $mockDb->expects( $this->at( 3 ) )
981 ->method( 'replace' )
984 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
988 'wl_namespace' => $newTarget->getNamespace() +
1,
989 'wl_title' => $newTarget->getDBkey(),
990 'wl_notificationtimestamp' => '20151212010101',
993 $this->isType( 'string' )
996 $mockCache = $this->getMockCache();
997 $mockCache->expects( $this->never() )->method( 'get' );
998 $mockCache->expects( $this->never() )->method( 'delete' );
1000 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1002 $store->duplicateAllAssociatedEntries(
1008 public function testAddWatch_nonAnonymousUser() {
1009 $mockDb = $this->getMockDb();
1010 $mockDb->expects( $this->once() )
1011 ->method( 'insert' )
1017 'wl_namespace' => 0,
1018 'wl_title' => 'Some_Page',
1019 'wl_notificationtimestamp' => null,
1024 $mockCache = $this->getMockCache();
1025 $mockCache->expects( $this->once() )
1026 ->method( 'delete' )
1027 ->with( '0:Some_Page:1' );
1029 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1032 new UserIdentityValue( 1, 'MockUser', 0 ),
1033 new TitleValue( 0, 'Some_Page' )
1037 public function testAddWatch_anonymousUser() {
1038 $mockDb = $this->getMockDb();
1039 $mockDb->expects( $this->never() )
1040 ->method( 'insert' );
1042 $mockCache = $this->getMockCache();
1043 $mockCache->expects( $this->never() )
1044 ->method( 'delete' );
1046 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1049 new UserIdentityValue( 0, 'AnonUser', 0 ),
1050 new TitleValue( 0, 'Some_Page' )
1054 public function testAddWatchBatchForUser_readOnlyDBReturnsFalse() {
1055 $store = $this->newWatchedItemStore(
1056 [ 'readOnlyMode' => $this->getMockReadOnlyMode( true ) ] );
1059 $store->addWatchBatchForUser(
1060 new UserIdentityValue( 1, 'MockUser', 0 ),
1061 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1066 public function testAddWatchBatchForUser_nonAnonymousUser() {
1067 $mockDb = $this->getMockDb();
1068 $mockDb->expects( $this->once() )
1069 ->method( 'insert' )
1075 'wl_namespace' => 0,
1076 'wl_title' => 'Some_Page',
1077 'wl_notificationtimestamp' => null,
1081 'wl_namespace' => 1,
1082 'wl_title' => 'Some_Page',
1083 'wl_notificationtimestamp' => null,
1088 $mockDb->expects( $this->once() )
1089 ->method( 'affectedRows' )
1092 $mockCache = $this->getMockCache();
1093 $mockCache->expects( $this->exactly( 2 ) )
1094 ->method( 'delete' );
1095 $mockCache->expects( $this->at( 1 ) )
1096 ->method( 'delete' )
1097 ->with( '0:Some_Page:1' );
1098 $mockCache->expects( $this->at( 3 ) )
1099 ->method( 'delete' )
1100 ->with( '1:Some_Page:1' );
1102 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1104 $mockUser = new UserIdentityValue( 1, 'MockUser', 0 );
1107 $store->addWatchBatchForUser(
1109 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1114 public function testAddWatchBatchForUser_anonymousUsersAreSkipped() {
1115 $mockDb = $this->getMockDb();
1116 $mockDb->expects( $this->never() )
1117 ->method( 'insert' );
1119 $mockCache = $this->getMockCache();
1120 $mockCache->expects( $this->never() )
1121 ->method( 'delete' );
1123 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1126 $store->addWatchBatchForUser(
1127 new UserIdentityValue( 0, 'AnonUser', 0 ),
1128 [ new TitleValue( 0, 'Other_Page' ) ]
1133 public function testAddWatchBatchReturnsTrue_whenGivenEmptyList() {
1134 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1135 $mockDb = $this->getMockDb();
1136 $mockDb->expects( $this->never() )
1137 ->method( 'insert' );
1139 $mockCache = $this->getMockCache();
1140 $mockCache->expects( $this->never() )
1141 ->method( 'delete' );
1143 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1146 $store->addWatchBatchForUser( $user, [] )
1150 public function testLoadWatchedItem_existingItem() {
1151 $mockDb = $this->getMockDb();
1152 $mockDb->expects( $this->once() )
1153 ->method( 'selectRow' )
1156 'wl_notificationtimestamp',
1159 'wl_namespace' => 0,
1160 'wl_title' => 'SomeDbKey',
1163 ->will( $this->returnValue(
1164 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1167 $mockCache = $this->getMockCache();
1168 $mockCache->expects( $this->once() )
1174 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1176 $watchedItem = $store->loadWatchedItem(
1177 new UserIdentityValue( 1, 'MockUser', 0 ),
1178 new TitleValue( 0, 'SomeDbKey' )
1180 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1181 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1182 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1183 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1186 public function testLoadWatchedItem_noItem() {
1187 $mockDb = $this->getMockDb();
1188 $mockDb->expects( $this->once() )
1189 ->method( 'selectRow' )
1192 'wl_notificationtimestamp',
1195 'wl_namespace' => 0,
1196 'wl_title' => 'SomeDbKey',
1199 ->will( $this->returnValue( [] ) );
1201 $mockCache = $this->getMockCache();
1202 $mockCache->expects( $this->never() )->method( 'get' );
1203 $mockCache->expects( $this->never() )->method( 'delete' );
1205 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1208 $store->loadWatchedItem(
1209 new UserIdentityValue( 1, 'MockUser', 0 ),
1210 new TitleValue( 0, 'SomeDbKey' )
1215 public function testLoadWatchedItem_anonymousUser() {
1216 $mockDb = $this->getMockDb();
1217 $mockDb->expects( $this->never() )
1218 ->method( 'selectRow' );
1220 $mockCache = $this->getMockCache();
1221 $mockCache->expects( $this->never() )->method( 'get' );
1222 $mockCache->expects( $this->never() )->method( 'delete' );
1224 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1227 $store->loadWatchedItem(
1228 new UserIdentityValue( 0, 'AnonUser', 0 ),
1229 new TitleValue( 0, 'SomeDbKey' )
1234 public function testRemoveWatch_existingItem() {
1235 $mockDb = $this->getMockDb();
1236 $mockDb->expects( $this->once() )
1237 ->method( 'delete' )
1243 'wl_namespace' => 0,
1244 'wl_title' => [ 'SomeDbKey' ],
1251 'wl_namespace' => 1,
1252 'wl_title' => [ 'SomeDbKey' ],
1256 $mockDb->expects( $this->exactly( 1 ) )
1257 ->method( 'affectedRows' )
1260 $mockCache = $this->getMockCache();
1261 $mockCache->expects( $this->never() )->method( 'get' );
1262 $mockCache->expects( $this->once() )
1263 ->method( 'delete' )
1265 [ '0:SomeDbKey:1' ],
1269 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1272 $store->removeWatch(
1273 new UserIdentityValue( 1, 'MockUser', 0 ),
1274 new TitleValue( 0, 'SomeDbKey' )
1279 public function testRemoveWatch_noItem() {
1280 $mockDb = $this->getMockDb();
1281 $mockDb->expects( $this->once() )
1282 ->method( 'delete' )
1288 'wl_namespace' => 0,
1289 'wl_title' => [ 'SomeDbKey' ],
1296 'wl_namespace' => 1,
1297 'wl_title' => [ 'SomeDbKey' ],
1302 $mockDb->expects( $this->once() )
1303 ->method( 'affectedRows' )
1306 $mockCache = $this->getMockCache();
1307 $mockCache->expects( $this->never() )->method( 'get' );
1308 $mockCache->expects( $this->once() )
1309 ->method( 'delete' )
1311 [ '0:SomeDbKey:1' ],
1315 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1318 $store->removeWatch(
1319 new UserIdentityValue( 1, 'MockUser', 0 ),
1320 new TitleValue( 0, 'SomeDbKey' )
1325 public function testRemoveWatch_anonymousUser() {
1326 $mockDb = $this->getMockDb();
1327 $mockDb->expects( $this->never() )
1328 ->method( 'delete' );
1330 $mockCache = $this->getMockCache();
1331 $mockCache->expects( $this->never() )->method( 'get' );
1332 $mockCache->expects( $this->never() )
1333 ->method( 'delete' );
1335 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1338 $store->removeWatch(
1339 new UserIdentityValue( 0, 'AnonUser', 0 ),
1340 new TitleValue( 0, 'SomeDbKey' )
1345 public function testGetWatchedItem_existingItem() {
1346 $mockDb = $this->getMockDb();
1347 $mockDb->expects( $this->once() )
1348 ->method( 'selectRow' )
1351 'wl_notificationtimestamp',
1354 'wl_namespace' => 0,
1355 'wl_title' => 'SomeDbKey',
1358 ->will( $this->returnValue(
1359 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1362 $mockCache = $this->getMockCache();
1363 $mockCache->expects( $this->never() )->method( 'delete' );
1364 $mockCache->expects( $this->once() )
1369 ->will( $this->returnValue( null ) );
1370 $mockCache->expects( $this->once() )
1376 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1378 $watchedItem = $store->getWatchedItem(
1379 new UserIdentityValue( 1, 'MockUser', 0 ),
1380 new TitleValue( 0, 'SomeDbKey' )
1382 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1383 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1384 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1385 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1388 public function testGetWatchedItem_cachedItem() {
1389 $mockDb = $this->getMockDb();
1390 $mockDb->expects( $this->never() )
1391 ->method( 'selectRow' );
1393 $mockUser = new UserIdentityValue( 1, 'MockUser', 0 );
1394 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1395 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1397 $mockCache = $this->getMockCache();
1398 $mockCache->expects( $this->never() )->method( 'delete' );
1399 $mockCache->expects( $this->never() )->method( 'set' );
1400 $mockCache->expects( $this->once() )
1405 ->will( $this->returnValue( $cachedItem ) );
1407 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1409 $this->assertEquals(
1411 $store->getWatchedItem(
1418 public function testGetWatchedItem_noItem() {
1419 $mockDb = $this->getMockDb();
1420 $mockDb->expects( $this->once() )
1421 ->method( 'selectRow' )
1424 'wl_notificationtimestamp',
1427 'wl_namespace' => 0,
1428 'wl_title' => 'SomeDbKey',
1431 ->will( $this->returnValue( [] ) );
1433 $mockCache = $this->getMockCache();
1434 $mockCache->expects( $this->never() )->method( 'set' );
1435 $mockCache->expects( $this->never() )->method( 'delete' );
1436 $mockCache->expects( $this->once() )
1438 ->with( '0:SomeDbKey:1' )
1439 ->will( $this->returnValue( false ) );
1441 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1444 $store->getWatchedItem(
1445 new UserIdentityValue( 1, 'MockUser', 0 ),
1446 new TitleValue( 0, 'SomeDbKey' )
1451 public function testGetWatchedItem_anonymousUser() {
1452 $mockDb = $this->getMockDb();
1453 $mockDb->expects( $this->never() )
1454 ->method( 'selectRow' );
1456 $mockCache = $this->getMockCache();
1457 $mockCache->expects( $this->never() )->method( 'set' );
1458 $mockCache->expects( $this->never() )->method( 'get' );
1459 $mockCache->expects( $this->never() )->method( 'delete' );
1461 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1464 $store->getWatchedItem(
1465 new UserIdentityValue( 0, 'AnonUser', 0 ),
1466 new TitleValue( 0, 'SomeDbKey' )
1471 public function testGetWatchedItemsForUser() {
1472 $mockDb = $this->getMockDb();
1473 $mockDb->expects( $this->once() )
1474 ->method( 'select' )
1477 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1480 ->will( $this->returnValue( [
1481 $this->getFakeRow( [
1482 'wl_namespace' => 0,
1483 'wl_title' => 'Foo1',
1484 'wl_notificationtimestamp' => '20151212010101',
1486 $this->getFakeRow( [
1487 'wl_namespace' => 1,
1488 'wl_title' => 'Foo2',
1489 'wl_notificationtimestamp' => null,
1493 $mockCache = $this->getMockCache();
1494 $mockCache->expects( $this->never() )->method( 'delete' );
1495 $mockCache->expects( $this->never() )->method( 'get' );
1496 $mockCache->expects( $this->never() )->method( 'set' );
1498 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1499 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1501 $watchedItems = $store->getWatchedItemsForUser( $user );
1503 $this->assertInternalType( 'array', $watchedItems );
1504 $this->assertCount( 2, $watchedItems );
1505 foreach ( $watchedItems as $watchedItem ) {
1506 $this->assertInstanceOf( WatchedItem
::class, $watchedItem );
1508 $this->assertEquals(
1509 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1512 $this->assertEquals(
1513 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1518 public function provideDbTypes() {
1520 [ false, DB_REPLICA
],
1521 [ true, DB_MASTER
],
1526 * @dataProvider provideDbTypes
1528 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1529 $mockDb = $this->getMockDb();
1530 $mockCache = $this->getMockCache();
1531 $mockLoadBalancer = $this->getMockLBFactory( $mockDb, $dbType );
1532 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1534 $mockDb->expects( $this->once() )
1535 ->method( 'select' )
1538 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1540 $this->isType( 'string' ),
1541 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1543 ->will( $this->returnValue( [] ) );
1545 $store = $this->newWatchedItemStore(
1546 [ 'lbFactory' => $mockLoadBalancer, 'cache' => $mockCache ] );
1548 $watchedItems = $store->getWatchedItemsForUser(
1550 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore
::SORT_ASC
]
1552 $this->assertEquals( [], $watchedItems );
1555 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
1556 $store = $this->newWatchedItemStore();
1558 $this->setExpectedException( InvalidArgumentException
::class );
1559 $store->getWatchedItemsForUser(
1560 new UserIdentityValue( 1, 'MockUser', 0 ),
1565 public function testIsWatchedItem_existingItem() {
1566 $mockDb = $this->getMockDb();
1567 $mockDb->expects( $this->once() )
1568 ->method( 'selectRow' )
1571 'wl_notificationtimestamp',
1574 'wl_namespace' => 0,
1575 'wl_title' => 'SomeDbKey',
1578 ->will( $this->returnValue(
1579 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1582 $mockCache = $this->getMockCache();
1583 $mockCache->expects( $this->never() )->method( 'delete' );
1584 $mockCache->expects( $this->once() )
1586 ->with( '0:SomeDbKey:1' )
1587 ->will( $this->returnValue( false ) );
1588 $mockCache->expects( $this->once() )
1594 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1598 new UserIdentityValue( 1, 'MockUser', 0 ),
1599 new TitleValue( 0, 'SomeDbKey' )
1604 public function testIsWatchedItem_noItem() {
1605 $mockDb = $this->getMockDb();
1606 $mockDb->expects( $this->once() )
1607 ->method( 'selectRow' )
1610 'wl_notificationtimestamp',
1613 'wl_namespace' => 0,
1614 'wl_title' => 'SomeDbKey',
1617 ->will( $this->returnValue( [] ) );
1619 $mockCache = $this->getMockCache();
1620 $mockCache->expects( $this->never() )->method( 'set' );
1621 $mockCache->expects( $this->never() )->method( 'delete' );
1622 $mockCache->expects( $this->once() )
1624 ->with( '0:SomeDbKey:1' )
1625 ->will( $this->returnValue( false ) );
1627 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1631 new UserIdentityValue( 1, 'MockUser', 0 ),
1632 new TitleValue( 0, 'SomeDbKey' )
1637 public function testIsWatchedItem_anonymousUser() {
1638 $mockDb = $this->getMockDb();
1639 $mockDb->expects( $this->never() )
1640 ->method( 'selectRow' );
1642 $mockCache = $this->getMockCache();
1643 $mockCache->expects( $this->never() )->method( 'set' );
1644 $mockCache->expects( $this->never() )->method( 'get' );
1645 $mockCache->expects( $this->never() )->method( 'delete' );
1647 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1651 new UserIdentityValue( 0, 'AnonUser', 0 ),
1652 new TitleValue( 0, 'SomeDbKey' )
1657 public function testGetNotificationTimestampsBatch() {
1659 new TitleValue( 0, 'SomeDbKey' ),
1660 new TitleValue( 1, 'AnotherDbKey' ),
1663 $mockDb = $this->getMockDb();
1665 $this->getFakeRow( [
1666 'wl_namespace' => '0',
1667 'wl_title' => 'SomeDbKey',
1668 'wl_notificationtimestamp' => '20151212010101',
1672 'wl_namespace' => '1',
1673 'wl_title' => 'AnotherDbKey',
1674 'wl_notificationtimestamp' => null,
1679 $mockDb->expects( $this->once() )
1680 ->method( 'makeWhereFrom2d' )
1682 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1683 $this->isType( 'string' ),
1684 $this->isType( 'string' )
1686 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1687 $mockDb->expects( $this->once() )
1688 ->method( 'select' )
1691 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1693 'makeWhereFrom2d return value',
1696 $this->isType( 'string' )
1698 ->will( $this->returnValue( $dbResult ) );
1700 $mockCache = $this->getMockCache();
1701 $mockCache->expects( $this->exactly( 2 ) )
1704 [ '0:SomeDbKey:1' ],
1705 [ '1:AnotherDbKey:1' ]
1707 ->will( $this->returnValue( null ) );
1708 $mockCache->expects( $this->never() )->method( 'set' );
1709 $mockCache->expects( $this->never() )->method( 'delete' );
1711 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1713 $this->assertEquals(
1715 0 => [ 'SomeDbKey' => '20151212010101', ],
1716 1 => [ 'AnotherDbKey' => null, ],
1718 $store->getNotificationTimestampsBatch(
1719 new UserIdentityValue( 1, 'MockUser', 0 ), $targets )
1723 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1725 new TitleValue( 0, 'OtherDbKey' ),
1728 $mockDb = $this->getMockDb();
1730 $mockDb->expects( $this->once() )
1731 ->method( 'makeWhereFrom2d' )
1733 [ [ 'OtherDbKey' => 1 ] ],
1734 $this->isType( 'string' ),
1735 $this->isType( 'string' )
1737 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1738 $mockDb->expects( $this->once() )
1739 ->method( 'select' )
1742 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1744 'makeWhereFrom2d return value',
1747 $this->isType( 'string' )
1749 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1751 $mockCache = $this->getMockCache();
1752 $mockCache->expects( $this->once() )
1754 ->with( '0:OtherDbKey:1' )
1755 ->will( $this->returnValue( null ) );
1756 $mockCache->expects( $this->never() )->method( 'set' );
1757 $mockCache->expects( $this->never() )->method( 'delete' );
1759 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1761 $this->assertEquals(
1763 0 => [ 'OtherDbKey' => false, ],
1765 $store->getNotificationTimestampsBatch(
1766 new UserIdentityValue( 1, 'MockUser', 0 ), $targets )
1770 public function testGetNotificationTimestampsBatch_cachedItem() {
1772 new TitleValue( 0, 'SomeDbKey' ),
1773 new TitleValue( 1, 'AnotherDbKey' ),
1776 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1777 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1779 $mockDb = $this->getMockDb();
1781 $mockDb->expects( $this->once() )
1782 ->method( 'makeWhereFrom2d' )
1784 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1785 $this->isType( 'string' ),
1786 $this->isType( 'string' )
1788 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1789 $mockDb->expects( $this->once() )
1790 ->method( 'select' )
1793 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1795 'makeWhereFrom2d return value',
1798 $this->isType( 'string' )
1800 ->will( $this->returnValue( [
1802 [ 'wl_namespace' => '1', 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1806 $mockCache = $this->getMockCache();
1807 $mockCache->expects( $this->at( 1 ) )
1809 ->with( '0:SomeDbKey:1' )
1810 ->will( $this->returnValue( $cachedItem ) );
1811 $mockCache->expects( $this->at( 3 ) )
1813 ->with( '1:AnotherDbKey:1' )
1814 ->will( $this->returnValue( null ) );
1815 $mockCache->expects( $this->never() )->method( 'set' );
1816 $mockCache->expects( $this->never() )->method( 'delete' );
1818 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1820 $this->assertEquals(
1822 0 => [ 'SomeDbKey' => '20151212010101', ],
1823 1 => [ 'AnotherDbKey' => null, ],
1825 $store->getNotificationTimestampsBatch( $user, $targets )
1829 public function testGetNotificationTimestampsBatch_allItemsCached() {
1831 new TitleValue( 0, 'SomeDbKey' ),
1832 new TitleValue( 1, 'AnotherDbKey' ),
1835 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1837 new WatchedItem( $user, $targets[0], '20151212010101' ),
1838 new WatchedItem( $user, $targets[1], null ),
1840 $mockDb = $this->getMockDb();
1841 $mockDb->expects( $this->never() )->method( $this->anything() );
1843 $mockCache = $this->getMockCache();
1844 $mockCache->expects( $this->at( 1 ) )
1846 ->with( '0:SomeDbKey:1' )
1847 ->will( $this->returnValue( $cachedItems[0] ) );
1848 $mockCache->expects( $this->at( 3 ) )
1850 ->with( '1:AnotherDbKey:1' )
1851 ->will( $this->returnValue( $cachedItems[1] ) );
1852 $mockCache->expects( $this->never() )->method( 'set' );
1853 $mockCache->expects( $this->never() )->method( 'delete' );
1855 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1857 $this->assertEquals(
1859 0 => [ 'SomeDbKey' => '20151212010101', ],
1860 1 => [ 'AnotherDbKey' => null, ],
1862 $store->getNotificationTimestampsBatch( $user, $targets )
1866 public function testGetNotificationTimestampsBatch_anonymousUser() {
1868 new TitleValue( 0, 'SomeDbKey' ),
1869 new TitleValue( 1, 'AnotherDbKey' ),
1872 $mockDb = $this->getMockDb();
1873 $mockDb->expects( $this->never() )->method( $this->anything() );
1875 $mockCache = $this->getMockCache();
1876 $mockCache->expects( $this->never() )->method( $this->anything() );
1878 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1880 $this->assertEquals(
1882 0 => [ 'SomeDbKey' => false, ],
1883 1 => [ 'AnotherDbKey' => false, ],
1885 $store->getNotificationTimestampsBatch(
1886 new UserIdentityValue( 0, 'AnonUser', 0 ), $targets )
1890 public function testResetNotificationTimestamp_anonymousUser() {
1891 $mockDb = $this->getMockDb();
1892 $mockDb->expects( $this->never() )
1893 ->method( 'selectRow' );
1895 $mockCache = $this->getMockCache();
1896 $mockCache->expects( $this->never() )->method( 'get' );
1897 $mockCache->expects( $this->never() )->method( 'set' );
1898 $mockCache->expects( $this->never() )->method( 'delete' );
1900 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1903 $store->resetNotificationTimestamp(
1904 new UserIdentityValue( 0, 'AnonUser', 0 ),
1905 new TitleValue( 0, 'SomeDbKey' )
1910 public function testResetNotificationTimestamp_noItem() {
1911 $mockDb = $this->getMockDb();
1912 $mockDb->expects( $this->once() )
1913 ->method( 'selectRow' )
1916 'wl_notificationtimestamp',
1919 'wl_namespace' => 0,
1920 'wl_title' => 'SomeDbKey',
1923 ->will( $this->returnValue( [] ) );
1925 $mockCache = $this->getMockCache();
1926 $mockCache->expects( $this->never() )->method( 'get' );
1927 $mockCache->expects( $this->never() )->method( 'set' );
1928 $mockCache->expects( $this->never() )->method( 'delete' );
1930 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
1933 $store->resetNotificationTimestamp(
1934 new UserIdentityValue( 1, 'MockUser', 0 ),
1935 new TitleValue( 0, 'SomeDbKey' )
1940 public function testResetNotificationTimestamp_item() {
1941 $user = new UserIdentityValue( 1, 'MockUser', 0 );
1942 $title = new TitleValue( 0, 'SomeDbKey' );
1944 $mockDb = $this->getMockDb();
1945 $mockDb->expects( $this->once() )
1946 ->method( 'selectRow' )
1949 'wl_notificationtimestamp',
1952 'wl_namespace' => 0,
1953 'wl_title' => 'SomeDbKey',
1956 ->will( $this->returnValue(
1957 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1960 $mockCache = $this->getMockCache();
1961 $mockCache->expects( $this->never() )->method( 'get' );
1962 $mockCache->expects( $this->once() )
1966 $this->isInstanceOf( WatchedItem
::class )
1968 $mockCache->expects( $this->once() )
1969 ->method( 'delete' )
1970 ->with( '0:SomeDbKey:1' );
1972 $mockQueueGroup = $this->getMockJobQueueGroup();
1973 $mockQueueGroup->expects( $this->once() )
1974 ->method( 'lazyPush' )
1975 ->willReturnCallback( function ( ActivityUpdateJob
$job ) {
1979 // We don't care if these methods actually do anything here
1980 $mockRevisionLookup = $this->getMockRevisionLookup( [
1981 'getRevisionByTitle' => function () {
1984 'getTimestampFromId' => function () {
1985 return '00000000000000';
1989 $store = $this->newWatchedItemStore( [
1991 'queueGroup' => $mockQueueGroup,
1992 'cache' => $mockCache,
1993 'revisionLookup' => $mockRevisionLookup,
1997 $store->resetNotificationTimestamp(
2004 public function testResetNotificationTimestamp_noItemForced() {
2005 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2006 $title = new TitleValue( 0, 'SomeDbKey' );
2008 $mockDb = $this->getMockDb();
2009 $mockDb->expects( $this->never() )
2010 ->method( 'selectRow' );
2012 $mockCache = $this->getMockCache();
2013 $mockCache->expects( $this->never() )->method( 'get' );
2014 $mockCache->expects( $this->never() )->method( 'set' );
2015 $mockCache->expects( $this->once() )
2016 ->method( 'delete' )
2017 ->with( '0:SomeDbKey:1' );
2019 $mockQueueGroup = $this->getMockJobQueueGroup();
2021 // We don't care if these methods actually do anything here
2022 $mockRevisionLookup = $this->getMockRevisionLookup( [
2023 'getRevisionByTitle' => function () {
2026 'getTimestampFromId' => function () {
2027 return '00000000000000';
2031 $store = $this->newWatchedItemStore( [
2033 'queueGroup' => $mockQueueGroup,
2034 'cache' => $mockCache,
2035 'revisionLookup' => $mockRevisionLookup,
2038 $mockQueueGroup->expects( $this->any() )
2039 ->method( 'lazyPush' )
2040 ->will( $this->returnCallback( function ( ActivityUpdateJob
$job ) {
2045 $store->resetNotificationTimestamp(
2053 private function verifyCallbackJob(
2054 ActivityUpdateJob
$job,
2055 LinkTarget
$expectedTitle,
2057 callable
$notificationTimestampCondition
2059 $this->assertEquals( $expectedTitle->getDBkey(), $job->getTitle()->getDBkey() );
2060 $this->assertEquals( $expectedTitle->getNamespace(), $job->getTitle()->getNamespace() );
2062 $jobParams = $job->getParams();
2063 $this->assertArrayHasKey( 'type', $jobParams );
2064 $this->assertEquals( 'updateWatchlistNotification', $jobParams['type'] );
2065 $this->assertArrayHasKey( 'userid', $jobParams );
2066 $this->assertEquals( $expectedUserId, $jobParams['userid'] );
2067 $this->assertArrayHasKey( 'notifTime', $jobParams );
2068 $this->assertTrue( $notificationTimestampCondition( $jobParams['notifTime'] ) );
2071 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
2072 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2074 $title = new TitleValue( 0, 'SomeTitle' );
2076 $mockDb = $this->getMockDb();
2077 $mockDb->expects( $this->never() )
2078 ->method( 'selectRow' );
2080 $mockCache = $this->getMockCache();
2081 $mockCache->expects( $this->never() )->method( 'get' );
2082 $mockCache->expects( $this->never() )->method( 'set' );
2083 $mockCache->expects( $this->once() )
2084 ->method( 'delete' )
2085 ->with( '0:SomeTitle:1' );
2087 $mockQueueGroup = $this->getMockJobQueueGroup();
2089 $mockRevisionRecord = $this->createMock( RevisionRecord
::class );
2090 $mockRevisionRecord->expects( $this->never() )->method( $this->anything() );
2092 $mockRevisionLookup = $this->getMockRevisionLookup( [
2093 'getTimestampFromId' => function () {
2094 return '00000000000000';
2096 'getRevisionById' => function ( $id, $flags ) use ( $oldid, $mockRevisionRecord ) {
2097 $this->assertSame( $oldid, $id );
2098 $this->assertSame( 0, $flags );
2099 return $mockRevisionRecord;
2101 'getNextRevision' =>
2102 function ( $oldRev ) use ( $mockRevisionRecord ) {
2103 $this->assertSame( $mockRevisionRecord, $oldRev );
2107 'getNextRevision' => 1,
2110 $store = $this->newWatchedItemStore( [
2112 'queueGroup' => $mockQueueGroup,
2113 'cache' => $mockCache,
2114 'revisionLookup' => $mockRevisionLookup,
2117 $mockQueueGroup->expects( $this->any() )
2118 ->method( 'lazyPush' )
2119 ->will( $this->returnCallback(
2120 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2121 $this->verifyCallbackJob(
2125 function ( $time ) {
2126 return $time === null;
2133 $store->resetNotificationTimestamp(
2142 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced() {
2143 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2145 $title = new TitleValue( 0, 'SomeDbKey' );
2147 $mockRevision = $this->createMock( RevisionRecord
::class );
2148 $mockRevision->expects( $this->never() )->method( $this->anything() );
2150 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2151 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2153 $mockDb = $this->getMockDb();
2154 $mockDb->expects( $this->once() )
2155 ->method( 'selectRow' )
2158 'wl_notificationtimestamp',
2161 'wl_namespace' => 0,
2162 'wl_title' => 'SomeDbKey',
2165 ->will( $this->returnValue(
2166 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2169 $mockCache = $this->getMockCache();
2170 $mockCache->expects( $this->never() )->method( 'get' );
2171 $mockCache->expects( $this->once() )
2173 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2174 $mockCache->expects( $this->once() )
2175 ->method( 'delete' )
2176 ->with( '0:SomeDbKey:1' );
2178 $mockQueueGroup = $this->getMockJobQueueGroup();
2180 $mockRevisionLookup = $this->getMockRevisionLookup(
2182 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2183 $this->assertSame( $oldid, $oldidParam );
2185 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2186 $this->assertSame( $oldid, $id );
2187 return $mockRevision;
2189 'getNextRevision' =>
2190 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2191 $this->assertSame( $mockRevision, $rev );
2192 return $mockNextRevision;
2196 'getTimestampFromId' => 2,
2197 'getRevisionById' => 1,
2198 'getNextRevision' => 1,
2201 $store = $this->newWatchedItemStore( [
2203 'queueGroup' => $mockQueueGroup,
2204 'cache' => $mockCache,
2205 'revisionLookup' => $mockRevisionLookup,
2208 $mockQueueGroup->expects( $this->any() )
2209 ->method( 'lazyPush' )
2210 ->will( $this->returnCallback(
2211 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2212 $this->verifyCallbackJob(
2216 function ( $time ) {
2217 return $time !== null && $time > '20151212010101';
2224 $store->resetNotificationTimestamp(
2233 public function testResetNotificationTimestamp_notWatchedPageForced() {
2234 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2236 $title = new TitleValue( 0, 'SomeDbKey' );
2238 $mockDb = $this->getMockDb();
2239 $mockDb->expects( $this->once() )
2240 ->method( 'selectRow' )
2243 'wl_notificationtimestamp',
2246 'wl_namespace' => 0,
2247 'wl_title' => 'SomeDbKey',
2250 ->will( $this->returnValue( false ) );
2252 $mockCache = $this->getMockCache();
2253 $mockCache->expects( $this->never() )->method( 'get' );
2254 $mockCache->expects( $this->never() )->method( 'set' );
2255 $mockCache->expects( $this->once() )
2256 ->method( 'delete' )
2257 ->with( '0:SomeDbKey:1' );
2259 $mockQueueGroup = $this->getMockJobQueueGroup();
2261 $mockRevision = $this->createMock( RevisionRecord
::class );
2262 $mockRevision->expects( $this->never() )->method( $this->anything() );
2264 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2265 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2267 $mockRevisionLookup = $this->getMockRevisionLookup(
2269 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2270 $this->assertSame( $oldid, $oldidParam );
2272 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2273 $this->assertSame( $oldid, $id );
2274 return $mockRevision;
2276 'getNextRevision' =>
2277 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2278 $this->assertSame( $mockRevision, $rev );
2279 return $mockNextRevision;
2283 'getTimestampFromId' => 1,
2284 'getRevisionById' => 1,
2285 'getNextRevision' => 1,
2289 $store = $this->newWatchedItemStore( [
2291 'queueGroup' => $mockQueueGroup,
2292 'cache' => $mockCache,
2293 'revisionLookup' => $mockRevisionLookup,
2296 $mockQueueGroup->expects( $this->any() )
2297 ->method( 'lazyPush' )
2298 ->will( $this->returnCallback(
2299 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2300 $this->verifyCallbackJob(
2304 function ( $time ) {
2305 return $time === null;
2312 $store->resetNotificationTimestamp(
2321 public function testResetNotificationTimestamp_futureNotificationTimestampForced() {
2322 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2324 $title = new TitleValue( 0, 'SomeDbKey' );
2326 $mockDb = $this->getMockDb();
2327 $mockDb->expects( $this->once() )
2328 ->method( 'selectRow' )
2331 'wl_notificationtimestamp',
2334 'wl_namespace' => 0,
2335 'wl_title' => 'SomeDbKey',
2338 ->will( $this->returnValue(
2339 $this->getFakeRow( [ 'wl_notificationtimestamp' => '30151212010101' ] )
2342 $mockCache = $this->getMockCache();
2343 $mockCache->expects( $this->never() )->method( 'get' );
2344 $mockCache->expects( $this->once() )
2346 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2347 $mockCache->expects( $this->once() )
2348 ->method( 'delete' )
2349 ->with( '0:SomeDbKey:1' );
2351 $mockQueueGroup = $this->getMockJobQueueGroup();
2353 $mockRevision = $this->createMock( RevisionRecord
::class );
2354 $mockRevision->expects( $this->never() )->method( $this->anything() );
2356 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2357 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2359 $mockRevisionLookup = $this->getMockRevisionLookup(
2361 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2362 $this->assertEquals( $oldid, $oldidParam );
2364 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2365 $this->assertSame( $oldid, $id );
2366 return $mockRevision;
2368 'getNextRevision' =>
2369 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2370 $this->assertSame( $mockRevision, $rev );
2371 return $mockNextRevision;
2375 'getTimestampFromId' => 2,
2376 'getRevisionById' => 1,
2377 'getNextRevision' => 1,
2381 $store = $this->newWatchedItemStore( [
2383 'queueGroup' => $mockQueueGroup,
2384 'cache' => $mockCache,
2385 'revisionLookup' => $mockRevisionLookup,
2388 $mockQueueGroup->expects( $this->any() )
2389 ->method( 'lazyPush' )
2390 ->will( $this->returnCallback(
2391 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2392 $this->verifyCallbackJob(
2396 function ( $time ) {
2397 return $time === '30151212010101';
2404 $store->resetNotificationTimestamp(
2413 public function testResetNotificationTimestamp_futureNotificationTimestampNotForced() {
2414 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2416 $title = new TitleValue( 0, 'SomeDbKey' );
2418 $mockDb = $this->getMockDb();
2419 $mockDb->expects( $this->once() )
2420 ->method( 'selectRow' )
2423 'wl_notificationtimestamp',
2426 'wl_namespace' => 0,
2427 'wl_title' => 'SomeDbKey',
2430 ->will( $this->returnValue(
2431 $this->getFakeRow( [ 'wl_notificationtimestamp' => '30151212010101' ] )
2434 $mockCache = $this->getMockCache();
2435 $mockCache->expects( $this->never() )->method( 'get' );
2436 $mockCache->expects( $this->once() )
2438 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2439 $mockCache->expects( $this->once() )
2440 ->method( 'delete' )
2441 ->with( '0:SomeDbKey:1' );
2443 $mockQueueGroup = $this->getMockJobQueueGroup();
2445 $mockRevision = $this->createMock( RevisionRecord
::class );
2446 $mockRevision->expects( $this->never() )->method( $this->anything() );
2448 $mockNextRevision = $this->createMock( RevisionRecord
::class );
2449 $mockNextRevision->expects( $this->never() )->method( $this->anything() );
2451 $mockRevisionLookup = $this->getMockRevisionLookup(
2453 'getTimestampFromId' => function ( $oldidParam ) use ( $oldid ) {
2454 $this->assertEquals( $oldid, $oldidParam );
2456 'getRevisionById' => function ( $id ) use ( $oldid, $mockRevision ) {
2457 $this->assertSame( $oldid, $id );
2458 return $mockRevision;
2460 'getNextRevision' =>
2461 function ( RevisionRecord
$rev ) use ( $mockRevision, $mockNextRevision ) {
2462 $this->assertSame( $mockRevision, $rev );
2463 return $mockNextRevision;
2467 'getTimestampFromId' => 2,
2468 'getRevisionById' => 1,
2469 'getNextRevision' => 1,
2472 $store = $this->newWatchedItemStore( [
2474 'queueGroup' => $mockQueueGroup,
2475 'cache' => $mockCache,
2476 'revisionLookup' => $mockRevisionLookup,
2479 $mockQueueGroup->expects( $this->any() )
2480 ->method( 'lazyPush' )
2481 ->will( $this->returnCallback(
2482 function ( ActivityUpdateJob
$job ) use ( $title, $user ) {
2483 $this->verifyCallbackJob(
2487 function ( $time ) {
2488 return $time === false;
2495 $store->resetNotificationTimestamp(
2504 public function testSetNotificationTimestampsForUser_anonUser() {
2505 $store = $this->newWatchedItemStore();
2506 $this->assertFalse( $store->setNotificationTimestampsForUser(
2507 new UserIdentityValue( 0, 'AnonUser', 0 ), '' ) );
2510 public function testSetNotificationTimestampsForUser_allRows() {
2511 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2512 $timestamp = '20100101010101';
2514 $store = $this->newWatchedItemStore();
2516 // Note: This does not actually assert the job is correct
2517 $callableCallCounter = 0;
2518 $mockCallback = function ( $callable ) use ( &$callableCallCounter ) {
2519 $callableCallCounter++
;
2520 $this->assertInternalType( 'callable', $callable );
2522 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
2525 $store->setNotificationTimestampsForUser( $user, $timestamp )
2527 $this->assertEquals( 1, $callableCallCounter );
2530 public function testSetNotificationTimestampsForUser_nullTimestamp() {
2531 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2534 $store = $this->newWatchedItemStore();
2536 // Note: This does not actually assert the job is correct
2537 $callableCallCounter = 0;
2538 $mockCallback = function ( $callable ) use ( &$callableCallCounter ) {
2539 $callableCallCounter++
;
2540 $this->assertInternalType( 'callable', $callable );
2542 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
2545 $store->setNotificationTimestampsForUser( $user, $timestamp )
2549 public function testSetNotificationTimestampsForUser_specificTargets() {
2550 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2551 $timestamp = '20100101010101';
2552 $targets = [ new TitleValue( 0, 'Foo' ), new TitleValue( 0, 'Bar' ) ];
2554 $mockDb = $this->getMockDb();
2555 $mockDb->expects( $this->once() )
2556 ->method( 'update' )
2559 [ 'wl_notificationtimestamp' => 'TS' . $timestamp . 'TS' ],
2560 [ 'wl_user' => 1, 'wl_namespace' => 0, 'wl_title' => [ 'Foo', 'Bar' ] ]
2562 ->will( $this->returnValue( true ) );
2563 $mockDb->expects( $this->exactly( 1 ) )
2564 ->method( 'timestamp' )
2565 ->will( $this->returnCallback( function ( $value ) {
2566 return 'TS' . $value . 'TS';
2568 $mockDb->expects( $this->once() )
2569 ->method( 'affectedRows' )
2570 ->will( $this->returnValue( 2 ) );
2572 $store = $this->newWatchedItemStore( [ 'db' => $mockDb ] );
2575 $store->setNotificationTimestampsForUser( $user, $timestamp, $targets )
2579 public function testUpdateNotificationTimestamp_watchersExist() {
2580 $mockDb = $this->getMockDb();
2581 $mockDb->expects( $this->once() )
2582 ->method( 'selectFieldValues' )
2588 'wl_namespace' => 0,
2589 'wl_title' => 'SomeDbKey',
2590 'wl_notificationtimestamp IS NULL'
2593 ->will( $this->returnValue( [ '2', '3' ] ) );
2594 $mockDb->expects( $this->once() )
2595 ->method( 'update' )
2598 [ 'wl_notificationtimestamp' => null ],
2600 'wl_user' => [ 2, 3 ],
2601 'wl_namespace' => 0,
2602 'wl_title' => 'SomeDbKey',
2606 $mockCache = $this->getMockCache();
2607 $mockCache->expects( $this->never() )->method( 'set' );
2608 $mockCache->expects( $this->never() )->method( 'get' );
2609 $mockCache->expects( $this->never() )->method( 'delete' );
2611 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2613 $this->assertEquals(
2615 $store->updateNotificationTimestamp(
2616 new UserIdentityValue( 1, 'MockUser', 0 ),
2617 new TitleValue( 0, 'SomeDbKey' ),
2623 public function testUpdateNotificationTimestamp_noWatchers() {
2624 $mockDb = $this->getMockDb();
2625 $mockDb->expects( $this->once() )
2626 ->method( 'selectFieldValues' )
2632 'wl_namespace' => 0,
2633 'wl_title' => 'SomeDbKey',
2634 'wl_notificationtimestamp IS NULL'
2638 $this->returnValue( [] )
2640 $mockDb->expects( $this->never() )
2641 ->method( 'update' );
2643 $mockCache = $this->getMockCache();
2644 $mockCache->expects( $this->never() )->method( 'set' );
2645 $mockCache->expects( $this->never() )->method( 'get' );
2646 $mockCache->expects( $this->never() )->method( 'delete' );
2648 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2650 $watchers = $store->updateNotificationTimestamp(
2651 new UserIdentityValue( 1, 'MockUser', 0 ),
2652 new TitleValue( 0, 'SomeDbKey' ),
2655 $this->assertInternalType( 'array', $watchers );
2656 $this->assertEmpty( $watchers );
2659 public function testUpdateNotificationTimestamp_clearsCachedItems() {
2660 $user = new UserIdentityValue( 1, 'MockUser', 0 );
2661 $titleValue = new TitleValue( 0, 'SomeDbKey' );
2663 $mockDb = $this->getMockDb();
2664 $mockDb->expects( $this->once() )
2665 ->method( 'selectRow' )
2666 ->will( $this->returnValue(
2667 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2669 $mockDb->expects( $this->once() )
2670 ->method( 'selectFieldValues' )
2672 $this->returnValue( [ '2', '3' ] )
2674 $mockDb->expects( $this->once() )
2675 ->method( 'update' );
2677 $mockCache = $this->getMockCache();
2678 $mockCache->expects( $this->once() )
2680 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2681 $mockCache->expects( $this->once() )
2683 ->with( '0:SomeDbKey:1' );
2684 $mockCache->expects( $this->once() )
2685 ->method( 'delete' )
2686 ->with( '0:SomeDbKey:1' );
2688 $store = $this->newWatchedItemStore( [ 'db' => $mockDb, 'cache' => $mockCache ] );
2690 // This will add the item to the cache
2691 $store->getWatchedItem( $user, $titleValue );
2693 $store->updateNotificationTimestamp(
2694 new UserIdentityValue( 1, 'MockUser', 0 ),