aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/api/ApiQueryPrefixSearchTest.php
blob: d495ef0f31c3d2cbab68a391b2a1a1d5e8a49e2e (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
<?php

/**
 * @group API
 * @group medium
 * @group Database
 *
 * @covers ApiQueryPrefixSearch
 */
class ApiQueryPrefixSearchTest extends ApiTestCase {
	private const TEST_QUERY = 'unittest';

	public function setUp() : void {
		parent::setUp();
		$this->setMwGlobals( [
			'wgSearchType' => MockCompletionSearchEngine::class,
		] );
		MockCompletionSearchEngine::clearMockResults();
		$results = [];
		foreach ( range( 0, 10 ) as $i ) {
			$title = "Search_Result_$i";
			$results[] = $title;
			$this->editPage( $title, 'hi there' );
		}
		MockCompletionSearchEngine::addMockResults( self::TEST_QUERY, $results );
	}

	public function offsetContinueProvider() {
		return [
			'no offset' => [ 2, 2, 0, 2 ],
			'with offset' => [ 7, 2, 5, 2 ],
			'past end, no offset' => [ null, 11, 0, 20 ],
			'past end, with offset' => [ null, 5, 6, 10 ],
		];
	}

	/**
	 * @dataProvider offsetContinueProvider
	 */
	public function testOffsetContinue( $expectedOffset, $expectedResults, $offset, $limit ) {
		$response = $this->doApiRequest( [
			'action' => 'query',
			'list' => 'prefixsearch',
			'pssearch' => self::TEST_QUERY,
			'psoffset' => $offset,
			'pslimit' => $limit,
		] );
		$result = $response[0];
		$this->assertArrayNotHasKey( 'warnings', $result );
		$suggestions = $result['query']['prefixsearch'];
		$this->assertCount( $expectedResults, $suggestions );
		if ( $expectedOffset == null ) {
			$this->assertArrayNotHasKey( 'continue', $result );
		} else {
			$this->assertArrayHasKey( 'continue', $result );
			$this->assertEquals( $expectedOffset, $result['continue']['psoffset'] );
		}
	}
}