2020-03-02 12:28:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use ApiBase;
|
|
|
|
|
use ApiMain;
|
|
|
|
|
use Exception;
|
2022-05-04 16:29:51 +00:00
|
|
|
use Language;
|
2022-10-28 10:04:25 +00:00
|
|
|
use MediaWiki\Request\FauxRequest;
|
2020-03-02 12:28:10 +00:00
|
|
|
use PHPUnit\Framework\MockObject\MockBuilder;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2020-03-12 12:54:51 +00:00
|
|
|
use RequestContext;
|
2020-03-02 12:28:10 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A trait providing utility functions for testing Handler classes
|
|
|
|
|
* derived from ActionModuleBasedHandler.
|
|
|
|
|
* This trait is intended to be used on subclasses of MediaWikiUnitTestCase
|
|
|
|
|
* or MediaWikiIntegrationTestCase.
|
|
|
|
|
*
|
|
|
|
|
* @package MediaWiki\Tests\Rest\Handler
|
2021-03-25 10:38:39 +00:00
|
|
|
* @method MockBuilder getMockBuilder(string $className)
|
2020-03-02 12:28:10 +00:00
|
|
|
*/
|
|
|
|
|
trait ActionModuleBasedHandlerTestTrait {
|
|
|
|
|
|
|
|
|
|
use HandlerTestTrait;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param ApiMain $main
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param array $resultData
|
|
|
|
|
* @param Exception|null $throwException
|
|
|
|
|
*
|
|
|
|
|
* @return ApiBase|MockObject
|
|
|
|
|
*/
|
|
|
|
|
private function getDummyApiModule(
|
|
|
|
|
ApiMain $main,
|
|
|
|
|
$name,
|
|
|
|
|
$resultData,
|
|
|
|
|
Exception $throwException = null
|
|
|
|
|
) {
|
|
|
|
|
/** @var ApiBase|MockObject $module */
|
|
|
|
|
$module = $this->getMockBuilder( ApiBase::class )
|
|
|
|
|
->setConstructorArgs( [ $main, $name ] )
|
|
|
|
|
->onlyMethods( [ 'execute' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$module->method( 'execute' )
|
|
|
|
|
->willReturnCallback(
|
2021-02-07 13:10:36 +00:00
|
|
|
static function () use ( $module, $resultData, $throwException ) {
|
2020-03-02 12:28:10 +00:00
|
|
|
if ( $throwException ) {
|
|
|
|
|
throw $throwException;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$res = $module->getResult();
|
|
|
|
|
foreach ( $resultData as $key => $value ) {
|
|
|
|
|
$res->addValue( null, $key, $value );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $module;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 12:54:51 +00:00
|
|
|
/**
|
2021-01-16 19:44:17 +00:00
|
|
|
* @param bool $csrfSafe
|
2020-03-12 12:54:51 +00:00
|
|
|
* @return ApiMain
|
|
|
|
|
*/
|
|
|
|
|
private function getApiMain( $csrfSafe = false ) {
|
2022-05-04 16:29:51 +00:00
|
|
|
$session = $this->getSession( $csrfSafe );
|
2020-03-12 12:54:51 +00:00
|
|
|
|
2022-10-28 10:04:25 +00:00
|
|
|
// NOTE: This being a MediaWiki\Request\FauxRequest instance triggers special case behavior
|
2020-03-12 12:54:51 +00:00
|
|
|
// in ApiMain, causing ApiMain::isInternalMode() to return true. Among other things,
|
|
|
|
|
// this causes ApiMain to throw errors rather than encode them in the result data.
|
2022-10-28 10:04:25 +00:00
|
|
|
/** @var MockObject|\MediaWiki\Request\FauxRequest $fauxRequest */
|
2020-03-12 12:54:51 +00:00
|
|
|
$fauxRequest = $this->getMockBuilder( FauxRequest::class )
|
|
|
|
|
->onlyMethods( [ 'getSession', 'getSessionId' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$fauxRequest->method( 'getSession' )->willReturn( $session );
|
|
|
|
|
$fauxRequest->method( 'getSessionId' )->willReturn( $session->getSessionId() );
|
|
|
|
|
|
2022-05-04 16:29:51 +00:00
|
|
|
/** @var Language|MockObject $language */
|
|
|
|
|
$language = $this->createNoOpMock( Language::class );
|
2021-08-05 06:54:11 +00:00
|
|
|
$testContext = RequestContext::getMain();
|
|
|
|
|
|
2020-03-12 12:54:51 +00:00
|
|
|
$fauxContext = new RequestContext();
|
|
|
|
|
$fauxContext->setRequest( $fauxRequest );
|
|
|
|
|
$fauxContext->setUser( $testContext->getUser() );
|
2022-05-04 16:29:51 +00:00
|
|
|
$fauxContext->setLanguage( $language );
|
2020-03-12 12:54:51 +00:00
|
|
|
|
2022-05-04 16:29:51 +00:00
|
|
|
return new ApiMain( $fauxContext, true );
|
2020-03-12 12:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
2020-03-02 12:28:10 +00:00
|
|
|
}
|