2013-11-02 13:36:44 +00:00
|
|
|
<?php
|
|
|
|
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2024-05-11 08:57:36 +00:00
|
|
|
use MediaWiki\Tests\Api\ApiTestCase;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\User\User;
|
|
|
|
|
2013-11-02 13:36:44 +00:00
|
|
|
/**
|
2014-02-26 02:12:47 +00:00
|
|
|
* Integration tests for the Thanks API module
|
2013-11-15 18:34:53 +00:00
|
|
|
*
|
2023-05-19 11:38:58 +00:00
|
|
|
* @covers \MediaWiki\Extension\Thanks\Api\ApiCoreThank
|
2013-11-02 13:36:44 +00:00
|
|
|
*
|
|
|
|
* @group Thanks
|
|
|
|
* @group Database
|
|
|
|
* @group medium
|
|
|
|
* @group API
|
|
|
|
*
|
2016-01-27 10:06:17 +00:00
|
|
|
* @author Addshore
|
2013-11-02 13:36:44 +00:00
|
|
|
*/
|
2018-02-21 03:46:30 +00:00
|
|
|
class ApiCoreThankIntegrationTest extends ApiTestCase {
|
2013-11-02 13:36:44 +00:00
|
|
|
|
2016-05-06 14:27:05 +00:00
|
|
|
/**
|
|
|
|
* @var int filled in setUp
|
|
|
|
*/
|
|
|
|
private $revId;
|
|
|
|
|
2020-02-03 16:24:36 +00:00
|
|
|
/**
|
|
|
|
* @var User filled in setUp
|
|
|
|
*/
|
|
|
|
private $uploader;
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
/**
|
|
|
|
* @var int The ID of a deletion log entry.
|
|
|
|
*/
|
|
|
|
protected $logId;
|
|
|
|
|
2021-07-23 22:52:23 +00:00
|
|
|
public function setUp(): void {
|
2013-11-02 13:36:44 +00:00
|
|
|
parent::setUp();
|
2016-05-06 14:27:05 +00:00
|
|
|
|
2023-08-18 00:25:37 +00:00
|
|
|
$this->uploader = $this->getTestUser()->getUser();
|
2020-02-03 16:24:36 +00:00
|
|
|
$user = $this->uploader;
|
2018-03-20 00:54:51 +00:00
|
|
|
|
2023-08-18 00:25:37 +00:00
|
|
|
// Here comes the hack! Make sure to create a random, useless revision with ID 1, so that the one created below
|
|
|
|
// that we will use to send thanks has an ID other than 1. Why? Because ApiCoreThank::getRevisionFromId thinks
|
|
|
|
// rev ID = 1 means invalid input, see T344475. Yay!
|
|
|
|
$this->getExistingTestPage( 'Why the heck is revID = 1 considered invalid?' );
|
|
|
|
|
2018-03-20 00:54:51 +00:00
|
|
|
$pageName = __CLASS__;
|
|
|
|
$content = __CLASS__;
|
2023-08-18 00:25:37 +00:00
|
|
|
// Make sure the page doesn't exist, otherwise our edit will not result in a new revision
|
|
|
|
$page = $this->getNonexistingTestPage( $pageName );
|
|
|
|
$result = $this->editPage( $page, $content, 'Summary', NS_MAIN, $user );
|
|
|
|
$this->revId = $result->getNewRevision()->getId();
|
2016-05-06 14:27:05 +00:00
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
// Create a 2nd page and delete it, so we can thank for the log entry.
|
2023-08-18 00:25:37 +00:00
|
|
|
$pageToDelete = $this->getExistingTestPage( 'Page to delete' );
|
2020-03-25 06:43:05 +00:00
|
|
|
|
2020-04-24 06:27:22 +00:00
|
|
|
$deleteStatus = $pageToDelete->doDeleteArticleReal( '', $user );
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->logId = $deleteStatus->getValue();
|
|
|
|
|
2016-05-06 14:27:05 +00:00
|
|
|
DeferredUpdates::clearPendingUpdates();
|
2013-11-02 13:36:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testRequestWithoutToken() {
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( 'missingparam' );
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->doApiRequest( [
|
2013-11-02 13:36:44 +00:00
|
|
|
'action' => 'thank',
|
|
|
|
'source' => 'someSource',
|
|
|
|
'rev' => 1,
|
2020-02-03 16:24:36 +00:00
|
|
|
], null, false, $this->getTestSysop()->getUser() );
|
2013-11-02 13:36:44 +00:00
|
|
|
}
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
public function testValidRevRequest() {
|
2024-03-12 19:49:46 +00:00
|
|
|
[ $result,, ] = $this->doApiRequestWithToken( [
|
2013-11-02 13:36:44 +00:00
|
|
|
'action' => 'thank',
|
2016-05-06 14:27:05 +00:00
|
|
|
'rev' => $this->revId,
|
2020-02-03 16:24:36 +00:00
|
|
|
], null, $this->getTestSysop()->getUser() );
|
2013-11-02 13:36:44 +00:00
|
|
|
$this->assertSuccess( $result );
|
|
|
|
}
|
|
|
|
|
2018-03-03 04:00:40 +00:00
|
|
|
public function testValidLogRequest() {
|
2024-03-12 19:49:46 +00:00
|
|
|
[ $result,, ] = $this->doApiRequestWithToken( [
|
2018-03-03 04:00:40 +00:00
|
|
|
'action' => 'thank',
|
|
|
|
'log' => $this->logId,
|
2020-02-03 16:24:36 +00:00
|
|
|
], null, $this->getTestSysop()->getUser() );
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->assertSuccess( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogRequestWithDisallowedLogType() {
|
2024-03-06 19:55:16 +00:00
|
|
|
$this->overrideConfigValue( 'ThanksAllowedLogTypes', [] );
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( 'thanks-error-invalid-log-type' );
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
'action' => 'thank',
|
|
|
|
'log' => $this->logId,
|
2020-02-03 16:24:36 +00:00
|
|
|
], null, $this->getTestSysop()->getUser() );
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogThanksForADeletedLogEntry() {
|
2024-03-14 21:40:44 +00:00
|
|
|
$this->setGroupPermissions( [
|
2020-01-29 02:58:30 +00:00
|
|
|
'logdeleter' => [
|
|
|
|
'read' => true,
|
|
|
|
'writeapi' => true,
|
|
|
|
'deletelogentry' => true
|
|
|
|
]
|
|
|
|
] );
|
2018-03-03 04:00:40 +00:00
|
|
|
|
|
|
|
// Mark our test log entry as deleted.
|
2020-01-29 02:58:30 +00:00
|
|
|
// To do this we briefly switch to a different test user.
|
|
|
|
$logdeleter = $this->getTestUser( [ 'logdeleter' ] )->getUser();
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
'action' => 'revisiondelete',
|
|
|
|
'type' => 'logging',
|
|
|
|
'ids' => $this->logId,
|
|
|
|
'hide' => 'content',
|
2020-01-29 02:58:30 +00:00
|
|
|
], null, $logdeleter );
|
2018-03-03 04:00:40 +00:00
|
|
|
|
2020-01-29 02:58:30 +00:00
|
|
|
$sysop = $this->getTestSysop()->getUser();
|
2018-03-03 04:00:40 +00:00
|
|
|
// Then try to thank for it, and we should get an exception.
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( 'thanks-error-log-deleted' );
|
2018-03-03 04:00:40 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
|
|
|
'action' => 'thank',
|
|
|
|
'log' => $this->logId,
|
2020-01-29 02:58:30 +00:00
|
|
|
], null, $sysop );
|
2018-03-03 04:00:40 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testValidRequestWithSource() {
|
2024-03-12 19:49:46 +00:00
|
|
|
[ $result,, ] = $this->doApiRequestWithToken( [
|
2013-11-02 13:36:44 +00:00
|
|
|
'action' => 'thank',
|
|
|
|
'source' => 'someSource',
|
2016-05-06 14:27:05 +00:00
|
|
|
'rev' => $this->revId,
|
2020-02-03 16:24:36 +00:00
|
|
|
], null, $this->getTestSysop()->getUser() );
|
2013-11-02 13:36:44 +00:00
|
|
|
$this->assertSuccess( $result );
|
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
protected function assertSuccess( $result ) {
|
|
|
|
$this->assertEquals( [
|
|
|
|
'result' => [
|
2013-10-24 01:13:43 +00:00
|
|
|
'success' => 1,
|
2020-02-03 16:24:36 +00:00
|
|
|
'recipient' => $this->uploader->getName(),
|
2016-04-22 20:13:56 +00:00
|
|
|
],
|
|
|
|
], $result );
|
2013-11-02 13:36:44 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testInvalidRequest() {
|
2019-10-12 22:55:31 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->doApiRequestWithToken( [ 'action' => 'thank' ] );
|
2013-11-02 13:36:44 +00:00
|
|
|
}
|
|
|
|
|
2013-12-06 08:00:55 +00:00
|
|
|
}
|