Debian Codegraph @

tests/phpunit/unit/includes/watchlist/WatchedItemQueryServiceUnitTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\CommentStore\CommentStore;
4 use MediaWiki\Revision\RevisionRecord;
5 use MediaWiki\Title\TitleValue;
6 use MediaWiki\User\Options\UserOptionsLookup;
7 use MediaWiki\User\TempUser\TempUserConfig;
8 use MediaWiki\User\User;
9 use MediaWiki\User\UserIdentityValue;
10 use MediaWiki\Watchlist\WatchedItem;
11 use MediaWiki\Watchlist\WatchedItemQueryService;
12 use MediaWiki\Watchlist\WatchedItemQueryServiceExtension;
13 use MediaWiki\Watchlist\WatchedItemStore;
14 use PHPUnit\Framework\MockObject\MockObject;
15 use Wikimedia\Rdbms\FakeResultWrapper;
16 use Wikimedia\Rdbms\IConnectionProvider;
17 use Wikimedia\Rdbms\IDatabase;
18 use Wikimedia\Rdbms\IReadableDatabase;
19 use Wikimedia\Rdbms\SelectQueryBuilder;
20 use Wikimedia\TestingAccessWrapper;
21
22 /**
23 * @covers \MediaWiki\Watchlist\WatchedItemQueryService
24 */
25 class WatchedItemQueryServiceUnitTest extends MediaWikiUnitTestCase {
26
27 /**
28 * @return MockObject&CommentStore
29 */
30 private function getMockCommentStore() {
31 $mockStore = $this->createMock( CommentStore::class );
32 $mockStore->method( 'getJoin' )
33 ->willReturn( [
34 'tables' => [ 'commentstore' => 'table' ],
35 'fields' => [ 'commentstore' => 'field' ],
36 'joins' => [ 'commentstore' => 'join' ],
37 ] );
38 return $mockStore;
39 }
40
41 /**
42 * @param IDatabase $mockDb
43 * @param UserOptionsLookup|null $userOptionsLookup
44 * @return WatchedItemQueryService
45 */
46 private function newService(
47 IDatabase $mockDb,
48 ?UserOptionsLookup $userOptionsLookup = null
49 ) {
50 return new WatchedItemQueryService(
51 $this->getMockDbProvider( $mockDb ),
52 $this->getMockCommentStore(),
53 $this->getMockWatchedItemStore(),
54 $this->createHookContainer(),
55 $userOptionsLookup ?? $this->createMock( UserOptionsLookup::class ),
56 $this->createMock( TempUserConfig::class ),
57 false
58 );
59 }
60
61 /**
62 * @return MockObject&IDatabase
63 */
64 private function getMockDb() {
65 $mock = $this->createMock( IDatabase::class );
66
67 $mock->method( 'makeList' )
68 ->with(
69 $this->isType( 'array' ),
70 $this->isType( 'int' )
71 )
72 ->willReturnCallback( static function ( $a, $conj ) {
73 $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
74 $conds = [];
75 foreach ( $a as $k => $v ) {
76 if ( is_int( $k ) ) {
77 $conds[] = "($v)";
78 } elseif ( is_array( $v ) ) {
79 $conds[] = "($k IN ('" . implode( "','", $v ) . "'))";
80 } else {
81 $conds[] = "($k = '$v')";
82 }
83 }
84 return implode( $sqlConj, $conds );
85 } );
86
87 $mock->method( 'buildComparison' )
88 ->with(
89 $this->isType( 'string' ),
90 $this->isType( 'array' )
91 )
92 ->willReturnCallback( static function ( string $op, array $conds ) {
93 $sql = '';
94 foreach ( array_reverse( $conds ) as $field => $value ) {
95 if ( $sql === '' ) {
96 $sql = "$field $op '$value'";
97 $op = rtrim( $op, '=' );
98 } else {
99 $sql = "$field $op '$value' OR ($field = '$value' AND ($sql))";
100 }
101 }
102 return $sql;
103 } );
104
105 $mock->method( 'timestamp' )
106 ->willReturnArgument( 0 );
107
108 $mock->method( 'bitAnd' )
109 ->willReturnCallback( static function ( $a, $b ) {
110 return "($a & $b)";
111 } );
112 $mock->method( 'newSelectQueryBuilder' )->willReturnCallback( static function () use ( $mock ) {
113 return new SelectQueryBuilder( $mock );
114 } );
115
116 return $mock;
117 }
118
119 private function getMockDbProvider( IReadableDatabase $mockDb ): IConnectionProvider {
120 $mock = $this->createMock( IConnectionProvider::class );
121 $mock->method( 'getReplicaDatabase' )
122 ->willReturn( $mockDb );
123 return $mock;
124 }
125
126 /**
127 * @return MockObject|WatchedItemStore
128 */
129 private function getMockWatchedItemStore() {
130 $mock = $this->createMock( WatchedItemStore::class );
131 $mock->method( 'getLatestNotificationTimestamp' )
132 ->willReturnArgument( 0 );
133 return $mock;
134 }
135
136 /**
137 * @param int $id
138 * @param bool $canPatrol result for User::useRCPatrol() and User::useNPPatrol()
139 * @param string|null $notAllowedAction for permission checks, the user has all other rights
140 * @param string[] $extraMethods Extra methods that are expected might be called
141 * @return MockObject|User
142 */
143 private function getMockUserWithId(
144 $id,
145 bool $canPatrol = true,
146 $notAllowedAction = null,
147 array $extraMethods = []
148 ) {
149 $methods = array_merge(
150 [ 'isRegistered', 'getId', 'useRCPatrol', 'useNPPatrol', 'isAllowed', 'isAllowedAny' ],
151 $extraMethods
152 );
153 $mock = $this->createNoOpMock(
154 User::class,
155 $methods
156 );
157 $mock->method( 'isRegistered' )->willReturn( true );
158 $mock->method( 'getId' )->willReturn( $id );
159 $mock->method( 'useRCPatrol' )->willReturn( $canPatrol );
160 $mock->method( 'useNPPatrol' )->willReturn( $canPatrol );
161 $mock->method( 'isAllowed' )->willReturnCallback(
162 static function ( $permission ) use ( $notAllowedAction ) {
163 return $permission !== $notAllowedAction;
164 }
165 );
166 $mock->method( 'isAllowedAny' )->willReturnCallback(
167 static function ( ...$permissions ) use ( $notAllowedAction ) {
168 return !in_array( $notAllowedAction, $permissions );
169 }
170 );
171 return $mock;
172 }
173
174 public function testGetWatchedItemsWithRecentChangeInfo() {
175 $mockDb = $this->getMockDb();
176 $mockDb->expects( $this->once() )
177 ->method( 'select' )
178 ->with(
179 [ 'recentchanges', 'watchlist', 'page' ],
180 [
181 'rc_id',
182 'rc_namespace',
183 'rc_title',
184 'rc_timestamp',
185 'rc_type',
186 'rc_deleted',
187 'wl_notificationtimestamp',
188 'rc_cur_id',
189 'rc_this_oldid',
190 'rc_last_oldid',
191 ],
192 [
193 'wl_user' => 1,
194 '(rc_this_oldid=page_latest) OR (rc_type=3)',
195 ],
196 $this->isType( 'string' ),
197 [
198 'LIMIT' => 3,
199 ],
200 [
201 'watchlist' => [
202 'JOIN',
203 [
204 'wl_namespace=rc_namespace',
205 'wl_title=rc_title'
206 ]
207 ],
208 'page' => [
209 'LEFT JOIN',
210 'rc_cur_id=page_id',
211 ],
212 ]
213 )
214 ->willReturn( new FakeResultWrapper( [
215 (object)[
216 'rc_id' => 1,
217 'rc_namespace' => 0,
218 'rc_title' => 'Foo1',
219 'rc_timestamp' => '20151212010101',
220 'rc_type' => RC_NEW,
221 'rc_deleted' => 0,
222 'wl_notificationtimestamp' => '20151212010101',
223 ],
224 (object)[
225 'rc_id' => 2,
226 'rc_namespace' => 1,
227 'rc_title' => 'Foo2',
228 'rc_timestamp' => '20151212010102',
229 'rc_type' => RC_NEW,
230 'rc_deleted' => 0,
231 'wl_notificationtimestamp' => null,
232 ],
233 (object)[
234 'rc_id' => 3,
235 'rc_namespace' => 1,
236 'rc_title' => 'Foo3',
237 'rc_timestamp' => '20151212010103',
238 'rc_type' => RC_NEW,
239 'rc_deleted' => 0,
240 'wl_notificationtimestamp' => null,
241 ],
242 ] ) );
243
244 $queryService = $this->newService( $mockDb );
245 $user = $this->getMockUserWithId( 1 );
246
247 $startFrom = null;
248 $items = $queryService->getWatchedItemsWithRecentChangeInfo(
249 $user, [ 'limit' => 2 ], $startFrom
250 );
251
252 $this->assertIsArray( $items );
253 $this->assertCount( 2, $items );
254
255 foreach ( $items as [ $watchedItem, $recentChangeInfo ] ) {
256 $this->assertInstanceOf( WatchedItem::class, $watchedItem );
257 $this->assertIsArray( $recentChangeInfo );
258 }
259
260 $this->assertEquals(
261 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
262 $items[0][0]
263 );
264 $this->assertEquals(
265 [
266 'rc_id' => 1,
267 'rc_namespace' => 0,
268 'rc_title' => 'Foo1',
269 'rc_timestamp' => '20151212010101',
270 'rc_type' => RC_NEW,
271 'rc_deleted' => 0,
272 ],
273 $items[0][1]
274 );
275
276 $this->assertEquals(
277 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
278 $items[1][0]
279 );
280 $this->assertEquals(
281 [
282 'rc_id' => 2,
283 'rc_namespace' => 1,
284 'rc_title' => 'Foo2',
285 'rc_timestamp' => '20151212010102',
286 'rc_type' => RC_NEW,
287 'rc_deleted' => 0,
288 ],
289 $items[1][1]
290 );
291
292 $this->assertEquals( [ '20151212010103', 3 ], $startFrom );
293 }
294
295 public function testGetWatchedItemsWithRecentChangeInfo_extension() {
296 $mockDb = $this->getMockDb();
297 $mockDb->expects( $this->once() )
298 ->method( 'select' )
299 ->with(
300 [ 'recentchanges', 'watchlist', 'page', 'extension_dummy_table' ],
301 [
302 'rc_id',
303 'rc_namespace',
304 'rc_title',
305 'rc_timestamp',
306 'rc_type',
307 'rc_deleted',
308 'wl_notificationtimestamp',
309 'rc_cur_id',
310 'rc_this_oldid',
311 'rc_last_oldid',
312 'extension_dummy_field',
313 ],
314 [
315 'wl_user' => 1,
316 '(rc_this_oldid=page_latest) OR (rc_type=3)',
317 'extension_dummy_cond',
318 ],
319 $this->isType( 'string' ),
320 [
321 'extension_dummy_option',
322 ],
323 [
324 'watchlist' => [
325 'JOIN',
326 [
327 'wl_namespace=rc_namespace',
328 'wl_title=rc_title'
329 ]
330 ],
331 'page' => [
332 'LEFT JOIN',
333 'rc_cur_id=page_id',
334 ],
335 'extension_dummy_join_cond' => [],
336 ]
337 )
338 ->willReturn( new FakeResultWrapper( [
339 (object)[
340 'rc_id' => 1,
341 'rc_namespace' => 0,
342 'rc_title' => 'Foo1',
343 'rc_timestamp' => '20151212010101',
344 'rc_type' => RC_NEW,
345 'rc_deleted' => 0,
346 'wl_notificationtimestamp' => '20151212010101',
347 ],
348 (object)[
349 'rc_id' => 2,
350 'rc_namespace' => 1,
351 'rc_title' => 'Foo2',
352 'rc_timestamp' => '20151212010102',
353 'rc_type' => RC_NEW,
354 'rc_deleted' => 0,
355 'wl_notificationtimestamp' => null,
356 ],
357 ] ) );
358
359 $user = $this->getMockUserWithId( 1 );
360
361 $mockExtension = $this->createMock( WatchedItemQueryServiceExtension::class );
362 $mockExtension->expects( $this->once() )
363 ->method( 'modifyWatchedItemsWithRCInfoQuery' )
364 ->with(
365 $this->identicalTo( $user ),
366 $this->isType( 'array' ),
367 $this->isInstanceOf( IDatabase::class ),
368 $this->isType( 'array' ),
369 $this->isType( 'array' ),
370 $this->isType( 'array' ),
371 $this->isType( 'array' ),
372 $this->isType( 'array' )
373 )
374 ->willReturnCallback( static function (
375 $user, $options, $db, &$tables, &$fields, &$conds, &$dbOptions, &$joinConds
376 ) {
377 $tables[] = 'extension_dummy_table';
378 $fields[] = 'extension_dummy_field';
379 $conds[] = 'extension_dummy_cond';
380 $dbOptions[] = 'extension_dummy_option';
381 $joinConds['extension_dummy_join_cond'] = [];
382 } );
383 $mockExtension->expects( $this->once() )
384 ->method( 'modifyWatchedItemsWithRCInfo' )
385 ->with(
386 $this->identicalTo( $user ),
387 $this->isType( 'array' ),
388 $this->isInstanceOf( IDatabase::class ),
389 $this->isType( 'array' ),
390 $this->anything(),
391 $this->anything() // Can't test for null here, PHPUnit applies this after the callback
392 )
393 ->willReturnCallback( function ( $user, $options, $db, &$items, $res, &$startFrom ) {
394 foreach ( $items as $i => &$item ) {
395 $item[1]['extension_dummy_field'] = $i;
396 }
397 unset( $item );
398
399 $this->assertNull( $startFrom );
400 $startFrom = [ '20160203123456', 42 ];
401 } );
402
403 $queryService = $this->newService( $mockDb );
404 TestingAccessWrapper::newFromObject( $queryService )->extensions = [ $mockExtension ];
405
406 $startFrom = null;
407 $items = $queryService->getWatchedItemsWithRecentChangeInfo(
408 $user, [], $startFrom
409 );
410
411 $this->assertIsArray( $items );
412 $this->assertCount( 2, $items );
413
414 foreach ( $items as [ $watchedItem, $recentChangeInfo ] ) {
415 $this->assertInstanceOf( WatchedItem::class, $watchedItem );
416 $this->assertIsArray( $recentChangeInfo );
417 }
418
419 $this->assertEquals(
420 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
421 $items[0][0]
422 );
423 $this->assertEquals(
424 [
425 'rc_id' => 1,
426 'rc_namespace' => 0,
427 'rc_title' => 'Foo1',
428 'rc_timestamp' => '20151212010101',
429 'rc_type' => RC_NEW,
430 'rc_deleted' => 0,
431 'extension_dummy_field' => 0,
432 ],
433 $items[0][1]
434 );
435
436 $this->assertEquals(
437 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
438 $items[1][0]
439 );
440 $this->assertEquals(
441 [
442 'rc_id' => 2,
443 'rc_namespace' => 1,
444 'rc_title' => 'Foo2',
445 'rc_timestamp' => '20151212010102',
446 'rc_type' => RC_NEW,
447 'rc_deleted' => 0,
448 'extension_dummy_field' => 1,
449 ],
450 $items[1][1]
451 );
452
453 $this->assertEquals( [ '20160203123456', 42 ], $startFrom );
454 }
455
456 public static function filterPatrolledOptionProvider() {
457 return [
458 'Patrolled' => [ WatchedItemQueryService::FILTER_PATROLLED ],
459 'Not patrolled' => [ WatchedItemQueryService::FILTER_NOT_PATROLLED ],
460 ];
461 }
462
463 /**
464 * @dataProvider filterPatrolledOptionProvider
465 */
466 public function testGetWatchedItemsWithRecentChangeInfo_filterPatrolledAndUserWithNoPatrolRights(
467 $filtersOption
468 ) {
469 $mockDb = $this->getMockDb();
470 $mockDb->expects( $this->once() )
471 ->method( 'select' )
472 ->with(
473 [ 'recentchanges', 'watchlist', 'page' ],
474 $this->isType( 'array' ),
475 [ 'wl_user' => 1, '(rc_this_oldid=page_latest) OR (rc_type=3)' ],
476 $this->isType( 'string' ),
477 $this->isType( 'array' ),
478 $this->isType( 'array' )
479 )
480 ->willReturn( new FakeResultWrapper( [] ) );
481
482 $user = $this->getMockUserWithId( 1, false );
483
484 $queryService = $this->newService( $mockDb );
485 $items = $queryService->getWatchedItemsWithRecentChangeInfo(
486 $user,
487 [ 'filters' => [ $filtersOption ] ]
488 );
489
490 $this->assertSame( [], $items );
491 }
492
493 public static function userPermissionRelatedExtraChecksProvider() {
494 return [
495 [
496 [],
497 'deletedhistory',
498 [],
499 [
500 '(rc_type != ' . RC_LOG . ') OR ((rc_deleted & ' . LogPage::DELETED_ACTION . ') != ' .
501 LogPage::DELETED_ACTION . ')'
502 ],
503 [],
504 ],
505 [
506 [],
507 'suppressrevision',
508 [],
509 [
510 '(rc_type != ' . RC_LOG . ') OR (' .
511 '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
512 ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
513 ],
514 [],
515 ],
516 [
517 [],
518 'viewsuppressed',
519 [],
520 [
521 '(rc_type != ' . RC_LOG . ') OR (' .
522 '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
523 ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
524 ],
525 [],
526 ],
527 [
528 [ 'onlyByUser' => 'SomeOtherUser' ],
529 'deletedhistory',
530 [ 'watchlist_actor' => 'actor' ],
531 [
532 'watchlist_actor.actor_name' => 'SomeOtherUser',
533 '(rc_deleted & ' . RevisionRecord::DELETED_USER . ') != ' . RevisionRecord::DELETED_USER,
534 '(rc_type != ' . RC_LOG . ') OR ((rc_deleted & ' . LogPage::DELETED_ACTION . ') != ' .
535 LogPage::DELETED_ACTION . ')'
536 ],
537 [ 'watchlist_actor' => [ 'JOIN', 'actor_id=rc_actor' ] ],
538 ],
539 [
540 [ 'onlyByUser' => 'SomeOtherUser' ],
541 'suppressrevision',
542 [ 'watchlist_actor' => 'actor' ],
543 [
544 'watchlist_actor.actor_name' => 'SomeOtherUser',
545 '(rc_deleted & ' . ( RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED ) . ') != ' .
546 ( RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED ),
547 '(rc_type != ' . RC_LOG . ') OR (' .
548 '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
549 ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
550 ],
551 [ 'watchlist_actor' => [ 'JOIN', 'actor_id=rc_actor' ] ],
552 ],
553 [
554 [ 'onlyByUser' => 'SomeOtherUser' ],
555 'viewsuppressed',
556 [ 'watchlist_actor' => 'actor' ],
557 [
558 'watchlist_actor.actor_name' => 'SomeOtherUser',
559 '(rc_deleted & ' . ( RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED ) . ') != ' .
560 ( RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED ),
561 '(rc_type != ' . RC_LOG . ') OR (' .
562 '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
563 ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
564 ],
565 [ 'watchlist_actor' => [ 'JOIN', 'actor_id=rc_actor' ] ],
566 ],
567 ];
568 }
569
570 /**
571 * @dataProvider userPermissionRelatedExtraChecksProvider
572 */
573 public function testGetWatchedItemsWithRecentChangeInfo_userPermissionRelatedExtraChecks(
574 array $options,
575 $notAllowedAction,
576 array $expectedExtraTables,
577 array $expectedExtraConds,
578 array $expectedExtraJoins
579 ) {
580 $commonConds = [ 'wl_user' => 1, '(rc_this_oldid=page_latest) OR (rc_type=3)' ];
581 $conds = array_merge( $commonConds, $expectedExtraConds );
582
583 $mockDb = $this->getMockDb();
584 $mockDb->expects( $this->once() )
585 ->method( 'select' )
586 ->with(
587 array_merge( [ 'recentchanges', 'watchlist', 'page' ], $expectedExtraTables ),
588 $this->isType( 'array' ),
589 $conds,
590 $this->isType( 'string' ),
591 $this->isType( 'array' ),
592 array_merge( [
593 'watchlist' => [ 'JOIN', [ 'wl_namespace=rc_namespace', 'wl_title=rc_title' ] ],
594 'page' => [ 'LEFT JOIN', 'rc_cur_id=page_id' ],
595 ], $expectedExtraJoins )
596 )
597 ->willReturn( new FakeResultWrapper( [] ) );
598
599 $user = $this->getMockUserWithId( 1, false, $notAllowedAction );
600
601 $queryService = $this->newService( $mockDb );
602 $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options );
603
604 $this->assertSame( [], $items );
605 }
606
607 public function testGetWatchedItemsWithRecentChangeInfo_allRevisionsOptionAndEmptyResult() {
608 $mockDb = $this->getMockDb();
609 $mockDb->expects( $this->once() )
610 ->method( 'select' )
611 ->with(
612 [ 'recentchanges', 'watchlist' ],
613 [
614 'rc_id',
615 'rc_namespace',
616 'rc_title',
617 'rc_timestamp',
618 'rc_type',
619 'rc_deleted',
620 'wl_notificationtimestamp',
621
622 'rc_cur_id',
623 'rc_this_oldid',
624 'rc_last_oldid',
625 ],
626 [ 'wl_user' => 1, ],
627 $this->isType( 'string' ),
628 [],
629 [
630 'watchlist' => [
631 'JOIN',
632 [
633 'wl_namespace=rc_namespace',
634 'wl_title=rc_title'
635 ]
636 ],
637 ]
638 )
639 ->willReturn( new FakeResultWrapper( [] ) );
640
641 $queryService = $this->newService( $mockDb );
642 $user = $this->getMockUserWithId( 1 );
643
644 $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, [ 'allRevisions' => true ] );
645
646 $this->assertSame( [], $items );
647 }
648
649 public function getWatchedItemsWithRecentChangeInfoInvalidOptionsProvider() {
650 return [
651 [
652 [ 'rcTypes' => [ 1337 ] ],
653 null,
654 'Bad value for parameter $options[\'rcTypes\']',
655 ],
656 [
657 [ 'rcTypes' => [ 'edit' ] ],
658 null,
659 'Bad value for parameter $options[\'rcTypes\']',
660 ],
661 [
662 [ 'rcTypes' => [ RC_EDIT, 1337 ] ],
663 null,
664 'Bad value for parameter $options[\'rcTypes\']',
665 ],
666 [
667 [ 'dir' => 'foo' ],
668 null,
669 'Bad value for parameter $options[\'dir\']',
670 ],
671 [
672 [ 'start' => '20151212010101' ],
673 null,
674 'Bad value for parameter $options[\'dir\']: must be provided',
675 ],
676 [
677 [ 'end' => '20151212010101' ],
678 null,
679 'Bad value for parameter $options[\'dir\']: must be provided',
680 ],
681 [
682 [],
683 [ '20151212010101', 123 ],
684 'Bad value for parameter $options[\'dir\']: must be provided',
685 ],
686 [
687 [ 'dir' => WatchedItemQueryService::DIR_OLDER ],
688 '20151212010101',
689 'Bad value for parameter $startFrom: must be a two-element array',
690 ],
691 [
692 [ 'dir' => WatchedItemQueryService::DIR_OLDER ],
693 [ '20151212010101' ],
694 'Bad value for parameter $startFrom: must be a two-element array',
695 ],
696 [
697 [ 'dir' => WatchedItemQueryService::DIR_OLDER ],
698 [ '20151212010101', 123, 'foo' ],
699 'Bad value for parameter $startFrom: must be a two-element array',
700 ],
701 [
702 [ 'watchlistOwner' => $this->getMockUserWithId( 2 ) ],
703 null,
704 'Bad value for parameter $options[\'watchlistOwnerToken\']',
705 ],
706 [
707 [ 'watchlistOwner' => 'Other User', 'watchlistOwnerToken' => 'some-token' ],
708 null,
709 'Bad value for parameter $options[\'watchlistOwner\']',
710 ],
711 ];
712 }
713
714 /**
715 * @dataProvider getWatchedItemsWithRecentChangeInfoInvalidOptionsProvider
716 */
717 public function testGetWatchedItemsWithRecentChangeInfo_invalidOptions(
718 array $options,
719 $startFrom,
720 $expectedInExceptionMessage
721 ) {
722 $mockDb = $this->getMockDb();
723 $mockDb->expects( $this->never() )
724 ->method( $this->anything() );
725
726 $queryService = $this->newService( $mockDb );
727 $user = $this->getMockUserWithId( 1 );
728
729 $this->expectException( InvalidArgumentException::class );
730 $this->expectExceptionMessage( $expectedInExceptionMessage );
731 $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options, $startFrom );
732 }
733
734 public function testGetWatchedItemsWithRecentChangeInfo_usedInGeneratorOptionAndEmptyResult() {
735 $mockDb = $this->getMockDb();
736 $mockDb->expects( $this->once() )
737 ->method( 'select' )
738 ->with(
739 [ 'recentchanges', 'watchlist', 'page' ],
740 [
741 'rc_id',
742 'rc_namespace',
743 'rc_title',
744 'rc_timestamp',
745 'rc_type',
746 'rc_deleted',
747 'wl_notificationtimestamp',
748 'rc_cur_id',
749 ],
750 [ 'wl_user' => 1, '(rc_this_oldid=page_latest) OR (rc_type=3)' ],
751 $this->isType( 'string' ),
752 [],
753 [
754 'watchlist' => [
755 'JOIN',
756 [
757 'wl_namespace=rc_namespace',
758 'wl_title=rc_title'
759 ]
760 ],
761 'page' => [
762 'LEFT JOIN',
763 'rc_cur_id=page_id',
764 ],
765 ]
766 )
767 ->willReturn( new FakeResultWrapper( [] ) );
768
769 $queryService = $this->newService( $mockDb );
770 $user = $this->getMockUserWithId( 1 );
771
772 $items = $queryService->getWatchedItemsWithRecentChangeInfo(
773 $user,
774 [ 'usedInGenerator' => true ]
775 );
776
777 $this->assertSame( [], $items );
778 }
779
780 public function testGetWatchedItemsWithRecentChangeInfo_usedInGeneratorAllRevisionsOptions() {
781 $mockDb = $this->getMockDb();
782 $mockDb->expects( $this->once() )
783 ->method( 'select' )
784 ->with(
785 [ 'recentchanges', 'watchlist' ],
786 [
787 'rc_id',
788 'rc_namespace',
789 'rc_title',
790 'rc_timestamp',
791 'rc_type',
792 'rc_deleted',
793 'wl_notificationtimestamp',
794 'rc_this_oldid',
795 ],
796 [ 'wl_user' => 1 ],
797 $this->isType( 'string' ),
798 [],
799 [
800 'watchlist' => [
801 'JOIN',
802 [
803 'wl_namespace=rc_namespace',
804 'wl_title=rc_title'
805 ]
806 ],
807 ]
808 )
809 ->willReturn( new FakeResultWrapper( [] ) );
810
811 $queryService = $this->newService( $mockDb );
812 $user = $this->getMockUserWithId( 1 );
813
814 $items = $queryService->getWatchedItemsWithRecentChangeInfo(
815 $user,
816 [ 'usedInGenerator' => true, 'allRevisions' => true, ]
817 );
818
819 $this->assertSame( [], $items );
820 }
821
822 public function testGetWatchedItemsWithRecentChangeInfo_watchlistOwnerOptionAndEmptyResult() {
823 $mockDb = $this->getMockDb();
824 $mockDb->expects( $this->once() )
825 ->method( 'select' )
826 ->with(
827 $this->isType( 'array' ),
828 $this->isType( 'array' ),
829 [
830 'wl_user' => 2,
831 '(rc_this_oldid=page_latest) OR (rc_type=3)',
832 ],
833 $this->isType( 'string' ),
834 $this->isType( 'array' ),
835 $this->isType( 'array' )
836 )
837 ->willReturn( new FakeResultWrapper( [] ) );
838
839 $user = $this->getMockUserWithId( 1 );
840 $otherUser = $this->getMockUserWithId( 2, true );
841 $userOptionsLookup = $this->createMock( UserOptionsLookup::class );
842 $userOptionsLookup->expects( $this->once() )
843 ->method( 'getOption' )
844 ->with( $otherUser, 'watchlisttoken' )
845 ->willReturn( '0123456789abcdef' );
846 $queryService = $this->newService( $mockDb, $userOptionsLookup );
847
848 $items = $queryService->getWatchedItemsWithRecentChangeInfo(
849 $user,
850 [ 'watchlistOwner' => $otherUser, 'watchlistOwnerToken' => '0123456789abcdef' ]
851 );
852
853 $this->assertSame( [], $items );
854 }
855
856 public function testGetWatchedItemsForUser() {
857 $mockDb = $this->getMockDb();
858 $mockDb->expects( $this->once() )
859 ->method( 'select' )
860 ->with(
861 [ 'watchlist' ],
862 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
863 [ 'wl_user' => 1 ]
864 )
865 ->willReturn( new FakeResultWrapper( [
866 (object)[
867 'wl_namespace' => 0,
868 'wl_title' => 'Foo1',
869 'wl_notificationtimestamp' => '20151212010101',
870 ],
871 (object)[
872 'wl_namespace' => 1,
873 'wl_title' => 'Foo2',
874 'wl_notificationtimestamp' => null,
875 ],
876 ] ) );
877
878 $queryService = $this->newService( $mockDb );
879 $user = $this->getMockUserWithId( 1 );
880
881 $items = $queryService->getWatchedItemsForUser( $user );
882
883 $this->assertIsArray( $items );
884 $this->assertCount( 2, $items );
885 $this->assertContainsOnlyInstancesOf( WatchedItem::class, $items );
886 $this->assertEquals(
887 new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
888 $items[0]
889 );
890 $this->assertEquals(
891 new WatchedItem( $user, new TitleValue( 1, 'Foo2' ), null ),
892 $items[1]
893 );
894 }
895
896 public static function provideGetWatchedItemsForUserOptions() {
897 return [
898 [
899 [ 'namespaceIds' => [ 0, 1 ], ],
900 [ 'wl_namespace' => [ 0, 1 ], ],
901 []
902 ],
903 [
904 [ 'sort' => WatchedItemQueryService::SORT_ASC, ],
905 [],
906 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
907 ],
908 [
909 [
910 'namespaceIds' => [ 0 ],
911 'sort' => WatchedItemQueryService::SORT_ASC,
912 ],
913 [ 'wl_namespace' => [ 0 ], ],
914 [ 'ORDER BY' => [ 'wl_title ASC' ] ]
915 ],
916 [
917 [ 'limit' => 10 ],
918 [],
919 [ 'LIMIT' => 10 ]
920 ],
921 [
922 [
923 'namespaceIds' => [ 0, "1; DROP TABLE watchlist;\n--" ],
924 'limit' => "10; DROP TABLE watchlist;\n--",
925 ],
926 [ 'wl_namespace' => [ 0, 1 ], ],
927 [ 'LIMIT' => 10 ]
928 ],
929 [
930 [ 'filter' => WatchedItemQueryService::FILTER_CHANGED ],
931 [ 'wl_notificationtimestamp IS NOT NULL' ],
932 []
933 ],
934 [
935 [ 'filter' => WatchedItemQueryService::FILTER_NOT_CHANGED ],
936 [ 'wl_notificationtimestamp IS NULL' ],
937 []
938 ],
939 [
940 [ 'sort' => WatchedItemQueryService::SORT_DESC, ],
941 [],
942 [ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
943 ],
944 [
945 [
946 'namespaceIds' => [ 0 ],
947 'sort' => WatchedItemQueryService::SORT_DESC,
948 ],
949 [ 'wl_namespace' => [ 0 ], ],
950 [ 'ORDER BY' => [ 'wl_title DESC' ] ]
951 ],
952 ];
953 }
954
955 /**
956 * @dataProvider provideGetWatchedItemsForUserOptions
957 */
958 public function testGetWatchedItemsForUser_optionsAndEmptyResult(
959 array $options,
960 array $expectedConds,
961 array $expectedDbOptions
962 ) {
963 $mockDb = $this->getMockDb();
964 $user = $this->getMockUserWithId( 1 );
965
966 $expectedConds = array_merge( [ 'wl_user' => 1 ], $expectedConds );
967 $mockDb->expects( $this->once() )
968 ->method( 'select' )
969 ->with(
970 [ 'watchlist' ],
971 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
972 $expectedConds,
973 $this->isType( 'string' ),
974 $expectedDbOptions
975 )
976 ->willReturn( new FakeResultWrapper( [] ) );
977
978 $queryService = $this->newService( $mockDb );
979
980 $items = $queryService->getWatchedItemsForUser( $user, $options );
981 $this->assertSame( [], $items );
982 }
983
984 public static function provideGetWatchedItemsForUser_fromUntilStartFromOptions() {
985 return [
986 [
987 [
988 'from' => new TitleValue( 0, 'SomeDbKey' ),
989 'sort' => WatchedItemQueryService::SORT_ASC
990 ],
991 [ "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'SomeDbKey'))", ],
992 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
993 ],
994 [
995 [
996 'from' => new TitleValue( 0, 'SomeDbKey' ),
997 'sort' => WatchedItemQueryService::SORT_DESC,
998 ],
999 [ "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeDbKey'))", ],
1000 [ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
1001 ],
1002 [
1003 [
1004 'until' => new TitleValue( 0, 'SomeDbKey' ),
1005 'sort' => WatchedItemQueryService::SORT_ASC
1006 ],
1007 [ "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeDbKey'))", ],
1008 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1009 ],
1010 [
1011 [
1012 'until' => new TitleValue( 0, 'SomeDbKey' ),
1013 'sort' => WatchedItemQueryService::SORT_DESC
1014 ],
1015 [ "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'SomeDbKey'))", ],
1016 [ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
1017 ],
1018 [
1019 [
1020 'from' => new TitleValue( 0, 'AnotherDbKey' ),
1021 'until' => new TitleValue( 0, 'SomeOtherDbKey' ),
1022 'startFrom' => new TitleValue( 0, 'SomeDbKey' ),
1023 'sort' => WatchedItemQueryService::SORT_ASC
1024 ],
1025 [
1026 "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'AnotherDbKey'))",
1027 "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeOtherDbKey'))",
1028 "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'SomeDbKey'))",
1029 ],
1030 [ 'ORDER BY' => [ 'wl_namespace ASC', 'wl_title ASC' ] ]
1031 ],
1032 [
1033 [
1034 'from' => new TitleValue( 0, 'SomeOtherDbKey' ),
1035 'until' => new TitleValue( 0, 'AnotherDbKey' ),
1036 'startFrom' => new TitleValue( 0, 'SomeDbKey' ),
1037 'sort' => WatchedItemQueryService::SORT_DESC
1038 ],
1039 [
1040 "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeOtherDbKey'))",
1041 "wl_namespace > '0' OR (wl_namespace = '0' AND (wl_title >= 'AnotherDbKey'))",
1042 "wl_namespace < '0' OR (wl_namespace = '0' AND (wl_title <= 'SomeDbKey'))",
1043 ],
1044 [ 'ORDER BY' => [ 'wl_namespace DESC', 'wl_title DESC' ] ]
1045 ],
1046 ];
1047 }
1048
1049 /**
1050 * @dataProvider provideGetWatchedItemsForUser_fromUntilStartFromOptions
1051 */
1052 public function testGetWatchedItemsForUser_fromUntilStartFromOptions(
1053 array $options,
1054 array $expectedConds,
1055 array $expectedDbOptions
1056 ) {
1057 $user = $this->getMockUserWithId( 1 );
1058
1059 $expectedConds = array_merge( [ 'wl_user' => 1 ], $expectedConds );
1060
1061 $mockDb = $this->getMockDb();
1062 $mockDb->method( 'makeList' )
1063 ->with(
1064 $this->isType( 'array' ),
1065 $this->isType( 'int' )
1066 )
1067 ->willReturnCallback( static function ( $a, $conj ) {
1068 $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
1069 return implode( $sqlConj, array_map( static function ( $s ) {
1070 return '(' . $s . ')';
1071 }, $a
1072 ) );
1073 } );
1074 $mockDb->expects( $this->once() )
1075 ->method( 'select' )
1076 ->with(
1077 [ 'watchlist' ],
1078 [ 'wl_namespace', 'wl_title', 'wl_notificationtimestamp' ],
1079 $expectedConds,
1080 $this->isType( 'string' ),
1081 $expectedDbOptions
1082 )
1083 ->willReturn( new FakeResultWrapper( [] ) );
1084
1085 $queryService = $this->newService( $mockDb );
1086
1087 $items = $queryService->getWatchedItemsForUser( $user, $options );
1088 $this->assertSame( [], $items );
1089 }
1090
1091 public static function getWatchedItemsForUserInvalidOptionsProvider() {
1092 return [
1093 [
1094 [ 'sort' => 'foo' ],
1095 'Bad value for parameter $options[\'sort\']'
1096 ],
1097 [
1098 [ 'filter' => 'foo' ],
1099 'Bad value for parameter $options[\'filter\']'
1100 ],
1101 [
1102 [ 'from' => new TitleValue( 0, 'SomeDbKey' ), ],
1103 'Bad value for parameter $options[\'sort\']: must be provided'
1104 ],
1105 [
1106 [ 'until' => new TitleValue( 0, 'SomeDbKey' ), ],
1107 'Bad value for parameter $options[\'sort\']: must be provided'
1108 ],
1109 [
1110 [ 'startFrom' => new TitleValue( 0, 'SomeDbKey' ), ],
1111 'Bad value for parameter $options[\'sort\']: must be provided'
1112 ],
1113 ];
1114 }
1115
1116 /**
1117 * @dataProvider getWatchedItemsForUserInvalidOptionsProvider
1118 */
1119 public function testGetWatchedItemsForUser_invalidOptionThrowsException(
1120 array $options,
1121 $expectedInExceptionMessage
1122 ) {
1123 $queryService = $this->newService( $this->getMockDb() );
1124
1125 $this->expectException( InvalidArgumentException::class );
1126 $this->expectExceptionMessage( $expectedInExceptionMessage );
1127 $queryService->getWatchedItemsForUser( $this->getMockUserWithId( 1 ), $options );
1128 }
1129
1130 public function testGetWatchedItemsForUser_userNotAllowedToViewWatchlist() {
1131 $mockDb = $this->getMockDb();
1132
1133 $mockDb->expects( $this->never() )
1134 ->method( $this->anything() );
1135
1136 $queryService = $this->newService( $mockDb );
1137
1138 $items = $queryService->getWatchedItemsForUser(
1139 new UserIdentityValue( 0, 'AnonUser' ) );
1140 $this->assertSame( [], $items );
1141 }
1142
1143 }