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(
22 $expectedConnectionType = null,
23 $readOnlyReason = false
25 $mock = $this->getMockBuilder( LoadBalancer
::class )
26 ->disableOriginalConstructor()
28 if ( $expectedConnectionType !== null ) {
29 $mock->expects( $this->any() )
30 ->method( 'getConnection' )
31 ->with( $expectedConnectionType )
32 ->will( $this->returnValue( $mockDb ) );
34 $mock->expects( $this->any() )
35 ->method( 'getConnection' )
36 ->will( $this->returnValue( $mockDb ) );
38 $mock->expects( $this->any() )
39 ->method( 'getReadOnlyReason' )
40 ->will( $this->returnValue( $readOnlyReason ) );
45 * @return PHPUnit_Framework_MockObject_MockObject|HashBagOStuff
47 private function getMockCache() {
48 $mock = $this->getMockBuilder( HashBagOStuff
::class )
49 ->disableOriginalConstructor()
51 $mock->expects( $this->any() )
53 ->will( $this->returnCallback( function() {
54 return implode( ':', func_get_args() );
61 * @return PHPUnit_Framework_MockObject_MockObject|User
63 private function getMockNonAnonUserWithId( $id ) {
64 $mock = $this->getMock( User
::class );
65 $mock->expects( $this->any() )
67 ->will( $this->returnValue( false ) );
68 $mock->expects( $this->any() )
70 ->will( $this->returnValue( $id ) );
77 private function getAnonUser() {
78 return User
::newFromName( 'Anon_User' );
81 private function getFakeRow( array $rowValues ) {
82 $fakeRow = new stdClass();
83 foreach ( $rowValues as $valueName => $value ) {
84 $fakeRow->$valueName = $value;
89 private function newWatchedItemStore( LoadBalancer
$loadBalancer, HashBagOStuff
$cache ) {
90 return new WatchedItemStore(
96 public function testGetDefaultInstance() {
97 $instanceOne = WatchedItemStore
::getDefaultInstance();
98 $instanceTwo = WatchedItemStore
::getDefaultInstance();
99 $this->assertSame( $instanceOne, $instanceTwo );
102 public function testOverrideDefaultInstance() {
103 $instance = WatchedItemStore
::getDefaultInstance();
104 $scopedOverride = $instance->overrideDefaultInstance( null );
106 $this->assertNotSame( $instance, WatchedItemStore
::getDefaultInstance() );
108 unset( $scopedOverride );
110 $this->assertSame( $instance, WatchedItemStore
::getDefaultInstance() );
113 public function testCountWatchedItems() {
114 $user = $this->getMockNonAnonUserWithId( 1 );
116 $mockDb = $this->getMockDb();
117 $mockDb->expects( $this->exactly( 1 ) )
118 ->method( 'selectField' )
123 'wl_user' => $user->getId(),
125 $this->isType( 'string' )
127 ->will( $this->returnValue( 12 ) );
129 $mockCache = $this->getMockCache();
130 $mockCache->expects( $this->never() )->method( 'get' );
131 $mockCache->expects( $this->never() )->method( 'set' );
132 $mockCache->expects( $this->never() )->method( 'delete' );
134 $store = $this->newWatchedItemStore(
135 $this->getMockLoadBalancer( $mockDb ),
139 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
142 public function testCountWatchers() {
143 $titleValue = new TitleValue( 0, 'SomeDbKey' );
145 $mockDb = $this->getMockDb();
146 $mockDb->expects( $this->exactly( 1 ) )
147 ->method( 'selectField' )
152 'wl_namespace' => $titleValue->getNamespace(),
153 'wl_title' => $titleValue->getDBkey(),
155 $this->isType( 'string' )
157 ->will( $this->returnValue( 7 ) );
159 $mockCache = $this->getMockCache();
160 $mockCache->expects( $this->never() )->method( 'get' );
161 $mockCache->expects( $this->never() )->method( 'set' );
162 $mockCache->expects( $this->never() )->method( 'delete' );
164 $store = $this->newWatchedItemStore(
165 $this->getMockLoadBalancer( $mockDb ),
169 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
172 public function testCountWatchersMultiple() {
174 new TitleValue( 0, 'SomeDbKey' ),
175 new TitleValue( 0, 'OtherDbKey' ),
176 new TitleValue( 1, 'AnotherDbKey' ),
179 $mockDb = $this->getMockDb();
182 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
183 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
184 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
187 $mockDb->expects( $this->once() )
188 ->method( 'makeWhereFrom2d' )
190 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
191 $this->isType( 'string' ),
192 $this->isType( 'string' )
194 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
195 $mockDb->expects( $this->once() )
199 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
200 [ 'makeWhereFrom2d return value' ],
201 $this->isType( 'string' ),
203 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
207 $this->returnValue( $dbResult )
210 $mockCache = $this->getMockCache();
211 $mockCache->expects( $this->never() )->method( 'get' );
212 $mockCache->expects( $this->never() )->method( 'set' );
213 $mockCache->expects( $this->never() )->method( 'delete' );
215 $store = $this->newWatchedItemStore(
216 $this->getMockLoadBalancer( $mockDb ),
221 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
222 1 => [ 'AnotherDbKey' => 500 ],
224 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
227 public function provideIntWithDbUnsafeVersion() {
230 [ "50; DROP TABLE watchlist;\n--" ],
235 * @dataProvider provideIntWithDbUnsafeVersion
237 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
239 new TitleValue( 0, 'SomeDbKey' ),
240 new TitleValue( 0, 'OtherDbKey' ),
241 new TitleValue( 1, 'AnotherDbKey' ),
244 $mockDb = $this->getMockDb();
247 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
248 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
249 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
252 $mockDb->expects( $this->once() )
253 ->method( 'makeWhereFrom2d' )
255 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
256 $this->isType( 'string' ),
257 $this->isType( 'string' )
259 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
260 $mockDb->expects( $this->once() )
264 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
265 [ 'makeWhereFrom2d return value' ],
266 $this->isType( 'string' ),
268 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
269 'HAVING' => 'COUNT(*) >= 50',
273 $this->returnValue( $dbResult )
276 $mockCache = $this->getMockCache();
277 $mockCache->expects( $this->never() )->method( 'get' );
278 $mockCache->expects( $this->never() )->method( 'set' );
279 $mockCache->expects( $this->never() )->method( 'delete' );
281 $store = $this->newWatchedItemStore(
282 $this->getMockLoadBalancer( $mockDb ),
287 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
288 1 => [ 'AnotherDbKey' => 500 ],
292 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
296 public function testCountVisitingWatchers() {
297 $titleValue = new TitleValue( 0, 'SomeDbKey' );
299 $mockDb = $this->getMockDb();
300 $mockDb->expects( $this->exactly( 1 ) )
301 ->method( 'selectField' )
306 'wl_namespace' => $titleValue->getNamespace(),
307 'wl_title' => $titleValue->getDBkey(),
308 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
310 $this->isType( 'string' )
312 ->will( $this->returnValue( 7 ) );
313 $mockDb->expects( $this->exactly( 1 ) )
314 ->method( 'addQuotes' )
315 ->will( $this->returnCallback( function( $value ) {
318 $mockDb->expects( $this->exactly( 1 ) )
319 ->method( 'timestamp' )
320 ->will( $this->returnCallback( function( $value ) {
321 return 'TS' . $value . 'TS';
324 $mockCache = $this->getMockCache();
325 $mockCache->expects( $this->never() )->method( 'set' );
326 $mockCache->expects( $this->never() )->method( 'get' );
327 $mockCache->expects( $this->never() )->method( 'delete' );
329 $store = $this->newWatchedItemStore(
330 $this->getMockLoadBalancer( $mockDb ),
334 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
337 public function testCountVisitingWatchersMultiple() {
338 $titleValuesWithThresholds = [
339 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
340 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
341 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
345 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
346 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
347 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
349 $mockDb = $this->getMockDb();
350 $mockDb->expects( $this->exactly( 2 * 3 ) )
351 ->method( 'addQuotes' )
352 ->will( $this->returnCallback( function( $value ) {
355 $mockDb->expects( $this->exactly( 3 ) )
356 ->method( 'timestamp' )
357 ->will( $this->returnCallback( function( $value ) {
358 return 'TS' . $value . 'TS';
360 $mockDb->expects( $this->any() )
361 ->method( 'makeList' )
363 $this->isType( 'array' ),
364 $this->isType( 'int' )
366 ->will( $this->returnCallback( function( $a, $conj ) {
367 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
368 return join( $sqlConj, array_map( function( $s ) {
369 return '(' . $s . ')';
373 $mockDb->expects( $this->never() )
374 ->method( 'makeWhereFrom2d' );
377 '((wl_namespace = 0) AND (' .
378 "(((wl_title = 'SomeDbKey') AND (" .
379 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
381 "(wl_title = 'OtherDbKey') AND (" .
382 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
384 ') OR ((wl_namespace = 1) AND (' .
385 "(((wl_title = 'AnotherDbKey') AND (".
386 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
388 $mockDb->expects( $this->once() )
392 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
394 $this->isType( 'string' ),
396 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
400 $this->returnValue( $dbResult )
403 $mockCache = $this->getMockCache();
404 $mockCache->expects( $this->never() )->method( 'get' );
405 $mockCache->expects( $this->never() )->method( 'set' );
406 $mockCache->expects( $this->never() )->method( 'delete' );
408 $store = $this->newWatchedItemStore(
409 $this->getMockLoadBalancer( $mockDb ),
414 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
415 1 => [ 'AnotherDbKey' => 500 ],
419 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
423 public function testCountVisitingWatchersMultiple_withMissingTargets() {
424 $titleValuesWithThresholds = [
425 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
426 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
427 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
428 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
429 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
433 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
434 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
435 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
437 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 100 ]
440 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 200 ]
443 $mockDb = $this->getMockDb();
444 $mockDb->expects( $this->exactly( 2 * 3 ) )
445 ->method( 'addQuotes' )
446 ->will( $this->returnCallback( function( $value ) {
449 $mockDb->expects( $this->exactly( 3 ) )
450 ->method( 'timestamp' )
451 ->will( $this->returnCallback( function( $value ) {
452 return 'TS' . $value . 'TS';
454 $mockDb->expects( $this->any() )
455 ->method( 'makeList' )
457 $this->isType( 'array' ),
458 $this->isType( 'int' )
460 ->will( $this->returnCallback( function( $a, $conj ) {
461 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
462 return join( $sqlConj, array_map( function( $s ) {
463 return '(' . $s . ')';
467 $mockDb->expects( $this->once() )
468 ->method( 'makeWhereFrom2d' )
470 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
471 $this->isType( 'string' ),
472 $this->isType( 'string' )
474 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
477 '((wl_namespace = 0) AND (' .
478 "(((wl_title = 'SomeDbKey') AND (" .
479 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
481 "(wl_title = 'OtherDbKey') AND (" .
482 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
484 ') OR ((wl_namespace = 1) AND (' .
485 "(((wl_title = 'AnotherDbKey') AND (".
486 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
489 '(makeWhereFrom2d return value)';
490 $mockDb->expects( $this->once() )
494 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
496 $this->isType( 'string' ),
498 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
502 $this->returnValue( $dbResult )
505 $mockCache = $this->getMockCache();
506 $mockCache->expects( $this->never() )->method( 'get' );
507 $mockCache->expects( $this->never() )->method( 'set' );
508 $mockCache->expects( $this->never() )->method( 'delete' );
510 $store = $this->newWatchedItemStore(
511 $this->getMockLoadBalancer( $mockDb ),
517 'SomeDbKey' => 100, 'OtherDbKey' => 300,
518 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
520 1 => [ 'AnotherDbKey' => 500 ],
524 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
529 * @dataProvider provideIntWithDbUnsafeVersion
531 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
532 $titleValuesWithThresholds = [
533 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
534 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
535 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
538 $mockDb = $this->getMockDb();
539 $mockDb->expects( $this->any() )
540 ->method( 'makeList' )
541 ->will( $this->returnValue( 'makeList return value' ) );
542 $mockDb->expects( $this->once() )
546 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
547 'makeList return value',
548 $this->isType( 'string' ),
550 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
551 'HAVING' => 'COUNT(*) >= 50',
555 $this->returnValue( [] )
558 $mockCache = $this->getMockCache();
559 $mockCache->expects( $this->never() )->method( 'get' );
560 $mockCache->expects( $this->never() )->method( 'set' );
561 $mockCache->expects( $this->never() )->method( 'delete' );
563 $store = $this->newWatchedItemStore(
564 $this->getMockLoadBalancer( $mockDb ),
569 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
570 1 => [ 'AnotherDbKey' => 0 ],
574 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
578 public function testCountUnreadNotifications() {
579 $user = $this->getMockNonAnonUserWithId( 1 );
581 $mockDb = $this->getMockDb();
582 $mockDb->expects( $this->exactly( 1 ) )
583 ->method( 'selectRowCount' )
588 "wl_notificationtimestamp IS NOT NULL",
591 $this->isType( 'string' )
593 ->will( $this->returnValue( 9 ) );
595 $mockCache = $this->getMockCache();
596 $mockCache->expects( $this->never() )->method( 'set' );
597 $mockCache->expects( $this->never() )->method( 'get' );
598 $mockCache->expects( $this->never() )->method( 'delete' );
600 $store = $this->newWatchedItemStore(
601 $this->getMockLoadBalancer( $mockDb ),
605 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
609 * @dataProvider provideIntWithDbUnsafeVersion
611 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
612 $user = $this->getMockNonAnonUserWithId( 1 );
614 $mockDb = $this->getMockDb();
615 $mockDb->expects( $this->exactly( 1 ) )
616 ->method( 'selectRowCount' )
621 "wl_notificationtimestamp IS NOT NULL",
624 $this->isType( 'string' ),
627 ->will( $this->returnValue( 50 ) );
629 $mockCache = $this->getMockCache();
630 $mockCache->expects( $this->never() )->method( 'set' );
631 $mockCache->expects( $this->never() )->method( 'get' );
632 $mockCache->expects( $this->never() )->method( 'delete' );
634 $store = $this->newWatchedItemStore(
635 $this->getMockLoadBalancer( $mockDb ),
641 $store->countUnreadNotifications( $user, $limit )
646 * @dataProvider provideIntWithDbUnsafeVersion
648 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
649 $user = $this->getMockNonAnonUserWithId( 1 );
651 $mockDb = $this->getMockDb();
652 $mockDb->expects( $this->exactly( 1 ) )
653 ->method( 'selectRowCount' )
658 "wl_notificationtimestamp IS NOT NULL",
661 $this->isType( 'string' ),
664 ->will( $this->returnValue( 9 ) );
666 $mockCache = $this->getMockCache();
667 $mockCache->expects( $this->never() )->method( 'set' );
668 $mockCache->expects( $this->never() )->method( 'get' );
669 $mockCache->expects( $this->never() )->method( 'delete' );
671 $store = $this->newWatchedItemStore(
672 $this->getMockLoadBalancer( $mockDb ),
678 $store->countUnreadNotifications( $user, $limit )
682 public function testDuplicateEntry_nothingToDuplicate() {
683 $mockDb = $this->getMockDb();
684 $mockDb->expects( $this->once() )
690 'wl_notificationtimestamp',
694 'wl_title' => 'Old_Title',
696 'WatchedItemStore::duplicateEntry',
699 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
701 $store = $this->newWatchedItemStore(
702 $this->getMockLoadBalancer( $mockDb ),
703 $this->getMockCache()
706 $store->duplicateEntry(
707 Title
::newFromText( 'Old_Title' ),
708 Title
::newFromText( 'New_Title' )
712 public function testDuplicateEntry_somethingToDuplicate() {
714 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
715 $this->getFakeRow( [ 'wl_user' => 2, 'wl_notificationtimestamp' => null ] ),
718 $mockDb = $this->getMockDb();
719 $mockDb->expects( $this->at( 0 ) )
725 'wl_notificationtimestamp',
729 'wl_title' => 'Old_Title',
732 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
733 $mockDb->expects( $this->at( 1 ) )
734 ->method( 'replace' )
737 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
742 'wl_title' => 'New_Title',
743 'wl_notificationtimestamp' => '20151212010101',
748 'wl_title' => 'New_Title',
749 'wl_notificationtimestamp' => null,
752 $this->isType( 'string' )
755 $mockCache = $this->getMockCache();
756 $mockCache->expects( $this->never() )->method( 'get' );
757 $mockCache->expects( $this->never() )->method( 'delete' );
759 $store = $this->newWatchedItemStore(
760 $this->getMockLoadBalancer( $mockDb ),
764 $store->duplicateEntry(
765 Title
::newFromText( 'Old_Title' ),
766 Title
::newFromText( 'New_Title' )
770 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
771 $mockDb = $this->getMockDb();
772 $mockDb->expects( $this->at( 0 ) )
778 'wl_notificationtimestamp',
782 'wl_title' => 'Old_Title',
785 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
786 $mockDb->expects( $this->at( 1 ) )
792 'wl_notificationtimestamp',
796 'wl_title' => 'Old_Title',
799 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
801 $mockCache = $this->getMockCache();
802 $mockCache->expects( $this->never() )->method( 'get' );
803 $mockCache->expects( $this->never() )->method( 'delete' );
805 $store = $this->newWatchedItemStore(
806 $this->getMockLoadBalancer( $mockDb ),
810 $store->duplicateAllAssociatedEntries(
811 Title
::newFromText( 'Old_Title' ),
812 Title
::newFromText( 'New_Title' )
816 public function testDuplicateAllAssociatedEntries_somethingToDuplicate() {
818 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
821 $mockDb = $this->getMockDb();
822 $mockDb->expects( $this->at( 0 ) )
828 'wl_notificationtimestamp',
832 'wl_title' => 'Old_Title',
835 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
836 $mockDb->expects( $this->at( 1 ) )
837 ->method( 'replace' )
840 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
845 'wl_title' => 'New_Title',
846 'wl_notificationtimestamp' => '20151212010101',
849 $this->isType( 'string' )
851 $mockDb->expects( $this->at( 2 ) )
857 'wl_notificationtimestamp',
861 'wl_title' => 'Old_Title',
864 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
865 $mockDb->expects( $this->at( 3 ) )
866 ->method( 'replace' )
869 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
874 'wl_title' => 'New_Title',
875 'wl_notificationtimestamp' => '20151212010101',
878 $this->isType( 'string' )
881 $mockCache = $this->getMockCache();
882 $mockCache->expects( $this->never() )->method( 'get' );
883 $mockCache->expects( $this->never() )->method( 'delete' );
885 $store = $this->newWatchedItemStore(
886 $this->getMockLoadBalancer( $mockDb ),
890 $store->duplicateAllAssociatedEntries(
891 Title
::newFromText( 'Old_Title' ),
892 Title
::newFromText( 'New_Title' )
896 public function testAddWatch_nonAnonymousUser() {
897 $mockDb = $this->getMockDb();
898 $mockDb->expects( $this->once() )
906 'wl_title' => 'Some_Page',
907 'wl_notificationtimestamp' => null,
912 $mockCache = $this->getMockCache();
913 $mockCache->expects( $this->once() )
915 ->with( '0:Some_Page:1' );
917 $store = $this->newWatchedItemStore(
918 $this->getMockLoadBalancer( $mockDb ),
923 $this->getMockNonAnonUserWithId( 1 ),
924 Title
::newFromText( 'Some_Page' )
928 public function testAddWatch_anonymousUser() {
929 $mockDb = $this->getMockDb();
930 $mockDb->expects( $this->never() )
931 ->method( 'insert' );
933 $mockCache = $this->getMockCache();
934 $mockCache->expects( $this->never() )
935 ->method( 'delete' );
937 $store = $this->newWatchedItemStore(
938 $this->getMockLoadBalancer( $mockDb ),
943 $this->getAnonUser(),
944 Title
::newFromText( 'Some_Page' )
948 public function testAddWatchBatchForUser_readOnlyDBReturnsFalse() {
949 $store = $this->newWatchedItemStore(
950 $this->getMockLoadBalancer( $this->getMockDb(), null, 'Some Reason' ),
951 $this->getMockCache()
955 $store->addWatchBatchForUser(
956 $this->getMockNonAnonUserWithId( 1 ),
957 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
962 public function testAddWatchBatchForUser_nonAnonymousUser() {
963 $mockDb = $this->getMockDb();
964 $mockDb->expects( $this->once() )
972 'wl_title' => 'Some_Page',
973 'wl_notificationtimestamp' => null,
978 'wl_title' => 'Some_Page',
979 'wl_notificationtimestamp' => null,
984 $mockCache = $this->getMockCache();
985 $mockCache->expects( $this->exactly( 2 ) )
986 ->method( 'delete' );
987 $mockCache->expects( $this->at( 1 ) )
989 ->with( '0:Some_Page:1' );
990 $mockCache->expects( $this->at( 3 ) )
992 ->with( '1:Some_Page:1' );
994 $store = $this->newWatchedItemStore(
995 $this->getMockLoadBalancer( $mockDb ),
999 $mockUser = $this->getMockNonAnonUserWithId( 1 );
1002 $store->addWatchBatchForUser(
1004 [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
1009 public function testAddWatchBatchForUser_anonymousUsersAreSkipped() {
1010 $mockDb = $this->getMockDb();
1011 $mockDb->expects( $this->never() )
1012 ->method( 'insert' );
1014 $mockCache = $this->getMockCache();
1015 $mockCache->expects( $this->never() )
1016 ->method( 'delete' );
1018 $store = $this->newWatchedItemStore(
1019 $this->getMockLoadBalancer( $mockDb ),
1024 $store->addWatchBatchForUser(
1025 $this->getAnonUser(),
1026 [ new TitleValue( 0, 'Other_Page' ) ]
1031 public function testAddWatchBatchReturnsTrue_whenGivenEmptyList() {
1032 $user = $this->getMockNonAnonUserWithId( 1 );
1033 $mockDb = $this->getMockDb();
1034 $mockDb->expects( $this->never() )
1035 ->method( 'insert' );
1037 $mockCache = $this->getMockCache();
1038 $mockCache->expects( $this->never() )
1039 ->method( 'delete' );
1041 $store = $this->newWatchedItemStore(
1042 $this->getMockLoadBalancer( $mockDb ),
1047 $store->addWatchBatchForUser( $user, [] )
1051 public function testLoadWatchedItem_existingItem() {
1052 $mockDb = $this->getMockDb();
1053 $mockDb->expects( $this->once() )
1054 ->method( 'selectRow' )
1057 'wl_notificationtimestamp',
1060 'wl_namespace' => 0,
1061 'wl_title' => 'SomeDbKey',
1064 ->will( $this->returnValue(
1065 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1068 $mockCache = $this->getMockCache();
1069 $mockCache->expects( $this->once() )
1075 $store = $this->newWatchedItemStore(
1076 $this->getMockLoadBalancer( $mockDb ),
1080 $watchedItem = $store->loadWatchedItem(
1081 $this->getMockNonAnonUserWithId( 1 ),
1082 new TitleValue( 0, 'SomeDbKey' )
1084 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1085 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1086 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1087 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1090 public function testLoadWatchedItem_noItem() {
1091 $mockDb = $this->getMockDb();
1092 $mockDb->expects( $this->once() )
1093 ->method( 'selectRow' )
1096 'wl_notificationtimestamp',
1099 'wl_namespace' => 0,
1100 'wl_title' => 'SomeDbKey',
1103 ->will( $this->returnValue( [] ) );
1105 $mockCache = $this->getMockCache();
1106 $mockCache->expects( $this->never() )->method( 'get' );
1107 $mockCache->expects( $this->never() )->method( 'delete' );
1109 $store = $this->newWatchedItemStore(
1110 $this->getMockLoadBalancer( $mockDb ),
1115 $store->loadWatchedItem(
1116 $this->getMockNonAnonUserWithId( 1 ),
1117 new TitleValue( 0, 'SomeDbKey' )
1122 public function testLoadWatchedItem_anonymousUser() {
1123 $mockDb = $this->getMockDb();
1124 $mockDb->expects( $this->never() )
1125 ->method( 'selectRow' );
1127 $mockCache = $this->getMockCache();
1128 $mockCache->expects( $this->never() )->method( 'get' );
1129 $mockCache->expects( $this->never() )->method( 'delete' );
1131 $store = $this->newWatchedItemStore(
1132 $this->getMockLoadBalancer( $mockDb ),
1137 $store->loadWatchedItem(
1138 $this->getAnonUser(),
1139 new TitleValue( 0, 'SomeDbKey' )
1144 public function testRemoveWatch_existingItem() {
1145 $mockDb = $this->getMockDb();
1146 $mockDb->expects( $this->once() )
1147 ->method( 'delete' )
1152 'wl_namespace' => 0,
1153 'wl_title' => 'SomeDbKey',
1156 $mockDb->expects( $this->once() )
1157 ->method( 'affectedRows' )
1158 ->will( $this->returnValue( 1 ) );
1160 $mockCache = $this->getMockCache();
1161 $mockCache->expects( $this->never() )->method( 'get' );
1162 $mockCache->expects( $this->once() )
1163 ->method( 'delete' )
1164 ->with( '0:SomeDbKey:1' );
1166 $store = $this->newWatchedItemStore(
1167 $this->getMockLoadBalancer( $mockDb ),
1172 $store->removeWatch(
1173 $this->getMockNonAnonUserWithId( 1 ),
1174 new TitleValue( 0, 'SomeDbKey' )
1179 public function testRemoveWatch_noItem() {
1180 $mockDb = $this->getMockDb();
1181 $mockDb->expects( $this->once() )
1182 ->method( 'delete' )
1187 'wl_namespace' => 0,
1188 'wl_title' => 'SomeDbKey',
1191 $mockDb->expects( $this->once() )
1192 ->method( 'affectedRows' )
1193 ->will( $this->returnValue( 0 ) );
1195 $mockCache = $this->getMockCache();
1196 $mockCache->expects( $this->never() )->method( 'get' );
1197 $mockCache->expects( $this->once() )
1198 ->method( 'delete' )
1199 ->with( '0:SomeDbKey:1' );
1201 $store = $this->newWatchedItemStore(
1202 $this->getMockLoadBalancer( $mockDb ),
1207 $store->removeWatch(
1208 $this->getMockNonAnonUserWithId( 1 ),
1209 new TitleValue( 0, 'SomeDbKey' )
1214 public function testRemoveWatch_anonymousUser() {
1215 $mockDb = $this->getMockDb();
1216 $mockDb->expects( $this->never() )
1217 ->method( 'delete' );
1219 $mockCache = $this->getMockCache();
1220 $mockCache->expects( $this->never() )->method( 'get' );
1221 $mockCache->expects( $this->never() )
1222 ->method( 'delete' );
1224 $store = $this->newWatchedItemStore(
1225 $this->getMockLoadBalancer( $mockDb ),
1230 $store->removeWatch(
1231 $this->getAnonUser(),
1232 new TitleValue( 0, 'SomeDbKey' )
1237 public function testGetWatchedItem_existingItem() {
1238 $mockDb = $this->getMockDb();
1239 $mockDb->expects( $this->once() )
1240 ->method( 'selectRow' )
1243 'wl_notificationtimestamp',
1246 'wl_namespace' => 0,
1247 'wl_title' => 'SomeDbKey',
1250 ->will( $this->returnValue(
1251 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1254 $mockCache = $this->getMockCache();
1255 $mockCache->expects( $this->never() )->method( 'delete' );
1256 $mockCache->expects( $this->once() )
1261 ->will( $this->returnValue( null ) );
1262 $mockCache->expects( $this->once() )
1268 $store = $this->newWatchedItemStore(
1269 $this->getMockLoadBalancer( $mockDb ),
1273 $watchedItem = $store->getWatchedItem(
1274 $this->getMockNonAnonUserWithId( 1 ),
1275 new TitleValue( 0, 'SomeDbKey' )
1277 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1278 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1279 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1280 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1283 public function testGetWatchedItem_cachedItem() {
1284 $mockDb = $this->getMockDb();
1285 $mockDb->expects( $this->never() )
1286 ->method( 'selectRow' );
1288 $mockUser = $this->getMockNonAnonUserWithId( 1 );
1289 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1290 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1292 $mockCache = $this->getMockCache();
1293 $mockCache->expects( $this->never() )->method( 'delete' );
1294 $mockCache->expects( $this->never() )->method( 'set' );
1295 $mockCache->expects( $this->once() )
1300 ->will( $this->returnValue( $cachedItem ) );
1302 $store = $this->newWatchedItemStore(
1303 $this->getMockLoadBalancer( $mockDb ),
1307 $this->assertEquals(
1309 $store->getWatchedItem(
1316 public function testGetWatchedItem_noItem() {
1317 $mockDb = $this->getMockDb();
1318 $mockDb->expects( $this->once() )
1319 ->method( 'selectRow' )
1322 'wl_notificationtimestamp',
1325 'wl_namespace' => 0,
1326 'wl_title' => 'SomeDbKey',
1329 ->will( $this->returnValue( [] ) );
1331 $mockCache = $this->getMockCache();
1332 $mockCache->expects( $this->never() )->method( 'set' );
1333 $mockCache->expects( $this->never() )->method( 'delete' );
1334 $mockCache->expects( $this->once() )
1336 ->with( '0:SomeDbKey:1' )
1337 ->will( $this->returnValue( false ) );
1339 $store = $this->newWatchedItemStore(
1340 $this->getMockLoadBalancer( $mockDb ),
1345 $store->getWatchedItem(
1346 $this->getMockNonAnonUserWithId( 1 ),
1347 new TitleValue( 0, 'SomeDbKey' )
1352 public function testGetWatchedItem_anonymousUser() {
1353 $mockDb = $this->getMockDb();
1354 $mockDb->expects( $this->never() )
1355 ->method( 'selectRow' );
1357 $mockCache = $this->getMockCache();
1358 $mockCache->expects( $this->never() )->method( 'set' );
1359 $mockCache->expects( $this->never() )->method( 'get' );
1360 $mockCache->expects( $this->never() )->method( 'delete' );
1362 $store = $this->newWatchedItemStore(
1363 $this->getMockLoadBalancer( $mockDb ),
1368 $store->getWatchedItem(
1369 $this->getAnonUser(),
1370 new TitleValue( 0, 'SomeDbKey' )
1375 public function testGetWatchedItemsForUser() {
1376 $mockDb = $this->getMockDb();
1377 $mockDb->expects( $this->once() )
1378 ->method( 'select' )
1381 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1384 ->will( $this->returnValue( [
1385 $this->getFakeRow( [
1386 'wl_namespace' => 0,
1387 'wl_title' => 'Foo1',
1388 'wl_notificationtimestamp' => '20151212010101',
1390 $this->getFakeRow( [
1391 'wl_namespace' => 1,
1392 'wl_title' => 'Foo2',
1393 'wl_notificationtimestamp' => null,
1397 $mockCache = $this->getMockCache();
1398 $mockCache->expects( $this->never() )->method( 'delete' );
1399 $mockCache->expects( $this->never() )->method( 'get' );
1400 $mockCache->expects( $this->never() )->method( 'set' );
1402 $store = $this->newWatchedItemStore(
1403 $this->getMockLoadBalancer( $mockDb ),
1406 $user = $this->getMockNonAnonUserWithId( 1 );
1408 $watchedItems = $store->getWatchedItemsForUser( $user );
1410 $this->assertInternalType( 'array', $watchedItems );
1411 $this->assertCount( 2, $watchedItems );
1412 foreach ( $watchedItems as $watchedItem ) {
1413 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1415 $this->assertEquals(
1416 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1419 $this->assertEquals(
1420 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1425 public function provideDbTypes() {
1427 [ false, DB_SLAVE
],
1428 [ true, DB_MASTER
],
1433 * @dataProvider provideDbTypes
1435 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1436 $mockDb = $this->getMockDb();
1437 $mockCache = $this->getMockCache();
1438 $mockLoadBalancer = $this->getMockLoadBalancer( $mockDb, $dbType );
1439 $user = $this->getMockNonAnonUserWithId( 1 );
1441 $mockDb->expects( $this->once() )
1442 ->method( 'select' )
1445 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1447 $this->isType( 'string' ),
1448 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1450 ->will( $this->returnValue( [] ) );
1452 $store = $this->newWatchedItemStore(
1457 $watchedItems = $store->getWatchedItemsForUser(
1459 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore
::SORT_ASC
]
1461 $this->assertEquals( [], $watchedItems );
1464 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
1465 $store = $this->newWatchedItemStore(
1466 $this->getMockLoadBalancer( $this->getMockDb() ),
1467 $this->getMockCache()
1470 $this->setExpectedException( 'InvalidArgumentException' );
1471 $store->getWatchedItemsForUser(
1472 $this->getMockNonAnonUserWithId( 1 ),
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 = $this->newWatchedItemStore(
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 ),