search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Processor/ProcessorKernelTestBase.php
tests/src/Kernel/Processor/ProcessorKernelTestBase.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Processor;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\Processor\ProcessorInterface;
use Drupal\Tests\search_api_meilisearch\Kernel\MeilisearchKernelTestBase;
/**
* Provides a base testing class for processor plugin.
*/
abstract class ProcessorKernelTestBase extends MeilisearchKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'user',
'system',
'entity_test',
'search_api',
'search_api_meilisearch',
];
/**
* The search api test index.
*
* @var \Drupal\search_api\Entity\Index|null
*/
protected ?Index $index = NULL;
/**
* The search api test processor.
*
* @var \Drupal\search_api\Processor\ProcessorInterface|null
*/
protected ?ProcessorInterface $processor = NULL;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$definitions['weight'] = BaseFieldDefinition::create('integer')->setLabel('Weight');
$definitions['body'] = BaseFieldDefinition::create('string')->setLabel('Optional body');
$this->container->get('state')->set(
'entity_test.additional_base_field_definitions',
$definitions
);
$this->installConfig('search_api');
$this->installSchema('search_api', ['search_api_item']);
$this->installEntitySchema('search_api_task');
$this->installEntitySchema('entity_test');
}
/**
* Prepares search api index with items for tests.
*/
protected function prepareTestIndex(string $indexName, ?string $processor = NULL): void {
$server = Server::create([
'id' => 'test_server',
'name' => 'Test server',
'backend' => 'search_api_meilisearch',
'backend_config' => [
'meilisearch_host_address' => getenv('MEILI_TEST_HOST') ?: 'http://meilisearch',
'meilisearch_host_port' => getenv('MEILI_TEST_PORT') ?: '7700',
'meilisearch_master_key' => getenv('MEILI_TEST_MASTER_KEY') ?: 'MASTER_KEY',
],
]);
$server->save();
$this->index = Index::create([
'id' => $indexName,
'name' => 'Test index',
'datasource_settings' => [
'entity:entity_test' => [],
],
'field_settings' => [
'name' => [
'type' => 'text',
'datasource_id' => 'entity:entity_test',
'property_path' => 'name',
],
'weight' => [
'type' => 'integer',
'datasource_id' => 'entity:entity_test',
'property_path' => 'weight',
],
'body' => [
'type' => 'string',
'datasource_id' => 'entity:entity_test',
'property_path' => 'body',
],
],
'tracker_settings' => [
'default' => [],
],
'server' => $server->id(),
]);
if ($processor) {
$this->processor = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createProcessorPlugin($this->index, $processor);
$this->index->addProcessor($this->processor);
}
$this->index->save();
}
}
