summaryrefslogtreecommitdiff
blob: 00a2b4114c155bba33307323d4a906e4f480fb67 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php

use MediaWiki\Block\DatabaseBlock;

/**
 * Unit tests for the Thanks API module
 *
 * @group Thanks
 * @group API
 *
 * @author Addshore
 */
class ApiCoreThankUnitTest extends MediaWikiTestCase {

	protected static $moduleName = 'thank';

	protected function getModule() {
		return new ApiCoreThank( new ApiMain(), self::$moduleName );
	}

	private function createBlock( $options ) {
		$options = array_merge( [
			'address' => 'Test user',
			'by' => 1,
			'reason' => __METHOD__,
			'timestamp' => wfTimestamp( TS_MW ),
			'expiry' => 'infinity',
		], $options );
		return new DatabaseBlock( $options );
	}

	/**
	 * @dataProvider provideDieOnBadUser
	 * @covers ApiThank::dieOnBadUser
	 * @covers ApiThank::dieOnSitewideBlockedUser
	 */
	public function testDieOnBadUser( $user, $dieMethod, $expectedError ) {
		$module = $this->getModule();
		$method = new ReflectionMethod( $module, $dieMethod );
		$method->setAccessible( true );

		if ( $expectedError ) {
			$this->expectException( ApiUsageException::class );
			$this->expectExceptionMessage( $expectedError );
		}

		$method->invoke( $module, $user );
		// perhaps the method should return true.. For now we must do this
		$this->assertTrue( true );
	}

	public function provideDieOnBadUser() {
		$testCases = [];

		$mockUser = $this->createMock( 'User' );
		$mockUser->expects( $this->once() )
			->method( 'isAnon' )
			->will( $this->returnValue( true ) );

		$testCases[ 'anon' ] = [
			$mockUser,
			'dieOnBadUser',
			'Anonymous users cannot send thanks'
		];

		$mockUser = $this->createMock( 'User' );
		$mockUser->expects( $this->once() )
			->method( 'isAnon' )
			->will( $this->returnValue( false ) );
		$mockUser->expects( $this->once() )
			->method( 'pingLimiter' )
			->will( $this->returnValue( true ) );

		$testCases[ 'ping' ] = [
			$mockUser,
			'dieOnBadUser',
			"You've exceeded your rate limit. Please wait some time and try again"
		];

		$mockUser = $this->createMock( 'User' );
		$mockUser->expects( $this->once() )
			->method( 'isAnon' )
			->will( $this->returnValue( false ) );
		$mockUser->expects( $this->once() )
			->method( 'pingLimiter' )
			->will( $this->returnValue( false ) );
		$mockUser->expects( $this->once() )
			->method( 'isBlockedGlobally' )
			->will( $this->returnValue( true ) );
		$mockUser->expects( $this->once() )
			->method( 'getGlobalBlock' )
			->will( $this->returnValue(
				$this->createBlock( [] )
			) );

		$testCases[ 'globally blocked' ] = [
			$mockUser,
			'dieOnBadUser',
			'You have been blocked from editing'
		];

		$mockUser = $this->createMock( 'User' );
		$mockUser->expects( $this->once() )
			->method( 'getBlock' )
			->will( $this->returnValue(
				$this->createBlock( [] )
			) );

		$testCases[ 'sitewide blocked' ] = [
			$mockUser,
			'dieOnSitewideBlockedUser',
			'You have been blocked from editing'
		];

		$mockUser = $this->createMock( 'User' );
		$mockUser->expects( $this->once() )
			->method( 'getBlock' )
			->will( $this->returnValue(
				$this->createBlock( [
					'sitewide' => false
				] )
			) );

		$testCases[ 'partial blocked' ] = [
			$mockUser,
			'dieOnSitewideBlockedUser',
			false
		];

		return $testCases;
	}

	// @todo test userAlreadySentThanksForRevision
	// @todo test getRevisionFromParams
	// @todo test getTitleFromRevision
	// @todo test getSourceFromParams
	// @todo test getUserIdFromRevision
	// @todo test markResultSuccess
	// @todo test sendThanks

}