6 * @covers WatchedItemStore
8 class WatchedItemStoreUnitTest
extends PHPUnit_Framework_TestCase
{
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 public function testGetDefaultInstance() {
86 $instanceOne = WatchedItemStore
::getDefaultInstance();
87 $instanceTwo = WatchedItemStore
::getDefaultInstance();
88 $this->assertSame( $instanceOne, $instanceTwo );
91 public function testOverrideDefaultInstance() {
92 $instance = WatchedItemStore
::getDefaultInstance();
93 $scopedOverride = $instance->overrideDefaultInstance( null );
95 $this->assertNotSame( $instance, WatchedItemStore
::getDefaultInstance() );
97 unset( $scopedOverride );
99 $this->assertSame( $instance, WatchedItemStore
::getDefaultInstance() );
102 public function testCountWatchedItems() {
103 $user = $this->getMockNonAnonUserWithId( 1 );
105 $mockDb = $this->getMockDb();
106 $mockDb->expects( $this->exactly( 1 ) )
107 ->method( 'selectField' )
112 'wl_user' => $user->getId(),
114 $this->isType( 'string' )
116 ->will( $this->returnValue( 12 ) );
118 $mockCache = $this->getMockCache();
119 $mockCache->expects( $this->never() )->method( 'get' );
120 $mockCache->expects( $this->never() )->method( 'set' );
121 $mockCache->expects( $this->never() )->method( 'delete' );
123 $store = new WatchedItemStore(
124 $this->getMockLoadBalancer( $mockDb ),
128 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
131 public function testCountWatchers() {
132 $titleValue = new TitleValue( 0, 'SomeDbKey' );
134 $mockDb = $this->getMockDb();
135 $mockDb->expects( $this->exactly( 1 ) )
136 ->method( 'selectField' )
141 'wl_namespace' => $titleValue->getNamespace(),
142 'wl_title' => $titleValue->getDBkey(),
144 $this->isType( 'string' )
146 ->will( $this->returnValue( 7 ) );
148 $mockCache = $this->getMockCache();
149 $mockCache->expects( $this->never() )->method( 'get' );
150 $mockCache->expects( $this->never() )->method( 'set' );
151 $mockCache->expects( $this->never() )->method( 'delete' );
153 $store = new WatchedItemStore(
154 $this->getMockLoadBalancer( $mockDb ),
158 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
161 public function testCountWatchersMultiple() {
163 new TitleValue( 0, 'SomeDbKey' ),
164 new TitleValue( 0, 'OtherDbKey' ),
165 new TitleValue( 1, 'AnotherDbKey' ),
168 $mockDb = $this->getMockDb();
171 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
172 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
173 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
176 $mockDb->expects( $this->once() )
177 ->method( 'makeWhereFrom2d' )
179 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
180 $this->isType( 'string' ),
181 $this->isType( 'string' )
183 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
184 $mockDb->expects( $this->once() )
188 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
189 [ 'makeWhereFrom2d return value' ],
190 $this->isType( 'string' ),
192 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
196 $this->returnValue( $dbResult )
199 $mockCache = $this->getMockCache();
200 $mockCache->expects( $this->never() )->method( 'get' );
201 $mockCache->expects( $this->never() )->method( 'set' );
202 $mockCache->expects( $this->never() )->method( 'delete' );
204 $store = new WatchedItemStore(
205 $this->getMockLoadBalancer( $mockDb ),
210 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
211 1 => [ 'AnotherDbKey' => 500 ],
213 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
216 public function provideIntWithDbUnsafeVersion() {
219 [ "50; DROP TABLE watchlist;\n--" ],
224 * @dataProvider provideIntWithDbUnsafeVersion
226 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
228 new TitleValue( 0, 'SomeDbKey' ),
229 new TitleValue( 0, 'OtherDbKey' ),
230 new TitleValue( 1, 'AnotherDbKey' ),
233 $mockDb = $this->getMockDb();
236 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
237 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
238 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
241 $mockDb->expects( $this->once() )
242 ->method( 'makeWhereFrom2d' )
244 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
245 $this->isType( 'string' ),
246 $this->isType( 'string' )
248 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
249 $mockDb->expects( $this->once() )
253 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
254 [ 'makeWhereFrom2d return value' ],
255 $this->isType( 'string' ),
257 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
258 'HAVING' => 'COUNT(*) >= 50',
262 $this->returnValue( $dbResult )
265 $mockCache = $this->getMockCache();
266 $mockCache->expects( $this->never() )->method( 'get' );
267 $mockCache->expects( $this->never() )->method( 'set' );
268 $mockCache->expects( $this->never() )->method( 'delete' );
270 $store = new WatchedItemStore(
271 $this->getMockLoadBalancer( $mockDb ),
276 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
277 1 => [ 'AnotherDbKey' => 500 ],
281 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
285 public function testCountVisitingWatchers() {
286 $titleValue = new TitleValue( 0, 'SomeDbKey' );
288 $mockDb = $this->getMockDb();
289 $mockDb->expects( $this->exactly( 1 ) )
290 ->method( 'selectField' )
295 'wl_namespace' => $titleValue->getNamespace(),
296 'wl_title' => $titleValue->getDBkey(),
297 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
299 $this->isType( 'string' )
301 ->will( $this->returnValue( 7 ) );
302 $mockDb->expects( $this->exactly( 1 ) )
303 ->method( 'addQuotes' )
304 ->will( $this->returnCallback( function( $value ) {
307 $mockDb->expects( $this->exactly( 1 ) )
308 ->method( 'timestamp' )
309 ->will( $this->returnCallback( function( $value ) {
310 return 'TS' . $value . 'TS';
313 $mockCache = $this->getMockCache();
314 $mockCache->expects( $this->never() )->method( 'set' );
315 $mockCache->expects( $this->never() )->method( 'get' );
316 $mockCache->expects( $this->never() )->method( 'delete' );
318 $store = new WatchedItemStore(
319 $this->getMockLoadBalancer( $mockDb ),
323 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
326 public function testCountVisitingWatchersMultiple() {
327 $titleValuesWithThresholds = [
328 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
329 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
330 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
334 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
335 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
336 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
338 $mockDb = $this->getMockDb();
339 $mockDb->expects( $this->exactly( 2 * 3 ) )
340 ->method( 'addQuotes' )
341 ->will( $this->returnCallback( function( $value ) {
344 $mockDb->expects( $this->exactly( 3 ) )
345 ->method( 'timestamp' )
346 ->will( $this->returnCallback( function( $value ) {
347 return 'TS' . $value . 'TS';
349 $mockDb->expects( $this->any() )
350 ->method( 'makeList' )
352 $this->isType( 'array' ),
353 $this->isType( 'int' )
355 ->will( $this->returnCallback( function( $a, $conj ) {
356 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
357 return join( $sqlConj, array_map( function( $s ) {
358 return '(' . $s . ')';
362 $mockDb->expects( $this->never() )
363 ->method( 'makeWhereFrom2d' );
366 '((wl_namespace = 0) AND (' .
367 "(((wl_title = 'SomeDbKey') AND (" .
368 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
370 "(wl_title = 'OtherDbKey') AND (" .
371 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
373 ') OR ((wl_namespace = 1) AND (' .
374 "(((wl_title = 'AnotherDbKey') AND (".
375 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
377 $mockDb->expects( $this->once() )
381 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
383 $this->isType( 'string' ),
385 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
389 $this->returnValue( $dbResult )
392 $mockCache = $this->getMockCache();
393 $mockCache->expects( $this->never() )->method( 'get' );
394 $mockCache->expects( $this->never() )->method( 'set' );
395 $mockCache->expects( $this->never() )->method( 'delete' );
397 $store = new WatchedItemStore(
398 $this->getMockLoadBalancer( $mockDb ),
403 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
404 1 => [ 'AnotherDbKey' => 500 ],
408 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
412 public function testCountVisitingWatchersMultiple_withMissingTargets() {
413 $titleValuesWithThresholds = [
414 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
415 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
416 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
417 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
418 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
422 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
423 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
424 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
426 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 100 ]
429 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 200 ]
432 $mockDb = $this->getMockDb();
433 $mockDb->expects( $this->exactly( 2 * 3 ) )
434 ->method( 'addQuotes' )
435 ->will( $this->returnCallback( function( $value ) {
438 $mockDb->expects( $this->exactly( 3 ) )
439 ->method( 'timestamp' )
440 ->will( $this->returnCallback( function( $value ) {
441 return 'TS' . $value . 'TS';
443 $mockDb->expects( $this->any() )
444 ->method( 'makeList' )
446 $this->isType( 'array' ),
447 $this->isType( 'int' )
449 ->will( $this->returnCallback( function( $a, $conj ) {
450 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
451 return join( $sqlConj, array_map( function( $s ) {
452 return '(' . $s . ')';
456 $mockDb->expects( $this->once() )
457 ->method( 'makeWhereFrom2d' )
459 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
460 $this->isType( 'string' ),
461 $this->isType( 'string' )
463 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
466 '((wl_namespace = 0) AND (' .
467 "(((wl_title = 'SomeDbKey') AND (" .
468 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
470 "(wl_title = 'OtherDbKey') AND (" .
471 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
473 ') OR ((wl_namespace = 1) AND (' .
474 "(((wl_title = 'AnotherDbKey') AND (".
475 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
478 '(makeWhereFrom2d return value)';
479 $mockDb->expects( $this->once() )
483 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
485 $this->isType( 'string' ),
487 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
491 $this->returnValue( $dbResult )
494 $mockCache = $this->getMockCache();
495 $mockCache->expects( $this->never() )->method( 'get' );
496 $mockCache->expects( $this->never() )->method( 'set' );
497 $mockCache->expects( $this->never() )->method( 'delete' );
499 $store = new WatchedItemStore(
500 $this->getMockLoadBalancer( $mockDb ),
506 'SomeDbKey' => 100, 'OtherDbKey' => 300,
507 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
509 1 => [ 'AnotherDbKey' => 500 ],
513 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
518 * @dataProvider provideIntWithDbUnsafeVersion
520 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
521 $titleValuesWithThresholds = [
522 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
523 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
524 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
527 $mockDb = $this->getMockDb();
528 $mockDb->expects( $this->any() )
529 ->method( 'makeList' )
530 ->will( $this->returnValue( 'makeList return value' ) );
531 $mockDb->expects( $this->once() )
535 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
536 'makeList return value',
537 $this->isType( 'string' ),
539 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
540 'HAVING' => 'COUNT(*) >= 50',
544 $this->returnValue( [] )
547 $mockCache = $this->getMockCache();
548 $mockCache->expects( $this->never() )->method( 'get' );
549 $mockCache->expects( $this->never() )->method( 'set' );
550 $mockCache->expects( $this->never() )->method( 'delete' );
552 $store = new WatchedItemStore(
553 $this->getMockLoadBalancer( $mockDb ),
558 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
559 1 => [ 'AnotherDbKey' => 0 ],
563 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
567 public function testCountUnreadNotifications() {
568 $user = $this->getMockNonAnonUserWithId( 1 );
570 $mockDb = $this->getMockDb();
571 $mockDb->expects( $this->exactly( 1 ) )
572 ->method( 'selectRowCount' )
577 "wl_notificationtimestamp IS NOT NULL",
580 $this->isType( 'string' )
582 ->will( $this->returnValue( 9 ) );
584 $mockCache = $this->getMockCache();
585 $mockCache->expects( $this->never() )->method( 'set' );
586 $mockCache->expects( $this->never() )->method( 'get' );
587 $mockCache->expects( $this->never() )->method( 'delete' );
589 $store = new WatchedItemStore(
590 $this->getMockLoadBalancer( $mockDb ),
594 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
598 * @dataProvider provideIntWithDbUnsafeVersion
600 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
601 $user = $this->getMockNonAnonUserWithId( 1 );
603 $mockDb = $this->getMockDb();
604 $mockDb->expects( $this->exactly( 1 ) )
605 ->method( 'selectRowCount' )
610 "wl_notificationtimestamp IS NOT NULL",
613 $this->isType( 'string' ),
616 ->will( $this->returnValue( 50 ) );
618 $mockCache = $this->getMockCache();
619 $mockCache->expects( $this->never() )->method( 'set' );
620 $mockCache->expects( $this->never() )->method( 'get' );
621 $mockCache->expects( $this->never() )->method( 'delete' );
623 $store = new WatchedItemStore(
624 $this->getMockLoadBalancer( $mockDb ),
630 $store->countUnreadNotifications( $user, $limit )
635 * @dataProvider provideIntWithDbUnsafeVersion
637 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
638 $user = $this->getMockNonAnonUserWithId( 1 );
640 $mockDb = $this->getMockDb();
641 $mockDb->expects( $this->exactly( 1 ) )
642 ->method( 'selectRowCount' )
647 "wl_notificationtimestamp IS NOT NULL",
650 $this->isType( 'string' ),
653 ->will( $this->returnValue( 9 ) );
655 $mockCache = $this->getMockCache();
656 $mockCache->expects( $this->never() )->method( 'set' );
657 $mockCache->expects( $this->never() )->method( 'get' );
658 $mockCache->expects( $this->never() )->method( 'delete' );
660 $store = new WatchedItemStore(
661 $this->getMockLoadBalancer( $mockDb ),
667 $store->countUnreadNotifications( $user, $limit )
671 public function testDuplicateEntry_nothingToDuplicate() {
672 $mockDb = $this->getMockDb();
673 $mockDb->expects( $this->once() )
679 'wl_notificationtimestamp',
683 'wl_title' => 'Old_Title',
685 'WatchedItemStore::duplicateEntry',
688 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
690 $store = new WatchedItemStore(
691 $this->getMockLoadBalancer( $mockDb ),
692 $this->getMockCache()
695 $store->duplicateEntry(
696 Title
::newFromText( 'Old_Title' ),
697 Title
::newFromText( 'New_Title' )
701 public function testDuplicateEntry_somethingToDuplicate() {
703 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
704 $this->getFakeRow( [ 'wl_user' => 2, 'wl_notificationtimestamp' => null ] ),
707 $mockDb = $this->getMockDb();
708 $mockDb->expects( $this->at( 0 ) )
714 'wl_notificationtimestamp',
718 'wl_title' => 'Old_Title',
721 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
722 $mockDb->expects( $this->at( 1 ) )
723 ->method( 'replace' )
726 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
731 'wl_title' => 'New_Title',
732 'wl_notificationtimestamp' => '20151212010101',
737 'wl_title' => 'New_Title',
738 'wl_notificationtimestamp' => null,
741 $this->isType( 'string' )
744 $mockCache = $this->getMockCache();
745 $mockCache->expects( $this->never() )->method( 'get' );
746 $mockCache->expects( $this->never() )->method( 'delete' );
748 $store = new WatchedItemStore(
749 $this->getMockLoadBalancer( $mockDb ),
753 $store->duplicateEntry(
754 Title
::newFromText( 'Old_Title' ),
755 Title
::newFromText( 'New_Title' )
759 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
760 $mockDb = $this->getMockDb();
761 $mockDb->expects( $this->at( 0 ) )
767 'wl_notificationtimestamp',
771 'wl_title' => 'Old_Title',
774 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
775 $mockDb->expects( $this->at( 1 ) )
781 'wl_notificationtimestamp',
785 'wl_title' => 'Old_Title',
788 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
790 $mockCache = $this->getMockCache();
791 $mockCache->expects( $this->never() )->method( 'get' );
792 $mockCache->expects( $this->never() )->method( 'delete' );
794 $store = new WatchedItemStore(
795 $this->getMockLoadBalancer( $mockDb ),
799 $store->duplicateAllAssociatedEntries(
800 Title
::newFromText( 'Old_Title' ),
801 Title
::newFromText( 'New_Title' )
805 public function testDuplicateAllAssociatedEntries_somethingToDuplicate() {
807 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
810 $mockDb = $this->getMockDb();
811 $mockDb->expects( $this->at( 0 ) )
817 'wl_notificationtimestamp',
821 'wl_title' => 'Old_Title',
824 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
825 $mockDb->expects( $this->at( 1 ) )
826 ->method( 'replace' )
829 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
834 'wl_title' => 'New_Title',
835 'wl_notificationtimestamp' => '20151212010101',
838 $this->isType( 'string' )
840 $mockDb->expects( $this->at( 2 ) )
846 'wl_notificationtimestamp',
850 'wl_title' => 'Old_Title',
853 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
854 $mockDb->expects( $this->at( 3 ) )
855 ->method( 'replace' )
858 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
863 'wl_title' => 'New_Title',
864 'wl_notificationtimestamp' => '20151212010101',
867 $this->isType( 'string' )
870 $mockCache = $this->getMockCache();
871 $mockCache->expects( $this->never() )->method( 'get' );
872 $mockCache->expects( $this->never() )->method( 'delete' );
874 $store = new WatchedItemStore(
875 $this->getMockLoadBalancer( $mockDb ),
879 $store->duplicateAllAssociatedEntries(
880 Title
::newFromText( 'Old_Title' ),
881 Title
::newFromText( 'New_Title' )
885 public function testAddWatch_nonAnonymousUser() {
886 $mockDb = $this->getMockDb();
887 $mockDb->expects( $this->once() )
895 'wl_title' => 'Some_Page',
896 'wl_notificationtimestamp' => null,
901 $mockCache = $this->getMockCache();
902 $mockCache->expects( $this->once() )
904 ->with( '0:Some_Page:1' );
906 $store = new WatchedItemStore(
907 $this->getMockLoadBalancer( $mockDb ),
912 $this->getMockNonAnonUserWithId( 1 ),
913 Title
::newFromText( 'Some_Page' )
917 public function testAddWatch_anonymousUser() {
918 $mockDb = $this->getMockDb();
919 $mockDb->expects( $this->never() )
920 ->method( 'insert' );
922 $mockCache = $this->getMockCache();
923 $mockCache->expects( $this->never() )
924 ->method( 'delete' );
926 $store = new WatchedItemStore(
927 $this->getMockLoadBalancer( $mockDb ),
932 $this->getAnonUser(),
933 Title
::newFromText( 'Some_Page' )
937 public function testAddWatchBatch_nonAnonymousUser() {
938 $mockDb = $this->getMockDb();
939 $mockDb->expects( $this->once() )
947 'wl_title' => 'Some_Page',
948 'wl_notificationtimestamp' => null,
953 'wl_title' => 'Some_Page',
954 'wl_notificationtimestamp' => null,
959 $mockCache = $this->getMockCache();
960 $mockCache->expects( $this->exactly( 2 ) )
961 ->method( 'delete' );
962 $mockCache->expects( $this->at( 1 ) )
964 ->with( '0:Some_Page:1' );
965 $mockCache->expects( $this->at( 3 ) )
967 ->with( '1:Some_Page:1' );
969 $store = new WatchedItemStore(
970 $this->getMockLoadBalancer( $mockDb ),
974 $mockUser = $this->getMockNonAnonUserWithId( 1 );
977 $store->addWatchBatch(
979 [ $mockUser, new TitleValue( 0, 'Some_Page' ) ],
980 [ $mockUser, new TitleValue( 1, 'Some_Page' ) ],
986 public function testAddWatchBatch_anonymousUserCombinationsAreSkipped() {
987 $mockDb = $this->getMockDb();
988 $mockDb->expects( $this->once() )
996 'wl_title' => 'Some_Page',
997 'wl_notificationtimestamp' => null,
1002 $mockCache = $this->getMockCache();
1003 $mockCache->expects( $this->once() )
1004 ->method( 'delete' )
1005 ->with( '0:Some_Page:1' );
1007 $store = new WatchedItemStore(
1008 $this->getMockLoadBalancer( $mockDb ),
1013 $store->addWatchBatch(
1015 [ $this->getMockNonAnonUserWithId( 1 ), new TitleValue( 0, 'Some_Page' ) ],
1016 [ $this->getAnonUser(), new TitleValue( 0, 'Other_Page' ) ],
1022 public function testAddWatchBatchReturnsFalse_whenOnlyGivenAnonymousUserCombinations() {
1023 $mockDb = $this->getMockDb();
1024 $mockDb->expects( $this->never() )
1025 ->method( 'insert' );
1027 $mockCache = $this->getMockCache();
1028 $mockCache->expects( $this->never() )
1029 ->method( 'delete' );
1031 $store = new WatchedItemStore(
1032 $this->getMockLoadBalancer( $mockDb ),
1036 $anonUser = $this->getAnonUser();
1038 $store->addWatchBatch(
1040 [ $anonUser, new TitleValue( 0, 'Some_Page' ) ],
1041 [ $anonUser, new TitleValue( 1, 'Other_Page' ) ],
1047 public function testAddWatchBatchReturnsFalse_whenGivenEmptyList() {
1048 $mockDb = $this->getMockDb();
1049 $mockDb->expects( $this->never() )
1050 ->method( 'insert' );
1052 $mockCache = $this->getMockCache();
1053 $mockCache->expects( $this->never() )
1054 ->method( 'delete' );
1056 $store = new WatchedItemStore(
1057 $this->getMockLoadBalancer( $mockDb ),
1062 $store->addWatchBatch( [] )
1066 public function testLoadWatchedItem_existingItem() {
1067 $mockDb = $this->getMockDb();
1068 $mockDb->expects( $this->once() )
1069 ->method( 'selectRow' )
1072 'wl_notificationtimestamp',
1075 'wl_namespace' => 0,
1076 'wl_title' => 'SomeDbKey',
1079 ->will( $this->returnValue(
1080 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1083 $mockCache = $this->getMockCache();
1084 $mockCache->expects( $this->once() )
1090 $store = new WatchedItemStore(
1091 $this->getMockLoadBalancer( $mockDb ),
1095 $watchedItem = $store->loadWatchedItem(
1096 $this->getMockNonAnonUserWithId( 1 ),
1097 new TitleValue( 0, 'SomeDbKey' )
1099 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1100 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1101 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1102 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1105 public function testLoadWatchedItem_noItem() {
1106 $mockDb = $this->getMockDb();
1107 $mockDb->expects( $this->once() )
1108 ->method( 'selectRow' )
1111 'wl_notificationtimestamp',
1114 'wl_namespace' => 0,
1115 'wl_title' => 'SomeDbKey',
1118 ->will( $this->returnValue( [] ) );
1120 $mockCache = $this->getMockCache();
1121 $mockCache->expects( $this->never() )->method( 'get' );
1122 $mockCache->expects( $this->never() )->method( 'delete' );
1124 $store = new WatchedItemStore(
1125 $this->getMockLoadBalancer( $mockDb ),
1130 $store->loadWatchedItem(
1131 $this->getMockNonAnonUserWithId( 1 ),
1132 new TitleValue( 0, 'SomeDbKey' )
1137 public function testLoadWatchedItem_anonymousUser() {
1138 $mockDb = $this->getMockDb();
1139 $mockDb->expects( $this->never() )
1140 ->method( 'selectRow' );
1142 $mockCache = $this->getMockCache();
1143 $mockCache->expects( $this->never() )->method( 'get' );
1144 $mockCache->expects( $this->never() )->method( 'delete' );
1146 $store = new WatchedItemStore(
1147 $this->getMockLoadBalancer( $mockDb ),
1152 $store->loadWatchedItem(
1153 $this->getAnonUser(),
1154 new TitleValue( 0, 'SomeDbKey' )
1159 public function testRemoveWatch_existingItem() {
1160 $mockDb = $this->getMockDb();
1161 $mockDb->expects( $this->once() )
1162 ->method( 'delete' )
1167 'wl_namespace' => 0,
1168 'wl_title' => 'SomeDbKey',
1171 $mockDb->expects( $this->once() )
1172 ->method( 'affectedRows' )
1173 ->will( $this->returnValue( 1 ) );
1175 $mockCache = $this->getMockCache();
1176 $mockCache->expects( $this->never() )->method( 'get' );
1177 $mockCache->expects( $this->once() )
1178 ->method( 'delete' )
1179 ->with( '0:SomeDbKey:1' );
1181 $store = new WatchedItemStore(
1182 $this->getMockLoadBalancer( $mockDb ),
1187 $store->removeWatch(
1188 $this->getMockNonAnonUserWithId( 1 ),
1189 new TitleValue( 0, 'SomeDbKey' )
1194 public function testRemoveWatch_noItem() {
1195 $mockDb = $this->getMockDb();
1196 $mockDb->expects( $this->once() )
1197 ->method( 'delete' )
1202 'wl_namespace' => 0,
1203 'wl_title' => 'SomeDbKey',
1206 $mockDb->expects( $this->once() )
1207 ->method( 'affectedRows' )
1208 ->will( $this->returnValue( 0 ) );
1210 $mockCache = $this->getMockCache();
1211 $mockCache->expects( $this->never() )->method( 'get' );
1212 $mockCache->expects( $this->once() )
1213 ->method( 'delete' )
1214 ->with( '0:SomeDbKey:1' );
1216 $store = new WatchedItemStore(
1217 $this->getMockLoadBalancer( $mockDb ),
1222 $store->removeWatch(
1223 $this->getMockNonAnonUserWithId( 1 ),
1224 new TitleValue( 0, 'SomeDbKey' )
1229 public function testRemoveWatch_anonymousUser() {
1230 $mockDb = $this->getMockDb();
1231 $mockDb->expects( $this->never() )
1232 ->method( 'delete' );
1234 $mockCache = $this->getMockCache();
1235 $mockCache->expects( $this->never() )->method( 'get' );
1236 $mockCache->expects( $this->never() )
1237 ->method( 'delete' );
1239 $store = new WatchedItemStore(
1240 $this->getMockLoadBalancer( $mockDb ),
1245 $store->removeWatch(
1246 $this->getAnonUser(),
1247 new TitleValue( 0, 'SomeDbKey' )
1252 public function testGetWatchedItem_existingItem() {
1253 $mockDb = $this->getMockDb();
1254 $mockDb->expects( $this->once() )
1255 ->method( 'selectRow' )
1258 'wl_notificationtimestamp',
1261 'wl_namespace' => 0,
1262 'wl_title' => 'SomeDbKey',
1265 ->will( $this->returnValue(
1266 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1269 $mockCache = $this->getMockCache();
1270 $mockCache->expects( $this->never() )->method( 'delete' );
1271 $mockCache->expects( $this->once() )
1276 ->will( $this->returnValue( null ) );
1277 $mockCache->expects( $this->once() )
1283 $store = new WatchedItemStore(
1284 $this->getMockLoadBalancer( $mockDb ),
1288 $watchedItem = $store->getWatchedItem(
1289 $this->getMockNonAnonUserWithId( 1 ),
1290 new TitleValue( 0, 'SomeDbKey' )
1292 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1293 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1294 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1295 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1298 public function testGetWatchedItem_cachedItem() {
1299 $mockDb = $this->getMockDb();
1300 $mockDb->expects( $this->never() )
1301 ->method( 'selectRow' );
1303 $mockUser = $this->getMockNonAnonUserWithId( 1 );
1304 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1305 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1307 $mockCache = $this->getMockCache();
1308 $mockCache->expects( $this->never() )->method( 'delete' );
1309 $mockCache->expects( $this->never() )->method( 'set' );
1310 $mockCache->expects( $this->once() )
1315 ->will( $this->returnValue( $cachedItem ) );
1317 $store = new WatchedItemStore(
1318 $this->getMockLoadBalancer( $mockDb ),
1322 $this->assertEquals(
1324 $store->getWatchedItem(
1331 public function testGetWatchedItem_noItem() {
1332 $mockDb = $this->getMockDb();
1333 $mockDb->expects( $this->once() )
1334 ->method( 'selectRow' )
1337 'wl_notificationtimestamp',
1340 'wl_namespace' => 0,
1341 'wl_title' => 'SomeDbKey',
1344 ->will( $this->returnValue( [] ) );
1346 $mockCache = $this->getMockCache();
1347 $mockCache->expects( $this->never() )->method( 'set' );
1348 $mockCache->expects( $this->never() )->method( 'delete' );
1349 $mockCache->expects( $this->once() )
1351 ->with( '0:SomeDbKey:1' )
1352 ->will( $this->returnValue( false ) );
1354 $store = new WatchedItemStore(
1355 $this->getMockLoadBalancer( $mockDb ),
1360 $store->getWatchedItem(
1361 $this->getMockNonAnonUserWithId( 1 ),
1362 new TitleValue( 0, 'SomeDbKey' )
1367 public function testGetWatchedItem_anonymousUser() {
1368 $mockDb = $this->getMockDb();
1369 $mockDb->expects( $this->never() )
1370 ->method( 'selectRow' );
1372 $mockCache = $this->getMockCache();
1373 $mockCache->expects( $this->never() )->method( 'set' );
1374 $mockCache->expects( $this->never() )->method( 'get' );
1375 $mockCache->expects( $this->never() )->method( 'delete' );
1377 $store = new WatchedItemStore(
1378 $this->getMockLoadBalancer( $mockDb ),
1383 $store->getWatchedItem(
1384 $this->getAnonUser(),
1385 new TitleValue( 0, 'SomeDbKey' )
1390 public function testGetWatchedItemsForUser() {
1391 $mockDb = $this->getMockDb();
1392 $mockDb->expects( $this->once() )
1393 ->method( 'select' )
1396 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1399 ->will( $this->returnValue( [
1400 $this->getFakeRow( [
1401 'wl_namespace' => 0,
1402 'wl_title' => 'Foo1',
1403 'wl_notificationtimestamp' => '20151212010101',
1405 $this->getFakeRow( [
1406 'wl_namespace' => 1,
1407 'wl_title' => 'Foo2',
1408 'wl_notificationtimestamp' => null,
1412 $mockCache = $this->getMockCache();
1413 $mockCache->expects( $this->never() )->method( 'delete' );
1414 $mockCache->expects( $this->never() )->method( 'get' );
1415 $mockCache->expects( $this->never() )->method( 'set' );
1417 $store = new WatchedItemStore(
1418 $this->getMockLoadBalancer( $mockDb ),
1421 $user = $this->getMockNonAnonUserWithId( 1 );
1423 $watchedItems = $store->getWatchedItemsForUser( $user );
1425 $this->assertInternalType( 'array', $watchedItems );
1426 $this->assertCount( 2, $watchedItems );
1427 foreach ( $watchedItems as $watchedItem ) {
1428 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1430 $this->assertEquals(
1431 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1434 $this->assertEquals(
1435 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1440 public function provideDbTypes() {
1442 [ false, DB_SLAVE
],
1443 [ true, DB_MASTER
],
1448 * @dataProvider provideDbTypes
1450 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1451 $mockDb = $this->getMockDb();
1452 $mockCache = $this->getMockCache();
1453 $mockLoadBalancer = $this->getMockLoadBalancer( $mockDb, $dbType );
1454 $user = $this->getMockNonAnonUserWithId( 1 );
1456 $mockDb->expects( $this->once() )
1457 ->method( 'select' )
1460 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1463 ->will( $this->returnValue( [] ) );
1465 $store = new WatchedItemStore(
1470 $watchedItems = $store->getWatchedItemsForUser(
1472 [ 'forWrite' => $forWrite ]
1474 $this->assertEquals( [], $watchedItems );
1477 public function testIsWatchedItem_existingItem() {
1478 $mockDb = $this->getMockDb();
1479 $mockDb->expects( $this->once() )
1480 ->method( 'selectRow' )
1483 'wl_notificationtimestamp',
1486 'wl_namespace' => 0,
1487 'wl_title' => 'SomeDbKey',
1490 ->will( $this->returnValue(
1491 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1494 $mockCache = $this->getMockCache();
1495 $mockCache->expects( $this->never() )->method( 'delete' );
1496 $mockCache->expects( $this->once() )
1498 ->with( '0:SomeDbKey:1' )
1499 ->will( $this->returnValue( false ) );
1500 $mockCache->expects( $this->once() )
1506 $store = new WatchedItemStore(
1507 $this->getMockLoadBalancer( $mockDb ),
1513 $this->getMockNonAnonUserWithId( 1 ),
1514 new TitleValue( 0, 'SomeDbKey' )
1519 public function testIsWatchedItem_noItem() {
1520 $mockDb = $this->getMockDb();
1521 $mockDb->expects( $this->once() )
1522 ->method( 'selectRow' )
1525 'wl_notificationtimestamp',
1528 'wl_namespace' => 0,
1529 'wl_title' => 'SomeDbKey',
1532 ->will( $this->returnValue( [] ) );
1534 $mockCache = $this->getMockCache();
1535 $mockCache->expects( $this->never() )->method( 'set' );
1536 $mockCache->expects( $this->never() )->method( 'delete' );
1537 $mockCache->expects( $this->once() )
1539 ->with( '0:SomeDbKey:1' )
1540 ->will( $this->returnValue( false ) );
1542 $store = new WatchedItemStore(
1543 $this->getMockLoadBalancer( $mockDb ),
1549 $this->getMockNonAnonUserWithId( 1 ),
1550 new TitleValue( 0, 'SomeDbKey' )
1555 public function testIsWatchedItem_anonymousUser() {
1556 $mockDb = $this->getMockDb();
1557 $mockDb->expects( $this->never() )
1558 ->method( 'selectRow' );
1560 $mockCache = $this->getMockCache();
1561 $mockCache->expects( $this->never() )->method( 'set' );
1562 $mockCache->expects( $this->never() )->method( 'get' );
1563 $mockCache->expects( $this->never() )->method( 'delete' );
1565 $store = new WatchedItemStore(
1566 $this->getMockLoadBalancer( $mockDb ),
1572 $this->getAnonUser(),
1573 new TitleValue( 0, 'SomeDbKey' )
1578 public function testGetNotificationTimestampsBatch() {
1580 new TitleValue( 0, 'SomeDbKey' ),
1581 new TitleValue( 1, 'AnotherDbKey' ),
1584 $mockDb = $this->getMockDb();
1586 $this->getFakeRow( [
1587 'wl_namespace' => 0,
1588 'wl_title' => 'SomeDbKey',
1589 'wl_notificationtimestamp' => '20151212010101',
1593 'wl_namespace' => 1,
1594 'wl_title' => 'AnotherDbKey',
1595 'wl_notificationtimestamp' => null,
1600 $mockDb->expects( $this->once() )
1601 ->method( 'makeWhereFrom2d' )
1603 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1604 $this->isType( 'string' ),
1605 $this->isType( 'string' )
1607 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1608 $mockDb->expects( $this->once() )
1609 ->method( 'select' )
1612 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1614 'makeWhereFrom2d return value',
1617 $this->isType( 'string' )
1619 ->will( $this->returnValue( $dbResult ) );
1621 $mockCache = $this->getMockCache();
1622 $mockCache->expects( $this->exactly( 2 ) )
1625 [ '0:SomeDbKey:1' ],
1626 [ '1:AnotherDbKey:1' ]
1628 ->will( $this->returnValue( null ) );
1629 $mockCache->expects( $this->never() )->method( 'set' );
1630 $mockCache->expects( $this->never() )->method( 'delete' );
1632 $store = new WatchedItemStore(
1633 $this->getMockLoadBalancer( $mockDb ),
1637 $this->assertEquals(
1639 0 => [ 'SomeDbKey' => '20151212010101', ],
1640 1 => [ 'AnotherDbKey' => null, ],
1642 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1646 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1648 new TitleValue( 0, 'OtherDbKey' ),
1651 $mockDb = $this->getMockDb();
1653 $mockDb->expects( $this->once() )
1654 ->method( 'makeWhereFrom2d' )
1656 [ [ 'OtherDbKey' => 1 ] ],
1657 $this->isType( 'string' ),
1658 $this->isType( 'string' )
1660 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1661 $mockDb->expects( $this->once() )
1662 ->method( 'select' )
1665 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1667 'makeWhereFrom2d return value',
1670 $this->isType( 'string' )
1672 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1674 $mockCache = $this->getMockCache();
1675 $mockCache->expects( $this->once() )
1677 ->with( '0:OtherDbKey:1' )
1678 ->will( $this->returnValue( null ) );
1679 $mockCache->expects( $this->never() )->method( 'set' );
1680 $mockCache->expects( $this->never() )->method( 'delete' );
1682 $store = new WatchedItemStore(
1683 $this->getMockLoadBalancer( $mockDb ),
1687 $this->assertEquals(
1689 0 => [ 'OtherDbKey' => false, ],
1691 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1695 public function testGetNotificationTimestampsBatch_cachedItem() {
1697 new TitleValue( 0, 'SomeDbKey' ),
1698 new TitleValue( 1, 'AnotherDbKey' ),
1701 $user = $this->getMockNonAnonUserWithId( 1 );
1702 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1704 $mockDb = $this->getMockDb();
1706 $mockDb->expects( $this->once() )
1707 ->method( 'makeWhereFrom2d' )
1709 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1710 $this->isType( 'string' ),
1711 $this->isType( 'string' )
1713 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1714 $mockDb->expects( $this->once() )
1715 ->method( 'select' )
1718 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1720 'makeWhereFrom2d return value',
1723 $this->isType( 'string' )
1725 ->will( $this->returnValue( [
1727 [ 'wl_namespace' => 1, 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1731 $mockCache = $this->getMockCache();
1732 $mockCache->expects( $this->at( 1 ) )
1734 ->with( '0:SomeDbKey:1' )
1735 ->will( $this->returnValue( $cachedItem ) );
1736 $mockCache->expects( $this->at( 3 ) )
1738 ->with( '1:AnotherDbKey:1' )
1739 ->will( $this->returnValue( null ) );
1740 $mockCache->expects( $this->never() )->method( 'set' );
1741 $mockCache->expects( $this->never() )->method( 'delete' );
1743 $store = new WatchedItemStore(
1744 $this->getMockLoadBalancer( $mockDb ),
1748 $this->assertEquals(
1750 0 => [ 'SomeDbKey' => '20151212010101', ],
1751 1 => [ 'AnotherDbKey' => null, ],
1753 $store->getNotificationTimestampsBatch( $user, $targets )
1757 public function testGetNotificationTimestampsBatch_allItemsCached() {
1759 new TitleValue( 0, 'SomeDbKey' ),
1760 new TitleValue( 1, 'AnotherDbKey' ),
1763 $user = $this->getMockNonAnonUserWithId( 1 );
1765 new WatchedItem( $user, $targets[0], '20151212010101' ),
1766 new WatchedItem( $user, $targets[1], null ),
1768 $mockDb = $this->getMockDb();
1769 $mockDb->expects( $this->never() )->method( $this->anything() );
1771 $mockCache = $this->getMockCache();
1772 $mockCache->expects( $this->at( 1 ) )
1774 ->with( '0:SomeDbKey:1' )
1775 ->will( $this->returnValue( $cachedItems[0] ) );
1776 $mockCache->expects( $this->at( 3 ) )
1778 ->with( '1:AnotherDbKey:1' )
1779 ->will( $this->returnValue( $cachedItems[1] ) );
1780 $mockCache->expects( $this->never() )->method( 'set' );
1781 $mockCache->expects( $this->never() )->method( 'delete' );
1783 $store = new WatchedItemStore(
1784 $this->getMockLoadBalancer( $mockDb ),
1788 $this->assertEquals(
1790 0 => [ 'SomeDbKey' => '20151212010101', ],
1791 1 => [ 'AnotherDbKey' => null, ],
1793 $store->getNotificationTimestampsBatch( $user, $targets )
1797 public function testGetNotificationTimestampsBatch_anonymousUser() {
1799 new TitleValue( 0, 'SomeDbKey' ),
1800 new TitleValue( 1, 'AnotherDbKey' ),
1803 $mockDb = $this->getMockDb();
1804 $mockDb->expects( $this->never() )->method( $this->anything() );
1806 $mockCache = $this->getMockCache();
1807 $mockCache->expects( $this->never() )->method( $this->anything() );
1809 $store = new WatchedItemStore(
1810 $this->getMockLoadBalancer( $mockDb ),
1814 $this->assertEquals(
1816 0 => [ 'SomeDbKey' => false, ],
1817 1 => [ 'AnotherDbKey' => false, ],
1819 $store->getNotificationTimestampsBatch( $this->getAnonUser(), $targets )
1823 public function testResetNotificationTimestamp_anonymousUser() {
1824 $mockDb = $this->getMockDb();
1825 $mockDb->expects( $this->never() )
1826 ->method( 'selectRow' );
1828 $mockCache = $this->getMockCache();
1829 $mockCache->expects( $this->never() )->method( 'get' );
1830 $mockCache->expects( $this->never() )->method( 'set' );
1831 $mockCache->expects( $this->never() )->method( 'delete' );
1833 $store = new WatchedItemStore(
1834 $this->getMockLoadBalancer( $mockDb ),
1839 $store->resetNotificationTimestamp(
1840 $this->getAnonUser(),
1841 Title
::newFromText( 'SomeDbKey' )
1846 public function testResetNotificationTimestamp_noItem() {
1847 $mockDb = $this->getMockDb();
1848 $mockDb->expects( $this->once() )
1849 ->method( 'selectRow' )
1852 'wl_notificationtimestamp',
1855 'wl_namespace' => 0,
1856 'wl_title' => 'SomeDbKey',
1859 ->will( $this->returnValue( [] ) );
1861 $mockCache = $this->getMockCache();
1862 $mockCache->expects( $this->never() )->method( 'get' );
1863 $mockCache->expects( $this->never() )->method( 'set' );
1864 $mockCache->expects( $this->never() )->method( 'delete' );
1866 $store = new WatchedItemStore(
1867 $this->getMockLoadBalancer( $mockDb ),
1872 $store->resetNotificationTimestamp(
1873 $this->getMockNonAnonUserWithId( 1 ),
1874 Title
::newFromText( 'SomeDbKey' )
1879 public function testResetNotificationTimestamp_item() {
1880 $user = $this->getMockNonAnonUserWithId( 1 );
1881 $title = Title
::newFromText( 'SomeDbKey' );
1883 $mockDb = $this->getMockDb();
1884 $mockDb->expects( $this->once() )
1885 ->method( 'selectRow' )
1888 'wl_notificationtimestamp',
1891 'wl_namespace' => 0,
1892 'wl_title' => 'SomeDbKey',
1895 ->will( $this->returnValue(
1896 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1899 $mockCache = $this->getMockCache();
1900 $mockCache->expects( $this->never() )->method( 'get' );
1901 $mockCache->expects( $this->once() )
1905 $this->isInstanceOf( WatchedItem
::class )
1907 $mockCache->expects( $this->once() )
1908 ->method( 'delete' )
1909 ->with( '0:SomeDbKey:1' );
1911 $store = new WatchedItemStore(
1912 $this->getMockLoadBalancer( $mockDb ),
1916 // Note: This does not actually assert the job is correct
1917 $callableCallCounter = 0;
1918 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1919 $callableCallCounter++
;
1920 $this->assertInternalType( 'callable', $callable );
1922 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1925 $store->resetNotificationTimestamp(
1930 $this->assertEquals( 1, $callableCallCounter );
1932 ScopedCallback
::consume( $scopedOverride );
1935 public function testResetNotificationTimestamp_noItemForced() {
1936 $user = $this->getMockNonAnonUserWithId( 1 );
1937 $title = Title
::newFromText( 'SomeDbKey' );
1939 $mockDb = $this->getMockDb();
1940 $mockDb->expects( $this->never() )
1941 ->method( 'selectRow' );
1943 $mockCache = $this->getMockCache();
1944 $mockDb->expects( $this->never() )
1946 $mockDb->expects( $this->never() )
1948 $mockDb->expects( $this->never() )
1949 ->method( 'delete' );
1951 $store = new WatchedItemStore(
1952 $this->getMockLoadBalancer( $mockDb ),
1956 // Note: This does not actually assert the job is correct
1957 $callableCallCounter = 0;
1958 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1959 $callableCallCounter++
;
1960 $this->assertInternalType( 'callable', $callable );
1962 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1965 $store->resetNotificationTimestamp(
1971 $this->assertEquals( 1, $callableCallCounter );
1973 ScopedCallback
::consume( $scopedOverride );
1980 * @return PHPUnit_Framework_MockObject_MockObject|Title
1982 private function getMockTitle( $text, $ns = 0 ) {
1983 $title = $this->getMock( Title
::class );
1984 $title->expects( $this->any() )
1985 ->method( 'getText' )
1986 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
1987 $title->expects( $this->any() )
1988 ->method( 'getDbKey' )
1989 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
1990 $title->expects( $this->any() )
1991 ->method( 'getNamespace' )
1992 ->will( $this->returnValue( $ns ) );
1996 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
1997 $user = $this->getMockNonAnonUserWithId( 1 );
1999 $title = $this->getMockTitle( 'SomeTitle' );
2000 $title->expects( $this->once() )
2001 ->method( 'getNextRevisionID' )
2003 ->will( $this->returnValue( false ) );
2005 $mockDb = $this->getMockDb();
2006 $mockDb->expects( $this->never() )
2007 ->method( 'selectRow' );
2009 $mockCache = $this->getMockCache();
2010 $mockDb->expects( $this->never() )
2012 $mockDb->expects( $this->never() )
2014 $mockDb->expects( $this->never() )
2015 ->method( 'delete' );
2017 $store = new WatchedItemStore(
2018 $this->getMockLoadBalancer( $mockDb ),
2022 // Note: This does not actually assert the job is correct
2023 $callableCallCounter = 0;
2024 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
2025 function( $callable ) use ( &$callableCallCounter ) {
2026 $callableCallCounter++
;
2027 $this->assertInternalType( 'callable', $callable );
2032 $store->resetNotificationTimestamp(
2039 $this->assertEquals( 1, $callableCallCounter );
2041 ScopedCallback
::consume( $scopedOverride );
2044 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced() {
2045 $user = $this->getMockNonAnonUserWithId( 1 );
2047 $title = $this->getMockTitle( 'SomeDbKey' );
2048 $title->expects( $this->once() )
2049 ->method( 'getNextRevisionID' )
2051 ->will( $this->returnValue( 33 ) );
2053 $mockDb = $this->getMockDb();
2054 $mockDb->expects( $this->once() )
2055 ->method( 'selectRow' )
2058 'wl_notificationtimestamp',
2061 'wl_namespace' => 0,
2062 'wl_title' => 'SomeDbKey',
2065 ->will( $this->returnValue(
2066 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2069 $mockCache = $this->getMockCache();
2070 $mockDb->expects( $this->never() )
2072 $mockDb->expects( $this->never() )
2074 $mockDb->expects( $this->never() )
2075 ->method( 'delete' );
2077 $store = new WatchedItemStore(
2078 $this->getMockLoadBalancer( $mockDb ),
2082 // Note: This does not actually assert the job is correct
2083 $addUpdateCallCounter = 0;
2084 $scopedOverrideDeferred = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
2085 function( $callable ) use ( &$addUpdateCallCounter ) {
2086 $addUpdateCallCounter++
;
2087 $this->assertInternalType( 'callable', $callable );
2091 $getTimestampCallCounter = 0;
2092 $scopedOverrideRevision = $store->overrideRevisionGetTimestampFromIdCallback(
2093 function( $titleParam, $oldidParam ) use ( &$getTimestampCallCounter, $title, $oldid ) {
2094 $getTimestampCallCounter++
;
2095 $this->assertEquals( $title, $titleParam );
2096 $this->assertEquals( $oldid, $oldidParam );
2101 $store->resetNotificationTimestamp(
2108 $this->assertEquals( 1, $addUpdateCallCounter );
2109 $this->assertEquals( 1, $getTimestampCallCounter );
2111 ScopedCallback
::consume( $scopedOverrideDeferred );
2112 ScopedCallback
::consume( $scopedOverrideRevision );
2115 public function testUpdateNotificationTimestamp_watchersExist() {
2116 $mockDb = $this->getMockDb();
2117 $mockDb->expects( $this->once() )
2118 ->method( 'select' )
2124 'wl_namespace' => 0,
2125 'wl_title' => 'SomeDbKey',
2126 'wl_notificationtimestamp IS NULL'
2130 $this->returnValue( [
2131 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2132 $this->getFakeRow( [ 'wl_user' => '3' ] )
2135 $mockDb->expects( $this->once() )
2136 ->method( 'onTransactionIdle' )
2137 ->with( $this->isType( 'callable' ) )
2138 ->will( $this->returnCallback( function( $callable ) {
2141 $mockDb->expects( $this->once() )
2142 ->method( 'update' )
2145 [ 'wl_notificationtimestamp' => null ],
2147 'wl_user' => [ 2, 3 ],
2148 'wl_namespace' => 0,
2149 'wl_title' => 'SomeDbKey',
2153 $mockCache = $this->getMockCache();
2154 $mockCache->expects( $this->never() )->method( 'set' );
2155 $mockCache->expects( $this->never() )->method( 'get' );
2156 $mockCache->expects( $this->never() )->method( 'delete' );
2158 $store = new WatchedItemStore(
2159 $this->getMockLoadBalancer( $mockDb ),
2163 $this->assertEquals(
2165 $store->updateNotificationTimestamp(
2166 $this->getMockNonAnonUserWithId( 1 ),
2167 new TitleValue( 0, 'SomeDbKey' ),
2173 public function testUpdateNotificationTimestamp_noWatchers() {
2174 $mockDb = $this->getMockDb();
2175 $mockDb->expects( $this->once() )
2176 ->method( 'select' )
2182 'wl_namespace' => 0,
2183 'wl_title' => 'SomeDbKey',
2184 'wl_notificationtimestamp IS NULL'
2188 $this->returnValue( [] )
2190 $mockDb->expects( $this->never() )
2191 ->method( 'onTransactionIdle' );
2192 $mockDb->expects( $this->never() )
2193 ->method( 'update' );
2195 $mockCache = $this->getMockCache();
2196 $mockCache->expects( $this->never() )->method( 'set' );
2197 $mockCache->expects( $this->never() )->method( 'get' );
2198 $mockCache->expects( $this->never() )->method( 'delete' );
2200 $store = new WatchedItemStore(
2201 $this->getMockLoadBalancer( $mockDb ),
2205 $watchers = $store->updateNotificationTimestamp(
2206 $this->getMockNonAnonUserWithId( 1 ),
2207 new TitleValue( 0, 'SomeDbKey' ),
2210 $this->assertInternalType( 'array', $watchers );
2211 $this->assertEmpty( $watchers );
2214 public function testUpdateNotificationTimestamp_clearsCachedItems() {
2215 $user = $this->getMockNonAnonUserWithId( 1 );
2216 $titleValue = new TitleValue( 0, 'SomeDbKey' );
2218 $mockDb = $this->getMockDb();
2219 $mockDb->expects( $this->once() )
2220 ->method( 'selectRow' )
2221 ->will( $this->returnValue(
2222 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2224 $mockDb->expects( $this->once() )
2225 ->method( 'select' )
2227 $this->returnValue( [
2228 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2229 $this->getFakeRow( [ 'wl_user' => '3' ] )
2232 $mockDb->expects( $this->once() )
2233 ->method( 'onTransactionIdle' )
2234 ->with( $this->isType( 'callable' ) )
2235 ->will( $this->returnCallback( function( $callable ) {
2238 $mockDb->expects( $this->once() )
2239 ->method( 'update' );
2241 $mockCache = $this->getMockCache();
2242 $mockCache->expects( $this->once() )
2244 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2245 $mockCache->expects( $this->once() )
2247 ->with( '0:SomeDbKey:1' );
2248 $mockCache->expects( $this->once() )
2249 ->method( 'delete' )
2250 ->with( '0:SomeDbKey:1' );
2252 $store = new WatchedItemStore(
2253 $this->getMockLoadBalancer( $mockDb ),
2257 // This will add the item to the cache
2258 $store->getWatchedItem( $user, $titleValue );
2260 $store->updateNotificationTimestamp(
2261 $this->getMockNonAnonUserWithId( 1 ),