6 * @covers WatchedItemStore
8 class WatchedItemStoreUnitTest
extends MediaWikiTestCase
{
11 * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
13 private function getMockDb() {
14 return $this->getMock( IDatabase
::class );
18 * @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
20 private function getMockLoadBalancer( $mockDb, $expectedConnectionType = null ) {
21 $mock = $this->getMockBuilder( LoadBalancer
::class )
22 ->disableOriginalConstructor()
24 if ( $expectedConnectionType !== null ) {
25 $mock->expects( $this->any() )
26 ->method( 'getConnection' )
27 ->with( $expectedConnectionType )
28 ->will( $this->returnValue( $mockDb ) );
30 $mock->expects( $this->any() )
31 ->method( 'getConnection' )
32 ->will( $this->returnValue( $mockDb ) );
34 $mock->expects( $this->any() )
35 ->method( 'getReadOnlyReason' )
36 ->will( $this->returnValue( false ) );
41 * @return PHPUnit_Framework_MockObject_MockObject|HashBagOStuff
43 private function getMockCache() {
44 $mock = $this->getMockBuilder( HashBagOStuff
::class )
45 ->disableOriginalConstructor()
47 $mock->expects( $this->any() )
49 ->will( $this->returnCallback( function() {
50 return implode( ':', func_get_args() );
57 * @return PHPUnit_Framework_MockObject_MockObject|User
59 private function getMockNonAnonUserWithId( $id ) {
60 $mock = $this->getMock( User
::class );
61 $mock->expects( $this->any() )
63 ->will( $this->returnValue( false ) );
64 $mock->expects( $this->any() )
66 ->will( $this->returnValue( $id ) );
73 private function getAnonUser() {
74 return User
::newFromName( 'Anon_User' );
77 private function getFakeRow( array $rowValues ) {
78 $fakeRow = new stdClass();
79 foreach ( $rowValues as $valueName => $value ) {
80 $fakeRow->$valueName = $value;
85 private function newWatchedItemStore( LoadBalancer
$loadBalancer, HashBagOStuff
$cache ) {
86 return new WatchedItemStore(
92 public function testGetDefaultInstance() {
93 $instanceOne = WatchedItemStore
::getDefaultInstance();
94 $instanceTwo = WatchedItemStore
::getDefaultInstance();
95 $this->assertSame( $instanceOne, $instanceTwo );
98 public function testCountWatchedItems() {
99 $user = $this->getMockNonAnonUserWithId( 1 );
101 $mockDb = $this->getMockDb();
102 $mockDb->expects( $this->exactly( 1 ) )
103 ->method( 'selectField' )
108 'wl_user' => $user->getId(),
110 $this->isType( 'string' )
112 ->will( $this->returnValue( 12 ) );
114 $mockCache = $this->getMockCache();
115 $mockCache->expects( $this->never() )->method( 'get' );
116 $mockCache->expects( $this->never() )->method( 'set' );
117 $mockCache->expects( $this->never() )->method( 'delete' );
119 $store = $this->newWatchedItemStore(
120 $this->getMockLoadBalancer( $mockDb ),
124 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
127 public function testCountWatchers() {
128 $titleValue = new TitleValue( 0, 'SomeDbKey' );
130 $mockDb = $this->getMockDb();
131 $mockDb->expects( $this->exactly( 1 ) )
132 ->method( 'selectField' )
137 'wl_namespace' => $titleValue->getNamespace(),
138 'wl_title' => $titleValue->getDBkey(),
140 $this->isType( 'string' )
142 ->will( $this->returnValue( 7 ) );
144 $mockCache = $this->getMockCache();
145 $mockCache->expects( $this->never() )->method( 'get' );
146 $mockCache->expects( $this->never() )->method( 'set' );
147 $mockCache->expects( $this->never() )->method( 'delete' );
149 $store = $this->newWatchedItemStore(
150 $this->getMockLoadBalancer( $mockDb ),
154 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
157 public function testCountWatchersMultiple() {
159 new TitleValue( 0, 'SomeDbKey' ),
160 new TitleValue( 0, 'OtherDbKey' ),
161 new TitleValue( 1, 'AnotherDbKey' ),
164 $mockDb = $this->getMockDb();
167 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
168 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
169 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
172 $mockDb->expects( $this->once() )
173 ->method( 'makeWhereFrom2d' )
175 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
176 $this->isType( 'string' ),
177 $this->isType( 'string' )
179 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
180 $mockDb->expects( $this->once() )
184 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
185 [ 'makeWhereFrom2d return value' ],
186 $this->isType( 'string' ),
188 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
192 $this->returnValue( $dbResult )
195 $mockCache = $this->getMockCache();
196 $mockCache->expects( $this->never() )->method( 'get' );
197 $mockCache->expects( $this->never() )->method( 'set' );
198 $mockCache->expects( $this->never() )->method( 'delete' );
200 $store = $this->newWatchedItemStore(
201 $this->getMockLoadBalancer( $mockDb ),
206 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
207 1 => [ 'AnotherDbKey' => 500 ],
209 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
212 public function provideIntWithDbUnsafeVersion() {
215 [ "50; DROP TABLE watchlist;\n--" ],
220 * @dataProvider provideIntWithDbUnsafeVersion
222 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
224 new TitleValue( 0, 'SomeDbKey' ),
225 new TitleValue( 0, 'OtherDbKey' ),
226 new TitleValue( 1, 'AnotherDbKey' ),
229 $mockDb = $this->getMockDb();
232 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
233 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
234 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
237 $mockDb->expects( $this->once() )
238 ->method( 'makeWhereFrom2d' )
240 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
241 $this->isType( 'string' ),
242 $this->isType( 'string' )
244 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
245 $mockDb->expects( $this->once() )
249 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
250 [ 'makeWhereFrom2d return value' ],
251 $this->isType( 'string' ),
253 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
254 'HAVING' => 'COUNT(*) >= 50',
258 $this->returnValue( $dbResult )
261 $mockCache = $this->getMockCache();
262 $mockCache->expects( $this->never() )->method( 'get' );
263 $mockCache->expects( $this->never() )->method( 'set' );
264 $mockCache->expects( $this->never() )->method( 'delete' );
266 $store = $this->newWatchedItemStore(
267 $this->getMockLoadBalancer( $mockDb ),
272 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
273 1 => [ 'AnotherDbKey' => 500 ],
277 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
281 public function testCountVisitingWatchers() {
282 $titleValue = new TitleValue( 0, 'SomeDbKey' );
284 $mockDb = $this->getMockDb();
285 $mockDb->expects( $this->exactly( 1 ) )
286 ->method( 'selectField' )
291 'wl_namespace' => $titleValue->getNamespace(),
292 'wl_title' => $titleValue->getDBkey(),
293 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
295 $this->isType( 'string' )
297 ->will( $this->returnValue( 7 ) );
298 $mockDb->expects( $this->exactly( 1 ) )
299 ->method( 'addQuotes' )
300 ->will( $this->returnCallback( function( $value ) {
303 $mockDb->expects( $this->exactly( 1 ) )
304 ->method( 'timestamp' )
305 ->will( $this->returnCallback( function( $value ) {
306 return 'TS' . $value . 'TS';
309 $mockCache = $this->getMockCache();
310 $mockCache->expects( $this->never() )->method( 'set' );
311 $mockCache->expects( $this->never() )->method( 'get' );
312 $mockCache->expects( $this->never() )->method( 'delete' );
314 $store = $this->newWatchedItemStore(
315 $this->getMockLoadBalancer( $mockDb ),
319 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
322 public function testCountVisitingWatchersMultiple() {
323 $titleValuesWithThresholds = [
324 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
325 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
326 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
330 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
331 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
332 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
334 $mockDb = $this->getMockDb();
335 $mockDb->expects( $this->exactly( 2 * 3 ) )
336 ->method( 'addQuotes' )
337 ->will( $this->returnCallback( function( $value ) {
340 $mockDb->expects( $this->exactly( 3 ) )
341 ->method( 'timestamp' )
342 ->will( $this->returnCallback( function( $value ) {
343 return 'TS' . $value . 'TS';
345 $mockDb->expects( $this->any() )
346 ->method( 'makeList' )
348 $this->isType( 'array' ),
349 $this->isType( 'int' )
351 ->will( $this->returnCallback( function( $a, $conj ) {
352 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
353 return join( $sqlConj, array_map( function( $s ) {
354 return '(' . $s . ')';
358 $mockDb->expects( $this->never() )
359 ->method( 'makeWhereFrom2d' );
362 '((wl_namespace = 0) AND (' .
363 "(((wl_title = 'SomeDbKey') AND (" .
364 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
366 "(wl_title = 'OtherDbKey') AND (" .
367 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
369 ') OR ((wl_namespace = 1) AND (' .
370 "(((wl_title = 'AnotherDbKey') AND (".
371 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
373 $mockDb->expects( $this->once() )
377 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
379 $this->isType( 'string' ),
381 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
385 $this->returnValue( $dbResult )
388 $mockCache = $this->getMockCache();
389 $mockCache->expects( $this->never() )->method( 'get' );
390 $mockCache->expects( $this->never() )->method( 'set' );
391 $mockCache->expects( $this->never() )->method( 'delete' );
393 $store = $this->newWatchedItemStore(
394 $this->getMockLoadBalancer( $mockDb ),
399 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
400 1 => [ 'AnotherDbKey' => 500 ],
404 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
408 public function testCountVisitingWatchersMultiple_withMissingTargets() {
409 $titleValuesWithThresholds = [
410 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
411 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
412 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
413 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
414 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
418 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
419 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
420 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
422 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 100 ]
425 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 200 ]
428 $mockDb = $this->getMockDb();
429 $mockDb->expects( $this->exactly( 2 * 3 ) )
430 ->method( 'addQuotes' )
431 ->will( $this->returnCallback( function( $value ) {
434 $mockDb->expects( $this->exactly( 3 ) )
435 ->method( 'timestamp' )
436 ->will( $this->returnCallback( function( $value ) {
437 return 'TS' . $value . 'TS';
439 $mockDb->expects( $this->any() )
440 ->method( 'makeList' )
442 $this->isType( 'array' ),
443 $this->isType( 'int' )
445 ->will( $this->returnCallback( function( $a, $conj ) {
446 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
447 return join( $sqlConj, array_map( function( $s ) {
448 return '(' . $s . ')';
452 $mockDb->expects( $this->once() )
453 ->method( 'makeWhereFrom2d' )
455 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
456 $this->isType( 'string' ),
457 $this->isType( 'string' )
459 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
462 '((wl_namespace = 0) AND (' .
463 "(((wl_title = 'SomeDbKey') AND (" .
464 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
466 "(wl_title = 'OtherDbKey') AND (" .
467 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
469 ') OR ((wl_namespace = 1) AND (' .
470 "(((wl_title = 'AnotherDbKey') AND (".
471 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
474 '(makeWhereFrom2d return value)';
475 $mockDb->expects( $this->once() )
479 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
481 $this->isType( 'string' ),
483 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
487 $this->returnValue( $dbResult )
490 $mockCache = $this->getMockCache();
491 $mockCache->expects( $this->never() )->method( 'get' );
492 $mockCache->expects( $this->never() )->method( 'set' );
493 $mockCache->expects( $this->never() )->method( 'delete' );
495 $store = $this->newWatchedItemStore(
496 $this->getMockLoadBalancer( $mockDb ),
502 'SomeDbKey' => 100, 'OtherDbKey' => 300,
503 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
505 1 => [ 'AnotherDbKey' => 500 ],
509 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
514 * @dataProvider provideIntWithDbUnsafeVersion
516 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
517 $titleValuesWithThresholds = [
518 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
519 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
520 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
523 $mockDb = $this->getMockDb();
524 $mockDb->expects( $this->any() )
525 ->method( 'makeList' )
526 ->will( $this->returnValue( 'makeList return value' ) );
527 $mockDb->expects( $this->once() )
531 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
532 'makeList return value',
533 $this->isType( 'string' ),
535 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
536 'HAVING' => 'COUNT(*) >= 50',
540 $this->returnValue( [] )
543 $mockCache = $this->getMockCache();
544 $mockCache->expects( $this->never() )->method( 'get' );
545 $mockCache->expects( $this->never() )->method( 'set' );
546 $mockCache->expects( $this->never() )->method( 'delete' );
548 $store = $this->newWatchedItemStore(
549 $this->getMockLoadBalancer( $mockDb ),
554 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
555 1 => [ 'AnotherDbKey' => 0 ],
559 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
563 public function testCountUnreadNotifications() {
564 $user = $this->getMockNonAnonUserWithId( 1 );
566 $mockDb = $this->getMockDb();
567 $mockDb->expects( $this->exactly( 1 ) )
568 ->method( 'selectRowCount' )
573 "wl_notificationtimestamp IS NOT NULL",
576 $this->isType( 'string' )
578 ->will( $this->returnValue( 9 ) );
580 $mockCache = $this->getMockCache();
581 $mockCache->expects( $this->never() )->method( 'set' );
582 $mockCache->expects( $this->never() )->method( 'get' );
583 $mockCache->expects( $this->never() )->method( 'delete' );
585 $store = $this->newWatchedItemStore(
586 $this->getMockLoadBalancer( $mockDb ),
590 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
594 * @dataProvider provideIntWithDbUnsafeVersion
596 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
597 $user = $this->getMockNonAnonUserWithId( 1 );
599 $mockDb = $this->getMockDb();
600 $mockDb->expects( $this->exactly( 1 ) )
601 ->method( 'selectRowCount' )
606 "wl_notificationtimestamp IS NOT NULL",
609 $this->isType( 'string' ),
612 ->will( $this->returnValue( 50 ) );
614 $mockCache = $this->getMockCache();
615 $mockCache->expects( $this->never() )->method( 'set' );
616 $mockCache->expects( $this->never() )->method( 'get' );
617 $mockCache->expects( $this->never() )->method( 'delete' );
619 $store = $this->newWatchedItemStore(
620 $this->getMockLoadBalancer( $mockDb ),
626 $store->countUnreadNotifications( $user, $limit )
631 * @dataProvider provideIntWithDbUnsafeVersion
633 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
634 $user = $this->getMockNonAnonUserWithId( 1 );
636 $mockDb = $this->getMockDb();
637 $mockDb->expects( $this->exactly( 1 ) )
638 ->method( 'selectRowCount' )
643 "wl_notificationtimestamp IS NOT NULL",
646 $this->isType( 'string' ),
649 ->will( $this->returnValue( 9 ) );
651 $mockCache = $this->getMockCache();
652 $mockCache->expects( $this->never() )->method( 'set' );
653 $mockCache->expects( $this->never() )->method( 'get' );
654 $mockCache->expects( $this->never() )->method( 'delete' );
656 $store = $this->newWatchedItemStore(
657 $this->getMockLoadBalancer( $mockDb ),
663 $store->countUnreadNotifications( $user, $limit )
667 public function testDuplicateEntry_nothingToDuplicate() {
668 $mockDb = $this->getMockDb();
669 $mockDb->expects( $this->once() )
675 'wl_notificationtimestamp',
679 'wl_title' => 'Old_Title',
681 'WatchedItemStore::duplicateEntry',
684 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
686 $store = $this->newWatchedItemStore(
687 $this->getMockLoadBalancer( $mockDb ),
688 $this->getMockCache()
691 $store->duplicateEntry(
692 Title
::newFromText( 'Old_Title' ),
693 Title
::newFromText( 'New_Title' )
697 public function testDuplicateEntry_somethingToDuplicate() {
699 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
700 $this->getFakeRow( [ 'wl_user' => 2, 'wl_notificationtimestamp' => null ] ),
703 $mockDb = $this->getMockDb();
704 $mockDb->expects( $this->at( 0 ) )
710 'wl_notificationtimestamp',
714 'wl_title' => 'Old_Title',
717 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
718 $mockDb->expects( $this->at( 1 ) )
719 ->method( 'replace' )
722 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
727 'wl_title' => 'New_Title',
728 'wl_notificationtimestamp' => '20151212010101',
733 'wl_title' => 'New_Title',
734 'wl_notificationtimestamp' => null,
737 $this->isType( 'string' )
740 $mockCache = $this->getMockCache();
741 $mockCache->expects( $this->never() )->method( 'get' );
742 $mockCache->expects( $this->never() )->method( 'delete' );
744 $store = $this->newWatchedItemStore(
745 $this->getMockLoadBalancer( $mockDb ),
749 $store->duplicateEntry(
750 Title
::newFromText( 'Old_Title' ),
751 Title
::newFromText( 'New_Title' )
755 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
756 $mockDb = $this->getMockDb();
757 $mockDb->expects( $this->at( 0 ) )
763 'wl_notificationtimestamp',
767 'wl_title' => 'Old_Title',
770 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
771 $mockDb->expects( $this->at( 1 ) )
777 'wl_notificationtimestamp',
781 'wl_title' => 'Old_Title',
784 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
786 $mockCache = $this->getMockCache();
787 $mockCache->expects( $this->never() )->method( 'get' );
788 $mockCache->expects( $this->never() )->method( 'delete' );
790 $store = $this->newWatchedItemStore(
791 $this->getMockLoadBalancer( $mockDb ),
795 $store->duplicateAllAssociatedEntries(
796 Title
::newFromText( 'Old_Title' ),
797 Title
::newFromText( 'New_Title' )
801 public function testDuplicateAllAssociatedEntries_somethingToDuplicate() {
803 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
806 $mockDb = $this->getMockDb();
807 $mockDb->expects( $this->at( 0 ) )
813 'wl_notificationtimestamp',
817 'wl_title' => 'Old_Title',
820 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
821 $mockDb->expects( $this->at( 1 ) )
822 ->method( 'replace' )
825 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
830 'wl_title' => 'New_Title',
831 'wl_notificationtimestamp' => '20151212010101',
834 $this->isType( 'string' )
836 $mockDb->expects( $this->at( 2 ) )
842 'wl_notificationtimestamp',
846 'wl_title' => 'Old_Title',
849 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
850 $mockDb->expects( $this->at( 3 ) )
851 ->method( 'replace' )
854 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
859 'wl_title' => 'New_Title',
860 'wl_notificationtimestamp' => '20151212010101',
863 $this->isType( 'string' )
866 $mockCache = $this->getMockCache();
867 $mockCache->expects( $this->never() )->method( 'get' );
868 $mockCache->expects( $this->never() )->method( 'delete' );
870 $store = $this->newWatchedItemStore(
871 $this->getMockLoadBalancer( $mockDb ),
875 $store->duplicateAllAssociatedEntries(
876 Title
::newFromText( 'Old_Title' ),
877 Title
::newFromText( 'New_Title' )
881 public function testAddWatch_nonAnonymousUser() {
882 $mockDb = $this->getMockDb();
883 $mockDb->expects( $this->once() )
891 'wl_title' => 'Some_Page',
892 'wl_notificationtimestamp' => null,
897 $mockCache = $this->getMockCache();
898 $mockCache->expects( $this->once() )
900 ->with( '0:Some_Page:1' );
902 $store = $this->newWatchedItemStore(
903 $this->getMockLoadBalancer( $mockDb ),
908 $this->getMockNonAnonUserWithId( 1 ),
909 Title
::newFromText( 'Some_Page' )
913 public function testAddWatch_anonymousUser() {
914 $mockDb = $this->getMockDb();
915 $mockDb->expects( $this->never() )
916 ->method( 'insert' );
918 $mockCache = $this->getMockCache();
919 $mockCache->expects( $this->never() )
920 ->method( 'delete' );
922 $store = $this->newWatchedItemStore(
923 $this->getMockLoadBalancer( $mockDb ),
928 $this->getAnonUser(),
929 Title
::newFromText( 'Some_Page' )
933 public function testAddWatchBatchForUser_nonAnonymousUser() {
934 $mockDb = $this->getMockDb();
935 $mockDb->expects( $this->once() )
943 'wl_title' => 'Some_Page',
944 'wl_notificationtimestamp' => null,
949 'wl_title' => 'Some_Page',
950 'wl_notificationtimestamp' => null,
955 $mockCache = $this->getMockCache();
956 $mockCache->expects( $this->exactly( 2 ) )
957 ->method( 'delete' );
958 $mockCache->expects( $this->at( 1 ) )
960 ->with( '0:Some_Page:1' );
961 $mockCache->expects( $this->at( 3 ) )
963 ->with( '1:Some_Page:1' );
965 $store = $this->newWatchedItemStore(
966 $this->getMockLoadBalancer( $mockDb ),
970 $mockUser = $this->getMockNonAnonUserWithId( 1 );
973 $store->addWatchBatchForUser(
975 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
980 public function testAddWatchBatchForUser_anonymousUsersAreSkipped() {
981 $mockDb = $this->getMockDb();
982 $mockDb->expects( $this->never() )
983 ->method( 'insert' );
985 $mockCache = $this->getMockCache();
986 $mockCache->expects( $this->never() )
987 ->method( 'delete' );
989 $store = $this->newWatchedItemStore(
990 $this->getMockLoadBalancer( $mockDb ),
995 $store->addWatchBatchForUser(
996 $this->getAnonUser(),
997 [ new TitleValue( 0, 'Other_Page' ) ]
1002 public function testAddWatchBatchReturnsTrue_whenGivenEmptyList() {
1003 $user = $this->getMockNonAnonUserWithId( 1 );
1004 $mockDb = $this->getMockDb();
1005 $mockDb->expects( $this->never() )
1006 ->method( 'insert' );
1008 $mockCache = $this->getMockCache();
1009 $mockCache->expects( $this->never() )
1010 ->method( 'delete' );
1012 $store = $this->newWatchedItemStore(
1013 $this->getMockLoadBalancer( $mockDb ),
1018 $store->addWatchBatchForUser( $user, [] )
1022 public function testLoadWatchedItem_existingItem() {
1023 $mockDb = $this->getMockDb();
1024 $mockDb->expects( $this->once() )
1025 ->method( 'selectRow' )
1028 'wl_notificationtimestamp',
1031 'wl_namespace' => 0,
1032 'wl_title' => 'SomeDbKey',
1035 ->will( $this->returnValue(
1036 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1039 $mockCache = $this->getMockCache();
1040 $mockCache->expects( $this->once() )
1046 $store = $this->newWatchedItemStore(
1047 $this->getMockLoadBalancer( $mockDb ),
1051 $watchedItem = $store->loadWatchedItem(
1052 $this->getMockNonAnonUserWithId( 1 ),
1053 new TitleValue( 0, 'SomeDbKey' )
1055 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1056 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1057 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1058 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1061 public function testLoadWatchedItem_noItem() {
1062 $mockDb = $this->getMockDb();
1063 $mockDb->expects( $this->once() )
1064 ->method( 'selectRow' )
1067 'wl_notificationtimestamp',
1070 'wl_namespace' => 0,
1071 'wl_title' => 'SomeDbKey',
1074 ->will( $this->returnValue( [] ) );
1076 $mockCache = $this->getMockCache();
1077 $mockCache->expects( $this->never() )->method( 'get' );
1078 $mockCache->expects( $this->never() )->method( 'delete' );
1080 $store = $this->newWatchedItemStore(
1081 $this->getMockLoadBalancer( $mockDb ),
1086 $store->loadWatchedItem(
1087 $this->getMockNonAnonUserWithId( 1 ),
1088 new TitleValue( 0, 'SomeDbKey' )
1093 public function testLoadWatchedItem_anonymousUser() {
1094 $mockDb = $this->getMockDb();
1095 $mockDb->expects( $this->never() )
1096 ->method( 'selectRow' );
1098 $mockCache = $this->getMockCache();
1099 $mockCache->expects( $this->never() )->method( 'get' );
1100 $mockCache->expects( $this->never() )->method( 'delete' );
1102 $store = $this->newWatchedItemStore(
1103 $this->getMockLoadBalancer( $mockDb ),
1108 $store->loadWatchedItem(
1109 $this->getAnonUser(),
1110 new TitleValue( 0, 'SomeDbKey' )
1115 public function testRemoveWatch_existingItem() {
1116 $mockDb = $this->getMockDb();
1117 $mockDb->expects( $this->once() )
1118 ->method( 'delete' )
1123 'wl_namespace' => 0,
1124 'wl_title' => 'SomeDbKey',
1127 $mockDb->expects( $this->once() )
1128 ->method( 'affectedRows' )
1129 ->will( $this->returnValue( 1 ) );
1131 $mockCache = $this->getMockCache();
1132 $mockCache->expects( $this->never() )->method( 'get' );
1133 $mockCache->expects( $this->once() )
1134 ->method( 'delete' )
1135 ->with( '0:SomeDbKey:1' );
1137 $store = $this->newWatchedItemStore(
1138 $this->getMockLoadBalancer( $mockDb ),
1143 $store->removeWatch(
1144 $this->getMockNonAnonUserWithId( 1 ),
1145 new TitleValue( 0, 'SomeDbKey' )
1150 public function testRemoveWatch_noItem() {
1151 $mockDb = $this->getMockDb();
1152 $mockDb->expects( $this->once() )
1153 ->method( 'delete' )
1158 'wl_namespace' => 0,
1159 'wl_title' => 'SomeDbKey',
1162 $mockDb->expects( $this->once() )
1163 ->method( 'affectedRows' )
1164 ->will( $this->returnValue( 0 ) );
1166 $mockCache = $this->getMockCache();
1167 $mockCache->expects( $this->never() )->method( 'get' );
1168 $mockCache->expects( $this->once() )
1169 ->method( 'delete' )
1170 ->with( '0:SomeDbKey:1' );
1172 $store = $this->newWatchedItemStore(
1173 $this->getMockLoadBalancer( $mockDb ),
1178 $store->removeWatch(
1179 $this->getMockNonAnonUserWithId( 1 ),
1180 new TitleValue( 0, 'SomeDbKey' )
1185 public function testRemoveWatch_anonymousUser() {
1186 $mockDb = $this->getMockDb();
1187 $mockDb->expects( $this->never() )
1188 ->method( 'delete' );
1190 $mockCache = $this->getMockCache();
1191 $mockCache->expects( $this->never() )->method( 'get' );
1192 $mockCache->expects( $this->never() )
1193 ->method( 'delete' );
1195 $store = $this->newWatchedItemStore(
1196 $this->getMockLoadBalancer( $mockDb ),
1201 $store->removeWatch(
1202 $this->getAnonUser(),
1203 new TitleValue( 0, 'SomeDbKey' )
1208 public function testGetWatchedItem_existingItem() {
1209 $mockDb = $this->getMockDb();
1210 $mockDb->expects( $this->once() )
1211 ->method( 'selectRow' )
1214 'wl_notificationtimestamp',
1217 'wl_namespace' => 0,
1218 'wl_title' => 'SomeDbKey',
1221 ->will( $this->returnValue(
1222 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1225 $mockCache = $this->getMockCache();
1226 $mockCache->expects( $this->never() )->method( 'delete' );
1227 $mockCache->expects( $this->once() )
1232 ->will( $this->returnValue( null ) );
1233 $mockCache->expects( $this->once() )
1239 $store = $this->newWatchedItemStore(
1240 $this->getMockLoadBalancer( $mockDb ),
1244 $watchedItem = $store->getWatchedItem(
1245 $this->getMockNonAnonUserWithId( 1 ),
1246 new TitleValue( 0, 'SomeDbKey' )
1248 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1249 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1250 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1251 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1254 public function testGetWatchedItem_cachedItem() {
1255 $mockDb = $this->getMockDb();
1256 $mockDb->expects( $this->never() )
1257 ->method( 'selectRow' );
1259 $mockUser = $this->getMockNonAnonUserWithId( 1 );
1260 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1261 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1263 $mockCache = $this->getMockCache();
1264 $mockCache->expects( $this->never() )->method( 'delete' );
1265 $mockCache->expects( $this->never() )->method( 'set' );
1266 $mockCache->expects( $this->once() )
1271 ->will( $this->returnValue( $cachedItem ) );
1273 $store = $this->newWatchedItemStore(
1274 $this->getMockLoadBalancer( $mockDb ),
1278 $this->assertEquals(
1280 $store->getWatchedItem(
1287 public function testGetWatchedItem_noItem() {
1288 $mockDb = $this->getMockDb();
1289 $mockDb->expects( $this->once() )
1290 ->method( 'selectRow' )
1293 'wl_notificationtimestamp',
1296 'wl_namespace' => 0,
1297 'wl_title' => 'SomeDbKey',
1300 ->will( $this->returnValue( [] ) );
1302 $mockCache = $this->getMockCache();
1303 $mockCache->expects( $this->never() )->method( 'set' );
1304 $mockCache->expects( $this->never() )->method( 'delete' );
1305 $mockCache->expects( $this->once() )
1307 ->with( '0:SomeDbKey:1' )
1308 ->will( $this->returnValue( false ) );
1310 $store = $this->newWatchedItemStore(
1311 $this->getMockLoadBalancer( $mockDb ),
1316 $store->getWatchedItem(
1317 $this->getMockNonAnonUserWithId( 1 ),
1318 new TitleValue( 0, 'SomeDbKey' )
1323 public function testGetWatchedItem_anonymousUser() {
1324 $mockDb = $this->getMockDb();
1325 $mockDb->expects( $this->never() )
1326 ->method( 'selectRow' );
1328 $mockCache = $this->getMockCache();
1329 $mockCache->expects( $this->never() )->method( 'set' );
1330 $mockCache->expects( $this->never() )->method( 'get' );
1331 $mockCache->expects( $this->never() )->method( 'delete' );
1333 $store = $this->newWatchedItemStore(
1334 $this->getMockLoadBalancer( $mockDb ),
1339 $store->getWatchedItem(
1340 $this->getAnonUser(),
1341 new TitleValue( 0, 'SomeDbKey' )
1346 public function testGetWatchedItemsForUser() {
1347 $mockDb = $this->getMockDb();
1348 $mockDb->expects( $this->once() )
1349 ->method( 'select' )
1352 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1355 ->will( $this->returnValue( [
1356 $this->getFakeRow( [
1357 'wl_namespace' => 0,
1358 'wl_title' => 'Foo1',
1359 'wl_notificationtimestamp' => '20151212010101',
1361 $this->getFakeRow( [
1362 'wl_namespace' => 1,
1363 'wl_title' => 'Foo2',
1364 'wl_notificationtimestamp' => null,
1368 $mockCache = $this->getMockCache();
1369 $mockCache->expects( $this->never() )->method( 'delete' );
1370 $mockCache->expects( $this->never() )->method( 'get' );
1371 $mockCache->expects( $this->never() )->method( 'set' );
1373 $store = $this->newWatchedItemStore(
1374 $this->getMockLoadBalancer( $mockDb ),
1377 $user = $this->getMockNonAnonUserWithId( 1 );
1379 $watchedItems = $store->getWatchedItemsForUser( $user );
1381 $this->assertInternalType( 'array', $watchedItems );
1382 $this->assertCount( 2, $watchedItems );
1383 foreach ( $watchedItems as $watchedItem ) {
1384 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1386 $this->assertEquals(
1387 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1390 $this->assertEquals(
1391 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1396 public function provideDbTypes() {
1398 [ false, DB_SLAVE
],
1399 [ true, DB_MASTER
],
1404 * @dataProvider provideDbTypes
1406 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1407 $mockDb = $this->getMockDb();
1408 $mockCache = $this->getMockCache();
1409 $mockLoadBalancer = $this->getMockLoadBalancer( $mockDb, $dbType );
1410 $user = $this->getMockNonAnonUserWithId( 1 );
1412 $mockDb->expects( $this->once() )
1413 ->method( 'select' )
1416 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1418 $this->isType( 'string' ),
1419 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1421 ->will( $this->returnValue( [] ) );
1423 $store = $this->newWatchedItemStore(
1428 $watchedItems = $store->getWatchedItemsForUser(
1430 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore
::SORT_ASC
]
1432 $this->assertEquals( [], $watchedItems );
1435 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
1436 $store = $this->newWatchedItemStore(
1437 $this->getMockLoadBalancer( $this->getMockDb() ),
1438 $this->getMockCache()
1441 $this->setExpectedException( 'InvalidArgumentException' );
1442 $store->getWatchedItemsForUser(
1443 $this->getMockNonAnonUserWithId( 1 ),
1448 public function testIsWatchedItem_existingItem() {
1449 $mockDb = $this->getMockDb();
1450 $mockDb->expects( $this->once() )
1451 ->method( 'selectRow' )
1454 'wl_notificationtimestamp',
1457 'wl_namespace' => 0,
1458 'wl_title' => 'SomeDbKey',
1461 ->will( $this->returnValue(
1462 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1465 $mockCache = $this->getMockCache();
1466 $mockCache->expects( $this->never() )->method( 'delete' );
1467 $mockCache->expects( $this->once() )
1469 ->with( '0:SomeDbKey:1' )
1470 ->will( $this->returnValue( false ) );
1471 $mockCache->expects( $this->once() )
1477 $store = $this->newWatchedItemStore(
1478 $this->getMockLoadBalancer( $mockDb ),
1484 $this->getMockNonAnonUserWithId( 1 ),
1485 new TitleValue( 0, 'SomeDbKey' )
1490 public function testIsWatchedItem_noItem() {
1491 $mockDb = $this->getMockDb();
1492 $mockDb->expects( $this->once() )
1493 ->method( 'selectRow' )
1496 'wl_notificationtimestamp',
1499 'wl_namespace' => 0,
1500 'wl_title' => 'SomeDbKey',
1503 ->will( $this->returnValue( [] ) );
1505 $mockCache = $this->getMockCache();
1506 $mockCache->expects( $this->never() )->method( 'set' );
1507 $mockCache->expects( $this->never() )->method( 'delete' );
1508 $mockCache->expects( $this->once() )
1510 ->with( '0:SomeDbKey:1' )
1511 ->will( $this->returnValue( false ) );
1513 $store = $this->newWatchedItemStore(
1514 $this->getMockLoadBalancer( $mockDb ),
1520 $this->getMockNonAnonUserWithId( 1 ),
1521 new TitleValue( 0, 'SomeDbKey' )
1526 public function testIsWatchedItem_anonymousUser() {
1527 $mockDb = $this->getMockDb();
1528 $mockDb->expects( $this->never() )
1529 ->method( 'selectRow' );
1531 $mockCache = $this->getMockCache();
1532 $mockCache->expects( $this->never() )->method( 'set' );
1533 $mockCache->expects( $this->never() )->method( 'get' );
1534 $mockCache->expects( $this->never() )->method( 'delete' );
1536 $store = $this->newWatchedItemStore(
1537 $this->getMockLoadBalancer( $mockDb ),
1543 $this->getAnonUser(),
1544 new TitleValue( 0, 'SomeDbKey' )
1549 public function testGetNotificationTimestampsBatch() {
1551 new TitleValue( 0, 'SomeDbKey' ),
1552 new TitleValue( 1, 'AnotherDbKey' ),
1555 $mockDb = $this->getMockDb();
1557 $this->getFakeRow( [
1558 'wl_namespace' => 0,
1559 'wl_title' => 'SomeDbKey',
1560 'wl_notificationtimestamp' => '20151212010101',
1564 'wl_namespace' => 1,
1565 'wl_title' => 'AnotherDbKey',
1566 'wl_notificationtimestamp' => null,
1571 $mockDb->expects( $this->once() )
1572 ->method( 'makeWhereFrom2d' )
1574 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1575 $this->isType( 'string' ),
1576 $this->isType( 'string' )
1578 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1579 $mockDb->expects( $this->once() )
1580 ->method( 'select' )
1583 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1585 'makeWhereFrom2d return value',
1588 $this->isType( 'string' )
1590 ->will( $this->returnValue( $dbResult ) );
1592 $mockCache = $this->getMockCache();
1593 $mockCache->expects( $this->exactly( 2 ) )
1596 [ '0:SomeDbKey:1' ],
1597 [ '1:AnotherDbKey:1' ]
1599 ->will( $this->returnValue( null ) );
1600 $mockCache->expects( $this->never() )->method( 'set' );
1601 $mockCache->expects( $this->never() )->method( 'delete' );
1603 $store = $this->newWatchedItemStore(
1604 $this->getMockLoadBalancer( $mockDb ),
1608 $this->assertEquals(
1610 0 => [ 'SomeDbKey' => '20151212010101', ],
1611 1 => [ 'AnotherDbKey' => null, ],
1613 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1617 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1619 new TitleValue( 0, 'OtherDbKey' ),
1622 $mockDb = $this->getMockDb();
1624 $mockDb->expects( $this->once() )
1625 ->method( 'makeWhereFrom2d' )
1627 [ [ 'OtherDbKey' => 1 ] ],
1628 $this->isType( 'string' ),
1629 $this->isType( 'string' )
1631 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1632 $mockDb->expects( $this->once() )
1633 ->method( 'select' )
1636 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1638 'makeWhereFrom2d return value',
1641 $this->isType( 'string' )
1643 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1645 $mockCache = $this->getMockCache();
1646 $mockCache->expects( $this->once() )
1648 ->with( '0:OtherDbKey:1' )
1649 ->will( $this->returnValue( null ) );
1650 $mockCache->expects( $this->never() )->method( 'set' );
1651 $mockCache->expects( $this->never() )->method( 'delete' );
1653 $store = $this->newWatchedItemStore(
1654 $this->getMockLoadBalancer( $mockDb ),
1658 $this->assertEquals(
1660 0 => [ 'OtherDbKey' => false, ],
1662 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1666 public function testGetNotificationTimestampsBatch_cachedItem() {
1668 new TitleValue( 0, 'SomeDbKey' ),
1669 new TitleValue( 1, 'AnotherDbKey' ),
1672 $user = $this->getMockNonAnonUserWithId( 1 );
1673 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1675 $mockDb = $this->getMockDb();
1677 $mockDb->expects( $this->once() )
1678 ->method( 'makeWhereFrom2d' )
1680 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1681 $this->isType( 'string' ),
1682 $this->isType( 'string' )
1684 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1685 $mockDb->expects( $this->once() )
1686 ->method( 'select' )
1689 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1691 'makeWhereFrom2d return value',
1694 $this->isType( 'string' )
1696 ->will( $this->returnValue( [
1698 [ 'wl_namespace' => 1, 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1702 $mockCache = $this->getMockCache();
1703 $mockCache->expects( $this->at( 1 ) )
1705 ->with( '0:SomeDbKey:1' )
1706 ->will( $this->returnValue( $cachedItem ) );
1707 $mockCache->expects( $this->at( 3 ) )
1709 ->with( '1:AnotherDbKey:1' )
1710 ->will( $this->returnValue( null ) );
1711 $mockCache->expects( $this->never() )->method( 'set' );
1712 $mockCache->expects( $this->never() )->method( 'delete' );
1714 $store = $this->newWatchedItemStore(
1715 $this->getMockLoadBalancer( $mockDb ),
1719 $this->assertEquals(
1721 0 => [ 'SomeDbKey' => '20151212010101', ],
1722 1 => [ 'AnotherDbKey' => null, ],
1724 $store->getNotificationTimestampsBatch( $user, $targets )
1728 public function testGetNotificationTimestampsBatch_allItemsCached() {
1730 new TitleValue( 0, 'SomeDbKey' ),
1731 new TitleValue( 1, 'AnotherDbKey' ),
1734 $user = $this->getMockNonAnonUserWithId( 1 );
1736 new WatchedItem( $user, $targets[0], '20151212010101' ),
1737 new WatchedItem( $user, $targets[1], null ),
1739 $mockDb = $this->getMockDb();
1740 $mockDb->expects( $this->never() )->method( $this->anything() );
1742 $mockCache = $this->getMockCache();
1743 $mockCache->expects( $this->at( 1 ) )
1745 ->with( '0:SomeDbKey:1' )
1746 ->will( $this->returnValue( $cachedItems[0] ) );
1747 $mockCache->expects( $this->at( 3 ) )
1749 ->with( '1:AnotherDbKey:1' )
1750 ->will( $this->returnValue( $cachedItems[1] ) );
1751 $mockCache->expects( $this->never() )->method( 'set' );
1752 $mockCache->expects( $this->never() )->method( 'delete' );
1754 $store = $this->newWatchedItemStore(
1755 $this->getMockLoadBalancer( $mockDb ),
1759 $this->assertEquals(
1761 0 => [ 'SomeDbKey' => '20151212010101', ],
1762 1 => [ 'AnotherDbKey' => null, ],
1764 $store->getNotificationTimestampsBatch( $user, $targets )
1768 public function testGetNotificationTimestampsBatch_anonymousUser() {
1770 new TitleValue( 0, 'SomeDbKey' ),
1771 new TitleValue( 1, 'AnotherDbKey' ),
1774 $mockDb = $this->getMockDb();
1775 $mockDb->expects( $this->never() )->method( $this->anything() );
1777 $mockCache = $this->getMockCache();
1778 $mockCache->expects( $this->never() )->method( $this->anything() );
1780 $store = $this->newWatchedItemStore(
1781 $this->getMockLoadBalancer( $mockDb ),
1785 $this->assertEquals(
1787 0 => [ 'SomeDbKey' => false, ],
1788 1 => [ 'AnotherDbKey' => false, ],
1790 $store->getNotificationTimestampsBatch( $this->getAnonUser(), $targets )
1794 public function testResetNotificationTimestamp_anonymousUser() {
1795 $mockDb = $this->getMockDb();
1796 $mockDb->expects( $this->never() )
1797 ->method( 'selectRow' );
1799 $mockCache = $this->getMockCache();
1800 $mockCache->expects( $this->never() )->method( 'get' );
1801 $mockCache->expects( $this->never() )->method( 'set' );
1802 $mockCache->expects( $this->never() )->method( 'delete' );
1804 $store = $this->newWatchedItemStore(
1805 $this->getMockLoadBalancer( $mockDb ),
1810 $store->resetNotificationTimestamp(
1811 $this->getAnonUser(),
1812 Title
::newFromText( 'SomeDbKey' )
1817 public function testResetNotificationTimestamp_noItem() {
1818 $mockDb = $this->getMockDb();
1819 $mockDb->expects( $this->once() )
1820 ->method( 'selectRow' )
1823 'wl_notificationtimestamp',
1826 'wl_namespace' => 0,
1827 'wl_title' => 'SomeDbKey',
1830 ->will( $this->returnValue( [] ) );
1832 $mockCache = $this->getMockCache();
1833 $mockCache->expects( $this->never() )->method( 'get' );
1834 $mockCache->expects( $this->never() )->method( 'set' );
1835 $mockCache->expects( $this->never() )->method( 'delete' );
1837 $store = $this->newWatchedItemStore(
1838 $this->getMockLoadBalancer( $mockDb ),
1843 $store->resetNotificationTimestamp(
1844 $this->getMockNonAnonUserWithId( 1 ),
1845 Title
::newFromText( 'SomeDbKey' )
1850 public function testResetNotificationTimestamp_item() {
1851 $user = $this->getMockNonAnonUserWithId( 1 );
1852 $title = Title
::newFromText( 'SomeDbKey' );
1854 $mockDb = $this->getMockDb();
1855 $mockDb->expects( $this->once() )
1856 ->method( 'selectRow' )
1859 'wl_notificationtimestamp',
1862 'wl_namespace' => 0,
1863 'wl_title' => 'SomeDbKey',
1866 ->will( $this->returnValue(
1867 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1870 $mockCache = $this->getMockCache();
1871 $mockCache->expects( $this->never() )->method( 'get' );
1872 $mockCache->expects( $this->once() )
1876 $this->isInstanceOf( WatchedItem
::class )
1878 $mockCache->expects( $this->once() )
1879 ->method( 'delete' )
1880 ->with( '0:SomeDbKey:1' );
1882 $store = $this->newWatchedItemStore(
1883 $this->getMockLoadBalancer( $mockDb ),
1887 // Note: This does not actually assert the job is correct
1888 $callableCallCounter = 0;
1889 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1890 $callableCallCounter++
;
1891 $this->assertInternalType( 'callable', $callable );
1893 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1896 $store->resetNotificationTimestamp(
1901 $this->assertEquals( 1, $callableCallCounter );
1903 ScopedCallback
::consume( $scopedOverride );
1906 public function testResetNotificationTimestamp_noItemForced() {
1907 $user = $this->getMockNonAnonUserWithId( 1 );
1908 $title = Title
::newFromText( 'SomeDbKey' );
1910 $mockDb = $this->getMockDb();
1911 $mockDb->expects( $this->never() )
1912 ->method( 'selectRow' );
1914 $mockCache = $this->getMockCache();
1915 $mockDb->expects( $this->never() )
1917 $mockDb->expects( $this->never() )
1919 $mockDb->expects( $this->never() )
1920 ->method( 'delete' );
1922 $store = $this->newWatchedItemStore(
1923 $this->getMockLoadBalancer( $mockDb ),
1927 // Note: This does not actually assert the job is correct
1928 $callableCallCounter = 0;
1929 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1930 $callableCallCounter++
;
1931 $this->assertInternalType( 'callable', $callable );
1933 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1936 $store->resetNotificationTimestamp(
1942 $this->assertEquals( 1, $callableCallCounter );
1944 ScopedCallback
::consume( $scopedOverride );
1951 * @return PHPUnit_Framework_MockObject_MockObject|Title
1953 private function getMockTitle( $text, $ns = 0 ) {
1954 $title = $this->getMock( Title
::class );
1955 $title->expects( $this->any() )
1956 ->method( 'getText' )
1957 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
1958 $title->expects( $this->any() )
1959 ->method( 'getDbKey' )
1960 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
1961 $title->expects( $this->any() )
1962 ->method( 'getNamespace' )
1963 ->will( $this->returnValue( $ns ) );
1967 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
1968 $user = $this->getMockNonAnonUserWithId( 1 );
1970 $title = $this->getMockTitle( 'SomeTitle' );
1971 $title->expects( $this->once() )
1972 ->method( 'getNextRevisionID' )
1974 ->will( $this->returnValue( false ) );
1976 $mockDb = $this->getMockDb();
1977 $mockDb->expects( $this->never() )
1978 ->method( 'selectRow' );
1980 $mockCache = $this->getMockCache();
1981 $mockDb->expects( $this->never() )
1983 $mockDb->expects( $this->never() )
1985 $mockDb->expects( $this->never() )
1986 ->method( 'delete' );
1988 $store = $this->newWatchedItemStore(
1989 $this->getMockLoadBalancer( $mockDb ),
1993 // Note: This does not actually assert the job is correct
1994 $callableCallCounter = 0;
1995 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
1996 function( $callable ) use ( &$callableCallCounter ) {
1997 $callableCallCounter++
;
1998 $this->assertInternalType( 'callable', $callable );
2003 $store->resetNotificationTimestamp(
2010 $this->assertEquals( 1, $callableCallCounter );
2012 ScopedCallback
::consume( $scopedOverride );
2015 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced() {
2016 $user = $this->getMockNonAnonUserWithId( 1 );
2018 $title = $this->getMockTitle( 'SomeDbKey' );
2019 $title->expects( $this->once() )
2020 ->method( 'getNextRevisionID' )
2022 ->will( $this->returnValue( 33 ) );
2024 $mockDb = $this->getMockDb();
2025 $mockDb->expects( $this->once() )
2026 ->method( 'selectRow' )
2029 'wl_notificationtimestamp',
2032 'wl_namespace' => 0,
2033 'wl_title' => 'SomeDbKey',
2036 ->will( $this->returnValue(
2037 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2040 $mockCache = $this->getMockCache();
2041 $mockDb->expects( $this->never() )
2043 $mockDb->expects( $this->never() )
2045 $mockDb->expects( $this->never() )
2046 ->method( 'delete' );
2048 $store = $this->newWatchedItemStore(
2049 $this->getMockLoadBalancer( $mockDb ),
2053 // Note: This does not actually assert the job is correct
2054 $addUpdateCallCounter = 0;
2055 $scopedOverrideDeferred = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
2056 function( $callable ) use ( &$addUpdateCallCounter ) {
2057 $addUpdateCallCounter++
;
2058 $this->assertInternalType( 'callable', $callable );
2062 $getTimestampCallCounter = 0;
2063 $scopedOverrideRevision = $store->overrideRevisionGetTimestampFromIdCallback(
2064 function( $titleParam, $oldidParam ) use ( &$getTimestampCallCounter, $title, $oldid ) {
2065 $getTimestampCallCounter++
;
2066 $this->assertEquals( $title, $titleParam );
2067 $this->assertEquals( $oldid, $oldidParam );
2072 $store->resetNotificationTimestamp(
2079 $this->assertEquals( 1, $addUpdateCallCounter );
2080 $this->assertEquals( 1, $getTimestampCallCounter );
2082 ScopedCallback
::consume( $scopedOverrideDeferred );
2083 ScopedCallback
::consume( $scopedOverrideRevision );
2086 public function testUpdateNotificationTimestamp_watchersExist() {
2087 $mockDb = $this->getMockDb();
2088 $mockDb->expects( $this->once() )
2089 ->method( 'select' )
2095 'wl_namespace' => 0,
2096 'wl_title' => 'SomeDbKey',
2097 'wl_notificationtimestamp IS NULL'
2101 $this->returnValue( [
2102 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2103 $this->getFakeRow( [ 'wl_user' => '3' ] )
2106 $mockDb->expects( $this->once() )
2107 ->method( 'onTransactionIdle' )
2108 ->with( $this->isType( 'callable' ) )
2109 ->will( $this->returnCallback( function( $callable ) {
2112 $mockDb->expects( $this->once() )
2113 ->method( 'update' )
2116 [ 'wl_notificationtimestamp' => null ],
2118 'wl_user' => [ 2, 3 ],
2119 'wl_namespace' => 0,
2120 'wl_title' => 'SomeDbKey',
2124 $mockCache = $this->getMockCache();
2125 $mockCache->expects( $this->never() )->method( 'set' );
2126 $mockCache->expects( $this->never() )->method( 'get' );
2127 $mockCache->expects( $this->never() )->method( 'delete' );
2129 $store = $this->newWatchedItemStore(
2130 $this->getMockLoadBalancer( $mockDb ),
2134 $this->assertEquals(
2136 $store->updateNotificationTimestamp(
2137 $this->getMockNonAnonUserWithId( 1 ),
2138 new TitleValue( 0, 'SomeDbKey' ),
2144 public function testUpdateNotificationTimestamp_noWatchers() {
2145 $mockDb = $this->getMockDb();
2146 $mockDb->expects( $this->once() )
2147 ->method( 'select' )
2153 'wl_namespace' => 0,
2154 'wl_title' => 'SomeDbKey',
2155 'wl_notificationtimestamp IS NULL'
2159 $this->returnValue( [] )
2161 $mockDb->expects( $this->never() )
2162 ->method( 'onTransactionIdle' );
2163 $mockDb->expects( $this->never() )
2164 ->method( 'update' );
2166 $mockCache = $this->getMockCache();
2167 $mockCache->expects( $this->never() )->method( 'set' );
2168 $mockCache->expects( $this->never() )->method( 'get' );
2169 $mockCache->expects( $this->never() )->method( 'delete' );
2171 $store = $this->newWatchedItemStore(
2172 $this->getMockLoadBalancer( $mockDb ),
2176 $watchers = $store->updateNotificationTimestamp(
2177 $this->getMockNonAnonUserWithId( 1 ),
2178 new TitleValue( 0, 'SomeDbKey' ),
2181 $this->assertInternalType( 'array', $watchers );
2182 $this->assertEmpty( $watchers );
2185 public function testUpdateNotificationTimestamp_clearsCachedItems() {
2186 $user = $this->getMockNonAnonUserWithId( 1 );
2187 $titleValue = new TitleValue( 0, 'SomeDbKey' );
2189 $mockDb = $this->getMockDb();
2190 $mockDb->expects( $this->once() )
2191 ->method( 'selectRow' )
2192 ->will( $this->returnValue(
2193 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2195 $mockDb->expects( $this->once() )
2196 ->method( 'select' )
2198 $this->returnValue( [
2199 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2200 $this->getFakeRow( [ 'wl_user' => '3' ] )
2203 $mockDb->expects( $this->once() )
2204 ->method( 'onTransactionIdle' )
2205 ->with( $this->isType( 'callable' ) )
2206 ->will( $this->returnCallback( function( $callable ) {
2209 $mockDb->expects( $this->once() )
2210 ->method( 'update' );
2212 $mockCache = $this->getMockCache();
2213 $mockCache->expects( $this->once() )
2215 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2216 $mockCache->expects( $this->once() )
2218 ->with( '0:SomeDbKey:1' );
2219 $mockCache->expects( $this->once() )
2220 ->method( 'delete' )
2221 ->with( '0:SomeDbKey:1' );
2223 $store = $this->newWatchedItemStore(
2224 $this->getMockLoadBalancer( $mockDb ),
2228 // This will add the item to the cache
2229 $store->getWatchedItem( $user, $titleValue );
2231 $store->updateNotificationTimestamp(
2232 $this->getMockNonAnonUserWithId( 1 ),