public function DependencyRemovalTest::testBackendDependency in Search API 8
Tests a backend with a dependency that gets removed.
If the dependency does not get removed, proper cascading to the index is also verified.
@dataProvider dependencyTestDataProvider
Parameters
bool $remove_dependency: Whether to remove the dependency from the backend when the object depended on is deleted.
File
- tests/
src/ Kernel/ ConfigEntity/ DependencyRemovalTest.php, line 157
Class
- DependencyRemovalTest
- Tests what happens when an index's or a server's dependencies are removed.
Namespace
Drupal\Tests\search_api\Kernel\ConfigEntityCode
public function testBackendDependency($remove_dependency) {
$dependency_key = $this->dependency
->getConfigDependencyKey();
$dependency_name = $this->dependency
->getConfigDependencyName();
// Create a server using the test backend, and set the dependency in the
// configuration.
/** @var \Drupal\search_api\ServerInterface $server */
$server = Server::create([
'id' => 'test_server',
'name' => 'Test server',
'backend' => 'search_api_test',
'backend_config' => [
'dependencies' => [
$dependency_key => [
$dependency_name,
],
],
],
]);
$server
->save();
$server_dependency_key = $server
->getConfigDependencyKey();
$server_dependency_name = $server
->getConfigDependencyName();
// Set the server on the index and save that, too. However, we don't want
// the index enabled, since that would lead to all kinds of overhead which
// is completely irrelevant for this test.
$this->index
->setServer($server);
$this->index
->disable();
$this->index
->save();
// Check that the dependencies were calculated correctly.
$server_dependencies = $server
->getDependencies();
$this
->assertContains($dependency_name, $server_dependencies[$dependency_key], 'Backend dependency correctly inserted');
$index_dependencies = $this->index
->getDependencies();
$this
->assertContains($server_dependency_name, $index_dependencies[$server_dependency_key], 'Server dependency correctly inserted');
// Tell the backend plugin whether it should successfully remove the
// dependency.
$this
->setReturnValue('backend', 'onDependencyRemoval', $remove_dependency);
// Delete the backend's dependency.
$this->dependency
->delete();
// Reload the index and check it's still there.
$this
->reloadIndex();
$this
->assertInstanceOf('Drupal\\search_api\\IndexInterface', $this->index, 'Index not removed');
// Reload the server.
$storage = \Drupal::entityTypeManager()
->getStorage('search_api_server');
$storage
->resetCache();
$server = $storage
->load($server
->id());
if ($remove_dependency) {
$this
->assertInstanceOf('Drupal\\search_api\\ServerInterface', $server, 'Server was not removed');
$this
->assertArrayNotHasKey('dependencies', $server
->get('backend_config'), 'Backend config was adapted');
// @todo Logically, this should not be changed: if the server does not get
// removed, there is no need to adapt the index's configuration.
// However, the way this config dependency cascading is actually
// implemented in
// \Drupal\Core\Config\ConfigManager::getConfigEntitiesToChangeOnDependencyRemoval()
// does not seem to follow that logic, but just computes the complete
// tree of dependencies once and operates generally on the assumption
// that all of them will be deleted. See #2642374.
// $this->assertEquals($server->id(), $this->index->getServerId(), "Index's server was not changed");
}
else {
$this
->assertNull($server, 'Server was removed');
$this
->assertEquals(NULL, $this->index
->getServerId(), 'Index server was changed');
}
}