2013-11-15 18:34:53 +00:00
|
|
|
<?php
|
|
|
|
|
2024-10-20 11:21:21 +00:00
|
|
|
use MediaWiki\Api\ApiMain;
|
2019-05-31 15:44:56 +00:00
|
|
|
use MediaWiki\Block\DatabaseBlock;
|
2023-05-19 11:38:58 +00:00
|
|
|
use MediaWiki\Extension\Thanks\Api\ApiCoreThank;
|
2024-05-11 08:57:36 +00:00
|
|
|
use MediaWiki\Tests\Api\ApiTestCase;
|
2024-01-04 21:22:56 +00:00
|
|
|
use MediaWiki\User\User;
|
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
|
2023-08-06 19:30:56 +00:00
|
|
|
* @group Database
|
2013-11-15 18:34:53 +00:00
|
|
|
*
|
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() {
|
2023-05-19 11:38:58 +00:00
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
return new ApiCoreThank(
|
|
|
|
new ApiMain(),
|
|
|
|
'thank',
|
|
|
|
$services->getPermissionManager(),
|
|
|
|
$services->getRevisionStore(),
|
|
|
|
$services->getUserFactory(),
|
2023-05-26 13:02:42 +00:00
|
|
|
$services->getService( 'ThanksLogStore' )
|
2023-05-19 11:38:58 +00:00
|
|
|
);
|
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
|
2023-05-19 11:38:58 +00:00
|
|
|
* @covers \MediaWiki\Extension\Thanks\Api\ApiThank::dieOnBadUser
|
|
|
|
* @covers \MediaWiki\Extension\Thanks\Api\ApiThank::dieOnUserBlockedFromThanks
|
2013-11-15 18:34:53 +00:00
|
|
|
*/
|
2023-05-20 16:31:17 +00:00
|
|
|
public function testDieOnBadUser(
|
2023-09-13 21:07:52 +00:00
|
|
|
$mockisNamed,
|
2023-05-20 16:31:17 +00:00
|
|
|
$mockPingLimited,
|
|
|
|
$mockBlock,
|
|
|
|
$dieMethod,
|
|
|
|
$expectedError
|
|
|
|
) {
|
|
|
|
$user = $this->createMock( User::class );
|
2023-09-13 21:07:52 +00:00
|
|
|
if ( $mockisNamed !== null ) {
|
2023-05-20 16:31:17 +00:00
|
|
|
$user->expects( $this->once() )
|
2023-09-13 21:07:52 +00:00
|
|
|
->method( 'isNamed' )
|
|
|
|
->willReturn( $mockisNamed );
|
2023-05-20 16:31:17 +00:00
|
|
|
}
|
|
|
|
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' => [
|
2023-09-13 21:07:52 +00:00
|
|
|
false,
|
2023-05-20 16:31:17 +00:00
|
|
|
null,
|
|
|
|
null,
|
|
|
|
'dieOnBadUser',
|
|
|
|
'notloggedin'
|
|
|
|
],
|
|
|
|
'ping' => [
|
2023-09-13 21:07:52 +00:00
|
|
|
true,
|
2023-05-20 16:31:17 +00:00
|
|
|
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
|
|
|
|
|
|
|
}
|