2013-11-15 18:34:53 +00:00
|
|
|
<?php
|
|
|
|
|
2019-05-31 15:44:56 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2021-03-14 03:43:32 +00:00
|
|
|
use MediaWiki\Extension\Thanks\ApiCoreThank;
|
2019-05-31 15:44:56 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
protected function getModule() {
|
2021-01-17 19:15:26 +00:00
|
|
|
return new ApiCoreThank( new ApiMain(), 'thank' );
|
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
|
2021-03-14 03:43:32 +00:00
|
|
|
* @covers \MediaWiki\Extension\Thanks\ApiThank::dieOnBadUser
|
|
|
|
* @covers \MediaWiki\Extension\Thanks\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
|
|
|
|
2021-04-08 19:14:44 +00:00
|
|
|
$mockUser = $this->createMock( User::class );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( true );
|
2013-11-15 18:34:53 +00:00
|
|
|
|
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
|
|
|
|
2021-04-08 19:14:44 +00:00
|
|
|
$mockUser = $this->createMock( User::class );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( false );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'pingLimiter' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( true );
|
2013-11-15 18:34:53 +00:00
|
|
|
|
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
|
|
|
|
2021-04-08 19:14:44 +00:00
|
|
|
$mockUser = $this->createMock( User::class );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( false );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'pingLimiter' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( false );
|
2013-11-15 18:34:53 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
2019-04-26 16:12:19 +00:00
|
|
|
->method( 'isBlockedGlobally' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( true );
|
2019-04-26 16:12:19 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'getGlobalBlock' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( $this->createBlock( [] ) );
|
2019-04-26 16:12:19 +00:00
|
|
|
|
|
|
|
$testCases[ 'globally blocked' ] = [
|
|
|
|
$mockUser,
|
|
|
|
'dieOnBadUser',
|
|
|
|
'You have been blocked from editing'
|
|
|
|
];
|
|
|
|
|
2021-04-08 19:14:44 +00:00
|
|
|
$mockUser = $this->createMock( User::class );
|
2016-11-03 19:16:57 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
2016-11-15 02:55:24 +00:00
|
|
|
->method( 'getBlock' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn( $this->createBlock( [] ) );
|
2019-04-26 16:12:19 +00:00
|
|
|
|
|
|
|
$testCases[ 'sitewide blocked' ] = [
|
|
|
|
$mockUser,
|
|
|
|
'dieOnSitewideBlockedUser',
|
|
|
|
'You have been blocked from editing'
|
|
|
|
];
|
|
|
|
|
2021-04-08 19:14:44 +00:00
|
|
|
$mockUser = $this->createMock( User::class );
|
2019-04-26 16:12:19 +00:00
|
|
|
$mockUser->expects( $this->once() )
|
|
|
|
->method( 'getBlock' )
|
2021-03-14 03:43:32 +00:00
|
|
|
->willReturn(
|
|
|
|
$this->createBlock( [ 'sitewide' => false ] )
|
|
|
|
);
|
2019-04-26 16:12:19 +00:00
|
|
|
|
|
|
|
$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
|
|
|
|
|
|
|
}
|