2013-11-02 13:36:44 +00:00
|
|
|
<?php
|
|
|
|
|
2020-06-10 00:30:47 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2020-04-24 06:27:22 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
|
|
|
|
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
|
|
|
*
|
2018-02-21 03:46:30 +00:00
|
|
|
* @covers 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;
|
|
|
|
|
2019-10-11 19:16:59 +00:00
|
|
|
public function setUp() : void {
|
2013-11-02 13:36:44 +00:00
|
|
|
parent::setUp();
|
2016-05-06 14:27:05 +00:00
|
|
|
|
2020-02-03 16:24:36 +00:00
|
|
|
$this->uploader = $this->getTestUser( [ 'uploader' ] )->getUser();
|
|
|
|
$user = $this->uploader;
|
2018-03-20 00:54:51 +00:00
|
|
|
|
|
|
|
$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 );
|
2020-04-24 06:27:22 +00:00
|
|
|
$wikiPage->doDeleteArticleReal( '', $user );
|
2018-03-20 00:54:51 +00:00
|
|
|
}
|
2020-02-03 16:24:36 +00:00
|
|
|
$result = $this->editPage( $pageName, $content, 'Summary', NS_MAIN, $user );
|
2016-05-06 14:27:05 +00:00
|
|
|
/** @var Status $result */
|
|
|
|
$result = $result->getValue();
|
2020-06-10 00:30:47 +00:00
|
|
|
/** @var RevisionRecord $revisionRecord */
|
|
|
|
$revisionRecord = $result['revision-record'];
|
|
|
|
$this->revId = $revisionRecord->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.
|
|
|
|
$pageToDeleteTitle = Title::newFromText( 'Page to delete' );
|
|
|
|
$pageToDelete = WikiPage::factory( $pageToDeleteTitle );
|
2020-02-03 16:24:36 +00:00
|
|
|
|
2020-04-24 06:27:22 +00:00
|
|
|
$updater = $pageToDelete->newPageUpdater( $user );
|
|
|
|
$updater->setcontent(
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
ContentHandler::makeContent( '', $pageToDeleteTitle )
|
|
|
|
);
|
|
|
|
$updater->saveRevision( CommentStoreComment::newUnsavedComment( '' ) );
|
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() {
|
2019-10-12 22:55:31 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
$this->expectExceptionMessage( 'The "token" parameter must be set.' );
|
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() {
|
2016-04-22 20:13:56 +00:00
|
|
|
list( $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() {
|
|
|
|
list( $result,, ) = $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
|
|
|
$this->assertSuccess( $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogRequestWithDisallowedLogType() {
|
|
|
|
// Empty the log-type whitelist.
|
|
|
|
$this->setMwGlobals( [ 'wgThanksLogTypeWhitelist' => [] ] );
|
2019-10-12 22:55:31 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
"Log type 'delete' is not in the whitelist of permitted log types." );
|
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() {
|
2020-01-29 02:58:30 +00:00
|
|
|
$this->mergeMwGlobalArrayValue( 'wgGroupPermissions', [
|
|
|
|
'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.
|
2019-10-12 22:55:31 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
$this->expectExceptionMessage(
|
|
|
|
"The requested log entry has been deleted and thanks cannot be given for it." );
|
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() {
|
|
|
|
list( $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
|
|
|
}
|