Merge "Use WIS::getWatchedItemsForUser in SpecialEditWatchlist"
[lhc/web/wiklou.git] / tests / phpunit / includes / WatchedItemStoreUnitTest.php
1 <?php
2
3 /**
4 * @author Addshore
5 *
6 * @covers WatchedItemStore
7 */
8 class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase {
9
10 /**
11 * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
12 */
13 private function getMockDb() {
14 return $this->getMock( IDatabase::class );
15 }
16
17 /**
18 * @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
19 */
20 private function getMockLoadBalancer( $mockDb, $expectedConnectionType = null ) {
21 $mock = $this->getMockBuilder( LoadBalancer::class )
22 ->disableOriginalConstructor()
23 ->getMock();
24 if ( $expectedConnectionType !== null ) {
25 $mock->expects( $this->any() )
26 ->method( 'getConnection' )
27 ->with( $expectedConnectionType )
28 ->will( $this->returnValue( $mockDb ) );
29 } else {
30 $mock->expects( $this->any() )
31 ->method( 'getConnection' )
32 ->will( $this->returnValue( $mockDb ) );
33 }
34 $mock->expects( $this->any() )
35 ->method( 'getReadOnlyReason' )
36 ->will( $this->returnValue( false ) );
37 return $mock;
38 }
39
40 /**
41 * @return PHPUnit_Framework_MockObject_MockObject|HashBagOStuff
42 */
43 private function getMockCache() {
44 $mock = $this->getMockBuilder( HashBagOStuff::class )
45 ->disableOriginalConstructor()
46 ->getMock();
47 $mock->expects( $this->any() )
48 ->method( 'makeKey' )
49 ->will( $this->returnCallback( function() {
50 return implode( ':', func_get_args() );
51 } ) );
52 return $mock;
53 }
54
55 /**
56 * @param int $id
57 * @return PHPUnit_Framework_MockObject_MockObject|User
58 */
59 private function getMockNonAnonUserWithId( $id ) {
60 $mock = $this->getMock( User::class );
61 $mock->expects( $this->any() )
62 ->method( 'isAnon' )
63 ->will( $this->returnValue( false ) );
64 $mock->expects( $this->any() )
65 ->method( 'getId' )
66 ->will( $this->returnValue( $id ) );
67 return $mock;
68 }
69
70 /**
71 * @return User
72 */
73 private function getAnonUser() {
74 return User::newFromName( 'Anon_User' );
75 }
76
77 private function getFakeRow( array $rowValues ) {
78 $fakeRow = new stdClass();
79 foreach ( $rowValues as $valueName => $value ) {
80 $fakeRow->$valueName = $value;
81 }
82 return $fakeRow;
83 }
84
85 public function testGetDefaultInstance() {
86 $instanceOne = WatchedItemStore::getDefaultInstance();
87 $instanceTwo = WatchedItemStore::getDefaultInstance();
88 $this->assertSame( $instanceOne, $instanceTwo );
89 }
90
91 public function testOverrideDefaultInstance() {
92 $instance = WatchedItemStore::getDefaultInstance();
93 $scopedOverride = $instance->overrideDefaultInstance( null );
94
95 $this->assertNotSame( $instance, WatchedItemStore::getDefaultInstance() );
96
97 unset( $scopedOverride );
98
99 $this->assertSame( $instance, WatchedItemStore::getDefaultInstance() );
100 }
101
102 public function testCountWatchedItems() {
103 $user = $this->getMockNonAnonUserWithId( 1 );
104
105 $mockDb = $this->getMockDb();
106 $mockDb->expects( $this->exactly( 1 ) )
107 ->method( 'selectField' )
108 ->with(
109 'watchlist',
110 'COUNT(*)',
111 [
112 'wl_user' => $user->getId(),
113 ],
114 $this->isType( 'string' )
115 )
116 ->will( $this->returnValue( 12 ) );
117
118 $mockCache = $this->getMockCache();
119 $mockCache->expects( $this->never() )->method( 'get' );
120 $mockCache->expects( $this->never() )->method( 'set' );
121 $mockCache->expects( $this->never() )->method( 'delete' );
122
123 $store = new WatchedItemStore(
124 $this->getMockLoadBalancer( $mockDb ),
125 $mockCache
126 );
127
128 $this->assertEquals( 12, $store->countWatchedItems( $user ) );
129 }
130
131 public function testCountWatchers() {
132 $titleValue = new TitleValue( 0, 'SomeDbKey' );
133
134 $mockDb = $this->getMockDb();
135 $mockDb->expects( $this->exactly( 1 ) )
136 ->method( 'selectField' )
137 ->with(
138 'watchlist',
139 'COUNT(*)',
140 [
141 'wl_namespace' => $titleValue->getNamespace(),
142 'wl_title' => $titleValue->getDBkey(),
143 ],
144 $this->isType( 'string' )
145 )
146 ->will( $this->returnValue( 7 ) );
147
148 $mockCache = $this->getMockCache();
149 $mockCache->expects( $this->never() )->method( 'get' );
150 $mockCache->expects( $this->never() )->method( 'set' );
151 $mockCache->expects( $this->never() )->method( 'delete' );
152
153 $store = new WatchedItemStore(
154 $this->getMockLoadBalancer( $mockDb ),
155 $mockCache
156 );
157
158 $this->assertEquals( 7, $store->countWatchers( $titleValue ) );
159 }
160
161 public function testCountWatchersMultiple() {
162 $titleValues = [
163 new TitleValue( 0, 'SomeDbKey' ),
164 new TitleValue( 0, 'OtherDbKey' ),
165 new TitleValue( 1, 'AnotherDbKey' ),
166 ];
167
168 $mockDb = $this->getMockDb();
169
170 $dbResult = [
171 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
172 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
173 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
174 ),
175 ];
176 $mockDb->expects( $this->once() )
177 ->method( 'makeWhereFrom2d' )
178 ->with(
179 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
180 $this->isType( 'string' ),
181 $this->isType( 'string' )
182 )
183 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
184 $mockDb->expects( $this->once() )
185 ->method( 'select' )
186 ->with(
187 'watchlist',
188 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
189 [ 'makeWhereFrom2d return value' ],
190 $this->isType( 'string' ),
191 [
192 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
193 ]
194 )
195 ->will(
196 $this->returnValue( $dbResult )
197 );
198
199 $mockCache = $this->getMockCache();
200 $mockCache->expects( $this->never() )->method( 'get' );
201 $mockCache->expects( $this->never() )->method( 'set' );
202 $mockCache->expects( $this->never() )->method( 'delete' );
203
204 $store = new WatchedItemStore(
205 $this->getMockLoadBalancer( $mockDb ),
206 $mockCache
207 );
208
209 $expected = [
210 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
211 1 => [ 'AnotherDbKey' => 500 ],
212 ];
213 $this->assertEquals( $expected, $store->countWatchersMultiple( $titleValues ) );
214 }
215
216 public function provideIntWithDbUnsafeVersion() {
217 return [
218 [ 50 ],
219 [ "50; DROP TABLE watchlist;\n--" ],
220 ];
221 }
222
223 /**
224 * @dataProvider provideIntWithDbUnsafeVersion
225 */
226 public function testCountWatchersMultiple_withMinimumWatchers( $minWatchers ) {
227 $titleValues = [
228 new TitleValue( 0, 'SomeDbKey' ),
229 new TitleValue( 0, 'OtherDbKey' ),
230 new TitleValue( 1, 'AnotherDbKey' ),
231 ];
232
233 $mockDb = $this->getMockDb();
234
235 $dbResult = [
236 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
237 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
238 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
239 ),
240 ];
241 $mockDb->expects( $this->once() )
242 ->method( 'makeWhereFrom2d' )
243 ->with(
244 [ [ 'SomeDbKey' => 1, 'OtherDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
245 $this->isType( 'string' ),
246 $this->isType( 'string' )
247 )
248 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
249 $mockDb->expects( $this->once() )
250 ->method( 'select' )
251 ->with(
252 'watchlist',
253 [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ],
254 [ 'makeWhereFrom2d return value' ],
255 $this->isType( 'string' ),
256 [
257 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
258 'HAVING' => 'COUNT(*) >= 50',
259 ]
260 )
261 ->will(
262 $this->returnValue( $dbResult )
263 );
264
265 $mockCache = $this->getMockCache();
266 $mockCache->expects( $this->never() )->method( 'get' );
267 $mockCache->expects( $this->never() )->method( 'set' );
268 $mockCache->expects( $this->never() )->method( 'delete' );
269
270 $store = new WatchedItemStore(
271 $this->getMockLoadBalancer( $mockDb ),
272 $mockCache
273 );
274
275 $expected = [
276 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
277 1 => [ 'AnotherDbKey' => 500 ],
278 ];
279 $this->assertEquals(
280 $expected,
281 $store->countWatchersMultiple( $titleValues, [ 'minimumWatchers' => $minWatchers ] )
282 );
283 }
284
285 public function testCountVisitingWatchers() {
286 $titleValue = new TitleValue( 0, 'SomeDbKey' );
287
288 $mockDb = $this->getMockDb();
289 $mockDb->expects( $this->exactly( 1 ) )
290 ->method( 'selectField' )
291 ->with(
292 'watchlist',
293 'COUNT(*)',
294 [
295 'wl_namespace' => $titleValue->getNamespace(),
296 'wl_title' => $titleValue->getDBkey(),
297 'wl_notificationtimestamp >= \'TS111TS\' OR wl_notificationtimestamp IS NULL',
298 ],
299 $this->isType( 'string' )
300 )
301 ->will( $this->returnValue( 7 ) );
302 $mockDb->expects( $this->exactly( 1 ) )
303 ->method( 'addQuotes' )
304 ->will( $this->returnCallback( function( $value ) {
305 return "'$value'";
306 } ) );
307 $mockDb->expects( $this->exactly( 1 ) )
308 ->method( 'timestamp' )
309 ->will( $this->returnCallback( function( $value ) {
310 return 'TS' . $value . 'TS';
311 } ) );
312
313 $mockCache = $this->getMockCache();
314 $mockCache->expects( $this->never() )->method( 'set' );
315 $mockCache->expects( $this->never() )->method( 'get' );
316 $mockCache->expects( $this->never() )->method( 'delete' );
317
318 $store = new WatchedItemStore(
319 $this->getMockLoadBalancer( $mockDb ),
320 $mockCache
321 );
322
323 $this->assertEquals( 7, $store->countVisitingWatchers( $titleValue, '111' ) );
324 }
325
326 public function testCountVisitingWatchersMultiple() {
327 $titleValuesWithThresholds = [
328 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
329 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
330 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
331 ];
332
333 $dbResult = [
334 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
335 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
336 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
337 ];
338 $mockDb = $this->getMockDb();
339 $mockDb->expects( $this->exactly( 2 * 3 ) )
340 ->method( 'addQuotes' )
341 ->will( $this->returnCallback( function( $value ) {
342 return "'$value'";
343 } ) );
344 $mockDb->expects( $this->exactly( 3 ) )
345 ->method( 'timestamp' )
346 ->will( $this->returnCallback( function( $value ) {
347 return 'TS' . $value . 'TS';
348 } ) );
349 $mockDb->expects( $this->any() )
350 ->method( 'makeList' )
351 ->with(
352 $this->isType( 'array' ),
353 $this->isType( 'int' )
354 )
355 ->will( $this->returnCallback( function( $a, $conj ) {
356 $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
357 return join( $sqlConj, array_map( function( $s ) {
358 return '(' . $s . ')';
359 }, $a
360 ) );
361 } ) );
362 $mockDb->expects( $this->never() )
363 ->method( 'makeWhereFrom2d' );
364
365 $expectedCond =
366 '((wl_namespace = 0) AND (' .
367 "(((wl_title = 'SomeDbKey') AND (" .
368 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
369 ')) OR (' .
370 "(wl_title = 'OtherDbKey') AND (" .
371 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
372 '))))' .
373 ') OR ((wl_namespace = 1) AND (' .
374 "(((wl_title = 'AnotherDbKey') AND (".
375 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
376 ')))))';
377 $mockDb->expects( $this->once() )
378 ->method( 'select' )
379 ->with(
380 'watchlist',
381 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
382 $expectedCond,
383 $this->isType( 'string' ),
384 [
385 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
386 ]
387 )
388 ->will(
389 $this->returnValue( $dbResult )
390 );
391
392 $mockCache = $this->getMockCache();
393 $mockCache->expects( $this->never() )->method( 'get' );
394 $mockCache->expects( $this->never() )->method( 'set' );
395 $mockCache->expects( $this->never() )->method( 'delete' );
396
397 $store = new WatchedItemStore(
398 $this->getMockLoadBalancer( $mockDb ),
399 $mockCache
400 );
401
402 $expected = [
403 0 => [ 'SomeDbKey' => 100, 'OtherDbKey' => 300 ],
404 1 => [ 'AnotherDbKey' => 500 ],
405 ];
406 $this->assertEquals(
407 $expected,
408 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
409 );
410 }
411
412 public function testCountVisitingWatchersMultiple_withMissingTargets() {
413 $titleValuesWithThresholds = [
414 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
415 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
416 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
417 [ new TitleValue( 0, 'SomeNotExisitingDbKey' ), null ],
418 [ new TitleValue( 0, 'OtherNotExisitingDbKey' ), null ],
419 ];
420
421 $dbResult = [
422 $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
423 $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
424 $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
425 $this->getFakeRow(
426 [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 100 ]
427 ),
428 $this->getFakeRow(
429 [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 200 ]
430 ),
431 ];
432 $mockDb = $this->getMockDb();
433 $mockDb->expects( $this->exactly( 2 * 3 ) )
434 ->method( 'addQuotes' )
435 ->will( $this->returnCallback( function( $value ) {
436 return "'$value'";
437 } ) );
438 $mockDb->expects( $this->exactly( 3 ) )
439 ->method( 'timestamp' )
440 ->will( $this->returnCallback( function( $value ) {
441 return 'TS' . $value . 'TS';
442 } ) );
443 $mockDb->expects( $this->any() )
444 ->method( 'makeList' )
445 ->with(
446 $this->isType( 'array' ),
447 $this->isType( 'int' )
448 )
449 ->will( $this->returnCallback( function( $a, $conj ) {
450 $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
451 return join( $sqlConj, array_map( function( $s ) {
452 return '(' . $s . ')';
453 }, $a
454 ) );
455 } ) );
456 $mockDb->expects( $this->once() )
457 ->method( 'makeWhereFrom2d' )
458 ->with(
459 [ [ 'SomeNotExisitingDbKey' => 1, 'OtherNotExisitingDbKey' => 1 ] ],
460 $this->isType( 'string' ),
461 $this->isType( 'string' )
462 )
463 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
464
465 $expectedCond =
466 '((wl_namespace = 0) AND (' .
467 "(((wl_title = 'SomeDbKey') AND (" .
468 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
469 ')) OR (' .
470 "(wl_title = 'OtherDbKey') AND (" .
471 "(wl_notificationtimestamp >= 'TS111TS') OR (wl_notificationtimestamp IS NULL)" .
472 '))))' .
473 ') OR ((wl_namespace = 1) AND (' .
474 "(((wl_title = 'AnotherDbKey') AND (".
475 "(wl_notificationtimestamp >= 'TS123TS') OR (wl_notificationtimestamp IS NULL)" .
476 '))))' .
477 ') OR ' .
478 '(makeWhereFrom2d return value)';
479 $mockDb->expects( $this->once() )
480 ->method( 'select' )
481 ->with(
482 'watchlist',
483 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
484 $expectedCond,
485 $this->isType( 'string' ),
486 [
487 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
488 ]
489 )
490 ->will(
491 $this->returnValue( $dbResult )
492 );
493
494 $mockCache = $this->getMockCache();
495 $mockCache->expects( $this->never() )->method( 'get' );
496 $mockCache->expects( $this->never() )->method( 'set' );
497 $mockCache->expects( $this->never() )->method( 'delete' );
498
499 $store = new WatchedItemStore(
500 $this->getMockLoadBalancer( $mockDb ),
501 $mockCache
502 );
503
504 $expected = [
505 0 => [
506 'SomeDbKey' => 100, 'OtherDbKey' => 300,
507 'SomeNotExisitingDbKey' => 100, 'OtherNotExisitingDbKey' => 200
508 ],
509 1 => [ 'AnotherDbKey' => 500 ],
510 ];
511 $this->assertEquals(
512 $expected,
513 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds )
514 );
515 }
516
517 /**
518 * @dataProvider provideIntWithDbUnsafeVersion
519 */
520 public function testCountVisitingWatchersMultiple_withMinimumWatchers( $minWatchers ) {
521 $titleValuesWithThresholds = [
522 [ new TitleValue( 0, 'SomeDbKey' ), '111' ],
523 [ new TitleValue( 0, 'OtherDbKey' ), '111' ],
524 [ new TitleValue( 1, 'AnotherDbKey' ), '123' ],
525 ];
526
527 $mockDb = $this->getMockDb();
528 $mockDb->expects( $this->any() )
529 ->method( 'makeList' )
530 ->will( $this->returnValue( 'makeList return value' ) );
531 $mockDb->expects( $this->once() )
532 ->method( 'select' )
533 ->with(
534 'watchlist',
535 [ 'wl_namespace', 'wl_title', 'watchers' => 'COUNT(*)' ],
536 'makeList return value',
537 $this->isType( 'string' ),
538 [
539 'GROUP BY' => [ 'wl_namespace', 'wl_title' ],
540 'HAVING' => 'COUNT(*) >= 50',
541 ]
542 )
543 ->will(
544 $this->returnValue( [] )
545 );
546
547 $mockCache = $this->getMockCache();
548 $mockCache->expects( $this->never() )->method( 'get' );
549 $mockCache->expects( $this->never() )->method( 'set' );
550 $mockCache->expects( $this->never() )->method( 'delete' );
551
552 $store = new WatchedItemStore(
553 $this->getMockLoadBalancer( $mockDb ),
554 $mockCache
555 );
556
557 $expected = [
558 0 => [ 'SomeDbKey' => 0, 'OtherDbKey' => 0 ],
559 1 => [ 'AnotherDbKey' => 0 ],
560 ];
561 $this->assertEquals(
562 $expected,
563 $store->countVisitingWatchersMultiple( $titleValuesWithThresholds, $minWatchers )
564 );
565 }
566
567 public function testCountUnreadNotifications() {
568 $user = $this->getMockNonAnonUserWithId( 1 );
569
570 $mockDb = $this->getMockDb();
571 $mockDb->expects( $this->exactly( 1 ) )
572 ->method( 'selectRowCount' )
573 ->with(
574 'watchlist',
575 '1',
576 [
577 "wl_notificationtimestamp IS NOT NULL",
578 'wl_user' => 1,
579 ],
580 $this->isType( 'string' )
581 )
582 ->will( $this->returnValue( 9 ) );
583
584 $mockCache = $this->getMockCache();
585 $mockCache->expects( $this->never() )->method( 'set' );
586 $mockCache->expects( $this->never() )->method( 'get' );
587 $mockCache->expects( $this->never() )->method( 'delete' );
588
589 $store = new WatchedItemStore(
590 $this->getMockLoadBalancer( $mockDb ),
591 $mockCache
592 );
593
594 $this->assertEquals( 9, $store->countUnreadNotifications( $user ) );
595 }
596
597 /**
598 * @dataProvider provideIntWithDbUnsafeVersion
599 */
600 public function testCountUnreadNotifications_withUnreadLimit_overLimit( $limit ) {
601 $user = $this->getMockNonAnonUserWithId( 1 );
602
603 $mockDb = $this->getMockDb();
604 $mockDb->expects( $this->exactly( 1 ) )
605 ->method( 'selectRowCount' )
606 ->with(
607 'watchlist',
608 '1',
609 [
610 "wl_notificationtimestamp IS NOT NULL",
611 'wl_user' => 1,
612 ],
613 $this->isType( 'string' ),
614 [ 'LIMIT' => 50 ]
615 )
616 ->will( $this->returnValue( 50 ) );
617
618 $mockCache = $this->getMockCache();
619 $mockCache->expects( $this->never() )->method( 'set' );
620 $mockCache->expects( $this->never() )->method( 'get' );
621 $mockCache->expects( $this->never() )->method( 'delete' );
622
623 $store = new WatchedItemStore(
624 $this->getMockLoadBalancer( $mockDb ),
625 $mockCache
626 );
627
628 $this->assertSame(
629 true,
630 $store->countUnreadNotifications( $user, $limit )
631 );
632 }
633
634 /**
635 * @dataProvider provideIntWithDbUnsafeVersion
636 */
637 public function testCountUnreadNotifications_withUnreadLimit_underLimit( $limit ) {
638 $user = $this->getMockNonAnonUserWithId( 1 );
639
640 $mockDb = $this->getMockDb();
641 $mockDb->expects( $this->exactly( 1 ) )
642 ->method( 'selectRowCount' )
643 ->with(
644 'watchlist',
645 '1',
646 [
647 "wl_notificationtimestamp IS NOT NULL",
648 'wl_user' => 1,
649 ],
650 $this->isType( 'string' ),
651 [ 'LIMIT' => 50 ]
652 )
653 ->will( $this->returnValue( 9 ) );
654
655 $mockCache = $this->getMockCache();
656 $mockCache->expects( $this->never() )->method( 'set' );
657 $mockCache->expects( $this->never() )->method( 'get' );
658 $mockCache->expects( $this->never() )->method( 'delete' );
659
660 $store = new WatchedItemStore(
661 $this->getMockLoadBalancer( $mockDb ),
662 $mockCache
663 );
664
665 $this->assertEquals(
666 9,
667 $store->countUnreadNotifications( $user, $limit )
668 );
669 }
670
671 public function testDuplicateEntry_nothingToDuplicate() {
672 $mockDb = $this->getMockDb();
673 $mockDb->expects( $this->once() )
674 ->method( 'select' )
675 ->with(
676 'watchlist',
677 [
678 'wl_user',
679 'wl_notificationtimestamp',
680 ],
681 [
682 'wl_namespace' => 0,
683 'wl_title' => 'Old_Title',
684 ],
685 'WatchedItemStore::duplicateEntry',
686 [ 'FOR UPDATE' ]
687 )
688 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
689
690 $store = new WatchedItemStore(
691 $this->getMockLoadBalancer( $mockDb ),
692 $this->getMockCache()
693 );
694
695 $store->duplicateEntry(
696 Title::newFromText( 'Old_Title' ),
697 Title::newFromText( 'New_Title' )
698 );
699 }
700
701 public function testDuplicateEntry_somethingToDuplicate() {
702 $fakeRows = [
703 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
704 $this->getFakeRow( [ 'wl_user' => 2, 'wl_notificationtimestamp' => null ] ),
705 ];
706
707 $mockDb = $this->getMockDb();
708 $mockDb->expects( $this->at( 0 ) )
709 ->method( 'select' )
710 ->with(
711 'watchlist',
712 [
713 'wl_user',
714 'wl_notificationtimestamp',
715 ],
716 [
717 'wl_namespace' => 0,
718 'wl_title' => 'Old_Title',
719 ]
720 )
721 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
722 $mockDb->expects( $this->at( 1 ) )
723 ->method( 'replace' )
724 ->with(
725 'watchlist',
726 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
727 [
728 [
729 'wl_user' => 1,
730 'wl_namespace' => 0,
731 'wl_title' => 'New_Title',
732 'wl_notificationtimestamp' => '20151212010101',
733 ],
734 [
735 'wl_user' => 2,
736 'wl_namespace' => 0,
737 'wl_title' => 'New_Title',
738 'wl_notificationtimestamp' => null,
739 ],
740 ],
741 $this->isType( 'string' )
742 );
743
744 $mockCache = $this->getMockCache();
745 $mockCache->expects( $this->never() )->method( 'get' );
746 $mockCache->expects( $this->never() )->method( 'delete' );
747
748 $store = new WatchedItemStore(
749 $this->getMockLoadBalancer( $mockDb ),
750 $mockCache
751 );
752
753 $store->duplicateEntry(
754 Title::newFromText( 'Old_Title' ),
755 Title::newFromText( 'New_Title' )
756 );
757 }
758
759 public function testDuplicateAllAssociatedEntries_nothingToDuplicate() {
760 $mockDb = $this->getMockDb();
761 $mockDb->expects( $this->at( 0 ) )
762 ->method( 'select' )
763 ->with(
764 'watchlist',
765 [
766 'wl_user',
767 'wl_notificationtimestamp',
768 ],
769 [
770 'wl_namespace' => 0,
771 'wl_title' => 'Old_Title',
772 ]
773 )
774 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
775 $mockDb->expects( $this->at( 1 ) )
776 ->method( 'select' )
777 ->with(
778 'watchlist',
779 [
780 'wl_user',
781 'wl_notificationtimestamp',
782 ],
783 [
784 'wl_namespace' => 1,
785 'wl_title' => 'Old_Title',
786 ]
787 )
788 ->will( $this->returnValue( new FakeResultWrapper( [] ) ) );
789
790 $mockCache = $this->getMockCache();
791 $mockCache->expects( $this->never() )->method( 'get' );
792 $mockCache->expects( $this->never() )->method( 'delete' );
793
794 $store = new WatchedItemStore(
795 $this->getMockLoadBalancer( $mockDb ),
796 $mockCache
797 );
798
799 $store->duplicateAllAssociatedEntries(
800 Title::newFromText( 'Old_Title' ),
801 Title::newFromText( 'New_Title' )
802 );
803 }
804
805 public function testDuplicateAllAssociatedEntries_somethingToDuplicate() {
806 $fakeRows = [
807 $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
808 ];
809
810 $mockDb = $this->getMockDb();
811 $mockDb->expects( $this->at( 0 ) )
812 ->method( 'select' )
813 ->with(
814 'watchlist',
815 [
816 'wl_user',
817 'wl_notificationtimestamp',
818 ],
819 [
820 'wl_namespace' => 0,
821 'wl_title' => 'Old_Title',
822 ]
823 )
824 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
825 $mockDb->expects( $this->at( 1 ) )
826 ->method( 'replace' )
827 ->with(
828 'watchlist',
829 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
830 [
831 [
832 'wl_user' => 1,
833 'wl_namespace' => 0,
834 'wl_title' => 'New_Title',
835 'wl_notificationtimestamp' => '20151212010101',
836 ],
837 ],
838 $this->isType( 'string' )
839 );
840 $mockDb->expects( $this->at( 2 ) )
841 ->method( 'select' )
842 ->with(
843 'watchlist',
844 [
845 'wl_user',
846 'wl_notificationtimestamp',
847 ],
848 [
849 'wl_namespace' => 1,
850 'wl_title' => 'Old_Title',
851 ]
852 )
853 ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
854 $mockDb->expects( $this->at( 3 ) )
855 ->method( 'replace' )
856 ->with(
857 'watchlist',
858 [ [ 'wl_user', 'wl_namespace', 'wl_title' ] ],
859 [
860 [
861 'wl_user' => 1,
862 'wl_namespace' => 1,
863 'wl_title' => 'New_Title',
864 'wl_notificationtimestamp' => '20151212010101',
865 ],
866 ],
867 $this->isType( 'string' )
868 );
869
870 $mockCache = $this->getMockCache();
871 $mockCache->expects( $this->never() )->method( 'get' );
872 $mockCache->expects( $this->never() )->method( 'delete' );
873
874 $store = new WatchedItemStore(
875 $this->getMockLoadBalancer( $mockDb ),
876 $mockCache
877 );
878
879 $store->duplicateAllAssociatedEntries(
880 Title::newFromText( 'Old_Title' ),
881 Title::newFromText( 'New_Title' )
882 );
883 }
884
885 public function testAddWatch_nonAnonymousUser() {
886 $mockDb = $this->getMockDb();
887 $mockDb->expects( $this->once() )
888 ->method( 'insert' )
889 ->with(
890 'watchlist',
891 [
892 [
893 'wl_user' => 1,
894 'wl_namespace' => 0,
895 'wl_title' => 'Some_Page',
896 'wl_notificationtimestamp' => null,
897 ]
898 ]
899 );
900
901 $mockCache = $this->getMockCache();
902 $mockCache->expects( $this->once() )
903 ->method( 'delete' )
904 ->with( '0:Some_Page:1' );
905
906 $store = new WatchedItemStore(
907 $this->getMockLoadBalancer( $mockDb ),
908 $mockCache
909 );
910
911 $store->addWatch(
912 $this->getMockNonAnonUserWithId( 1 ),
913 Title::newFromText( 'Some_Page' )
914 );
915 }
916
917 public function testAddWatch_anonymousUser() {
918 $mockDb = $this->getMockDb();
919 $mockDb->expects( $this->never() )
920 ->method( 'insert' );
921
922 $mockCache = $this->getMockCache();
923 $mockCache->expects( $this->never() )
924 ->method( 'delete' );
925
926 $store = new WatchedItemStore(
927 $this->getMockLoadBalancer( $mockDb ),
928 $mockCache
929 );
930
931 $store->addWatch(
932 $this->getAnonUser(),
933 Title::newFromText( 'Some_Page' )
934 );
935 }
936
937 public function testAddWatchBatch_nonAnonymousUser() {
938 $mockDb = $this->getMockDb();
939 $mockDb->expects( $this->once() )
940 ->method( 'insert' )
941 ->with(
942 'watchlist',
943 [
944 [
945 'wl_user' => 1,
946 'wl_namespace' => 0,
947 'wl_title' => 'Some_Page',
948 'wl_notificationtimestamp' => null,
949 ],
950 [
951 'wl_user' => 1,
952 'wl_namespace' => 1,
953 'wl_title' => 'Some_Page',
954 'wl_notificationtimestamp' => null,
955 ]
956 ]
957 );
958
959 $mockCache = $this->getMockCache();
960 $mockCache->expects( $this->exactly( 2 ) )
961 ->method( 'delete' );
962 $mockCache->expects( $this->at( 1 ) )
963 ->method( 'delete' )
964 ->with( '0:Some_Page:1' );
965 $mockCache->expects( $this->at( 3 ) )
966 ->method( 'delete' )
967 ->with( '1:Some_Page:1' );
968
969 $store = new WatchedItemStore(
970 $this->getMockLoadBalancer( $mockDb ),
971 $mockCache
972 );
973
974 $mockUser = $this->getMockNonAnonUserWithId( 1 );
975
976 $this->assertTrue(
977 $store->addWatchBatch(
978 [
979 [ $mockUser, new TitleValue( 0, 'Some_Page' ) ],
980 [ $mockUser, new TitleValue( 1, 'Some_Page' ) ],
981 ]
982 )
983 );
984 }
985
986 public function testAddWatchBatch_anonymousUserCombinationsAreSkipped() {
987 $mockDb = $this->getMockDb();
988 $mockDb->expects( $this->once() )
989 ->method( 'insert' )
990 ->with(
991 'watchlist',
992 [
993 [
994 'wl_user' => 1,
995 'wl_namespace' => 0,
996 'wl_title' => 'Some_Page',
997 'wl_notificationtimestamp' => null,
998 ]
999 ]
1000 );
1001
1002 $mockCache = $this->getMockCache();
1003 $mockCache->expects( $this->once() )
1004 ->method( 'delete' )
1005 ->with( '0:Some_Page:1' );
1006
1007 $store = new WatchedItemStore(
1008 $this->getMockLoadBalancer( $mockDb ),
1009 $mockCache
1010 );
1011
1012 $this->assertTrue(
1013 $store->addWatchBatch(
1014 [
1015 [ $this->getMockNonAnonUserWithId( 1 ), new TitleValue( 0, 'Some_Page' ) ],
1016 [ $this->getAnonUser(), new TitleValue( 0, 'Other_Page' ) ],
1017 ]
1018 )
1019 );
1020 }
1021
1022 public function testAddWatchBatchReturnsFalse_whenOnlyGivenAnonymousUserCombinations() {
1023 $mockDb = $this->getMockDb();
1024 $mockDb->expects( $this->never() )
1025 ->method( 'insert' );
1026
1027 $mockCache = $this->getMockCache();
1028 $mockCache->expects( $this->never() )
1029 ->method( 'delete' );
1030
1031 $store = new WatchedItemStore(
1032 $this->getMockLoadBalancer( $mockDb ),
1033 $mockCache
1034 );
1035
1036 $anonUser = $this->getAnonUser();
1037 $this->assertFalse(
1038 $store->addWatchBatch(
1039 [
1040 [ $anonUser, new TitleValue( 0, 'Some_Page' ) ],
1041 [ $anonUser, new TitleValue( 1, 'Other_Page' ) ],
1042 ]
1043 )
1044 );
1045 }
1046
1047 public function testAddWatchBatchReturnsFalse_whenGivenEmptyList() {
1048 $mockDb = $this->getMockDb();
1049 $mockDb->expects( $this->never() )
1050 ->method( 'insert' );
1051
1052 $mockCache = $this->getMockCache();
1053 $mockCache->expects( $this->never() )
1054 ->method( 'delete' );
1055
1056 $store = new WatchedItemStore(
1057 $this->getMockLoadBalancer( $mockDb ),
1058 $mockCache
1059 );
1060
1061 $this->assertFalse(
1062 $store->addWatchBatch( [] )
1063 );
1064 }
1065
1066 public function testLoadWatchedItem_existingItem() {
1067 $mockDb = $this->getMockDb();
1068 $mockDb->expects( $this->once() )
1069 ->method( 'selectRow' )
1070 ->with(
1071 'watchlist',
1072 'wl_notificationtimestamp',
1073 [
1074 'wl_user' => 1,
1075 'wl_namespace' => 0,
1076 'wl_title' => 'SomeDbKey',
1077 ]
1078 )
1079 ->will( $this->returnValue(
1080 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1081 ) );
1082
1083 $mockCache = $this->getMockCache();
1084 $mockCache->expects( $this->once() )
1085 ->method( 'set' )
1086 ->with(
1087 '0:SomeDbKey:1'
1088 );
1089
1090 $store = new WatchedItemStore(
1091 $this->getMockLoadBalancer( $mockDb ),
1092 $mockCache
1093 );
1094
1095 $watchedItem = $store->loadWatchedItem(
1096 $this->getMockNonAnonUserWithId( 1 ),
1097 new TitleValue( 0, 'SomeDbKey' )
1098 );
1099 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1100 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1101 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1102 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1103 }
1104
1105 public function testLoadWatchedItem_noItem() {
1106 $mockDb = $this->getMockDb();
1107 $mockDb->expects( $this->once() )
1108 ->method( 'selectRow' )
1109 ->with(
1110 'watchlist',
1111 'wl_notificationtimestamp',
1112 [
1113 'wl_user' => 1,
1114 'wl_namespace' => 0,
1115 'wl_title' => 'SomeDbKey',
1116 ]
1117 )
1118 ->will( $this->returnValue( [] ) );
1119
1120 $mockCache = $this->getMockCache();
1121 $mockCache->expects( $this->never() )->method( 'get' );
1122 $mockCache->expects( $this->never() )->method( 'delete' );
1123
1124 $store = new WatchedItemStore(
1125 $this->getMockLoadBalancer( $mockDb ),
1126 $mockCache
1127 );
1128
1129 $this->assertFalse(
1130 $store->loadWatchedItem(
1131 $this->getMockNonAnonUserWithId( 1 ),
1132 new TitleValue( 0, 'SomeDbKey' )
1133 )
1134 );
1135 }
1136
1137 public function testLoadWatchedItem_anonymousUser() {
1138 $mockDb = $this->getMockDb();
1139 $mockDb->expects( $this->never() )
1140 ->method( 'selectRow' );
1141
1142 $mockCache = $this->getMockCache();
1143 $mockCache->expects( $this->never() )->method( 'get' );
1144 $mockCache->expects( $this->never() )->method( 'delete' );
1145
1146 $store = new WatchedItemStore(
1147 $this->getMockLoadBalancer( $mockDb ),
1148 $mockCache
1149 );
1150
1151 $this->assertFalse(
1152 $store->loadWatchedItem(
1153 $this->getAnonUser(),
1154 new TitleValue( 0, 'SomeDbKey' )
1155 )
1156 );
1157 }
1158
1159 public function testRemoveWatch_existingItem() {
1160 $mockDb = $this->getMockDb();
1161 $mockDb->expects( $this->once() )
1162 ->method( 'delete' )
1163 ->with(
1164 'watchlist',
1165 [
1166 'wl_user' => 1,
1167 'wl_namespace' => 0,
1168 'wl_title' => 'SomeDbKey',
1169 ]
1170 );
1171 $mockDb->expects( $this->once() )
1172 ->method( 'affectedRows' )
1173 ->will( $this->returnValue( 1 ) );
1174
1175 $mockCache = $this->getMockCache();
1176 $mockCache->expects( $this->never() )->method( 'get' );
1177 $mockCache->expects( $this->once() )
1178 ->method( 'delete' )
1179 ->with( '0:SomeDbKey:1' );
1180
1181 $store = new WatchedItemStore(
1182 $this->getMockLoadBalancer( $mockDb ),
1183 $mockCache
1184 );
1185
1186 $this->assertTrue(
1187 $store->removeWatch(
1188 $this->getMockNonAnonUserWithId( 1 ),
1189 new TitleValue( 0, 'SomeDbKey' )
1190 )
1191 );
1192 }
1193
1194 public function testRemoveWatch_noItem() {
1195 $mockDb = $this->getMockDb();
1196 $mockDb->expects( $this->once() )
1197 ->method( 'delete' )
1198 ->with(
1199 'watchlist',
1200 [
1201 'wl_user' => 1,
1202 'wl_namespace' => 0,
1203 'wl_title' => 'SomeDbKey',
1204 ]
1205 );
1206 $mockDb->expects( $this->once() )
1207 ->method( 'affectedRows' )
1208 ->will( $this->returnValue( 0 ) );
1209
1210 $mockCache = $this->getMockCache();
1211 $mockCache->expects( $this->never() )->method( 'get' );
1212 $mockCache->expects( $this->once() )
1213 ->method( 'delete' )
1214 ->with( '0:SomeDbKey:1' );
1215
1216 $store = new WatchedItemStore(
1217 $this->getMockLoadBalancer( $mockDb ),
1218 $mockCache
1219 );
1220
1221 $this->assertFalse(
1222 $store->removeWatch(
1223 $this->getMockNonAnonUserWithId( 1 ),
1224 new TitleValue( 0, 'SomeDbKey' )
1225 )
1226 );
1227 }
1228
1229 public function testRemoveWatch_anonymousUser() {
1230 $mockDb = $this->getMockDb();
1231 $mockDb->expects( $this->never() )
1232 ->method( 'delete' );
1233
1234 $mockCache = $this->getMockCache();
1235 $mockCache->expects( $this->never() )->method( 'get' );
1236 $mockCache->expects( $this->never() )
1237 ->method( 'delete' );
1238
1239 $store = new WatchedItemStore(
1240 $this->getMockLoadBalancer( $mockDb ),
1241 $mockCache
1242 );
1243
1244 $this->assertFalse(
1245 $store->removeWatch(
1246 $this->getAnonUser(),
1247 new TitleValue( 0, 'SomeDbKey' )
1248 )
1249 );
1250 }
1251
1252 public function testGetWatchedItem_existingItem() {
1253 $mockDb = $this->getMockDb();
1254 $mockDb->expects( $this->once() )
1255 ->method( 'selectRow' )
1256 ->with(
1257 'watchlist',
1258 'wl_notificationtimestamp',
1259 [
1260 'wl_user' => 1,
1261 'wl_namespace' => 0,
1262 'wl_title' => 'SomeDbKey',
1263 ]
1264 )
1265 ->will( $this->returnValue(
1266 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1267 ) );
1268
1269 $mockCache = $this->getMockCache();
1270 $mockCache->expects( $this->never() )->method( 'delete' );
1271 $mockCache->expects( $this->once() )
1272 ->method( 'get' )
1273 ->with(
1274 '0:SomeDbKey:1'
1275 )
1276 ->will( $this->returnValue( null ) );
1277 $mockCache->expects( $this->once() )
1278 ->method( 'set' )
1279 ->with(
1280 '0:SomeDbKey:1'
1281 );
1282
1283 $store = new WatchedItemStore(
1284 $this->getMockLoadBalancer( $mockDb ),
1285 $mockCache
1286 );
1287
1288 $watchedItem = $store->getWatchedItem(
1289 $this->getMockNonAnonUserWithId( 1 ),
1290 new TitleValue( 0, 'SomeDbKey' )
1291 );
1292 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1293 $this->assertEquals( 1, $watchedItem->getUser()->getId() );
1294 $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
1295 $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
1296 }
1297
1298 public function testGetWatchedItem_cachedItem() {
1299 $mockDb = $this->getMockDb();
1300 $mockDb->expects( $this->never() )
1301 ->method( 'selectRow' );
1302
1303 $mockUser = $this->getMockNonAnonUserWithId( 1 );
1304 $linkTarget = new TitleValue( 0, 'SomeDbKey' );
1305 $cachedItem = new WatchedItem( $mockUser, $linkTarget, '20151212010101' );
1306
1307 $mockCache = $this->getMockCache();
1308 $mockCache->expects( $this->never() )->method( 'delete' );
1309 $mockCache->expects( $this->never() )->method( 'set' );
1310 $mockCache->expects( $this->once() )
1311 ->method( 'get' )
1312 ->with(
1313 '0:SomeDbKey:1'
1314 )
1315 ->will( $this->returnValue( $cachedItem ) );
1316
1317 $store = new WatchedItemStore(
1318 $this->getMockLoadBalancer( $mockDb ),
1319 $mockCache
1320 );
1321
1322 $this->assertEquals(
1323 $cachedItem,
1324 $store->getWatchedItem(
1325 $mockUser,
1326 $linkTarget
1327 )
1328 );
1329 }
1330
1331 public function testGetWatchedItem_noItem() {
1332 $mockDb = $this->getMockDb();
1333 $mockDb->expects( $this->once() )
1334 ->method( 'selectRow' )
1335 ->with(
1336 'watchlist',
1337 'wl_notificationtimestamp',
1338 [
1339 'wl_user' => 1,
1340 'wl_namespace' => 0,
1341 'wl_title' => 'SomeDbKey',
1342 ]
1343 )
1344 ->will( $this->returnValue( [] ) );
1345
1346 $mockCache = $this->getMockCache();
1347 $mockCache->expects( $this->never() )->method( 'set' );
1348 $mockCache->expects( $this->never() )->method( 'delete' );
1349 $mockCache->expects( $this->once() )
1350 ->method( 'get' )
1351 ->with( '0:SomeDbKey:1' )
1352 ->will( $this->returnValue( false ) );
1353
1354 $store = new WatchedItemStore(
1355 $this->getMockLoadBalancer( $mockDb ),
1356 $mockCache
1357 );
1358
1359 $this->assertFalse(
1360 $store->getWatchedItem(
1361 $this->getMockNonAnonUserWithId( 1 ),
1362 new TitleValue( 0, 'SomeDbKey' )
1363 )
1364 );
1365 }
1366
1367 public function testGetWatchedItem_anonymousUser() {
1368 $mockDb = $this->getMockDb();
1369 $mockDb->expects( $this->never() )
1370 ->method( 'selectRow' );
1371
1372 $mockCache = $this->getMockCache();
1373 $mockCache->expects( $this->never() )->method( 'set' );
1374 $mockCache->expects( $this->never() )->method( 'get' );
1375 $mockCache->expects( $this->never() )->method( 'delete' );
1376
1377 $store = new WatchedItemStore(
1378 $this->getMockLoadBalancer( $mockDb ),
1379 $mockCache
1380 );
1381
1382 $this->assertFalse(
1383 $store->getWatchedItem(
1384 $this->getAnonUser(),
1385 new TitleValue( 0, 'SomeDbKey' )
1386 )
1387 );
1388 }
1389
1390 public function testGetWatchedItemsForUser() {
1391 $mockDb = $this->getMockDb();
1392 $mockDb->expects( $this->once() )
1393 ->method( 'select' )
1394 ->with(
1395 'watchlist',
1396 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1397 [ 'wl_user' => 1 ]
1398 )
1399 ->will( $this->returnValue( [
1400 $this->getFakeRow( [
1401 'wl_namespace' => 0,
1402 'wl_title' => 'Foo1',
1403 'wl_notificationtimestamp' => '20151212010101',
1404 ] ),
1405 $this->getFakeRow( [
1406 'wl_namespace' => 1,
1407 'wl_title' => 'Foo2',
1408 'wl_notificationtimestamp' => null,
1409 ] ),
1410 ] ) );
1411
1412 $mockCache = $this->getMockCache();
1413 $mockCache->expects( $this->never() )->method( 'delete' );
1414 $mockCache->expects( $this->never() )->method( 'get' );
1415 $mockCache->expects( $this->never() )->method( 'set' );
1416
1417 $store = new WatchedItemStore(
1418 $this->getMockLoadBalancer( $mockDb ),
1419 $mockCache
1420 );
1421 $user = $this->getMockNonAnonUserWithId( 1 );
1422
1423 $watchedItems = $store->getWatchedItemsForUser( $user );
1424
1425 $this->assertInternalType( 'array', $watchedItems );
1426 $this->assertCount( 2, $watchedItems );
1427 foreach ( $watchedItems as $watchedItem ) {
1428 $this->assertInstanceOf( 'WatchedItem', $watchedItem );
1429 }
1430 $this->assertEquals(
1431 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
1432 $watchedItems[0]
1433 );
1434 $this->assertEquals(
1435 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
1436 $watchedItems[1]
1437 );
1438 }
1439
1440 public function provideDbTypes() {
1441 return [
1442 [ false, DB_SLAVE ],
1443 [ true, DB_MASTER ],
1444 ];
1445 }
1446
1447 /**
1448 * @dataProvider provideDbTypes
1449 */
1450 public function testGetWatchedItemsForUser_optionsAndEmptyResult( $forWrite, $dbType ) {
1451 $mockDb = $this->getMockDb();
1452 $mockCache = $this->getMockCache();
1453 $mockLoadBalancer = $this->getMockLoadBalancer( $mockDb, $dbType );
1454 $user = $this->getMockNonAnonUserWithId( 1 );
1455
1456 $mockDb->expects( $this->once() )
1457 ->method( 'select' )
1458 ->with(
1459 'watchlist',
1460 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1461 [ 'wl_user' => 1 ],
1462 $this->isType( 'string' ),
1463 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1464 )
1465 ->will( $this->returnValue( [] ) );
1466
1467 $store = new WatchedItemStore(
1468 $mockLoadBalancer,
1469 $mockCache
1470 );
1471
1472 $watchedItems = $store->getWatchedItemsForUser(
1473 $user,
1474 [ 'forWrite' => $forWrite, 'sort' => WatchedItemStore::SORT_ASC ]
1475 );
1476 $this->assertEquals( [], $watchedItems );
1477 }
1478
1479 public function testGetWatchedItemsForUser_badSortOptionThrowsException() {
1480 $store = new WatchedItemStore(
1481 $this->getMockLoadBalancer( $this->getMockDb() ),
1482 $this->getMockCache()
1483 );
1484
1485 $this->setExpectedException( 'InvalidArgumentException' );
1486 $store->getWatchedItemsForUser(
1487 $this->getMockNonAnonUserWithId( 1 ),
1488 [ 'sort' => 'foo' ]
1489 );
1490 }
1491
1492 public function testIsWatchedItem_existingItem() {
1493 $mockDb = $this->getMockDb();
1494 $mockDb->expects( $this->once() )
1495 ->method( 'selectRow' )
1496 ->with(
1497 'watchlist',
1498 'wl_notificationtimestamp',
1499 [
1500 'wl_user' => 1,
1501 'wl_namespace' => 0,
1502 'wl_title' => 'SomeDbKey',
1503 ]
1504 )
1505 ->will( $this->returnValue(
1506 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1507 ) );
1508
1509 $mockCache = $this->getMockCache();
1510 $mockCache->expects( $this->never() )->method( 'delete' );
1511 $mockCache->expects( $this->once() )
1512 ->method( 'get' )
1513 ->with( '0:SomeDbKey:1' )
1514 ->will( $this->returnValue( false ) );
1515 $mockCache->expects( $this->once() )
1516 ->method( 'set' )
1517 ->with(
1518 '0:SomeDbKey:1'
1519 );
1520
1521 $store = new WatchedItemStore(
1522 $this->getMockLoadBalancer( $mockDb ),
1523 $mockCache
1524 );
1525
1526 $this->assertTrue(
1527 $store->isWatched(
1528 $this->getMockNonAnonUserWithId( 1 ),
1529 new TitleValue( 0, 'SomeDbKey' )
1530 )
1531 );
1532 }
1533
1534 public function testIsWatchedItem_noItem() {
1535 $mockDb = $this->getMockDb();
1536 $mockDb->expects( $this->once() )
1537 ->method( 'selectRow' )
1538 ->with(
1539 'watchlist',
1540 'wl_notificationtimestamp',
1541 [
1542 'wl_user' => 1,
1543 'wl_namespace' => 0,
1544 'wl_title' => 'SomeDbKey',
1545 ]
1546 )
1547 ->will( $this->returnValue( [] ) );
1548
1549 $mockCache = $this->getMockCache();
1550 $mockCache->expects( $this->never() )->method( 'set' );
1551 $mockCache->expects( $this->never() )->method( 'delete' );
1552 $mockCache->expects( $this->once() )
1553 ->method( 'get' )
1554 ->with( '0:SomeDbKey:1' )
1555 ->will( $this->returnValue( false ) );
1556
1557 $store = new WatchedItemStore(
1558 $this->getMockLoadBalancer( $mockDb ),
1559 $mockCache
1560 );
1561
1562 $this->assertFalse(
1563 $store->isWatched(
1564 $this->getMockNonAnonUserWithId( 1 ),
1565 new TitleValue( 0, 'SomeDbKey' )
1566 )
1567 );
1568 }
1569
1570 public function testIsWatchedItem_anonymousUser() {
1571 $mockDb = $this->getMockDb();
1572 $mockDb->expects( $this->never() )
1573 ->method( 'selectRow' );
1574
1575 $mockCache = $this->getMockCache();
1576 $mockCache->expects( $this->never() )->method( 'set' );
1577 $mockCache->expects( $this->never() )->method( 'get' );
1578 $mockCache->expects( $this->never() )->method( 'delete' );
1579
1580 $store = new WatchedItemStore(
1581 $this->getMockLoadBalancer( $mockDb ),
1582 $mockCache
1583 );
1584
1585 $this->assertFalse(
1586 $store->isWatched(
1587 $this->getAnonUser(),
1588 new TitleValue( 0, 'SomeDbKey' )
1589 )
1590 );
1591 }
1592
1593 public function testGetNotificationTimestampsBatch() {
1594 $targets = [
1595 new TitleValue( 0, 'SomeDbKey' ),
1596 new TitleValue( 1, 'AnotherDbKey' ),
1597 ];
1598
1599 $mockDb = $this->getMockDb();
1600 $dbResult = [
1601 $this->getFakeRow( [
1602 'wl_namespace' => 0,
1603 'wl_title' => 'SomeDbKey',
1604 'wl_notificationtimestamp' => '20151212010101',
1605 ] ),
1606 $this->getFakeRow(
1607 [
1608 'wl_namespace' => 1,
1609 'wl_title' => 'AnotherDbKey',
1610 'wl_notificationtimestamp' => null,
1611 ]
1612 ),
1613 ];
1614
1615 $mockDb->expects( $this->once() )
1616 ->method( 'makeWhereFrom2d' )
1617 ->with(
1618 [ [ 'SomeDbKey' => 1 ], [ 'AnotherDbKey' => 1 ] ],
1619 $this->isType( 'string' ),
1620 $this->isType( 'string' )
1621 )
1622 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1623 $mockDb->expects( $this->once() )
1624 ->method( 'select' )
1625 ->with(
1626 'watchlist',
1627 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1628 [
1629 'makeWhereFrom2d return value',
1630 'wl_user' => 1
1631 ],
1632 $this->isType( 'string' )
1633 )
1634 ->will( $this->returnValue( $dbResult ) );
1635
1636 $mockCache = $this->getMockCache();
1637 $mockCache->expects( $this->exactly( 2 ) )
1638 ->method( 'get' )
1639 ->withConsecutive(
1640 [ '0:SomeDbKey:1' ],
1641 [ '1:AnotherDbKey:1' ]
1642 )
1643 ->will( $this->returnValue( null ) );
1644 $mockCache->expects( $this->never() )->method( 'set' );
1645 $mockCache->expects( $this->never() )->method( 'delete' );
1646
1647 $store = new WatchedItemStore(
1648 $this->getMockLoadBalancer( $mockDb ),
1649 $mockCache
1650 );
1651
1652 $this->assertEquals(
1653 [
1654 0 => [ 'SomeDbKey' => '20151212010101', ],
1655 1 => [ 'AnotherDbKey' => null, ],
1656 ],
1657 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1658 );
1659 }
1660
1661 public function testGetNotificationTimestampsBatch_notWatchedTarget() {
1662 $targets = [
1663 new TitleValue( 0, 'OtherDbKey' ),
1664 ];
1665
1666 $mockDb = $this->getMockDb();
1667
1668 $mockDb->expects( $this->once() )
1669 ->method( 'makeWhereFrom2d' )
1670 ->with(
1671 [ [ 'OtherDbKey' => 1 ] ],
1672 $this->isType( 'string' ),
1673 $this->isType( 'string' )
1674 )
1675 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1676 $mockDb->expects( $this->once() )
1677 ->method( 'select' )
1678 ->with(
1679 'watchlist',
1680 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1681 [
1682 'makeWhereFrom2d return value',
1683 'wl_user' => 1
1684 ],
1685 $this->isType( 'string' )
1686 )
1687 ->will( $this->returnValue( $this->getFakeRow( [] ) ) );
1688
1689 $mockCache = $this->getMockCache();
1690 $mockCache->expects( $this->once() )
1691 ->method( 'get' )
1692 ->with( '0:OtherDbKey:1' )
1693 ->will( $this->returnValue( null ) );
1694 $mockCache->expects( $this->never() )->method( 'set' );
1695 $mockCache->expects( $this->never() )->method( 'delete' );
1696
1697 $store = new WatchedItemStore(
1698 $this->getMockLoadBalancer( $mockDb ),
1699 $mockCache
1700 );
1701
1702 $this->assertEquals(
1703 [
1704 0 => [ 'OtherDbKey' => false, ],
1705 ],
1706 $store->getNotificationTimestampsBatch( $this->getMockNonAnonUserWithId( 1 ), $targets )
1707 );
1708 }
1709
1710 public function testGetNotificationTimestampsBatch_cachedItem() {
1711 $targets = [
1712 new TitleValue( 0, 'SomeDbKey' ),
1713 new TitleValue( 1, 'AnotherDbKey' ),
1714 ];
1715
1716 $user = $this->getMockNonAnonUserWithId( 1 );
1717 $cachedItem = new WatchedItem( $user, $targets[0], '20151212010101' );
1718
1719 $mockDb = $this->getMockDb();
1720
1721 $mockDb->expects( $this->once() )
1722 ->method( 'makeWhereFrom2d' )
1723 ->with(
1724 [ 1 => [ 'AnotherDbKey' => 1 ] ],
1725 $this->isType( 'string' ),
1726 $this->isType( 'string' )
1727 )
1728 ->will( $this->returnValue( 'makeWhereFrom2d return value' ) );
1729 $mockDb->expects( $this->once() )
1730 ->method( 'select' )
1731 ->with(
1732 'watchlist',
1733 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1734 [
1735 'makeWhereFrom2d return value',
1736 'wl_user' => 1
1737 ],
1738 $this->isType( 'string' )
1739 )
1740 ->will( $this->returnValue( [
1741 $this->getFakeRow(
1742 [ 'wl_namespace' => 1, 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
1743 )
1744 ] ) );
1745
1746 $mockCache = $this->getMockCache();
1747 $mockCache->expects( $this->at( 1 ) )
1748 ->method( 'get' )
1749 ->with( '0:SomeDbKey:1' )
1750 ->will( $this->returnValue( $cachedItem ) );
1751 $mockCache->expects( $this->at( 3 ) )
1752 ->method( 'get' )
1753 ->with( '1:AnotherDbKey:1' )
1754 ->will( $this->returnValue( null ) );
1755 $mockCache->expects( $this->never() )->method( 'set' );
1756 $mockCache->expects( $this->never() )->method( 'delete' );
1757
1758 $store = new WatchedItemStore(
1759 $this->getMockLoadBalancer( $mockDb ),
1760 $mockCache
1761 );
1762
1763 $this->assertEquals(
1764 [
1765 0 => [ 'SomeDbKey' => '20151212010101', ],
1766 1 => [ 'AnotherDbKey' => null, ],
1767 ],
1768 $store->getNotificationTimestampsBatch( $user, $targets )
1769 );
1770 }
1771
1772 public function testGetNotificationTimestampsBatch_allItemsCached() {
1773 $targets = [
1774 new TitleValue( 0, 'SomeDbKey' ),
1775 new TitleValue( 1, 'AnotherDbKey' ),
1776 ];
1777
1778 $user = $this->getMockNonAnonUserWithId( 1 );
1779 $cachedItems = [
1780 new WatchedItem( $user, $targets[0], '20151212010101' ),
1781 new WatchedItem( $user, $targets[1], null ),
1782 ];
1783 $mockDb = $this->getMockDb();
1784 $mockDb->expects( $this->never() )->method( $this->anything() );
1785
1786 $mockCache = $this->getMockCache();
1787 $mockCache->expects( $this->at( 1 ) )
1788 ->method( 'get' )
1789 ->with( '0:SomeDbKey:1' )
1790 ->will( $this->returnValue( $cachedItems[0] ) );
1791 $mockCache->expects( $this->at( 3 ) )
1792 ->method( 'get' )
1793 ->with( '1:AnotherDbKey:1' )
1794 ->will( $this->returnValue( $cachedItems[1] ) );
1795 $mockCache->expects( $this->never() )->method( 'set' );
1796 $mockCache->expects( $this->never() )->method( 'delete' );
1797
1798 $store = new WatchedItemStore(
1799 $this->getMockLoadBalancer( $mockDb ),
1800 $mockCache
1801 );
1802
1803 $this->assertEquals(
1804 [
1805 0 => [ 'SomeDbKey' => '20151212010101', ],
1806 1 => [ 'AnotherDbKey' => null, ],
1807 ],
1808 $store->getNotificationTimestampsBatch( $user, $targets )
1809 );
1810 }
1811
1812 public function testGetNotificationTimestampsBatch_anonymousUser() {
1813 $targets = [
1814 new TitleValue( 0, 'SomeDbKey' ),
1815 new TitleValue( 1, 'AnotherDbKey' ),
1816 ];
1817
1818 $mockDb = $this->getMockDb();
1819 $mockDb->expects( $this->never() )->method( $this->anything() );
1820
1821 $mockCache = $this->getMockCache();
1822 $mockCache->expects( $this->never() )->method( $this->anything() );
1823
1824 $store = new WatchedItemStore(
1825 $this->getMockLoadBalancer( $mockDb ),
1826 $mockCache
1827 );
1828
1829 $this->assertEquals(
1830 [
1831 0 => [ 'SomeDbKey' => false, ],
1832 1 => [ 'AnotherDbKey' => false, ],
1833 ],
1834 $store->getNotificationTimestampsBatch( $this->getAnonUser(), $targets )
1835 );
1836 }
1837
1838 public function testResetNotificationTimestamp_anonymousUser() {
1839 $mockDb = $this->getMockDb();
1840 $mockDb->expects( $this->never() )
1841 ->method( 'selectRow' );
1842
1843 $mockCache = $this->getMockCache();
1844 $mockCache->expects( $this->never() )->method( 'get' );
1845 $mockCache->expects( $this->never() )->method( 'set' );
1846 $mockCache->expects( $this->never() )->method( 'delete' );
1847
1848 $store = new WatchedItemStore(
1849 $this->getMockLoadBalancer( $mockDb ),
1850 $mockCache
1851 );
1852
1853 $this->assertFalse(
1854 $store->resetNotificationTimestamp(
1855 $this->getAnonUser(),
1856 Title::newFromText( 'SomeDbKey' )
1857 )
1858 );
1859 }
1860
1861 public function testResetNotificationTimestamp_noItem() {
1862 $mockDb = $this->getMockDb();
1863 $mockDb->expects( $this->once() )
1864 ->method( 'selectRow' )
1865 ->with(
1866 'watchlist',
1867 'wl_notificationtimestamp',
1868 [
1869 'wl_user' => 1,
1870 'wl_namespace' => 0,
1871 'wl_title' => 'SomeDbKey',
1872 ]
1873 )
1874 ->will( $this->returnValue( [] ) );
1875
1876 $mockCache = $this->getMockCache();
1877 $mockCache->expects( $this->never() )->method( 'get' );
1878 $mockCache->expects( $this->never() )->method( 'set' );
1879 $mockCache->expects( $this->never() )->method( 'delete' );
1880
1881 $store = new WatchedItemStore(
1882 $this->getMockLoadBalancer( $mockDb ),
1883 $mockCache
1884 );
1885
1886 $this->assertFalse(
1887 $store->resetNotificationTimestamp(
1888 $this->getMockNonAnonUserWithId( 1 ),
1889 Title::newFromText( 'SomeDbKey' )
1890 )
1891 );
1892 }
1893
1894 public function testResetNotificationTimestamp_item() {
1895 $user = $this->getMockNonAnonUserWithId( 1 );
1896 $title = Title::newFromText( 'SomeDbKey' );
1897
1898 $mockDb = $this->getMockDb();
1899 $mockDb->expects( $this->once() )
1900 ->method( 'selectRow' )
1901 ->with(
1902 'watchlist',
1903 'wl_notificationtimestamp',
1904 [
1905 'wl_user' => 1,
1906 'wl_namespace' => 0,
1907 'wl_title' => 'SomeDbKey',
1908 ]
1909 )
1910 ->will( $this->returnValue(
1911 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
1912 ) );
1913
1914 $mockCache = $this->getMockCache();
1915 $mockCache->expects( $this->never() )->method( 'get' );
1916 $mockCache->expects( $this->once() )
1917 ->method( 'set' )
1918 ->with(
1919 '0:SomeDbKey:1',
1920 $this->isInstanceOf( WatchedItem::class )
1921 );
1922 $mockCache->expects( $this->once() )
1923 ->method( 'delete' )
1924 ->with( '0:SomeDbKey:1' );
1925
1926 $store = new WatchedItemStore(
1927 $this->getMockLoadBalancer( $mockDb ),
1928 $mockCache
1929 );
1930
1931 // Note: This does not actually assert the job is correct
1932 $callableCallCounter = 0;
1933 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1934 $callableCallCounter++;
1935 $this->assertInternalType( 'callable', $callable );
1936 };
1937 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1938
1939 $this->assertTrue(
1940 $store->resetNotificationTimestamp(
1941 $user,
1942 $title
1943 )
1944 );
1945 $this->assertEquals( 1, $callableCallCounter );
1946
1947 ScopedCallback::consume( $scopedOverride );
1948 }
1949
1950 public function testResetNotificationTimestamp_noItemForced() {
1951 $user = $this->getMockNonAnonUserWithId( 1 );
1952 $title = Title::newFromText( 'SomeDbKey' );
1953
1954 $mockDb = $this->getMockDb();
1955 $mockDb->expects( $this->never() )
1956 ->method( 'selectRow' );
1957
1958 $mockCache = $this->getMockCache();
1959 $mockDb->expects( $this->never() )
1960 ->method( 'get' );
1961 $mockDb->expects( $this->never() )
1962 ->method( 'set' );
1963 $mockDb->expects( $this->never() )
1964 ->method( 'delete' );
1965
1966 $store = new WatchedItemStore(
1967 $this->getMockLoadBalancer( $mockDb ),
1968 $mockCache
1969 );
1970
1971 // Note: This does not actually assert the job is correct
1972 $callableCallCounter = 0;
1973 $mockCallback = function( $callable ) use ( &$callableCallCounter ) {
1974 $callableCallCounter++;
1975 $this->assertInternalType( 'callable', $callable );
1976 };
1977 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback( $mockCallback );
1978
1979 $this->assertTrue(
1980 $store->resetNotificationTimestamp(
1981 $user,
1982 $title,
1983 'force'
1984 )
1985 );
1986 $this->assertEquals( 1, $callableCallCounter );
1987
1988 ScopedCallback::consume( $scopedOverride );
1989 }
1990
1991 /**
1992 * @param $text
1993 * @param int $ns
1994 *
1995 * @return PHPUnit_Framework_MockObject_MockObject|Title
1996 */
1997 private function getMockTitle( $text, $ns = 0 ) {
1998 $title = $this->getMock( Title::class );
1999 $title->expects( $this->any() )
2000 ->method( 'getText' )
2001 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
2002 $title->expects( $this->any() )
2003 ->method( 'getDbKey' )
2004 ->will( $this->returnValue( str_replace( '_', ' ', $text ) ) );
2005 $title->expects( $this->any() )
2006 ->method( 'getNamespace' )
2007 ->will( $this->returnValue( $ns ) );
2008 return $title;
2009 }
2010
2011 public function testResetNotificationTimestamp_oldidSpecifiedLatestRevisionForced() {
2012 $user = $this->getMockNonAnonUserWithId( 1 );
2013 $oldid = 22;
2014 $title = $this->getMockTitle( 'SomeTitle' );
2015 $title->expects( $this->once() )
2016 ->method( 'getNextRevisionID' )
2017 ->with( $oldid )
2018 ->will( $this->returnValue( false ) );
2019
2020 $mockDb = $this->getMockDb();
2021 $mockDb->expects( $this->never() )
2022 ->method( 'selectRow' );
2023
2024 $mockCache = $this->getMockCache();
2025 $mockDb->expects( $this->never() )
2026 ->method( 'get' );
2027 $mockDb->expects( $this->never() )
2028 ->method( 'set' );
2029 $mockDb->expects( $this->never() )
2030 ->method( 'delete' );
2031
2032 $store = new WatchedItemStore(
2033 $this->getMockLoadBalancer( $mockDb ),
2034 $mockCache
2035 );
2036
2037 // Note: This does not actually assert the job is correct
2038 $callableCallCounter = 0;
2039 $scopedOverride = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
2040 function( $callable ) use ( &$callableCallCounter ) {
2041 $callableCallCounter++;
2042 $this->assertInternalType( 'callable', $callable );
2043 }
2044 );
2045
2046 $this->assertTrue(
2047 $store->resetNotificationTimestamp(
2048 $user,
2049 $title,
2050 'force',
2051 $oldid
2052 )
2053 );
2054 $this->assertEquals( 1, $callableCallCounter );
2055
2056 ScopedCallback::consume( $scopedOverride );
2057 }
2058
2059 public function testResetNotificationTimestamp_oldidSpecifiedNotLatestRevisionForced() {
2060 $user = $this->getMockNonAnonUserWithId( 1 );
2061 $oldid = 22;
2062 $title = $this->getMockTitle( 'SomeDbKey' );
2063 $title->expects( $this->once() )
2064 ->method( 'getNextRevisionID' )
2065 ->with( $oldid )
2066 ->will( $this->returnValue( 33 ) );
2067
2068 $mockDb = $this->getMockDb();
2069 $mockDb->expects( $this->once() )
2070 ->method( 'selectRow' )
2071 ->with(
2072 'watchlist',
2073 'wl_notificationtimestamp',
2074 [
2075 'wl_user' => 1,
2076 'wl_namespace' => 0,
2077 'wl_title' => 'SomeDbKey',
2078 ]
2079 )
2080 ->will( $this->returnValue(
2081 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2082 ) );
2083
2084 $mockCache = $this->getMockCache();
2085 $mockDb->expects( $this->never() )
2086 ->method( 'get' );
2087 $mockDb->expects( $this->never() )
2088 ->method( 'set' );
2089 $mockDb->expects( $this->never() )
2090 ->method( 'delete' );
2091
2092 $store = new WatchedItemStore(
2093 $this->getMockLoadBalancer( $mockDb ),
2094 $mockCache
2095 );
2096
2097 // Note: This does not actually assert the job is correct
2098 $addUpdateCallCounter = 0;
2099 $scopedOverrideDeferred = $store->overrideDeferredUpdatesAddCallableUpdateCallback(
2100 function( $callable ) use ( &$addUpdateCallCounter ) {
2101 $addUpdateCallCounter++;
2102 $this->assertInternalType( 'callable', $callable );
2103 }
2104 );
2105
2106 $getTimestampCallCounter = 0;
2107 $scopedOverrideRevision = $store->overrideRevisionGetTimestampFromIdCallback(
2108 function( $titleParam, $oldidParam ) use ( &$getTimestampCallCounter, $title, $oldid ) {
2109 $getTimestampCallCounter++;
2110 $this->assertEquals( $title, $titleParam );
2111 $this->assertEquals( $oldid, $oldidParam );
2112 }
2113 );
2114
2115 $this->assertTrue(
2116 $store->resetNotificationTimestamp(
2117 $user,
2118 $title,
2119 'force',
2120 $oldid
2121 )
2122 );
2123 $this->assertEquals( 1, $addUpdateCallCounter );
2124 $this->assertEquals( 1, $getTimestampCallCounter );
2125
2126 ScopedCallback::consume( $scopedOverrideDeferred );
2127 ScopedCallback::consume( $scopedOverrideRevision );
2128 }
2129
2130 public function testUpdateNotificationTimestamp_watchersExist() {
2131 $mockDb = $this->getMockDb();
2132 $mockDb->expects( $this->once() )
2133 ->method( 'select' )
2134 ->with(
2135 [ 'watchlist' ],
2136 [ 'wl_user' ],
2137 [
2138 'wl_user != 1',
2139 'wl_namespace' => 0,
2140 'wl_title' => 'SomeDbKey',
2141 'wl_notificationtimestamp IS NULL'
2142 ]
2143 )
2144 ->will(
2145 $this->returnValue( [
2146 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2147 $this->getFakeRow( [ 'wl_user' => '3' ] )
2148 ] )
2149 );
2150 $mockDb->expects( $this->once() )
2151 ->method( 'onTransactionIdle' )
2152 ->with( $this->isType( 'callable' ) )
2153 ->will( $this->returnCallback( function( $callable ) {
2154 $callable();
2155 } ) );
2156 $mockDb->expects( $this->once() )
2157 ->method( 'update' )
2158 ->with(
2159 'watchlist',
2160 [ 'wl_notificationtimestamp' => null ],
2161 [
2162 'wl_user' => [ 2, 3 ],
2163 'wl_namespace' => 0,
2164 'wl_title' => 'SomeDbKey',
2165 ]
2166 );
2167
2168 $mockCache = $this->getMockCache();
2169 $mockCache->expects( $this->never() )->method( 'set' );
2170 $mockCache->expects( $this->never() )->method( 'get' );
2171 $mockCache->expects( $this->never() )->method( 'delete' );
2172
2173 $store = new WatchedItemStore(
2174 $this->getMockLoadBalancer( $mockDb ),
2175 $mockCache
2176 );
2177
2178 $this->assertEquals(
2179 [ 2, 3 ],
2180 $store->updateNotificationTimestamp(
2181 $this->getMockNonAnonUserWithId( 1 ),
2182 new TitleValue( 0, 'SomeDbKey' ),
2183 '20151212010101'
2184 )
2185 );
2186 }
2187
2188 public function testUpdateNotificationTimestamp_noWatchers() {
2189 $mockDb = $this->getMockDb();
2190 $mockDb->expects( $this->once() )
2191 ->method( 'select' )
2192 ->with(
2193 [ 'watchlist' ],
2194 [ 'wl_user' ],
2195 [
2196 'wl_user != 1',
2197 'wl_namespace' => 0,
2198 'wl_title' => 'SomeDbKey',
2199 'wl_notificationtimestamp IS NULL'
2200 ]
2201 )
2202 ->will(
2203 $this->returnValue( [] )
2204 );
2205 $mockDb->expects( $this->never() )
2206 ->method( 'onTransactionIdle' );
2207 $mockDb->expects( $this->never() )
2208 ->method( 'update' );
2209
2210 $mockCache = $this->getMockCache();
2211 $mockCache->expects( $this->never() )->method( 'set' );
2212 $mockCache->expects( $this->never() )->method( 'get' );
2213 $mockCache->expects( $this->never() )->method( 'delete' );
2214
2215 $store = new WatchedItemStore(
2216 $this->getMockLoadBalancer( $mockDb ),
2217 $mockCache
2218 );
2219
2220 $watchers = $store->updateNotificationTimestamp(
2221 $this->getMockNonAnonUserWithId( 1 ),
2222 new TitleValue( 0, 'SomeDbKey' ),
2223 '20151212010101'
2224 );
2225 $this->assertInternalType( 'array', $watchers );
2226 $this->assertEmpty( $watchers );
2227 }
2228
2229 public function testUpdateNotificationTimestamp_clearsCachedItems() {
2230 $user = $this->getMockNonAnonUserWithId( 1 );
2231 $titleValue = new TitleValue( 0, 'SomeDbKey' );
2232
2233 $mockDb = $this->getMockDb();
2234 $mockDb->expects( $this->once() )
2235 ->method( 'selectRow' )
2236 ->will( $this->returnValue(
2237 $this->getFakeRow( [ 'wl_notificationtimestamp' => '20151212010101' ] )
2238 ) );
2239 $mockDb->expects( $this->once() )
2240 ->method( 'select' )
2241 ->will(
2242 $this->returnValue( [
2243 $this->getFakeRow( [ 'wl_user' => '2' ] ),
2244 $this->getFakeRow( [ 'wl_user' => '3' ] )
2245 ] )
2246 );
2247 $mockDb->expects( $this->once() )
2248 ->method( 'onTransactionIdle' )
2249 ->with( $this->isType( 'callable' ) )
2250 ->will( $this->returnCallback( function( $callable ) {
2251 $callable();
2252 } ) );
2253 $mockDb->expects( $this->once() )
2254 ->method( 'update' );
2255
2256 $mockCache = $this->getMockCache();
2257 $mockCache->expects( $this->once() )
2258 ->method( 'set' )
2259 ->with( '0:SomeDbKey:1', $this->isType( 'object' ) );
2260 $mockCache->expects( $this->once() )
2261 ->method( 'get' )
2262 ->with( '0:SomeDbKey:1' );
2263 $mockCache->expects( $this->once() )
2264 ->method( 'delete' )
2265 ->with( '0:SomeDbKey:1' );
2266
2267 $store = new WatchedItemStore(
2268 $this->getMockLoadBalancer( $mockDb ),
2269 $mockCache
2270 );
2271
2272 // This will add the item to the cache
2273 $store->getWatchedItem( $user, $titleValue );
2274
2275 $store->updateNotificationTimestamp(
2276 $this->getMockNonAnonUserWithId( 1 ),
2277 $titleValue,
2278 '20151212010101'
2279 );
2280 }
2281
2282 }