View source
<?php
namespace Drupal\Tests\search_api\Kernel\ConfigEntity;
use Drupal\KernelTests\KernelTestBase;
use Drupal\search_api\Backend\BackendInterface;
use Drupal\search_api\Entity\Index;
use Drupal\search_api\Entity\Server;
use Drupal\search_api\Processor\ProcessorInterface;
use Drupal\search_api\Utility\Utility;
use Drupal\search_api_test\PluginTestTrait;
class ConfigOverrideKernelTest extends KernelTestBase {
use PluginTestTrait;
public static $modules = [
'search_api',
'search_api_test',
'system',
'user',
];
protected $server;
protected $index;
public function setUp() {
parent::setUp();
$this
->installSchema('search_api', [
'search_api_item',
]);
$this
->installSchema('user', [
'users_data',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('search_api_task');
$this
->installConfig('search_api');
if (!Utility::isRunningInCli()) {
\Drupal::state()
->set('search_api_use_tracking_batch', FALSE);
}
$GLOBALS['config']['search_api.server.test_server'] = [
'name' => 'Overridden server',
'backend' => 'search_api_test',
'backend_config' => [
'test' => 'foobar',
],
];
$GLOBALS['config']['search_api.index.test_index'] = [
'name' => 'Overridden index',
'server' => 'test_server',
'processor_settings' => [
'search_api_test' => [],
],
];
$this->server = Server::create([
'id' => 'test_server',
'name' => 'Test server',
'backend' => 'does not exist',
]);
$this->index = Index::create([
'id' => 'test_index',
'name' => 'Test index',
'server' => 'unknown_server',
'datasource_settings' => [
'entity:user' => [],
],
'tracker_settings' => [
'default' => [],
],
]);
}
protected function tearDown() {
parent::tearDown();
unset($GLOBALS['config']['search_api.server.test_server']);
unset($GLOBALS['config']['search_api.index.test_index']);
}
public function testIndexSave() {
$this->server
->save();
$this
->assertEmpty($this->index
->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE));
$this->index
->save();
$this
->assertEquals([
'preIndexSave',
], $this
->getCalledMethods('processor'));
$this
->assertEmpty($this->index
->getProcessorsByStage(ProcessorInterface::STAGE_PRE_INDEX_SAVE));
$index = Index::load($this->index
->id());
$this
->assertEquals('Overridden index', $index
->label());
$server = $index
->getServerInstance();
$this
->assertNotEmpty($server);
$this
->assertEquals('Overridden server', $server
->label());
$this
->assertTrue($index
->status());
$index_storage = \Drupal::entityTypeManager()
->getStorage('search_api_index');
$index = $index_storage
->loadOverrideFree($index
->id());
$this
->assertEquals('Test index', $index
->label());
$args = [];
$this
->setMethodOverride('backend', 'updateIndex', function () use (&$args) {
$args = func_get_args();
});
$index
->set('name', 'New index name')
->save();
$this
->assertCount(2, $args);
$index = $args[1];
$this
->assertEquals('Overridden index', $index
->label());
$index = Index::load($this->index
->id());
$this
->assertEquals('Overridden index', $index
->label());
$this
->assertEquals('Overridden server', $index
->getServerInstance()
->label());
$index = $index_storage
->loadOverrideFree($index
->id());
$this
->assertEquals('New index name', $index
->label());
}
public function testServerSave() {
$passed_config = [];
$passed_name = NULL;
$override = function (BackendInterface $backend) use (&$passed_config, &$passed_name) {
$passed_config = $backend
->getConfiguration();
$passed_name = $backend
->getServer()
->label();
};
$this
->setMethodOverride('backend', 'postInsert', $override);
$this->server
->save();
$this
->assertEquals([
'test' => 'foobar',
], $passed_config);
$this
->assertEquals('Overridden server', $passed_name);
$this
->assertEquals('Test server', $this->server
->label());
$this
->assertTrue($this->server
->status());
$this->index
->save();
$server = Server::load($this->server
->id());
$this
->assertEquals('Overridden server', $server
->label());
$this
->assertTrue($server
->status());
$this
->assertEquals('does not exist', $this->server
->getBackendId());
$this
->setMethodOverride('backend', 'preUpdate', $override);
$this->server
->save();
$this
->assertEquals([
'test' => 'foobar',
], $passed_config);
$GLOBALS['config']['search_api.server.test_server']['status'] = TRUE;
\Drupal::configFactory()
->clearStaticCache();
$server_storage = \Drupal::entityTypeManager()
->getStorage('search_api_server');
$server = $server_storage
->loadOverrideFree($server
->id());
$server
->disable()
->save();
\Drupal::configFactory()
->clearStaticCache();
$index = Index::load($this->index
->id());
$this
->assertTrue($index
->status());
$server = Server::load($this->server
->id());
$this
->assertTrue($server
->status());
$server = $server_storage
->loadOverrideFree($server
->id());
$this
->assertEquals('Test server', $server
->label());
}
}