3 namespace MediaWiki\Tests\Api
;
5 use MediaWiki\Api\ApiMain
;
6 use MediaWiki\Api\ApiOpenSearch
;
7 use MediaWiki\Context\RequestContext
;
8 use MediaWikiIntegrationTestCase
;
10 use SearchEngineConfig
;
11 use SearchEngineFactory
;
12 use Wikimedia\ParamValidator\ParamValidator
;
15 * TODO convert to unit test, no integration is needed
17 * @covers \MediaWiki\Api\ApiOpenSearch
19 class ApiOpenSearchTest
extends MediaWikiIntegrationTestCase
{
20 public function testGetAllowedParams() {
21 $config = $this->replaceSearchEngineConfig();
22 $config->method( 'getSearchTypes' )
23 ->willReturn( [ 'the one ring' ] );
25 [ $engine, $engineFactory ] = $this->replaceSearchEngine();
27 $ctx = new RequestContext();
28 $apiMain = new ApiMain( $ctx );
29 $api = new ApiOpenSearch(
32 $this->getServiceContainer()->getLinkBatchFactory(),
35 $this->getServiceContainer()->getUrlUtils()
38 $engine->method( 'getProfiles' )
40 [ SearchEngine
::COMPLETION_PROFILE_TYPE
, $api->getUser(), [
43 'desc-message' => 'normal-message',
48 'desc-message' => 'strict-message',
53 $params = $api->getAllowedParams();
55 $this->assertArrayNotHasKey( 'offset', $params );
56 $this->assertArrayHasKey( 'profile', $params, print_r( $params, true ) );
57 $this->assertEquals( 'normal', $params['profile'][ParamValidator
::PARAM_DEFAULT
] );
60 private function replaceSearchEngineConfig() {
61 $config = $this->createMock( SearchEngineConfig
::class );
62 $this->setService( 'SearchEngineConfig', $config );
67 private function replaceSearchEngine() {
68 $engine = $this->createMock( SearchEngine
::class );
69 $engineFactory = $this->createMock( SearchEngineFactory
::class );
70 $engineFactory->method( 'create' )
71 ->willReturn( $engine );
72 $this->setService( 'SearchEngineFactory', $engineFactory );
74 return [ $engine, $engineFactory ];