mediawiki-extensions-Thanks/tests/ApiFlowThankIntegrationTest.php
Benjamin Chen fbaed178a5 Add integration test for flowthank API
The test suite sets up various mocks for Flow classes and inserts
them into Flow\Container, so the API module retrieves the correct
mock classes instead.

Change-Id: I9c5e910e288f3dd72c66e68ca558265b8ca8ce5e
2014-03-14 19:16:11 +08:00

224 lines
6.2 KiB
PHP

<?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
*/
public
$topic,
$postByOtherUser,
$postByMe;
public function setUp() {
parent::setUp();
if ( !class_exists( 'FlowHooks' ) ) {
$this->markTestSkipped( 'Flow is not installed' );
}
// mock topic and post
$this->topic = $this->generateObject();
$this->postByOtherUser = $this->generateObject( array(
'tree_orig_user_id' => self::$users[ 'uploader' ]->user->getId(),
'tree_parent_id' => $this->topic->getPostId()->getBinary(),
), array(), 1 );
$this->postByMe = $this->generateObject( array(
'tree_orig_user_id' => self::$users[ 'sysop' ]->user->getId(),
'tree_parent_id' => $this->topic->getPostId()->getBinary(),
), array(), 1 );
// Set up mock classes in Container.
$mockLoader = $this->getMockBuilder( '\Flow\Data\RootPostLoader' )
->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():
return array(
'post' => $that->postByOtherUser,
'root' => $that->topic
);
case $that->postByMe->getPostId():
return array(
'post' => $that->postByMe,
'root' => $that->topic
);
default:
return array( 'post' => null );
}
}
) );
$container = Flow\Container::getContainer();
$container[ 'loader.root_post' ] = $mockLoader;
$mockWorkflow = $this->getMock( '\Flow\Model\Workflow' );
$mockWorkflow->expects( $this->any() )
->method( 'getArticleTitle' )
->will( $this->returnValue( new Title() ));
$mockStorage = $this->getMockBuilder( '\Flow\Data\ManagerGroup' )
->disableOriginalConstructor()
->getMock();
$mockStorage->expects( $this->any() )
->method( 'get' )
->will( $this->returnValue( $mockWorkflow ) );
$container[ 'storage' ] = $mockStorage;
$mockTemplating = $this->getMockBuilder( '\Flow\Templating' )
->disableOriginalConstructor()
->getMock();
$mockTemplating->expects( $this->any() )
->method( 'getContent' )
->will( $this->returnValue( 'test content' ) );
$container[ 'templating' ] = $mockTemplating;
$this->doLogin( 'sysop' );
}
public function testRequestWithoutToken(){
$this->setExpectedException( 'UsageException', 'The token parameter must be set' );
$this->doApiRequest( array(
'action' => 'flowthank',
'postid' => UUID::create( '42' ),
) );
}
public function testInvalidRequest(){
$this->setExpectedException( 'UsageException', 'The postid parameter must be set' );
$this->doApiRequestWithToken( array( 'action' => 'flowthank' ) );
}
public function testValidRequest(){
list( $result,, ) = $this->doApiRequestWithToken( array(
'action' => 'flowthank',
'postid' => $this->postByOtherUser->getPostId(),
) );
$this->assertSuccess( $result );
}
public function testRequestWithInvalidId(){
$this->setExpectedException( 'UsageException', 'Post ID is invalid' );
list( $result,, ) = $this->doApiRequestWithToken( array(
'action' => 'flowthank',
'postid' => UUID::create( '42' ),
) );
}
public function testRequestWithOwnId(){
$this->setExpectedException( 'UsageException', 'You cannot thank yourself' );
list( $result,, ) = $this->doApiRequestWithToken( array(
'action' => 'flowthank',
'postid' => $this->postByMe->getPostId(),
) );
}
protected function assertSuccess( $result ){
$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
*/
protected function generateRow( array $row = array() ) {
$uuidPost = UUID::create();
$uuidRevision = UUID::create();
$user = User::newFromName( 'UTSysop' );
list( $userId, $userIp ) = PostRevision::userFields( $user );
return $row + array(
// flow_revision
'rev_id' => $uuidRevision->getBinary(),
'rev_type' => 'post',
'rev_user_id' => $userId,
'rev_user_ip' => $userIp,
'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,
'rev_mod_timestamp' => null,
'rev_mod_reason' => null,
'rev_last_edit_id' => null,
'rev_edit_user_id' => null,
'rev_edit_user_ip' => null,
// flow_tree_revision
'tree_rev_descendant_id' => $uuidPost->getBinary(),
'tree_rev_id' => $uuidRevision->getBinary(),
'tree_orig_create_time' => wfTimestampNow(),
'tree_orig_user_id' => $userId,
'tree_orig_user_ip' => $userIp,
'tree_parent_id' => null,
);
}
/**
* 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
*/
protected function generateObject( array $row = array(), $children = array(), $depth = 0 ) {
$row = $this->generateRow( $row );
$revision = PostRevision::fromStorageRow( $row );
$revision->setChildren( $children );
$revision->setDepth( $depth );
return $revision;
}
}