search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Backend/RankingScoreTest.php
tests/src/Kernel/Backend/RankingScoreTest.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Backend;
use Drupal\entity_test\Entity\EntityTest;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests for ranking score.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class RankingScoreTest extends BackendKernelTestBase {
/**
* The test index.
*/
protected const TEST_INDEX = 'kernel_ranking_score_test_index';
/**
* Tests ranking score is getting set.
*/
public function testRankingScore(): void {
$this->prepareTestIndex(self::TEST_INDEX);
$this->populateIndex();
// Query with no search key means we get all results and
// default score of 1.0 is set.
$queryOne = $this->index->query(['showRankingScore' => TRUE]);
$rankingScoreNotSet = $queryOne->range()->keys('')->execute();
$this->assertCount(3, $rankingScoreNotSet->getResultItems());
$resultsNotSet = $rankingScoreNotSet->getResultItems();
$rankingScoresDefaultQuery = [];
foreach ($resultsNotSet as $resultNotSet) {
$this->assertEquals(1.0, $resultNotSet->getScore());
$rankingScoresDefaultQuery[] = $resultNotSet->getScore();
}
// Query with search key set means we get relevant results
// and score set based on that (not default).
$queryTwo = $this->index->query();
$rankingScoreSet = $queryTwo->range()->keys('his')->execute();
$this->assertCount(2, $rankingScoreSet->getResultItems());
$resultsSet = $rankingScoreSet->getResultItems();
$rankingScoresQuery = [];
foreach ($resultsSet as $resultSet) {
$this->assertNotEquals(1.0, $resultSet->getScore());
$rankingScoresQuery[] = $resultSet->getScore();
}
// Check that scores do not match.
$this->assertNotEquals($rankingScoresDefaultQuery, $rankingScoresQuery);
// Do a meili search with api, to get the _rankingScore field
// for comparison with query field.
$meiliSearch = $this->apiService->search(self::TEST_INDEX, 'his', ['showRankingScore' => TRUE]);
$hits = $meiliSearch->getHits();
$rankingScores = [];
foreach ($hits as $hit) {
$rankingScores[] = $hit['_rankingScore'];
}
$this->assertEquals($rankingScoresQuery, $rankingScores);
}
/**
* {@inheritdoc}
*/
protected function getTestIndexes(): array {
return [self::TEST_INDEX];
}
/**
* Prepares search api index with items for tests.
*/
protected function populateIndex(): void {
EntityTest::create([
'name' => 'Die Hard',
'body' => 'Die Hard tells the story of a New York City detective named John McClane who is separated from his wife. She invites him to a Christmas party at her office.',
'weight' => 111,
])->save();
EntityTest::create([
'name' => 'The Matrix',
'body' => 'Thomas Anderson, a computer programmer, is led to fight an underground war against powerful computers who have constructed his entire reality with a system called the Matrix.',
'weight' => 222,
])->save();
EntityTest::create([
'name' => 'Clerks',
'body' => 'Two store clerks, Dante and Randal, trouble customers, discuss movies, laze around and play hockey on the store roof all the time.',
'weight' => 333,
])->save();
$this->index->indexItems();
}
/**
* {@inheritdoc}
*/
protected function prepareTestIndex(string $indexName, bool $readOnly = FALSE): void {
parent::prepareTestIndex($indexName);
$this->index->getField('name')->setType('text');
$this->index->getField('body')->setType('text');
$this->index->save();
}
}
