public function SearchApiSolrTest::testAutocomplete in Search API Solr 8
Same name and namespace in other branches
- 8.2 tests/src/Kernel/SearchApiSolrTest.php \Drupal\Tests\search_api_solr\Kernel\SearchApiSolrTest::testAutocomplete()
Tests the autocomplete support.
File
- tests/
src/ Kernel/ SearchApiSolrTest.php, line 841
Class
- SearchApiSolrTest
- Tests index and search capabilities using the Solr search backend.
Namespace
Drupal\Tests\search_api_solr\KernelCode
public function testAutocomplete() {
// Only run the tests if we have a Solr core available.
if ($this->solrAvailable) {
$this
->addTestEntity(1, [
'name' => 'Test Article 1',
'body' => 'The test article number 1 about cats, dogs and trees.',
'type' => 'article',
]);
// Add another node with body length equal to the limit.
$this
->addTestEntity(2, [
'name' => 'Test Article 1',
'body' => 'The test article number 2 about a tree.',
'type' => 'article',
]);
$this
->indexItems($this->indexId);
/** @var \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend $backend */
$backend = Server::load($this->serverId)
->getBackend();
$autocompleteSearch = new Search([], 'search_api_autocomplete_search');
$query = $this
->buildSearch([
'artic',
], [], [
'body',
], FALSE);
$suggestions = $backend
->getAutocompleteSuggestions($query, $autocompleteSearch, 'artic', 'artic');
$this
->assertEquals(1, count($suggestions));
$this
->assertEquals('le', $suggestions[0]
->getSuggestionSuffix());
$this
->assertEquals(2, $suggestions[0]
->getResultsCount());
$query = $this
->buildSearch([
'articel',
], [], [
'body',
], FALSE);
$suggestions = $backend
->getAutocompleteSuggestions($query, $autocompleteSearch, 'articel', 'articel');
$this
->assertEquals(1, count($suggestions));
$this
->assertEquals('article', $suggestions[0]
->getSuggestedKeys());
$this
->assertEquals(0, $suggestions[0]
->getResultsCount());
$query = $this
->buildSearch([
'articel doks',
], [], [
'body',
], FALSE);
$suggestions = $backend
->getAutocompleteSuggestions($query, $autocompleteSearch, 'doks', 'articel doks');
$this
->assertEquals(1, count($suggestions));
$this
->assertEquals('article dogs', $suggestions[0]
->getSuggestedKeys());
/* Spell check plus suffixes don't quite work in Solarium 6 yet
$query = $this->buildSearch(['articel tre'], [], ['body'], FALSE);
$suggestions = $backend->getAutocompleteSuggestions($query, $autocompleteSearch, 'tre', 'articel tre');
$this->assertEquals(5, count($suggestions));
$this->assertEquals('e', $suggestions[0]->getSuggestionSuffix());
$this->assertEquals(1, $suggestions[0]->getResultsCount());
$this->assertEquals('es', $suggestions[1]->getSuggestionSuffix());
$this->assertEquals(1, $suggestions[1]->getResultsCount());
$this->assertEquals('article tre', $suggestions[2]->getSuggestedKeys());
$this->assertEquals(0, $suggestions[2]->getResultsCount());
$this->assertEquals('article tree', $suggestions[3]->getSuggestedKeys());
$this->assertEquals(0, $suggestions[3]->getResultsCount());
$this->assertEquals('article trees', $suggestions[4]->getSuggestedKeys());
$this->assertEquals(0, $suggestions[4]->getResultsCount());
*/
}
else {
$this
->assertTrue(TRUE, 'Error: The Solr instance could not be found. Please enable a multi-core one on http://localhost:8983/solr/drupal');
}
}