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 ) {
21 $mock = $this->getMockBuilder( LoadBalancer
::class )
22 ->disableOriginalConstructor()
24 $mock->expects( $this->any() )
25 ->method( 'getConnection' )
26 ->will( $this->returnValue( $mockDb ) );
27 $mock->expects( $this->any() )
28 ->method( 'getReadOnlyReason' )
29 ->will( $this->returnValue( false ) );
34 * @return PHPUnit_Framework_MockObject_MockObject|HashBagOStuff
36 private function getMockCache() {
37 $mock = $this->getMockBuilder( HashBagOStuff
::class )
38 ->disableOriginalConstructor()
40 $mock->expects( $this->any() )
42 ->will( $this->returnCallback( function() {
43 return implode( ':', func_get_args() );
50 * @return PHPUnit_Framework_MockObject_MockObject|User
52 private function getMockNonAnonUserWithId( $id ) {
53 $mock = $this->getMock( User
::class );
54 $mock->expects( $this->any() )
56 ->will( $this->returnValue( false ) );
57 $mock->expects( $this->any() )
59 ->will( $this->returnValue( $id ) );
66 private function getAnonUser() {
67 return User
::newFromName( 'Anon_User' );
70 private function getFakeRow( array $rowValues ) {
71 $fakeRow = new stdClass();
72 foreach ( $rowValues as $valueName => $value ) {
73 $fakeRow->$valueName = $value;
78 public function testGetDefaultInstance() {
79 $instanceOne = WatchedItemStore
::getDefaultInstance();
80 $instanceTwo = WatchedItemStore
::getDefaultInstance();
81 $this->assertSame( $instanceOne, $instanceTwo );
84 public function testOverrideDefaultInstance() {
85 $instance = WatchedItemStore
::getDefaultInstance();
86 $scopedOverride = $instance->overrideDefaultInstance( null );
88 $this->assertNotSame( $instance, WatchedItemStore
::getDefaultInstance() );
90 unset( $scopedOverride );
92 $this->assertSame( $instance, WatchedItemStore
::getDefaultInstance() );
95 public function testCountWatchedItems() {
96 $user = $this->getMockNonAnonUserWithId( 1 );
98 $mockDb = $this->getMockDb();
99 $mockDb->expects( $this->exactly( 1 ) )
100 ->method( 'selectField' )
105 'wl_user' => $user->getId(),
107 $this->isType( 'string' )
109 ->will( $this->returnValue( 12 ) );
111 $mockCache = $this->getMockCache();
112 $mockCache->expects( $this->never() )->method( 'get' );
113 $mockCache->expects( $this->never() )->method( 'set' );
114 $mockCache->expects( $this->never() )->method( 'delete' );
116 $store = new WatchedItemStore(
117 $this->getMockLoadBalancer( $mockDb ),
121 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
124 public function testCountWatchers() {
125 $titleValue = new TitleValue( 0, 'SomeDbKey' );
127 $mockDb = $this->getMockDb();
128 $mockDb->expects( $this->exactly( 1 ) )
129 ->method( 'selectField' )
134 'wl_namespace' => $titleValue->getNamespace(),
135 'wl_title' => $titleValue->getDBkey(),
137 $this->isType( 'string' )
139 ->will( $this->returnValue( 7 ) );
141 $mockCache = $this->getMockCache();
142 $mockCache->expects( $this->never() )->method( 'get' );
143 $mockCache->expects( $this->never() )->method( 'set' );
144 $mockCache->expects( $this->never() )->method( 'delete' );
146 $store = new WatchedItemStore(
147 $this->getMockLoadBalancer( $mockDb ),
151 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
154 public function testCountWatchersMultiple() {
156 new TitleValue( 0, 'SomeDbKey' ),
157 new TitleValue( 0, 'OtherDbKey' ),
158 new TitleValue( 1, 'AnotherDbKey' ),
161 $mockDb = $this->getMockDb();
164 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
165 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
166 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
169 $mockDb->expects( $this->once() )
170 ->method( 'makeWhereFrom2d' )
172 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
173 $this->isType( 'string' ),
174 $this->isType( 'string' )
176 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
177 $mockDb->expects( $this->once() )
181 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
182 [ 'makeWhereFrom2d return value' ],
183 $this->isType( 'string' ),
185 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
189 $this->returnValue( $dbResult )
192 $mockCache = $this->getMockCache();
193 $mockCache->expects( $this->never() )->method( 'get' );
194 $mockCache->expects( $this->never() )->method( 'set' );
195 $mockCache->expects( $this->never() )->method( 'delete' );
197 $store = new WatchedItemStore(
198 $this->getMockLoadBalancer( $mockDb ),
203 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
204 1 => [ 'AnotherDbKey' => 500 ],
206 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
209 public function provideIntWithDbUnsafeVersion() {
212 [ "50; DROP TABLE watchlist;\n--" ],
217 * @dataProvider provideIntWithDbUnsafeVersion
219 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
221 new TitleValue( 0, 'SomeDbKey' ),
222 new TitleValue( 0, 'OtherDbKey' ),
223 new TitleValue( 1, 'AnotherDbKey' ),
226 $mockDb = $this->getMockDb();
229 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
230 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
231 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
234 $mockDb->expects( $this->once() )
235 ->method( 'makeWhereFrom2d' )
237 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
238 $this->isType( 'string' ),
239 $this->isType( 'string' )
241 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
242 $mockDb->expects( $this->once() )
246 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
247 [ 'makeWhereFrom2d return value' ],
248 $this->isType( 'string' ),
250 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
251 'HAVING' => 'COUNT(*) >= 50',
255 $this->returnValue( $dbResult )
258 $mockCache = $this->getMockCache();
259 $mockCache->expects( $this->never() )->method( 'get' );
260 $mockCache->expects( $this->never() )->method( 'set' );
261 $mockCache->expects( $this->never() )->method( 'delete' );
263 $store = new WatchedItemStore(
264 $this->getMockLoadBalancer( $mockDb ),
269 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
270 1 => [ 'AnotherDbKey' => 500 ],
274 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
278 public function testCountVisitingWatchers() {
279 $titleValue = new TitleValue( 0, 'SomeDbKey' );
281 $mockDb = $this->getMockDb();
282 $mockDb->expects( $this->exactly( 1 ) )
283 ->method( 'selectField' )
288 'wl_namespace' => $titleValue->getNamespace(),
289 'wl_title' => $titleValue->getDBkey(),
290 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
292 $this->isType( 'string' )
294 ->will( $this->returnValue( 7 ) );
295 $mockDb->expects( $this->exactly( 1 ) )
296 ->method( 'addQuotes' )
297 ->will( $this->returnCallback( function( $value ) {
300 $mockDb->expects( $this->exactly( 1 ) )
301 ->method( 'timestamp' )
302 ->will( $this->returnCallback( function( $value ) {
303 return 'TS' . $value . 'TS';
306 $mockCache = $this->getMockCache();
307 $mockCache->expects( $this->never() )->method( 'set' );
308 $mockCache->expects( $this->never() )->method( 'get' );
309 $mockCache->expects( $this->never() )->method( 'delete' );
311 $store = new WatchedItemStore(
312 $this->getMockLoadBalancer( $mockDb ),
316 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
319 public function testCountVisitingWatchersMultiple() {
320 $titleValuesWithThresholds = [
321 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
322 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
323 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
327 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
328 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
329 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
331 $mockDb = $this->getMockDb();
332 $mockDb->expects( $this->exactly( 2 * 3 ) )
333 ->method( 'addQuotes' )
334 ->will( $this->returnCallback( function( $value ) {
337 $mockDb->expects( $this->exactly( 3 ) )
338 ->method( 'timestamp' )
339 ->will( $this->returnCallback( function( $value ) {
340 return 'TS' . $value . 'TS';
342 $mockDb->expects( $this->any() )
343 ->method( 'makeList' )
345 $this->isType( 'array' ),
346 $this->isType( 'int' )
348 ->will( $this->returnCallback( function( $a, $conj ) {
349 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
350 return join( $sqlConj, array_map( function( $s ) {
351 return '(' . $s . ')';
355 $mockDb->expects( $this->never() )
356 ->method( 'makeWhereFrom2d' );
359 '((wl_namespace = 0) AND (' .
360 "(((wl_title = 'SomeDbKey') AND (" .
361 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
363 "(wl_title = 'OtherDbKey') AND (" .
364 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
366 ') OR ((wl_namespace = 1) AND (' .
367 "(((wl_title = 'AnotherDbKey') AND (".
368 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
370 $mockDb->expects( $this->once() )
374 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
376 $this->isType( 'string' ),
378 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
382 $this->returnValue( $dbResult )
385 $mockCache = $this->getMockCache();
386 $mockCache->expects( $this->never() )->method( 'get' );
387 $mockCache->expects( $this->never() )->method( 'set' );
388 $mockCache->expects( $this->never() )->method( 'delete' );
390 $store = new WatchedItemStore(
391 $this->getMockLoadBalancer( $mockDb ),
396 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
397 1 => [ 'AnotherDbKey' => 500 ],
401 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
405 public function testCountVisitingWatchersMultiple_withMissingTargets() {
406 $titleValuesWithThresholds = [
407 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
408 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
409 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
410 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
411 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
415 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
416 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
417 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
419 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 100 ]
422 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 200 ]
425 $mockDb = $this->getMockDb();
426 $mockDb->expects( $this->exactly( 2 * 3 ) )
427 ->method( 'addQuotes' )
428 ->will( $this->returnCallback( function( $value ) {
431 $mockDb->expects( $this->exactly( 3 ) )
432 ->method( 'timestamp' )
433 ->will( $this->returnCallback( function( $value ) {
434 return 'TS' . $value . 'TS';
436 $mockDb->expects( $this->any() )
437 ->method( 'makeList' )
439 $this->isType( 'array' ),
440 $this->isType( 'int' )
442 ->will( $this->returnCallback( function( $a, $conj ) {
443 $sqlConj = $conj === LIST_AND ?
' AND ' : ' OR ';
444 return join( $sqlConj, array_map( function( $s ) {
445 return '(' . $s . ')';
449 $mockDb->expects( $this->once() )
450 ->method( 'makeWhereFrom2d' )
452 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
453 $this->isType( 'string' ),
454 $this->isType( 'string' )
456 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
459 '((wl_namespace = 0) AND (' .
460 "(((wl_title = 'SomeDbKey') AND (" .
461 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
463 "(wl_title = 'OtherDbKey') AND (" .
464 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
466 ') OR ((wl_namespace = 1) AND (' .
467 "(((wl_title = 'AnotherDbKey') AND (".
468 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
471 '(makeWhereFrom2d return value)';
472 $mockDb->expects( $this->once() )
476 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
478 $this->isType( 'string' ),
480 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
484 $this->returnValue( $dbResult )
487 $mockCache = $this->getMockCache();
488 $mockCache->expects( $this->never() )->method( 'get' );
489 $mockCache->expects( $this->never() )->method( 'set' );
490 $mockCache->expects( $this->never() )->method( 'delete' );
492 $store = new WatchedItemStore(
493 $this->getMockLoadBalancer( $mockDb ),
499 'SomeDbKey' => 100, 'OtherDbKey' => 300,
500 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
502 1 => [ 'AnotherDbKey' => 500 ],
506 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
511 * @dataProvider provideIntWithDbUnsafeVersion
513 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
514 $titleValuesWithThresholds = [
515 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
516 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
517 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
520 $mockDb = $this->getMockDb();
521 $mockDb->expects( $this->any() )
522 ->method( 'makeList' )
523 ->will( $this->returnValue( 'makeList return value' ) );
524 $mockDb->expects( $this->once() )
528 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
529 'makeList return value',
530 $this->isType( 'string' ),
532 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
533 'HAVING' => 'COUNT(*) >= 50',
537 $this->returnValue( [] )
540 $mockCache = $this->getMockCache();
541 $mockCache->expects( $this->never() )->method( 'get' );
542 $mockCache->expects( $this->never() )->method( 'set' );
543 $mockCache->expects( $this->never() )->method( 'delete' );
545 $store = new WatchedItemStore(
546 $this->getMockLoadBalancer( $mockDb ),
551 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
552 1 => [ 'AnotherDbKey' => 0 ],
556 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
560 public function testCountUnreadNotifications() {
561 $user = $this->getMockNonAnonUserWithId( 1 );
563 $mockDb = $this->getMockDb();
564 $mockDb->expects( $this->exactly( 1 ) )
565 ->method( 'selectRowCount' )
570 "wl_notificationtimestamp IS NOT NULL",
573 $this->isType( 'string' )
575 ->will( $this->returnValue( 9 ) );
577 $mockCache = $this->getMockCache();
578 $mockCache->expects( $this->never() )->method( 'set' );
579 $mockCache->expects( $this->never() )->method( 'get' );
580 $mockCache->expects( $this->never() )->method( 'delete' );
582 $store = new WatchedItemStore(
583 $this->getMockLoadBalancer( $mockDb ),
587 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
591 * @dataProvider provideIntWithDbUnsafeVersion
593 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
594 $user = $this->getMockNonAnonUserWithId( 1 );
596 $mockDb = $this->getMockDb();
597 $mockDb->expects( $this->exactly( 1 ) )
598 ->method( 'selectRowCount' )
603 "wl_notificationtimestamp IS NOT NULL",
606 $this->isType( 'string' ),
609 ->will( $this->returnValue( 50 ) );
611 $mockCache = $this->getMockCache();
612 $mockCache->expects( $this->never() )->method( 'set' );
613 $mockCache->expects( $this->never() )->method( 'get' );
614 $mockCache->expects( $this->never() )->method( 'delete' );
616 $store = new WatchedItemStore(
617 $this->getMockLoadBalancer( $mockDb ),
623 $store->countUnreadNotifications( $user, $limit )
628 * @dataProvider provideIntWithDbUnsafeVersion
630 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
631 $user = $this->getMockNonAnonUserWithId( 1 );
633 $mockDb = $this->getMockDb();
634 $mockDb->expects( $this->exactly( 1 ) )
635 ->method( 'selectRowCount' )
640 "wl_notificationtimestamp IS NOT NULL",
643 $this->isType( 'string' ),
646 ->will( $this->returnValue( 9 ) );
648 $mockCache = $this->getMockCache();
649 $mockCache->expects( $this->never() )->method( 'set' );
650 $mockCache->expects( $this->never() )->method( 'get' );
651 $mockCache->expects( $this->never() )->method( 'delete' );
653 $store = new WatchedItemStore(
654 $this->getMockLoadBalancer( $mockDb ),
660 $store->countUnreadNotifications( $user, $limit )
664 public function testDuplicateEntry_nothingToDuplicate() {
665 $mockDb = $this->getMockDb();
666 $mockDb->expects( $this->once() )
672 'wl_notificationtimestamp',
676 'wl_title' => 'Old_Title',
678 'WatchedItemStore::duplicateEntry',
681 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
683 $store = new WatchedItemStore(
684 $this->getMockLoadBalancer( $mockDb ),
685 $this->getMockCache()
688 $store->duplicateEntry(
689 Title
::newFromText( 'Old_Title' ),
690 Title
::newFromText( 'New_Title' )
694 public function testDuplicateEntry_somethingToDuplicate() {
696 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
697 $this->getFakeRow( [ 'wl_user' => 2, 'wl_notificationtimestamp' => null ] ),
700 $mockDb = $this->getMockDb();
701 $mockDb->expects( $this->at( 0 ) )
707 'wl_notificationtimestamp',
711 'wl_title' => 'Old_Title',
714 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
715 $mockDb->expects( $this->at( 1 ) )
716 ->method( 'replace' )
719 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
724 'wl_title' => 'New_Title',
725 'wl_notificationtimestamp' => '20151212010101',
730 'wl_title' => 'New_Title',
731 'wl_notificationtimestamp' => null,
734 $this->isType( 'string' )
737 $mockCache = $this->getMockCache();
738 $mockCache->expects( $this->never() )->method( 'get' );
739 $mockCache->expects( $this->never() )->method( 'delete' );
741 $store = new WatchedItemStore(
742 $this->getMockLoadBalancer( $mockDb ),
746 $store->duplicateEntry(
747 Title
::newFromText( 'Old_Title' ),
748 Title
::newFromText( 'New_Title' )
752 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
753 $mockDb = $this->getMockDb();
754 $mockDb->expects( $this->at( 0 ) )
760 'wl_notificationtimestamp',
764 'wl_title' => 'Old_Title',
767 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
768 $mockDb->expects( $this->at( 1 ) )
774 'wl_notificationtimestamp',
778 'wl_title' => 'Old_Title',
781 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
783 $mockCache = $this->getMockCache();
784 $mockCache->expects( $this->never() )->method( 'get' );
785 $mockCache->expects( $this->never() )->method( 'delete' );
787 $store = new WatchedItemStore(
788 $this->getMockLoadBalancer( $mockDb ),
792 $store->duplicateAllAssociatedEntries(
793 Title
::newFromText( 'Old_Title' ),
794 Title
::newFromText( 'New_Title' )
798 public function testDuplicateAllAssociatedEntries_somethingToDuplicate() {
800 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
803 $mockDb = $this->getMockDb();
804 $mockDb->expects( $this->at( 0 ) )
810 'wl_notificationtimestamp',
814 'wl_title' => 'Old_Title',
817 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
818 $mockDb->expects( $this->at( 1 ) )
819 ->method( 'replace' )
822 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
827 'wl_title' => 'New_Title',
828 'wl_notificationtimestamp' => '20151212010101',
831 $this->isType( 'string' )
833 $mockDb->expects( $this->at( 2 ) )
839 'wl_notificationtimestamp',
843 'wl_title' => 'Old_Title',
846 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
847 $mockDb->expects( $this->at( 3 ) )
848 ->method( 'replace' )
851 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
856 'wl_title' => 'New_Title',
857 'wl_notificationtimestamp' => '20151212010101',
860 $this->isType( 'string' )
863 $mockCache = $this->getMockCache();
864 $mockCache->expects( $this->never() )->method( 'get' );
865 $mockCache->expects( $this->never() )->method( 'delete' );
867 $store = new WatchedItemStore(
868 $this->getMockLoadBalancer( $mockDb ),
872 $store->duplicateAllAssociatedEntries(
873 Title
::newFromText( 'Old_Title' ),
874 Title
::newFromText( 'New_Title' )
878 public function testAddWatch_nonAnonymousUser() {
879 $mockDb = $this->getMockDb();
880 $mockDb->expects( $this->once() )
888 'wl_title' => 'Some_Page',
889 'wl_notificationtimestamp' => null,
894 $mockCache = $this->getMockCache();
895 $mockCache->expects( $this->once() )
897 ->with( '0:Some_Page:1' );
899 $store = new WatchedItemStore(
900 $this->getMockLoadBalancer( $mockDb ),
905 $this->getMockNonAnonUserWithId( 1 ),
906 Title
::newFromText( 'Some_Page' )
910 public function testAddWatch_anonymousUser() {
911 $mockDb = $this->getMockDb();
912 $mockDb->expects( $this->never() )
913 ->method( 'insert' );
915 $mockCache = $this->getMockCache();
916 $mockCache->expects( $this->never() )
917 ->method( 'delete' );
919 $store = new WatchedItemStore(
920 $this->getMockLoadBalancer( $mockDb ),
925 $this->getAnonUser(),
926 Title
::newFromText( 'Some_Page' )
930 public function testAddWatchBatch_nonAnonymousUser() {
931 $mockDb = $this->getMockDb();
932 $mockDb->expects( $this->once() )
940 'wl_title' => 'Some_Page',
941 'wl_notificationtimestamp' => null,
946 'wl_title' => 'Some_Page',
947 'wl_notificationtimestamp' => null,
952 $mockCache = $this->getMockCache();
953 $mockCache->expects( $this->exactly( 2 ) )
954 ->method( 'delete' );
955 $mockCache->expects( $this->at( 1 ) )
957 ->with( '0:Some_Page:1' );
958 $mockCache->expects( $this->at( 3 ) )
960 ->with( '1:Some_Page:1' );
962 $store = new WatchedItemStore(
963 $this->getMockLoadBalancer( $mockDb ),
967 $mockUser = $this->getMockNonAnonUserWithId( 1 );
970 $store->addWatchBatch(
972 [ $mockUser, new TitleValue( 0, 'Some_Page' ) ],
973 [ $mockUser, new TitleValue( 1, 'Some_Page' ) ],
979 public function testAddWatchBatch_anonymousUserCombinationsAreSkipped() {
980 $mockDb = $this->getMockDb();
981 $mockDb->expects( $this->once() )
989 'wl_title' => 'Some_Page',
990 'wl_notificationtimestamp' => null,
995 $mockCache = $this->getMockCache();
996 $mockCache->expects( $this->once() )
998 ->with( '0:Some_Page:1' );
1000 $store = new WatchedItemStore(
1001 $this->getMockLoadBalancer( $mockDb ),
1006 $store->addWatchBatch(
1008 [ $this->getMockNonAnonUserWithId( 1 ), new TitleValue( 0, 'Some_Page' ) ],
1009 [ $this->getAnonUser(), new TitleValue( 0, 'Other_Page' ) ],
1015 public function testAddWatchBatchReturnsFalse_whenOnlyGivenAnonymousUserCombinations() {
1016 $mockDb = $this->getMockDb();
1017 $mockDb->expects( $this->never() )
1018 ->method( 'insert' );
1020 $mockCache = $this->getMockCache();
1021 $mockCache->expects( $this->never() )
1022 ->method( 'delete' );
1024 $store = new WatchedItemStore(
1025 $this->getMockLoadBalancer( $mockDb ),
1029 $anonUser = $this->getAnonUser();
1031 $store->addWatchBatch(
1033 [ $anonUser, new TitleValue( 0, 'Some_Page' ) ],
1034 [ $anonUser, new TitleValue( 1, 'Other_Page' ) ],
1040 public function testAddWatchBatchReturnsFalse_whenGivenEmptyList() {
1041 $mockDb = $this->getMockDb();
1042 $mockDb->expects( $this->never() )
1043 ->method( 'insert' );
1045 $mockCache = $this->getMockCache();
1046 $mockCache->expects( $this->never() )
1047 ->method( 'delete' );
1049 $store = new WatchedItemStore(
1050 $this->getMockLoadBalancer( $mockDb ),
1055 $store->addWatchBatch( [] )
1059 public function testLoadWatchedItem_existingItem() {
1060 $mockDb = $this->getMockDb();
1061 $mockDb->expects( $this->once() )
1062 ->method( 'selectRow' )
1065 'wl_notificationtimestamp',
1068 'wl_namespace' => 0,
1069 'wl_title' => 'SomeDbKey',
1072 ->will( $this->returnValue(
1073 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1076 $mockCache = $this->getMockCache();
1077 $mockCache->expects( $this->once() )
1083 $store = new WatchedItemStore(
1084 $this->getMockLoadBalancer( $mockDb ),
1088 $watchedItem = $store->loadWatchedItem(
1089 $this->getMockNonAnonUserWithId( 1 ),
1090 new TitleValue( 0, 'SomeDbKey' )
1092 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1093 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1094 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1095 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1098 public function testLoadWatchedItem_noItem() {
1099 $mockDb = $this->getMockDb();
1100 $mockDb->expects( $this->once() )
1101 ->method( 'selectRow' )
1104 'wl_notificationtimestamp',
1107 'wl_namespace' => 0,
1108 'wl_title' => 'SomeDbKey',
1111 ->will( $this->returnValue( [] ) );
1113 $mockCache = $this->getMockCache();
1114 $mockCache->expects( $this->never() )->method( 'get' );
1115 $mockCache->expects( $this->never() )->method( 'delete' );
1117 $store = new WatchedItemStore(
1118 $this->getMockLoadBalancer( $mockDb ),
1123 $store->loadWatchedItem(
1124 $this->getMockNonAnonUserWithId( 1 ),
1125 new TitleValue( 0, 'SomeDbKey' )
1130 public function testLoadWatchedItem_anonymousUser() {
1131 $mockDb = $this->getMockDb();
1132 $mockDb->expects( $this->never() )
1133 ->method( 'selectRow' );
1135 $mockCache = $this->getMockCache();
1136 $mockCache->expects( $this->never() )->method( 'get' );
1137 $mockCache->expects( $this->never() )->method( 'delete' );
1139 $store = new WatchedItemStore(
1140 $this->getMockLoadBalancer( $mockDb ),
1145 $store->loadWatchedItem(
1146 $this->getAnonUser(),
1147 new TitleValue( 0, 'SomeDbKey' )
1152 public function testRemoveWatch_existingItem() {
1153 $mockDb = $this->getMockDb();
1154 $mockDb->expects( $this->once() )
1155 ->method( 'delete' )
1160 'wl_namespace' => 0,
1161 'wl_title' => 'SomeDbKey',
1164 $mockDb->expects( $this->once() )
1165 ->method( 'affectedRows' )
1166 ->will( $this->returnValue( 1 ) );
1168 $mockCache = $this->getMockCache();
1169 $mockCache->expects( $this->never() )->method( 'get' );
1170 $mockCache->expects( $this->once() )
1171 ->method( 'delete' )
1172 ->with( '0:SomeDbKey:1' );
1174 $store = new WatchedItemStore(
1175 $this->getMockLoadBalancer( $mockDb ),
1180 $store->removeWatch(
1181 $this->getMockNonAnonUserWithId( 1 ),
1182 new TitleValue( 0, 'SomeDbKey' )
1187 public function testRemoveWatch_noItem() {
1188 $mockDb = $this->getMockDb();
1189 $mockDb->expects( $this->once() )
1190 ->method( 'delete' )
1195 'wl_namespace' => 0,
1196 'wl_title' => 'SomeDbKey',
1199 $mockDb->expects( $this->once() )
1200 ->method( 'affectedRows' )
1201 ->will( $this->returnValue( 0 ) );
1203 $mockCache = $this->getMockCache();
1204 $mockCache->expects( $this->never() )->method( 'get' );
1205 $mockCache->expects( $this->once() )
1206 ->method( 'delete' )
1207 ->with( '0:SomeDbKey:1' );
1209 $store = new WatchedItemStore(
1210 $this->getMockLoadBalancer( $mockDb ),
1215 $store->removeWatch(
1216 $this->getMockNonAnonUserWithId( 1 ),
1217 new TitleValue( 0, 'SomeDbKey' )
1222 public function testRemoveWatch_anonymousUser() {
1223 $mockDb = $this->getMockDb();
1224 $mockDb->expects( $this->never() )
1225 ->method( 'delete' );
1227 $mockCache = $this->getMockCache();
1228 $mockCache->expects( $this->never() )->method( 'get' );
1229 $mockCache->expects( $this->never() )
1230 ->method( 'delete' );
1232 $store = new WatchedItemStore(
1233 $this->getMockLoadBalancer( $mockDb ),
1238 $store->removeWatch(
1239 $this->getAnonUser(),
1240 new TitleValue( 0, 'SomeDbKey' )
1245 public function testGetWatchedItem_existingItem() {
1246 $mockDb = $this->getMockDb();
1247 $mockDb->expects( $this->once() )
1248 ->method( 'selectRow' )
1251 'wl_notificationtimestamp',
1254 'wl_namespace' => 0,
1255 'wl_title' => 'SomeDbKey',
1258 ->will( $this->returnValue(
1259 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1262 $mockCache = $this->getMockCache();
1263 $mockCache->expects( $this->never() )->method( 'delete' );
1264 $mockCache->expects( $this->once() )
1269 ->will( $this->returnValue( null ) );
1270 $mockCache->expects( $this->once() )
1276 $store = new WatchedItemStore(
1277 $this->getMockLoadBalancer( $mockDb ),
1281 $watchedItem = $store->getWatchedItem(
1282 $this->getMockNonAnonUserWithId( 1 ),
1283 new TitleValue( 0, 'SomeDbKey' )
1285 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1286 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1287 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1288 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1291 public function testGetWatchedItem_cachedItem() {
1292 $mockDb = $this->getMockDb();
1293 $mockDb->expects( $this->never() )
1294 ->method( 'selectRow' );
1296 $mockUser = $this->getMockNonAnonUserWithId( 1 );
1297 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1298 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1300 $mockCache = $this->getMockCache();
1301 $mockCache->expects( $this->never() )->method( 'delete' );
1302 $mockCache->expects( $this->never() )->method( 'set' );
1303 $mockCache->expects( $this->once() )
1308 ->will( $this->returnValue( $cachedItem ) );
1310 $store = new WatchedItemStore(
1311 $this->getMockLoadBalancer( $mockDb ),
1315 $this->assertEquals(
1317 $store->getWatchedItem(
1324 public function testGetWatchedItem_noItem() {
1325 $mockDb = $this->getMockDb();
1326 $mockDb->expects( $this->once() )
1327 ->method( 'selectRow' )
1330 'wl_notificationtimestamp',
1333 'wl_namespace' => 0,
1334 'wl_title' => 'SomeDbKey',
1337 ->will( $this->returnValue( [] ) );
1339 $mockCache = $this->getMockCache();
1340 $mockCache->expects( $this->never() )->method( 'set' );
1341 $mockCache->expects( $this->never() )->method( 'delete' );
1342 $mockCache->expects( $this->once() )
1344 ->with( '0:SomeDbKey:1' )
1345 ->will( $this->returnValue( false ) );
1347 $store = new WatchedItemStore(
1348 $this->getMockLoadBalancer( $mockDb ),
1353 $store->getWatchedItem(
1354 $this->getMockNonAnonUserWithId( 1 ),
1355 new TitleValue( 0, 'SomeDbKey' )
1360 public function testGetWatchedItem_anonymousUser() {
1361 $mockDb = $this->getMockDb();
1362 $mockDb->expects( $this->never() )
1363 ->method( 'selectRow' );
1365 $mockCache = $this->getMockCache();
1366 $mockCache->expects( $this->never() )->method( 'set' );
1367 $mockCache->expects( $this->never() )->method( 'get' );
1368 $mockCache->expects( $this->never() )->method( 'delete' );
1370 $store = new WatchedItemStore(
1371 $this->getMockLoadBalancer( $mockDb ),
1376 $store->getWatchedItem(
1377 $this->getAnonUser(),
1378 new TitleValue( 0, 'SomeDbKey' )
1383 public function testIsWatchedItem_existingItem() {
1384 $mockDb = $this->getMockDb();
1385 $mockDb->expects( $this->once() )
1386 ->method( 'selectRow' )
1389 'wl_notificationtimestamp',
1392 'wl_namespace' => 0,
1393 'wl_title' => 'SomeDbKey',
1396 ->will( $this->returnValue(
1397 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1400 $mockCache = $this->getMockCache();
1401 $mockCache->expects( $this->never() )->method( 'delete' );
1402 $mockCache->expects( $this->once() )
1404 ->with( '0:SomeDbKey:1' )
1405 ->will( $this->returnValue( false ) );
1406 $mockCache->expects( $this->once() )
1412 $store = new WatchedItemStore(
1413 $this->getMockLoadBalancer( $mockDb ),
1419 $this->getMockNonAnonUserWithId( 1 ),
1420 new TitleValue( 0, 'SomeDbKey' )
1425 public function testIsWatchedItem_noItem() {
1426 $mockDb = $this->getMockDb();
1427 $mockDb->expects( $this->once() )
1428 ->method( 'selectRow' )
1431 'wl_notificationtimestamp',
1434 'wl_namespace' => 0,
1435 'wl_title' => 'SomeDbKey',
1438 ->will( $this->returnValue( [] ) );
1440 $mockCache = $this->getMockCache();
1441 $mockCache->expects( $this->never() )->method( 'set' );
1442 $mockCache->expects( $this->never() )->method( 'delete' );
1443 $mockCache->expects( $this->once() )
1445 ->with( '0:SomeDbKey:1' )
1446 ->will( $this->returnValue( false ) );
1448 $store = new WatchedItemStore(
1449 $this->getMockLoadBalancer( $mockDb ),
1455 $this->getMockNonAnonUserWithId( 1 ),
1456 new TitleValue( 0, 'SomeDbKey' )
1461 public function testIsWatchedItem_anonymousUser() {
1462 $mockDb = $this->getMockDb();
1463 $mockDb->expects( $this->never() )
1464 ->method( 'selectRow' );
1466 $mockCache = $this->getMockCache();
1467 $mockCache->expects( $this->never() )->method( 'set' );
1468 $mockCache->expects( $this->never() )->method( 'get' );
1469 $mockCache->expects( $this->never() )->method( 'delete' );
1471 $store = new WatchedItemStore(
1472 $this->getMockLoadBalancer( $mockDb ),
1478 $this->getAnonUser(),
1479 new TitleValue( 0, 'SomeDbKey' )
1484 public function testGetNotificationTimestampsBatch() {
1486 new TitleValue( 0, 'SomeDbKey' ),
1487 new TitleValue( 1, 'AnotherDbKey' ),
1490 $mockDb = $this->getMockDb();
1492 $this->getFakeRow( [
1493 'wl_namespace' => 0,
1494 'wl_title' => 'SomeDbKey',
1495 'wl_notificationtimestamp' => '20151212010101',
1499 'wl_namespace' => 1,
1500 'wl_title' => 'AnotherDbKey',
1501 'wl_notificationtimestamp' => null,
1506 $mockDb->expects( $this->once() )
1507 ->method( 'makeWhereFrom2d' )
1509 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1510 $this->isType( 'string' ),
1511 $this->isType( 'string' )
1513 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1514 $mockDb->expects( $this->once() )
1515 ->method( 'select' )
1518 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1520 'makeWhereFrom2d return value',
1523 $this->isType( 'string' )
1525 ->will( $this->returnValue( $dbResult ) );
1527 $mockCache = $this->getMockCache();
1528 $mockCache->expects( $this->exactly( 2 ) )
1531 [ '0:SomeDbKey:1' ],
1532 [ '1:AnotherDbKey:1' ]
1534 ->will( $this->returnValue( null ) );
1535 $mockCache->expects( $this->never() )->method( 'set' );
1536 $mockCache->expects( $this->never() )->method( 'delete' );
1538 $store = new WatchedItemStore(
1539 $this->getMockLoadBalancer( $mockDb ),
1543 $this->assertEquals(
1545 0 => [ 'SomeDbKey' => '20151212010101', ],
1546 1 => [ 'AnotherDbKey' => null, ],
1548 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1552 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1554 new TitleValue( 0, 'OtherDbKey' ),
1557 $mockDb = $this->getMockDb();
1559 $mockDb->expects( $this->once() )
1560 ->method( 'makeWhereFrom2d' )
1562 [ [ 'OtherDbKey' => 1 ] ],
1563 $this->isType( 'string' ),
1564 $this->isType( 'string' )
1566 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1567 $mockDb->expects( $this->once() )
1568 ->method( 'select' )
1571 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1573 'makeWhereFrom2d return value',
1576 $this->isType( 'string' )
1578 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1580 $mockCache = $this->getMockCache();
1581 $mockCache->expects( $this->once() )
1583 ->with( '0:OtherDbKey:1' )
1584 ->will( $this->returnValue( null ) );
1585 $mockCache->expects( $this->never() )->method( 'set' );
1586 $mockCache->expects( $this->never() )->method( 'delete' );
1588 $store = new WatchedItemStore(
1589 $this->getMockLoadBalancer( $mockDb ),
1593 $this->assertEquals(
1595 0 => [ 'OtherDbKey' => false, ],
1597 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1601 public function testGetNotificationTimestampsBatch_cachedItem() {
1603 new TitleValue( 0, 'SomeDbKey' ),
1604 new TitleValue( 1, 'AnotherDbKey' ),
1607 $user = $this->getMockNonAnonUserWithId( 1 );
1608 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1610 $mockDb = $this->getMockDb();
1612 $mockDb->expects( $this->once() )
1613 ->method( 'makeWhereFrom2d' )
1615 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1616 $this->isType( 'string' ),
1617 $this->isType( 'string' )
1619 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1620 $mockDb->expects( $this->once() )
1621 ->method( 'select' )
1624 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1626 'makeWhereFrom2d return value',
1629 $this->isType( 'string' )
1631 ->will( $this->returnValue( [
1633 [ 'wl_namespace' => 1, 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1637 $mockCache = $this->getMockCache();
1638 $mockCache->expects( $this->at( 1 ) )
1640 ->with( '0:SomeDbKey:1' )
1641 ->will( $this->returnValue( $cachedItem ) );
1642 $mockCache->expects( $this->at( 3 ) )
1644 ->with( '1:AnotherDbKey:1' )
1645 ->will( $this->returnValue( null ) );
1646 $mockCache->expects( $this->never() )->method( 'set' );
1647 $mockCache->expects( $this->never() )->method( 'delete' );
1649 $store = new WatchedItemStore(
1650 $this->getMockLoadBalancer( $mockDb ),
1654 $this->assertEquals(
1656 0 => [ 'SomeDbKey' => '20151212010101', ],
1657 1 => [ 'AnotherDbKey' => null, ],
1659 $store->getNotificationTimestampsBatch( $user, $targets )
1663 public function testGetNotificationTimestampsBatch_allItemsCached() {
1665 new TitleValue( 0, 'SomeDbKey' ),
1666 new TitleValue( 1, 'AnotherDbKey' ),
1669 $user = $this->getMockNonAnonUserWithId( 1 );
1671 new WatchedItem( $user, $targets[0], '20151212010101' ),
1672 new WatchedItem( $user, $targets[1], null ),
1674 $mockDb = $this->getMockDb();
1675 $mockDb->expects( $this->never() )->method( $this->anything() );
1677 $mockCache = $this->getMockCache();
1678 $mockCache->expects( $this->at( 1 ) )
1680 ->with( '0:SomeDbKey:1' )
1681 ->will( $this->returnValue( $cachedItems[0] ) );
1682 $mockCache->expects( $this->at( 3 ) )
1684 ->with( '1:AnotherDbKey:1' )
1685 ->will( $this->returnValue( $cachedItems[1] ) );
1686 $mockCache->expects( $this->never() )->method( 'set' );
1687 $mockCache->expects( $this->never() )->method( 'delete' );
1689 $store = new WatchedItemStore(
1690 $this->getMockLoadBalancer( $mockDb ),
1694 $this->assertEquals(
1696 0 => [ 'SomeDbKey' => '20151212010101', ],
1697 1 => [ 'AnotherDbKey' => null, ],
1699 $store->getNotificationTimestampsBatch( $user, $targets )
1703 public function testGetNotificationTimestampsBatch_anonymousUser() {
1705 new TitleValue( 0, 'SomeDbKey' ),
1706 new TitleValue( 1, 'AnotherDbKey' ),
1709 $mockDb = $this->getMockDb();
1710 $mockDb->expects( $this->never() )->method( $this->anything() );
1712 $mockCache = $this->getMockCache();
1713 $mockCache->expects( $this->never() )->method( $this->anything() );
1715 $store = new WatchedItemStore(
1716 $this->getMockLoadBalancer( $mockDb ),
1720 $this->assertEquals(
1722 0 => [ 'SomeDbKey' => false, ],
1723 1 => [ 'AnotherDbKey' => false, ],
1725 $store->getNotificationTimestampsBatch( $this->getAnonUser(), $targets )
1729 public function testResetNotificationTimestamp_anonymousUser() {
1730 $mockDb = $this->getMockDb();
1731 $mockDb->expects( $this->never() )
1732 ->method( 'selectRow' );
1734 $mockCache = $this->getMockCache();
1735 $mockCache->expects( $this->never() )->method( 'get' );
1736 $mockCache->expects( $this->never() )->method( 'set' );
1737 $mockCache->expects( $this->never() )->method( 'delete' );
1739 $store = new WatchedItemStore(
1740 $this->getMockLoadBalancer( $mockDb ),
1745 $store->resetNotificationTimestamp(
1746 $this->getAnonUser(),
1747 Title
::newFromText( 'SomeDbKey' )
1752 public function testResetNotificationTimestamp_noItem() {
1753 $mockDb = $this->getMockDb();
1754 $mockDb->expects( $this->once() )
1755 ->method( 'selectRow' )
1758 'wl_notificationtimestamp',
1761 'wl_namespace' => 0,
1762 'wl_title' => 'SomeDbKey',
1765 ->will( $this->returnValue( [] ) );
1767 $mockCache = $this->getMockCache();
1768 $mockCache->expects( $this->never() )->method( 'get' );
1769 $mockCache->expects( $this->never() )->method( 'set' );
1770 $mockCache->expects( $this->never() )->method( 'delete' );
1772 $store = new WatchedItemStore(
1773 $this->getMockLoadBalancer( $mockDb ),
1778 $store->resetNotificationTimestamp(
1779 $this->getMockNonAnonUserWithId( 1 ),
1780 Title
::newFromText( 'SomeDbKey' )
1785 public function testResetNotificationTimestamp_item() {
1786 $user = $this->getMockNonAnonUserWithId( 1 );
1787 $title = Title
::newFromText( 'SomeDbKey' );
1789 $mockDb = $this->getMockDb();
1790 $mockDb->expects( $this->once() )
1791 ->method( 'selectRow' )
1794 'wl_notificationtimestamp',
1797 'wl_namespace' => 0,
1798 'wl_title' => 'SomeDbKey',
1801 ->will( $this->returnValue(
1802 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1805 $mockCache = $this->getMockCache();
1806 $mockCache->expects( $this->never() )->method( 'get' );
1807 $mockCache->expects( $this->once() )
1811 $this->isInstanceOf( WatchedItem
::class )
1813 $mockCache->expects( $this->once() )
1814 ->method( 'delete' )
1815 ->with( '0:SomeDbKey:1' );
1817 $store = new WatchedItemStore(
1818 $this->getMockLoadBalancer( $mockDb ),
1822 // Note: This does not actually assert the job is correct
1823 $callableCallCounter = 0;
1824 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1825 $callableCallCounter++
;
1826 $this->assertInternalType( 'callable', $callable );
1828 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1831 $store->resetNotificationTimestamp(
1836 $this->assertEquals( 1, $callableCallCounter );
1838 ScopedCallback
::consume( $scopedOverride );
1841 public function testResetNotificationTimestamp_noItemForced() {
1842 $user = $this->getMockNonAnonUserWithId( 1 );
1843 $title = Title
::newFromText( 'SomeDbKey' );
1845 $mockDb = $this->getMockDb();
1846 $mockDb->expects( $this->never() )
1847 ->method( 'selectRow' );
1849 $mockCache = $this->getMockCache();
1850 $mockDb->expects( $this->never() )
1852 $mockDb->expects( $this->never() )
1854 $mockDb->expects( $this->never() )
1855 ->method( 'delete' );
1857 $store = new WatchedItemStore(
1858 $this->getMockLoadBalancer( $mockDb ),
1862 // Note: This does not actually assert the job is correct
1863 $callableCallCounter = 0;
1864 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1865 $callableCallCounter++
;
1866 $this->assertInternalType( 'callable', $callable );
1868 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1871 $store->resetNotificationTimestamp(
1877 $this->assertEquals( 1, $callableCallCounter );
1879 ScopedCallback
::consume( $scopedOverride );
1886 * @return PHPUnit_Framework_MockObject_MockObject|Title
1888 private function getMockTitle( $text, $ns = 0 ) {
1889 $title = $this->getMock( Title
::class );
1890 $title->expects( $this->any() )
1891 ->method( 'getText' )
1892 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
1893 $title->expects( $this->any() )
1894 ->method( 'getDbKey' )
1895 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
1896 $title->expects( $this->any() )
1897 ->method( 'getNamespace' )
1898 ->will( $this->returnValue( $ns ) );
1902 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
1903 $user = $this->getMockNonAnonUserWithId( 1 );
1905 $title = $this->getMockTitle( 'SomeTitle' );
1906 $title->expects( $this->once() )
1907 ->method( 'getNextRevisionID' )
1909 ->will( $this->returnValue( false ) );
1911 $mockDb = $this->getMockDb();
1912 $mockDb->expects( $this->never() )
1913 ->method( 'selectRow' );
1915 $mockCache = $this->getMockCache();
1916 $mockDb->expects( $this->never() )
1918 $mockDb->expects( $this->never() )
1920 $mockDb->expects( $this->never() )
1921 ->method( 'delete' );
1923 $store = new WatchedItemStore(
1924 $this->getMockLoadBalancer( $mockDb ),
1928 // Note: This does not actually assert the job is correct
1929 $callableCallCounter = 0;
1930 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
1931 function( $callable ) use ( &$callableCallCounter ) {
1932 $callableCallCounter++
;
1933 $this->assertInternalType( 'callable', $callable );
1938 $store->resetNotificationTimestamp(
1945 $this->assertEquals( 1, $callableCallCounter );
1947 ScopedCallback
::consume( $scopedOverride );
1950 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced() {
1951 $user = $this->getMockNonAnonUserWithId( 1 );
1953 $title = $this->getMockTitle( 'SomeDbKey' );
1954 $title->expects( $this->once() )
1955 ->method( 'getNextRevisionID' )
1957 ->will( $this->returnValue( 33 ) );
1959 $mockDb = $this->getMockDb();
1960 $mockDb->expects( $this->once() )
1961 ->method( 'selectRow' )
1964 'wl_notificationtimestamp',
1967 'wl_namespace' => 0,
1968 'wl_title' => 'SomeDbKey',
1971 ->will( $this->returnValue(
1972 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1975 $mockCache = $this->getMockCache();
1976 $mockDb->expects( $this->never() )
1978 $mockDb->expects( $this->never() )
1980 $mockDb->expects( $this->never() )
1981 ->method( 'delete' );
1983 $store = new WatchedItemStore(
1984 $this->getMockLoadBalancer( $mockDb ),
1988 // Note: This does not actually assert the job is correct
1989 $addUpdateCallCounter = 0;
1990 $scopedOverrideDeferred = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
1991 function( $callable ) use ( &$addUpdateCallCounter ) {
1992 $addUpdateCallCounter++
;
1993 $this->assertInternalType( 'callable', $callable );
1997 $getTimestampCallCounter = 0;
1998 $scopedOverrideRevision = $store->overrideRevisionGetTimestampFromIdCallback(
1999 function( $titleParam, $oldidParam ) use ( &$getTimestampCallCounter, $title, $oldid ) {
2000 $getTimestampCallCounter++
;
2001 $this->assertEquals( $title, $titleParam );
2002 $this->assertEquals( $oldid, $oldidParam );
2007 $store->resetNotificationTimestamp(
2014 $this->assertEquals( 1, $addUpdateCallCounter );
2015 $this->assertEquals( 1, $getTimestampCallCounter );
2017 ScopedCallback
::consume( $scopedOverrideDeferred );
2018 ScopedCallback
::consume( $scopedOverrideRevision );
2021 public function testUpdateNotificationTimestamp_watchersExist() {
2022 $mockDb = $this->getMockDb();
2023 $mockDb->expects( $this->once() )
2024 ->method( 'select' )
2030 'wl_namespace' => 0,
2031 'wl_title' => 'SomeDbKey',
2032 'wl_notificationtimestamp IS NULL'
2036 $this->returnValue( [
2037 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2038 $this->getFakeRow( [ 'wl_user' => '3' ] )
2041 $mockDb->expects( $this->once() )
2042 ->method( 'onTransactionIdle' )
2043 ->with( $this->isType( 'callable' ) )
2044 ->will( $this->returnCallback( function( $callable ) {
2047 $mockDb->expects( $this->once() )
2048 ->method( 'update' )
2051 [ 'wl_notificationtimestamp' => null ],
2053 'wl_user' => [ 2, 3 ],
2054 'wl_namespace' => 0,
2055 'wl_title' => 'SomeDbKey',
2059 $mockCache = $this->getMockCache();
2060 $mockCache->expects( $this->never() )->method( 'set' );
2061 $mockCache->expects( $this->never() )->method( 'get' );
2062 $mockCache->expects( $this->never() )->method( 'delete' );
2064 $store = new WatchedItemStore(
2065 $this->getMockLoadBalancer( $mockDb ),
2069 $this->assertEquals(
2071 $store->updateNotificationTimestamp(
2072 $this->getMockNonAnonUserWithId( 1 ),
2073 new TitleValue( 0, 'SomeDbKey' ),
2079 public function testUpdateNotificationTimestamp_noWatchers() {
2080 $mockDb = $this->getMockDb();
2081 $mockDb->expects( $this->once() )
2082 ->method( 'select' )
2088 'wl_namespace' => 0,
2089 'wl_title' => 'SomeDbKey',
2090 'wl_notificationtimestamp IS NULL'
2094 $this->returnValue( [] )
2096 $mockDb->expects( $this->never() )
2097 ->method( 'onTransactionIdle' );
2098 $mockDb->expects( $this->never() )
2099 ->method( 'update' );
2101 $mockCache = $this->getMockCache();
2102 $mockCache->expects( $this->never() )->method( 'set' );
2103 $mockCache->expects( $this->never() )->method( 'get' );
2104 $mockCache->expects( $this->never() )->method( 'delete' );
2106 $store = new WatchedItemStore(
2107 $this->getMockLoadBalancer( $mockDb ),
2111 $watchers = $store->updateNotificationTimestamp(
2112 $this->getMockNonAnonUserWithId( 1 ),
2113 new TitleValue( 0, 'SomeDbKey' ),
2116 $this->assertInternalType( 'array', $watchers );
2117 $this->assertEmpty( $watchers );
2120 public function testUpdateNotificationTimestamp_clearsCachedItems() {
2121 $user = $this->getMockNonAnonUserWithId( 1 );
2122 $titleValue = new TitleValue( 0, 'SomeDbKey' );
2124 $mockDb = $this->getMockDb();
2125 $mockDb->expects( $this->once() )
2126 ->method( 'selectRow' )
2127 ->will( $this->returnValue(
2128 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2130 $mockDb->expects( $this->once() )
2131 ->method( 'select' )
2133 $this->returnValue( [
2134 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2135 $this->getFakeRow( [ 'wl_user' => '3' ] )
2138 $mockDb->expects( $this->once() )
2139 ->method( 'onTransactionIdle' )
2140 ->with( $this->isType( 'callable' ) )
2141 ->will( $this->returnCallback( function( $callable ) {
2144 $mockDb->expects( $this->once() )
2145 ->method( 'update' );
2147 $mockCache = $this->getMockCache();
2148 $mockCache->expects( $this->once() )
2150 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2151 $mockCache->expects( $this->once() )
2153 ->with( '0:SomeDbKey:1' );
2154 $mockCache->expects( $this->once() )
2155 ->method( 'delete' )
2156 ->with( '0:SomeDbKey:1' );
2158 $store = new WatchedItemStore(
2159 $this->getMockLoadBalancer( $mockDb ),
2163 // This will add the item to the cache
2164 $store->getWatchedItem( $user, $titleValue );
2166 $store->updateNotificationTimestamp(
2167 $this->getMockNonAnonUserWithId( 1 ),