| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Rest\Handler; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use MediaWiki\Config\HashConfig; |
| 7 |
use MediaWiki\Config\ServiceOptions; |
| 8 |
use MediaWiki\HookContainer\HookContainer; |
| 9 |
use MediaWiki\Language\FormatterFactory; |
| 10 |
use MediaWiki\Language\Language; |
| 11 |
use MediaWiki\Language\RawMessage; |
| 12 |
use MediaWiki\Linker\LinkTarget; |
| 13 |
use MediaWiki\MainConfigNames; |
| 14 |
use MediaWiki\Page\PageIdentity; |
| 15 |
use MediaWiki\Page\PageIdentityValue; |
| 16 |
use MediaWiki\Page\PageStore; |
| 17 |
use MediaWiki\Page\ProperPageIdentity; |
| 18 |
use MediaWiki\Page\RedirectLookup; |
| 19 |
use MediaWiki\Permissions\PermissionManager; |
| 20 |
use MediaWiki\Rest\Handler\SearchHandler; |
| 21 |
use MediaWiki\Rest\LocalizedHttpException; |
| 22 |
use MediaWiki\Rest\RequestData; |
| 23 |
use MediaWiki\Search\Entity\SearchResultThumbnail; |
| 24 |
use MediaWiki\Search\SearchResultThumbnailProvider; |
| 25 |
use MediaWiki\Status\Status; |
| 26 |
use MediaWiki\Status\StatusFormatter; |
| 27 |
use MediaWiki\Tests\Unit\DummyServicesTrait; |
| 28 |
use MediaWiki\Title\TitleFormatter; |
| 29 |
use MediaWiki\Title\TitleValue; |
| 30 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 31 |
use MediaWikiUnitTestCase; |
| 32 |
use MockSearchResultSet; |
| 33 |
use PHPUnit\Framework\MockObject\MockObject; |
| 34 |
use SearchEngine; |
| 35 |
use SearchEngineConfig; |
| 36 |
use SearchEngineFactory; |
| 37 |
use SearchResult; |
| 38 |
use SearchResultSet; |
| 39 |
use SearchSuggestion; |
| 40 |
use SearchSuggestionSet; |
| 41 |
use Wikimedia\Message\MessageValue; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class SearchHandlerTest extends MediaWikiUnitTestCase { |
| 47 |
|
| 48 |
use DummyServicesTrait; |
| 49 |
use MediaTestTrait; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
private $searchEngine = null; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
private function newHandler( |
| 70 |
$query, |
| 71 |
$titleResult, |
| 72 |
$textResult, |
| 73 |
$completionResult = null, |
| 74 |
$permissionManager = null, |
| 75 |
$redirectLookup = null, |
| 76 |
$pageStore = null, |
| 77 |
$mockTitleFormatter = null, |
| 78 |
?HookContainer $hookContainer = null |
| 79 |
) { |
| 80 |
$sources = [ |
| 81 |
MainConfigNames::SearchType => 'test', |
| 82 |
MainConfigNames::SearchTypeAlternatives => [], |
| 83 |
MainConfigNames::NamespacesToBeSearchedDefault => [ NS_MAIN => true ], |
| 84 |
MainConfigNames::SearchSuggestCacheExpiry => 1200, |
| 85 |
]; |
| 86 |
$config = new HashConfig( $sources ); |
| 87 |
|
| 88 |
|
| 89 |
$language = $this->createNoOpMock( Language::class ); |
| 90 |
$hookContainer ??= $this->createHookContainer(); |
| 91 |
|
| 92 |
$userOptionsLookup = $this->createMock( UserOptionsLookup::class ); |
| 93 |
$searchEngineConfig = new SearchEngineConfig( |
| 94 |
new ServiceOptions( |
| 95 |
SearchEngineConfig::CONSTRUCTOR_OPTIONS, |
| 96 |
$sources |
| 97 |
), |
| 98 |
$language, |
| 99 |
$hookContainer, |
| 100 |
[], |
| 101 |
$userOptionsLookup |
| 102 |
); |
| 103 |
|
| 104 |
if ( !$permissionManager ) { |
| 105 |
$permissionManager = $this->createMock( PermissionManager::class ); |
| 106 |
$permissionManager->method( 'isEveryoneAllowed' ) |
| 107 |
->with( 'read' ) |
| 108 |
->willReturn( true ); |
| 109 |
} |
| 110 |
if ( !$pageStore ) { |
| 111 |
$pageStore = $this->createMock( PageStore::class ); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if ( !$redirectLookup ) { |
| 116 |
$redirectLookup = $this->createMock( RedirectLookup::class ); |
| 117 |
$redirectLookup->method( 'getRedirectTarget' ) |
| 118 |
->willReturn( null ); |
| 119 |
} |
| 120 |
|
| 121 |
if ( !$mockTitleFormatter ) { |
| 122 |
$mockTitleFormatter = $this->getDummyTitleFormatter(); |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
$this->searchEngine = $this->createMock( SearchEngine::class ); |
| 127 |
$this->searchEngine->method( 'searchTitle' ) |
| 128 |
->with( $query ) |
| 129 |
->willReturn( $titleResult ); |
| 130 |
$this->searchEngine->method( 'searchText' ) |
| 131 |
->with( $query ) |
| 132 |
->willReturn( $textResult ); |
| 133 |
|
| 134 |
if ( $completionResult ) { |
| 135 |
$this->searchEngine->method( 'completionSearchWithVariants' ) |
| 136 |
->with( $query ) |
| 137 |
->willReturn( $completionResult ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
$searchEngineFactory = $this->createNoOpMock( SearchEngineFactory::class, [ 'create' ] ); |
| 142 |
$searchEngineFactory->method( 'create' ) |
| 143 |
->willReturn( $this->searchEngine ); |
| 144 |
|
| 145 |
$searchResultThumbnailProvider = new SearchResultThumbnailProvider( |
| 146 |
$this->makeMockRepoGroup( [] ), |
| 147 |
$hookContainer |
| 148 |
); |
| 149 |
|
| 150 |
$mockStatusFormatter = $this->createNoOpMock( StatusFormatter::class, [ 'getMessage' ] ); |
| 151 |
$mockStatusFormatter->method( 'getMessage' )->willReturn( |
| 152 |
new RawMessage( 'testing' ) |
| 153 |
); |
| 154 |
|
| 155 |
$mockFormatterFactory = $this->createNoOpMock( FormatterFactory::class, [ 'getStatusFormatter' ] ); |
| 156 |
$mockFormatterFactory->method( 'getStatusFormatter' )->willReturn( $mockStatusFormatter ); |
| 157 |
|
| 158 |
return new SearchHandler( |
| 159 |
$config, |
| 160 |
$searchEngineFactory, |
| 161 |
$searchEngineConfig, |
| 162 |
$searchResultThumbnailProvider, |
| 163 |
$permissionManager, |
| 164 |
$redirectLookup, |
| 165 |
$pageStore, |
| 166 |
$mockTitleFormatter, |
| 167 |
$mockFormatterFactory, |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
private function makeMockSearchResult( |
| 180 |
$pageName, |
| 181 |
$textSnippet = 'Lorem Ipsum', |
| 182 |
$broken = false, |
| 183 |
$missing = false |
| 184 |
) { |
| 185 |
$title = $this->makeMockTitle( $pageName ); |
| 186 |
|
| 187 |
|
| 188 |
$result = $this->createNoOpMock( SearchResult::class, [ |
| 189 |
'getTitle', 'isBrokenTitle', 'isMissingRevision', 'getTextSnippet' |
| 190 |
] ); |
| 191 |
$result->method( 'getTitle' )->willReturn( $title ); |
| 192 |
$result->method( 'getTextSnippet' )->willReturn( $textSnippet ); |
| 193 |
$result->method( 'isBrokenTitle' )->willReturn( $broken ); |
| 194 |
$result->method( 'isMissingRevision' )->willReturn( $missing ); |
| 195 |
|
| 196 |
return $result; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private function makeMockSearchSuggestion( $pageName, $title = null ) { |
| 206 |
if ( !$title ) { |
| 207 |
$title = $this->makeMockTitle( $pageName ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
$suggestion = $this->createNoOpMock( |
| 212 |
SearchSuggestion::class, |
| 213 |
[ 'getSuggestedTitle', 'getSuggestedTitleID', 'getText' ] |
| 214 |
); |
| 215 |
$suggestion->method( 'getSuggestedTitle' )->willReturn( $title ); |
| 216 |
$suggestion->method( 'getSuggestedTitleID' )->willReturn( $title->getArticleID() ); |
| 217 |
$suggestion->method( 'getText' )->willReturn( $title->getPrefixedText() ); |
| 218 |
|
| 219 |
return $suggestion; |
| 220 |
} |
| 221 |
|
| 222 |
public function testExecuteFulltextSearch() { |
| 223 |
$titleResults = new MockSearchResultSet( [ |
| 224 |
$this->makeMockSearchResult( 'Foo', 'one' ), |
| 225 |
$this->makeMockSearchResult( 'Forbidden Foo', 'two' ), |
| 226 |
$this->makeMockSearchResult( 'FooBar', 'three' ), |
| 227 |
$this->makeMockSearchResult( 'Foo Moo', 'four', true, false ), |
| 228 |
] ); |
| 229 |
$textResults = new MockSearchResultSet( [ |
| 230 |
$this->makeMockSearchResult( 'Quux', 'one' ), |
| 231 |
$this->makeMockSearchResult( 'Forbidden Quux', 'two' ), |
| 232 |
$this->makeMockSearchResult( 'Xyzzy', 'three' ), |
| 233 |
$this->makeMockSearchResult( 'Yookoo', 'four', false, true ), |
| 234 |
] ); |
| 235 |
|
| 236 |
$query = 'foo'; |
| 237 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 238 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 239 |
$config = [ 'mode' => SearchHandler::FULLTEXT_MODE ]; |
| 240 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [], [], [], |
| 241 |
$this->mockAnonAuthority( static function ( string $permission, ?PageIdentity $target ) { |
| 242 |
return $target && !preg_match( '/Forbidden/', $target->getDBkey() ); |
| 243 |
} ) ); |
| 244 |
|
| 245 |
$this->assertArrayHasKey( 'pages', $data ); |
| 246 |
$this->assertCount( 4, $data['pages'] ); |
| 247 |
$this->assertSame( 'Foo', $data['pages'][0]['title'] ); |
| 248 |
$this->assertSame( 'one', $data['pages'][0]['excerpt'] ); |
| 249 |
$this->assertSame( 'FooBar', $data['pages'][1]['title'] ); |
| 250 |
$this->assertSame( 'three', $data['pages'][1]['excerpt'] ); |
| 251 |
$this->assertSame( 'Quux', $data['pages'][2]['title'] ); |
| 252 |
$this->assertSame( 'one', $data['pages'][2]['excerpt'] ); |
| 253 |
$this->assertSame( 'Xyzzy', $data['pages'][3]['title'] ); |
| 254 |
$this->assertSame( 'three', $data['pages'][3]['excerpt'] ); |
| 255 |
} |
| 256 |
|
| 257 |
public function testExecuteCompletionSearch() { |
| 258 |
$titleResults = new MockSearchResultSet( [] ); |
| 259 |
$textResults = new MockSearchResultSet( [ |
| 260 |
$this->makeMockSearchResult( 'Quux', 'one' ), |
| 261 |
] ); |
| 262 |
$completionResults = new SearchSuggestionSet( [ |
| 263 |
$this->makeMockSearchSuggestion( 'Frob' ), |
| 264 |
$this->makeMockSearchSuggestion( 'Frobnitz' ), |
| 265 |
] ); |
| 266 |
|
| 267 |
$query = 'foo'; |
| 268 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 269 |
|
| 270 |
$handler = $this->newHandler( |
| 271 |
$query, $titleResults, $textResults, $completionResults ); |
| 272 |
$config = [ 'mode' => SearchHandler::COMPLETION_MODE ]; |
| 273 |
$response = $this->executeHandler( $handler, $request, $config ); |
| 274 |
|
| 275 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 276 |
$this->assertSame( 'application/json', $response->getHeaderLine( 'Content-Type' ) ); |
| 277 |
$this->assertSame( 'public, max-age=1200', $response->getHeaderLine( 'Cache-Control' ) ); |
| 278 |
|
| 279 |
$data = json_decode( $response->getBody(), true ); |
| 280 |
$this->assertIsArray( $data, 'Body must be a JSON array' ); |
| 281 |
|
| 282 |
$this->assertArrayHasKey( 'pages', $data ); |
| 283 |
$this->assertCount( 2, $data['pages'] ); |
| 284 |
$this->assertSame( 'Frob', $data['pages'][0]['title'] ); |
| 285 |
$this->assertSame( 'Frob', $data['pages'][0]['excerpt'] ); |
| 286 |
$this->assertSame( 'Frobnitz', $data['pages'][1]['title'] ); |
| 287 |
$this->assertSame( 'Frobnitz', $data['pages'][1]['excerpt'] ); |
| 288 |
} |
| 289 |
|
| 290 |
public function testCompletionSearchNotCachedForPublicPages() { |
| 291 |
$titleResults = new MockSearchResultSet( [] ); |
| 292 |
$textResults = new MockSearchResultSet( [] ); |
| 293 |
$completionResults = new SearchSuggestionSet( [] ); |
| 294 |
|
| 295 |
$query = 'foo'; |
| 296 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 297 |
|
| 298 |
$permissionManager = $this->createMock( PermissionManager::class ); |
| 299 |
$permissionManager->method( 'isEveryoneAllowed' ) |
| 300 |
->with( 'read' ) |
| 301 |
->willReturn( false ); |
| 302 |
|
| 303 |
$handler = $this->newHandler( $query, $titleResults, $textResults, $completionResults, $permissionManager ); |
| 304 |
$config = [ 'mode' => SearchHandler::COMPLETION_MODE ]; |
| 305 |
|
| 306 |
$response = $this->executeHandler( $handler, $request, $config ); |
| 307 |
$this->assertSame( 'no-store, max-age=0', $response->getHeaderLine( 'Cache-Control' ) ); |
| 308 |
$this->assertSame( 200, $response->getStatusCode() ); |
| 309 |
} |
| 310 |
|
| 311 |
public function testExecute_limit() { |
| 312 |
$titleResults = new MockSearchResultSet( [ |
| 313 |
$this->makeMockSearchResult( 'Foo' ), |
| 314 |
$this->makeMockSearchResult( 'FooBar' ), |
| 315 |
] ); |
| 316 |
$textResults = new MockSearchResultSet( [] ); |
| 317 |
|
| 318 |
$query = 'foo'; |
| 319 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query, 'limit' => 7 ] ] ); |
| 320 |
|
| 321 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
$this->searchEngine |
| 326 |
->expects( $this->atLeastOnce() ) |
| 327 |
->method( 'setLimitOffset' ) |
| 328 |
->with( 7, 0 ); |
| 329 |
|
| 330 |
$this->executeHandler( $handler, $request ); |
| 331 |
} |
| 332 |
|
| 333 |
public function testExecute_limit_default() { |
| 334 |
$titleResults = new MockSearchResultSet( [ |
| 335 |
$this->makeMockSearchResult( 'Foo' ), |
| 336 |
$this->makeMockSearchResult( 'FooBar' ), |
| 337 |
] ); |
| 338 |
$textResults = new MockSearchResultSet( [] ); |
| 339 |
|
| 340 |
$query = 'foo'; |
| 341 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 342 |
|
| 343 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
$this->searchEngine |
| 348 |
->expects( $this->atLeastOnce() ) |
| 349 |
->method( 'setLimitOffset' ) |
| 350 |
->with( 50, 0 ); |
| 351 |
|
| 352 |
$this->executeHandler( $handler, $request ); |
| 353 |
} |
| 354 |
|
| 355 |
public static function provideExecute_limit_error() { |
| 356 |
yield [ 0, 'paramvalidator-outofrange-minmax' ]; |
| 357 |
yield [ 123, 'paramvalidator-outofrange-minmax' ]; |
| 358 |
yield [ 'xyz', 'paramvalidator-badinteger' ]; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
public function testExecute_limit_error( $requestedLimit, $error ) { |
| 367 |
$titleResults = new MockSearchResultSet( [ |
| 368 |
$this->makeMockSearchResult( 'Foo' ), |
| 369 |
$this->makeMockSearchResult( 'FooBar' ), |
| 370 |
] ); |
| 371 |
$textResults = new MockSearchResultSet( [] ); |
| 372 |
|
| 373 |
$query = 'foo'; |
| 374 |
$request = |
| 375 |
new RequestData( [ 'queryParams' => [ 'q' => $query, 'limit' => $requestedLimit ] ] ); |
| 376 |
|
| 377 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 378 |
|
| 379 |
$this->expectExceptionObject( |
| 380 |
new LocalizedHttpException( new MessageValue( $error ), 400 ) |
| 381 |
); |
| 382 |
|
| 383 |
$this->executeHandler( $handler, $request ); |
| 384 |
} |
| 385 |
|
| 386 |
public function testExecute_status() { |
| 387 |
$titleResults = Status::newGood( new MockSearchResultSet( [ |
| 388 |
$this->makeMockSearchResult( 'Foo', 'one' ), |
| 389 |
$this->makeMockSearchResult( 'FooBar', 'three' ), |
| 390 |
] ) ); |
| 391 |
$textResults = Status::newGood( new MockSearchResultSet( [ |
| 392 |
$this->makeMockSearchResult( 'Quux', 'one' ), |
| 393 |
$this->makeMockSearchResult( 'Xyzzy', 'three' ), |
| 394 |
] ) ); |
| 395 |
|
| 396 |
$query = 'foo'; |
| 397 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 398 |
|
| 399 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 400 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request ); |
| 401 |
|
| 402 |
$this->assertArrayHasKey( 'pages', $data ); |
| 403 |
$this->assertCount( 4, $data['pages'] ); |
| 404 |
} |
| 405 |
|
| 406 |
public function testExecute_missingparam() { |
| 407 |
$titleResults = Status::newFatal( 'testing' ); |
| 408 |
$textResults = new MockSearchResultSet( [] ); |
| 409 |
|
| 410 |
$query = ''; |
| 411 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 412 |
|
| 413 |
$this->expectExceptionObject( |
| 414 |
new LocalizedHttpException( |
| 415 |
new MessageValue( "paramvalidator-missingparam", [ 'q' ] ), |
| 416 |
400 |
| 417 |
) |
| 418 |
); |
| 419 |
|
| 420 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 421 |
$this->executeHandler( $handler, $request ); |
| 422 |
} |
| 423 |
|
| 424 |
public function testExecute_error() { |
| 425 |
$titleResults = Status::newFatal( 'testing' ); |
| 426 |
$textResults = new MockSearchResultSet( [] ); |
| 427 |
|
| 428 |
$query = 'foo'; |
| 429 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 430 |
|
| 431 |
$this->expectExceptionObject( |
| 432 |
new LocalizedHttpException( new MessageValue( "rest-search-error", [ 'testing' ] ) ) |
| 433 |
); |
| 434 |
|
| 435 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 436 |
$this->executeHandler( $handler, $request ); |
| 437 |
} |
| 438 |
|
| 439 |
public function testNeedsWriteWriteAccess() { |
| 440 |
$titleResults = new MockSearchResultSet( [] ); |
| 441 |
$textResults = new MockSearchResultSet( [] ); |
| 442 |
|
| 443 |
$handler = $this->newHandler( '', $titleResults, $textResults ); |
| 444 |
$this->assertTrue( $handler->needsReadAccess() ); |
| 445 |
$this->assertFalse( $handler->needsWriteAccess() ); |
| 446 |
} |
| 447 |
|
| 448 |
public function testExecute_augmentedFields() { |
| 449 |
$titleResults = Status::newGood( new MockSearchResultSet( [ |
| 450 |
$this->makeMockSearchResult( 'Foo', 'one' ), |
| 451 |
$this->makeMockSearchResult( 'FooBar', 'three' ), |
| 452 |
] ) ); |
| 453 |
$textResults = Status::newGood( new MockSearchResultSet( [ |
| 454 |
$this->makeMockSearchResult( 'Quux', 'one' ), |
| 455 |
$this->makeMockSearchResult( 'Xyzzy', 'three' ), |
| 456 |
] ) ); |
| 457 |
|
| 458 |
$query = 'foo'; |
| 459 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 460 |
|
| 461 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 462 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request ); |
| 463 |
|
| 464 |
$this->assertArrayHasKey( 'pages', $data ); |
| 465 |
$this->assertCount( 4, $data['pages'] ); |
| 466 |
$this->assertArrayHasKey( 'thumbnail', $data['pages'][0] ); |
| 467 |
$this->assertNull( $data['pages'][0][ 'thumbnail' ] ); |
| 468 |
|
| 469 |
$this->assertArrayHasKey( 'description', $data['pages'][0] ); |
| 470 |
$this->assertNull( $data['pages'][0][ 'description' ] ); |
| 471 |
|
| 472 |
$this->assertArrayHasKey( 'matched_title', $data['pages'][0] ); |
| 473 |
$this->assertNull( $data['pages'][0][ 'matched_title' ] ); |
| 474 |
} |
| 475 |
|
| 476 |
public function testExecute_augmentedFieldsDescriptionAndThumbnailProvided() { |
| 477 |
$titleResults = Status::newGood( new MockSearchResultSet( [ |
| 478 |
$this->makeMockSearchResult( 'Foo', 'one' ), |
| 479 |
] ) ); |
| 480 |
$textResults = Status::newGood( new MockSearchResultSet( [ |
| 481 |
$this->makeMockSearchResult( 'Quux', 'one' ), |
| 482 |
] ) ); |
| 483 |
|
| 484 |
$query = 'foo'; |
| 485 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 486 |
|
| 487 |
$hookContainer = $this->createHookContainer( [ |
| 488 |
'SearchResultProvideDescription' => |
| 489 |
static function ( array $pageIdentities, array &$result ) { |
| 490 |
foreach ( $pageIdentities as $pageId => $pageIdentity ) { |
| 491 |
$result[ $pageId ] = 'Description_' . $pageIdentity->getId(); |
| 492 |
} |
| 493 |
}, |
| 494 |
'SearchResultProvideThumbnail' => |
| 495 |
static function ( array $pageIdentities, array &$result ) { |
| 496 |
foreach ( $pageIdentities as $pageId => $pageIdentity ) { |
| 497 |
$result[ $pageId ] = new SearchResultThumbnail( |
| 498 |
'image/png', |
| 499 |
null, |
| 500 |
100, |
| 501 |
125, |
| 502 |
500, |
| 503 |
'http:/example.org/url_' . $pageIdentity->getId(), |
| 504 |
null |
| 505 |
); |
| 506 |
} |
| 507 |
} |
| 508 |
] ); |
| 509 |
|
| 510 |
$handler = $this->newHandler( |
| 511 |
$query, $titleResults, $textResults, null, null, null, null, null, $hookContainer |
| 512 |
); |
| 513 |
|
| 514 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, [], $hookContainer ); |
| 515 |
|
| 516 |
$this->assertArrayHasKey( 'pages', $data ); |
| 517 |
$this->assertCount( 2, $data['pages'] ); |
| 518 |
$this->assertArrayHasKey( 'thumbnail', $data['pages'][0] ); |
| 519 |
|
| 520 |
$this->assertSame( 'http:/example.org/url_1', $data['pages'][0][ 'thumbnail' ]['url'] ); |
| 521 |
$this->assertSame( 125, $data['pages'][0][ 'thumbnail' ]['height'] ); |
| 522 |
$this->assertSame( 100, $data['pages'][0][ 'thumbnail' ]['width'] ); |
| 523 |
$this->assertSame( 'image/png', $data['pages'][0][ 'thumbnail' ]['mimetype'] ); |
| 524 |
$this->assertSame( 500, $data['pages'][0][ 'thumbnail' ]['duration'] ); |
| 525 |
$this->assertArrayHasKey( 'description', $data['pages'][0] ); |
| 526 |
$this->assertSame( 'Description_1', $data['pages'][0][ 'description' ] ); |
| 527 |
} |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
public function testExecute_ResolvesRedirect() { |
| 533 |
$textResults = new MockSearchResultSet( [ |
| 534 |
$this->makeMockSearchResult( 'Foo Redirect Source' ), |
| 535 |
$this->makeMockSearchResult( 'FooBarBaz' ), |
| 536 |
] ); |
| 537 |
|
| 538 |
$pageTarget = new PageIdentityValue( 1, NS_MAIN, 'Foo_Redirect_Target', PageIdentityValue::LOCAL ); |
| 539 |
|
| 540 |
$mockRedirectLinkTarget = $this->createMock( LinkTarget::class ); |
| 541 |
$mockPageStore = $this->createMock( PageStore::class ); |
| 542 |
$mockPageStore->method( 'getPageForLink' )->willReturn( $pageTarget ); |
| 543 |
$mockRedirectLookup = $this->createMock( RedirectLookup::class ); |
| 544 |
|
| 545 |
|
| 546 |
$mockRedirectLookup |
| 547 |
->method( 'getRedirectTarget' ) |
| 548 |
->willReturnOnConsecutiveCalls( $mockRedirectLinkTarget, null ); |
| 549 |
|
| 550 |
$query = 'foo'; |
| 551 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 552 |
$config = []; |
| 553 |
$handler = $this->newHandler( |
| 554 |
$query, null, $textResults, null, null, |
| 555 |
$mockRedirectLookup, $mockPageStore |
| 556 |
); |
| 557 |
|
| 558 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [] ); |
| 559 |
|
| 560 |
$this->assertCount( 2, $data['pages'] ); |
| 561 |
$this->assertArrayHasKey( 'matched_title', $data['pages'][0] ); |
| 562 |
$this->assertArrayHasKey( 'matched_title', $data['pages'][1] ); |
| 563 |
|
| 564 |
$this->assertSame( 'Foo Redirect Source', $data['pages'][0]['matched_title'] ); |
| 565 |
$this->assertSame( 'Foo Redirect Target', $data['pages'][0]['title'] ); |
| 566 |
$this->assertSame( null, $data['pages'][1]['matched_title'] ); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
public function testExecute_RemovesDuplicateRedirectAndSource() { |
| 574 |
$pageTarget = $this->createMock( ProperPageIdentity::class ); |
| 575 |
$pageTarget->method( 'getID' )->willReturn( 10 ); |
| 576 |
$pageTarget->method( 'getDBKey' )->willReturn( 'Foo_Redirect_Target' ); |
| 577 |
|
| 578 |
$mockRedirectLinkTarget = $this->createMock( LinkTarget::class ); |
| 579 |
$mockPageStore = $this->createMock( PageStore::class ); |
| 580 |
$mockPageStore->method( 'getPageForLink' )->willReturn( $pageTarget ); |
| 581 |
$mockRedirectLookup = $this->createMock( RedirectLookup::class ); |
| 582 |
|
| 583 |
|
| 584 |
$mockRedirectLookup |
| 585 |
->method( 'getRedirectTarget' ) |
| 586 |
->willReturnOnConsecutiveCalls( $mockRedirectLinkTarget, null ); |
| 587 |
|
| 588 |
$mockTitle = $this->makeMockTitle( 'Foo Redirect Target', [ 'id' => 10 ] ); |
| 589 |
$completionConfig = [ 'mode' => SearchHandler::COMPLETION_MODE ]; |
| 590 |
$completionResults = new SearchSuggestionSet( [ |
| 591 |
$this->makeMockSearchSuggestion( 'Foo Redirect Source', $mockTitle ), |
| 592 |
$this->makeMockSearchSuggestion( 'Foo Redirect Target', $mockTitle ), |
| 593 |
] ); |
| 594 |
$query = 'foo'; |
| 595 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 596 |
|
| 597 |
$handler = $this->newHandler( |
| 598 |
$query, null, null, $completionResults, null, |
| 599 |
$mockRedirectLookup, $mockPageStore |
| 600 |
); |
| 601 |
|
| 602 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $completionConfig, [] ); |
| 603 |
|
| 604 |
$this->assertCount( 1, $data['pages'] ); |
| 605 |
$this->assertArrayHasKey( 'matched_title', $data['pages'][0] ); |
| 606 |
|
| 607 |
$this->assertSame( 'Foo Redirect Target', $data['pages'][0]['title'] ); |
| 608 |
} |
| 609 |
|
| 610 |
public function testExecute_NonProperPage() { |
| 611 |
$page = $this->makeMockTitle( 'Blankpage', [ 'namespace' => NS_SPECIAL ] ); |
| 612 |
$result = $this->createNoOpMock( |
| 613 |
SearchResult::class, |
| 614 |
[ 'getTitle', 'isBrokenTitle', 'isMissingRevision', 'getTextSnippet' ] |
| 615 |
); |
| 616 |
$result->method( 'getTitle' )->willReturn( $page ); |
| 617 |
$textResults = new MockSearchResultSet( [ $result ] ); |
| 618 |
|
| 619 |
$query = 'foo'; |
| 620 |
|
| 621 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 622 |
$handler = $this->newHandler( |
| 623 |
$query, |
| 624 |
null, |
| 625 |
$textResults |
| 626 |
); |
| 627 |
|
| 628 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request ); |
| 629 |
$this->assertArrayHasKey( 'pages', $data ); |
| 630 |
$this->assertCount( 1, $data['pages'] ); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public function testExecute_NonProperPageAsRedirect() { |
| 637 |
$textResults = new MockSearchResultSet( [ |
| 638 |
$this->makeMockSearchResult( 'Special Page Redirect Source' ), |
| 639 |
] ); |
| 640 |
|
| 641 |
$mockRedirectLinkTarget = new TitleValue( NS_SPECIAL, 'MyPage' ); |
| 642 |
$mockRedirectLookup = $this->createMock( RedirectLookup::class ); |
| 643 |
$mockRedirectLookup->method( 'getRedirectTarget' ) |
| 644 |
->willReturn( $mockRedirectLinkTarget ); |
| 645 |
|
| 646 |
$query = 'foo'; |
| 647 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 648 |
|
| 649 |
$config = []; |
| 650 |
$handler = $this->newHandler( |
| 651 |
$query, null, $textResults, null, null, |
| 652 |
$mockRedirectLookup |
| 653 |
); |
| 654 |
|
| 655 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [] ); |
| 656 |
$this->assertCount( 1, $data['pages'] ); |
| 657 |
$this->assertEquals( 'Special Page Redirect Source', $data['pages'][0]['title'] ); |
| 658 |
$this->assertNull( $data['pages'][0]['matched_title'] ); |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
public function testExecute_InterwikiLinkAsRedirect() { |
| 665 |
$textResults = new MockSearchResultSet( [ |
| 666 |
$this->makeMockSearchResult( 'Interwiki Redirect Source' ), |
| 667 |
] ); |
| 668 |
|
| 669 |
$mockRedirectLinkTarget = $this->createMock( LinkTarget::class ); |
| 670 |
$mockRedirectLinkTarget->method( 'getNamespace' )->willReturn( 0 ); |
| 671 |
$mockRedirectLinkTarget->method( 'isExternal' )->willReturn( true ); |
| 672 |
$mockRedirectLinkTarget->method( 'getDBkey' )->willReturn( 'Interwiki_Redirect_Target' ); |
| 673 |
|
| 674 |
$mockRedirectLookup = $this->createMock( RedirectLookup::class ); |
| 675 |
$mockRedirectLookup->method( 'getRedirectTarget' ) |
| 676 |
->willReturn( $mockRedirectLinkTarget ); |
| 677 |
|
| 678 |
$query = 'foo'; |
| 679 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 680 |
|
| 681 |
$config = []; |
| 682 |
$handler = $this->newHandler( |
| 683 |
$query, null, $textResults, null, null, |
| 684 |
$mockRedirectLookup |
| 685 |
); |
| 686 |
|
| 687 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request, $config, [] ); |
| 688 |
|
| 689 |
$this->assertCount( 1, $data['pages'] ); |
| 690 |
$this->assertNull( $data['pages'][0]['matched_title'] ); |
| 691 |
$this->assertEquals( 'Interwiki Redirect Source', $data['pages'][0]['title'] ); |
| 692 |
} |
| 693 |
|
| 694 |
public function testExecute_NullResults() { |
| 695 |
$query = 'foo'; |
| 696 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 697 |
|
| 698 |
$handler = $this->newHandler( $query, null, null ); |
| 699 |
$data = $this->executeHandlerAndGetBodyData( $handler, $request ); |
| 700 |
|
| 701 |
$this->assertArrayHasKey( 'pages', $data ); |
| 702 |
$this->assertCount( 0, $data['pages'] ); |
| 703 |
} |
| 704 |
|
| 705 |
public function testInitWrongConfig() { |
| 706 |
$titleResults = Status::newGood( new MockSearchResultSet( [ |
| 707 |
$this->makeMockSearchResult( 'Foo', 'one' ), |
| 708 |
$this->makeMockSearchResult( 'FooBar', 'three' ), |
| 709 |
] ) ); |
| 710 |
$textResults = Status::newGood( new MockSearchResultSet( [ |
| 711 |
$this->makeMockSearchResult( 'Quux', 'one' ), |
| 712 |
$this->makeMockSearchResult( 'Xyzzy', 'three' ), |
| 713 |
] ) ); |
| 714 |
|
| 715 |
$query = 'foo'; |
| 716 |
$request = new RequestData( [ 'queryParams' => [ 'q' => $query ] ] ); |
| 717 |
|
| 718 |
$this->expectException( InvalidArgumentException::class ); |
| 719 |
|
| 720 |
$handler = $this->newHandler( $query, $titleResults, $textResults ); |
| 721 |
$data = $this->executeHandlerAndGetBodyData( |
| 722 |
$handler, |
| 723 |
$request, |
| 724 |
[ |
| 725 |
'mode' => 'SomethingWrong' |
| 726 |
] ); |
| 727 |
|
| 728 |
$this->assertArrayHasKey( 'pages', $data ); |
| 729 |
$this->assertCount( 0, $data['pages'] ); |
| 730 |
} |
| 731 |
} |