2014-03-13 13:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Flow\Model\AbstractRevision;
|
|
|
|
use Flow\Model\PostRevision;
|
|
|
|
use Flow\Model\UUID;
|
2019-10-22 05:18:33 +00:00
|
|
|
use Flow\Model\Workflow;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\Deferred\DeferredUpdates;
|
2024-10-20 11:21:21 +00:00
|
|
|
use MediaWiki\Registration\ExtensionRegistry;
|
2024-05-11 08:57:36 +00:00
|
|
|
use MediaWiki\Tests\Api\ApiTestCase;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\Title\Title;
|
|
|
|
use MediaWiki\User\User;
|
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Integration tests for the Thanks Flow api module
|
|
|
|
*
|
2023-05-19 11:38:58 +00:00
|
|
|
* @covers \MediaWiki\Extension\Thanks\Api\ApiFlowThank
|
2014-03-13 13:20:02 +00:00
|
|
|
*
|
|
|
|
* @group Thanks
|
2018-06-04 07:28:49 +00:00
|
|
|
* @group Database
|
2014-03-13 13:20:02 +00:00
|
|
|
* @group medium
|
|
|
|
* @group API
|
|
|
|
* @group Flow
|
|
|
|
*
|
|
|
|
* @author Benjamin Chen
|
|
|
|
*/
|
2018-01-30 02:21:53 +00:00
|
|
|
class ApiFlowThankIntegrationTest extends ApiTestCase {
|
2018-01-19 08:38:40 +00:00
|
|
|
|
|
|
|
/** @var PostRevision */
|
|
|
|
public $topic;
|
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
public $meUser;
|
|
|
|
|
|
|
|
/** @var User */
|
|
|
|
public $otherUser;
|
|
|
|
|
|
|
|
/** @var PostRevision */
|
|
|
|
public $postByOtherUser;
|
|
|
|
|
|
|
|
/** @var PostRevision */
|
|
|
|
public $postByMe;
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2021-07-23 22:52:23 +00:00
|
|
|
public function setUp(): void {
|
2014-03-13 13:20:02 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2019-03-03 01:36:06 +00:00
|
|
|
if ( !ExtensionRegistry::getInstance()->isLoaded( 'Flow' ) ) {
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->markTestSkipped( 'Flow is not installed' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// mock topic and post
|
2023-11-26 11:01:03 +00:00
|
|
|
$this->meUser = $this->getMutableTestUser()->getUser();
|
|
|
|
$this->otherUser = $this->getMutableTestUser()->getUser();
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->topic = $this->generateObject();
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->postByOtherUser = $this->generateObject( [
|
2016-05-19 18:59:33 +00:00
|
|
|
'tree_orig_user_id' => $this->otherUser->getId(),
|
2014-03-13 13:20:02 +00:00
|
|
|
'tree_parent_id' => $this->topic->getPostId()->getBinary(),
|
2016-04-22 20:13:56 +00:00
|
|
|
], [], 1 );
|
|
|
|
$this->postByMe = $this->generateObject( [
|
2016-05-19 18:59:33 +00:00
|
|
|
'tree_orig_user_id' => $this->meUser->getId(),
|
2014-03-13 13:20:02 +00:00
|
|
|
'tree_parent_id' => $this->topic->getPostId()->getBinary(),
|
2016-04-22 20:13:56 +00:00
|
|
|
], [], 1 );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
// Set up mock classes in Container.
|
2022-09-29 14:08:53 +00:00
|
|
|
$mockLoader = $this->createMock( \Flow\Repository\RootPostLoader::class );
|
2014-03-13 13:20:02 +00:00
|
|
|
$mockLoader->expects( $this->any() )
|
|
|
|
->method( 'getWithRoot' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturnCallback(
|
2021-05-14 17:25:15 +00:00
|
|
|
function ( $postId ) {
|
2014-03-13 13:20:02 +00:00
|
|
|
switch ( $postId ) {
|
2021-05-14 17:25:15 +00:00
|
|
|
case $this->postByOtherUser->getPostId():
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2021-05-14 17:25:15 +00:00
|
|
|
'post' => $this->postByOtherUser,
|
|
|
|
'root' => $this->topic
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2021-05-14 17:25:15 +00:00
|
|
|
case $this->postByMe->getPostId():
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2021-05-14 17:25:15 +00:00
|
|
|
'post' => $this->postByMe,
|
|
|
|
'root' => $this->topic
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
default:
|
2016-04-22 20:13:56 +00:00
|
|
|
return [ 'post' => null ];
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-14 03:43:32 +00:00
|
|
|
);
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2019-10-22 05:18:33 +00:00
|
|
|
$mockWorkflow = $this->createMock( Workflow::class );
|
2014-03-13 13:20:02 +00:00
|
|
|
$mockWorkflow->expects( $this->any() )
|
2014-08-20 10:38:15 +00:00
|
|
|
->method( 'getOwnerTitle' )
|
2020-03-07 13:14:39 +00:00
|
|
|
->willReturn( $this->createMock( Title::class ) );
|
2016-07-28 15:15:37 +00:00
|
|
|
$mockWorkflow->expects( $this->any() )
|
|
|
|
->method( 'getArticleTitle' )
|
2020-03-07 13:14:39 +00:00
|
|
|
->willReturn( $this->createMock( Title::class ) );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2022-09-29 14:08:53 +00:00
|
|
|
$mockStorage = $this->createMock( \Flow\Data\ManagerGroup::class );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
$mockStorage->expects( $this->any() )
|
|
|
|
->method( 'get' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( $mockWorkflow );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2022-09-29 14:08:53 +00:00
|
|
|
$mockTemplating = $this->createMock( \Flow\Templating::class );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
$mockTemplating->expects( $this->any() )
|
|
|
|
->method( 'getContent' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( 'test content' );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2015-03-09 16:32:47 +00:00
|
|
|
Flow\Container::reset();
|
|
|
|
$container = Flow\Container::getContainer();
|
|
|
|
$container[ 'loader.root_post' ] = $mockLoader;
|
|
|
|
$container[ 'storage' ] = $mockStorage;
|
2014-03-13 13:20:02 +00:00
|
|
|
$container[ 'templating' ] = $mockTemplating;
|
|
|
|
|
2024-01-04 21:22:56 +00:00
|
|
|
DeferredUpdates::clearPendingUpdates();
|
2014-03-13 13:20:02 +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( [
|
2014-03-13 13:20:02 +00:00
|
|
|
'action' => 'flowthank',
|
2014-04-15 03:36:13 +00:00
|
|
|
'postid' => UUID::create( '42' )->getAlphadecimal(),
|
2016-04-22 20:13:56 +00:00
|
|
|
] );
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testInvalidRequest() {
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( 'missingparam' );
|
2016-04-22 20:13:56 +00:00
|
|
|
$this->doApiRequestWithToken( [ 'action' => 'flowthank' ] );
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testValidRequest() {
|
2024-03-12 19:49:46 +00:00
|
|
|
[ $result,, ] = $this->doApiRequestWithToken( [
|
2014-03-13 13:20:02 +00:00
|
|
|
'action' => 'flowthank',
|
2014-04-15 03:36:13 +00:00
|
|
|
'postid' => $this->postByOtherUser->getPostId()->getAlphadecimal(),
|
2016-04-22 20:13:56 +00:00
|
|
|
] );
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->assertSuccess( $result );
|
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testRequestWithInvalidId() {
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( 'invalidpostid' );
|
2021-03-14 03:43:32 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
2014-03-13 13:20:02 +00:00
|
|
|
'action' => 'flowthank',
|
2014-04-15 03:36:13 +00:00
|
|
|
'postid' => UUID::create( '42' )->getAlphadecimal(),
|
2016-04-22 20:13:56 +00:00
|
|
|
] );
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testRequestWithOwnId() {
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( 'invalidrecipient' );
|
2021-03-14 03:43:32 +00:00
|
|
|
$this->doApiRequestWithToken( [
|
2014-03-13 13:20:02 +00:00
|
|
|
'action' => 'flowthank',
|
2014-04-15 03:36:13 +00:00
|
|
|
'postid' => $this->postByMe->getPostId()->getAlphadecimal(),
|
2016-05-19 18:59:33 +00:00
|
|
|
], null, $this->meUser );
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
protected function assertSuccess( $result ) {
|
2020-05-30 08:22:24 +00:00
|
|
|
$this->assertSame( 1, $result[ 'result' ][ 'success' ] );
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is obtained from Flow/tests/PostRevisionTestCase.php
|
|
|
|
*
|
|
|
|
* Returns an array, representing flow_revision & flow_tree_revision db
|
|
|
|
* columns.
|
|
|
|
*
|
|
|
|
* You can pass in arguments to override default data.
|
|
|
|
* With no arguments tossed in, default data (resembling a newly-created
|
|
|
|
* topic title) will be returned.
|
|
|
|
*
|
2021-01-30 09:12:26 +00:00
|
|
|
* @param array $row DB row data (only specify override columns)
|
2014-03-13 13:20:02 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-04-22 20:13:56 +00:00
|
|
|
protected function generateRow( array $row = [] ) {
|
2014-03-13 13:20:02 +00:00
|
|
|
$uuidPost = UUID::create();
|
|
|
|
$uuidRevision = UUID::create();
|
|
|
|
|
2016-05-19 18:59:33 +00:00
|
|
|
$user = $this->meUser;
|
2014-09-16 20:27:58 +00:00
|
|
|
$userId = $user->getId();
|
|
|
|
$userIp = null;
|
2014-03-13 13:20:02 +00:00
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
return $row + [
|
2014-03-13 13:20:02 +00:00
|
|
|
// flow_revision
|
|
|
|
'rev_id' => $uuidRevision->getBinary(),
|
|
|
|
'rev_type' => 'post',
|
|
|
|
'rev_user_id' => $userId,
|
|
|
|
'rev_user_ip' => $userIp,
|
2021-12-21 01:32:37 +00:00
|
|
|
'rev_user_wiki' => WikiMap::getCurrentWikiId(),
|
2014-03-13 13:20:02 +00:00
|
|
|
'rev_parent_id' => null,
|
|
|
|
'rev_flags' => 'html',
|
|
|
|
'rev_content' => 'test content',
|
|
|
|
'rev_change_type' => 'new-post',
|
|
|
|
'rev_mod_state' => AbstractRevision::MODERATED_NONE,
|
|
|
|
'rev_mod_user_id' => null,
|
|
|
|
'rev_mod_user_ip' => null,
|
2014-09-16 20:27:58 +00:00
|
|
|
'rev_mod_user_wiki' => null,
|
2014-03-13 13:20:02 +00:00
|
|
|
'rev_mod_timestamp' => null,
|
|
|
|
'rev_mod_reason' => null,
|
|
|
|
'rev_last_edit_id' => null,
|
|
|
|
'rev_edit_user_id' => null,
|
|
|
|
'rev_edit_user_ip' => null,
|
2014-09-16 20:27:58 +00:00
|
|
|
'rev_edit_user_wiki' => null,
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
// flow_tree_revision
|
|
|
|
'tree_rev_descendant_id' => $uuidPost->getBinary(),
|
2014-04-15 01:29:38 +00:00
|
|
|
'rev_type_id' => $uuidPost->getBinary(),
|
2014-03-13 13:20:02 +00:00
|
|
|
'tree_rev_id' => $uuidRevision->getBinary(),
|
|
|
|
'tree_orig_create_time' => wfTimestampNow(),
|
|
|
|
'tree_orig_user_id' => $userId,
|
|
|
|
'tree_orig_user_ip' => $userIp,
|
2021-12-21 01:32:37 +00:00
|
|
|
'tree_orig_user_wiki' => WikiMap::getCurrentWikiId(),
|
2014-03-13 13:20:02 +00:00
|
|
|
'tree_parent_id' => null,
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is obtained from Flow/tests/PostRevisionTestCase.php
|
|
|
|
*
|
|
|
|
* Returns a PostRevision object.
|
|
|
|
*
|
|
|
|
* You can pass in arguments to override default data.
|
|
|
|
* With no arguments tossed in, a default revision (resembling a newly-
|
|
|
|
* created topic title) will be returned.
|
|
|
|
*
|
2021-01-30 09:12:26 +00:00
|
|
|
* @param array $row DB row data (only specify override columns)
|
|
|
|
* @param array $children Array of child PostRevision objects
|
|
|
|
* @param int $depth Depth of the PostRevision object
|
2014-03-13 13:20:02 +00:00
|
|
|
* @return PostRevision
|
|
|
|
*/
|
2016-04-22 20:13:56 +00:00
|
|
|
protected function generateObject( array $row = [], $children = [], $depth = 0 ) {
|
2014-03-13 13:20:02 +00:00
|
|
|
$row = $this->generateRow( $row );
|
|
|
|
|
|
|
|
$revision = PostRevision::fromStorageRow( $row );
|
|
|
|
$revision->setChildren( $children );
|
|
|
|
$revision->setDepth( $depth );
|
|
|
|
|
|
|
|
return $revision;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|