2020-06-16 12:05:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Rest\Handler\ContributionsCountHandler;
|
|
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\RequestData;
|
2020-06-25 12:25:40 +00:00
|
|
|
use MediaWiki\Rest\RequestInterface;
|
2020-06-16 12:05:50 +00:00
|
|
|
use MediaWiki\Revision\ContributionsLookup;
|
2021-01-06 18:12:43 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2020-07-22 21:40:51 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2020-06-16 12:05:50 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Rest\Handler\ContributionsCountHandler
|
|
|
|
|
*/
|
|
|
|
|
class ContributionsCountHandlerTest extends \MediaWikiUnitTestCase {
|
2021-01-22 00:20:44 +00:00
|
|
|
|
2020-06-16 12:05:50 +00:00
|
|
|
use HandlerTestTrait;
|
|
|
|
|
|
2020-07-22 21:40:51 +00:00
|
|
|
private function newHandler( $numContributions = 5 ) {
|
2020-06-16 12:05:50 +00:00
|
|
|
/** @var MockObject|ContributionsLookup $mockContributionsLookup */
|
|
|
|
|
$mockContributionsLookup = $this->createNoOpMock( ContributionsLookup::class,
|
2020-06-25 12:25:40 +00:00
|
|
|
[ 'getContributionCount' ]
|
2020-06-16 12:05:50 +00:00
|
|
|
);
|
2020-07-22 21:40:51 +00:00
|
|
|
|
|
|
|
|
$mockContributionsLookup->method( 'getContributionCount' )->willReturn( $numContributions );
|
|
|
|
|
$mockUserNameUtils = $this->createNoOpMock( UserNameUtils::class,
|
|
|
|
|
[ 'isIP' ]
|
|
|
|
|
);
|
2020-07-29 19:43:11 +00:00
|
|
|
|
2020-07-22 21:40:51 +00:00
|
|
|
$mockUserNameUtils->method( 'isIP' )
|
2021-02-07 13:10:36 +00:00
|
|
|
->willReturnCallback( static function ( $name ) {
|
2020-07-22 21:40:51 +00:00
|
|
|
return $name === '127.0.0.1';
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
return new ContributionsCountHandler(
|
|
|
|
|
$mockContributionsLookup,
|
|
|
|
|
$mockUserNameUtils
|
|
|
|
|
);
|
2020-06-16 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
2020-06-25 12:25:40 +00:00
|
|
|
public function provideTestThatParametersAreHandledCorrectly() {
|
2020-07-22 21:40:51 +00:00
|
|
|
yield [ new RequestData( [] ), 'me' ];
|
2020-06-25 12:25:40 +00:00
|
|
|
yield [ new RequestData(
|
|
|
|
|
[ 'queryParams' => [ 'tag' => 'test' ] ]
|
2020-07-22 21:40:51 +00:00
|
|
|
), 'me' ];
|
2020-06-25 12:25:40 +00:00
|
|
|
yield [ new RequestData(
|
|
|
|
|
[ 'queryParams' => [ 'tag' => null ] ]
|
2020-07-22 21:40:51 +00:00
|
|
|
), 'me' ];
|
|
|
|
|
yield [ new RequestData(
|
2020-08-05 05:09:10 +00:00
|
|
|
[ 'pathParams' => [ 'user' => 'someUser' ], 'queryParams' => [ 'tag' => '' ] ]
|
2020-07-22 21:40:51 +00:00
|
|
|
), 'user' ];
|
2020-06-25 12:25:40 +00:00
|
|
|
yield [ new RequestData(
|
2020-08-05 05:09:10 +00:00
|
|
|
[ 'pathParams' => [ 'user' => 'someUser' ] ]
|
2020-07-22 21:40:51 +00:00
|
|
|
), 'user' ];
|
2020-06-25 12:25:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideTestThatParametersAreHandledCorrectly
|
|
|
|
|
*/
|
2020-07-22 21:40:51 +00:00
|
|
|
public function testThatParametersAreHandledCorrectly( RequestInterface $request, $mode ) {
|
2020-06-25 12:25:40 +00:00
|
|
|
$mockContributionsLookup = $this->createNoOpMock( ContributionsLookup::class,
|
|
|
|
|
[ 'getContributionCount' ]
|
|
|
|
|
);
|
2021-01-06 18:12:43 +00:00
|
|
|
$username = $request->getPathParams()['user'] ?? null;
|
|
|
|
|
$user = $username ? new UserIdentityValue( 42, $username, 24 ) : null;
|
2020-06-25 12:25:40 +00:00
|
|
|
|
|
|
|
|
$tag = $request->getQueryParams()['tag'] ?? null;
|
|
|
|
|
$mockContributionsLookup->method( 'getContributionCount' )
|
2021-01-06 18:12:43 +00:00
|
|
|
->with( $user, $this->anything(), $tag )
|
2020-06-25 12:25:40 +00:00
|
|
|
->willReturn( 123 );
|
|
|
|
|
|
2020-07-22 21:40:51 +00:00
|
|
|
$handler = $this->newHandler( 5 );
|
2020-07-29 19:43:11 +00:00
|
|
|
$validatedParams = [
|
2021-01-06 18:12:43 +00:00
|
|
|
'user' => $user,
|
2020-07-29 19:43:11 +00:00
|
|
|
'tag' => $tag ?? null,
|
|
|
|
|
];
|
2021-01-22 00:20:44 +00:00
|
|
|
$response = $this->executeHandler( $handler, $request, [ 'mode' => $mode ], [], $validatedParams, [],
|
|
|
|
|
$mode === 'me' ? $this->mockRegisteredUltimateAuthority() : null );
|
2020-06-25 12:25:40 +00:00
|
|
|
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-16 12:05:50 +00:00
|
|
|
public function testThatAnonymousUserReturns401() {
|
|
|
|
|
$handler = $this->newHandler();
|
|
|
|
|
$request = new RequestData( [] );
|
2020-08-05 05:09:10 +00:00
|
|
|
$validatedParams = [ 'user' => null, 'tag' => null ];
|
2020-06-16 12:05:50 +00:00
|
|
|
|
|
|
|
|
$this->expectExceptionObject(
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'rest-permission-denied-anon' ), 401 )
|
|
|
|
|
);
|
2021-01-06 18:12:43 +00:00
|
|
|
$this->executeHandler( $handler, $request, [ 'mode' => 'me' ], [], $validatedParams, [],
|
2021-01-22 00:20:44 +00:00
|
|
|
$this->mockAnonUltimateAuthority() );
|
2020-06-16 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideThatResponseConformsToSchema() {
|
2020-07-22 21:40:51 +00:00
|
|
|
yield [ 0, [ 'count' => 0 ], [], 'me' ];
|
|
|
|
|
yield [ 3, [ 'count' => 3 ], [], 'me' ];
|
2020-08-05 05:09:10 +00:00
|
|
|
yield [ 0, [ 'count' => 0 ], [ 'pathParams' => [ 'user' => 'someName' ] ], 'user' ];
|
|
|
|
|
yield [ 3, [ 'count' => 3 ], [ 'pathParams' => [ 'user' => 'someName' ] ] , 'user' ];
|
2020-06-16 12:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideThatResponseConformsToSchema
|
|
|
|
|
*/
|
2020-07-22 21:40:51 +00:00
|
|
|
public function testThatResponseConformsToSchema( $numContributions, $expectedResponse, $config, $mode ) {
|
|
|
|
|
$handler = $this->newHandler( $numContributions );
|
|
|
|
|
$request = new RequestData( $config );
|
2020-08-05 05:09:10 +00:00
|
|
|
$username = $request->getPathParams()['user'] ?? null;
|
2020-07-29 19:43:11 +00:00
|
|
|
$validatedParams = [
|
2021-01-06 18:12:43 +00:00
|
|
|
'user' => $username ? new UserIdentityValue( 42, $username, 24 ) : null,
|
2020-07-29 19:43:11 +00:00
|
|
|
'tag' => null
|
|
|
|
|
];
|
2020-06-16 12:05:50 +00:00
|
|
|
|
2020-07-29 19:43:11 +00:00
|
|
|
$response = $this->executeHandlerAndGetBodyData(
|
2021-01-06 18:12:43 +00:00
|
|
|
$handler, $request, [ 'mode' => $mode ], [], $validatedParams, [],
|
2021-01-22 00:20:44 +00:00
|
|
|
$this->mockRegisteredUltimateAuthority()
|
2020-07-22 21:40:51 +00:00
|
|
|
);
|
2020-07-29 19:43:11 +00:00
|
|
|
|
|
|
|
|
$this->assertSame( $expectedResponse, $response );
|
2020-07-22 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testThatUnknownUserReturns404() {
|
2020-07-29 19:43:11 +00:00
|
|
|
$username = 'UNKNOWN';
|
2020-07-22 21:40:51 +00:00
|
|
|
$handler = $this->newHandler();
|
2020-08-05 05:09:10 +00:00
|
|
|
$request = new RequestData( [ 'pathParams' => [ 'user' => $username ] ] );
|
2020-07-22 21:40:51 +00:00
|
|
|
|
2020-07-29 19:43:11 +00:00
|
|
|
$validatedParams = [
|
2021-01-06 18:12:43 +00:00
|
|
|
'user' => new UserIdentityValue( 0, $username, 0 ),
|
2020-07-29 19:43:11 +00:00
|
|
|
'tag' => null
|
|
|
|
|
];
|
|
|
|
|
|
2020-07-22 21:40:51 +00:00
|
|
|
$this->expectExceptionObject(
|
|
|
|
|
new LocalizedHttpException( new MessageValue( 'rest-nonexistent-user' ), 404 )
|
|
|
|
|
);
|
2020-07-29 19:43:11 +00:00
|
|
|
$this->executeHandler( $handler, $request, [ 'mode' => 'user' ], [], $validatedParams );
|
2020-07-22 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testThatIpUserReturns200() {
|
|
|
|
|
$handler = $this->newHandler();
|
2020-07-29 19:43:11 +00:00
|
|
|
$ipAddr = '127.0.0.1';
|
2020-08-05 05:09:10 +00:00
|
|
|
$request = new RequestData( [ 'pathParams' => [ 'user' => $ipAddr ] ] );
|
2020-07-29 19:43:11 +00:00
|
|
|
$validatedParams = [
|
2021-01-06 18:12:43 +00:00
|
|
|
'user' => new UserIdentityValue( 0, $ipAddr, 0 ),
|
2020-07-29 19:43:11 +00:00
|
|
|
'tag' => null
|
|
|
|
|
];
|
2020-07-22 21:40:51 +00:00
|
|
|
|
2020-07-29 19:43:11 +00:00
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, [ 'mode' => 'user' ], [], $validatedParams );
|
2020-07-22 21:40:51 +00:00
|
|
|
$this->assertArrayHasKey( 'count', $data );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|