1<?php 2 3use MediaWiki\User\UserOptionsLookup; 4use MediaWiki\Watchlist\WatchlistManager; 5use Wikimedia\TestingAccessWrapper; 6 7/** 8 * @group API 9 * @covers ApiWatchlistTrait 10 */ 11class ApiWatchlistTraitTest extends MediaWikiUnitTestCase { 12 13 /** 14 * @dataProvider provideWatchlistValue 15 */ 16 public function testWatchlistValue( $watchlistValue, $setOption, $isBot, $isWatched, $expect ) { 17 // TODO we don't currently test any of the logic that depends on if the title exists 18 $user = $this->createMock( User::class ); 19 $user->method( 'isBot' )->willReturn( $isBot ); 20 21 $userOptionsLookup = $this->createMock( UserOptionsLookup::class ); 22 $userOptionsLookup->method( 'getBoolOption' )->willReturnCallback( 23 static function ( $unused, $optionName ) use ( $setOption ) { 24 if ( $optionName === 'watchdefault' ) { 25 return (bool)$setOption; 26 } 27 // watchcreations is not currently tested, by default it is enabled 28 return true; 29 } 30 ); 31 32 $watchlistManager = $this->createMock( WatchlistManager::class ); 33 $watchlistManager->method( 'isWatchable' )->willReturn( true ); 34 $watchlistManager->method( 'isWatchedIgnoringRights' )->willReturn( $isWatched ); 35 36 $title = $this->createMock( Title::class ); 37 $title->method( 'exists' )->willReturn( true ); 38 39 $trait = TestingAccessWrapper::newFromObject( $this->getMockForTrait( ApiWatchlistTrait::class ) ); 40 $trait->watchlistManager = $watchlistManager; 41 $trait->userOptionsLookup = $userOptionsLookup; 42 43 $watch = $trait->getWatchlistValue( $watchlistValue, $title, $user, 'watchdefault' ); 44 $this->assertEquals( $expect, $watch ); 45 } 46 47 public function provideWatchlistValue() { 48 return [ 49 'watch option on unwatched page' => [ 'watch', null, false, false, true ], 50 'watch option on watched page' => [ 'watch', null, false, true, true ], 51 'unwatch option on unwatched page' => [ 'unwatch', null, false, false, false ], 52 'unwatch option on watched page' => [ 'unwatch', null, false, true, false ], 53 'preferences set to true on unwatched page' => [ 'preferences', true, false, false, true ], 54 'preferences set to false on unwatched page' => [ 'preferences', false, false, false, false ], 55 'preferences set to true on unwatched page (bot group)' => [ 'preferences', true, true, false, false ], 56 'preferences set to true on watched page (bot group)' => [ 'preferences', true, true, true, true ], 57 'preferences set to false on unwatched page (bot group)' => [ 'preferences', false, true, false, false ], 58 'preferences set to false on watched page (bot group)' => [ 'preferences', false, true, true, true ], 59 'nochange option on watched page' => [ 'nochange', null, false, true, true ], 60 'nochange option on unwatched page' => [ 'nochange', null, false, false, false ], 61 ]; 62 } 63 64} 65