2014-03-13 13:20:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Flow\Model\AbstractRevision;
|
|
|
|
use Flow\Model\PostRevision;
|
|
|
|
use Flow\Model\UUID;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Integration tests for the Thanks Flow api module
|
|
|
|
*
|
|
|
|
* @covers ApiFlowThank
|
|
|
|
*
|
|
|
|
* @group Thanks
|
|
|
|
* @gropu Database
|
|
|
|
* @group medium
|
|
|
|
* @group API
|
|
|
|
* @group Flow
|
|
|
|
*
|
|
|
|
* @author Benjamin Chen
|
|
|
|
*/
|
|
|
|
class ApiFlowThankTest extends ApiTestCase {
|
|
|
|
/**
|
|
|
|
* @var PostRevision
|
|
|
|
*/
|
2016-04-22 20:13:56 +00:00
|
|
|
public $topic,
|
2016-05-19 18:59:33 +00:00
|
|
|
$meUser,
|
|
|
|
$otherUser,
|
2014-03-13 13:20:02 +00:00
|
|
|
$postByOtherUser,
|
|
|
|
$postByMe;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
if ( !class_exists( 'FlowHooks' ) ) {
|
|
|
|
$this->markTestSkipped( 'Flow is not installed' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// mock topic and post
|
2016-05-19 18:59:33 +00:00
|
|
|
if ( method_exists( $this, 'getTestUser' ) ) {
|
2016-05-24 03:15:12 +00:00
|
|
|
$this->meUser = $this->getMutableTestUser()->getUser();
|
|
|
|
$this->otherUser = $this->getMutableTestUser()->getUser();
|
2016-05-19 18:59:33 +00:00
|
|
|
} else {
|
|
|
|
$this->meUser = self::$users[ 'sysop' ]->getUser();
|
|
|
|
$this->otherUser = self::$users[ 'uploader' ]->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.
|
2014-09-16 20:27:58 +00:00
|
|
|
$mockLoader = $this->getMockBuilder( '\Flow\Repository\RootPostLoader' )
|
2014-03-13 13:20:02 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$that = $this;
|
|
|
|
$mockLoader->expects( $this->any() )
|
|
|
|
->method( 'getWithRoot' )
|
|
|
|
->will( $this->returnCallback(
|
|
|
|
// Hard to work with class variables or callbacks,
|
|
|
|
// using anonymous function instead.
|
|
|
|
function( $postId ) use ( $that ) {
|
|
|
|
switch ( $postId ) {
|
|
|
|
case $that->postByOtherUser->getPostId():
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2014-03-13 13:20:02 +00:00
|
|
|
'post' => $that->postByOtherUser,
|
|
|
|
'root' => $that->topic
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
case $that->postByMe->getPostId():
|
2016-04-22 20:13:56 +00:00
|
|
|
return [
|
2014-03-13 13:20:02 +00:00
|
|
|
'post' => $that->postByMe,
|
|
|
|
'root' => $that->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
|
|
|
}
|
|
|
|
}
|
|
|
|
) );
|
|
|
|
|
|
|
|
$mockWorkflow = $this->getMock( '\Flow\Model\Workflow' );
|
|
|
|
$mockWorkflow->expects( $this->any() )
|
2014-08-20 10:38:15 +00:00
|
|
|
->method( 'getOwnerTitle' )
|
2016-04-22 20:13:56 +00:00
|
|
|
->will( $this->returnValue( new Title() ) );
|
2014-03-13 13:20:02 +00:00
|
|
|
|
|
|
|
$mockStorage = $this->getMockBuilder( '\Flow\Data\ManagerGroup' )
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$mockStorage->expects( $this->any() )
|
|
|
|
->method( 'get' )
|
|
|
|
->will( $this->returnValue( $mockWorkflow ) );
|
|
|
|
|
2014-04-02 21:39:26 +00:00
|
|
|
$mockTemplating = $this->getMockBuilder( 'Flow\Templating' )
|
2014-03-13 13:20:02 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$mockTemplating->expects( $this->any() )
|
|
|
|
->method( 'getContent' )
|
|
|
|
->will( $this->returnValue( 'test content' ) );
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
$this->doLogin( 'sysop' );
|
2014-10-29 18:58:27 +00:00
|
|
|
\DeferredUpdates::clearPendingUpdates();
|
2014-03-13 13:20:02 +00:00
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
public function testRequestWithoutToken() {
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->setExpectedException( 'UsageException', 'The token parameter must be set' );
|
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() {
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->setExpectedException( 'UsageException', 'The postid parameter must be set' );
|
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() {
|
|
|
|
list( $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() {
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->setExpectedException( 'UsageException', 'Post ID is invalid' );
|
2016-04-22 20:13:56 +00:00
|
|
|
list( $result,, ) = $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() {
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->setExpectedException( 'UsageException', 'You cannot thank yourself' );
|
2016-04-22 20:13:56 +00:00
|
|
|
list( $result,, ) = $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 ) {
|
2014-03-13 13:20:02 +00:00
|
|
|
$this->assertEquals( 1, $result[ 'result' ][ 'success' ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @param array[optional] $row DB row data (only specify override columns)
|
|
|
|
* @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,
|
2014-09-16 20:27:58 +00:00
|
|
|
'rev_user_wiki' => wfWikiId(),
|
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,
|
2014-09-16 20:27:58 +00:00
|
|
|
'tree_orig_user_wiki' => wfWikiId(),
|
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.
|
|
|
|
*
|
|
|
|
* @param array[optional] $row DB row data (only specify override columns)
|
|
|
|
* @param array[optional] $children Array of child PostRevision objects
|
|
|
|
* @param int[optional] $depth Depth of the PostRevision object
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|