Debian Codegraph @

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

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Page\PageIdentity;
4 use MediaWiki\Page\PageIdentityValue;
5 use MediaWiki\Page\PageReference;
6 use MediaWiki\Page\PageReferenceValue;
7 use MediaWiki\Page\WikiPageFactory;
8 use MediaWiki\Revision\RevisionLookup;
9 use MediaWiki\Tests\Unit\DummyServicesTrait;
10 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
11 use MediaWiki\Title\Title;
12 use MediaWiki\Title\TitleValue;
13 use MediaWiki\User\TalkPageNotificationManager;
14 use MediaWiki\User\User;
15 use MediaWiki\User\UserFactory;
16 use MediaWiki\User\UserIdentity;
17 use MediaWiki\User\UserIdentityValue;
18 use MediaWiki\Watchlist\WatchedItem;
19 use MediaWiki\Watchlist\WatchedItemStoreInterface;
20 use MediaWiki\Watchlist\WatchlistManager;
21
22 /**
23 * @covers \MediaWiki\Watchlist\WatchlistManager
24 *
25 * Cannot use the name `WatchlistManagerTest`, already used by the integration test
26 * @phpcs:disable MediaWiki.Files.ClassMatchesFilename
27 *
28 * @author DannyS712
29 */
30 class WatchlistManagerUnitTest extends MediaWikiUnitTestCase {
31 use DummyServicesTrait;
32 use MockTitleTrait;
33 use MockAuthorityTrait;
34
35 private function getWikiPageFactory() {
36 // Needed so that we can test addWatchIgnoringRights and removeWatchIgnoringRights,
37 // which convert a PageIdentity to a WikiPage to use WikiPage::getTitle so that
38 // the Title (a LinkTarget) can be used for interacting with the NamespaceInfo
39 // service
40 $wikiPageFactory = $this->createMock( WikiPageFactory::class );
41 $wikiPageFactory->method( 'newFromTitle' )->willReturnCallback(
42 function ( PageIdentity $pageIdentity ) {
43 $title = Title::newFromPageReference( $pageIdentity );
44 $wikiPage = $this->createMock( WikiPage::class );
45 $wikiPage->method( 'getTitle' )->willReturn( $title );
46 return $wikiPage;
47 }
48 );
49 return $wikiPageFactory;
50 }
51
52 private function getManager( array $params = [] ) {
53 $options = $params['options'] ?? [
54 WatchlistManager::OPTION_ENOTIF => false,
55 ];
56
57 $talkPageNotificationManager = $params['talkPageNotificationManager'] ??
58 $this->createNoOpMock( TalkPageNotificationManager::class );
59
60 $watchedItemStore = $params['watchedItemStore'] ??
61 $this->createNoOpAbstractMock( WatchedItemStoreInterface::class );
62
63 $userFactory = $params['userFactory'] ??
64 $this->createNoOpMock( UserFactory::class );
65
66 $hookContainer = $params['hookContainer'] ?? $this->createHookContainer();
67
68 $nsInfo = $this->getDummyNamespaceInfo( [
69 'hookContainer' => $hookContainer, // in case any of the hooks matter
70 ] );
71
72 return new WatchlistManager(
73 $options,
74 $hookContainer,
75 $this->getDummyReadOnlyMode( $params['readOnly'] ?? false ),
76 $this->createMock( RevisionLookup::class ),
77 $talkPageNotificationManager,
78 $watchedItemStore,
79 $userFactory,
80 $nsInfo,
81 $this->getWikiPageFactory()
82 );
83 }
84
85 private function getAuthorityAndUserFactory( UserIdentity $userIdentity, array $permissions = [] ) {
86 $authority = $this->mockUserAuthorityWithPermissions(
87 $userIdentity,
88 $permissions
89 );
90
91 $userFactory = $this->createMock( UserFactory::class );
92 $userFactory->method( 'newFromUserIdentity' )
93 ->willReturnCallback( function ( $userIdentity ) use ( $permissions ) {
94 $user = $this->createMock( User::class );
95 $user->method( 'isAllowed' )
96 ->willReturnCallback( static function ( $permission ) use ( $permissions ) {
97 return in_array( $permission, $permissions );
98 } );
99 $user->method( 'getUser' )
100 ->willReturn( $userIdentity );
101 return $user;
102 } );
103
104 return [ $authority, $userFactory ];
105 }
106
107 public function testClearAllUserNotifications_readOnly() {
108 // ********** Code path #1 **********
109 // Early return: read only mode
110
111 $userIdentity = new UserIdentityValue( 100, 'User Name' );
112 [ $authority, ] = $this->getAuthorityAndUserFactory( $userIdentity );
113
114 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
115 $watchedItemStore->expects( $this->never() )
116 ->method( 'resetAllNotificationTimestampsForUser' );
117
118 $manager = $this->getManager( [
119 'readOnly' => true,
120 'watchedItemStore' => $watchedItemStore
121 ] );
122
123 $manager->clearAllUserNotifications( $userIdentity );
124 $manager->clearAllUserNotifications( $authority );
125 }
126
127 public function testClearAllUserNotifications_noPerms() {
128 // ********** Code path #2 **********
129 // Early return: User lacks `editmywatchlist`
130
131 $userIdentity = new UserIdentityValue( 100, 'User Name' );
132 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( $userIdentity );
133
134 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
135 $watchedItemStore->expects( $this->never() )
136 ->method( 'resetAllNotificationTimestampsForUser' );
137
138 $talkPageNotificationManager = $this->createMock( TalkPageNotificationManager::class );
139 $talkPageNotificationManager->expects( $this->exactly( 2 ) )
140 ->method( 'removeUserHasNewMessages' )
141 ->with( $userIdentity );
142
143 $manager = $this->getManager( [
144 'watchedItemStore' => $watchedItemStore,
145 'talkPageNotificationManager' => $talkPageNotificationManager,
146 'userFactory' => $userFactory
147 ] );
148
149 $manager->clearAllUserNotifications( $userIdentity );
150 $manager->clearAllUserNotifications( $authority );
151 }
152
153 public function testClearAllUserNotifications_configDisabled() {
154 // ********** Code path #3 **********
155 // Early return: config with OPTION_ENOTIF false
156
157 $userIdentity = new UserIdentityValue( 100, 'User Name' );
158 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
159 $userIdentity,
160 [ 'editmywatchlist' ]
161 );
162
163 $talkPageNotificationManager = $this->createMock( TalkPageNotificationManager::class );
164 $talkPageNotificationManager->expects( $this->exactly( 2 ) )
165 ->method( 'removeUserHasNewMessages' );
166
167 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
168 $watchedItemStore->expects( $this->never() )
169 ->method( 'resetAllNotificationTimestampsForUser' );
170
171 $manager = $this->getManager( [
172 'watchedItemStore' => $watchedItemStore,
173 'talkPageNotificationManager' => $talkPageNotificationManager,
174 'userFactory' => $userFactory
175 ] );
176
177 $manager->clearAllUserNotifications( $userIdentity );
178 $manager->clearAllUserNotifications( $authority );
179 }
180
181 public function testClearAllUserNotifications_falseyId() {
182 // ********** Code path #4 **********
183 // Early return: user's id is falsey
184
185 $options = [
186 WatchlistManager::OPTION_ENOTIF => true
187 ];
188
189 $userIdentity = new UserIdentityValue( 0, 'User Name' );
190 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
191 $userIdentity,
192 [ 'editmywatchlist' ]
193 );
194
195 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
196 $watchedItemStore->expects( $this->never() )
197 ->method( 'resetAllNotificationTimestampsForUser' );
198
199 $manager = $this->getManager( [
200 'options' => $options,
201 'watchedItemStore' => $watchedItemStore,
202 'userFactory' => $userFactory
203 ] );
204
205 $manager->clearAllUserNotifications( $userIdentity );
206 $manager->clearAllUserNotifications( $authority );
207 }
208
209 public function testClearAllUserNotifications() {
210 // ********** Code path #5 **********
211 // No early returns
212
213 $options = [
214 WatchlistManager::OPTION_ENOTIF => true
215 ];
216
217 $userIdentity = new UserIdentityValue( 100, 'User Name' );
218 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
219 $userIdentity,
220 [ 'editmywatchlist' ]
221 );
222
223 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
224 $watchedItemStore->expects( $this->exactly( 2 ) )
225 ->method( 'resetAllNotificationTimestampsForUser' );
226
227 $manager = $this->getManager( [
228 'options' => $options,
229 'watchedItemStore' => $watchedItemStore,
230 'userFactory' => $userFactory
231 ] );
232
233 $manager->clearAllUserNotifications( $userIdentity );
234 $manager->clearAllUserNotifications( $authority );
235 }
236
237 public function provideTestPageFactory() {
238 yield [ static function ( $pageId, $namespace, $dbKey ) {
239 return new TitleValue( $namespace, $dbKey );
240 } ];
241 yield [ static function ( $pageId, $namespace, $dbKey ) {
242 return new PageIdentityValue( $pageId, $namespace, $dbKey, PageIdentityValue::LOCAL );
243 } ];
244 yield [ function ( $pageId, $namespace, $dbKey ) {
245 return $this->makeMockTitle( $dbKey, [
246 'id' => $pageId,
247 'namespace' => $namespace
248 ] );
249 } ];
250 }
251
252 /**
253 * @dataProvider provideTestPageFactory
254 */
255 public function testClearTitleUserNotifications_readOnly( $testPageFactory ) {
256 // ********** Code path #1 **********
257 // Early return: read only mode
258
259 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
260
261 $userIdentity = new UserIdentityValue( 100, 'User Name' );
262 [ $authority, ] = $this->getAuthorityAndUserFactory( $userIdentity );
263
264 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
265 $watchedItemStore->expects( $this->never() )
266 ->method( 'resetNotificationTimestamp' );
267
268 $manager = $this->getManager( [
269 'readOnly' => true,
270 'watchedItemStore' => $watchedItemStore
271 ] );
272
273 $manager->clearTitleUserNotifications( $userIdentity, $title );
274 $manager->clearTitleUserNotifications( $authority, $title );
275 }
276
277 /**
278 * @dataProvider provideTestPageFactory
279 */
280 public function testClearTitleUserNotifications_noPerms( $testPageFactory ) {
281 // ********** Code path #2 **********
282 // Early return: User lacks `editmywatchlist`
283
284 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
285
286 $userIdentity = new UserIdentityValue( 100, 'User Name' );
287 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory( $userIdentity );
288
289 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
290 $watchedItemStore->expects( $this->never() )
291 ->method( 'resetNotificationTimestamp' );
292
293 $manager = $this->getManager( [
294 'watchedItemStore' => $watchedItemStore,
295 'userFactory' => $userFactory
296 ] );
297
298 $manager->clearTitleUserNotifications( $userIdentity, $title );
299 $manager->clearTitleUserNotifications( $authority, $title );
300 }
301
302 /**
303 * @dataProvider provideTestPageFactory
304 */
305 public function testClearTitleUserNotifications_configDisabled( $testPageFactory ) {
306 // ********** Code path #3 **********
307 // Early return: config with OPTION_ENOTIF false
308
309 $title = $testPageFactory( 100, NS_USER_TALK, 'PageTitleGoesHere' );
310
311 $userIdentity = new UserIdentityValue( 100, 'User Name' );
312 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
313 $userIdentity,
314 [ 'editmywatchlist' ]
315 );
316
317 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
318 $watchedItemStore->expects( $this->never() )
319 ->method( 'resetNotificationTimestamp' );
320
321 $manager = $this->getManager( [
322 'watchedItemStore' => $watchedItemStore,
323 'userFactory' => $userFactory
324 ] );
325
326 $manager->clearTitleUserNotifications( $userIdentity, $title );
327 $manager->clearTitleUserNotifications( $authority, $title );
328 }
329
330 /**
331 * @dataProvider provideTestPageFactory
332 */
333 public function testClearTitleUserNotifications_notRegistered( $testPageFactory ) {
334 // ********** Code path #4 **********
335 // Early return: user is not registered
336
337 $title = $testPageFactory( 100, NS_USER_TALK, 'PageTitleGoesHere' );
338
339 $options = [
340 WatchlistManager::OPTION_ENOTIF => true,
341 ];
342
343 $userIdentity = new UserIdentityValue( 0, 'User Name' );
344 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
345 $userIdentity,
346 [ 'editmywatchlist' ]
347 );
348
349 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
350 $watchedItemStore->expects( $this->never() )
351 ->method( 'resetNotificationTimestamp' );
352
353 $manager = $this->getManager( [
354 'options' => $options,
355 'userFactory' => $userFactory,
356 'watchedItemStore' => $watchedItemStore
357 ] );
358
359 $manager->clearTitleUserNotifications( $userIdentity, $title );
360 $manager->clearTitleUserNotifications( $authority, $title );
361 }
362
363 /**
364 * @dataProvider provideTestPageFactory
365 */
366 public function testClearTitleUserNotifications( $testPageFactory ) {
367 // ********** Code path #5 **********
368 // No early returns; resetNotificationTimestamp is called
369
370 $title = $testPageFactory( 100, NS_USER_TALK, 'PageTitleGoesHere' );
371
372 $options = [
373 WatchlistManager::OPTION_ENOTIF => true,
374 ];
375
376 $userIdentity = new UserIdentityValue( 100, 'User Name' );
377 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
378 $userIdentity,
379 [ 'editmywatchlist' ]
380 );
381
382 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
383 $watchedItemStore->expects( $this->exactly( 2 ) )
384 ->method( 'resetNotificationTimestamp' );
385
386 $manager = $this->getManager( [
387 'options' => $options,
388 'userFactory' => $userFactory,
389 'watchedItemStore' => $watchedItemStore
390 ] );
391
392 $manager->clearTitleUserNotifications( $userIdentity, $title );
393 $manager->clearTitleUserNotifications( $authority, $title );
394 }
395
396 /**
397 * @dataProvider provideTestPageFactory
398 */
399 public function testGetTitleNotificationTimestamp_falseyId( $testPageFactory ) {
400 // ********** Code path #1 **********
401 // Early return: user id is falsey
402
403 $userIdentity = new UserIdentityValue( 0, 'User Name' );
404
405 $title = $testPageFactory( 100, 0, 'SomeDbKey' );
406
407 $manager = $this->getManager();
408
409 $res = $manager->getTitleNotificationTimestamp( $userIdentity, $title );
410
411 $this->assertFalse( $res, 'Early return for anonymous users is false' );
412 }
413
414 /**
415 * @dataProvider provideTestPageFactory
416 */
417 public function testGetTitleNotificationTimestamp_timestamp( $testPageFactory ) {
418 // ********** Code path #2 **********
419 // Early return: value is already cached - will be tested after #3 because
420 // an entry in the cache is needed (duh)
421
422 // ********** Code path #3 **********
423 // Actually check watchedItemStore, v.1-a - returns a WatchedItem with a timestamp
424 // From here on a cache key will be generated each time
425
426 $userIdentity = new UserIdentityValue( 100, 'User Name' );
427
428 $title = $testPageFactory( 100, NS_MAIN, 'Page_db_Key_goesHere' );
429
430 $watchedItem = $this->createMock( WatchedItem::class );
431 $watchedItem->expects( $this->once() )
432 ->method( 'getNotificationTimestamp' )
433 ->willReturn( 'stringTimestamp' );
434
435 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
436 $watchedItemStore->expects( $this->once() )
437 ->method( 'getWatchedItem' )
438 ->with( $userIdentity, $title )
439 ->willReturn( $watchedItem );
440
441 $manager = $this->getManager( [
442 'watchedItemStore' => $watchedItemStore
443 ] );
444
445 $res = $manager->getTitleNotificationTimestamp( $userIdentity, $title );
446
447 $this->assertSame(
448 'stringTimestamp',
449 $res,
450 'if getWatchedItem returns a WatchedItem, that object\'s timestamp is returned'
451 );
452
453 // ********** Code path #2 **********
454 // Actually test code path #2 now that there is something in the cache
455 // use the same $manager instance (so the value is in the cache, duh)
456 // all of the same expectations apply - getWatchedItem shouldn't be called again, and
457 // so was only expecting to be called ->once() above
458 $res = $manager->getTitleNotificationTimestamp( $userIdentity, $title );
459 $this->assertSame(
460 'stringTimestamp',
461 $res,
462 'if the timestamp is cached getWatchedItem is not called again'
463 );
464 }
465
466 /**
467 * @dataProvider provideTestPageFactory
468 */
469 public function testGetTitleNotificationTimestamp_null( $testPageFactory ) {
470 // ********** Code path #4 **********
471 // Actually check watchedItemStore, v.1-b - returns a WatchedItem with null
472
473 $userIdentity = new UserIdentityValue( 100, 'User Name' );
474
475 $title = $testPageFactory( 100, NS_MAIN, 'Page_db_Key_goesHere' );
476
477 $watchedItem = $this->createMock( WatchedItem::class );
478 $watchedItem->expects( $this->once() )
479 ->method( 'getNotificationTimestamp' )
480 ->willReturn( null );
481
482 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
483 $watchedItemStore->expects( $this->once() )
484 ->method( 'getWatchedItem' )
485 ->with( $userIdentity, $title )
486 ->willReturn( $watchedItem );
487
488 $manager = $this->getManager( [
489 'watchedItemStore' => $watchedItemStore
490 ] );
491
492 $res = $manager->getTitleNotificationTimestamp( $userIdentity, $title );
493
494 $this->assertNull( $res, 'WatchedItem can return null instead of a timestamp' );
495 }
496
497 /**
498 * @dataProvider provideTestPageFactory
499 */
500 public function testGetTitleNotificationTimestamp_false( $testPageFactory ) {
501 // ********** Code path #5 **********
502 // Actually check watchedItemStore, v.2 - returns false
503
504 $userIdentity = new UserIdentityValue( 100, 'User Name' );
505
506 $title = $testPageFactory( 100, NS_MAIN, 'Page_db_Key_goesHere' );
507
508 $watchedItemStore = $this->createMock( WatchedItemStoreInterface::class );
509 $watchedItemStore->expects( $this->once() )
510 ->method( 'getWatchedItem' )
511 ->with( $userIdentity, $title )
512 ->willReturn( false );
513
514 $manager = $this->getManager( [
515 'watchedItemStore' => $watchedItemStore
516 ] );
517
518 $res = $manager->getTitleNotificationTimestamp( $userIdentity, $title );
519
520 $this->assertFalse(
521 $res,
522 'getWatchedItem can return false if the item is not watched'
523 );
524 }
525
526 public function testIsWatchable() {
527 $manager = $this->getManager( [ 'readOnly' => 'never' ] );
528
529 $target = new PageReferenceValue( NS_USER, __METHOD__, PageReference::LOCAL );
530 $this->assertTrue( $manager->isWatchable( $target ) );
531 }
532
533 public static function provideNotIsWatchable() {
534 yield [ new PageReferenceValue( NS_SPECIAL, 'Contributions', PageReference::LOCAL ) ];
535 yield [ Title::makeTitle( NS_MAIN, '', 'References' ) ];
536 yield [ Title::makeTitle( NS_MAIN, 'Foo', '', 'acme' ) ];
537 }
538
539 /**
540 * @dataProvider provideNotIsWatchable
541 */
542 public function testNotIsWatchable( $target ) {
543 $manager = $this->getManager( [ 'readOnly' => 'never' ] );
544
545 $this->assertFalse( $manager->isWatchable( $target ) );
546 }
547
548 /**
549 * @covers \MediaWiki\Watchlist\WatchlistManager::setWatch()
550 */
551 public function testSetWatchWithExpiry() {
552 // Already watched, but we're adding an expiry so 'addWatch' should be called.
553 $userIdentity = new UserIdentityValue( 100, 'User Name' );
554 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
555 $userIdentity,
556 [ 'editmywatchlist' ]
557 );
558 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
559
560 $watchedItemStore = $this->getDummyWatchedItemStore();
561 $watchedItemStore->expects( $this->exactly( 4 ) )->method( 'addWatch' ); // watch page and its talk page twice
562 $watchedItemStore->expects( $this->never() )->method( 'removeWatch' );
563
564 $watchlistManager = $this->getManager( [
565 'userFactory' => $userFactory,
566 'watchedItemStore' => $watchedItemStore,
567 ] );
568
569 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
570
571 $status = $watchlistManager->setWatch( true, $authority, $title, '1 week' );
572
573 $this->assertStatusGood( $status );
574 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
575 }
576
577 /**
578 * @covers \MediaWiki\Watchlist\WatchlistManager::setWatch()
579 */
580 public function testSetWatchUserNotLoggedIn() {
581 $userIdentity = new UserIdentityValue( 0, 'User Name' );
582 $performer = $this->mockUserAuthorityWithPermissions( $userIdentity, [ 'editmywatchlist' ] );
583 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
584
585 $watchedItemStore = $this->getDummyWatchedItemStore();
586 $watchedItemStore->expects( $this->never() )->method( 'addWatch' );
587 $watchedItemStore->expects( $this->never() )->method( 'removeWatch' );
588
589 $watchlistManager = $this->getManager( [
590 'watchedItemStore' => $watchedItemStore,
591 ] );
592
593 $status = $watchlistManager->setWatch( true, $performer, $title );
594
595 // returns immediately with no error if not logged in
596 $this->assertStatusGood( $status );
597 }
598
599 /**
600 * @covers \MediaWiki\Watchlist\WatchlistManager::setWatch()
601 */
602 public function testSetWatchSkipsIfAlreadyWatched() {
603 $userIdentity = new UserIdentityValue( 100, 'User Name' );
604 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
605 $userIdentity,
606 [ 'editmywatchlist' ]
607 );
608 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
609
610 $watchedItemStore = $this->getDummyWatchedItemStore();
611 $watchedItemStore->expects( $this->exactly( 2 ) )->method( 'addWatch' ); // watch page and its talk page
612 $watchedItemStore->expects( $this->never() )->method( 'removeWatch' );
613
614 $watchlistManager = $this->getManager( [
615 'userFactory' => $userFactory,
616 'watchedItemStore' => $watchedItemStore,
617 ] );
618
619 $expiry = '99990123000000';
620 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title, $expiry );
621
622 // Same expiry
623 $status = $watchlistManager->setWatch( true, $authority, $title, $expiry );
624
625 $this->assertStatusGood( $status );
626 }
627
628 /**
629 * @covers \MediaWiki\Watchlist\WatchlistManager::setWatch()
630 */
631 public function testSetWatchSkipsIfAlreadyUnWatched() {
632 $userIdentity = new UserIdentityValue( 100, 'User Name' );
633 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
634 $userIdentity,
635 [ 'editmywatchlist' ]
636 );
637 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
638
639 $watchedItemStore = $this->getDummyWatchedItemStore();
640 $watchedItemStore->expects( $this->never() )->method( 'addWatch' );
641 $watchedItemStore->expects( $this->never() )->method( 'removeWatch' );
642
643 $watchlistManager = $this->getManager( [
644 'userFactory' => $userFactory,
645 'watchedItemStore' => $watchedItemStore,
646 ] );
647
648 $status = $watchlistManager->setWatch( false, $authority, $title );
649
650 $this->assertStatusGood( $status );
651 }
652
653 /**
654 * @covers \MediaWiki\Watchlist\WatchlistManager::setWatch()
655 */
656 public function testSetWatchWatchesIfWatch() {
657 $userIdentity = new UserIdentityValue( 100, 'User Name' );
658 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
659 $userIdentity,
660 [ 'editmywatchlist' ]
661 );
662 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
663
664 $watchedItemStore = $this->getDummyWatchedItemStore();
665 $watchlistManager = $this->getManager( [
666 'userFactory' => $userFactory,
667 'watchedItemStore' => $watchedItemStore,
668 ] );
669
670 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
671
672 $status = $watchlistManager->setWatch( true, $authority, $title );
673
674 $this->assertStatusGood( $status );
675 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
676 }
677
678 /**
679 * @covers \MediaWiki\Watchlist\WatchlistManager::setWatch()
680 */
681 public function testSetWatchUnwatchesIfUnwatch() {
682 $userIdentity = new UserIdentityValue( 100, 'User Name' );
683 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
684 $userIdentity,
685 [ 'editmywatchlist' ]
686 );
687 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
688
689 $watchedItemStore = $this->getDummyWatchedItemStore();
690 $watchlistManager = $this->getManager( [
691 'userFactory' => $userFactory,
692 'watchedItemStore' => $watchedItemStore,
693 ] );
694
695 $status = $watchlistManager->setWatch( false, $authority, $title );
696
697 $this->assertStatusGood( $status );
698 $this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
699 }
700
701 /**
702 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatchIgnoringRights()
703 */
704 public function testAddWatchNoCheckRights() {
705 $userIdentity = new UserIdentityValue( 100, 'User Name' );
706 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
707 $userIdentity,
708 []
709 );
710 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
711
712 $watchedItemStore = $this->getDummyWatchedItemStore();
713 $watchlistManager = $this->getManager( [
714 'userFactory' => $userFactory,
715 'watchedItemStore' => $watchedItemStore,
716 ] );
717
718 $actual = $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
719
720 $this->assertStatusGood( $actual );
721 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
722 }
723
724 /**
725 * @covers \MediaWiki\Watchlist\WatchlistManager::addWatch()
726 */
727 public function testAddWatchSuccess() {
728 $userIdentity = new UserIdentityValue( 100, 'User Name' );
729 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
730 $userIdentity,
731 [ 'editmywatchlist' ]
732 );
733 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
734
735 $watchedItemStore = $this->getDummyWatchedItemStore();
736 $watchlistManager = $this->getManager( [
737 'userFactory' => $userFactory,
738 'watchedItemStore' => $watchedItemStore,
739 ] );
740
741 $actual = $watchlistManager->addWatch( $authority, $title );
742
743 $this->assertStatusGood( $actual );
744 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
745 }
746
747 /**
748 * @covers \MediaWiki\Watchlist\WatchlistManager::removeWatch()
749 */
750 public function testRemoveWatchUserHookAborted() {
751 $userIdentity = new UserIdentityValue( 100, 'User Name' );
752 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
753 $userIdentity,
754 [ 'editmywatchlist' ]
755 );
756 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
757
758 $hookContainer = $this->createHookContainer( [
759 'UnwatchArticle' => static function () {
760 return false;
761 },
762 ] );
763 $watchedItemStore = $this->getDummyWatchedItemStore();
764 $watchlistManager = $this->getManager( [
765 'hookContainer' => $hookContainer,
766 'userFactory' => $userFactory,
767 'watchedItemStore' => $watchedItemStore,
768 ] );
769
770 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
771
772 $status = $watchlistManager->removeWatch( $authority, $title );
773
774 $this->assertStatusError( 'hookaborted', $status );
775 $this->assertTrue( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
776 }
777
778 /**
779 * @covers \MediaWiki\Watchlist\WatchlistManager::removeWatch()
780 */
781 public function testRemoveWatchSuccess() {
782 $userIdentity = new UserIdentityValue( 100, 'User Name' );
783 [ $authority, $userFactory ] = $this->getAuthorityAndUserFactory(
784 $userIdentity,
785 [ 'editmywatchlist' ]
786 );
787 $title = new PageIdentityValue( 100, NS_MAIN, 'Page_db_Key_goesHere', PageIdentityValue::LOCAL );
788
789 $watchedItemStore = $this->getDummyWatchedItemStore();
790 $watchlistManager = $this->getManager( [
791 'userFactory' => $userFactory,
792 'watchedItemStore' => $watchedItemStore,
793 ] );
794
795 $watchlistManager->addWatchIgnoringRights( $userIdentity, $title );
796
797 $status = $watchlistManager->removeWatch( $authority, $title );
798
799 $this->assertStatusGood( $status );
800 $this->assertFalse( $watchlistManager->isWatchedIgnoringRights( $userIdentity, $title ) );
801 }
802 }