summaryrefslogtreecommitdiff
blob: 9c0c7cb89d3ce6883ae50908e74e81cd218af580 (plain)
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
71
72
73
74
<?php

use Wikimedia\TestingAccessWrapper;

/**
 * @coversDefaultClass EchoNotificationController
 */
class NotificationControllerUnitTest extends MediaWikiUnitTestCase {

	/**
	 * @dataProvider PageLinkedTitleMutedByUserDataProvider
	 * @covers ::isPageLinkedTitleMutedByUser
	 * @param Title $title
	 * @param User $user
	 * @param bool $expected
	 */
	public function testIsPageLinkedTitleMutedByUser(
		Title $title, User $user, bool $expected ): void {
		$wrapper = TestingAccessWrapper::newFromClass( EchoNotificationController::class );
		$wrapper->mutedPageLinkedTitlesCache = $this->getMapCacheLruMock();
		$this->assertSame(
			$expected,
			$wrapper->isPageLinkedTitleMutedByUser( $title, $user )
		);
	}

	public function PageLinkedTitleMutedByUserDataProvider() :array {
		return [
			[
				$this->getMockTitle( 123 ),
				$this->getMockUser( [] ),
				false
			],
			[
				$this->getMockTitle( 123 ),
				$this->getMockUser( [ 123, 456, 789 ] ),
				true
			],
			[
				$this->getMockTitle( 456 ),
				$this->getMockUser( [ 489 ] ),
				false
			]

		];
	}

	private function getMockTitle( int $articleID ) {
		$title = $this->getMockBuilder( Title::class )
			->disableOriginalConstructor()
			->getMock();
		$title->method( 'getArticleID' )
			->willReturn( $articleID );
		return $title;
	}

	private function getMockUser( $mutedTitlePreferences = [] ) {
		$user = $this->getMockBuilder( User::class )
			->disableOriginalConstructor()
			->getMock();
		$user->method( 'getId' )
			->willReturn( 456 );
		$user->method( 'getOption' )
			->willReturn( implode( "\n", $mutedTitlePreferences ) );
		return $user;
	}

	private function getMapCacheLruMock() {
		return $this->getMockBuilder( MapCacheLRU::class )
			->disableOriginalConstructor()
			->getMock();
	}

}