aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/api/ApiOpenSearchTest.php
blob: 67de632b1d2e526779bba8a9f35d8574aa3f1639 (plain) (blame)
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
<?php

use MediaWiki\Context\RequestContext;
use Wikimedia\ParamValidator\ParamValidator;

/**
 * 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' ] );

		[ $engine, $engineFactory ] = $this->replaceSearchEngine();

		$ctx = new RequestContext();
		$apiMain = new ApiMain( $ctx );
		$api = new ApiOpenSearch(
			$apiMain,
			'opensearch',
			$this->getServiceContainer()->getLinkBatchFactory(),
			$config,
			$engineFactory,
			$this->getServiceContainer()->getUrlUtils()
		);

		$engine->method( 'getProfiles' )
			->willReturnMap( [
				[ 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'][ParamValidator::PARAM_DEFAULT] );
	}

	private function replaceSearchEngineConfig() {
		$config = $this->createMock( SearchEngineConfig::class );
		$this->setService( 'SearchEngineConfig', $config );

		return $config;
	}

	private function replaceSearchEngine() {
		$engine = $this->createMock( SearchEngine::class );
		$engineFactory = $this->createMock( SearchEngineFactory::class );
		$engineFactory->method( 'create' )
			->willReturn( $engine );
		$this->setService( 'SearchEngineFactory', $engineFactory );

		return [ $engine, $engineFactory ];
	}
}