public function DependencyRemovalTest::testDatasourceDependency in Search API 8
Tests a datasource with a dependency that gets removed.
@dataProvider dependencyTestDataProvider
Parameters
bool $remove_dependency: Whether to remove the dependency from the datasource when the object depended on is deleted.
File
- tests/
src/ Kernel/ ConfigEntity/ DependencyRemovalTest.php, line 237
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 testDatasourceDependency($remove_dependency) {
// Add the datasource to the index and save it. The datasource configuration
// contains the dependencies it will return – in our case, we use the test
// server.
$dependency_key = $this->dependency
->getConfigDependencyKey();
$dependency_name = $this->dependency
->getConfigDependencyName();
// Also index users, to verify that they are unaffected by the processor.
$datasources = \Drupal::getContainer()
->get('search_api.plugin_helper')
->createDatasourcePlugins($this->index, [
'entity:user',
'search_api_test',
], [
'search_api_test' => [
$dependency_key => [
$dependency_name,
],
],
]);
$this->index
->setDatasources($datasources);
$this->index
->save();
// Check the dependencies were calculated correctly.
$dependencies = $this->index
->getDependencies();
$this
->assertContains($dependency_name, $dependencies[$dependency_key], 'Datasource dependency correctly inserted');
// Tell the datasource plugin whether it should successfully remove the
// dependency.
$this
->setReturnValue('datasource', 'onDependencyRemoval', $remove_dependency);
// Delete the datasource'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');
// Make sure the dependency has been removed, one way or the other.
$dependencies = $this->index
->getDependencies();
$dependencies += [
$dependency_key => [],
];
$this
->assertNotContains($dependency_name, $dependencies[$dependency_key], 'Datasource dependency removed from index');
// Depending on whether the plugin should have removed the dependency or
// not, make sure the right action was taken.
$datasources = $this->index
->getDatasources();
if ($remove_dependency) {
$this
->assertArrayHasKey('search_api_test', $datasources, 'Datasource not removed');
$this
->assertEmpty($datasources['search_api_test']
->getConfiguration(), 'Datasource settings adapted');
}
else {
$this
->assertArrayNotHasKey('search_api_test', $datasources, 'Datasource removed');
}
}