1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
<?php
namespace MediaWiki\Tests\Api;
use ArrayAccess;
use LogicException;
use MediaWiki\Api\ApiBase;
use MediaWiki\Api\ApiErrorFormatter;
use MediaWiki\Api\ApiMain;
use MediaWiki\Api\ApiMessage;
use MediaWiki\Api\ApiQueryTokens;
use MediaWiki\Api\ApiResult;
use MediaWiki\Api\ApiUsageException;
use MediaWiki\Context\RequestContext;
use MediaWiki\MediaWikiServices;
use MediaWiki\Message\Message;
use MediaWiki\Permissions\Authority;
use MediaWiki\Request\FauxRequest;
use MediaWiki\Session\Session;
use MediaWiki\Session\SessionManager;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWikiLangTestCase;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Constraint\Constraint;
use ReturnTypeWillChange;
abstract class ApiTestCase extends MediaWikiLangTestCase {
use MockAuthorityTrait;
/** @var string */
protected static $apiUrl;
/** @var ApiErrorFormatter|null */
protected static $errorFormatter = null;
/**
* @var ApiTestContext
*/
protected $apiContext;
protected function setUp(): void {
global $wgServer;
parent::setUp();
self::$apiUrl = $wgServer . wfScript( 'api' );
// HACK: Avoid creating test users in the DB if the test may not need them.
$getters = [
'sysop' => fn () => $this->getTestSysop(),
'uploader' => fn () => $this->getTestUser(),
];
$fakeUserArray = new class ( $getters ) implements ArrayAccess {
private array $getters;
private array $extraUsers = [];
public function __construct( array $getters ) {
$this->getters = $getters;
}
public function offsetExists( $offset ): bool {
return isset( $this->getters[$offset] ) || isset( $this->extraUsers[$offset] );
}
#[ReturnTypeWillChange]
public function offsetGet( $offset ) {
if ( isset( $this->getters[$offset] ) ) {
return ( $this->getters[$offset] )();
}
if ( isset( $this->extraUsers[$offset] ) ) {
return $this->extraUsers[$offset];
}
throw new LogicException( "Requested unknown user $offset" );
}
public function offsetSet( $offset, $value ): void {
$this->extraUsers[$offset] = $value;
}
public function offsetUnset( $offset ): void {
unset( $this->getters[$offset] );
unset( $this->extraUsers[$offset] );
}
};
self::$users = $fakeUserArray;
$this->setRequest( new FauxRequest( [] ) );
$this->apiContext = new ApiTestContext();
}
protected function tearDown(): void {
// Avoid leaking session over tests
SessionManager::getGlobalSession()->clear();
ApiBase::clearCacheForTest();
parent::tearDown();
}
/**
* Does the API request and returns the result.
*
* @param array $params
* @param array|null $session
* @param bool $appendModule
* @param Authority|null $performer
* @param string|null $tokenType Set to a string like 'csrf' to send an
* appropriate token
* @param string|null $paramPrefix Prefix to prepend to parameters
* @return array List of:
* - the result data (array)
* - the request (WebRequest)
* - the session data of the request (array)
* - if $appendModule is true, the Api module $module
* @throws ApiUsageException
*/
protected function doApiRequest( array $params, ?array $session = null,
$appendModule = false, ?Authority $performer = null, $tokenType = null,
$paramPrefix = null
) {
global $wgRequest;
// re-use existing global session by default
$session ??= $wgRequest->getSessionArray();
$sessionObj = SessionManager::singleton()->getEmptySession();
if ( $session !== null ) {
foreach ( $session as $key => $value ) {
$sessionObj->set( $key, $value );
}
}
// set up global environment
if ( !$performer && !$this->needsDB() ) {
$performer = $this->mockRegisteredUltimateAuthority();
}
if ( $performer ) {
$legacyUser = $this->getServiceContainer()->getUserFactory()->newFromAuthority( $performer );
$contextUser = $legacyUser;
// Clone the user object, because something in Session code will replace its user with "Unknown user"
// if it doesn't exist. But that'll also change $contextUser, and the token won't match (T341953).
$sessionUser = clone $contextUser;
} else {
$contextUser = $this->getTestSysop()->getUser();
$performer = $contextUser;
$sessionUser = $contextUser;
}
$sessionObj->setUser( $sessionUser );
if ( $tokenType !== null ) {
if ( $tokenType === 'auto' ) {
$tokenType = ( new ApiMain() )->getModuleManager()
->getModule( $params['action'], 'action' )->needsToken();
}
if ( $tokenType !== false ) {
$params['token'] = ApiQueryTokens::getToken(
$contextUser,
$sessionObj,
ApiQueryTokens::getTokenTypeSalts()[$tokenType]
)->toString();
}
}
// prepend parameters with prefix
if ( $paramPrefix !== null && $paramPrefix !== '' ) {
$prefixedParams = [];
foreach ( $params as $key => $value ) {
$prefixedParams[$paramPrefix . $key] = $value;
}
$params = $prefixedParams;
}
$wgRequest = $this->buildFauxRequest( $params, $sessionObj );
RequestContext::getMain()->setRequest( $wgRequest );
RequestContext::getMain()->setAuthority( $performer );
RequestContext::getMain()->setUser( $sessionUser );
// set up local environment
$context = $this->apiContext->newTestContext( $wgRequest, $performer );
$module = new ApiMain( $context, true );
// run it!
$module->execute();
// construct result
$results = [
$module->getResult()->getResultData( null, [ 'Strip' => 'all' ] ),
$context->getRequest(),
$context->getRequest()->getSessionArray()
];
if ( $appendModule ) {
$results[] = $module;
}
return $results;
}
/**
* @since 1.37
* @param array $params
* @param Session|array|null $session
* @return FauxRequest
*/
protected function buildFauxRequest( $params, $session ) {
return new FauxRequest( $params, true, $session );
}
/**
* Convenience function to access the token parameter of doApiRequest()
* more succinctly.
*
* @param array $params Key-value API params
* @param array|null $session Session array
* @param Authority|null $performer A User object for the context
* @param string $tokenType Which token type to pass
* @param string|null $paramPrefix Prefix to prepend to parameters
* @return array Result of the API call
*/
protected function doApiRequestWithToken( array $params, ?array $session = null,
?Authority $performer = null, $tokenType = 'auto', $paramPrefix = null
) {
return $this->doApiRequest( $params, $session, false, $performer, $tokenType, $paramPrefix );
}
protected static function getErrorFormatter() {
self::$errorFormatter ??= new ApiErrorFormatter(
new ApiResult( false ),
MediaWikiServices::getInstance()->getLanguageFactory()->getLanguage( 'en' ),
'none'
);
return self::$errorFormatter;
}
public static function apiExceptionHasCode( ApiUsageException $ex, $code ) {
return (bool)array_filter(
self::getErrorFormatter()->arrayFromStatus( $ex->getStatusValue() ),
static function ( $e ) use ( $code ) {
return is_array( $e ) && $e['code'] === $code;
}
);
}
/**
* Expect an ApiUsageException to be thrown with the given parameters, which are the same as
* ApiUsageException::newWithMessage()'s parameters. This allows checking for an exception
* whose text is given by a message key instead of text, so as not to hard-code the message's
* text into test code.
*
* @deprecated since 1.43; use expectApiErrorCode() instead, it's better to test error codes than messages
* @param string|array|Message $msg
* @param string|null $code
* @param array|null $data
* @param int $httpCode
*/
protected function setExpectedApiException(
$msg, $code = null, ?array $data = null, $httpCode = 0
) {
$expected = ApiUsageException::newWithMessage( null, $msg, $code, $data, $httpCode );
$this->expectException( ApiUsageException::class );
$this->expectExceptionMessage( $expected->getMessage() );
}
private ?string $expectedApiErrorCode;
/**
* Expect an ApiUsageException that results in the given API error code to be thrown.
*
* Note that you can't mix this method with standard PHPUnit expectException() methods,
* as PHPUnit will catch the exception and prevent us from testing it.
*
* @since 1.41
* @param string $expectedCode
*/
protected function expectApiErrorCode( string $expectedCode ) {
$this->expectedApiErrorCode = $expectedCode;
}
/**
* Assert that an ApiUsageException will result in the given API error code being outputted.
*
* @since 1.41
* @param string $expectedCode
* @param ApiUsageException $exception
* @param string $message
*/
protected function assertApiErrorCode( string $expectedCode, ApiUsageException $exception, string $message = '' ) {
$constraint = new class( $expectedCode ) extends Constraint {
private string $expectedApiErrorCode;
public function __construct( string $expected ) {
$this->expectedApiErrorCode = $expected;
}
public function toString(): string {
return 'API error code is ';
}
private function getApiErrorCode( $other ) {
if ( !$other instanceof ApiUsageException ) {
return null;
}
$errors = $other->getStatusValue()->getMessages();
if ( count( $errors ) === 0 ) {
return '(no error)';
} elseif ( count( $errors ) > 1 ) {
return '(multiple errors)';
}
return ApiMessage::create( $errors[0] )->getApiCode();
}
protected function matches( $other ): bool {
return $this->getApiErrorCode( $other ) === $this->expectedApiErrorCode;
}
protected function failureDescription( $other ): string {
return sprintf(
'%s is equal to expected API error code %s',
$this->exporter()->export( $this->getApiErrorCode( $other ) ),
$this->exporter()->export( $this->expectedApiErrorCode )
);
}
};
$this->assertThat( $exception, $constraint, $message );
}
/**
* @inheritDoc
*
* Adds support for expectApiErrorCode().
*/
protected function runTest() {
try {
$testResult = parent::runTest();
} catch ( ApiUsageException $exception ) {
if ( !isset( $this->expectedApiErrorCode ) ) {
throw $exception;
}
$this->assertApiErrorCode( $this->expectedApiErrorCode, $exception );
return null;
}
if ( !isset( $this->expectedApiErrorCode ) ) {
return $testResult;
}
throw new AssertionFailedError(
sprintf(
'Failed asserting that exception with API error code "%s" is thrown',
$this->expectedApiErrorCode
)
);
}
}
/** @deprecated class alias since 1.42 */
class_alias( ApiTestCase::class, 'ApiTestCase' );
|