| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest; |
| 4 |
|
| 5 |
use MediaWiki\Config\ServiceOptions; |
| 6 |
use MediaWiki\MainConfigNames; |
| 7 |
use MediaWiki\Rest\BasicAccess\StaticBasicAuthorizer; |
| 8 |
use MediaWiki\Rest\Module\Module; |
| 9 |
use MediaWiki\Rest\Reporter\PHPErrorReporter; |
| 10 |
use MediaWiki\Rest\RequestData; |
| 11 |
use MediaWiki\Rest\ResponseFactory; |
| 12 |
use MediaWiki\Rest\Router; |
| 13 |
use MediaWiki\Rest\Validator\Validator; |
| 14 |
use MediaWiki\Tests\Rest\Handler\SessionHelperTestTrait; |
| 15 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 16 |
use Psr\Container\ContainerInterface; |
| 17 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 18 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
trait RestTestTrait { |
| 28 |
use SessionHelperTestTrait; |
| 29 |
use MockAuthorityTrait; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
private function newRouter( array $params = [] ) { |
| 41 |
$responseFactory = new ResponseFactory( [] ); |
| 42 |
$responseFactory->setShowExceptionDetails( true ); |
| 43 |
|
| 44 |
$objectFactory = new ObjectFactory( |
| 45 |
$this->getMockForAbstractClass( ContainerInterface::class ) |
| 46 |
); |
| 47 |
$authority = $params['authority'] ?? $this->mockAnonUltimateAuthority(); |
| 48 |
|
| 49 |
$config = ( $params['config'] ?? [] ) + [ |
| 50 |
MainConfigNames::CanonicalServer => 'https://wiki.example.com', |
| 51 |
MainConfigNames::InternalServer => 'http://api.local:8080', |
| 52 |
MainConfigNames::RestPath => '/rest', |
| 53 |
MainConfigNames::ScriptPath => '/w', |
| 54 |
MainConfigNames::RightsUrl => 'https://rights.url', |
| 55 |
MainConfigNames::RightsText => 'your rights', |
| 56 |
MainConfigNames::EmergencyContact => '[email protected]', |
| 57 |
MainConfigNames::Sitename => 'Test Site', |
| 58 |
]; |
| 59 |
|
| 60 |
$request = $params['request'] ?? new RequestData(); |
| 61 |
|
| 62 |
return new Router( |
| 63 |
$params['routeFiles'] ?? [ MW_INSTALL_PATH . '/tests/phpunit/unit/includes/Rest/testRoutes.json' ], |
| 64 |
$params['extraRoutes'] ?? [], |
| 65 |
$params['options'] ?? new ServiceOptions( Router::CONSTRUCTOR_OPTIONS, $config ), |
| 66 |
$params['cacheBag'] ?? new EmptyBagOStuff(), |
| 67 |
$params['responseFactory'] ?? $responseFactory, |
| 68 |
$params['basicAuth'] ?? new StaticBasicAuthorizer(), |
| 69 |
$params['authority'] ?? $authority, |
| 70 |
$params['objectFactory'] ?? $objectFactory, |
| 71 |
$params['validator'] ?? new Validator( $objectFactory, $request, $authority ), |
| 72 |
$params['errorReporter'] ?? new PHPErrorReporter(), |
| 73 |
$params['hookContainer'] ?? $this->createHookContainer(), |
| 74 |
$params['session'] ?? $this->getSession( true ) |
| 75 |
); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
private function newModule( array $params ) { |
| 84 |
$objectFactory = new ObjectFactory( |
| 85 |
$this->getMockForAbstractClass( ContainerInterface::class ) |
| 86 |
); |
| 87 |
|
| 88 |
$authority = $params['authority'] ?? $this->mockAnonUltimateAuthority(); |
| 89 |
$request = $params['request'] ?? new RequestData(); |
| 90 |
|
| 91 |
$module = $this->getMockBuilder( Module::class ) |
| 92 |
->setConstructorArgs( [ |
| 93 |
$params['router'] ?? $this->newRouter( $params ), |
| 94 |
$params['pathPrefix'] ?? 'mock', |
| 95 |
$params['responseFactory'] ?? new ResponseFactory( [] ), |
| 96 |
$params['basicAuth'] ?? new StaticBasicAuthorizer(), |
| 97 |
$params['objectFactory'] ?? $objectFactory, |
| 98 |
$params['restValidator'] ?? new Validator( $objectFactory, $request, $authority ), |
| 99 |
$params['errorReporter'] ?? new PHPErrorReporter() |
| 100 |
] ) |
| 101 |
->onlyMethods( [] ) |
| 102 |
->getMockForAbstractClass(); |
| 103 |
|
| 104 |
return $module; |
| 105 |
} |
| 106 |
|
| 107 |
} |