public function SearchApiSolrTest::testHighlight in Search API Solr 8.2
Tests highlight options.
File
- tests/
src/ Kernel/ SearchApiSolrTest.php, line 579
Class
- SearchApiSolrTest
- Tests index and search capabilities using the Solr search backend.
Namespace
Drupal\Tests\search_api_solr\KernelCode
public function testHighlight() {
$server = $this
->getIndex()
->getServerInstance();
$config = $server
->getBackendConfig();
$this
->insertExampleContent();
$this
->indexItems($this->indexId);
$query = $this
->buildSearch('foobar');
$results = $query
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for »foobar« returned correct number of results.');
/** @var \Drupal\search_api\Item\ItemInterface $result */
foreach ($results as $result) {
$this
->assertEmpty($result
->getExtraData('highlighted_fields', []));
$this
->assertEmpty($result
->getExtraData('highlighted_keys', []));
}
$config['highlight_data'] = TRUE;
$server
->setBackendConfig($config);
$server
->save();
$query = $this
->buildSearch('foobar');
$results = $query
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for »foobar« returned correct number of results.');
/** @var \Drupal\search_api\Item\ItemInterface $result */
foreach ($results as $result) {
$this
->assertContains('<strong>foobar</strong>', (string) $result
->getExtraData('highlighted_fields', [
'body' => [
'',
],
])['body'][0]);
$this
->assertEquals([
'foobar',
], $result
->getExtraData('highlighted_keys', []));
$this
->assertEquals('… bar … test <strong>foobar</strong> Case …', $result
->getExcerpt());
}
// Test highlghting with stemming.
$query = $this
->buildSearch('foobars');
$results = $query
->execute();
$this
->assertEquals(1, $results
->getResultCount(), 'Search for »foobar« returned correct number of results.');
/** @var \Drupal\search_api\Item\ItemInterface $result */
foreach ($results as $result) {
$this
->assertContains('<strong>foobar</strong>', (string) $result
->getExtraData('highlighted_fields', [
'body' => [
'',
],
])['body'][0]);
$this
->assertEquals([
'foobar',
], $result
->getExtraData('highlighted_keys', []));
$this
->assertEquals('… bar … test <strong>foobar</strong> Case …', $result
->getExcerpt());
}
}