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
|
|
|
}
|
|
|
|
|
2023-05-20 16:31:17 +00:00
|
|
|
private static function createBlock( $options ) {
|
2019-04-26 16:12:19 +00:00
|
|
|
$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
|
|
|
*/
|
2023-05-20 16:31:17 +00:00
|
|
|
public function testDieOnBadUser(
|
|
|
|
$mockAnon,
|
|
|
|
$mockPingLimited,
|
|
|
|
$mockBlock,
|
|
|
|
$dieMethod,
|
|
|
|
$expectedError
|
|
|
|
) {
|
|
|
|
$user = $this->createMock( User::class );
|
|
|
|
if ( $mockAnon !== null ) {
|
|
|
|
$user->expects( $this->once() )
|
|
|
|
->method( 'isAnon' )
|
|
|
|
->willReturn( $mockAnon );
|
|
|
|
}
|
|
|
|
if ( $mockPingLimited !== null ) {
|
|
|
|
$user->expects( $this->once() )
|
|
|
|
->method( 'pingLimiter' )
|
|
|
|
->willReturn( $mockPingLimited );
|
|
|
|
}
|
|
|
|
if ( $mockBlock !== null ) {
|
|
|
|
$user->expects( $this->once() )
|
|
|
|
->method( 'getBlock' )
|
|
|
|
->willReturn( $mockBlock );
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2023-05-20 16:31:17 +00:00
|
|
|
public static function provideDieOnBadUser() {
|
|
|
|
return [
|
|
|
|
'anon' => [
|
|
|
|
true,
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'dieOnBadUser',
|
|
|
|
'notloggedin'
|
|
|
|
],
|
|
|
|
'ping' => [
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
null,
|
|
|
|
'dieOnBadUser',
|
|
|
|
'ratelimited'
|
|
|
|
],
|
|
|
|
'sitewide blocked' => [
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
self::createBlock( [] ),
|
|
|
|
'dieOnUserBlockedFromThanks',
|
|
|
|
'blocked'
|
|
|
|
],
|
|
|
|
'partial blocked' => [
|
|
|
|
null,
|
|
|
|
null,
|
|
|
|
self::createBlock( [ 'sitewide' => false ] ),
|
|
|
|
'dieOnUserBlockedFromThanks',
|
|
|
|
false
|
|
|
|
],
|
2016-04-22 20:13:56 +00:00
|
|
|
];
|
2013-11-15 18:34:53 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
}
|