search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Backend/ReservedIndexFieldsTest.php
tests/src/Kernel/Backend/ReservedIndexFieldsTest.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Backend;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\search_api\Item\Field;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests when using reserved names as index fields.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class ReservedIndexFieldsTest extends BackendKernelTestBase {
protected const TEST_INDEX = 'kernel_index_fields_test_index';
/**
* Tests setting reserved field in index fields.
*/
public function testReservedFieldInIndexThrowsException(): void {
$this->prepareTestIndex(self::TEST_INDEX);
$fields = $this->index->getFields();
$fields['id'] = new Field($this->index, 'id');
$fields['id']->setPropertyPath('id')
->setDatasourceId('entity:entity_test')
->setType('integer');
$this->index->setFields($fields);
$this->expectException(\Exception::class);
$this->index->save();
}
/**
* Tests searching with reserved "search_api_id" field.
*/
public function testSearchWithItemId(): void {
$this->prepareTestIndex(self::TEST_INDEX);
$this->prepareItems();
$results = $this->index
->query()
->range()
->addCondition('search_api_id', 'entity:entity_test/1:en')
->execute();
$this->assertEquals(1, $results->getResultCount());
}
/**
* Prepares and indexes test entities.
*/
protected function prepareItems(): void {
foreach (range(1, 5) as $weight) {
EntityTest::create([
'name' => 'Test entity ' . $weight,
'body' => 'This is the test body',
'weight' => $weight,
])->save();
}
$this->index->indexItems();
}
/**
* {@inheritdoc}
*/
protected function getTestIndexes(): array {
return [self::TEST_INDEX];
}
}
