Used in api classes where services can be injected, no need to rely on MediaWikiServices Will make it possible to convert ApiOpenSearchTest to a unit test, since everything is injected, but that will be done in a follow-up Plus some minor cleanup to SearchApi Change-Id: If0a3a60f1ead897947143b57d98a3a506387f6d5
70 lines
1.8 KiB
PHP
70 lines
1.8 KiB
PHP
<?php
|
|
|
|
/**
|
|
* TODO convert to unit test, no integration is needed
|
|
*
|
|
* @covers ApiOpenSearch
|
|
*/
|
|
class ApiOpenSearchTest extends MediaWikiIntegrationTestCase {
|
|
public function testGetAllowedParams() {
|
|
$config = $this->replaceSearchEngineConfig();
|
|
$config->method( 'getSearchTypes' )
|
|
->willReturn( [ 'the one ring' ] );
|
|
|
|
list( $engine, $engineFactory ) = $this->replaceSearchEngine();
|
|
|
|
$ctx = new RequestContext();
|
|
$apiMain = new ApiMain( $ctx );
|
|
$api = new ApiOpenSearch(
|
|
$apiMain,
|
|
'opensearch',
|
|
$this->getServiceContainer()->getLinkBatchFactory(),
|
|
$config,
|
|
$engineFactory
|
|
);
|
|
|
|
$engine->method( 'getProfiles' )
|
|
->will( $this->returnValueMap( [
|
|
[ SearchEngine::COMPLETION_PROFILE_TYPE, $api->getUser(), [
|
|
[
|
|
'name' => 'normal',
|
|
'desc-message' => 'normal-message',
|
|
'default' => true,
|
|
],
|
|
[
|
|
'name' => 'strict',
|
|
'desc-message' => 'strict-message',
|
|
],
|
|
] ],
|
|
] ) );
|
|
|
|
$params = $api->getAllowedParams();
|
|
|
|
$this->assertArrayNotHasKey( 'offset', $params );
|
|
$this->assertArrayHasKey( 'profile', $params, print_r( $params, true ) );
|
|
$this->assertEquals( 'normal', $params['profile'][ApiBase::PARAM_DFLT] );
|
|
}
|
|
|
|
private function replaceSearchEngineConfig() {
|
|
$config = $this->getMockBuilder( SearchEngineConfig::class )
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$this->setService( 'SearchEngineConfig', $config );
|
|
|
|
return $config;
|
|
}
|
|
|
|
private function replaceSearchEngine() {
|
|
$engine = $this->getMockBuilder( SearchEngine::class )
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$engineFactory = $this->getMockBuilder( SearchEngineFactory::class )
|
|
->disableOriginalConstructor()
|
|
->getMock();
|
|
$engineFactory->method( 'create' )
|
|
->willReturn( $engine );
|
|
$this->setService( 'SearchEngineFactory', $engineFactory );
|
|
|
|
return [ $engine, $engineFactory ];
|
|
}
|
|
}
|