aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/api/ApiWatchlistTraitTest.php
blob: 9cf8d07babcad14c9c0ed5734273f5a3c8b3d371 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace MediaWiki\Tests\Api;

use MediaWiki\Api\ApiWatchlistTrait;
use MediaWiki\Title\Title;
use MediaWiki\User\Options\UserOptionsLookup;
use MediaWiki\User\User;
use MediaWiki\Watchlist\WatchlistManager;
use MediaWikiUnitTestCase;
use Wikimedia\TestingAccessWrapper;

/**
 * @group API
 * @covers \MediaWiki\Api\ApiWatchlistTrait
 */
class ApiWatchlistTraitTest extends MediaWikiUnitTestCase {

	/**
	 * @dataProvider provideWatchlistValue
	 */
	public function testWatchlistValue( $watchlistValue, $setOption, $isBot, $isWatched, $expect ) {
		// TODO we don't currently test any of the logic that depends on if the title exists
		$user = $this->createMock( User::class );
		$user->method( 'isBot' )->willReturn( $isBot );

		$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
		$userOptionsLookup->method( 'getBoolOption' )->willReturnCallback(
			static function ( $unused, $optionName ) use ( $setOption ) {
				if ( $optionName === 'watchdefault' ) {
					return (bool)$setOption;
				}
				// watchcreations is not currently tested, by default it is enabled
				return true;
			}
		);

		$watchlistManager = $this->createMock( WatchlistManager::class );
		$watchlistManager->method( 'isWatchable' )->willReturn( true );
		$watchlistManager->method( 'isWatchedIgnoringRights' )->willReturn( $isWatched );

		$title = $this->createMock( Title::class );
		$title->method( 'exists' )->willReturn( true );

		$trait = TestingAccessWrapper::newFromObject( $this->getMockForTrait( ApiWatchlistTrait::class ) );
		$trait->watchlistManager = $watchlistManager;
		$trait->userOptionsLookup = $userOptionsLookup;

		$watch = $trait->getWatchlistValue( $watchlistValue, $title, $user, 'watchdefault' );
		$this->assertEquals( $expect, $watch );
	}

	public static function provideWatchlistValue() {
		return [
			'watch option on unwatched page' => [ 'watch', null, false, false, true ],
			'watch option on watched page' => [ 'watch', null, false, true, true ],
			'unwatch option on unwatched page' => [ 'unwatch', null, false, false, false ],
			'unwatch option on watched page' => [ 'unwatch', null, false, true, false ],
			'preferences set to true on unwatched page' => [ 'preferences', true, false, false, true ],
			'preferences set to false on unwatched page' => [ 'preferences', false, false, false, false ],
			'preferences set to true on unwatched page (bot group)' => [ 'preferences', true, true, false, false ],
			'preferences set to true on watched page (bot group)' => [ 'preferences', true, true, true, true ],
			'preferences set to false on unwatched page (bot group)' => [ 'preferences', false, true, false, false ],
			'preferences set to false on watched page (bot group)' => [ 'preferences', false, true, true, true ],
			'nochange option on watched page' => [ 'nochange', null, false, true, true ],
			'nochange option on unwatched page' => [ 'nochange', null, false, false, false ],
		];
	}

}