search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Backend/FieldBoostTest.php
tests/src/Kernel/Backend/FieldBoostTest.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Backend;
use Drupal\entity_test\Entity\EntityTest;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests for field boost settings.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class FieldBoostTest extends BackendKernelTestBase {
protected const TEST_INDEX = 'kernel_field_boost_test_index';
/**
* Tests that field boost affects order of the results.
*/
public function testFieldBoostRelevancy(): void {
$this->prepareTestIndex(self::TEST_INDEX);
$this->populateIndex();
// Test results order is default without search query.
$query = $this->index->query();
$results = $query->range()->execute();
$expected = [
'entity:entity_test/1:en',
'entity:entity_test/2:en',
];
$this->assertEquals($expected, array_keys($results->getResultItems()));
// Test results order is affected by field boost with a search word.
$query = $this->index->query();
$query->range()->keys('keyword');
$results = $query->execute();
$expected = [
'entity:entity_test/2:en',
'entity:entity_test/1:en',
];
$this->assertEquals($expected, array_keys($results->getResultItems()));
}
/**
* Populates the index with entities.
*/
protected function populateIndex(): void {
EntityTest::create([
'name' => 'Test keyword entity',
'body' => 'This is the body of entity',
'weight' => 10,
])->save();
EntityTest::create([
'name' => 'Test entity',
'body' => 'This is the body keyword of entity',
'weight' => 10,
])->save();
$this->index->indexItems();
}
/**
* {@inheritdoc}
*/
protected function prepareTestIndex(string $indexName, bool $readOnly = FALSE): void {
parent::prepareTestIndex($indexName);
$this->index->getField('body')->setType('text')->setBoost(10.0);
$this->index->getField('name')->setType('text');
$this->index->save();
}
/**
* {@inheritdoc}
*/
protected function getTestIndexes(): array {
return [self::TEST_INDEX];
}
}
