2013-11-15 18:34:53 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-31 15:44:56 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
|
|
|
|
2013-11-15 18:34:53 +00:00
|
|
|
/**
|
2014-02-26 02:12:47 +00:00
|
|
|
* Unit tests for the Thanks API module
|
2013-11-15 18:34:53 +00:00
|
|
|
*
|
|
|
|
* @group Thanks
|
|
|
|
* @group API
|
|
|
|
*
|
2016-01-27 10:06:17 +00:00
|
|
|
* @author Addshore
|
2013-11-15 18:34:53 +00:00
|
|
|
*/
|
2018-02-21 03:46:30 +00:00
|
|
|
class ApiCoreThankUnitTest extends MediaWikiTestCase {
|
2013-11-15 18:34:53 +00:00
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
protected static $moduleName = 'thank';
|
2013-11-15 18:34:53 +00:00
|
|
|
|
|
|
|
protected function getModule() {
|
2018-02-21 03:46:30 +00:00
|
|
|
return new ApiCoreThank( new ApiMain(), self::$moduleName );
|
2013-11-15 18:34:53 +00:00
|
|
|
}
|
|
|
|
|
2019-04-26 16:12:19 +00:00
|
|
|
private function createBlock( $options ) {
|
|
|
|
$options = array_merge( [
|
|
|
|
'address' => 'Test user',
|
|
|
|
'by' => 1,
|
|
|
|
'reason' => __METHOD__,
|
|
|
|
'timestamp' => wfTimestamp( TS_MW ),
|
|
|
|
'expiry' => 'infinity',
|
|
|
|
], $options );
|
2019-05-31 15:44:56 +00:00
|
|
|
return new DatabaseBlock( $options );
|
2019-04-26 16:12:19 +00:00
|
|
|
}
|
|
|
|
|
2013-11-15 18:34:53 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider provideDieOnBadUser
|
|
|
|
* @covers ApiThank::dieOnBadUser
|
2019-04-26 16:12:19 +00:00
|
|
|
* @covers ApiThank::dieOnSitewideBlockedUser
|
2013-11-15 18:34:53 +00:00
|
|
|
*/
|
2019-04-26 16:12:19 +00:00
|
|
|
public function testDieOnBadUser( $user, $dieMethod, $expectedError ) {
|
2013-11-15 18:34:53 +00:00
|
|
|
$module = $this->getModule();
|
2019-04-26 16:12:19 +00:00
|
|
|
$method = new ReflectionMethod( $module, $dieMethod );
|
2013-11-15 18:34:53 +00:00
|
|
|
$method->setAccessible( true );
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
if ( $expectedError ) {
|
2019-10-12 22:55:31 +00:00
|
|
|
$this->expectException( ApiUsageException::class );
|
|
|
|
$this->expectExceptionMessage( $expectedError );
|
2013-11-15 18:34:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$method->invoke( $module, $user );
|
2016-04-22 20:13:56 +00:00
|
|
|
// perhaps the method should return true.. For now we must do this
|
2013-11-15 18:34:53 +00:00
|
|
|
$this->assertTrue( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideDieOnBadUser() {
|
2016-04-22 20:13:56 +00:00
|
|
|
$testCases = [];
|
2013-11-15 18:34:53 +00:00
|
|
|
|
2019-10-13 09:41:05 +00:00
|
|
|
$mockUser = $this->createMock( 'User' );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
2019-04-26 16:12:19 +00:00
|
|
|
$testCases[ 'anon' ] = [
|
|
|
|
$mockUser,
|
|
|
|
'dieOnBadUser',
|
|
|
|
'Anonymous users cannot send thanks'
|
|
|
|
];
|
2013-11-15 18:34:53 +00:00
|
|
|
|
2019-10-13 09:41:05 +00:00
|
|
|
$mockUser = $this->createMock( 'User' );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'pingLimiter' )
|
|
|
|
->will( $this->returnValue( true ) );
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
$testCases[ 'ping' ] = [
|
|
|
|
$mockUser,
|
2019-04-26 16:12:19 +00:00
|
|
|
'dieOnBadUser',
|
2016-04-22 20:13:56 +00:00
|
|
|
"You've exceeded your rate limit. Please wait some time and try again"
|
|
|
|
];
|
2013-11-15 18:34:53 +00:00
|
|
|
|
2019-10-13 09:41:05 +00:00
|
|
|
$mockUser = $this->createMock( 'User' );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'pingLimiter' )
|
|
|
|
->will( $this->returnValue( false ) );
|
|
|
|
$mockUser->expects( $this->once() )
|
2019-04-26 16:12:19 +00:00
|
|
|
->method( 'isBlockedGlobally' )
|
2013-11-15 18:34:53 +00:00
|
|
|
->will( $this->returnValue( true ) );
|
2019-04-26 16:12:19 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'getGlobalBlock' )
|
|
|
|
->will( $this->returnValue(
|
|
|
|
$this->createBlock( [] )
|
|
|
|
) );
|
|
|
|
|
|
|
|
$testCases[ 'globally blocked' ] = [
|
|
|
|
$mockUser,
|
|
|
|
'dieOnBadUser',
|
|
|
|
'You have been blocked from editing'
|
|
|
|
];
|
|
|
|
|
2019-10-13 09:41:05 +00:00
|
|
|
$mockUser = $this->createMock( 'User' );
|
2016-11-03 19:16:57 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
2016-11-15 02:55:24 +00:00
|
|
|
->method( 'getBlock' )
|
2019-04-26 16:12:19 +00:00
|
|
|
->will( $this->returnValue(
|
|
|
|
$this->createBlock( [] )
|
|
|
|
) );
|
|
|
|
|
|
|
|
$testCases[ 'sitewide blocked' ] = [
|
|
|
|
$mockUser,
|
|
|
|
'dieOnSitewideBlockedUser',
|
|
|
|
'You have been blocked from editing'
|
|
|
|
];
|
|
|
|
|
2019-10-13 09:41:05 +00:00
|
|
|
$mockUser = $this->createMock( 'User' );
|
2019-04-26 16:12:19 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'getBlock' )
|
|
|
|
->will( $this->returnValue(
|
|
|
|
$this->createBlock( [
|
|
|
|
'sitewide' => false
|
|
|
|
] )
|
|
|
|
) );
|
|
|
|
|
|
|
|
$testCases[ 'partial blocked' ] = [
|
|
|
|
$mockUser,
|
|
|
|
'dieOnSitewideBlockedUser',
|
|
|
|
false
|
|
|
|
];
|
2013-11-15 18:34:53 +00:00
|
|
|
|
|
|
|
return $testCases;
|
|
|
|
}
|
|
|
|
|
2016-04-22 20:13:56 +00:00
|
|
|
// @todo test userAlreadySentThanksForRevision
|
|
|
|
// @todo test getRevisionFromParams
|
|
|
|
// @todo test getTitleFromRevision
|
|
|
|
// @todo test getSourceFromParams
|
|
|
|
// @todo test getUserIdFromRevision
|
|
|
|
// @todo test markResultSuccess
|
|
|
|
// @todo test sendThanks
|
2013-11-15 18:34:53 +00:00
|
|
|
|
|
|
|
}
|