View source
<?php
namespace Drupal\Tests\search_api\Kernel\Index;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Utility\Utility;
class IndexImportTest extends KernelTestBase {
protected $storage;
public static $modules = [
'field',
'search_api',
'search_api_db',
'search_api_test_db',
'search_api_test',
'user',
'system',
'entity_test',
'text',
];
public function setUp() {
parent::setUp();
$this
->installSchema('search_api', [
'search_api_item',
]);
$this
->installSchema('user', [
'users_data',
]);
$this
->installEntitySchema('entity_test_mulrev_changed');
$this
->installEntitySchema('search_api_task');
$this
->installConfig('search_api');
$this
->installConfig('system');
$this
->config('system.site')
->set('UUID', $this->container
->get('uuid')
->generate());
if (!Utility::isRunningInCli()) {
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
}
$this
->installConfig([
'search_api_test_db',
]);
$this->storage = $this->container
->get('entity_type.manager')
->getStorage('search_api_index');
}
public function testIndexImport() {
$index = $this->storage
->load('database_search_index');
$this
->assertFalse($index
->isValidProcessor('stopwords'), 'Processor is not in index');
$sync = $this->container
->get('config.storage.sync');
$active = $this->container
->get('config.storage');
$this
->copyConfig($active, $sync);
$expected_stopwords = [
'a',
'an',
'and',
'are',
'as',
];
$import_config = $sync
->read('search_api.index.database_search_index');
$import_config['processor_settings']['stopwords'] = [
'weights' => [
'preprocess_query' => -10,
'postprocess_query' => -10,
],
'fields' => [
'name',
],
'stopwords' => $expected_stopwords,
];
$sync
->write('search_api.index.database_search_index', $import_config);
$config_importer = $this
->configImporter();
$this
->assertTrue($config_importer
->hasUnprocessedConfigurationChanges(), 'Import prepared');
$config_importer
->import();
$this->storage
->resetCache([
'database_search_index',
]);
$imported_index = $this->storage
->load('database_search_index');
$this
->assertTrue($imported_index
->isValidProcessor('stopwords'), 'Processor is in index after import');
$processor_config = $imported_index
->getProcessor('stopwords')
->getConfiguration();
$this
->assertArrayHasKey('stopwords', $processor_config, 'Stopwords are configured');
$actual_stopwords = $processor_config['stopwords'];
$this
->assertEquals($expected_stopwords, $actual_stopwords, 'Processor config was correctly set during import');
}
}