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