2020-02-27 22:00:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Rest\Handler;
|
|
|
|
|
|
|
|
|
|
use HashConfig;
|
2020-04-15 12:33:15 +00:00
|
|
|
use InvalidArgumentException;
|
2020-02-27 22:00:28 +00:00
|
|
|
use Language;
|
2022-08-31 14:51:57 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2022-01-13 19:35:27 +00:00
|
|
|
use MediaWiki\Linker\LinkTarget;
|
2022-08-17 20:33:06 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2021-01-22 00:20:44 +00:00
|
|
|
use MediaWiki\Page\PageIdentity;
|
2022-01-13 19:35:27 +00:00
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
|
|
|
|
use MediaWiki\Page\PageStore;
|
|
|
|
|
use MediaWiki\Page\ProperPageIdentity;
|
|
|
|
|
use MediaWiki\Page\RedirectLookup;
|
2021-10-08 20:05:15 +00:00
|
|
|
use MediaWiki\Permissions\PermissionManager;
|
2020-02-27 22:00:28 +00:00
|
|
|
use MediaWiki\Rest\Handler\SearchHandler;
|
|
|
|
|
use MediaWiki\Rest\LocalizedHttpException;
|
|
|
|
|
use MediaWiki\Rest\RequestData;
|
2020-04-15 12:33:15 +00:00
|
|
|
use MediaWiki\Search\Entity\SearchResultThumbnail;
|
2022-08-31 14:51:57 +00:00
|
|
|
use MediaWiki\Search\SearchResultThumbnailProvider;
|
2022-01-13 19:35:27 +00:00
|
|
|
use MediaWiki\Tests\Unit\DummyServicesTrait;
|
2022-04-22 12:39:40 +00:00
|
|
|
use MediaWiki\User\UserOptionsLookup;
|
2020-02-27 22:00:28 +00:00
|
|
|
use MockSearchResultSet;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use SearchEngine;
|
|
|
|
|
use SearchEngineFactory;
|
|
|
|
|
use SearchResult;
|
2020-03-25 17:21:12 +00:00
|
|
|
use SearchResultSet;
|
|
|
|
|
use SearchSuggestion;
|
|
|
|
|
use SearchSuggestionSet;
|
2020-02-27 22:00:28 +00:00
|
|
|
use Status;
|
2022-01-13 19:35:27 +00:00
|
|
|
use TitleFormatter;
|
2022-02-15 20:56:22 +00:00
|
|
|
use TitleValue;
|
2020-02-27 22:00:28 +00:00
|
|
|
use Wikimedia\Message\MessageValue;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Rest\Handler\SearchHandler
|
|
|
|
|
*/
|
|
|
|
|
class SearchHandlerTest extends \MediaWikiUnitTestCase {
|
|
|
|
|
|
2022-01-13 19:35:27 +00:00
|
|
|
use DummyServicesTrait;
|
2022-08-31 14:51:57 +00:00
|
|
|
use MediaTestTrait;
|
2020-02-27 22:00:28 +00:00
|
|
|
|
2020-02-28 11:59:25 +00:00
|
|
|
/**
|
|
|
|
|
* @var SearchEngine|MockObject|null
|
|
|
|
|
*/
|
|
|
|
|
private $searchEngine = null;
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
/**
|
2021-01-14 08:20:36 +00:00
|
|
|
* @param string $query
|
2020-03-25 17:21:12 +00:00
|
|
|
* @param SearchResultSet|Status $titleResult
|
|
|
|
|
* @param SearchResultSet|Status $textResult
|
|
|
|
|
* @param SearchSuggestionSet|null $completionResult
|
2021-10-08 20:05:15 +00:00
|
|
|
* @param PermissionManager|null $permissionManager
|
2022-01-13 19:35:27 +00:00
|
|
|
* @param RedirectLookup|null $redirectLookup
|
|
|
|
|
* @param PageStore|null $pageStore
|
|
|
|
|
* @param TitleFormatter|null $mockTitleFormatter
|
2022-08-31 14:51:57 +00:00
|
|
|
* @param HookContainer|null $hookContainer
|
2020-03-25 17:21:12 +00:00
|
|
|
*
|
|
|
|
|
* @return SearchHandler
|
|
|
|
|
*/
|
2020-02-27 22:00:28 +00:00
|
|
|
private function newHandler(
|
|
|
|
|
$query,
|
|
|
|
|
$titleResult,
|
2020-03-25 17:21:12 +00:00
|
|
|
$textResult,
|
2021-10-08 20:05:15 +00:00
|
|
|
$completionResult = null,
|
2022-01-13 19:35:27 +00:00
|
|
|
$permissionManager = null,
|
|
|
|
|
$redirectLookup = null,
|
|
|
|
|
$pageStore = null,
|
2022-08-31 14:51:57 +00:00
|
|
|
$mockTitleFormatter = null,
|
|
|
|
|
HookContainer $hookContainer = null
|
2020-02-27 22:00:28 +00:00
|
|
|
) {
|
|
|
|
|
$config = new HashConfig( [
|
2022-08-17 20:33:06 +00:00
|
|
|
MainConfigNames::SearchType => 'test',
|
|
|
|
|
MainConfigNames::SearchTypeAlternatives => [],
|
|
|
|
|
MainConfigNames::NamespacesToBeSearchedDefault => [ NS_MAIN => true ],
|
|
|
|
|
MainConfigNames::SearchSuggestCacheExpiry => 1200,
|
2020-02-27 22:00:28 +00:00
|
|
|
] );
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
/** @var Language|MockObject $language */
|
2020-02-27 22:00:28 +00:00
|
|
|
$language = $this->createNoOpMock( Language::class );
|
2022-10-21 02:26:49 +00:00
|
|
|
$hookContainer ??= $this->createHookContainer();
|
2022-04-22 12:39:40 +00:00
|
|
|
/** @var UserOptionsLookup|MockObject $userOptionsLookup */
|
|
|
|
|
$userOptionsLookup = $this->createMock( UserOptionsLookup::class );
|
|
|
|
|
$searchEngineConfig = new \SearchEngineConfig(
|
|
|
|
|
$config,
|
|
|
|
|
$language,
|
|
|
|
|
$hookContainer,
|
|
|
|
|
[],
|
|
|
|
|
$userOptionsLookup
|
|
|
|
|
);
|
2020-02-27 22:00:28 +00:00
|
|
|
|
2021-10-08 20:05:15 +00:00
|
|
|
if ( !$permissionManager ) {
|
|
|
|
|
$permissionManager = $this->createMock( PermissionManager::class );
|
|
|
|
|
$permissionManager->method( 'isEveryoneAllowed' )
|
|
|
|
|
->with( 'read' )
|
|
|
|
|
->willReturn( true );
|
|
|
|
|
}
|
2022-01-13 19:35:27 +00:00
|
|
|
if ( !$pageStore ) {
|
|
|
|
|
$pageStore = $this->createMock( PageStore::class );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Our mock RedirectLookup defaults to not finding a redirect for our given page
|
|
|
|
|
if ( !$redirectLookup ) {
|
|
|
|
|
$redirectLookup = $this->createMock( RedirectLookup::class );
|
|
|
|
|
$redirectLookup->method( 'getRedirectTarget' )
|
|
|
|
|
->willReturn( null );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !$mockTitleFormatter ) {
|
|
|
|
|
$mockTitleFormatter = $this->getDummyTitleFormatter();
|
|
|
|
|
}
|
2021-10-08 20:05:15 +00:00
|
|
|
|
2020-02-27 22:00:28 +00:00
|
|
|
/** @var SearchEngine|MockObject $searchEngine */
|
2020-02-28 11:59:25 +00:00
|
|
|
$this->searchEngine = $this->createMock( SearchEngine::class );
|
|
|
|
|
$this->searchEngine->method( 'searchTitle' )
|
2020-02-27 22:00:28 +00:00
|
|
|
->with( $query )
|
|
|
|
|
->willReturn( $titleResult );
|
2020-02-28 11:59:25 +00:00
|
|
|
$this->searchEngine->method( 'searchText' )
|
2020-02-27 22:00:28 +00:00
|
|
|
->with( $query )
|
|
|
|
|
->willReturn( $textResult );
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
if ( $completionResult ) {
|
|
|
|
|
$this->searchEngine->method( 'completionSearchWithVariants' )
|
|
|
|
|
->with( $query )
|
|
|
|
|
->willReturn( $completionResult );
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 22:00:28 +00:00
|
|
|
/** @var SearchEngineFactory|MockObject $searchEngineFactory */
|
|
|
|
|
$searchEngineFactory = $this->createNoOpMock( SearchEngineFactory::class, [ 'create' ] );
|
|
|
|
|
$searchEngineFactory->method( 'create' )
|
2020-02-28 11:59:25 +00:00
|
|
|
->willReturn( $this->searchEngine );
|
2020-02-27 22:00:28 +00:00
|
|
|
|
2022-08-31 14:51:57 +00:00
|
|
|
$searchResultThumbnailProvider = new SearchResultThumbnailProvider(
|
|
|
|
|
$this->makeMockRepoGroup( [] ),
|
|
|
|
|
$hookContainer
|
|
|
|
|
);
|
|
|
|
|
|
2020-02-27 22:00:28 +00:00
|
|
|
return new SearchHandler(
|
2020-03-25 21:33:27 +00:00
|
|
|
$config,
|
2020-02-27 22:00:28 +00:00
|
|
|
$searchEngineFactory,
|
2021-10-08 20:05:15 +00:00
|
|
|
$searchEngineConfig,
|
2022-08-31 14:51:57 +00:00
|
|
|
$searchResultThumbnailProvider,
|
2022-01-13 19:35:27 +00:00
|
|
|
$permissionManager,
|
|
|
|
|
$redirectLookup,
|
|
|
|
|
$pageStore,
|
|
|
|
|
$mockTitleFormatter
|
2020-02-27 22:00:28 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-03-25 17:21:12 +00:00
|
|
|
* @param string $pageName
|
|
|
|
|
* @param string $textSnippet
|
|
|
|
|
* @param bool $broken
|
|
|
|
|
* @param bool $missing
|
|
|
|
|
*
|
2020-02-27 22:00:28 +00:00
|
|
|
* @return SearchResult
|
|
|
|
|
*/
|
|
|
|
|
private function makeMockSearchResult(
|
|
|
|
|
$pageName,
|
|
|
|
|
$textSnippet = 'Lorem Ipsum',
|
|
|
|
|
$broken = false,
|
|
|
|
|
$missing = false
|
|
|
|
|
) {
|
|
|
|
|
$title = $this->makeMockTitle( $pageName );
|
|
|
|
|
|
|
|
|
|
/** @var SearchResult|MockObject $result */
|
|
|
|
|
$result = $this->createNoOpMock( SearchResult::class, [
|
|
|
|
|
'getTitle', 'isBrokenTitle', 'isMissingRevision', 'getTextSnippet'
|
|
|
|
|
] );
|
|
|
|
|
$result->method( 'getTitle' )->willReturn( $title );
|
|
|
|
|
$result->method( 'getTextSnippet' )->willReturn( $textSnippet );
|
|
|
|
|
$result->method( 'isBrokenTitle' )->willReturn( $broken );
|
|
|
|
|
$result->method( 'isMissingRevision' )->willReturn( $missing );
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $pageName
|
2022-01-13 19:35:27 +00:00
|
|
|
* @param MockObject|null $title
|
2020-03-25 17:21:12 +00:00
|
|
|
*
|
|
|
|
|
* @return SearchSuggestion
|
|
|
|
|
*/
|
2022-01-13 19:35:27 +00:00
|
|
|
private function makeMockSearchSuggestion( $pageName, $title = null ) {
|
|
|
|
|
if ( !$title ) {
|
|
|
|
|
$title = $this->makeMockTitle( $pageName );
|
|
|
|
|
}
|
2020-03-25 17:21:12 +00:00
|
|
|
|
|
|
|
|
/** @var SearchSuggestion|MockObject $suggestion */
|
|
|
|
|
$suggestion = $this->createNoOpMock(
|
|
|
|
|
SearchSuggestion::class,
|
|
|
|
|
[ 'getSuggestedTitle', 'getSuggestedTitleID', 'getText' ]
|
|
|
|
|
);
|
|
|
|
|
$suggestion->method( 'getSuggestedTitle' )->willReturn( $title );
|
|
|
|
|
$suggestion->method( 'getSuggestedTitleID' )->willReturn( $title->getArticleID() );
|
|
|
|
|
$suggestion->method( 'getText' )->willReturn( $title->getPrefixedText() );
|
|
|
|
|
|
|
|
|
|
return $suggestion;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecuteFulltextSearch() {
|
2020-02-27 22:00:28 +00:00
|
|
|
$titleResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Forbidden Foo', 'two' ), // forbidden
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar', 'three' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Foo Moo', 'four', true, false ), // missing
|
|
|
|
|
] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Quux', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Forbidden Quux', 'two' ), // forbidden
|
|
|
|
|
$this->makeMockSearchResult( 'Xyzzy', 'three' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Yookoo', 'four', false, true ), // broken
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
2020-03-25 17:21:12 +00:00
|
|
|
$config = [ 'mode' => SearchHandler::FULLTEXT_MODE ];
|
2021-01-22 00:20:44 +00:00
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [], [], [],
|
2021-02-07 13:10:36 +00:00
|
|
|
$this->mockAnonAuthority( static function ( string $permission, ?PageIdentity $target ) {
|
2021-01-22 00:20:44 +00:00
|
|
|
return $target && !preg_match( '/Forbidden/', $target->getDBkey() );
|
|
|
|
|
} ) );
|
2020-02-27 22:00:28 +00:00
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 4, $data['pages'] );
|
|
|
|
|
$this->assertSame( 'Foo', $data['pages'][0]['title'] );
|
|
|
|
|
$this->assertSame( 'one', $data['pages'][0]['excerpt'] );
|
|
|
|
|
$this->assertSame( 'FooBar', $data['pages'][1]['title'] );
|
|
|
|
|
$this->assertSame( 'three', $data['pages'][1]['excerpt'] );
|
|
|
|
|
$this->assertSame( 'Quux', $data['pages'][2]['title'] );
|
|
|
|
|
$this->assertSame( 'one', $data['pages'][2]['excerpt'] );
|
|
|
|
|
$this->assertSame( 'Xyzzy', $data['pages'][3]['title'] );
|
|
|
|
|
$this->assertSame( 'three', $data['pages'][3]['excerpt'] );
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-25 17:21:12 +00:00
|
|
|
public function testExecuteCompletionSearch() {
|
|
|
|
|
$titleResults = new MockSearchResultSet( [] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Quux', 'one' ),
|
|
|
|
|
] );
|
|
|
|
|
$completionResults = new SearchSuggestionSet( [
|
|
|
|
|
$this->makeMockSearchSuggestion( 'Frob' ),
|
|
|
|
|
$this->makeMockSearchSuggestion( 'Frobnitz' ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
2022-01-13 19:35:27 +00:00
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query, $titleResults, $textResults, $completionResults );
|
2020-03-25 17:21:12 +00:00
|
|
|
$config = [ 'mode' => SearchHandler::COMPLETION_MODE ];
|
2020-03-25 21:33:27 +00:00
|
|
|
$response = $this->executeHandler( $handler, $request, $config );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
$this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) );
|
|
|
|
|
$this->assertSame( 'public, max-age=1200', $response->getHeaderLine( 'Cache-Control' ) );
|
|
|
|
|
|
|
|
|
|
$data = json_decode( $response->getBody(), true );
|
|
|
|
|
$this->assertIsArray( $data, 'Body must be a JSON array' );
|
2020-03-25 17:21:12 +00:00
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 2, $data['pages'] );
|
|
|
|
|
$this->assertSame( 'Frob', $data['pages'][0]['title'] );
|
|
|
|
|
$this->assertSame( 'Frob', $data['pages'][0]['excerpt'] );
|
|
|
|
|
$this->assertSame( 'Frobnitz', $data['pages'][1]['title'] );
|
|
|
|
|
$this->assertSame( 'Frobnitz', $data['pages'][1]['excerpt'] );
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 20:05:15 +00:00
|
|
|
public function testCompletionSearchNotCachedForPublicPages() {
|
|
|
|
|
$titleResults = new MockSearchResultSet( [] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
$completionResults = new SearchSuggestionSet( [] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$permissionManager = $this->createMock( PermissionManager::class );
|
|
|
|
|
$permissionManager->method( 'isEveryoneAllowed' )
|
|
|
|
|
->with( 'read' )
|
|
|
|
|
->willReturn( false );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults, $completionResults, $permissionManager );
|
|
|
|
|
$config = [ 'mode' => SearchHandler::COMPLETION_MODE ];
|
|
|
|
|
|
|
|
|
|
$response = $this->executeHandler( $handler, $request, $config );
|
|
|
|
|
$this->assertSame( 'no-store, max-age=0', $response->getHeaderLine( 'Cache-Control' ) );
|
|
|
|
|
$this->assertSame( 200, $response->getStatusCode() );
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-28 11:59:25 +00:00
|
|
|
public function testExecute_limit() {
|
|
|
|
|
$titleResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar' ),
|
|
|
|
|
] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query, 'limit' => 7 ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
|
|
|
|
|
// Limits are enforced by the SearchEngine, which we mock.
|
|
|
|
|
// So we have to do assertions on the mock, not on the result data.
|
|
|
|
|
$this->searchEngine
|
|
|
|
|
->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'setLimitOffset' )
|
|
|
|
|
->with( 7, 0 );
|
|
|
|
|
|
|
|
|
|
$this->executeHandler( $handler, $request );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecute_limit_default() {
|
|
|
|
|
$titleResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar' ),
|
|
|
|
|
] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
|
|
|
|
|
// Limits are enforced by the SearchEngine, which we mock.
|
|
|
|
|
// So we have to do assertions on the mock, not on the result data.
|
|
|
|
|
$this->searchEngine
|
|
|
|
|
->expects( $this->atLeastOnce() )
|
|
|
|
|
->method( 'setLimitOffset' )
|
|
|
|
|
->with( 50, 0 );
|
|
|
|
|
|
|
|
|
|
$this->executeHandler( $handler, $request );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideExecute_limit_error() {
|
|
|
|
|
yield [ 0, 'paramvalidator-outofrange-minmax' ];
|
|
|
|
|
yield [ 123, 'paramvalidator-outofrange-minmax' ];
|
|
|
|
|
yield [ 'xyz', 'paramvalidator-badinteger' ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideExecute_limit_error
|
|
|
|
|
* @param int $requestedLimit
|
|
|
|
|
* @param string $error
|
|
|
|
|
*/
|
|
|
|
|
public function testExecute_limit_error( $requestedLimit, $error ) {
|
|
|
|
|
$titleResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar' ),
|
|
|
|
|
] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request =
|
|
|
|
|
new RequestData( [ 'queryParams' => [ 'q' => $query, 'limit' => $requestedLimit ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
|
|
|
|
|
$this->expectExceptionObject(
|
|
|
|
|
new LocalizedHttpException( new MessageValue( $error ), 400 )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->executeHandler( $handler, $request );
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-27 22:00:28 +00:00
|
|
|
public function testExecute_status() {
|
|
|
|
|
$titleResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar', 'three' ),
|
|
|
|
|
] ) );
|
|
|
|
|
$textResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Quux', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Xyzzy', 'three' ),
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 4, $data['pages'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecute_missingparam() {
|
|
|
|
|
$titleResults = Status::newFatal( 'testing' );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
|
|
|
|
|
$query = '';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$this->expectExceptionObject(
|
|
|
|
|
new LocalizedHttpException(
|
|
|
|
|
new MessageValue( "paramvalidator-missingparam", [ 'q' ] ),
|
|
|
|
|
400
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
$this->executeHandler( $handler, $request );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecute_error() {
|
|
|
|
|
$titleResults = Status::newFatal( 'testing' );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$this->expectExceptionObject(
|
|
|
|
|
new LocalizedHttpException( new MessageValue( "rest-search-error", [ 'testing' ] ) )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
$this->executeHandler( $handler, $request );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testNeedsWriteWriteAccess() {
|
|
|
|
|
$titleResults = new MockSearchResultSet( [] );
|
|
|
|
|
$textResults = new MockSearchResultSet( [] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( '', $titleResults, $textResults );
|
|
|
|
|
$this->assertTrue( $handler->needsReadAccess() );
|
|
|
|
|
$this->assertFalse( $handler->needsWriteAccess() );
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-15 12:33:15 +00:00
|
|
|
public function testExecute_augmentedFields() {
|
|
|
|
|
$titleResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar', 'three' ),
|
|
|
|
|
] ) );
|
|
|
|
|
$textResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Quux', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Xyzzy', 'three' ),
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 4, $data['pages'] );
|
|
|
|
|
$this->assertArrayHasKey( 'thumbnail', $data['pages'][0] );
|
|
|
|
|
$this->assertNull( $data['pages'][0][ 'thumbnail' ] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'description', $data['pages'][0] );
|
|
|
|
|
$this->assertNull( $data['pages'][0][ 'description' ] );
|
2022-01-13 19:35:27 +00:00
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'matched_title', $data['pages'][0] );
|
|
|
|
|
$this->assertNull( $data['pages'][0][ 'matched_title' ] );
|
2020-04-15 12:33:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testExecute_augmentedFieldsDescriptionAndThumbnailProvided() {
|
|
|
|
|
$titleResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo', 'one' ),
|
|
|
|
|
] ) );
|
|
|
|
|
$textResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Quux', 'one' ),
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
2022-08-31 14:51:57 +00:00
|
|
|
$hookContainer = $this->createHookContainer( [
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
'SearchResultProvideDescription' =>
|
2021-02-07 13:10:36 +00:00
|
|
|
static function ( array $pageIdentities, array &$result ) {
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
foreach ( $pageIdentities as $pageId => $pageIdentity ) {
|
|
|
|
|
$result[ $pageId ] = 'Description_' . $pageIdentity->getId();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
'SearchResultProvideThumbnail' =>
|
2021-02-07 13:10:36 +00:00
|
|
|
static function ( array $pageIdentities, array &$result ) {
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
foreach ( $pageIdentities as $pageId => $pageIdentity ) {
|
|
|
|
|
$result[ $pageId ] = new SearchResultThumbnail(
|
|
|
|
|
'image/png',
|
|
|
|
|
2250,
|
|
|
|
|
100,
|
|
|
|
|
125,
|
|
|
|
|
500,
|
|
|
|
|
'http:/example.org/url_' . $pageIdentity->getId(),
|
|
|
|
|
null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
] );
|
2020-04-15 12:33:15 +00:00
|
|
|
|
2022-08-31 14:51:57 +00:00
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query, $titleResults, $textResults, null, null, null, null, null, $hookContainer
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, [], $hookContainer );
|
|
|
|
|
|
2020-04-15 12:33:15 +00:00
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 2, $data['pages'] );
|
|
|
|
|
$this->assertArrayHasKey( 'thumbnail', $data['pages'][0] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 'http:/example.org/url_1', $data['pages'][0][ 'thumbnail' ]['url'] );
|
|
|
|
|
$this->assertSame( 125, $data['pages'][0][ 'thumbnail' ]['height'] );
|
|
|
|
|
$this->assertSame( 100, $data['pages'][0][ 'thumbnail' ]['width'] );
|
|
|
|
|
$this->assertSame( 'image/png', $data['pages'][0][ 'thumbnail' ]['mimetype'] );
|
|
|
|
|
$this->assertSame( 2250, $data['pages'][0][ 'thumbnail' ]['size'] );
|
|
|
|
|
$this->assertSame( 500, $data['pages'][0][ 'thumbnail' ]['duration'] );
|
|
|
|
|
$this->assertArrayHasKey( 'description', $data['pages'][0] );
|
|
|
|
|
$this->assertSame( 'Description_1', $data['pages'][0][ 'description' ] );
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 19:35:27 +00:00
|
|
|
/**
|
|
|
|
|
* Tests the case where a search term matches a page with a redirect.
|
|
|
|
|
*/
|
|
|
|
|
public function testExecute_ResolvesRedirect() {
|
|
|
|
|
$textResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo Redirect Source' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBarBaz' ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$pageTarget = new PageIdentityValue( 1, NS_MAIN, 'Foo_Redirect_Target', PageIdentityValue::LOCAL );
|
|
|
|
|
|
|
|
|
|
$mockRedirectLinkTarget = $this->createMock( LinkTarget::class );
|
|
|
|
|
$mockPageStore = $this->createMock( PageStore::class );
|
|
|
|
|
$mockPageStore->method( 'getPageForLink' )->willReturn( $pageTarget );
|
|
|
|
|
$mockRedirectLookup = $this->createMock( RedirectLookup::class );
|
|
|
|
|
|
2022-10-06 20:25:11 +00:00
|
|
|
// first call has a redirect, second call does not
|
|
|
|
|
$mockRedirectLookup
|
|
|
|
|
->method( 'getRedirectTarget' )
|
|
|
|
|
->willReturnOnConsecutiveCalls( $mockRedirectLinkTarget, null );
|
2022-01-13 19:35:27 +00:00
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
$config = [];
|
|
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query, null, $textResults, null, null,
|
|
|
|
|
$mockRedirectLookup, $mockPageStore
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [] );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 2, $data['pages'] );
|
|
|
|
|
$this->assertArrayHasKey( 'matched_title', $data['pages'][0] );
|
|
|
|
|
$this->assertArrayHasKey( 'matched_title', $data['pages'][1] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 'Foo Redirect Source', $data['pages'][0]['matched_title'] );
|
|
|
|
|
$this->assertSame( 'Foo Redirect Target', $data['pages'][0]['title'] );
|
|
|
|
|
$this->assertSame( null, $data['pages'][1]['matched_title'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests the case where a search term matches both the redirect source and the redirect target page.
|
|
|
|
|
* We expect to remove the redirect source, and keep the redirect target.
|
|
|
|
|
*/
|
|
|
|
|
public function testExecute_RemovesDuplicateRedirectAndSource() {
|
|
|
|
|
$pageTarget = $this->createMock( ProperPageIdentity::class );
|
|
|
|
|
$pageTarget->method( 'getID' )->willReturn( 10 );
|
|
|
|
|
$pageTarget->method( 'getDBKey' )->willReturn( 'Foo_Redirect_Target' );
|
|
|
|
|
|
|
|
|
|
$mockRedirectLinkTarget = $this->createMock( LinkTarget::class );
|
|
|
|
|
$mockPageStore = $this->createMock( PageStore::class );
|
|
|
|
|
$mockPageStore->method( 'getPageForLink' )->willReturn( $pageTarget );
|
|
|
|
|
$mockRedirectLookup = $this->createMock( RedirectLookup::class );
|
|
|
|
|
|
2022-10-06 20:25:11 +00:00
|
|
|
// first call has a redirect, second call does not
|
|
|
|
|
$mockRedirectLookup
|
|
|
|
|
->method( 'getRedirectTarget' )
|
|
|
|
|
->willReturnOnConsecutiveCalls( $mockRedirectLinkTarget, null );
|
2022-01-13 19:35:27 +00:00
|
|
|
|
|
|
|
|
$mockTitle = $this->makeMockTitle( 'Foo Redirect Target', [ 'id' => 10 ] );
|
|
|
|
|
$completionConfig = [ 'mode' => SearchHandler::COMPLETION_MODE ];
|
|
|
|
|
$completionResults = new SearchSuggestionSet( [
|
|
|
|
|
$this->makeMockSearchSuggestion( 'Foo Redirect Source', $mockTitle ),
|
|
|
|
|
$this->makeMockSearchSuggestion( 'Foo Redirect Target', $mockTitle ),
|
|
|
|
|
] );
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query, null, null, $completionResults, null,
|
|
|
|
|
$mockRedirectLookup, $mockPageStore
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $completionConfig, [] );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $data['pages'] );
|
|
|
|
|
$this->assertArrayHasKey( 'matched_title', $data['pages'][0] );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( 'Foo Redirect Target', $data['pages'][0]['title'] );
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-09 14:29:34 +00:00
|
|
|
public function testExecute_NonProperPage() {
|
|
|
|
|
$page = $this->makeMockTitle( 'Blankpage', [ 'namespace' => NS_SPECIAL ] );
|
|
|
|
|
$result = $this->createNoOpMock(
|
|
|
|
|
SearchResult::class,
|
2022-02-15 20:56:22 +00:00
|
|
|
[ 'getTitle', 'isBrokenTitle', 'isMissingRevision', 'getTextSnippet' ]
|
2022-02-09 14:29:34 +00:00
|
|
|
);
|
|
|
|
|
$result->method( 'getTitle' )->willReturn( $page );
|
|
|
|
|
$textResults = new MockSearchResultSet( [ $result ] );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query,
|
|
|
|
|
null,
|
|
|
|
|
$textResults
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request );
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
2022-02-15 20:56:22 +00:00
|
|
|
$this->assertCount( 1, $data['pages'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When a page in a real namespace redirects to a Special Page, it should come back in results
|
|
|
|
|
*/
|
|
|
|
|
public function testExecute_NonProperPageAsRedirect() {
|
|
|
|
|
$textResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Special Page Redirect Source' ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$mockRedirectLinkTarget = new TitleValue( NS_SPECIAL, 'MyPage' );
|
|
|
|
|
$mockRedirectLookup = $this->createMock( RedirectLookup::class );
|
|
|
|
|
$mockRedirectLookup->method( 'getRedirectTarget' )
|
|
|
|
|
->willReturn( $mockRedirectLinkTarget );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query, null, $textResults, null, null,
|
|
|
|
|
$mockRedirectLookup
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [] );
|
|
|
|
|
$this->assertCount( 1, $data['pages'] );
|
|
|
|
|
$this->assertEquals( 'Special Page Redirect Source', $data['pages'][0]['title'] );
|
|
|
|
|
$this->assertNull( $data['pages'][0]['matched_title'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* When a page in a real namespace redirects to an interwiki, it should come back in results
|
|
|
|
|
*/
|
|
|
|
|
public function testExecute_InterwikiLinkAsRedirect() {
|
|
|
|
|
$textResults = new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Interwiki Redirect Source' ),
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$mockRedirectLinkTarget = $this->createMock( LinkTarget::class );
|
|
|
|
|
$mockRedirectLinkTarget->method( 'getNamespace' )->willReturn( 0 );
|
|
|
|
|
$mockRedirectLinkTarget->method( 'isExternal' )->willReturn( true );
|
|
|
|
|
$mockRedirectLinkTarget->method( 'getDBkey' )->willReturn( 'Interwiki_Redirect_Target' );
|
|
|
|
|
|
|
|
|
|
$mockRedirectLookup = $this->createMock( RedirectLookup::class );
|
|
|
|
|
$mockRedirectLookup->method( 'getRedirectTarget' )
|
|
|
|
|
->willReturn( $mockRedirectLinkTarget );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$config = [];
|
|
|
|
|
$handler = $this->newHandler(
|
|
|
|
|
$query, null, $textResults, null, null,
|
|
|
|
|
$mockRedirectLookup
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [] );
|
|
|
|
|
|
|
|
|
|
$this->assertCount( 1, $data['pages'] );
|
|
|
|
|
$this->assertNull( $data['pages'][0]['matched_title'] );
|
|
|
|
|
$this->assertEquals( 'Interwiki Redirect Source', $data['pages'][0]['title'] );
|
2022-02-09 14:29:34 +00:00
|
|
|
}
|
|
|
|
|
|
2020-04-15 12:33:15 +00:00
|
|
|
public function testExecute_NullResults() {
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, null, null );
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData( $handler, $request );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 0, $data['pages'] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testInitWrongConfig() {
|
|
|
|
|
$titleResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Foo', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'FooBar', 'three' ),
|
|
|
|
|
] ) );
|
|
|
|
|
$textResults = Status::newGood( new MockSearchResultSet( [
|
|
|
|
|
$this->makeMockSearchResult( 'Quux', 'one' ),
|
|
|
|
|
$this->makeMockSearchResult( 'Xyzzy', 'three' ),
|
|
|
|
|
] ) );
|
|
|
|
|
|
|
|
|
|
$query = 'foo';
|
|
|
|
|
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] );
|
|
|
|
|
|
|
|
|
|
$this->expectException( InvalidArgumentException::class );
|
|
|
|
|
|
|
|
|
|
$handler = $this->newHandler( $query, $titleResults, $textResults );
|
|
|
|
|
$data = $this->executeHandlerAndGetBodyData(
|
|
|
|
|
$handler,
|
|
|
|
|
$request,
|
|
|
|
|
[
|
|
|
|
|
'mode' => 'SomethingWrong'
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$this->assertArrayHasKey( 'pages', $data );
|
|
|
|
|
$this->assertCount( 0, $data['pages'] );
|
|
|
|
|
}
|
2020-02-27 22:00:28 +00:00
|
|
|
}
|