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;
|
2021-05-31 16:28:29 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
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
|
|
|
*/
|
2023-04-26 11:16:58 +00:00
|
|
|
class ApiCoreThankUnitTest extends ApiTestCase {
|
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',
|
2021-05-31 16:28:29 +00:00
|
|
|
'by' => new UserIdentityValue( 1, 'TestUser' ),
|
2019-04-26 16:12:19 +00:00
|
|
|
'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
|
2021-04-20 15:41:21 +00:00
|
|
|
* @covers \MediaWiki\Extension\Thanks\ApiThank::dieOnUserBlockedFromThanks
|
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 ) {
|
2023-04-26 11:16:58 +00:00
|
|
|
$this->expectApiErrorCode( $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',
|
2023-04-26 11:16:58 +00:00
|
|
|
'notloggedin'
|
2019-04-26 16:12:19 +00:00
|
|
|
];
|
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',
|
2023-04-26 11:16:58 +00:00
|
|
|
'ratelimited'
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
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 );
|
2019-04-26 16:12:19 +00:00
|
|
|
|
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,
|
2021-04-20 15:41:21 +00:00
|
|
|
'dieOnUserBlockedFromThanks',
|
2023-04-26 11:16:58 +00:00
|
|
|
'blocked'
|
2019-04-26 16:12:19 +00:00
|
|
|
];
|
|
|
|
|
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,
|
2021-04-20 15:41:21 +00:00
|
|
|
'dieOnUserBlockedFromThanks',
|
2019-04-26 16:12:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
}
|