search_api_meilisearch-1.0.x-dev/tests/src/Kernel/Update/UpdateHook9001Test.php
tests/src/Kernel/Update/UpdateHook9001Test.php
<?php
namespace Drupal\Tests\search_api_meilisearch\Kernel\Update;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\Tests\search_api_meilisearch\Kernel\MeilisearchKernelTestBase;
use Drupal\Tests\search_api_meilisearch\Traits\UpdateHookTrait;
use PHPUnit\Framework\Attributes\Group;
/**
* Provides tests for hook_update_9001.
*
* @group search_api_meilisearch
*/
#[Group('search_api_meilisearch')]
class UpdateHook9001Test extends MeilisearchKernelTestBase {
use UpdateHookTrait;
protected const PROCESSOR_ID = 'search_api_meilisearch_synonyms';
/**
* {@inheritdoc}
*/
protected static $modules = [
'search_api',
'search_api_meilisearch',
];
/**
* {@inheritdoc}
*/
protected static $configSchemaCheckerExclusions = [
'search_api_meilisearch.settings',
];
/**
* The list of various indexes.
*
* @var \Drupal\search_api\IndexInterface[]
*/
protected array $indexes = [];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installConfig('search_api');
$this->installSchema('search_api', ['search_api_item']);
$this->installEntitySchema('search_api_task');
$this->prepareTestIndexes();
$this->prepareOldConfiguration();
}
/**
* Tests the update hook.
*/
public function testUpdateHook(): void {
$this->runUpdateHook(9001);
// Reload indexes to get new configuration.
$storage = $this->container
->get('entity_type.manager')
->getStorage('search_api_index');
foreach ($this->indexes as &$index) {
$index = $storage->load($index->id());
}
// Test that configuration was removed from module configuration.
$this->assertArrayNotHasKey(
'meilisearch_synonyms',
$this->config('search_api_meilisearch.settings')->getRawData()
);
// Test that configuration was moved to processor configuration.
$expected = [
'testing' => ['test', 'the test', 'le test'],
'chad' => ['giga chad'],
'saving' => ['save', 'store'],
];
$synonyms = $this->indexes['index_with_synonyms']
->getProcessor(self::PROCESSOR_ID)
->getConfiguration()['synonyms'];
$this->assertEquals($expected, $synonyms);
// Test that default configuration is on the processor without previous
// configuration.
$synonyms = $this->indexes['index_without_synonyms']
->getProcessor(self::PROCESSOR_ID)
->getConfiguration()['synonyms'];
$this->assertEquals([], $synonyms);
}
/**
* Sets up a test index.
*/
protected function prepareTestIndexes(): void {
$server = Server::create([
'id' => 'test_server_1',
'name' => 'Test server',
'backend' => 'search_api_meilisearch',
'backend_config' => [],
]);
$server->save();
$index = Index::create([
'id' => 'test_index',
'name' => 'Test index with synonyms',
'server' => $server->id(),
]);
$processor = $this->container
->get('search_api.plugin_helper')
->createProcessorPlugin($index, self::PROCESSOR_ID);
$index->addProcessor($processor);
$index->save();
$this->indexes['index_with_synonyms'] = $index;
$server = Server::create([
'id' => 'test_server_2',
'name' => 'Test server',
'backend' => 'search_api_meilisearch',
'backend_config' => [],
]);
$server->save();
$index = Index::create([
'id' => 'test_index_no_synonyms',
'name' => 'Test index without synonyms',
'server' => $server->id(),
]);
$processor = $this->container
->get('search_api.plugin_helper')
->createProcessorPlugin($index, self::PROCESSOR_ID);
$index->addProcessor($processor);
$index->save();
$this->indexes['index_without_synonyms'] = $index;
}
/**
* Sets the old configuration before updates are run.
*/
protected function prepareOldConfiguration(): void {
$this->config('search_api_meilisearch.settings')
->set('meilisearch_synonyms', [
'test_index' => "testing: test, the test, le test\nchad: giga chad\r\nsaving: save, store",
'non_existing_index' => "testing: test, the test, le test",
])->save(TRUE);
}
/**
* {@inheritdoc}
*/
protected function getTestIndexes(): array {
// Indexes do not have proper backend_config set and are not created in the
// Meilisearch test instance.
return [];
}
}
