search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Backend/BooleanIndexFieldsTest.php
tests/src/Kernel/Backend/BooleanIndexFieldsTest.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Backend;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\search_api\Item\Field;
use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests for boolean type index fields.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class BooleanIndexFieldsTest extends BackendKernelTestBase {
use EntityDefinitionTestTrait;
protected const TEST_INDEX = 'kernel_boolean_fields_test_index';
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$state = $this->container->get('state');
$definitions = $state->get('entity_test.additional_base_field_definitions');
$definitions['test_boolean'] = BaseFieldDefinition::create('boolean')
->setName('test_boolean')
->setLabel(new TranslatableMarkup('Test boolean'));
$state->set('entity_test.additional_base_field_definitions', $definitions);
$this->applyEntityUpdates('entity_test');
$this->prepareTestIndex(self::TEST_INDEX);
$this->prepareItems();
}
/**
* Tests boolean fields filter queries.
*/
public function testBooleanFieldsFilterReturnsCorrectResults(): void {
$query = $this->index->query()->addCondition('test_boolean', TRUE);
$result = $query->execute();
$this->assertEquals(1, $result->getResultCount());
$query = $this->index->query()->addCondition('test_boolean', 1);
$result = $query->execute();
$this->assertEquals(1, $result->getResultCount());
$query = $this->index->query()->addCondition('test_boolean', '1');
$result = $query->execute();
$this->assertEquals(1, $result->getResultCount());
$query = $this->index->query()->addCondition('test_boolean', FALSE);
$result = $query->execute();
$this->assertEquals(0, $result->getResultCount());
$query = $this->index->query()->addCondition('test_boolean', 0);
$result = $query->execute();
$this->assertEquals(0, $result->getResultCount());
$query = $this->index->query()->addCondition('test_boolean', '0');
$result = $query->execute();
$this->assertEquals(0, $result->getResultCount());
}
/**
* {@inheritdoc}
*/
protected function prepareTestIndex(string $indexName, bool $readOnly = FALSE): void {
parent::prepareTestIndex($indexName, $readOnly);
$field = new Field($this->index, 'test_boolean');
$field->setType('boolean')
->setDatasourceId('entity:entity_test')
->setPropertyPath('test_boolean')
->setLabel('Test boolean');
$this->index->addField($field)->save();
}
/**
* Populates the index with entities.
*/
protected function prepareItems(): void {
EntityTest::create([
'name' => 'Test entity TRUE',
'body' => 'This is the test body',
'weight' => 1,
'test_boolean' => TRUE,
])->save();
$this->index->indexItems();
}
/**
* {@inheritdoc}
*/
protected function getTestIndexes(): array {
return [self::TEST_INDEX];
}
}
