Debian Codegraph @

extensions/Thanks/tests/phpunit/ApiCoreThankIntegrationTest.php

raw symbols SCIP
1 <?php
2
3 use MediaWiki\Api\ApiUsageException;
4 use MediaWiki\Deferred\DeferredUpdates;
5 use MediaWiki\Tests\Api\ApiTestCase;
6 use MediaWiki\User\User;
7
8 /**
9 * Integration tests for the Thanks API module
10 *
11 * @covers \MediaWiki\Extension\Thanks\Api\ApiCoreThank
12 *
13 * @group Thanks
14 * @group Database
15 * @group medium
16 * @group API
17 *
18 * @author Addshore
19 */
20 class ApiCoreThankIntegrationTest extends ApiTestCase {
21
22 /**
23 * @var int filled in setUp
24 */
25 private $revId;
26
27 /**
28 * @var User filled in setUp
29 */
30 private $uploader;
31
32 /**
33 * @var int The ID of a deletion log entry.
34 */
35 protected $logId;
36
37 public function setUp(): void {
38 parent::setUp();
39
40 $this->uploader = $this->getTestUser()->getUser();
41 $user = $this->uploader;
42
43 // Here comes the hack! Make sure to create a random, useless revision with ID 1, so that the one created below
44 // that we will use to send thanks has an ID other than 1. Why? Because ApiCoreThank::getRevisionFromId thinks
45 // rev ID = 1 means invalid input, see T344475. Yay!
46 $this->getExistingTestPage( 'Why the heck is revID = 1 considered invalid?' );
47
48 $pageName = __CLASS__;
49 $content = __CLASS__;
50 // Make sure the page doesn't exist, otherwise our edit will not result in a new revision
51 $page = $this->getNonexistingTestPage( $pageName );
52 $result = $this->editPage( $page, $content, 'Summary', NS_MAIN, $user );
53 $this->revId = $result->getNewRevision()->getId();
54
55 // Create a 2nd page and delete it, so we can thank for the log entry.
56 $pageToDelete = $this->getExistingTestPage( 'Page to delete' );
57
58 $deleteStatus = $pageToDelete->doDeleteArticleReal( '', $user );
59 $this->logId = $deleteStatus->getValue();
60
61 DeferredUpdates::clearPendingUpdates();
62 }
63
64 public function testRequestWithoutToken() {
65 $this->expectApiErrorCode( 'missingparam' );
66 $this->doApiRequest( [
67 'action' => 'thank',
68 'source' => 'someSource',
69 'rev' => 1,
70 ], null, false, $this->getTestSysop()->getUser() );
71 }
72
73 public function testValidRevRequest() {
74 [ $result,, ] = $this->doApiRequestWithToken( [
75 'action' => 'thank',
76 'rev' => $this->revId,
77 ], null, $this->getTestSysop()->getUser() );
78 $this->assertSuccess( $result );
79 }
80
81 public function testValidLogRequest() {
82 [ $result,, ] = $this->doApiRequestWithToken( [
83 'action' => 'thank',
84 'log' => $this->logId,
85 ], null, $this->getTestSysop()->getUser() );
86 $this->assertSuccess( $result );
87 }
88
89 public function testLogRequestWithDisallowedLogType() {
90 $this->overrideConfigValue( 'ThanksAllowedLogTypes', [] );
91 $this->expectApiErrorCode( 'thanks-error-invalid-log-type' );
92 $this->doApiRequestWithToken( [
93 'action' => 'thank',
94 'log' => $this->logId,
95 ], null, $this->getTestSysop()->getUser() );
96 }
97
98 public function testLogThanksForADeletedLogEntry() {
99 $this->setGroupPermissions( [
100 'logdeleter' => [
101 'read' => true,
102 'deletelogentry' => true
103 ]
104 ] );
105
106 // Mark our test log entry as deleted.
107 // To do this, we briefly switch to a different test user.
108 $logdeleter = $this->getTestUser( [ 'logdeleter' ] )->getUser();
109 $this->doApiRequestWithToken( [
110 'action' => 'revisiondelete',
111 'type' => 'logging',
112 'ids' => $this->logId,
113 'hide' => 'content',
114 ], null, $logdeleter );
115
116 $sysop = $this->getTestSysop()->getUser();
117 // Then try to thank for it, and we should get an exception.
118 $this->expectApiErrorCode( 'thanks-error-log-deleted' );
119 $this->doApiRequestWithToken( [
120 'action' => 'thank',
121 'log' => $this->logId,
122 ], null, $sysop );
123 }
124
125 public function testValidRequestWithSource() {
126 [ $result,, ] = $this->doApiRequestWithToken( [
127 'action' => 'thank',
128 'source' => 'someSource',
129 'rev' => $this->revId,
130 ], null, $this->getTestSysop()->getUser() );
131 $this->assertSuccess( $result );
132 }
133
134 protected function assertSuccess( $result ) {
135 $this->assertEquals( [
136 'result' => [
137 'success' => 1,
138 'recipient' => $this->uploader->getName(),
139 ],
140 ], $result );
141 }
142
143 public function testInvalidRequest() {
144 $this->expectException( ApiUsageException::class );
145 $this->doApiRequestWithToken( [ 'action' => 'thank' ] );
146 }
147
148 }