summaryrefslogtreecommitdiff
blob: f38f8bc2a6a843c02b64862056313d47801920b2 (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
142
143
144
145
146
<?php

/**
 * Integration tests for the Thanks API module
 *
 * @covers ApiCoreThank
 *
 * @group Thanks
 * @group Database
 * @group medium
 * @group API
 *
 * @author Addshore
 */
class ApiCoreThankIntegrationTest extends ApiTestCase {

	/**
	 * @var int filled in setUp
	 */
	private $revId;

	/**
	 * @var int The ID of a deletion log entry.
	 */
	protected $logId;

	public function setUp() {
		parent::setUp();

		// You can't thank yourself, kind of hacky but just use this other user
		$this->doLogin( 'uploader' );

		$pageName = __CLASS__;
		$content = __CLASS__;
		$pageTitle = Title::newFromText( $pageName );
		// If the page already exists, delete it, otherwise our edit will not result in a new revision
		if ( $pageTitle->exists() ) {
			$wikiPage = WikiPage::factory( $pageTitle );
			$wikiPage->doDeleteArticleReal( '' );
		}
		$result = $this->editPage( $pageName, $content );
		/** @var Status $result */
		$result = $result->getValue();
		/** @var Revision $revision */
		$revision = $result['revision'];
		$this->revId = $revision->getId();

		// Create a 2nd page and delete it, so we can thank for the log entry.
		$pageToDeleteTitle = Title::newFromText( 'Page to delete' );
		$pageToDelete = WikiPage::factory( $pageToDeleteTitle );
		$pageToDelete->doEditContent( ContentHandler::makeContent( '', $pageToDeleteTitle ), '' );
		$deleteStatus = $pageToDelete->doDeleteArticleReal( '' );
		$this->logId = $deleteStatus->getValue();

		$this->doLogin( 'sysop' );
		DeferredUpdates::clearPendingUpdates();
	}

	public function testRequestWithoutToken() {
		$this->setExpectedException( 'ApiUsageException', 'The "token" parameter must be set.' );
		$this->doApiRequest( [
			'action' => 'thank',
			'source' => 'someSource',
			'rev' => 1,
		] );
	}

	public function testValidRevRequest() {
		list( $result,, ) = $this->doApiRequestWithToken( [
			'action' => 'thank',
			'rev' => $this->revId,
		] );
		$this->assertSuccess( $result );
	}

	public function testValidLogRequest() {
		list( $result,, ) = $this->doApiRequestWithToken( [
			'action' => 'thank',
			'log' => $this->logId,
		] );
		$this->assertSuccess( $result );
	}

	public function testLogRequestWithDisallowedLogType() {
		// Empty the log-type whitelist.
		$this->setMwGlobals( [ 'wgThanksLogTypeWhitelist' => [] ] );
		$this->setExpectedException(
			ApiUsageException::class,
			"Log type 'delete' is not in the whitelist of permitted log types."
		);
		$this->doApiRequestWithToken( [
			'action' => 'thank',
			'log' => $this->logId,
		] );
	}

	public function testLogThanksForADeletedLogEntry() {
		global $wgUser;

		// Mark our test log entry as deleted.
		// To do this we briefly switch back to our 'uploader' test user.
		$this->doLogin( 'uploader' );
		$wgUser->mRights[] = 'deletelogentry';
		$this->doApiRequestWithToken( [
			'action' => 'revisiondelete',
			'type'   => 'logging',
			'ids'    => $this->logId,
			'hide'   => 'content',
		] );
		$this->doLogin( 'sysop' );

		// Then try to thank for it, and we should get an exception.
		$this->setExpectedException(
			ApiUsageException::class,
			"The requested log entry has been deleted and thanks cannot be given for it."
		);
		$this->doApiRequestWithToken( [
			'action' => 'thank',
			'log' => $this->logId,
		] );
	}

	public function testValidRequestWithSource() {
		list( $result,, ) = $this->doApiRequestWithToken( [
			'action' => 'thank',
			'source' => 'someSource',
			'rev' => $this->revId,
		] );
		$this->assertSuccess( $result );
	}

	protected function assertSuccess( $result ) {
		$this->assertEquals( [
			'result' => [
				'success' => 1,
				'recipient' => self::$users['uploader']->getUser()->getName(),
			],
		], $result );
	}

	public function testInvalidRequest() {
		$this->setExpectedException( 'ApiUsageException' );
		$this->doApiRequestWithToken( [ 'action' => 'thank' ] );
	}

}